Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 0 additions & 131 deletions l1-contracts/test/staking/StakingCheater.sol

This file was deleted.

20 changes: 13 additions & 7 deletions l1-contracts/test/staking/base.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,33 @@
pragma solidity >=0.8.27;

import {TestBase} from "@test/base/Base.sol";

import {StakingCheater} from "./StakingCheater.sol";
import {IStaking} from "@aztec/core/interfaces/IStaking.sol";
import {TestConstants} from "../harnesses/TestConstants.sol";
import {TestERC20} from "@aztec/mock/TestERC20.sol";
import {RollupBuilder} from "../builder/RollupBuilder.sol";

contract StakingBase is TestBase {
StakingCheater internal staking;
IStaking internal staking;
TestERC20 internal stakingAsset;

uint256 internal constant MINIMUM_STAKE = 100e18;

address internal constant PROPOSER = address(bytes20("PROPOSER"));
address internal constant ATTESTER = address(bytes20("ATTESTER"));
address internal constant WITHDRAWER = address(bytes20("WITHDRAWER"));
address internal constant RECIPIENT = address(bytes20("RECIPIENT"));

uint256 internal MINIMUM_STAKE = 100e18;

address internal SLASHER;

function setUp() public virtual {
stakingAsset = new TestERC20("test", "TEST", address(this));
staking = new StakingCheater(stakingAsset, MINIMUM_STAKE, 1, 1);
RollupBuilder builder =
new RollupBuilder(address(this)).setSlashingQuorum(1).setSlashingRoundSize(1);
builder.deploy();

staking = IStaking(address(builder.getConfig().rollup));
stakingAsset = builder.getConfig().testERC20;

MINIMUM_STAKE = staking.getMinimumStake();

SLASHER = staking.getSlasher();
}
Expand Down
57 changes: 34 additions & 23 deletions l1-contracts/test/staking/deposit.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,40 @@ contract DepositTest is StakingBase {
{
// it reverts

// Show that everything else than the none status is rejected
for (uint256 i = 1; i < 4; i++) {
staking.cheat__SetStatus(ATTESTER, Status(i));

// Try to register the attester again
vm.expectRevert(abi.encodeWithSelector(Errors.Staking__AlreadyRegistered.selector, ATTESTER));
staking.deposit({
_attester: ATTESTER,
_proposer: PROPOSER,
_withdrawer: WITHDRAWER,
_amount: depositAmount
});
}
staking.deposit({
_attester: ATTESTER,
_proposer: PROPOSER,
_withdrawer: WITHDRAWER,
_amount: depositAmount
});

vm.expectRevert(abi.encodeWithSelector(Errors.Staking__AlreadyRegistered.selector, ATTESTER));
staking.deposit({
_attester: ATTESTER,
_proposer: PROPOSER,
_withdrawer: WITHDRAWER,
_amount: depositAmount
});

vm.prank(SLASHER);
staking.slash(ATTESTER, depositAmount / 2);
vm.expectRevert(abi.encodeWithSelector(Errors.Staking__AlreadyRegistered.selector, ATTESTER));
staking.deposit({
_attester: ATTESTER,
_proposer: PROPOSER,
_withdrawer: WITHDRAWER,
_amount: depositAmount
});

vm.prank(WITHDRAWER);
staking.initiateWithdraw(ATTESTER, WITHDRAWER);
vm.expectRevert(abi.encodeWithSelector(Errors.Staking__AlreadyRegistered.selector, ATTESTER));
staking.deposit({
_attester: ATTESTER,
_proposer: PROPOSER,
_withdrawer: WITHDRAWER,
_amount: depositAmount
});
}

modifier givenAttesterIsNotRegistered() {
Expand All @@ -120,16 +141,6 @@ contract DepositTest is StakingBase {

// This should not be possible to get to as the attester is registered until exit
// and to exit it must already have been removed from the active set.

staking.cheat__AddAttester(ATTESTER);

vm.expectRevert(abi.encodeWithSelector(Errors.Staking__AlreadyActive.selector, ATTESTER));
staking.deposit({
_attester: ATTESTER,
_proposer: PROPOSER,
_withdrawer: WITHDRAWER,
_amount: depositAmount
});
}

function test_GivenAttesterIsNotActive(uint256 _depositAmount)
Expand Down
28 changes: 20 additions & 8 deletions l1-contracts/test/staking/finaliseWithdraw.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,27 @@ contract FinaliseWithdrawTest is StakingBase {
function test_GivenStatusIsNotExiting() external {
// it revert

for (uint256 i = 0; i < 3; i++) {
staking.cheat__SetStatus(ATTESTER, Status(i));
vm.expectRevert(abi.encodeWithSelector(Errors.Staking__NotExiting.selector, ATTESTER));
staking.finaliseWithdraw(ATTESTER);

stakingAsset.mint(address(this), MINIMUM_STAKE);
stakingAsset.approve(address(staking), MINIMUM_STAKE);

staking.deposit({
_attester: ATTESTER,
_proposer: PROPOSER,
_withdrawer: WITHDRAWER,
_amount: MINIMUM_STAKE
});

vm.expectRevert(abi.encodeWithSelector(Errors.Staking__NotExiting.selector, ATTESTER));
staking.finaliseWithdraw(ATTESTER);

vm.prank(SLASHER);
staking.slash(ATTESTER, MINIMUM_STAKE);

vm.expectRevert(abi.encodeWithSelector(Errors.Staking__NotExiting.selector, ATTESTER));
staking.finaliseWithdraw(ATTESTER);
}
vm.expectRevert(abi.encodeWithSelector(Errors.Staking__NotExiting.selector, ATTESTER));
staking.finaliseWithdraw(ATTESTER);
}

modifier givenStatusIsExiting() {
Expand All @@ -32,9 +47,6 @@ contract FinaliseWithdrawTest is StakingBase {
_amount: MINIMUM_STAKE
});

// Progress into the next epoch
staking.cheat__progressEpoch();

vm.prank(WITHDRAWER);
staking.initiateWithdraw(ATTESTER, RECIPIENT);

Expand Down
3 changes: 0 additions & 3 deletions l1-contracts/test/staking/getters.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ contract GettersTest is StakingBase {
_withdrawer: WITHDRAWER,
_amount: MINIMUM_STAKE
});

// Progress into the next epoch
staking.cheat__progressEpoch();
}

function test_getAttesterAtIndex() external view {
Expand Down
26 changes: 10 additions & 16 deletions l1-contracts/test/staking/initiateWithdraw.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ contract InitiateWithdrawTest is StakingBase {
_withdrawer: WITHDRAWER,
_amount: MINIMUM_STAKE
});

// Progress into the next epoch
staking.cheat__progressEpoch();
_;
}

Expand All @@ -55,13 +52,15 @@ contract InitiateWithdrawTest is StakingBase {
{
// it revert

staking.cheat__SetStatus(ATTESTER, Status.EXITING);
vm.expectRevert(abi.encodeWithSelector(Errors.Staking__NothingToExit.selector, ATTESTER));
assertTrue(staking.getInfo(address(1)).status == Status.NONE);
vm.expectRevert(abi.encodeWithSelector(Errors.Staking__NothingToExit.selector, address(1)));
vm.prank(address(0));
staking.initiateWithdraw(address(1), address(2));

vm.prank(WITHDRAWER);
staking.initiateWithdraw(ATTESTER, RECIPIENT);

// Should not be possible to hit this, as you should have failed with withdrawer being address(0)
staking.cheat__SetStatus(ATTESTER, Status.NONE);
assertTrue(staking.getInfo(ATTESTER).status == Status.EXITING);
vm.expectRevert(abi.encodeWithSelector(Errors.Staking__NothingToExit.selector, ATTESTER));
vm.prank(WITHDRAWER);
staking.initiateWithdraw(ATTESTER, RECIPIENT);
Expand All @@ -78,12 +77,7 @@ contract InitiateWithdrawTest is StakingBase {
givenAttesterIsValidating
{
// it revert

// Again, this should not be possible to hit
staking.cheat__RemoveAttester(ATTESTER);
vm.expectRevert(abi.encodeWithSelector(Errors.Staking__FailedToRemove.selector, ATTESTER));
vm.prank(WITHDRAWER);
staking.initiateWithdraw(ATTESTER, RECIPIENT);
// this should not be possible to hit
}

function test_GivenAttesterIsInTheActiveSet()
Expand Down Expand Up @@ -128,8 +122,8 @@ contract InitiateWithdrawTest is StakingBase {
// it updates the operator status to exiting
// it emits a {WithdrawInitiated} event

staking.cheat__SetStatus(ATTESTER, Status.LIVING);
staking.cheat__RemoveAttester(ATTESTER);
vm.prank(SLASHER);
staking.slash(ATTESTER, MINIMUM_STAKE / 2);

assertEq(stakingAsset.balanceOf(address(staking)), MINIMUM_STAKE);
assertEq(stakingAsset.balanceOf(RECIPIENT), 0);
Expand All @@ -142,7 +136,7 @@ contract InitiateWithdrawTest is StakingBase {
assertEq(staking.getActiveAttesterCount(), 0);

vm.expectEmit(true, true, true, true, address(staking));
emit IStakingCore.WithdrawInitiated(ATTESTER, RECIPIENT, MINIMUM_STAKE);
emit IStakingCore.WithdrawInitiated(ATTESTER, RECIPIENT, MINIMUM_STAKE - MINIMUM_STAKE / 2);

vm.prank(WITHDRAWER);
staking.initiateWithdraw(ATTESTER, RECIPIENT);
Expand Down
Loading