Skip to content

Commit

Permalink
Merge branch 'main' into fix-claim-all
Browse files Browse the repository at this point in the history
  • Loading branch information
SegueII committed Nov 15, 2024
2 parents 9474c17 + 026af6b commit 846319a
Show file tree
Hide file tree
Showing 15 changed files with 491 additions and 87 deletions.
2 changes: 1 addition & 1 deletion bindings/bin/gov_deployed.hex

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion bindings/bin/l1staking_deployed.hex

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion bindings/bin/l2staking_deployed.hex

Large diffs are not rendered by default.

87 changes: 85 additions & 2 deletions bindings/bindings/gov.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions bindings/bindings/gov_more.go

Large diffs are not rendered by default.

35 changes: 33 additions & 2 deletions bindings/bindings/l1staking.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions bindings/bindings/l1staking_more.go

Large diffs are not rendered by default.

125 changes: 99 additions & 26 deletions bindings/bindings/l2staking.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions bindings/bindings/l2staking_more.go

Large diffs are not rendered by default.

19 changes: 16 additions & 3 deletions contracts/contracts/l1/staking/L1Staking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ contract L1Staking is IL1Staking, Staking, OwnableUpgradeable, ReentrancyGuardUp
/// @notice challenge deposit value
uint256 public challengeDeposit;

/// @notice nonce of staking L1 => L2 msg
uint256 public nonce;

/**********************
* Function Modifiers *
**********************/
Expand Down Expand Up @@ -219,6 +222,9 @@ contract L1Staking is IL1Staking, Staking, OwnableUpgradeable, ReentrancyGuardUp

uint256 valueSum;
for (uint256 i = 0; i < sequencers.length; i++) {
if (sequencers[i] == address(0)) {
continue;
}
if (withdrawals[sequencers[i]] > 0) {
delete withdrawals[sequencers[i]];
valueSum += stakingValue;
Expand Down Expand Up @@ -475,9 +481,10 @@ contract L1Staking is IL1Staking, Staking, OwnableUpgradeable, ReentrancyGuardUp
MESSENGER.sendMessage(
address(OTHER_STAKING),
0,
abi.encodeCall(IL2Staking.addStaker, (add)),
abi.encodeCall(IL2Staking.addStaker, (nonce, add)),
gasLimitAddStaker
);
nonce = nonce + 1;
}

/// @notice remove stakers
Expand All @@ -486,16 +493,22 @@ contract L1Staking is IL1Staking, Staking, OwnableUpgradeable, ReentrancyGuardUp
MESSENGER.sendMessage(
address(OTHER_STAKING),
0,
abi.encodeCall(IL2Staking.removeStakers, (remove)),
abi.encodeCall(IL2Staking.removeStakers, (nonce, remove)),
gasLimitRemoveStakers
);
nonce = nonce + 1;
}

