Skip to content

Commit

Permalink
Adding more test cases for upgradeability
Browse files Browse the repository at this point in the history
  • Loading branch information
brickpop committed May 9, 2024
1 parent ecaab89 commit 3c84d12
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/OptimisticTokenVotingPlugin.sol
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ contract OptimisticTokenVotingPlugin is
IVotesUpgradeable public votingToken;

/// @notice The struct storing the governance settings.
OptimisticGovernanceSettings private governanceSettings;
OptimisticGovernanceSettings public governanceSettings;

/// @notice A mapping between proposal IDs and proposal information.
mapping(uint256 => Proposal) internal proposals;
Expand Down
67 changes: 67 additions & 0 deletions test/EmergencyMultisig.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ contract EmergencyMultisigTest is AragonTest {
);
event Approved(uint256 indexed proposalId, address indexed approver);
event Executed(uint256 indexed proposalId);
event Upgraded(address indexed implementation);

function setUp() public {
switchTo(alice);
Expand Down Expand Up @@ -1975,4 +1976,70 @@ contract EmergencyMultisigTest is AragonTest {

assertEq(allowFailureMap, 0, "Should be 0");
}

// Upgrade plugin

function test_UpgradeToRevertsWhenCalledFromNonUpgrader() public {
address _newImplementation = address(new EmergencyMultisig());

vm.expectRevert(
abi.encodeWithSelector(
DaoUnauthorized.selector, address(dao), address(plugin), alice, plugin.UPGRADE_PLUGIN_PERMISSION_ID()
)
);

plugin.upgradeTo(_newImplementation);

assertEq(plugin.implementation(), address(EMERGENCY_MULTISIG_BASE));
}

function test_UpgradeToAndCallRevertsWhenCalledFromNonUpgrader() public {
dao.grant(address(plugin), alice, plugin.UPDATE_MULTISIG_SETTINGS_PERMISSION_ID());
address _newImplementation = address(new EmergencyMultisig());

EmergencyMultisig.MultisigSettings memory settings =
EmergencyMultisig.MultisigSettings({onlyListed: true, minApprovals: 3, addresslistSource: multisig});

vm.expectRevert(
abi.encodeWithSelector(
DaoUnauthorized.selector, address(dao), address(plugin), alice, plugin.UPGRADE_PLUGIN_PERMISSION_ID()
)
);
plugin.upgradeToAndCall(
_newImplementation, abi.encodeCall(EmergencyMultisig.updateMultisigSettings, (settings))
);

assertEq(plugin.implementation(), address(EMERGENCY_MULTISIG_BASE));
}

function test_UpgradeToSucceedsWhenCalledFromUpgrader() public {
dao.grant(address(plugin), alice, plugin.UPGRADE_PLUGIN_PERMISSION_ID());

address _newImplementation = address(new EmergencyMultisig());

vm.expectEmit();
emit Upgraded(_newImplementation);

plugin.upgradeTo(_newImplementation);

assertEq(plugin.implementation(), address(_newImplementation));
}

function test_UpgradeToAndCallSucceedsWhenCalledFromUpgrader() public {
dao.grant(address(plugin), alice, plugin.UPGRADE_PLUGIN_PERMISSION_ID());
dao.grant(address(plugin), alice, plugin.UPDATE_MULTISIG_SETTINGS_PERMISSION_ID());

address _newImplementation = address(new EmergencyMultisig());

vm.expectEmit();
emit Upgraded(_newImplementation);

EmergencyMultisig.MultisigSettings memory settings =
EmergencyMultisig.MultisigSettings({onlyListed: true, minApprovals: 3, addresslistSource: multisig});
plugin.upgradeToAndCall(
_newImplementation, abi.encodeCall(EmergencyMultisig.updateMultisigSettings, (settings))
);

assertEq(plugin.implementation(), address(_newImplementation));
}
}
63 changes: 63 additions & 0 deletions test/Multisig.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ contract MultisigTest is AragonTest {
);
event Approved(uint256 indexed proposalId, address indexed approver);
event Executed(uint256 indexed proposalId);
event Upgraded(address indexed implementation);

