Skip to content

Commit

Permalink
fix: Allow ETH deposits exceeding the required amount to prevent grie…
Browse files Browse the repository at this point in the history
…fing
  • Loading branch information
dgusakov committed Aug 20, 2024
1 parent 7c3df56 commit b17b4eb
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/CSModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ contract CSModule is
);

if (
msg.value != accounting.getBondAmountByKeysCount(keysCount, curveId)
msg.value < accounting.getBondAmountByKeysCount(keysCount, curveId)
) {
revert InvalidAmount();
}
Expand Down Expand Up @@ -384,7 +384,7 @@ contract CSModule is
_onlyNodeOperatorManager(nodeOperatorId);

if (
msg.value !=
msg.value <
accounting.getRequiredBondForNextKeys(nodeOperatorId, keysCount)
) {
revert InvalidAmount();
Expand Down
62 changes: 62 additions & 0 deletions test/CSModule.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,44 @@ contract CSMAddNodeOperatorETH is CSMCommon {
assertEq(csm.getNonce(), nonce + 1);
}

function test_AddNodeOperatorETH_withMoreEthThanRequired()
public
assertInvariants
{
uint16 keysCount = 1;
(bytes memory keys, bytes memory signatures) = keysSignatures(
keysCount
);
uint256 deposit = BOND_SIZE + 1 ether;
vm.deal(nodeOperator, deposit);
uint256 nonce = csm.getNonce();

{
vm.expectEmit(true, true, true, true, address(csm));
emit CSModule.NodeOperatorAdded(0, nodeOperator, nodeOperator);
vm.expectEmit(true, true, true, true, address(csm));
emit IStakingModule.SigningKeyAdded(0, keys);
vm.expectEmit(true, true, true, true, address(csm));
emit CSModule.TotalSigningKeysCountChanged(0, 1);
}

vm.prank(nodeOperator);
csm.addNodeOperatorETH{ value: deposit }(
keysCount,
keys,
signatures,
NodeOperatorManagementProperties({
managerAddress: address(0),
rewardAddress: address(0),
extendedManagerPermissions: false
}),
new bytes32[](0),
address(0)
);
assertEq(csm.getNodeOperatorsCount(), 1);
assertEq(csm.getNonce(), nonce + 1);
}

function test_AddNodeOperatorETH_withCustomAddresses()
public
assertInvariants
Expand Down Expand Up @@ -1497,6 +1535,30 @@ contract CSMAddValidatorKeys is CSMCommon {
assertEq(csm.getNonce(), nonce + 1);
}

function test_AddValidatorKeysETH_withMoreEthThanRequired()
public
assertInvariants
brutalizeMemory
{
uint256 noId = createNodeOperator();
(bytes memory keys, bytes memory signatures) = keysSignatures(1, 1);

uint256 required = accounting.getRequiredBondForNextKeys(0, 1);
uint256 deposit = required + 1 ether;
vm.deal(nodeOperator, deposit);
uint256 nonce = csm.getNonce();

vm.prank(nodeOperator);
{
vm.expectEmit(true, true, true, true, address(csm));
emit IStakingModule.SigningKeyAdded(noId, keys);
vm.expectEmit(true, true, true, true, address(csm));
emit CSModule.TotalSigningKeysCountChanged(noId, 2);
}
csm.addValidatorKeysETH{ value: deposit }(noId, 1, keys, signatures);
assertEq(csm.getNonce(), nonce + 1);
}

function test_AddValidatorKeysETH_RevertWhen_InvalidAmount()
public
assertInvariants
Expand Down

0 comments on commit b17b4eb

Please sign in to comment.