Skip to content

Commit

Permalink
ProposalId adapted tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brickpop committed May 13, 2024
1 parent 1fbe1a9 commit e52b008
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 24 deletions.
47 changes: 47 additions & 0 deletions src/OptimisticTokenVotingPlugin.sol
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,53 @@ contract OptimisticTokenVotingPlugin is
_updateOptimisticGovernanceSettings(_governanceSettings);
}

/// @notice Splits the components behind the given proposal ID
/// @param _proposalId The ID to split
function parseProposalId(uint256 _proposalId)
public
pure
returns (uint256 counter, uint64 startDate, uint64 endDate)
{
counter = _proposalId & type(uint64).max;
startDate = uint64((_proposalId & (uint256(type(uint64).max) << 128)) >> 128);
endDate = uint64((_proposalId & (uint256(type(uint64).max) << 64)) >> 64);
}

/// @dev Creates a new proposal ID, containing the start and end timestamps
function _makeProposalId(uint64 _startDate, uint64 _endDate) internal returns (uint256 proposalId) {
proposalId = uint256(_startDate) << 128 | uint256(_endDate) << 64;
proposalId |= _createProposalId();
}

/// @notice Internal function to create a proposal.
/// @param _metadata The proposal metadata.
/// @param _startDate The start date of the proposal in seconds.
/// @param _endDate The end date of the proposal in seconds.
/// @param _allowFailureMap A bitmap allowing the proposal to succeed, even if individual actions might revert. If the bit at index `i` is 1, the proposal succeeds even if the `i`th action reverts. A failure map value of 0 requires every action to not revert.
/// @param _actions The actions that will be executed after the proposal passes.
/// @return proposalId The ID of the proposal.
function _createProposal(
address _creator,
bytes calldata _metadata,
uint64 _startDate,
uint64 _endDate,
IDAO.Action[] calldata _actions,
uint256 _allowFailureMap
) internal override returns (uint256 proposalId) {
// Returns an autoincremental number
proposalId = _makeProposalId(_startDate, _endDate);

emit ProposalCreated({
proposalId: proposalId,
creator: _creator,
metadata: _metadata,
startDate: _startDate,
endDate: _endDate,
actions: _actions,
allowFailureMap: _allowFailureMap
});
}

