Skip to content

Commit

Permalink
Downtimeslasher foundry test migration (#10978)
Browse files Browse the repository at this point in the history
* ++ some downtime slasher test

* small fixes

* ++ tests

* missing slash test

* remove old test from CI

* remove old unused test contract

* ++ more tests

* passing slash test

* updated double signing test to not use `MockUsingPrecompiles` for `validatorSignerAddressFromSet`

* removed debug

* all tests migrated
  • Loading branch information
soloseng authored May 21, 2024
1 parent eded310 commit f655dff
Show file tree
Hide file tree
Showing 9 changed files with 1,159 additions and 939 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/celo-monorepo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,6 @@ jobs:
- name: Protocol Compatibility
command: |
yarn --cwd packages/protocol test compatibility/
- name: Protocol Governance Validators
command: |
yarn --cwd packages/protocol test governance/validators/
- name: Protocol scripts test
command: |
yarn --cwd packages/protocol test:scripts
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,11 @@ contract MockUsingPrecompiles {
return keccak256(header);
}

function validatorSignerAddressFromSet(
uint256 index,
uint256 blockNumber
) public view returns (address) {
return epochSigner[keccak256(abi.encodePacked(calcEpoch(blockNumber), index))];
}

function getVerifiedSealBitmapFromHeader(bytes memory header) public view returns (bytes32) {
return verifiedSealBitmap[keccak256(abi.encodePacked(header))];
}

function getParentSealBitmap(uint256 blockNumber) public view returns (bytes32) {
return parentSealBitmap[blockNumber];
}

function calcEpoch(uint256 blockNumber) internal pure returns (uint256) {
uint256 epochSize = 100;

// Follows GetEpochNumber from celo-blockchain/blob/master/consensus/istanbul/utils.go
uint256 epochNumber = blockNumber / epochSize;
if (blockNumber % epochSize == 0) {
return epochNumber;
} else {
return epochNumber + 1;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import "@celo-contracts/governance/test/MockLockedGold.sol";
import "@celo-contracts/governance/DoubleSigningSlasher.sol";
import "@celo-contracts/governance/test/MockUsingPrecompiles.sol";

contract DoubleSigningSlasherTest is DoubleSigningSlasher(true), MockUsingPrecompiles {
contract DoubleSigningSlasherTest is DoubleSigningSlasher(true), MockUsingPrecompiles, Test {
struct SlashParams {
address signer;
uint256 index;
Expand All @@ -26,7 +26,13 @@ contract DoubleSigningSlasherTest is DoubleSigningSlasher(true), MockUsingPrecom
uint256[] groupElectionIndices;
}

function mockSlash(SlashParams calldata slashParams) external {
function mockSlash(SlashParams calldata slashParams, address _validator) external {
ph.mockReturn(
ph.GET_VALIDATOR(),
abi.encodePacked(slashParams.index, getBlockNumberFromHeader(slashParams.headerA)),
abi.encode(_validator)
);

slash(
slashParams.signer,
slashParams.index,
Expand Down Expand Up @@ -94,7 +100,7 @@ contract DoubleSigningSlasherBaseTest is Test {
(nonOwner, nonOwnerPK) = actorWithPK("nonOwner");
(validator, validatorPK) = actorWithPK("validator");
(group, groupPK) = actorWithPK("group");
(otherValidator, validatorPK) = actorWithPK("otherValidator");
(otherValidator, otherValidatorPK) = actorWithPK("otherValidator");
(otherGroup, groupPK) = actorWithPK("otherGroup");
(caller2, caller2PK) = actorWithPK("caller2");

Expand Down Expand Up @@ -253,7 +259,7 @@ contract DoubleSigningSlasherSlash is DoubleSigningSlasherBaseTest {
groupElectionIndices: groupElectionIndices
});
vm.expectRevert("Block headers are from different height");
slasher.mockSlash(params);
slasher.mockSlash(params, validator);
}

function test_RevertIf_NotSignedAtIndex() public {
Expand All @@ -273,7 +279,7 @@ contract DoubleSigningSlasherSlash is DoubleSigningSlasherBaseTest {

vm.expectRevert("Didn't sign first block");

slasher.mockSlash(params);
slasher.mockSlash(params, otherValidator);
}

function test_RevertIf_EpochSignerIsWrong() public {
Expand All @@ -292,7 +298,7 @@ contract DoubleSigningSlasherSlash is DoubleSigningSlasherBaseTest {
});

vm.expectRevert("Wasn't a signer with given index");
slasher.mockSlash(params);
slasher.mockSlash(params, validator);
}

function test_RevertIf_NotEnoughSigners() public {
Expand All @@ -313,7 +319,7 @@ contract DoubleSigningSlasherSlash is DoubleSigningSlasherBaseTest {
});

vm.expectRevert("Not enough signers in the first block");
slasher.mockSlash(params);
slasher.mockSlash(params, validator);
}

function test_Emits_DoubleSigningSlashPerformedEvent() public {
Expand All @@ -332,7 +338,7 @@ contract DoubleSigningSlasherSlash is DoubleSigningSlasherBaseTest {
});
vm.expectEmit(true, true, true, true);
emit DoubleSigningSlashPerformed(validator, blockNumber);
slasher.mockSlash(params);
slasher.mockSlash(params, validator);
}

function test_ShouldDecrementCELO() public {
Expand All @@ -350,7 +356,7 @@ contract DoubleSigningSlasherSlash is DoubleSigningSlasherBaseTest {
groupElectionIndices: groupElectionIndices
});

slasher.mockSlash(params);
slasher.mockSlash(params, validator);

assertEq(lockedGold.accountTotalLockedGold(validator), 40000);
}
Expand All @@ -370,7 +376,7 @@ contract DoubleSigningSlasherSlash is DoubleSigningSlasherBaseTest {
groupElectionIndices: groupElectionIndices
});

slasher.mockSlash(params);
slasher.mockSlash(params, validator);
assertEq(lockedGold.accountTotalLockedGold(group), 40000);
}

Expand All @@ -388,9 +394,9 @@ contract DoubleSigningSlasherSlash is DoubleSigningSlasherBaseTest {
groupElectionGreaters: groupElectionGreaters,
groupElectionIndices: groupElectionIndices
});
slasher.mockSlash(params);
slasher.mockSlash(params, validator);
vm.expectRevert("Already slashed");
slasher.mockSlash(params);
slasher.mockSlash(params, validator);
}

function test_Reverts_WhenL2() public {
Expand All @@ -409,6 +415,6 @@ contract DoubleSigningSlasherSlash is DoubleSigningSlasherBaseTest {
groupElectionIndices: groupElectionIndices
});
vm.expectRevert("This method is no longer supported in L2.");
slasher.mockSlash(params);
slasher.mockSlash(params, validator);
}
}
Loading

0 comments on commit f655dff

Please sign in to comment.