Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

evm: request execution to peer #12

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions evm/src/WormholeGuardiansAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ contract WormholeGuardiansAdapter is IWormholeGuardiansAdapter {
// =============== Public Getters ======================================================

/// @inheritdoc IWormholeGuardiansAdapter
function getPeer(uint16 chainId) public view returns (bytes32) {
return _getPeersStorage()[chainId];
function getPeer(uint16 chainId) public view returns (bytes32 peerContract) {
peerContract = _getPeersStorage()[chainId];
if (peerContract == bytes32(0)) {
revert UnregisteredPeer(chainId);
}
}

/// @inheritdoc IWormholeGuardiansAdapter
Expand Down Expand Up @@ -143,7 +146,7 @@ contract WormholeGuardiansAdapter is IWormholeGuardiansAdapter {

/// @inheritdoc IWormholeGuardiansAdapter
function setPeer(uint16 peerChain, bytes32 peerContract) external onlyAdmin {
if (peerChain == 0) {
if (peerChain == 0 || peerChain == ourChain) {
revert InvalidChain(peerChain);
}
if (peerContract == bytes32(0)) {
Expand Down
2 changes: 1 addition & 1 deletion evm/src/WormholeGuardiansAdapterWithExecutor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ contract WormholeGuardiansAdapterWithExecutor is WormholeGuardiansAdapter {

executor.requestExecution{value: execMsgValue}(
dstChain,
dstAddr.toBytes32(),
getPeer(dstChain),
refundAddr,
signedQuote,
ExecutorMessages.makeVAAV1Request(ourChain, emitterAddress, coreSeq),
Expand Down
7 changes: 6 additions & 1 deletion evm/src/interfaces/IWormholeGuardiansAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,16 @@ interface IWormholeGuardiansAdapter is IAdapter {
/// @dev Selector: 0xf839a0cb
error InvalidPeerZeroAddress();

/// @notice Error when the chain ID doesn't match this chain.
/// @notice Error when the chain ID is zero or our chain.
/// @dev Selector: 0x587c94c3
/// @param chain The Wormhole chain ID of the peer.
error InvalidChain(uint16 chain);

/// @notice Error when the peer adapter is not registered for the given chain.
/// @dev Selector: 0xa98c9e21
/// @param chain The Wormhole chain ID of the peer.
error UnregisteredPeer(uint16 chain);

/// @notice Error when the peer adapter is invalid.
/// @dev Selector: 0xaf1181fa
/// @param chain The Wormhole chain ID of the peer.
Expand Down
19 changes: 13 additions & 6 deletions evm/test/WormholeGuardiansAdapter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ contract WormholeGuardiansAdapterTest is Test {
WormholeGuardiansAdapterForTest public srcAdapter;
MockWormhole destWormhole;
WormholeGuardiansAdapterForTest public destAdapter;
WormholeGuardiansAdapterForTest public otherDestAdapter;
uint8 consistencyLevel = 200;

uint16 ourChain = 42;
Expand All @@ -77,6 +78,9 @@ contract WormholeGuardiansAdapterTest is Test {
destAdapter = new WormholeGuardiansAdapterForTest(
destChain, admin, address(destEndpoint), address(destWormhole), consistencyLevel
);
otherDestAdapter = new WormholeGuardiansAdapterForTest(
destChain, admin, address(destEndpoint), address(destWormhole), consistencyLevel
);

// Give everyone some money to play with.
vm.deal(integratorAddr, 1 ether);
Expand Down Expand Up @@ -266,11 +270,9 @@ contract WormholeGuardiansAdapterTest is Test {
"Peer for chain two is wrong"
);

// If you get a peer for a chain that's not set, it returns zero.
require(
srcAdapter.getPeer(peerChain3) == UniversalAddressLibrary.fromAddress(address(0)).toBytes32(),
"Peer for chain three should not be set"
);
// If you get a peer for a chain that's not set, it reverts.
vm.expectRevert(abi.encodeWithSelector(IWormholeGuardiansAdapter.UnregisteredPeer.selector, peerChain3));
srcAdapter.getPeer(peerChain3);
}

function test_getAdapterType() public view {
Expand Down Expand Up @@ -325,6 +327,7 @@ contract WormholeGuardiansAdapterTest is Test {
vm.startPrank(admin);
srcAdapter.setPeer(destChain, UniversalAddressLibrary.fromAddress(address(destAdapter)).toBytes32());
destAdapter.setPeer(srcChain, UniversalAddressLibrary.fromAddress(address(srcAdapter)).toBytes32());
otherDestAdapter.setPeer(srcChain, bytes32(uint256(1)));

UniversalAddress srcAddr = UniversalAddressLibrary.fromAddress(address(userA));
uint16 dstChain = destChain;
Expand All @@ -350,11 +353,15 @@ contract WormholeGuardiansAdapterTest is Test {
require(destEndpoint.lastDestinationAddress() == dstAddr, "dstAddr is wrong");
require(destEndpoint.lastPayloadHash() == payloadHash, "payloadHash is wrong");

// Can't post it from a chain that isn't registered
vm.expectRevert(abi.encodeWithSelector(IWormholeGuardiansAdapter.UnregisteredPeer.selector, srcChain));
srcAdapter.receiveMessage(vaa);
// Can't post it to the wrong adapter.
vm.startPrank(endpointAddr);
vm.expectRevert(
abi.encodeWithSelector(IWormholeGuardiansAdapter.InvalidPeer.selector, srcChain, address(srcAdapter))
);
srcAdapter.receiveMessage(vaa);
otherDestAdapter.receiveMessage(vaa);

// An invalid VAA should revert.
destWormhole.setValidFlag(false, "This is bad!");
Expand Down
3 changes: 3 additions & 0 deletions evm/test/WormholeGuardiansAdapterWithExecutor.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ contract WormholeGuardiansAdapterWithExecutorTest is Test {

bytes memory instructions = srcAdapter.encodeInstructions(execMsgValue, signedQuote, relayInstructions);

vm.startPrank(admin);
srcAdapter.setPeer(dstChain, dstAddr.toBytes32());

// Only the endpoint can call send message.
vm.startPrank(someoneElse);
vm.expectRevert(abi.encodeWithSelector(IAdapter.CallerNotEndpoint.selector, someoneElse));
Expand Down
Loading