/// @notice Internal implementation
function _updateOptimisticGovernanceSettings(OptimisticGovernanceSettings calldata _governanceSettings) internal {
// Require the minimum veto ratio value to be in the interval [0, 10^6], because `>=` comparision is used.
Expand Down
14 changes: 10 additions & 4 deletions test/EmergencyMultisig.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1542,7 +1542,8 @@ contract EmergencyMultisigTest is AragonTest {
vm.expectEmit();
emit Executed(pid);
vm.expectEmit();
emit ProposalCreated(0, address(plugin), 10, 10, "", actions, 0);
uint256 targetPid = 10 << 128 | 10 << 64;
emit ProposalCreated(targetPid, address(plugin), 10, 10, "", actions, 0);
plugin.execute(pid, actions);

// 2
Expand Down Expand Up @@ -1573,7 +1574,8 @@ contract EmergencyMultisigTest is AragonTest {
vm.expectEmit();
emit Executed(pid);
vm.expectEmit();
emit ProposalCreated(1, address(plugin), 20, 20, "ipfs://", actions, 0);
targetPid = (20 << 128 | 20 << 64) + 1;
emit ProposalCreated(targetPid, address(plugin), 20, 20, "ipfs://", actions, 0);
plugin.execute(pid, actions);
}

Expand Down Expand Up @@ -1938,7 +1940,9 @@ contract EmergencyMultisigTest is AragonTest {
plugin.execute(pid, submittedActions);

// Check round
(open, executed, parameters, vetoTally, retrievedActions, allowFailureMap) = optimisticPlugin.getProposal(pid);
uint256 targetPid = (uint256(block.timestamp) << 128 | uint256(block.timestamp) << 64);
(open, executed, parameters, vetoTally, retrievedActions, allowFailureMap) =
optimisticPlugin.getProposal(targetPid);

assertEq(open, false, "Should not be open");
assertEq(executed, true, "Should be executed");
Expand Down Expand Up @@ -1987,7 +1991,9 @@ contract EmergencyMultisigTest is AragonTest {
plugin.execute(pid, submittedActions);

// Check round
(open, executed, parameters, vetoTally, retrievedActions, allowFailureMap) = optimisticPlugin.getProposal(pid);
targetPid = (uint256(block.timestamp) << 128 | uint256(block.timestamp) << 64) + 1;
(open, executed, parameters, vetoTally, retrievedActions, allowFailureMap) =
optimisticPlugin.getProposal(targetPid);

assertEq(open, false, "Should not be open");
assertEq(executed, true, "Should be executed");
Expand Down
24 changes: 17 additions & 7 deletions test/Multisig.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2748,9 +2748,10 @@ contract MultisigTest is AragonTest {
// event
vm.expectEmit();
emit Executed(pid);
uint256 targetPid = uint256(block.timestamp) << 128 | uint256(block.timestamp + 4 days) << 64;
vm.expectEmit();
emit ProposalCreated(
0,
targetPid,
address(plugin),
uint64(block.timestamp),
uint64(block.timestamp) + 4 days,
Expand Down Expand Up @@ -2792,9 +2793,10 @@ contract MultisigTest is AragonTest {
// events
vm.expectEmit();
emit Executed(pid);
targetPid = (uint256(block.timestamp + 10 days) << 128 | uint256(block.timestamp + 50 days) << 64) + 1;
vm.expectEmit();
emit ProposalCreated(
1,
targetPid,
address(plugin),
10 days,
50 days,
Expand Down Expand Up @@ -2888,9 +2890,11 @@ contract MultisigTest is AragonTest {
emit Approved(pid, carol);
vm.expectEmit();
emit Executed(pid);

uint256 targetPid = (uint256(block.timestamp) << 128 | uint256(block.timestamp + 4 days) << 64);
vm.expectEmit();
emit ProposalCreated(
0, // foreign pid
targetPid,
address(plugin),
uint64(block.timestamp),
uint64(block.timestamp) + 4 days,
Expand Down Expand Up @@ -2931,9 +2935,11 @@ contract MultisigTest is AragonTest {
emit Approved(pid, carol);
vm.expectEmit();
emit Executed(pid);

targetPid = (uint256(5 days) << 128 | uint256(20 days) << 64) + 1;
vm.expectEmit();
emit ProposalCreated(
1, // foreign pid
targetPid, // foreign pid
address(plugin),
5 days,
20 days,
Expand Down Expand Up @@ -2974,9 +2980,11 @@ contract MultisigTest is AragonTest {
emit Approved(pid, carol);
vm.expectEmit();
emit Executed(pid);

targetPid = (uint256(3 days) << 128 | uint256(500 days) << 64) + 2;
vm.expectEmit();
emit ProposalCreated(
2, // foreign pid
targetPid,
address(plugin),
3 days,
500 days,
Expand Down Expand Up @@ -3733,14 +3741,15 @@ contract MultisigTest is AragonTest {
plugin.execute(pid);

// Check round
uint targetPid = uint256(4 days) << 64; // start=0, end=4d, counter=0
(
open,
executed,
parameters,
vetoTally,
actions,
allowFailureMap
) = optimisticPlugin.getProposal(pid);
) = optimisticPlugin.getProposal(targetPid);

assertEq(open, true, "Should be open");
assertEq(executed, false, "Should not be executed");
Expand Down Expand Up @@ -3800,14 +3809,15 @@ contract MultisigTest is AragonTest {
plugin.execute(pid);

// Check round
targetPid = (uint256(block.timestamp + 1 days) << 128 | uint256(block.timestamp + 6 days) << 64) + 1;
(
open,
executed,
parameters,
vetoTally,
actions,
allowFailureMap
) = optimisticPlugin.getProposal(pid);
) = optimisticPlugin.getProposal(targetPid);

assertEq(open, false, "Should not be open");
assertEq(executed, false, "Should not be executed");
Expand Down
42 changes: 29 additions & 13 deletions test/OptimisticTokenVotingPlugin.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@ contract OptimisticTokenVotingPluginTest is AragonTest {
vm.startPrank(alice);

(dao, plugin, votingToken) = makeDaoWithOptimisticTokenVoting(alice);

// The plugin can execute on the DAO
dao.grant(address(dao), address(plugin), dao.EXECUTE_PERMISSION_ID());

// Alice can create proposals on the plugin
dao.grant(address(plugin), alice, plugin.PROPOSER_PERMISSION_ID());
}

// Initialize
Expand Down Expand Up @@ -378,10 +372,9 @@ contract OptimisticTokenVotingPluginTest is AragonTest {
vm.startPrank(bob);

IDAO.Action[] memory actions = new IDAO.Action[](0);
uint256 proposalId = plugin.createProposal("", actions, 0, 0, 0);
assertEq(proposalId, 0);
proposalId = plugin.createProposal("", actions, 0, 0, 0);
assertEq(proposalId, 1);
uint256 proposalId1 = plugin.createProposal("", actions, 0, 0, 0);
uint256 proposalId2 = plugin.createProposal("", actions, 0, 0, 0);
assertEq(proposalId1 + 1, proposalId2, "Should be +1");
}

function test_CreateProposalRevertsWhenTheCallerOwnsLessThanTheMinimumVotingPower() public {
Expand Down Expand Up @@ -523,20 +516,43 @@ contract OptimisticTokenVotingPluginTest is AragonTest {

function test_CreateProposalReturnsTheProposalId() public {
IDAO.Action[] memory actions = new IDAO.Action[](0);

uint256 proposalId = plugin.createProposal("", actions, 0, 0, 0);
assertEq(proposalId == 0, true, "Should have created proposal 0");
uint256 expectedPid = uint256(block.timestamp) << 128 | uint256(block.timestamp + 10 days) << 64;
assertEq(proposalId, expectedPid, "Should have created proposal 0");

proposalId = plugin.createProposal("", actions, 0, 0, 0);
assertEq(proposalId == 1, true, "Should have created proposal 1");
expectedPid = (uint256(block.timestamp) << 128 | uint256(block.timestamp + 10 days) << 64) + 1;
assertEq(proposalId, expectedPid, "Should have created proposal 1");
}

function test_CreateProposalEmitsAnEvent() public {
uint256 expectedPid = uint256(block.timestamp) << 128 | uint256(block.timestamp + 10 days) << 64;

IDAO.Action[] memory actions = new IDAO.Action[](0);
vm.expectEmit();
emit ProposalCreated(0, alice, uint64(block.timestamp), uint64(block.timestamp + 10 days), "", actions, 0);
emit ProposalCreated(
expectedPid, alice, uint64(block.timestamp), uint64(block.timestamp + 10 days), "", actions, 0
);
plugin.createProposal("", actions, 0, 0, 0);
}

function test_ParseProposalIdReturnsTheRightValues() public {
IDAO.Action[] memory actions = new IDAO.Action[](0);
uint256 proposalId1 = plugin.createProposal("", actions, 0, 0, 0);

timeForward(23456);
uint256 proposalId2 = plugin.createProposal("", actions, 0, 0, 0);

(uint256 counter1, uint64 startDate1, uint64 endDate1) = plugin.parseProposalId(proposalId1);
(uint256 counter2, uint64 startDate2, uint64 endDate2) = plugin.parseProposalId(proposalId2);

assertEq(counter1, 0, "Counter should be 0");
assertEq(counter2, 1, "Counter should be 1");
assertEq(startDate2 - startDate1, 23456, "Date diff should be +23456");
assertEq(endDate2 - endDate1, 23456, "Date diff should be +23456");
}

function test_GetProposalReturnsTheRightValues() public {
vm.warp(500);
uint32 startDate = 600;
Expand Down
6 changes: 6 additions & 0 deletions test/base/AragonTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ contract AragonTest is Test {
)
);

// The plugin can execute on the DAO
dao.grant(address(dao), address(optimisticPlugin), dao.EXECUTE_PERMISSION_ID());

// Alice can create proposals on the plugin
dao.grant(address(optimisticPlugin), alice, optimisticPlugin.PROPOSER_PERMISSION_ID());

vm.label(address(dao), "dao");
vm.label(address(optimisticPlugin), "optimisticPlugin");

Expand Down

0 comments on commit e52b008

Please sign in to comment.