Skip to content

Commit

Permalink
feat: configurable validatorAfkBlocks (#665)
Browse files Browse the repository at this point in the history
Co-authored-by: Henry <[email protected]>
  • Loading branch information
gzeoneth and godzillaba authored Jun 6, 2024
1 parent 4926ab5 commit 6b42a38
Show file tree
Hide file tree
Showing 16 changed files with 558 additions and 73 deletions.
14 changes: 14 additions & 0 deletions contracts/src/rollup/IRollupAdmin.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ interface IRollupAdmin {
/// @dev A new minimum assertion period was set
event MinimumAssertionPeriodSet(uint256 newPeriod);

/// @dev A new validator afk blocks was set
event ValidatorAfkBlocksSet(uint256 newPeriod);

/// @dev New confirm period blocks was set
event ConfirmPeriodBlocksSet(uint64 newConfirmPeriod);

Expand Down Expand Up @@ -115,6 +118,17 @@ interface IRollupAdmin {
*/
function setMinimumAssertionPeriod(uint256 newPeriod) external;

/**
* @notice Set validator afk blocks for the rollup
* @param newAfkBlocks new number of blocks before a validator is considered afk (0 to disable)
* @dev ValidatorAfkBlocks is the number of blocks since the last confirmed
* assertion (or its first child) before the validator whitelist is removed.
* It's important that this time is greater than the max amount of time it can take to
* to confirm an assertion via the normal method. Therefore we need it to be greater
* than max(2* confirmPeriod, 2 * challengePeriod) with some additional margin.
*/
function setValidatorAfkBlocks(uint64 newAfkBlocks) external;

/**
* @notice Set number of blocks until a assertion is considered confirmed
* @param newConfirmPeriod new number of blocks until a assertion is confirmed
Expand Down
2 changes: 2 additions & 0 deletions contracts/src/rollup/IRollupCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ interface IRollupCore is IAssertionChain {

function confirmPeriodBlocks() external view returns (uint64);

function validatorAfkBlocks() external view returns (uint64);

function chainId() external view returns (uint256);

function baseStake() external view returns (uint256);
Expand Down
18 changes: 18 additions & 0 deletions contracts/src/rollup/RollupAdminLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ contract RollupAdminLogic is RollupCore, IRollupAdmin, DoubleLogicUUPSUpgradeabl
wasmModuleRoot = config.wasmModuleRoot;
// A little over 15 minutes
minimumAssertionPeriod = 75;
// ValidatorAfkBlocks is defaulted to 28 days assuming a 12 seconds block time.
// Since it can take 14 days under normal circumstances to confirm an assertion, this means
// the validators will have been inactive for a further 14 days before the whitelist is removed.
validatorAfkBlocks = 201600;
challengeGracePeriodBlocks = config.challengeGracePeriodBlocks;

// loser stake is now sent directly to loserStakeEscrow, it must not
Expand Down Expand Up @@ -214,6 +218,20 @@ contract RollupAdminLogic is RollupCore, IRollupAdmin, DoubleLogicUUPSUpgradeabl
// previously: emit OwnerFunctionCalled(8);
}

/**
* @notice Set validator afk blocks for the rollup
* @param newAfkBlocks new number of blocks before a validator is considered afk (0 to disable)
* @dev ValidatorAfkBlocks is the number of blocks since the last confirmed
* assertion (or its first child) before the validator whitelist is removed.
* It's important that this time is greater than the max amount of time it can take to
* to confirm an assertion via the normal method. Therefore we need it to be greater
* than max(2* confirmPeriod, 2 * challengePeriod) with some additional margin.
*/
function setValidatorAfkBlocks(uint64 newAfkBlocks) external override {
validatorAfkBlocks = newAfkBlocks;
emit ValidatorAfkBlocksSet(newAfkBlocks);
}

/**
* @notice Set number of blocks until a assertion is considered confirmed
* @param newConfirmPeriod new number of blocks
Expand Down
2 changes: 2 additions & 0 deletions contracts/src/rollup/RollupCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ abstract contract RollupCore is IRollupCore, PausableUpgradeable {
// These 4 config should be stored into the prev and not used directly
// An assertion can be confirmed after confirmPeriodBlocks when it is unchallenged
uint64 public confirmPeriodBlocks;
// The validator whitelist can be dropped permissionlessly once the last confirmed assertion or its first child is at least validatorAfkBlocks old
uint64 public validatorAfkBlocks;

// ------------------------------
// STAKING
Expand Down
20 changes: 4 additions & 16 deletions contracts/src/rollup/RollupUserLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,17 @@ contract RollupUserLogic is RollupCore, UUPSNotUpgradeable, IRollupUser {
return deployTimeChainId != block.chainid;
}

/**
* @notice Number of blocks since the last confirmed assertion before the validator whitelist is removed
* This is 28 days assuming a 12 seconds block time. Since it can take 14 days under normal
* circumstances to confirm an assertion, this means that validators will have been inactive for
* a further 14 days before the validator whitelist is removed.
*
* It's important that this time is greater than the max amount of time it can take to
* to confirm an assertion via the normal method. Therefore we need it to be greater
* than max(2* confirmPeriod, 2 * challengePeriod). With some additional margin
* It's expected that initially confirm and challenge periods are set to 1 week, so 4 weeks
* should give a two weeks of margin before the validators are considered afk.
*/
uint256 public constant VALIDATOR_AFK_BLOCKS = 201600;

function _validatorIsAfk() internal view returns (bool) {
AssertionNode memory latestConfirmedAssertion = getAssertionStorage(latestConfirmed());
uint256 _validatorAfkBlocks = validatorAfkBlocks; // cache and cast to uint256 to prevent overflow
if (_validatorAfkBlocks == 0) return false;
if (latestConfirmedAssertion.createdAtBlock == 0) return false;
// We consider the validator is gone if the last known assertion is older than VALIDATOR_AFK_BLOCKS
// Which is either the latest confirmed assertion or the first child of the latest confirmed assertion
if (latestConfirmedAssertion.firstChildBlock > 0) {
return latestConfirmedAssertion.firstChildBlock + VALIDATOR_AFK_BLOCKS < block.number;
return latestConfirmedAssertion.firstChildBlock + _validatorAfkBlocks < block.number;
}
return latestConfirmedAssertion.createdAtBlock + VALIDATOR_AFK_BLOCKS < block.number;
return latestConfirmedAssertion.createdAtBlock + _validatorAfkBlocks < block.number;
}

function removeWhitelistAfterFork() external {
Expand Down
23 changes: 22 additions & 1 deletion contracts/test/Rollup.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,28 @@ contract RollupTest is Test {

function testSuccessRemoveWhitelistAfterValidatorAfk() public {
(bytes32 assertionHash,,) = testSuccessConfirmUnchallengedAssertions();
vm.roll(userRollup.getAssertion(assertionHash).createdAtBlock + userRollup.VALIDATOR_AFK_BLOCKS() + 1);
vm.roll(userRollup.getAssertion(assertionHash).createdAtBlock + userRollup.validatorAfkBlocks() + 1);
userRollup.removeWhitelistAfterValidatorAfk();
}

function testSuccessSetValidatorAfk(uint32 x) public {
vm.assume(x > 0);
(bytes32 assertionHash,,) = testSuccessConfirmUnchallengedAssertions();
vm.prank(upgradeExecutorAddr);
adminRollup.setValidatorAfkBlocks(x);
vm.roll(userRollup.getAssertion(assertionHash).createdAtBlock + x);
vm.expectRevert("VALIDATOR_NOT_AFK");
userRollup.removeWhitelistAfterValidatorAfk();
vm.roll(block.number + 1);
userRollup.removeWhitelistAfterValidatorAfk();
}

function testSuccessValidatorAfkDisable() public {
(bytes32 assertionHash,,) = testSuccessConfirmUnchallengedAssertions();
vm.prank(upgradeExecutorAddr);
adminRollup.setValidatorAfkBlocks(0); // set 0 to disable
vm.roll(userRollup.getAssertion(assertionHash).createdAtBlock + 1);
vm.expectRevert("VALIDATOR_NOT_AFK");
userRollup.removeWhitelistAfterValidatorAfk();
}

Expand Down
2 changes: 2 additions & 0 deletions contracts/test/signatures/RollupAdminLogic
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"setOwner(address)": "13af4035",
"setSequencerInbox(address)": "4f61f850",
"setValidator(address[],bool[])": "a3ffb772",
"setValidatorAfkBlocks(uint64)": "f112cea3",
"setValidatorWhitelistDisabled(bool)": "a2b4f1d8",
"setWasmModuleRoot(bytes32)": "89384960",
"stakeToken()": "51ed6a30",
Expand All @@ -61,6 +62,7 @@
"upgradeToAndCall(address,bytes)": "4f1ef286",
"validateAssertionHash(bytes32,((bytes32[2],uint64[2]),uint8,bytes32),bytes32,bytes32)": "e51019a6",
"validateConfig(bytes32,(bytes32,uint256,address,uint64,uint64))": "04972af9",
"validatorAfkBlocks()": "e6b3082c",
"validatorWalletCreator()": "bc45e0ae",
"validatorWhitelistDisabled()": "12ab3d3b",
"wasmModuleRoot()": "8ee1a126",
Expand Down
1 change: 1 addition & 0 deletions contracts/test/signatures/RollupCore
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"totalWithdrawableFunds()": "71ef232c",
"validateAssertionHash(bytes32,((bytes32[2],uint64[2]),uint8,bytes32),bytes32,bytes32)": "e51019a6",
"validateConfig(bytes32,(bytes32,uint256,address,uint64,uint64))": "04972af9",
"validatorAfkBlocks()": "e6b3082c",
"validatorWalletCreator()": "bc45e0ae",
"validatorWhitelistDisabled()": "12ab3d3b",
"wasmModuleRoot()": "8ee1a126",
Expand Down
2 changes: 1 addition & 1 deletion contracts/test/signatures/RollupUserLogic
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"VALIDATOR_AFK_BLOCKS()": "4ceccfe5",
"_stakerMap(address)": "e8bd4922",
"addToDeposit(address,address,uint256)": "685f5ecc",
"amountStaked(address)": "ef40a670",
Expand Down Expand Up @@ -53,6 +52,7 @@
"totalWithdrawableFunds()": "71ef232c",
"validateAssertionHash(bytes32,((bytes32[2],uint64[2]),uint8,bytes32),bytes32,bytes32)": "e51019a6",
"validateConfig(bytes32,(bytes32,uint256,address,uint64,uint64))": "04972af9",
"validatorAfkBlocks()": "e6b3082c",
"validatorWalletCreator()": "bc45e0ae",
"validatorWhitelistDisabled()": "12ab3d3b",
"wasmModuleRoot()": "8ee1a126",
Expand Down
1 change: 1 addition & 0 deletions contracts/test/storage/RollupAdminLogic
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
| __gap | uint256[49] | 52 | 0 | 1568 | src/rollup/RollupAdminLogic.sol:RollupAdminLogic |
| chainId | uint256 | 101 | 0 | 32 | src/rollup/RollupAdminLogic.sol:RollupAdminLogic |
| confirmPeriodBlocks | uint64 | 102 | 0 | 8 | src/rollup/RollupAdminLogic.sol:RollupAdminLogic |
| validatorAfkBlocks | uint64 | 102 | 8 | 8 | src/rollup/RollupAdminLogic.sol:RollupAdminLogic |
| baseStake | uint256 | 103 | 0 | 32 | src/rollup/RollupAdminLogic.sol:RollupAdminLogic |
| wasmModuleRoot | bytes32 | 104 | 0 | 32 | src/rollup/RollupAdminLogic.sol:RollupAdminLogic |
| challengeManager | contract IEdgeChallengeManager | 105 | 0 | 20 | src/rollup/RollupAdminLogic.sol:RollupAdminLogic |
Expand Down
1 change: 1 addition & 0 deletions contracts/test/storage/RollupCore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
| __gap | uint256[49] | 52 | 0 | 1568 | src/rollup/RollupCore.sol:RollupCore |
| chainId | uint256 | 101 | 0 | 32 | src/rollup/RollupCore.sol:RollupCore |
| confirmPeriodBlocks | uint64 | 102 | 0 | 8 | src/rollup/RollupCore.sol:RollupCore |
| validatorAfkBlocks | uint64 | 102 | 8 | 8 | src/rollup/RollupCore.sol:RollupCore |
| baseStake | uint256 | 103 | 0 | 32 | src/rollup/RollupCore.sol:RollupCore |
| wasmModuleRoot | bytes32 | 104 | 0 | 32 | src/rollup/RollupCore.sol:RollupCore |
| challengeManager | contract IEdgeChallengeManager | 105 | 0 | 20 | src/rollup/RollupCore.sol:RollupCore |
Expand Down
1 change: 1 addition & 0 deletions contracts/test/storage/RollupUserLogic
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
| __gap | uint256[49] | 52 | 0 | 1568 | src/rollup/RollupUserLogic.sol:RollupUserLogic |
| chainId | uint256 | 101 | 0 | 32 | src/rollup/RollupUserLogic.sol:RollupUserLogic |
| confirmPeriodBlocks | uint64 | 102 | 0 | 8 | src/rollup/RollupUserLogic.sol:RollupUserLogic |
| validatorAfkBlocks | uint64 | 102 | 8 | 8 | src/rollup/RollupUserLogic.sol:RollupUserLogic |
| baseStake | uint256 | 103 | 0 | 32 | src/rollup/RollupUserLogic.sol:RollupUserLogic |
| wasmModuleRoot | bytes32 | 104 | 0 | 32 | src/rollup/RollupUserLogic.sol:RollupUserLogic |
| challengeManager | contract IEdgeChallengeManager | 105 | 0 | 20 | src/rollup/RollupUserLogic.sol:RollupUserLogic |
Expand Down
Loading

0 comments on commit 6b42a38

Please sign in to comment.