Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GovernanceSlasher test Foundry Migration #10818

Merged
merged 5 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.5.13;

import "celo-foundry/Test.sol";

import "../../../contracts/common/Accounts.sol";
import "../../../contracts/common/FixidityLib.sol";
import "../../../contracts/common/interfaces/IRegistry.sol";
import "../../../contracts/governance/Proposals.sol";
import "../../../contracts/governance/test/MockLockedGold.sol";
import "../../../contracts/governance/test/MockValidators.sol";

// Contract to test
import "../../../contracts/governance/GovernanceSlasher.sol";
soloseng marked this conversation as resolved.
Show resolved Hide resolved

contract GovernanceSlasherTest is Test {
event SlashingApproved(address indexed account, uint256 amount);
event GovernanceSlashPerformed(address indexed account, uint256 amount);

IRegistry registry;
Accounts accounts;
MockLockedGold mockLockedGold;
MockValidators mockValidators;

GovernanceSlasher public governanceSlasher;
address owner;
address nonOwner;
address validator;
address slashedAddress;

address registryAddress = 0x000000000000000000000000000000000000ce10;

function setUp() public {
owner = address(this);
nonOwner = actor("nonOwner");
validator = actor("validator");
slashedAddress = actor("slashedAddress");

accounts = new Accounts(true);
mockLockedGold = new MockLockedGold();
mockValidators = new MockValidators();
soloseng marked this conversation as resolved.
Show resolved Hide resolved
governanceSlasher = new GovernanceSlasher(true);

deployCodeTo("Registry.sol", abi.encode(false), registryAddress);
soloseng marked this conversation as resolved.
Show resolved Hide resolved
registry = IRegistry(registryAddress);
registry.setAddressFor("Accounts", address(accounts));
registry.setAddressFor("LockedGold", address(mockLockedGold));
registry.setAddressFor("Validators", address(mockValidators));

governanceSlasher.initialize(registryAddress);
mockLockedGold.setAccountTotalLockedGold(validator, 5000);
}
}

contract GovernanceSlasherInitializeTest is GovernanceSlasherTest {
function test_shouldHaveSetOwner() public {
assertEq(governanceSlasher.owner(), owner);
}

function test_CanOnlyBeCalledOnce() public {
vm.expectRevert("contract already initialized");
governanceSlasher.initialize(registryAddress);
}
}

contract GovernanceSlasherApproveSlashingTest is GovernanceSlasherTest {
function test_ShouldSetSlashableAmount() public {
governanceSlasher.approveSlashing(slashedAddress, 1000);
assertEq(governanceSlasher.getApprovedSlashing(slashedAddress), 1000);
}

function test_ShouldIncrementSlashableAmountWhenApprovedTwice() public {
governanceSlasher.approveSlashing(slashedAddress, 1000);
governanceSlasher.approveSlashing(slashedAddress, 1000);
assertEq(governanceSlasher.getApprovedSlashing(slashedAddress), 2000);
}

function test_CanOnlyBeCalledByOnwer() public {
vm.expectRevert("Ownable: caller is not the owner");
vm.prank(nonOwner);
governanceSlasher.approveSlashing(slashedAddress, 1000);
}

function test_EmitsSlashingApprovedEvent() public {
vm.expectEmit(true, true, true, true);
emit SlashingApproved(slashedAddress, 1000);
governanceSlasher.approveSlashing(slashedAddress, 1000);

}
}

contract GovernanceSlasherSlashTest is GovernanceSlasherTest {
address[] lessers = new address[](0);
address[] greaters = new address[](0);
uint256[] indices = new uint256[](0);

function test_ShouldFailIfThereIsNothingToSlash() public {
vm.expectRevert("No penalty given by governance");
governanceSlasher.slash(validator, lessers, greaters, indices);
}

function test_ShouldDecrementCelo() public {
governanceSlasher.approveSlashing(validator, 1000);
governanceSlasher.slash(validator, lessers, greaters, indices);
assertEq(mockLockedGold.accountTotalLockedGold(validator), 4000);
}

function test_ShouldHaveSetTheApprovedSlashingToZero() public {
governanceSlasher.approveSlashing(validator, 1000);
governanceSlasher.slash(validator, lessers, greaters, indices);
assertEq(governanceSlasher.getApprovedSlashing(validator), 0);
}

function test_EmitsGovernanceSlashPerformedEvent() public {
governanceSlasher.approveSlashing(validator, 1000);
vm.expectEmit(true, true, true, true);
emit GovernanceSlashPerformed(validator, 1000);
governanceSlasher.slash(validator, lessers, greaters, indices);
}
}
120 changes: 0 additions & 120 deletions packages/protocol/test/governance/network/governance_slasher.ts

This file was deleted.

Loading