Skip to content

Commit

Permalink
feat: add getBlocksTillWithdrawAllowed query + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shaspitz committed Apr 25, 2024
1 parent f211aa0 commit a23816c
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 4 deletions.
19 changes: 19 additions & 0 deletions contracts-abi/abi/ValidatorRegistry.abi
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@
"inputs": [],
"stateMutability": "nonpayable"
},
{
"type": "function",
"name": "getBlocksTillWithdrawAllowed",
"inputs": [
{
"name": "valBLSPubKey",
"type": "bytes",
"internalType": "bytes"
}
],
"outputs": [
{
"name": "",
"type": "uint256",
"internalType": "uint256"
}
],
"stateMutability": "view"
},
{
"type": "function",
"name": "getNumberOfStakedValidators",
Expand Down
33 changes: 32 additions & 1 deletion contracts-abi/clients/ValidatorRegistry/ValidatorRegistry.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions contracts/contracts/ValidatorRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ contract ValidatorRegistry is OwnableUpgradeable, ReentrancyGuardUpgradeable {
return unstakingBalances[valBLSPubKey];
}

function getBlocksTillWithdrawAllowed(bytes calldata valBLSPubKey) external view returns (uint256) {
require(unstakeBlockNums[valBLSPubKey] > 0, "Unstake must be initiated to check withdrawal eligibility");
uint256 blocksSinceUnstakeInitiated = block.number - unstakeBlockNums[valBLSPubKey];
return blocksSinceUnstakeInitiated > unstakePeriodBlocks ? 0 : unstakePeriodBlocks - blocksSinceUnstakeInitiated;
}

/// @return numStakedValidators uint number of currently staked validators.
/// @return stakedValsetVersion uint version of the staked valset at the time of query.
function getNumberOfStakedValidators() external view returns (uint256, uint256) {
Expand Down
6 changes: 5 additions & 1 deletion contracts/scripts/DeployScripts.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,14 @@ contract DeployValidatorRegistry is Script {
function run() external {
vm.startBroadcast();

// 7000 blocks @ 200ms per block = 23.3 min. This allows two L1 epochs (finalization time) + settlement buffer,
// to pass between validator unstake initiation and withdrawal.
uint256 unstakePeriodBlocks = 7000;

// Can later be upgraded with https://docs.openzeppelin.com/upgrades-plugins/1.x/api-foundry-upgrades#Upgrades-upgradeProxy-address-string-bytes-
address proxy = Upgrades.deployUUPSProxy(
"ValidatorRegistry.sol",
abi.encodeCall(ValidatorRegistry.initialize, (3 ether, 64, msg.sender))
abi.encodeCall(ValidatorRegistry.initialize, (3 ether, unstakePeriodBlocks, msg.sender))
);
console.log("ValidatorRegistry UUPS proxy deployed to:", address(proxy));

Expand Down
26 changes: 24 additions & 2 deletions contracts/scripts/ValidatorExampleScript.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,33 @@ abstract contract ExampleScript is Script {
console.log("--------------------");
}

uint256 numStakedValidators = _validatorRegistry.getNumberOfStakedValidators();
(uint256 numStakedValidators, uint256 stakedValsetVersion) = _validatorRegistry.getNumberOfStakedValidators();
console.log("Num Staked Validators:", numStakedValidators);
console.log("Staked Valset Version from len query:", stakedValsetVersion);
if (numStakedValidators == 0) {
return;
}
bytes[] memory vals = _validatorRegistry.getStakedValidators(0, numStakedValidators);
bytes[] memory vals;
(vals, stakedValsetVersion) = _validatorRegistry.getStakedValidators(0, numStakedValidators);
for (uint i = 0; i < vals.length; i++) {
console.log("Staked validator from batch query: ");
console.logBytes(vals[i]);
}
console.log("Staked Valset Version from getStakedValidators query:", stakedValsetVersion);
}

function checkWithdrawal(bytes[] memory blsKeys) public view {
console.log("--------------------");
console.log("Checking Withdrawal related state...");
console.log("--------------------");
for (uint i = 0; i < blsKeys.length; i++) {
uint256 blocksTillWithdrawAllowed = _validatorRegistry.getBlocksTillWithdrawAllowed(blsKeys[i]);
console.log("--------------------");
console.log("BLS Key: ");
console.logBytes(blsKeys[i]);
console.log("Blocks till Withdraw Allowed:", blocksTillWithdrawAllowed);
console.log("--------------------");
}
}
}

Expand Down Expand Up @@ -87,6 +104,9 @@ contract UnstakeExample is ExampleScript {
checkStaking(validators);

console.log("Balance of 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266:", defaultEOA.balance);

checkWithdrawal(validators);

vm.stopBroadcast();
}
}
Expand All @@ -105,6 +125,8 @@ contract WithdrawExample is ExampleScript {

checkStaking(validators);

checkWithdrawal(validators);

_validatorRegistry.withdraw(validators);
console.log("Withdraw initiated");

Expand Down
Loading

0 comments on commit a23816c

Please sign in to comment.