Skip to content

Commit

Permalink
Fix use of proposal id
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkeating committed Oct 4, 2023
1 parent 3218636 commit 7723f64
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 9 deletions.
27 changes: 27 additions & 0 deletions src/routers/WormholeL2GovernorMetadataOptimized.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import {WormholeL2GovernorMetadata} from "src/WormholeL2GovernorMetadata.sol";

contract WormholeL2GovernorMetadataOptimized is WormholeL2GovernorMetadata {
/// @notice
uint16 internal _proposalId = 1;

/// @notice The id of the proposal mapped to the proposal metadata.
mapping(uint256 governorProposalId => uint16) public optimizedProposalIds;

constructor(address _relayer, address _owner) WormholeL2GovernorMetadata(_relayer, _owner) {}

function _addProposal(uint256 proposalId, uint256 voteStart, uint256 voteEnd, bool isCanceled)
internal
virtual
override
{
super._addProposal(proposalId, voteStart, voteEnd, isCanceled);
uint16 internalId = optimizedProposalIds[proposalId];
if (internalId == 0) {
optimizedProposalIds[proposalId] = _proposalId;
++_proposalId;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import {WormholeL2GovernorMetadataOptimized} from
"src/routers/WormholeL2GovernorMetadataOptimized.sol";

contract WormholeL2GovernorMetadataOptimizedHarness is WormholeL2GovernorMetadataOptimized {
constructor(address _core, address _owner) WormholeL2GovernorMetadataOptimized(_core, _owner) {}

function exposed_addProposal(
uint256 proposalId,
uint256 voteStart,
uint256 voteEnd,
bool isCanceled
) external {
_addProposal(proposalId, voteStart, voteEnd, isCanceled);
}
}
32 changes: 26 additions & 6 deletions test/mock/GovernorMetadataMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
pragma solidity ^0.8.0;

import {WormholeL2GovernorMetadata} from "src/WormholeL2GovernorMetadata.sol";
import {WormholeL2GovernorMetadataOptimized} from
"src/routers/WormholeL2GovernorMetadataOptimized.sol";
import {L2GovernorMetadata} from "src/L2GovernorMetadata.sol";

contract GovernorMetadataMock is WormholeL2GovernorMetadata {
constructor(address _core) WormholeL2GovernorMetadata(_core, msg.sender) {
_proposals[1] =
Proposal({voteStart: block.number, voteEnd: block.number + 3000, isCanceled: false});
}

abstract contract GovernorMetadataMockBase is L2GovernorMetadata {
function createProposal(uint256 proposalId, uint128 timeToProposalEnd)
public
returns (Proposal memory)
Expand All @@ -35,3 +33,25 @@ contract GovernorMetadataMock is WormholeL2GovernorMetadata {
return proposal;
}
}

contract GovernorMetadataMock is GovernorMetadataMockBase, WormholeL2GovernorMetadata {
constructor(address _core) WormholeL2GovernorMetadata(_core, msg.sender) {
_proposals[1] =
Proposal({voteStart: block.number, voteEnd: block.number + 3000, isCanceled: false});
}
}

contract GovernorMetadataOptimizedMock is
GovernorMetadataMockBase,
WormholeL2GovernorMetadataOptimized
{
constructor(address _core) WormholeL2GovernorMetadataOptimized(_core, msg.sender) {}

function _addProposal(uint256 proposalId, uint256 voteStart, uint256 voteEnd, bool isCanceled)
internal
virtual
override(L2GovernorMetadata, WormholeL2GovernorMetadataOptimized)
{
WormholeL2GovernorMetadataOptimized._addProposal(proposalId, voteStart, voteEnd, isCanceled);
}
}
130 changes: 130 additions & 0 deletions test/routers/WormholeL2GovernorMetadataOptimized.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import {Test} from "forge-std/Test.sol";

import {L2GovernorMetadata} from "src/L2GovernorMetadata.sol";
import {WormholeL2GovernorMetadataOptimizedHarness} from
"test/harness/optimized/WormholeL2GovernorMetadataOptimizedHarness.sol";
import {TestConstants} from "test/Constants.sol";
import {WormholeReceiver} from "src/WormholeReceiver.sol";

// Test one proposal works
// test two proposals
// What if the same id is bridged twice

contract WormholeL2GovernorMetadataOptimizedTest is TestConstants {
WormholeL2GovernorMetadataOptimizedHarness l2GovernorMetadata;

function setUp() public {
l2GovernorMetadata =
new WormholeL2GovernorMetadataOptimizedHarness(L2_CHAIN.wormholeRelayer, msg.sender);
vm.prank(l2GovernorMetadata.owner());
l2GovernorMetadata.setRegisteredSender(
L1_CHAIN.wormholeChainId, MOCK_WORMHOLE_SERIALIZED_ADDRESS
);
}
}

contract _AddProposal is WormholeL2GovernorMetadataOptimizedTest {
function testFuzz_CorrectlyAddASingleProposal(
uint256 proposalId,
uint256 l1VoteStart,
uint256 l1VoteEnd,
bool isCanceled
) public {
l2GovernorMetadata.exposed_addProposal(proposalId, l1VoteStart, l1VoteEnd, isCanceled);
L2GovernorMetadata.Proposal memory l2Proposal = l2GovernorMetadata.getProposal(proposalId);
uint256 internalProposalId = l2GovernorMetadata.optimizedProposalIds(proposalId);

assertEq(l2Proposal.voteStart, l1VoteStart, "Vote start has been incorrectly set");
assertEq(l2Proposal.voteEnd, l1VoteEnd, "Vote start has been incorrectly set");
assertEq(l2Proposal.isCanceled, isCanceled, "Canceled status of the vote is incorrect");
assertEq(internalProposalId, 1, "Internal id is incorrect");
}

function testFuzz_CorrectlyAddAMultipleProposals(
uint256 firstProposalId,
uint256 firstL1VoteStart,
uint256 firstL1VoteEnd,
bool firstIsCanceled,
uint256 secondProposalId,
uint256 secondL1VoteStart,
uint256 secondL1VoteEnd,
bool secondIsCanceled,
uint256 thirdProposalId,
uint256 thirdL1VoteStart,
uint256 thirdL1VoteEnd,
bool thirdIsCanceled
) public {
vm.assume(firstProposalId != secondProposalId);
vm.assume(thirdProposalId != secondProposalId);
vm.assume(thirdProposalId != firstProposalId);

l2GovernorMetadata.exposed_addProposal(
firstProposalId, firstL1VoteStart, firstL1VoteEnd, firstIsCanceled
);
l2GovernorMetadata.exposed_addProposal(
secondProposalId, secondL1VoteStart, secondL1VoteEnd, secondIsCanceled
);
l2GovernorMetadata.exposed_addProposal(
thirdProposalId, thirdL1VoteStart, thirdL1VoteEnd, thirdIsCanceled
);

L2GovernorMetadata.Proposal memory thirdL2Proposal =
l2GovernorMetadata.getProposal(thirdProposalId);
uint256 thirdInternalProposalId = l2GovernorMetadata.optimizedProposalIds(thirdProposalId);

assertEq(
thirdL2Proposal.voteStart, thirdL1VoteStart, "Third vote start has been incorrectly set"
);
assertEq(thirdL2Proposal.voteEnd, thirdL1VoteEnd, "Third vote end has been incorrectly set");
assertEq(
thirdL2Proposal.isCanceled, thirdIsCanceled, "Third canceled status of the vote is incorrect"
);
assertEq(thirdInternalProposalId, 3, "Third internal id is incorrect");
}

function testFuzz_CorrectlyUpdateTheSameProposal(
uint256 proposalId,
uint256 initialVoteStart,
uint256 initialVoteEnd,
bool initialIsCanceled,
uint256 updatedVoteStart,
uint256 updatedVoteEnd,
bool updatedIsCanceled
) public {
l2GovernorMetadata.exposed_addProposal(
proposalId, initialVoteStart, initialVoteEnd, initialIsCanceled
);

L2GovernorMetadata.Proposal memory l2Proposal = l2GovernorMetadata.getProposal(proposalId);
uint256 initialInternalProposalId = l2GovernorMetadata.optimizedProposalIds(proposalId);

assertEq(l2Proposal.voteStart, initialVoteStart, "Initial vote start has been incorrectly set");
assertEq(l2Proposal.voteEnd, initialVoteEnd, "Initial vote end has been incorrectly set");
assertEq(
l2Proposal.isCanceled, initialIsCanceled, "Initial canceled status of the vote is incorrect"
);
assertEq(initialInternalProposalId, 1, "Initial internal id is incorrect");

l2GovernorMetadata.exposed_addProposal(
proposalId, updatedVoteStart, updatedVoteEnd, updatedIsCanceled
);

L2GovernorMetadata.Proposal memory updatedL2Proposal =
l2GovernorMetadata.getProposal(proposalId);
uint256 updatedInternalProposalId = l2GovernorMetadata.optimizedProposalIds(proposalId);

assertEq(
updatedL2Proposal.voteStart, updatedVoteStart, "Updated vote start has been incorrectly set"
);
assertEq(updatedL2Proposal.voteEnd, updatedVoteEnd, "Updated vote end has been incorrectly set");
assertEq(
updatedL2Proposal.isCanceled,
updatedIsCanceled,
"Updated canceled status of the vote is incorrect"
);
assertEq(updatedInternalProposalId, 1, "Updated internal id is incorrect");
}
}
8 changes: 5 additions & 3 deletions test/routers/WormholeL2VoteAggregatorRouter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import {WormholeL2VoteAggregatorRouter} from "src/routers/WormholeL2VoteAggregat
import {WormholeL2VoteAggregator} from "src/WormholeL2VoteAggregator.sol";
import {FakeERC20} from "src/FakeERC20.sol";
import {WormholeL2ERC20} from "src/WormholeL2ERC20.sol";
import {GovernorMetadataMock} from "test/mock/GovernorMetadataMock.sol";
import {
GovernorMetadataOptimizedMock, GovernorMetadataMock
} from "test/mock/GovernorMetadataMock.sol";
import {L2GovernorMetadata} from "src/L2GovernorMetadata.sol";
import {L2VoteAggregator} from "src/L2VoteAggregator.sol";
import {TestConstants} from "test/Constants.sol";
Expand Down Expand Up @@ -56,7 +58,7 @@ contract WormholeL2VoteAggregatorRouterHarness is WormholeL2VoteAggregatorRouter
contract WormholeL2ERC20RouterTest is Test, TestConstants {
WormholeL2VoteAggregatorRouter router;
FakeERC20 l2Erc20;
GovernorMetadataMock l2GovernorMetadataMock;
GovernorMetadataOptimizedMock l2GovernorMetadataMock;
address voterAddress;
uint256 privateKey;
WormholeL2VoteAggregatorRouterHarness routerHarness;
Expand All @@ -68,7 +70,7 @@ contract WormholeL2ERC20RouterTest is Test, TestConstants {
function setUp() public {
(voterAddress, privateKey) = makeAddrAndKey("voter");
L1Block l1Block = new L1Block();
l2GovernorMetadataMock = new GovernorMetadataMock(L2_CHAIN.wormholeRelayer);
l2GovernorMetadataMock = new GovernorMetadataOptimizedMock(L2_CHAIN.wormholeRelayer);
l2Erc20 = new FakeERC20("GovExample", "GOV");
router =
new WormholeL2VoteAggregatorRouter(address(l2Erc20), L2_CHAIN.wormholeRelayer, address(l2GovernorMetadataMock), address(l1Block), L2_CHAIN.wormholeChainId, L1_CHAIN.wormholeChainId);
Expand Down

0 comments on commit 7723f64

Please sign in to comment.