/// @notice clean staker store
function _cleanStakerStore() internal {
uint256 i = 0;
while (i < deleteList.length) {
if (deleteableHeight[deleteList[i]] <= block.number) {
if (deleteList[i] == address(0)) {
// clean deleteList
delete deleteableHeight[deleteList[i]];
deleteList[i] = deleteList[deleteList.length - 1];
deleteList.pop();
} else if (deleteableHeight[deleteList[i]] <= block.number) {
// clean stakerSet
delete stakerSet[stakerIndexes[deleteList[i]] - 1];
delete stakerIndexes[deleteList[i]];
Expand Down
36 changes: 27 additions & 9 deletions contracts/contracts/l2/staking/Gov.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ contract Gov is IGov, OwnableUpgradeable {
uint256 public override currentProposalID;

/// @notice the start index of undeleted proposals
uint256 private undeletedProposalStart;
uint256 public undeletedProposalStart;

/// @notice proposal data
mapping(uint256 proposalID => ProposalData) public proposalData;
Expand All @@ -61,6 +61,9 @@ contract Gov is IGov, OwnableUpgradeable {
/// @notice proposal voter info
mapping(uint256 proposalID => EnumerableSetUpgradeable.AddressSet) internal votes;

/// @notice latest executed proposal ID
uint256 public latestExecutedProposalID;

/**********************
* Function Modifiers *
**********************/
Expand Down Expand Up @@ -146,6 +149,7 @@ contract Gov is IGov, OwnableUpgradeable {
/// @notice vote a proposal
function vote(uint256 proposalID) external onlySequencer {
require(proposalID <= currentProposalID, "invalid proposalID");
require(proposalID > latestExecutedProposalID, "expired proposalID");
require(proposalID >= undeletedProposalStart, "proposal pruned");
uint256 expirationTime = proposalInfos[proposalID].expirationTime;
require(
Expand All @@ -160,6 +164,7 @@ contract Gov is IGov, OwnableUpgradeable {
}
}

/// @notice set voting duration
function setVotingDuration(uint256 _votingDuration) external onlyOwner {
require(_votingDuration > 0 && _votingDuration != votingDuration, "invalid new proposal voting duration");
uint256 _oldVotingDuration = votingDuration;
Expand All @@ -180,14 +185,33 @@ contract Gov is IGov, OwnableUpgradeable {
_executeProposal(proposalID);
}

/// @notice execute a passed proposal
/// @param deleteTo last proposal ID to delete
function cleanUpExpiredProposals(uint256 deleteTo) external {
require(deleteTo < latestExecutedProposalID, "only allow to delete the proposal befor latest passed proposal");
// when a proposal is passed, the previous proposals will be invalidated and deleted
for (uint256 i = undeletedProposalStart; i <= deleteTo; i++) {
delete proposalData[i];
delete proposalInfos[i];
delete votes[i];
}
undeletedProposalStart = deleteTo + 1;
}

/*************************
* Public View Functions *
*************************/

/// @notice return proposal status. {finished, passed, executed}
function proposalStatus(uint256 proposalID) public view returns (bool, bool, bool) {
require(proposalID <= currentProposalID, "invalid proposalID");
require(proposalID >= latestExecutedProposalID, "expired proposal");
require(proposalID >= undeletedProposalStart, "proposal pruned");

if (proposalID == latestExecutedProposalID) {
return (true, true, true);
}

bool executed = proposalInfos[proposalID].executed;
uint256 expirationTime = proposalInfos[proposalID].expirationTime;
return (
Expand All @@ -210,6 +234,8 @@ contract Gov is IGov, OwnableUpgradeable {

/// @notice execute a passed proposal
function _executeProposal(uint256 proposalID) internal {
latestExecutedProposalID = proposalID;

if (batchBlockInterval != proposalData[proposalID].batchBlockInterval) {
uint256 _oldValue = batchBlockInterval;
batchBlockInterval = proposalData[proposalID].batchBlockInterval;
Expand All @@ -228,14 +254,6 @@ contract Gov is IGov, OwnableUpgradeable {
}
proposalInfos[proposalID].executed = true;

// when a proposal is passed, the previous proposals will be invalidated and deleted
for (uint256 i = undeletedProposalStart; i < proposalID; i++) {
delete proposalData[i];
delete proposalInfos[i];
delete votes[i];
}
undeletedProposalStart = proposalID;

emit ProposalExecuted(proposalID, batchBlockInterval, batchTimeout, rollupEpoch);
}

Expand Down
10 changes: 6 additions & 4 deletions contracts/contracts/l2/staking/IL2Staking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,14 @@ interface IL2Staking {
*****************************/

/// @notice add staker, sync from L1
/// @param add staker to add. {addr, tmKey, blsKey}
function addStaker(Types.StakerInfo calldata add) external;
/// @param nonce msg nonce
/// @param add staker to add. {addr, tmKey, blsKey}
function addStaker(uint256 nonce, Types.StakerInfo calldata add) external;

/// @notice remove stakers, sync from L1
/// @param remove staker to remove
function removeStakers(address[] calldata remove) external;
/// @param nonce msg nonce
/// @param remove staker to remove
function removeStakers(uint256 nonce, address[] calldata remove) external;

/// @notice setCommissionRate set delegate commission percentage
/// @param commission commission percentage, denominator is 100
Expand Down
73 changes: 69 additions & 4 deletions contracts/contracts/l2/staking/L2Staking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ contract L2Staking is IL2Staking, Staking, OwnableUpgradeable, ReentrancyGuardUp
/// @notice delegator's undelegations
mapping(address delegator => Undelegation[]) public undelegations;

/// @notice nonce of staking L1 => L2 msg
uint256 public nonce;

/**********************
* Function Modifiers *
**********************/
Expand All @@ -94,6 +97,12 @@ contract L2Staking is IL2Staking, Staking, OwnableUpgradeable, ReentrancyGuardUp
_;
}

/// @notice check nonce
modifier checkNonce(uint256 _nonce) {
require(_nonce == nonce, "invalid nonce");
_;
}

/***************
* Constructor *
***************/
Expand Down Expand Up @@ -154,8 +163,10 @@ contract L2Staking is IL2Staking, Staking, OwnableUpgradeable, ReentrancyGuardUp
************************/

/// @notice add staker, sync from L1
/// @param add staker to add. {addr, tmKey, blsKey}
function addStaker(Types.StakerInfo calldata add) external onlyOtherStaking {
/// @param _nonce msg nonce
/// @param add staker to add. {addr, tmKey, blsKey}
function addStaker(uint256 _nonce, Types.StakerInfo calldata add) external onlyOtherStaking checkNonce(_nonce) {
nonce = _nonce + 1;
if (stakerRankings[add.addr] == 0) {
stakerAddresses.push(add.addr);
stakerRankings[add.addr] = stakerAddresses.length;
Expand All @@ -169,8 +180,62 @@ contract L2Staking is IL2Staking, Staking, OwnableUpgradeable, ReentrancyGuardUp
}

/// @notice remove stakers, sync from L1. If new sequencer set is nil, layer2 will stop producing blocks
/// @param remove staker to remove
function removeStakers(address[] calldata remove) external onlyOtherStaking {
/// @param _nonce msg nonce
/// @param remove staker to remove
function removeStakers(uint256 _nonce, address[] calldata remove) external onlyOtherStaking checkNonce(_nonce) {
nonce = _nonce + 1;
bool updateSequencerSet = false;
for (uint256 i = 0; i < remove.length; i++) {
if (stakerRankings[remove[i]] <= latestSequencerSetSize) {
updateSequencerSet = true;
}

if (stakerRankings[remove[i]] > 0) {
// update stakerRankings
for (uint256 j = stakerRankings[remove[i]] - 1; j < stakerAddresses.length - 1; j++) {
stakerAddresses[j] = stakerAddresses[j + 1];
stakerRankings[stakerAddresses[j]] -= 1;
}
stakerAddresses.pop();
delete stakerRankings[remove[i]];

// update candidateNumber
if (stakerDelegations[remove[i]] > 0) {
candidateNumber -= 1;
}
}

delete stakers[remove[i]];
}
emit StakerRemoved(remove);

if (updateSequencerSet) {
_updateSequencerSet();
}
}

/// @notice add staker. Only can be called when a serious bug causes L1 and L2 data to be out of sync
/// @param _nonce msg nonce
/// @param add staker to add. {addr, tmKey, blsKey}
function emergencyAddStaker(uint256 _nonce, Types.StakerInfo calldata add) external onlyOwner checkNonce(_nonce) {
nonce = _nonce + 1;
if (stakerRankings[add.addr] == 0) {
stakerAddresses.push(add.addr);
stakerRankings[add.addr] = stakerAddresses.length;
}
stakers[add.addr] = add;
emit StakerAdded(add.addr, add.tmKey, add.blsKey);

if (!rewardStarted && stakerAddresses.length <= sequencerSetMaxSize) {
_updateSequencerSet();
}
}

/// @notice remove stakers. Only can be called when a serious bug causes L1 and L2 data to be out of sync
/// @param _nonce msg nonce
/// @param remove staker to remove
function emergencyRemoveStakers(uint256 _nonce, address[] calldata remove) external onlyOwner checkNonce(_nonce) {
nonce = _nonce + 1;
bool updateSequencerSet = false;
for (uint256 i = 0; i < remove.length; i++) {
if (stakerRankings[remove[i]] <= latestSequencerSetSize) {
Expand Down
Loading

0 comments on commit 846319a

Please sign in to comment.