function setUp() public {
vm.startPrank(alice);
Expand Down Expand Up @@ -3826,4 +3827,66 @@ contract MultisigTest is AragonTest {

assertEq(allowFailureMap, 0, "Should be 0");
}

// Upgrade plugin

function test_UpgradeToRevertsWhenCalledFromNonUpgrader() public {
address _newImplementation = address(new Multisig());

vm.expectRevert(
abi.encodeWithSelector(
DaoUnauthorized.selector, address(dao), address(plugin), alice, plugin.UPGRADE_PLUGIN_PERMISSION_ID()
)
);

plugin.upgradeTo(_newImplementation);

assertEq(plugin.implementation(), address(MULTISIG_BASE));
}

function test_UpgradeToAndCallRevertsWhenCalledFromNonUpgrader() public {
dao.grant(address(plugin), alice, plugin.UPDATE_MULTISIG_SETTINGS_PERMISSION_ID());
address _newImplementation = address(new Multisig());

Multisig.MultisigSettings memory settings =
Multisig.MultisigSettings({onlyListed: false, minApprovals: 2, destinationMinDuration: 14 days});

vm.expectRevert(
abi.encodeWithSelector(
DaoUnauthorized.selector, address(dao), address(plugin), alice, plugin.UPGRADE_PLUGIN_PERMISSION_ID()
)
);
plugin.upgradeToAndCall(_newImplementation, abi.encodeCall(Multisig.updateMultisigSettings, (settings)));

assertEq(plugin.implementation(), address(MULTISIG_BASE));
}

function test_UpgradeToSucceedsWhenCalledFromUpgrader() public {
dao.grant(address(plugin), alice, plugin.UPGRADE_PLUGIN_PERMISSION_ID());

address _newImplementation = address(new Multisig());

vm.expectEmit();
emit Upgraded(_newImplementation);

plugin.upgradeTo(_newImplementation);

assertEq(plugin.implementation(), address(_newImplementation));
}

function test_UpgradeToAndCallSucceedsWhenCalledFromUpgrader() public {
dao.grant(address(plugin), alice, plugin.UPGRADE_PLUGIN_PERMISSION_ID());
dao.grant(address(plugin), alice, plugin.UPDATE_MULTISIG_SETTINGS_PERMISSION_ID());

address _newImplementation = address(new Multisig());

vm.expectEmit();
emit Upgraded(_newImplementation);

Multisig.MultisigSettings memory settings =
Multisig.MultisigSettings({onlyListed: false, minApprovals: 2, destinationMinDuration: 14 days});
plugin.upgradeToAndCall(_newImplementation, abi.encodeCall(Multisig.updateMultisigSettings, (settings)));

assertEq(plugin.implementation(), address(_newImplementation));
}
}
74 changes: 64 additions & 10 deletions test/OptimisticTokenVotingPlugin.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1230,23 +1230,69 @@ contract OptimisticTokenVotingPluginTest is AragonTest {
plugin.updateOptimisticGovernanceSettings(newSettings);
}

function test_GovernanceSettingsReturnsTheRightValues() public {
(uint32 minVetoRatio, uint64 minDuration, uint256 minProposerVotingPower) = plugin.governanceSettings();

assertEq(minVetoRatio, uint32(RATIO_BASE / 10));
assertEq(minDuration, 10 days);
assertEq(minProposerVotingPower, 0);

// Deploy a new plugin instance
OptimisticTokenVotingPlugin.OptimisticGovernanceSettings memory newSettings = OptimisticTokenVotingPlugin
.OptimisticGovernanceSettings({
minVetoRatio: uint32(RATIO_BASE / 2),
minDuration: 0,
minProposerVotingPower: 1234
});

plugin = OptimisticTokenVotingPlugin(
createProxyAndCall(
address(OPTIMISTIC_BASE),
abi.encodeCall(OptimisticTokenVotingPlugin.initialize, (dao, newSettings, votingToken))
)
);

(minVetoRatio, minDuration, minProposerVotingPower) = plugin.governanceSettings();
assertEq(minVetoRatio, uint32(RATIO_BASE / 2));
assertEq(minDuration, 0);
assertEq(minProposerVotingPower, 1234);

// updated settings
dao.grant(address(plugin), alice, plugin.UPDATE_OPTIMISTIC_GOVERNANCE_SETTINGS_PERMISSION_ID());

newSettings = OptimisticTokenVotingPlugin.OptimisticGovernanceSettings({
minVetoRatio: uint32(RATIO_BASE / 5),
minDuration: 15 days,
minProposerVotingPower: 5 ether
});

plugin.updateOptimisticGovernanceSettings(newSettings);

(minVetoRatio, minDuration, minProposerVotingPower) = plugin.governanceSettings();
assertEq(minVetoRatio, uint32(RATIO_BASE / 5));
assertEq(minDuration, 15 days);
assertEq(minProposerVotingPower, 5 ether);
}

