Skip to content

Commit

Permalink
Allow bridging if cast window is active (#95)
Browse files Browse the repository at this point in the history
* Only allow bridging when cast window is active
  • Loading branch information
alexkeating authored Feb 15, 2024
1 parent c449a77 commit 319d660
Show file tree
Hide file tree
Showing 18 changed files with 144 additions and 82 deletions.
3 changes: 2 additions & 1 deletion script/WormholeL2FlexibleVotingDeploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ contract WormholeL2FlexibleVotingDeploy is Script, ScriptConstants {
l1BlockAddress,
L2_CHAIN.wormholeChainId,
L1_CHAIN.wormholeChainId,
vm.envOr("CONTRACT_OWNER", msg.sender)
vm.envOr("CONTRACT_OWNER", msg.sender),
uint32(vm.envOr("CAST_WINDOW", uint256(1200)))
);

vm.broadcast();
Expand Down
5 changes: 3 additions & 2 deletions src/L2GovernorMetadata.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ abstract contract L2GovernorMetadata {
// before the end of the proposal to cast the vote.
/// @dev If the block conversion hack is removed from this contract, then this storage var is
/// probably not needed in this contract and can probably be moved back to the `L2VoteAggregator`
uint32 public constant CAST_VOTE_WINDOW = 1200;
uint32 public immutable CAST_VOTE_WINDOW;

error PastBlockNumber();

Expand All @@ -55,8 +55,9 @@ abstract contract L2GovernorMetadata {
event ProposalCanceled(uint256 proposalId);

/// @param _l1BlockAddress The address of the L1Block contract.
constructor(address _l1BlockAddress) {
constructor(address _l1BlockAddress, uint32 _castWindow) {
L1_BLOCK = IL1Block(_l1BlockAddress);
CAST_VOTE_WINDOW = _castWindow;
}

/// @notice Add proposal to internal storage.
Expand Down
15 changes: 11 additions & 4 deletions src/L2VoteAggregator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ abstract contract L2VoteAggregator is EIP712, L2GovernorMetadata, L2CountingFrac
function state(uint256 proposalId) external view virtual returns (ProposalState) {
L2GovernorMetadata.Proposal memory proposal = getProposal(proposalId);
if (VOTING_TOKEN.clock() < proposal.voteStart) return ProposalState.Pending;
else if (proposalVoteActive(proposalId)) return ProposalState.Active;
else if (proposalL2VoteActive(proposalId)) return ProposalState.Active;
else if (proposal.isCanceled) return ProposalState.Canceled;
else return ProposalState.Expired;
}
Expand Down Expand Up @@ -213,7 +213,7 @@ abstract contract L2VoteAggregator is EIP712, L2GovernorMetadata, L2CountingFrac
/// @notice Bridges a vote to the L1.
/// @param proposalId The id of the proposal to bridge.
function bridgeVote(uint256 proposalId) external payable {
if (!proposalVoteActive(proposalId)) revert ProposalInactive();
if (!proposalL1VoteActive(proposalId)) revert ProposalInactive();

(uint256 againstVotes, uint256 forVotes, uint256 abstainVotes) = proposalVotes(proposalId);

Expand Down Expand Up @@ -253,7 +253,7 @@ abstract contract L2VoteAggregator is EIP712, L2GovernorMetadata, L2CountingFrac
string memory reason,
bytes memory params
) internal virtual returns (uint256) {
if (!proposalVoteActive(proposalId)) revert ProposalInactive();
if (!proposalL2VoteActive(proposalId)) revert ProposalInactive();

L2GovernorMetadata.Proposal memory proposal = getProposal(proposalId);
uint256 weight = VOTING_TOKEN.getPastVotes(account, proposal.voteStart);
Expand All @@ -266,10 +266,17 @@ abstract contract L2VoteAggregator is EIP712, L2GovernorMetadata, L2CountingFrac
return weight;
}

function proposalVoteActive(uint256 proposalId) public view returns (bool active) {
function proposalL2VoteActive(uint256 proposalId) public view returns (bool active) {
L2GovernorMetadata.Proposal memory proposal = getProposal(proposalId);

return L1_BLOCK.number() <= internalVotingPeriodEnd(proposalId)
&& L1_BLOCK.number() >= proposal.voteStart && !proposal.isCanceled;
}

function proposalL1VoteActive(uint256 proposalId) public view returns (bool active) {
L2GovernorMetadata.Proposal memory proposal = getProposal(proposalId);

return L1_BLOCK.number() <= proposal.voteEnd && L1_BLOCK.number() >= proposal.voteStart
&& !proposal.isCanceled;
}
}
4 changes: 2 additions & 2 deletions src/WormholeL2GovernorMetadata.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import {L2GovernorMetadata} from "src/L2GovernorMetadata.sol";
contract WormholeL2GovernorMetadata is L2GovernorMetadata, WormholeReceiver {
/// @param _relayer The address of the WormholeL2GovernorMetadata contract.
/// @param _owner The address that will become the contract owner.
constructor(address _relayer, address _owner, address _l1BlockAddress)
constructor(address _relayer, address _owner, address _l1BlockAddress, uint32 _castWindow)
WormholeBase(_relayer)
WormholeReceiver(_owner)
L2GovernorMetadata(_l1BlockAddress)
L2GovernorMetadata(_l1BlockAddress, _castWindow)
{}

/// @notice Receives a message from L1 and saves the proposal metadata.
Expand Down
5 changes: 3 additions & 2 deletions src/WormholeL2VoteAggregator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ contract WormholeL2VoteAggregator is WormholeSender, L2VoteAggregator, WormholeL
address _l1BlockAddress,
uint16 _sourceChain,
uint16 _targetChain,
address _owner
address _owner,
uint32 _castWindow
)
L2VoteAggregator(_votingToken)
WormholeSender(_sourceChain, _targetChain)
WormholeL2GovernorMetadata(_relayer, _owner, _l1BlockAddress)
WormholeL2GovernorMetadata(_relayer, _owner, _l1BlockAddress, _castWindow)
{}

/// @notice Wormhole-specific implementation of `_bridgeVote`.
Expand Down
4 changes: 2 additions & 2 deletions src/optimized/WormholeL2GovernorMetadataOptimized.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ contract WormholeL2GovernorMetadataOptimized is WormholeL2GovernorMetadata {
/// @notice The ID of the proposal mapped to an internal proposal ID.
mapping(uint256 governorProposalId => uint16) public optimizedProposalIds;

constructor(address _relayer, address _owner, address _l1BlockAddress)
WormholeL2GovernorMetadata(_relayer, _owner, _l1BlockAddress)
constructor(address _relayer, address _owner, address _l1BlockAddress, uint32 _castWindow)
WormholeL2GovernorMetadata(_relayer, _owner, _l1BlockAddress, _castWindow)
{}

/// @inheritdoc L2GovernorMetadata
Expand Down
6 changes: 4 additions & 2 deletions src/optimized/WormholeL2VoteAggregatorCalldataCompressor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,17 @@ contract WormholeL2VoteAggregatorCalldataCompressor is WormholeL2VoteAggregator
address _l1BlockAddress,
uint16 _sourceChain,
uint16 _targetChain,
address _owner
address _owner,
uint32 _castWindow
)
WormholeL2VoteAggregator(
_votingToken,
_relayer,
_l1BlockAddress,
_sourceChain,
_targetChain,
_owner
_owner,
_castWindow
)
{}

Expand Down
Loading

0 comments on commit 319d660

Please sign in to comment.