From 45d03ff7c5046aabd71e5889e10ca09179ddd074 Mon Sep 17 00:00:00 2001 From: Luca Georges Francois Date: Tue, 9 Aug 2022 10:32:07 -0700 Subject: [PATCH] fix: update random number generation Signed-off-by: Luca Georges Francois --- src/contracts/StakingContract.sol | 2 +- src/test/StakingContract.t.sol | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/contracts/StakingContract.sol b/src/contracts/StakingContract.sol index ce2b8f1..4a1a257 100644 --- a/src/contracts/StakingContract.sol +++ b/src/contracts/StakingContract.sol @@ -788,7 +788,7 @@ contract StakingContract { ) internal { uint256 operatorCount = _operators.value.length; uint8 optimusPrime = _getClosestPrimeAbove(uint8(operatorCount)); - bytes32 blockHash = blockhash(block.number); // weak random number as it's not a security issue + bytes32 blockHash = blockhash(block.number - 1); // weak random number as it's not a security issue ValidatorAllocationCache[] memory vd = new ValidatorAllocationCache[](operatorCount); diff --git a/src/test/StakingContract.t.sol b/src/test/StakingContract.t.sol index ba5e5e7..8fb06dd 100644 --- a/src/test/StakingContract.t.sol +++ b/src/test/StakingContract.t.sol @@ -1509,7 +1509,8 @@ contract StakingContractDistributionTest is DSTestPlus { } for (uint256 i; i < depositCount; ) { - vm.roll(i); + // +1 To prevent underflow. + vm.roll(i + 1); uint256 availableKeys = stakingContract.getAvailableValidatorCount(); salt = keccak256(abi.encode(salt)); uint256 newDeposits = (uint8(salt[0]) % 31) + 1;