Skip to content

Commit

Permalink
Merge pull request #556 from livepeer/rb/foundry-upgradeability-refac…
Browse files Browse the repository at this point in the history
…toring

Rb/foundry upgradeability refactoring
  • Loading branch information
RiccardoBiosas authored Jun 9, 2022
2 parents d1e2222 + e2152f5 commit 7c51ea4
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 252 deletions.
86 changes: 16 additions & 70 deletions src/test/BondingManagerForceChangeDelegateFix.sol
Original file line number Diff line number Diff line change
@@ -1,62 +1,21 @@
pragma solidity ^0.8.9;

import "ds-test/test.sol";
import "contracts/governance/Governor.sol";
import "./base/GovernorBaseTest.sol";
import "contracts/Controller.sol";
import "contracts/bonding/BondingManager.sol";
import "contracts/snapshots/MerkleSnapshot.sol";

interface CheatCodes {
function roll(uint256) external;

function prank(address) external;

function expectRevert(bytes calldata) external;

function expectEmit(
bool checkTopic1,
bool checkTopic2,
bool checkTopic3,
bool checkData
) external;

function mockCall(
address,
bytes calldata,
bytes calldata
) external;
}

interface L2Migrator {
function claimStake(
address,
uint256,
uint256,
bytes32[] calldata,
address
) external;
}
import "./interfaces/ICheatCodes.sol";
import "./interfaces/IL2Migrator.sol";

