-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add transition period for continuity #18
Merged
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
322563d
draft
gershido 3868915
implementation and tests
gershido eb8adaa
format
gershido 7de54f8
emit event on election pulling and update logic of allowed reelection…
gershido 6afd3dd
add tie indication in event and test events
gershido 72c09ec
implementation updates
gershido 6479a0d
documentation and optimizations
gershido db7f0b4
documentation
gershido 7547ce9
optimizations
gershido 28c9614
wrap internal functions
gershido File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,9 @@ contract JokeraceEligibility is HatsEligibilityModule { | |
//////////////////////////////////////////////////////////////*/ | ||
|
||
/// @notice Emitted when a reelection is set | ||
event NewTerm(address NewContest, uint256 newTopK, uint256 newTermEnd); | ||
event NewTerm(address NewContest, uint256 newTopK, uint256 newTermEnd, uint256 newTransitionPeriod); | ||
/// @notice Emitted when election's results are pulled | ||
event ElectionResultsPulled(address NewContest); | ||
|
||
/*////////////////////////////////////////////////////////////// | ||
PUBLIC CONSTANTS | ||
|
@@ -69,9 +71,16 @@ contract JokeraceEligibility is HatsEligibilityModule { | |
//////////////////////////////////////////////////////////////*/ | ||
|
||
/// @notice Current Jokerace contest (election) | ||
address public underlyingContest; | ||
address public currentContest; | ||
/// @notice Next Jokerace contest (election) | ||
address public nextContest; | ||
/// @notice First second after the current term (a unix timestamp) | ||
uint256 public termEnd; | ||
/** | ||
* @notice Period of time after the term end when reelection is allowed. During this period, previous elected members | ||
* are still considered eligible until a new term is set. | ||
*/ | ||
uint256 public transitionPeriod; | ||
/// @notice First K winners of the contest will be eligible | ||
uint256 public topK; | ||
/// @notice Eligible wearers according to each contest | ||
|
@@ -86,19 +95,22 @@ contract JokeraceEligibility is HatsEligibilityModule { | |
* @dev Only callable by the hats-module factory. Since the factory only calls this function during a new deployment, | ||
* this ensures it can only be called once per instance, and that the implementation contract is never initialized. | ||
* @param _initData Packed initialization data with the following parameters: | ||
* _underlyingContest - Jokerace contest. The contest must have down-voting disabled and sorting enabled. | ||
* _contest - Jokerace contest. The contest must have down-voting disabled and sorting enabled. | ||
* _termEnd - Final second of the current term (a unix timestamp) | ||
* _transitionPeriod - Period of time after the term end when calling reelection is allowed and previous elected are | ||
* still eligibile as long as reelection was not called | ||
* _topK - First K winners of the contest will be eligible | ||
*/ | ||
function _setUp(bytes calldata _initData) internal override { | ||
(address payable _underlyingContest, uint256 _termEnd, uint256 _topK) = | ||
abi.decode(_initData, (address, uint256, uint256)); | ||
(address payable _contest, uint256 _termEnd, uint256 _transitionPeriod, uint256 _topK) = | ||
abi.decode(_initData, (address, uint256, uint256, uint256)); | ||
|
||
_checkContestSupportsSorting(GovernorCountingSimple(_underlyingContest)); | ||
_checkContestSupportsSorting(GovernorCountingSimple(_contest)); | ||
|
||
// initialize the mutable state vars | ||
underlyingContest = _underlyingContest; | ||
nextContest = _contest; | ||
termEnd = _termEnd; | ||
transitionPeriod = _transitionPeriod; | ||
topK = _topK; | ||
} | ||
|
||
|
@@ -125,8 +137,8 @@ contract JokeraceEligibility is HatsEligibilityModule { | |
returns (bool eligible, bool standing) | ||
{ | ||
standing = true; | ||
if (block.timestamp < termEnd) { | ||
eligible = eligibleWearersPerContest[_wearer][underlyingContest]; | ||
if (block.timestamp < termEnd + transitionPeriod) { | ||
eligible = eligibleWearersPerContest[_wearer][currentContest]; | ||
} | ||
} | ||
|
||
|
@@ -141,33 +153,34 @@ contract JokeraceEligibility is HatsEligibilityModule { | |
* rejected. | ||
*/ | ||
function pullElectionResults() public returns (bool success) { | ||
GovernorCountingSimple currentContest = GovernorCountingSimple(payable(underlyingContest)); | ||
GovernorCountingSimple contest = GovernorCountingSimple(payable(nextContest)); | ||
|
||
if (currentContest.state() != Governor.ContestState.Completed) { | ||
if (contest.state() != Governor.ContestState.Completed) { | ||
revert JokeraceEligibility_ContestNotCompleted(); | ||
} | ||
|
||
uint256 k = topK; | ||
uint256 winningProposalsCount; | ||
for (uint256 currentRank = 1; currentRank <= k;) { | ||
try currentContest.getRankIndex(currentRank) returns (uint256 rankIndex) { | ||
try contest.getRankIndex(currentRank) returns (uint256 rankIndex) { | ||
// get the score of the curent rank (amount of 'for' votes) | ||
uint256 forVotesOfCurrentRank = currentContest.sortedRanks(rankIndex); | ||
uint256 forVotesOfCurrentRank = contest.sortedRanks(rankIndex); | ||
// get the proposal IDs with the current score | ||
uint256[] memory proposalsOfCurrentRank = currentContest.getProposalsWithThisManyForVotes(forVotesOfCurrentRank); | ||
uint256[] memory proposalsOfCurrentRank = contest.getProposalsWithThisManyForVotes(forVotesOfCurrentRank); | ||
uint256 numProposalsOfCurrentRank = proposalsOfCurrentRank.length; | ||
winningProposalsCount += numProposalsOfCurrentRank; | ||
|
||
// if there's a tie | ||
if (winningProposalsCount > k) { | ||
termEnd = block.timestamp; // update the term end so that reelection will be immediately possible | ||
nextContest = address(0); | ||
return false; | ||
} | ||
|
||
// get the authors of the proposals and update their eligibility | ||
for (uint256 proposalIndex; proposalIndex < numProposalsOfCurrentRank;) { | ||
address candidate = _getCandidate(currentContest, proposalsOfCurrentRank[proposalIndex]); | ||
eligibleWearersPerContest[candidate][address(currentContest)] = true; | ||
address candidate = _getCandidate(contest, proposalsOfCurrentRank[proposalIndex]); | ||
eligibleWearersPerContest[candidate][address(contest)] = true; | ||
|
||
unchecked { | ||
++proposalIndex; | ||
|
@@ -187,6 +200,9 @@ contract JokeraceEligibility is HatsEligibilityModule { | |
} | ||
} | ||
|
||
currentContest = address(contest); | ||
nextContest = address(0); | ||
emit ElectionResultsPulled(address(contest)); | ||
return true; | ||
} | ||
|
||
|
@@ -195,12 +211,12 @@ contract JokeraceEligibility is HatsEligibilityModule { | |
* @dev Only the module's admin/s have the permission to set a reelection. If an admin is not set at the module | ||
* creation, then any admin of hatId is considered an admin by the module. | ||
*/ | ||
function reelection(address newUnderlyingContest, uint256 newTermEnd, uint256 newTopK) public { | ||
function reelection(address newContest, uint256 newTermEnd, uint256 newTransitionPeriod, uint256 newTopK) public { | ||
if (!reelectionAllowed()) { | ||
revert JokeraceEligibility_TermNotCompleted(); | ||
} | ||
|
||
_checkContestSupportsSorting(GovernorCountingSimple(payable(newUnderlyingContest))); | ||
_checkContestSupportsSorting(GovernorCountingSimple(payable(newContest))); | ||
|
||
uint256 admin = ADMIN_HAT(); | ||
// if an admin hat is not set, then the Hats admins of hatId are granted the permission to set a reelection | ||
|
@@ -214,11 +230,12 @@ contract JokeraceEligibility is HatsEligibilityModule { | |
} | ||
} | ||
|
||
underlyingContest = newUnderlyingContest; | ||
nextContest = newContest; | ||
termEnd = newTermEnd; | ||
transitionPeriod = newTransitionPeriod; | ||
topK = newTopK; | ||
|
||
emit NewTerm(newUnderlyingContest, newTopK, newTermEnd); | ||
emit NewTerm(newContest, newTopK, newTermEnd, newTransitionPeriod); | ||
} | ||
|
||
/*////////////////////////////////////////////////////////////// | ||
|
@@ -227,8 +244,16 @@ contract JokeraceEligibility is HatsEligibilityModule { | |
|
||
/// @notice Check if setting a new election is allowed. | ||
function reelectionAllowed() public view returns (bool allowed) { | ||
allowed = block.timestamp >= termEnd | ||
|| GovernorCountingSimple(payable(underlyingContest)).state() == Governor.ContestState.Canceled; | ||
// if the current term has ended | ||
allowed = block.timestamp >= termEnd // or if the current contest was canceled | ||
|| ( | ||
currentContest != address(0) | ||
spengrah marked this conversation as resolved.
Show resolved
Hide resolved
|
||
&& GovernorCountingSimple(payable(currentContest)).state() == Governor.ContestState.Canceled | ||
) // or if we're in a transition period, and the next contest was canceled | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a test for this new logic? I might be missing it, but I don't see one There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test added |
||
|| ( | ||
nextContest != address(0) | ||
&& GovernorCountingSimple(payable(nextContest)).state() == Governor.ContestState.Canceled | ||
); | ||
} | ||
|
||
/*////////////////////////////////////////////////////////////// | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would like to see a test for this event