Skip to content

Commit

Permalink
Merge pull request #598 from morph-l2/fix-get-delegators
Browse files Browse the repository at this point in the history
Fix get all delegators in pagination
  • Loading branch information
SegueII authored Nov 15, 2024
2 parents ec87648 + 3e19f69 commit c56fb93
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 14 deletions.
2 changes: 1 addition & 1 deletion bindings/bin/l2staking_deployed.hex

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion bindings/bindings/l2staking.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion bindings/bindings/l2staking_more.go

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions contracts/contracts/l2/staking/L2Staking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -552,8 +552,10 @@ contract L2Staking is IL2Staking, Staking, OwnableUpgradeable, ReentrancyGuardUp
if (end > (delegatorsTotalNumber - 1)) {
end = delegatorsTotalNumber - 1;
}
for (uint256 i = start; i <= end; i++) {
delegatorsInPage[i] = delegators[staker].at(i);
uint256 i = start;
uint256 j = 0;
while (i <= end) {
delegatorsInPage[j++] = delegators[staker].at(i++);
}
return (delegatorsTotalNumber, delegatorsInPage);
}
Expand Down
121 changes: 112 additions & 9 deletions contracts/contracts/test/L2Staking.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2196,17 +2196,120 @@ contract L2StakingTest is L2StakingBaseTest {
l2Staking.delegateStake(thirdStaker, 5 ether);
hevm.stopPrank();

(, address[] memory delegator0) = l2Staking.getAllDelegatorsInPagination(firstStaker, 10, 0);
(, address[] memory delegator1) = l2Staking.getAllDelegatorsInPagination(secondStaker, 10, 0);
(, address[] memory delegator2) = l2Staking.getAllDelegatorsInPagination(thirdStaker, 10, 0);
hevm.startPrank(alice1);
morphToken.approve(address(l2Staking), type(uint256).max);
l2Staking.delegateStake(firstStaker, 5 ether);
l2Staking.delegateStake(secondStaker, 5 ether);
hevm.stopPrank();

hevm.startPrank(alice2);
morphToken.approve(address(l2Staking), type(uint256).max);
l2Staking.delegateStake(firstStaker, 5 ether);
l2Staking.delegateStake(secondStaker, 5 ether);
hevm.stopPrank();

hevm.startPrank(alice3);
morphToken.approve(address(l2Staking), type(uint256).max);
l2Staking.delegateStake(firstStaker, 5 ether);
hevm.stopPrank();

assertEq(delegator0.length, 10);
assertEq(delegator1.length, 10);
assertEq(delegator2.length, 10);
hevm.startPrank(alice4);
morphToken.approve(address(l2Staking), type(uint256).max);
l2Staking.delegateStake(firstStaker, 5 ether);
hevm.stopPrank();

// check firstStaker
{
(uint256 total, address[] memory delegators) = l2Staking.getAllDelegatorsInPagination(firstStaker, 10, 0);
assertEq(total, 5);
assertEq(delegators.length, 10);
assertEq(delegators[0], alice);
assertEq(delegators[1], alice1);
assertEq(delegators[2], alice2);
assertEq(delegators[3], alice3);
assertEq(delegators[4], alice4);
assertEq(delegators[5], address(0));
}

assertEq(delegator0[0], alice);
assertEq(delegator1[0], alice);
assertEq(delegator2[0], alice);
{
(uint256 total, address[] memory delegators) = l2Staking.getAllDelegatorsInPagination(firstStaker, 1, 0);
assertEq(total, 5);
assertEq(delegators.length, 1);
assertEq(delegators[0], alice);
}

{
(uint256 total, address[] memory delegators) = l2Staking.getAllDelegatorsInPagination(firstStaker, 1, 1);
assertEq(total, 5);
assertEq(delegators.length, 1);
assertEq(delegators[0], alice1);
}

{
(uint256 total, address[] memory delegators) = l2Staking.getAllDelegatorsInPagination(firstStaker, 2, 2);
assertEq(total, 5);
assertEq(delegators.length, 2);
assertEq(delegators[0], alice4);
assertEq(delegators[1], address(0));
}

{
(uint256 total, address[] memory delegators) = l2Staking.getAllDelegatorsInPagination(firstStaker, 10, 3);
assertEq(total, 5);
assertEq(delegators.length, 10);
assertEq(delegators[0], address(0));
assertEq(delegators[1], address(0));
}

// check secondStaker
{
(uint256 total, address[] memory delegators) = l2Staking.getAllDelegatorsInPagination(secondStaker, 10, 0);
assertEq(total, 3);
assertEq(delegators.length, 10);
assertEq(delegators[0], alice);
assertEq(delegators[1], alice1);
assertEq(delegators[2], alice2);
assertEq(delegators[3], address(0));
}

{
(uint256 total, address[] memory delegators) = l2Staking.getAllDelegatorsInPagination(secondStaker, 2, 1);
assertEq(total, 3);
assertEq(delegators.length, 2);
assertEq(delegators[0], alice2);
assertEq(delegators[1], address(0));
}

{
(uint256 total, address[] memory delegators) = l2Staking.getAllDelegatorsInPagination(secondStaker, 2, 2);
assertEq(total, 3);
assertEq(delegators.length, 2);
assertEq(delegators[0], address(0));
assertEq(delegators[1], address(0));
}

// check thirdStaker
{
(uint256 total, address[] memory delegators) = l2Staking.getAllDelegatorsInPagination(thirdStaker, 10, 0);
assertEq(total, 1);
assertEq(delegators.length, 10);
assertEq(delegators[0], alice);
assertEq(delegators[1], address(0));
}

{
(uint256 total, address[] memory delegators) = l2Staking.getAllDelegatorsInPagination(thirdStaker, 1, 0);
assertEq(total, 1);
assertEq(delegators.length, 1);
assertEq(delegators[0], alice);
}

{
(uint256 total, address[] memory delegators) = l2Staking.getAllDelegatorsInPagination(thirdStaker, 1, 1);
assertEq(total, 1);
assertEq(delegators.length, 1);
assertEq(delegators[0], address(0));
}
}

/**
Expand Down

0 comments on commit c56fb93

Please sign in to comment.