// forge test --match-contract BondingManagerForceChangeDelegateFix --fork-url https://arbitrum-mainnet.infura.io/v3/<INFURA_KEY> -vvv --fork-block-number 8006620
contract BondingManagerForceChangeDelegateFix is DSTest {
CheatCodes public constant CHEATS = CheatCodes(HEVM_ADDRESS);

Governor public constant GOVERNOR = Governor(0xD9dEd6f9959176F0A04dcf88a0d2306178A736a6);
Controller public constant CONTROLLER = Controller(0xD8E8328501E9645d16Cf49539efC04f734606ee4);
contract BondingManagerForceChangeDelegateFix is GovernorBaseTest {
BondingManager public constant BONDING_MANAGER = BondingManager(0x35Bcf3c30594191d53231E4FF333E8A770453e40);
MerkleSnapshot public constant MERKLE_SNAPSHOT = MerkleSnapshot(0x10736ffaCe687658F88a46D042631d182C7757f7);
L2Migrator public constant L2_MIGRATOR = L2Migrator(0x148D5b6B4df9530c7C76A810bd1Cdf69EC4c2085);

address public constant GOVERNOR_OWNER = 0x04F53A0bb244f015cC97731570BeD26F0229da05;
IL2Migrator public constant L2_MIGRATOR = IL2Migrator(0x148D5b6B4df9530c7C76A810bd1Cdf69EC4c2085);

bytes32 public constant BONDING_MANAGER_TARGET_ID = keccak256("BondingManagerTarget");

// Governor update
address[] public targets;
uint256[] public values;
bytes[] public datas;
bytes20 public gitCommitHash;

BondingManager public newBondingManagerTarget;

event Bond(
Expand All @@ -70,40 +29,27 @@ contract BondingManagerForceChangeDelegateFix is DSTest {
function setUp() public {
newBondingManagerTarget = new BondingManager(address(CONTROLLER));

targets = [address(CONTROLLER)];
values = [0];

(, gitCommitHash) = CONTROLLER.getContractInfo(BONDING_MANAGER_TARGET_ID);
datas = [

uint256 round = 2499;
uint256 blockNum = round * 5760;
CHEATS.roll(blockNum);

stageAndExecuteOne(
address(CONTROLLER),
0,
abi.encodeWithSelector(
CONTROLLER.setContractInfo.selector,
BONDING_MANAGER_TARGET_ID,
address(newBondingManagerTarget),
gitCommitHash
)
];

uint256 round = 2499;
uint256 blockNum = round * 5760;
CHEATS.roll(blockNum);

upgradeBondingManager();
}

function upgradeBondingManager() public {
Governor.Update memory update = Governor.Update({ target: targets, value: values, data: datas, nonce: 0 });

// Impersonate Governor owner
CHEATS.prank(GOVERNOR_OWNER);
GOVERNOR.stage(update, 0);
GOVERNOR.execute(update);
);
}

function testUpgrade() public {
(, bytes20 gitCommitHash) = CONTROLLER.getContractInfo(BONDING_MANAGER_TARGET_ID);

// Check that new BondingManagerTarget is registered
(address infoAddr, bytes20 infoGitCommitHash) = CONTROLLER.getContractInfo(BONDING_MANAGER_TARGET_ID);
(address infoAddr, bytes20 infoGitCommitHash) = fetchContractInfo(BONDING_MANAGER_TARGET_ID);
assertEq(infoAddr, address(newBondingManagerTarget));
assertEq(infoGitCommitHash, gitCommitHash);
}
Expand Down Expand Up @@ -146,8 +92,8 @@ contract BondingManagerForceChangeDelegateFix is DSTest {
address delegate = 0x91f19C0335BC776f4693EeB1D88765243f63e9D6;

bytes32[] memory proof;
CHEATS.prank(delegator);

CHEATS.prank(delegator);
CHEATS.expectEmit(true, true, true, true);
emit Bond(delegate, address(0), delegator, 500000000000000000000, 500000000000000000000);
L2_MIGRATOR.claimStake(delegate, 500000000000000000000, 0, proof, address(0));
Expand Down
45 changes: 11 additions & 34 deletions src/test/BondingManagerNullDelegateBondFix.sol
Original file line number Diff line number Diff line change
@@ -1,68 +1,45 @@
pragma solidity ^0.8.9;

import "ds-test/test.sol";
import "contracts/governance/Governor.sol";
import "contracts/Controller.sol";
import "./base/GovernorBaseTest.sol";
import "contracts/bonding/BondingManager.sol";
import "contracts/snapshots/MerkleSnapshot.sol";

interface CheatCodes {
function roll(uint256) external;

function prank(address) external;
}

// forge test -vvv --fork-url <ARB_MAINNET_RPC_URL> --fork-block-number 6768456 --match-contract BondingManagerNullDelegateBondFix
contract BondingManagerNullDelegateBondFix is DSTest {
CheatCodes public constant CHEATS = CheatCodes(HEVM_ADDRESS);

Governor public constant GOVERNOR = Governor(0xD9dEd6f9959176F0A04dcf88a0d2306178A736a6);
Controller public constant CONTROLLER = Controller(0xD8E8328501E9645d16Cf49539efC04f734606ee4);
contract BondingManagerNullDelegateBondFix is GovernorBaseTest {
BondingManager public constant BONDING_MANAGER = BondingManager(0x35Bcf3c30594191d53231E4FF333E8A770453e40);

address public constant GOVERNOR_OWNER = 0x04F53A0bb244f015cC97731570BeD26F0229da05;

bytes32 public constant BONDING_MANAGER_TARGET_ID = keccak256("BondingManagerTarget");

// Governor update
address[] public targets;
uint256[] public values;
bytes[] public datas;

BondingManager public newBondingManagerTarget;

function setUp() public {
newBondingManagerTarget = new BondingManager(address(CONTROLLER));

targets = [address(CONTROLLER)];
values = [0];

(, bytes20 gitCommitHash) = CONTROLLER.getContractInfo(BONDING_MANAGER_TARGET_ID);
datas = [

stageAndExecuteOne(
address(CONTROLLER),
0,
abi.encodeWithSelector(
CONTROLLER.setContractInfo.selector,
BONDING_MANAGER_TARGET_ID,
address(newBondingManagerTarget),
gitCommitHash
)
];
);
}

function testUpgrade() public {
(, bytes20 gitCommitHash) = CONTROLLER.getContractInfo(BONDING_MANAGER_TARGET_ID);

Governor.Update memory update = Governor.Update({ target: targets, value: values, data: datas, nonce: 0 });

// Impersonate Governor owner
CHEATS.prank(GOVERNOR_OWNER);
GOVERNOR.stage(update, 0);
GOVERNOR.execute(update);
(, bytes20 gitCommitHash) = fetchContractInfo(BONDING_MANAGER_TARGET_ID);

// Check that new BondingManagerTarget is registered
(address infoAddr, bytes20 infoGitCommitHash) = CONTROLLER.getContractInfo(BONDING_MANAGER_TARGET_ID);
(address infoAddr, bytes20 infoGitCommitHash) = fetchContractInfo(BONDING_MANAGER_TARGET_ID);
assertEq(infoAddr, address(newBondingManagerTarget));
assertEq(infoGitCommitHash, gitCommitHash);
}

function testNullDelegateBond() public {
// This test should be run with --fork-block-number 6768456
// We are forking right after https://arbiscan.io/address/0xF8E893C7D84E366f7Bc6bc1cdB568Ff8c91bCF57
// This is the corresponding L1 block number
Expand Down
62 changes: 12 additions & 50 deletions src/test/BondingManagerNullDelegateTransferBondFix.sol
Original file line number Diff line number Diff line change
@@ -1,86 +1,48 @@
pragma solidity ^0.8.9;

import "ds-test/test.sol";
import "contracts/governance/Governor.sol";
import "contracts/Controller.sol";
import "./base/GovernorBaseTest.sol";
import "contracts/bonding/BondingManager.sol";
import "contracts/snapshots/MerkleSnapshot.sol";

interface CheatCodes {
function roll(uint256) external;

function mockCall(
address,
bytes calldata,
bytes calldata
) external;

function prank(address) external;
}

interface L2Migrator {
function claimStake(
address,
uint256,
uint256,
bytes32[] calldata,
address
) external;
}
import "./interfaces/IL2Migrator.sol";

// forge test -vvv --fork-url <ARB_MAINNET_RPC_URL> --fork-block-number 6768454 --match-contract BondingManagerNullDelegateTransferBondFix
contract BondingManagerNullDelegateTransferBondFix is DSTest {
CheatCodes public constant CHEATS = CheatCodes(HEVM_ADDRESS);

Governor public constant GOVERNOR = Governor(0xD9dEd6f9959176F0A04dcf88a0d2306178A736a6);
Controller public constant CONTROLLER = Controller(0xD8E8328501E9645d16Cf49539efC04f734606ee4);
contract BondingManagerNullDelegateTransferBondFix is GovernorBaseTest {
BondingManager public constant BONDING_MANAGER = BondingManager(0x35Bcf3c30594191d53231E4FF333E8A770453e40);
MerkleSnapshot public constant MERKLE_SNAPSHOT = MerkleSnapshot(0x10736ffaCe687658F88a46D042631d182C7757f7);
L2Migrator public constant L2_MIGRATOR = L2Migrator(0x148D5b6B4df9530c7C76A810bd1Cdf69EC4c2085);

address public constant GOVERNOR_OWNER = 0x04F53A0bb244f015cC97731570BeD26F0229da05;
IL2Migrator public constant L2_MIGRATOR = IL2Migrator(0x148D5b6B4df9530c7C76A810bd1Cdf69EC4c2085);

bytes32 public constant BONDING_MANAGER_TARGET_ID = keccak256("BondingManagerTarget");

// Governor update
address[] public targets;
uint256[] public values;
bytes[] public datas;

BondingManager public newBondingManagerTarget;

function setUp() public {
newBondingManagerTarget = new BondingManager(address(CONTROLLER));

targets = [address(CONTROLLER)];
values = [0];

(, bytes20 gitCommitHash) = CONTROLLER.getContractInfo(BONDING_MANAGER_TARGET_ID);
datas = [

stageAndExecuteOne(
address(CONTROLLER),
0,
abi.encodeWithSelector(
CONTROLLER.setContractInfo.selector,
BONDING_MANAGER_TARGET_ID,
address(newBondingManagerTarget),
gitCommitHash
)
];
);
}

function testUpgrade() public {
(, bytes20 gitCommitHash) = CONTROLLER.getContractInfo(BONDING_MANAGER_TARGET_ID);

Governor.Update memory update = Governor.Update({ target: targets, value: values, data: datas, nonce: 0 });

// Impersonate Governor owner
CHEATS.prank(GOVERNOR_OWNER);
GOVERNOR.stage(update, 0);
GOVERNOR.execute(update);

// Check that new BondingManagerTarget is registered
(address infoAddr, bytes20 infoGitCommitHash) = CONTROLLER.getContractInfo(BONDING_MANAGER_TARGET_ID);
(address infoAddr, bytes20 infoGitCommitHash) = fetchContractInfo(BONDING_MANAGER_TARGET_ID);
assertEq(infoAddr, address(newBondingManagerTarget));
assertEq(infoGitCommitHash, gitCommitHash);
}

function testTransferBond() public {
CHEATS.mockCall(
address(MERKLE_SNAPSHOT),
abi.encodeWithSelector(MERKLE_SNAPSHOT.verify.selector),
Expand Down
Loading

0 comments on commit 7c51ea4

Please sign in to comment.