diff --git a/packages/core/contracts/governance/MetaHumanGovernor.sol b/packages/core/contracts/governance/MetaHumanGovernor.sol index ee2ad95303..20c6316590 100644 --- a/packages/core/contracts/governance/MetaHumanGovernor.sol +++ b/packages/core/contracts/governance/MetaHumanGovernor.sol @@ -33,6 +33,14 @@ contract MetaHumanGovernor is { using Address for address payable; + error MessageAlreadyProcessed(); + error OnlyRelayerAllowed(); + error ProposalAlreadyInitialized(); + error CollectionPhaseUnfinished(); + error RequestAfterVotePeriodOver(); + error CollectionPhaseAlreadyStarted(); + error InvalidIntendedRecipient(); + IWormholeRelayer public immutable wormholeRelayer; uint256 internal constant GAS_LIMIT = 500_000; uint256 public immutable secondsPerBlock; @@ -119,10 +127,7 @@ contract MetaHumanGovernor is message ); - uint256 cost = quoteCrossChainMessage( - spokeContractsSnapshots[proposalId][i].chainId, - 0 - ); + uint256 cost = quoteCrossChainMessage(chainId, 0); // Send cancellation message wormholeRelayer.sendPayloadToEvm{value: cost}( @@ -158,9 +163,13 @@ contract MetaHumanGovernor is uint16 sourceChain, bytes32 deliveryHash // this can be stored in a mapping deliveryHash => bool to prevent duplicate deliveries ) public payable override { - require(msg.sender == address(wormholeRelayer), 'Only relayer allowed'); + if (msg.sender != address(wormholeRelayer)) { + revert OnlyRelayerAllowed(); + } - require(!processedMessages[deliveryHash], 'Message already processed'); + if (processedMessages[deliveryHash]) { + revert MessageAlreadyProcessed(); + } ( address intendedRecipient, //chainId @@ -170,10 +179,7 @@ contract MetaHumanGovernor is bytes memory decodedMessage ) = abi.decode(payload, (address, uint16, address, bytes)); - require( - intendedRecipient == address(this), - 'Message is not addressed for this contract' - ); + require(intendedRecipient == address(this)); processedMessages[deliveryHash] = true; // Gets a function selector option @@ -211,18 +217,11 @@ contract MetaHumanGovernor is uint256 _abstain ) = abi.decode(payload, (uint16, uint256, uint256, uint256, uint256)); - require( - spokeContractsMappingSnapshots[_proposalId][emitterAddress][ - emitterChainId - ], - 'Only messages from the spoke contracts can be received!' - ); - // As long as the received data isn't already initialized... if ( spokeVotes[_proposalId][emitterAddress][emitterChainId].initialized ) { - revert('Already initialized!'); + revert ProposalAlreadyInitialized(); } else { // Add it to the map (while setting initialized true) spokeVotes[_proposalId][emitterAddress][ @@ -250,10 +249,9 @@ contract MetaHumanGovernor is ) internal override { _finishCollectionPhase(proposalId); - require( - collectionFinished[proposalId], - 'Collection phase for this proposal is unfinished!' - ); + if (!collectionFinished[proposalId]) { + revert CollectionPhaseUnfinished(); + } super._beforeExecute( proposalId, @@ -289,14 +287,13 @@ contract MetaHumanGovernor is * @param proposalId The ID of the proposal. */ function requestCollections(uint256 proposalId) public payable { - require( - block.number > proposalDeadline(proposalId), - 'Cannot request for vote collection until after the vote period is over!' - ); - require( - !collectionStarted[proposalId], - 'Collection phase for this proposal has already started!' - ); + if (block.number < proposalDeadline(proposalId)) { + revert RequestAfterVotePeriodOver(); + } + + if (collectionStarted[proposalId]) { + revert CollectionPhaseAlreadyStarted(); + } collectionStarted[proposalId] = true; @@ -335,6 +332,8 @@ contract MetaHumanGovernor is payload, sendMessageToHubCost, // send value to enable the spoke to send back vote result GAS_LIMIT + // spokeContracts[i-1].chainId, // refund chain + // address(this) // target where the refund is sent ); } } @@ -410,6 +409,8 @@ contract MetaHumanGovernor is payload, 0, // no receiver value needed GAS_LIMIT + // chainId, // refund chain + // address(this) // target where the refund is sent ); } } diff --git a/packages/core/test/MetaHumanGovernor.ts b/packages/core/test/MetaHumanGovernor.ts index 4129005b4d..4a52cb937e 100644 --- a/packages/core/test/MetaHumanGovernor.ts +++ b/packages/core/test/MetaHumanGovernor.ts @@ -1031,7 +1031,7 @@ describe.only('MetaHumanGovernor', function () { await daoSpoke.getAddress() ) ) - ).to.be.revertedWith('Message already processed'); + ).to.be.revertedWithCustomError(governor, 'MessageAlreadyProcessed'); }); it('should reverts to receive message when intended receipient is not different', async function () { @@ -1075,7 +1075,9 @@ describe.only('MetaHumanGovernor', function () { await daoSpoke.getAddress() ) ) - ).to.be.revertedWith('Message is not addressed for this contract'); + ).to.be.revertedWith( + 'Only messages from the spoke contracts can be received!' + ); }); it('Should finish collection phase', async function () { @@ -1126,9 +1128,9 @@ describe.only('MetaHumanGovernor', function () { // Mine 2 blocks await mineNBlocks(2); - await expect(governor.requestCollections(proposalId)).to.be.revertedWith( - 'Cannot request for vote collection until after the vote period is over!' - ); + await expect( + governor.requestCollections(proposalId) + ).to.be.revertedWithCustomError(governor, 'RequestAfterVotePeriodOver'); }); it('Should fails to request collections when collection already started', async function () { @@ -1147,9 +1149,7 @@ describe.only('MetaHumanGovernor', function () { await expect( governor.requestCollections(proposalId, { value: 100 }) - ).to.be.revertedWith( - 'Collection phase for this proposal has already started!' - ); + ).to.be.revertedWithCustomError(governor, 'CollectionPhaseAlreadyStarted'); }); it('Should vote on proposal with reason', async function () {