Skip to content

Commit

Permalink
simplify test
Browse files Browse the repository at this point in the history
  • Loading branch information
danoctavian committed Oct 1, 2024
1 parent 3ce68f7 commit 3832d35
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 41 deletions.
14 changes: 14 additions & 0 deletions script/Utils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,18 @@ contract Utils {
return difference <= 2; // Allow for a 1-2 wei difference only
}
}

/**
* @dev Creates an array of uint256 with a specified value repeated a given number of times.
* @param value The value to be repeated in the array.
* @param count The number of times to repeat the value.
* @return uint256[] An array containing the repeated value.
*/
function repeat(uint256 value, uint256 count) public pure returns (uint256[] memory) {
uint256[] memory result = new uint256[](count);
for (uint256 i = 0; i < count; i++) {
result[i] = value;
}
return result;
}
}
76 changes: 35 additions & 41 deletions test/integration/StakingNode.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ contract StakingNodeVerifyWithdrawalCredentials is StakingNodeTestBase {


uint256 nodeId;
uint40 validatorIndex;
uint40[] validatorIndices;
uint256 AMOUNT = 32 ether;

function setUp() public override {
Expand All @@ -317,62 +317,38 @@ contract StakingNodeVerifyWithdrawalCredentials is StakingNodeTestBase {
vm.deal(user, 1000 ether);

yneth.depositETH{value: 1000 ether}(user);

// create staking node
{
vm.prank(actors.ops.STAKING_NODE_CREATOR);
stakingNodesManager.createStakingNode();
nodeId = stakingNodesManager.nodesLength() - 1;
}

// create validator
{
bytes memory _withdrawalCredentials = stakingNodesManager.getWithdrawalCredentials(nodeId);
validatorIndex = beaconChain.newValidator{ value: AMOUNT }(_withdrawalCredentials);
beaconChain.advanceEpoch_NoRewards();
}

// register validator
{
bytes memory _dummyPubkey = new bytes(48);
IStakingNodesManager.ValidatorData[] memory _data = new IStakingNodesManager.ValidatorData[](1);
_data[0] = IStakingNodesManager.ValidatorData({
publicKey: _dummyPubkey,
signature: ZERO_SIGNATURE,
depositDataRoot: stakingNodesManager.generateDepositRoot(
_dummyPubkey,
ZERO_SIGNATURE,
stakingNodesManager.getWithdrawalCredentials(nodeId),
AMOUNT
),
nodeId: nodeId
});
vm.prank(actors.ops.VALIDATOR_MANAGER);
stakingNodesManager.registerValidators(_data);
}
}

function testVerifyWithdrawalCredentials() public {
function testVerifyWithdrawalCredentialsForOneValidator() public {

_verifyWithdrawalCredentials(nodeId, validatorIndex);
uint256 nodeId = createStakingNodes(1)[0];
// Call createValidators with the nodeIds array and validatorCount
validatorIndices = createValidators(repeat(nodeId, 1), 1);
beaconChain.advanceEpoch_NoRewards();
registerValidators(repeat(nodeId, 1));

_verifyWithdrawalCredentials(nodeId, validatorIndices[0]);

// check that unverifiedStakedETH is 0 and podOwnerShares is 32 ETH (AMOUNT)
assertEq(stakingNodesManager.nodes(nodeId).unverifiedStakedETH(), 0, "_testVerifyWithdrawalCredentials: E0");
assertEq(uint256(eigenPodManager.podOwnerShares(address(stakingNodesManager.nodes(nodeId)))), AMOUNT, "_testVerifyWithdrawalCredentials: E1");
}

function testVerifyWithdrawalCredentialsTwice() public {
uint256 nodeId = createStakingNodes(1)[0];
// Call createValidators with the nodeIds array and validatorCount
validatorIndices = createValidators(repeat(nodeId, 1), 1);
beaconChain.advanceEpoch_NoRewards();
registerValidators(repeat(nodeId, 1));

uint40 validatorIndex = validatorIndices[0];

// First verification
_verifyWithdrawalCredentials(nodeId, validatorIndex);

// Check that unverifiedStakedETH is 0 and podOwnerShares is 32 ETH (AMOUNT) after first verification
assertEq(stakingNodesManager.nodes(nodeId).unverifiedStakedETH(), 0, "First verification: unverifiedStakedETH should be 0");
assertEq(uint256(eigenPodManager.podOwnerShares(address(stakingNodesManager.nodes(nodeId)))), AMOUNT, "First verification: podOwnerShares should be AMOUNT");

// Try to verify withdrawal credentials again
uint40[] memory _validators = new uint40[](1);
_validators[0] = validatorIndex;


CredentialProofs memory _proofs = beaconChain.getCredentialProofs(_validators);
vm.startPrank(actors.ops.STAKING_NODES_OPERATOR);
Expand All @@ -390,6 +366,14 @@ contract StakingNodeVerifyWithdrawalCredentials is StakingNodeTestBase {

function testVerifyCheckpointsForOneValidator() public {

uint256 nodeId = createStakingNodes(1)[0];
// Call createValidators with the nodeIds array and validatorCount
validatorIndices = createValidators(repeat(nodeId, 1), 1);
beaconChain.advanceEpoch_NoRewards();
registerValidators(repeat(nodeId, 1));

uint40 validatorIndex = validatorIndices[0];

{
_verifyWithdrawalCredentials(nodeId, validatorIndex);

Expand Down Expand Up @@ -447,6 +431,16 @@ contract StakingNodeVerifyWithdrawalCredentials is StakingNodeTestBase {
});
vm.stopPrank();
}

function createStakingNodes(uint nodeCount) public returns (uint256[] memory) {
uint256[] memory nodeIds = new uint256[](nodeCount);
for (uint256 i = 0; i < nodeCount; i++) {
vm.prank(actors.ops.STAKING_NODE_CREATOR);
IStakingNode node = stakingNodesManager.createStakingNode();
nodeIds[i] = node.nodeId();
}
return nodeIds;
}
}

// contract StakingNodeStakedETHAllocationTests is StakingNodeTestBase {
Expand Down

0 comments on commit 3832d35

Please sign in to comment.