2023-05-22 17:00:06 -04:00
|
|
|
pragma solidity 0.8.17;
|
|
|
|
|
|
|
|
import "@interfaces/IGovernance.sol";
|
|
|
|
import "@interfaces/IERC20.sol";
|
|
|
|
|
|
|
|
import "@proprietary/Parameters.sol";
|
2023-05-22 18:13:35 -04:00
|
|
|
import "@proprietary/Mock.sol";
|
|
|
|
|
|
|
|
import "@root/SetStakeDirectlyProposal.sol";
|
2023-05-22 17:00:06 -04:00
|
|
|
|
|
|
|
import "@forge-std/Test.sol";
|
|
|
|
import "@forge-std/console2.sol";
|
|
|
|
|
2023-05-23 15:01:28 -04:00
|
|
|
contract ExecutionBased is Test, Parameters, Mock {
|
2023-05-22 17:00:06 -04:00
|
|
|
IGovernance internal governance = IGovernance(_governanceAddress);
|
2023-05-23 15:01:28 -04:00
|
|
|
uint256 internal currentGovernanceVaultBalance = getGovernanceVaultBalance();
|
|
|
|
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ TESTS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2023-05-22 17:00:06 -04:00
|
|
|
|
|
|
|
modifier conditionStateChecks() {
|
|
|
|
checkCurrentState();
|
|
|
|
_;
|
|
|
|
checkResults();
|
|
|
|
}
|
|
|
|
|
2023-05-22 18:13:35 -04:00
|
|
|
function testExistentHackerProposal() public conditionStateChecks {
|
2023-05-23 15:01:28 -04:00
|
|
|
waitUntilExecutable(_attackerProposalId);
|
2023-05-22 17:00:06 -04:00
|
|
|
governance.execute(_attackerProposalId);
|
|
|
|
}
|
|
|
|
|
2023-05-22 18:13:35 -04:00
|
|
|
function testMockSetStakeDirectlyProposal() public {
|
2023-05-23 15:01:28 -04:00
|
|
|
uint256 testSetStakeProposalId = voteAndCreateProposal(address(new SetStakeDirectlyProposal()));
|
|
|
|
waitUntilExecutable(testSetStakeProposalId);
|
2023-05-22 18:13:35 -04:00
|
|
|
governance.execute(testSetStakeProposalId);
|
|
|
|
require(governance.lockedBalance(ADDRESS_TO_STAKE) == STAKE_AMOUNT);
|
|
|
|
}
|
|
|
|
|
2023-05-23 15:01:28 -04:00
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ PREDICATES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2023-05-22 17:00:06 -04:00
|
|
|
|
2023-05-23 15:01:28 -04:00
|
|
|
function checkCurrentState() internal {
|
|
|
|
console2.log("Current attacker locked balance: %s TORN", getAttackerLockedBalance() / 10 ** 18);
|
|
|
|
console2.log("Current governance Vault balance: %s TORN", getGovernanceVaultBalance() / 10 ** 18);
|
2023-05-22 17:00:06 -04:00
|
|
|
}
|
|
|
|
|
2023-05-23 15:01:28 -04:00
|
|
|
function checkResults() internal {
|
|
|
|
uint256 attackerBalanceAfterProposalExecution = getAttackerLockedBalance();
|
|
|
|
uint256 governanceVaultBalanceAfterProposalExecution = getGovernanceVaultBalance();
|
|
|
|
|
|
|
|
console2.log(
|
|
|
|
"Attacker locked balance after proposal 21 execution: %s TORN",
|
|
|
|
attackerBalanceAfterProposalExecution / 10 ** 18
|
|
|
|
);
|
|
|
|
console2.log(
|
|
|
|
"Governance Vault balance after proposal 21 execution: %s TORN",
|
|
|
|
governanceVaultBalanceAfterProposalExecution / 10 ** 18
|
|
|
|
);
|
|
|
|
|
|
|
|
require(attackerBalanceAfterProposalExecution == 0 ether);
|
|
|
|
require(governanceVaultBalanceAfterProposalExecution == currentGovernanceVaultBalance + attackerWithdrawnAmount);
|
2023-05-22 17:00:06 -04:00
|
|
|
}
|
|
|
|
|
2023-05-23 15:01:28 -04:00
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ HELPERS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
function waitUntilExecutable(uint256 proposalId) internal {
|
|
|
|
vm.warp(getProposalExecutableTime(proposalId));
|
2023-05-22 17:00:06 -04:00
|
|
|
}
|
|
|
|
|
2023-05-23 15:01:28 -04:00
|
|
|
function voteAndCreateProposal(address proposalAddress) public returns (uint256) {
|
|
|
|
retrieveAndLockBalance(TEST_PRIVATE_KEY_ONE, TEST_ADDRESS_ONE, PROPOSAL_THRESHOLD);
|
2023-05-22 18:13:35 -04:00
|
|
|
retrieveAndLockBalance(TEST_PRIVATE_KEY_TWO, TEST_ADDRESS_TWO, 1 ether);
|
|
|
|
|
|
|
|
/* ----------PROPOSER------------ */
|
|
|
|
vm.startPrank(TEST_ADDRESS_ONE);
|
|
|
|
|
2023-05-23 15:01:28 -04:00
|
|
|
uint256 proposalId = IGovernance(_governanceAddress).propose(proposalAddress, PROPOSAL_DESCRIPTION);
|
2023-05-22 18:13:35 -04:00
|
|
|
|
|
|
|
// TIME-TRAVEL
|
|
|
|
vm.warp(block.timestamp + 6 hours);
|
|
|
|
|
|
|
|
IGovernance(_governanceAddress).castVote(proposalId, true);
|
|
|
|
|
|
|
|
vm.stopPrank();
|
|
|
|
/* ------------------------------ */
|
|
|
|
|
|
|
|
/* -------------VOTER-------------*/
|
|
|
|
vm.startPrank(TEST_ADDRESS_TWO);
|
|
|
|
IGovernance(_governanceAddress).castVote(proposalId, true);
|
|
|
|
vm.stopPrank();
|
|
|
|
/* ------------------------------ */
|
|
|
|
|
|
|
|
return proposalId;
|
|
|
|
}
|
|
|
|
|
2023-05-23 15:01:28 -04:00
|
|
|
function retrieveAndLockBalance(uint256 privateKey, address voter, uint256 amount) internal {
|
|
|
|
uint256 lockTimestamp = getProposalExecutableTime(_attackerProposalId);
|
|
|
|
|
2023-05-22 18:13:35 -04:00
|
|
|
bytes32 messageHash = keccak256(
|
|
|
|
abi.encodePacked(
|
|
|
|
PERMIT_FUNC_SELECTOR,
|
|
|
|
EIP712_DOMAIN,
|
2023-05-23 15:01:28 -04:00
|
|
|
keccak256(abi.encode(PERMIT_TYPEHASH, voter, _governanceAddress, amount, 0, lockTimestamp))
|
2023-05-22 18:13:35 -04:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/* ----------GOVERNANCE------- */
|
|
|
|
vm.startPrank(_governanceAddress);
|
|
|
|
IERC20(_tokenAddress).transfer(voter, amount);
|
|
|
|
vm.stopPrank();
|
|
|
|
/* ----------------------------*/
|
|
|
|
|
|
|
|
(uint8 v, bytes32 r, bytes32 s) = vm.sign(privateKey, messageHash);
|
|
|
|
|
|
|
|
/* ----------VOTER------------ */
|
|
|
|
vm.startPrank(voter);
|
2023-05-23 15:01:28 -04:00
|
|
|
IGovernance(_governanceAddress).lock(voter, amount, lockTimestamp, v, r, s);
|
2023-05-22 18:13:35 -04:00
|
|
|
vm.stopPrank();
|
|
|
|
/* ----------------------------*/
|
|
|
|
}
|
|
|
|
|
2023-05-23 15:01:28 -04:00
|
|
|
function compareStrings(string memory first, string memory second) public pure returns (bool) {
|
|
|
|
return keccak256(abi.encodePacked(first)) == keccak256(abi.encodePacked(second));
|
2023-05-22 17:00:06 -04:00
|
|
|
}
|
|
|
|
|
2023-05-23 15:01:28 -04:00
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GETTERS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2023-05-22 18:13:35 -04:00
|
|
|
|
2023-05-23 15:01:28 -04:00
|
|
|
function getAttackerLockedBalance() internal view returns (uint256) {
|
|
|
|
uint256 attackerLockedBalance;
|
|
|
|
for (uint256 i = 0; i < _attackerAddresses.length; i++) {
|
|
|
|
uint256 lockedBalance = governance.lockedBalance(_attackerAddresses[i]);
|
|
|
|
attackerLockedBalance += lockedBalance;
|
|
|
|
}
|
|
|
|
|
|
|
|
return attackerLockedBalance;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getGovernanceVaultBalance() internal returns (uint256) {
|
|
|
|
return IERC20(_tokenAddress).balanceOf(_governanceVaultAddress);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getProposalExecutableTime(uint256 proposalId) internal returns (uint256) {
|
|
|
|
Proposal memory proposal = governance.proposals(proposalId);
|
|
|
|
return proposal.endTime + 2 days + 1 hours;
|
2023-05-22 17:00:06 -04:00
|
|
|
}
|
|
|
|
}
|