Skip to content
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 10 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion script/JokeraceEligibility.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ contract DeployImplementation is Script {
address deployer = vm.rememberKey(privKey);
vm.startBroadcast(deployer);

implementation = new JokeraceEligibility{ salt: SALT}(version);
implementation = new JokeraceEligibility{ salt: SALT }(version);

vm.stopBroadcast();

Expand Down
69 changes: 47 additions & 22 deletions src/JokeraceEligibility.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

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


/*//////////////////////////////////////////////////////////////
PUBLIC CONSTANTS
Expand Down Expand Up @@ -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
Expand All @@ -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;
}

Expand All @@ -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];
}
}

Expand All @@ -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;
Expand All @@ -187,6 +200,9 @@ contract JokeraceEligibility is HatsEligibilityModule {
}
}

currentContest = address(contest);
nextContest = address(0);
emit ElectionResultsPulled(address(contest));
return true;
}

Expand All @@ -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
Expand All @@ -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);
}

/*//////////////////////////////////////////////////////////////
Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test added

|| (
nextContest != address(0)
&& GovernorCountingSimple(payable(nextContest)).state() == Governor.ContestState.Canceled
);
}

/*//////////////////////////////////////////////////////////////
Expand Down
Loading
Loading