// Upgrade plugin
function test_UpgradeToRevertsWhenCalledFromNonUpgrader() public {
address _pluginBase = address(new OptimisticTokenVotingPlugin());
address _newImplementation = address(new OptimisticTokenVotingPlugin());

vm.expectRevert(
abi.encodeWithSelector(
DaoUnauthorized.selector, address(dao), address(plugin), alice, plugin.UPGRADE_PLUGIN_PERMISSION_ID()
)
);

plugin.upgradeTo(_pluginBase);
plugin.upgradeTo(_newImplementation);

assertEq(plugin.implementation(), address(OPTIMISTIC_BASE));
}

function test_UpgradeToAndCallRevertsWhenCalledFromNonUpgrader() public {
dao.grant(address(plugin), alice, plugin.UPDATE_OPTIMISTIC_GOVERNANCE_SETTINGS_PERMISSION_ID());

address _pluginBase = address(new OptimisticTokenVotingPlugin());
address _newImplementation = address(new OptimisticTokenVotingPlugin());

OptimisticTokenVotingPlugin.OptimisticGovernanceSettings memory settings = OptimisticTokenVotingPlugin
.OptimisticGovernanceSettings({
Expand All @@ -1262,29 +1308,34 @@ contract OptimisticTokenVotingPluginTest is AragonTest {
);

plugin.upgradeToAndCall(
_pluginBase, abi.encodeCall(OptimisticTokenVotingPlugin.updateOptimisticGovernanceSettings, (settings))
_newImplementation,
abi.encodeCall(OptimisticTokenVotingPlugin.updateOptimisticGovernanceSettings, (settings))
);

assertEq(plugin.implementation(), address(OPTIMISTIC_BASE));
}

function test_UpgradeToSucceedsWhenCalledFromUpgrader() public {
dao.grant(address(plugin), alice, plugin.UPGRADE_PLUGIN_PERMISSION_ID());

address _pluginBase = address(new OptimisticTokenVotingPlugin());
address _newImplementation = address(new OptimisticTokenVotingPlugin());

vm.expectEmit();
emit Upgraded(_pluginBase);
emit Upgraded(_newImplementation);

plugin.upgradeTo(_newImplementation);

plugin.upgradeTo(_pluginBase);
assertEq(plugin.implementation(), address(_newImplementation));
}

function test_UpgradeToAndCallSucceedsWhenCalledFromUpgrader() public {
dao.grant(address(plugin), alice, plugin.UPGRADE_PLUGIN_PERMISSION_ID());
dao.grant(address(plugin), alice, plugin.UPDATE_OPTIMISTIC_GOVERNANCE_SETTINGS_PERMISSION_ID());

address _pluginBase = address(new OptimisticTokenVotingPlugin());
address _newImplementation = address(new OptimisticTokenVotingPlugin());

vm.expectEmit();
emit Upgraded(_pluginBase);
emit Upgraded(_newImplementation);

OptimisticTokenVotingPlugin.OptimisticGovernanceSettings memory settings = OptimisticTokenVotingPlugin
.OptimisticGovernanceSettings({
Expand All @@ -1293,7 +1344,10 @@ contract OptimisticTokenVotingPluginTest is AragonTest {
minProposerVotingPower: 1 ether
});
plugin.upgradeToAndCall(
_pluginBase, abi.encodeCall(OptimisticTokenVotingPlugin.updateOptimisticGovernanceSettings, (settings))
_newImplementation,
abi.encodeCall(OptimisticTokenVotingPlugin.updateOptimisticGovernanceSettings, (settings))
);

assertEq(plugin.implementation(), address(_newImplementation));
}
}

0 comments on commit 3c84d12

Please sign in to comment.