Skip to content

Commit

Permalink
SRC-10: add custom errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mrhouzlane committed Mar 4, 2024
1 parent b39b527 commit 1b311f4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 38 deletions.
61 changes: 31 additions & 30 deletions packages/core/contracts/governance/MetaHumanGovernor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}(
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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][
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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
);
}
}
Expand Down Expand Up @@ -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
);
}
}
Expand Down
16 changes: 8 additions & 8 deletions packages/core/test/MetaHumanGovernor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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 () {
Expand Down Expand Up @@ -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 () {
Expand All @@ -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 () {
Expand Down

0 comments on commit 1b311f4

Please sign in to comment.