diff --git a/scripts/Adapters/DeployWormholeExecutorAdapter.sol b/scripts/Adapters/DeployWormholeExecutorAdapter.sol new file mode 100644 index 00000000..e6523dbc --- /dev/null +++ b/scripts/Adapters/DeployWormholeExecutorAdapter.sol @@ -0,0 +1,65 @@ +// SPDX-License-Identifier: BUSL-1.1 +pragma solidity ^0.8.0; + +import {WormholeExecutorAdapter, IWormholeExecutorAdapter, IBaseAdapter} from '../../src/contracts/adapters/wormhole/WormholeExecutorAdapter.sol'; +import './BaseAdapterScript.sol'; + +library WormholeExecutorAdapterDeploymentHelper { + struct WormholeExecutorAdapterArgs { + BaseAdapterArgs baseArgs; + address wormholeCore; + address executorQuoterRouter; + address quoter; + address refundAddress; + } + + function getAdapterCode( + WormholeExecutorAdapterArgs memory wormholeArgs + ) internal pure returns (bytes memory) { + bytes memory creationCode = type(WormholeExecutorAdapter).creationCode; + + return + abi.encodePacked( + creationCode, + abi.encode( + wormholeArgs.baseArgs.crossChainController, + wormholeArgs.wormholeCore, + wormholeArgs.executorQuoterRouter, + wormholeArgs.quoter, + wormholeArgs.refundAddress, + wormholeArgs.baseArgs.providerGasLimit, + wormholeArgs.baseArgs.trustedRemotes + ) + ); + } +} + +abstract contract BaseWormholeExecutorAdapter is BaseAdapterScript { + function WORMHOLE_CORE() internal view virtual returns (address); + + function EXECUTOR_QUOTER_ROUTER() internal view virtual returns (address); + + function QUOTER() internal view virtual returns (address); + + /// @dev refund address is only relevant on the sending side; receive-only deployments may return 0 + function REFUND_ADDRESS() internal view virtual returns (address); + + function _getAdapterByteCode( + BaseAdapterArgs memory baseArgs + ) internal view override returns (bytes memory) { + require(WORMHOLE_CORE() != address(0), 'Wormhole core can not be 0'); + require(EXECUTOR_QUOTER_ROUTER() != address(0), 'Wormhole quoter router can not be 0'); + require(QUOTER() != address(0), 'Wormhole quoter can not be 0'); + + return + WormholeExecutorAdapterDeploymentHelper.getAdapterCode( + WormholeExecutorAdapterDeploymentHelper.WormholeExecutorAdapterArgs({ + baseArgs: baseArgs, + wormholeCore: WORMHOLE_CORE(), + executorQuoterRouter: EXECUTOR_QUOTER_ROUTER(), + quoter: QUOTER(), + refundAddress: REFUND_ADDRESS() + }) + ); + } +} diff --git a/security/Certora - Aave Wormhole a.DI Adapter - Final Report.pdf b/security/Certora - Aave Wormhole a.DI Adapter - Final Report.pdf new file mode 100644 index 00000000..459e5d90 Binary files /dev/null and b/security/Certora - Aave Wormhole a.DI Adapter - Final Report.pdf differ diff --git a/src/contracts/adapters/BaseAdapter.sol b/src/contracts/adapters/BaseAdapter.sol index b06ea356..7ca8365a 100644 --- a/src/contracts/adapters/BaseAdapter.sol +++ b/src/contracts/adapters/BaseAdapter.sol @@ -73,7 +73,7 @@ abstract contract BaseAdapter is IBaseAdapter { * @param _payload bytes containing the bridged message * @param originChainId id of the chain where the message originated */ - function _registerReceivedMessage(bytes calldata _payload, uint256 originChainId) internal { + function _registerReceivedMessage(bytes memory _payload, uint256 originChainId) internal { // this method should be always called via call require(address(this) == _selfAddress, Errors.DELEGATE_CALL_FORBIDDEN); CROSS_CHAIN_CONTROLLER.receiveCrossChainMessage(_payload, originChainId); diff --git a/src/contracts/adapters/wormhole/IWormholeExecutorAdapter.sol b/src/contracts/adapters/wormhole/IWormholeExecutorAdapter.sol new file mode 100644 index 00000000..5e230934 --- /dev/null +++ b/src/contracts/adapters/wormhole/IWormholeExecutorAdapter.sol @@ -0,0 +1,46 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +/** + * @title IWormholeExecutorAdapter + * @author Aave Labs + * @notice interface containing the methods definitions used in the Wormhole Executor bridge adapter + */ +interface IWormholeExecutorAdapter { + /** + * @notice method to get the Wormhole Core bridge address + * @return address of the Wormhole Core bridge + */ + function WORMHOLE_CORE() external view returns (address); + + /** + * @notice method to get the Wormhole Executor quoter router address + * @return address of the Wormhole Executor quoter router + */ + function EXECUTOR_QUOTER_ROUTER() external view returns (address); + + /** + * @notice method to get the on-chain quoter address of the chosen relay provider + * @return address of the relay provider quoter + */ + function QUOTER() external view returns (address); + + /** + * @notice method to get the refund address on destination chain + * @return address that will receive the refunds + * @dev should be CrossChainController on destination chain + */ + function REFUND_ADDRESS() external view returns (address); + + /** + * @notice method to get the Wormhole chain id of the chain where this adapter is deployed + * @return the Wormhole chain id used as the emitter chain of published messages + */ + function WORMHOLE_SOURCE_CHAIN_ID() external view returns (uint16); + + /** + * @notice method to get the Wormhole consistency (finality) level used when publishing messages + * @return the consistency level (chain specific, eg. 202 = finalized on Ethereum, 0 on Monad) + */ + function CONSISTENCY_LEVEL() external view returns (uint8); +} diff --git a/src/contracts/adapters/wormhole/WormholeExecutorAdapter.sol b/src/contracts/adapters/wormhole/WormholeExecutorAdapter.sol new file mode 100644 index 00000000..2e190cbd --- /dev/null +++ b/src/contracts/adapters/wormhole/WormholeExecutorAdapter.sol @@ -0,0 +1,256 @@ +// SPDX-License-Identifier: BUSL-1.1 +pragma solidity ^0.8.0; + +import {ChainIds} from 'solidity-utils/contracts/utils/ChainHelpers.sol'; +import {SafeCast} from 'openzeppelin-contracts/contracts/utils/math/SafeCast.sol'; +import {ICoreBridge, CoreBridgeVM} from './interfaces/ICoreBridge.sol'; +import {IExecutorQuoterRouter} from './interfaces/IExecutorQuoterRouter.sol'; +import {IVaaV1Receiver} from './interfaces/IVaaV1Receiver.sol'; +import {IWormholeExecutorAdapter} from './IWormholeExecutorAdapter.sol'; +import {BaseAdapter, IBaseAdapter} from '../BaseAdapter.sol'; + +import {Errors} from '../../libs/Errors.sol'; +import {WormholeExecutorEncoding} from './libs/WormholeExecutorEncoding.sol'; + + +/** + * @title WormholeExecutorAdapter + * @author Aave Labs + * @notice Wormhole bridge adapter built on the Wormhole Executor framework. Used to send and receive + * messages cross chain on networks that do not have the legacy Wormhole standard relayer + * (eg. Monad). + * @dev to bridge a message it publishes it to the Wormhole Core bridge and requests its relaying via the + * Executor quoter router using an on-chain quote. It uses the eth balance of the CrossChainController + * contract to pay for message bridging as `forwardMessage` is called via delegate call. + * @dev delivery on the destination chain is permissionless (anyone may call `executeVAAv1`), so the + * received VAA is verified against the Wormhole Core bridge and its emitter checked against the + * configured trusted remotes. + */ +contract WormholeExecutorAdapter is BaseAdapter, IWormholeExecutorAdapter, IVaaV1Receiver { + using WormholeExecutorEncoding for *; + + /// @inheritdoc IWormholeExecutorAdapter + address public immutable WORMHOLE_CORE; + + /// @inheritdoc IWormholeExecutorAdapter + address public immutable EXECUTOR_QUOTER_ROUTER; + + /// @inheritdoc IWormholeExecutorAdapter + address public immutable QUOTER; + + /// @inheritdoc IWormholeExecutorAdapter + address public immutable REFUND_ADDRESS; + + /// @inheritdoc IWormholeExecutorAdapter + uint16 public immutable WORMHOLE_SOURCE_CHAIN_ID; + + /// @inheritdoc IWormholeExecutorAdapter + uint8 public constant CONSISTENCY_LEVEL = 202; + + /** + * @param crossChainController address of the cross chain controller that will use this bridge adapter + * @param wormholeCore Wormhole Core bridge entry point address + * @param executorQuoterRouter Wormhole Executor on-chain quoter router address + * @param quoter on-chain quoter address of the chosen relay provider + * @param refundAddress address that will receive left over gas on destination + * @param providerGasLimit base gas limit used by the bridge adapter + * @param trustedRemotes list of remote configurations to set as trusted + */ + constructor( + address crossChainController, + address wormholeCore, + address executorQuoterRouter, + address quoter, + address refundAddress, + uint256 providerGasLimit, + TrustedRemotesConfig[] memory trustedRemotes + ) + BaseAdapter(crossChainController, providerGasLimit, 'Wormhole Executor adapter', trustedRemotes) + { + require(wormholeCore != address(0), Errors.WORMHOLE_CORE_CANT_BE_ADDRESS_0); + require(executorQuoterRouter != address(0), Errors.WORMHOLE_QUOTER_ROUTER_CANT_BE_ADDRESS_0); + require(quoter != address(0), Errors.WORMHOLE_QUOTER_CANT_BE_ADDRESS_0); + WORMHOLE_CORE = wormholeCore; + EXECUTOR_QUOTER_ROUTER = executorQuoterRouter; + QUOTER = quoter; + REFUND_ADDRESS = refundAddress; + WORMHOLE_SOURCE_CHAIN_ID = ICoreBridge(wormholeCore).chainId(); + } + + /// @inheritdoc IBaseAdapter + function forwardMessage( + address receiver, + uint256 executionGasLimit, + uint256 destinationChainId, + bytes calldata message + ) external returns (address, uint256) { + uint16 nativeChainId = SafeCast.toUint16(infraToNativeChainId(destinationChainId)); + require(nativeChainId != uint16(0), Errors.DESTINATION_CHAIN_ID_NOT_SUPPORTED); + require(receiver != address(0), Errors.RECEIVER_NOT_SET); + + bytes memory relayInstructions = WormholeExecutorEncoding.encodeGas( + SafeCast.toUint128(executionGasLimit + BASE_GAS_LIMIT), + 0 + ); + + uint64 sequence = _publishAndRequest( + nativeChainId, + bytes32(uint256(uint160(receiver))), + relayInstructions, + message + ); + + return (EXECUTOR_QUOTER_ROUTER, uint256(sequence)); + } + + /** + * @notice Publishes the message to the Wormhole Core bridge and requests its relaying via the Executor + * @return sequence the Wormhole sequence number of the published message + */ + function _publishAndRequest( + uint16 nativeChainId, + bytes32 dstAddr, + bytes memory relayInstructions, + bytes calldata message + ) private returns (uint64 sequence) { + bytes32 emitter = bytes32(uint256(uint160(address(this)))); + + // quote using a placeholder sequence: the executor fee does not depend on the sequence number + uint256 executorFee = IExecutorQuoterRouter(EXECUTOR_QUOTER_ROUTER).quoteExecution( + nativeChainId, + dstAddr, + REFUND_ADDRESS, + QUOTER, + WormholeExecutorEncoding.encodeVaaMultiSigRequest(WORMHOLE_SOURCE_CHAIN_ID, emitter, 0), + relayInstructions + ); + + uint256 messageFee = ICoreBridge(WORMHOLE_CORE).messageFee(); + require( + executorFee + messageFee <= address(this).balance, + Errors.NOT_ENOUGH_VALUE_TO_PAY_BRIDGE_FEES + ); + + // the message is the a.DI Transaction envelope, which already carries the destination chain id and is + // validated by the destination CrossChainController, so it is published as-is + sequence = ICoreBridge(WORMHOLE_CORE).publishMessage{value: messageFee}( + 0, // nonce, unused (uniqueness comes from the auto-incremented sequence) + message, + CONSISTENCY_LEVEL + ); + + IExecutorQuoterRouter(EXECUTOR_QUOTER_ROUTER).requestExecution{value: executorFee}( + nativeChainId, + dstAddr, + REFUND_ADDRESS, + QUOTER, + WormholeExecutorEncoding.encodeVaaMultiSigRequest(WORMHOLE_SOURCE_CHAIN_ID, emitter, sequence), + relayInstructions + ); + } + + /// @inheritdoc IVaaV1Receiver + function executeVAAv1(bytes memory multiSigVaa) external payable { + (CoreBridgeVM memory vm, bool valid, string memory reason) = ICoreBridge(WORMHOLE_CORE) + .parseAndVerifyVM(multiSigVaa); + require(valid, bytes(reason).length == 0 ? Errors.INVALID_VAA : reason); + + address srcAddress = address(uint160(uint256(vm.emitterAddress))); + uint256 originChainId = nativeToInfraChainId(vm.emitterChainId); + require( + _trustedRemotes[originChainId] == srcAddress && srcAddress != address(0), + Errors.REMOTE_NOT_TRUSTED + ); + + // delivery via `executeVAAv1` is permissionless, but the CrossChainController deduplicates messages + // per adapter and validates the envelope's destination chain id, so no extra replay/destination guard + // is needed here + _registerReceivedMessage(vm.payload, originChainId); + } + + /// @inheritdoc IBaseAdapter + function nativeToInfraChainId( + uint256 nativeChainId + ) public pure virtual override returns (uint256) { + if (nativeChainId == 2) { + return ChainIds.ETHEREUM; + } else if (nativeChainId == 4) { + return ChainIds.BNB; + } else if (nativeChainId == 5) { + return ChainIds.POLYGON; + } else if (nativeChainId == 6) { + return ChainIds.AVALANCHE; + } else if (nativeChainId == 14) { + return ChainIds.CELO; + } else if (nativeChainId == 23) { + return ChainIds.ARBITRUM; + } else if (nativeChainId == 24) { + return ChainIds.OPTIMISM; + } else if (nativeChainId == 30) { + return ChainIds.BASE; + } else if (nativeChainId == 34) { + return ChainIds.SCROLL; + } else if (nativeChainId == 35) { + return ChainIds.MANTLE; + } else if (nativeChainId == 37) { + return ChainIds.XLAYER; + } else if (nativeChainId == 38) { + return ChainIds.LINEA; + } else if (nativeChainId == 46) { + return ChainIds.INK; + } else if (nativeChainId == 48) { + return ChainIds.MONAD; + } else if (nativeChainId == 52) { + return ChainIds.SONIC; + } else if (nativeChainId == 58) { + return ChainIds.PLASMA; + } else if (nativeChainId == 64) { + return ChainIds.MEGAETH; + } else { + return 0; + } + } + + /// @inheritdoc IBaseAdapter + function infraToNativeChainId( + uint256 infraChainId + ) public pure virtual override returns (uint256) { + if (infraChainId == ChainIds.ETHEREUM) { + return 2; + } else if (infraChainId == ChainIds.BNB) { + return 4; + } else if (infraChainId == ChainIds.POLYGON) { + return 5; + } else if (infraChainId == ChainIds.AVALANCHE) { + return 6; + } else if (infraChainId == ChainIds.CELO) { + return 14; + } else if (infraChainId == ChainIds.ARBITRUM) { + return 23; + } else if (infraChainId == ChainIds.OPTIMISM) { + return 24; + } else if (infraChainId == ChainIds.BASE) { + return 30; + } else if (infraChainId == ChainIds.SCROLL) { + return 34; + } else if (infraChainId == ChainIds.MANTLE) { + return 35; + } else if (infraChainId == ChainIds.XLAYER) { + return 37; + } else if (infraChainId == ChainIds.LINEA) { + return 38; + } else if (infraChainId == ChainIds.INK) { + return 46; + } else if (infraChainId == ChainIds.MONAD) { + return 48; + } else if (infraChainId == ChainIds.SONIC) { + return 52; + } else if (infraChainId == ChainIds.PLASMA) { + return 58; + } else if (infraChainId == ChainIds.MEGAETH) { + return 64; + } else { + return 0; + } + } +} diff --git a/src/contracts/adapters/wormhole/interfaces/ICoreBridge.sol b/src/contracts/adapters/wormhole/interfaces/ICoreBridge.sol new file mode 100644 index 00000000..4e0fc9cd --- /dev/null +++ b/src/contracts/adapters/wormhole/interfaces/ICoreBridge.sol @@ -0,0 +1,73 @@ +// SPDX-License-Identifier: Apache 2 +// Slimmed-down interface of the Wormhole Core bridge. +// Adapted from: https://github.com/wormhole-foundation/wormhole-solidity-sdk/blob/main/src/interfaces/ICoreBridge.sol +pragma solidity ^0.8.0; + +struct GuardianSignature { + bytes32 r; + bytes32 s; + uint8 v; + uint8 guardianIndex; +} + +/** + * @notice VM = Verified Message, the legacy struct returned by the Core bridge on `parseAndVerifyVM`. + * @dev finalized VAAs should use the unique (emitterChainId, emitterAddress, sequence) triple for cheap + * replay protection. + */ +struct CoreBridgeVM { + uint8 version; + uint32 timestamp; + uint32 nonce; + uint16 emitterChainId; + bytes32 emitterAddress; + uint64 sequence; + uint8 consistencyLevel; + bytes payload; + uint32 guardianSetIndex; + GuardianSignature[] signatures; + bytes32 hash; +} + +/** + * @title ICoreBridge + * @notice interface of the Wormhole Core bridge (aka the Wormhole contract) used by the Wormhole Executor adapter + */ +interface ICoreBridge { + /** + * @notice method to get the fee that must be paid as `msg.value` when publishing a message + * @return the message fee in units of the native currency + */ + function messageFee() external view returns (uint256); + + /** + * @notice publishes a message to be attested by the Wormhole guardians + * @dev requires `msg.value` equal to `messageFee()` + * @param nonce arbitrary nonce (unused here) + * @param payload arbitrary message bytes + * @param consistencyLevel finality level at which the guardians should attest the message + * @return sequence sequence number of the published message for the calling emitter + */ + function publishMessage( + uint32 nonce, + bytes memory payload, + uint8 consistencyLevel + ) external payable returns (uint64 sequence); + + /** + * @notice parses and verifies the guardian signatures of an encoded VAA + * @param encodedVM the encoded VAA bytes + * @return vm the parsed VAA + * @return valid whether the VAA signatures are valid + * @return reason failure reason when `valid` is false + */ + function parseAndVerifyVM( + bytes calldata encodedVM + ) external view returns (CoreBridgeVM memory vm, bool valid, string memory reason); + + /** + * @notice method to get the Wormhole chain id of the chain where this Core bridge is deployed + * @return the Wormhole chain id + */ + function chainId() external view returns (uint16); +} diff --git a/src/contracts/adapters/wormhole/interfaces/IExecutorQuoterRouter.sol b/src/contracts/adapters/wormhole/interfaces/IExecutorQuoterRouter.sol new file mode 100644 index 00000000..de9919da --- /dev/null +++ b/src/contracts/adapters/wormhole/interfaces/IExecutorQuoterRouter.sol @@ -0,0 +1,49 @@ +// SPDX-License-Identifier: Apache 2 +// Adapted from: https://github.com/wormhole-foundation/wormhole-solidity-sdk/blob/main/src/interfaces/IExecutor.sol +pragma solidity ^0.8.0; + +/** + * @title IExecutorQuoterRouter + * @notice interface of the Wormhole Executor on-chain quoter router. It is the canonical entry point used + * to request an on-chain delivery quote and to request execution (relaying) of a published message. + * @dev see https://github.com/wormholelabs-xyz/example-messaging-executor + */ +interface IExecutorQuoterRouter { + /** + * @notice returns the native fee required to relay a request to `dstChain` using `quoterAddr` + * @param dstChain destination chain in Wormhole chain id format + * @param dstAddr destination contract (universal address format) that will receive the message + * @param refundAddr address that will receive any leftover gas refunds + * @param quoterAddr the on-chain quoter contract of the chosen relay provider + * @param requestBytes encoded execution request (eg. a VAA v1 request) + * @param relayInstructions encoded relay instructions (eg. gas limit) + * @return the required executor fee in units of the native currency + */ + function quoteExecution( + uint16 dstChain, + bytes32 dstAddr, + address refundAddr, + address quoterAddr, + bytes calldata requestBytes, + bytes calldata relayInstructions + ) external view returns (uint256); + + /** + * @notice requests execution (relaying) of an already published message, paying the executor fee + * @dev must be called with `msg.value` equal to the value returned by `quoteExecution` + * @param dstChain destination chain in Wormhole chain id format + * @param dstAddr destination contract (universal address format) that will receive the message + * @param refundAddr address that will receive any leftover gas refunds + * @param quoterAddr the on-chain quoter contract of the chosen relay provider + * @param requestBytes encoded execution request (eg. a VAA v1 request) + * @param relayInstructions encoded relay instructions (eg. gas limit) + */ + function requestExecution( + uint16 dstChain, + bytes32 dstAddr, + address refundAddr, + address quoterAddr, + bytes calldata requestBytes, + bytes calldata relayInstructions + ) external payable; +} diff --git a/src/contracts/adapters/wormhole/interfaces/IVaaV1Receiver.sol b/src/contracts/adapters/wormhole/interfaces/IVaaV1Receiver.sol new file mode 100644 index 00000000..6a662fcb --- /dev/null +++ b/src/contracts/adapters/wormhole/interfaces/IVaaV1Receiver.sol @@ -0,0 +1,18 @@ +// SPDX-License-Identifier: Apache 2 +// Adapted from: https://github.com/wormhole-foundation/wormhole-solidity-sdk/blob/main/src/interfaces/IExecutor.sol +pragma solidity ^0.8.0; + +/** + * @title IVaaV1Receiver + * @notice required interface for receiving MultiSig (=V1) VAAs from the Wormhole Executor relay provider. + * @dev the relay provider delivers a message by calling `executeVAAv1` on the destination contract. This + * call is permissionless, so the receiving contract MUST verify the VAA via the Core bridge and that + * its emitter is a known trusted remote. + */ +interface IVaaV1Receiver { + /** + * @notice entry point invoked by the Executor relay provider to deliver an attested message + * @param multiSigVaa the encoded, guardian-signed VAA + */ + function executeVAAv1(bytes memory multiSigVaa) external payable; +} diff --git a/src/contracts/adapters/wormhole/libs/WormholeExecutorEncoding.sol b/src/contracts/adapters/wormhole/libs/WormholeExecutorEncoding.sol new file mode 100644 index 00000000..4b289dc2 --- /dev/null +++ b/src/contracts/adapters/wormhole/libs/WormholeExecutorEncoding.sol @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: BUSL-1.1 +pragma solidity ^0.8.0; + +/** + * @title WormholeExecutorEncoding + * @notice Minimal encoders for the Wormhole Executor relay request and relay instructions. + * @dev Mirrors `RelayInstructionLib`/`RequestLib` from the wormhole-solidity-sdk so the off-chain relay + * providers can parse the request. Kept local to avoid pulling the whole SDK as a dependency. + */ +library WormholeExecutorEncoding { + // recv instruction type for a plain gas limit (+ optional msg value) on the destination chain + uint8 internal constant RECV_INST_TYPE_GAS = 1; + // request type for a MultiSig (=V1) VAA + bytes4 internal constant REQ_VAA_V1 = 'ERV1'; + + function encodeGas(uint128 gasLimit, uint128 msgValue) internal pure returns (bytes memory) { + return abi.encodePacked(RECV_INST_TYPE_GAS, gasLimit, msgValue); + } + + function encodeVaaMultiSigRequest( + uint16 emitterChain, + bytes32 emitterAddress, + uint64 sequence + ) internal pure returns (bytes memory) { + return abi.encodePacked(REQ_VAA_V1, emitterChain, emitterAddress, sequence); + } +} diff --git a/src/contracts/libs/Errors.sol b/src/contracts/libs/Errors.sol index a0ec2adb..efddb1b0 100644 --- a/src/contracts/libs/Errors.sol +++ b/src/contracts/libs/Errors.sol @@ -53,4 +53,8 @@ library Errors { string public constant CL_GAS_PRICE_ORACLE_CANT_BE_ADDRESS_0 = '44'; // ChainLink gas price oracle can not be address 0 string public constant CALLER_NOT_LINEA_MESSAGE_SERVICE = '45'; // caller must be the Linea message service string public constant LINEA_MESSAGE_SERVICE_CANT_BE_ADDRESS_0 = '46'; // Linea message service can not be address 0 + string public constant WORMHOLE_CORE_CANT_BE_ADDRESS_0 = '47'; // Wormhole core bridge can not be address 0 + string public constant WORMHOLE_QUOTER_ROUTER_CANT_BE_ADDRESS_0 = '48'; // Wormhole executor quoter router can not be address 0 + string public constant WORMHOLE_QUOTER_CANT_BE_ADDRESS_0 = '49'; // Wormhole executor quoter can not be address 0 + string public constant INVALID_VAA = '50'; // the received VAA could not be verified by the Wormhole core bridge } diff --git a/tests/adapters/WormholeExecutorAdapter.t.sol b/tests/adapters/WormholeExecutorAdapter.t.sol new file mode 100644 index 00000000..1683c7a2 --- /dev/null +++ b/tests/adapters/WormholeExecutorAdapter.t.sol @@ -0,0 +1,325 @@ +// SPDX-License-Identifier: BUSL-1.1 +pragma solidity ^0.8.0; + +import {WormholeExecutorAdapter} from '../../src/contracts/adapters/wormhole/WormholeExecutorAdapter.sol'; +import {IWormholeExecutorAdapter} from '../../src/contracts/adapters/wormhole/IWormholeExecutorAdapter.sol'; +import {ICoreBridge, CoreBridgeVM, GuardianSignature} from '../../src/contracts/adapters/wormhole/interfaces/ICoreBridge.sol'; +import {IExecutorQuoterRouter} from '../../src/contracts/adapters/wormhole/interfaces/IExecutorQuoterRouter.sol'; +import {IBaseAdapter} from '../../src/contracts/adapters/IBaseAdapter.sol'; +import {ICrossChainReceiver} from '../../src/contracts/interfaces/ICrossChainReceiver.sol'; +import {ChainIds} from 'solidity-utils/contracts/utils/ChainHelpers.sol'; +import {Errors} from '../../src/contracts/libs/Errors.sol'; +import {BaseAdapterTest} from './BaseAdapterTest.sol'; + +contract WormholeExecutorAdapterTest is BaseAdapterTest { + // wormhole chain ids + uint16 internal constant WH_ETHEREUM = 2; + uint16 internal constant WH_MONAD = 48; + + WormholeExecutorAdapter internal adapter; + + address internal crossChainController = makeAddr('crossChainController'); + address internal wormholeCore = makeAddr('wormholeCore'); + address internal quoterRouter = makeAddr('quoterRouter'); + address internal quoter = makeAddr('quoter'); + address internal refundAddress = makeAddr('refundAddress'); + address internal originForwarder = makeAddr('originForwarder'); + uint256 internal constant BASE_GAS_LIMIT = 100_000; + uint8 internal constant CONSISTENCY_LEVEL = 202; + + function setUp() public { + // the source chain id is read from the Core bridge in the constructor + vm.mockCall( + wormholeCore, + abi.encodeWithSelector(ICoreBridge.chainId.selector), + abi.encode(WH_ETHEREUM) + ); + adapter = _deployAdapter(crossChainController); + } + + function _deployAdapter(address ccc) internal returns (WormholeExecutorAdapter) { + IBaseAdapter.TrustedRemotesConfig[] + memory originConfigs = new IBaseAdapter.TrustedRemotesConfig[](1); + originConfigs[0] = IBaseAdapter.TrustedRemotesConfig({ + originForwarder: originForwarder, + originChainId: ChainIds.ETHEREUM + }); + return + new WormholeExecutorAdapter( + ccc, + wormholeCore, + quoterRouter, + quoter, + refundAddress, + BASE_GAS_LIMIT, + originConfigs + ); + } + + // ------------------------------------------------------------------------- + // constructor + // ------------------------------------------------------------------------- + + function test_initialize() public view { + assertEq(adapter.WORMHOLE_CORE(), wormholeCore); + assertEq(adapter.EXECUTOR_QUOTER_ROUTER(), quoterRouter); + assertEq(adapter.QUOTER(), quoter); + assertEq(adapter.REFUND_ADDRESS(), refundAddress); + assertEq(adapter.WORMHOLE_SOURCE_CHAIN_ID(), WH_ETHEREUM); + assertEq(adapter.CONSISTENCY_LEVEL(), CONSISTENCY_LEVEL); + assertEq(adapter.getTrustedRemoteByChainId(ChainIds.ETHEREUM), originForwarder); + } + + function test_constructorRevertsWhenCoreIsZero() public { + IBaseAdapter.TrustedRemotesConfig[] + memory originConfigs = new IBaseAdapter.TrustedRemotesConfig[](0); + vm.expectRevert(bytes(Errors.WORMHOLE_CORE_CANT_BE_ADDRESS_0)); + new WormholeExecutorAdapter( + crossChainController, + address(0), + quoterRouter, + quoter, + refundAddress, + BASE_GAS_LIMIT, + originConfigs + ); + } + + function test_constructorRevertsWhenQuoterRouterIsZero() public { + IBaseAdapter.TrustedRemotesConfig[] + memory originConfigs = new IBaseAdapter.TrustedRemotesConfig[](0); + vm.expectRevert(bytes(Errors.WORMHOLE_QUOTER_ROUTER_CANT_BE_ADDRESS_0)); + new WormholeExecutorAdapter( + crossChainController, + wormholeCore, + address(0), + quoter, + refundAddress, + BASE_GAS_LIMIT, + originConfigs + ); + } + + function test_constructorRevertsWhenQuoterIsZero() public { + IBaseAdapter.TrustedRemotesConfig[] + memory originConfigs = new IBaseAdapter.TrustedRemotesConfig[](0); + vm.expectRevert(bytes(Errors.WORMHOLE_QUOTER_CANT_BE_ADDRESS_0)); + new WormholeExecutorAdapter( + crossChainController, + wormholeCore, + quoterRouter, + address(0), + refundAddress, + BASE_GAS_LIMIT, + originConfigs + ); + } + + // ------------------------------------------------------------------------- + // chain id maps + // ------------------------------------------------------------------------- + + function test_nativeToInfraChainId() public view { + assertEq(adapter.nativeToInfraChainId(WH_ETHEREUM), ChainIds.ETHEREUM); + assertEq(adapter.nativeToInfraChainId(WH_MONAD), ChainIds.MONAD); + assertEq(adapter.nativeToInfraChainId(4), ChainIds.BNB); + assertEq(adapter.nativeToInfraChainId(30), ChainIds.BASE); + assertEq(adapter.nativeToInfraChainId(34), ChainIds.SCROLL); + assertEq(adapter.nativeToInfraChainId(37), ChainIds.XLAYER); + assertEq(adapter.nativeToInfraChainId(46), ChainIds.INK); + assertEq(adapter.nativeToInfraChainId(64), ChainIds.MEGAETH); + assertEq(adapter.nativeToInfraChainId(99), 0); + } + + function test_infraToNativeChainId() public view { + assertEq(adapter.infraToNativeChainId(ChainIds.ETHEREUM), WH_ETHEREUM); + assertEq(adapter.infraToNativeChainId(ChainIds.MONAD), WH_MONAD); + assertEq(adapter.infraToNativeChainId(ChainIds.BNB), 4); + assertEq(adapter.infraToNativeChainId(ChainIds.BASE), 30); + assertEq(adapter.infraToNativeChainId(ChainIds.MANTLE), 35); + assertEq(adapter.infraToNativeChainId(ChainIds.LINEA), 38); + assertEq(adapter.infraToNativeChainId(ChainIds.SONIC), 52); + assertEq(adapter.infraToNativeChainId(ChainIds.PLASMA), 58); + // chains without a Wormhole core are intentionally unmapped + assertEq(adapter.infraToNativeChainId(ChainIds.ZKSYNC), 0); + assertEq(adapter.infraToNativeChainId(ChainIds.GNOSIS), 0); + assertEq(adapter.infraToNativeChainId(99), 0); + } + + // ------------------------------------------------------------------------- + // forwardMessage + // ------------------------------------------------------------------------- + + function test_forwardMessage() public { + bytes memory message = abi.encode('test message'); + uint256 dstGasLimit = 300_000; + address receiver = makeAddr('receiver'); + uint256 executorFee = 100; + uint256 messageFee = 10; + uint64 sequence = 42; + + _mockSend(executorFee, messageFee, sequence); + + // the raw a.DI envelope is published as-is (no wrapping), at the configured consistency level + vm.expectCall( + wormholeCore, + messageFee, + abi.encodeWithSelector( + ICoreBridge.publishMessage.selector, + uint32(0), + message, + CONSISTENCY_LEVEL + ) + ); + + vm.deal(address(this), 1 ether); + (bool success, bytes memory returnData) = address(adapter).delegatecall( + abi.encodeWithSelector( + IBaseAdapter.forwardMessage.selector, + receiver, + dstGasLimit, + ChainIds.MONAD, + message + ) + ); + vm.clearMockedCalls(); + + assertEq(success, true); + assertEq(returnData, abi.encode(quoterRouter, uint256(sequence))); + } + + function test_forwardMessageRevertsWhenChainNotSupported() public { + // Gnosis has no Wormhole core, so it is intentionally unmapped (returns 0) + vm.expectRevert(bytes(Errors.DESTINATION_CHAIN_ID_NOT_SUPPORTED)); + adapter.forwardMessage(makeAddr('receiver'), 300_000, ChainIds.GNOSIS, abi.encode('x')); + } + + function test_forwardMessageRevertsWhenReceiverNotSet() public { + vm.expectRevert(bytes(Errors.RECEIVER_NOT_SET)); + adapter.forwardMessage(address(0), 300_000, ChainIds.MONAD, abi.encode('x')); + } + + function test_forwardMessageRevertsWhenNotEnoughBalance() public { + bytes memory message = abi.encode('test message'); + _mockSend(100, 10, 42); + + // address(this) under delegatecall holds no balance to cover the fees + vm.deal(address(this), 5); + (bool success, bytes memory returnData) = address(adapter).delegatecall( + abi.encodeWithSelector( + IBaseAdapter.forwardMessage.selector, + makeAddr('receiver'), + uint256(300_000), + ChainIds.MONAD, + message + ) + ); + vm.clearMockedCalls(); + + assertEq(success, false); + assertEq( + returnData, + abi.encodeWithSignature('Error(string)', Errors.NOT_ENOUGH_VALUE_TO_PAY_BRIDGE_FEES) + ); + } + + function _mockSend(uint256 executorFee, uint256 messageFee, uint64 sequence) internal { + vm.mockCall( + quoterRouter, + abi.encodeWithSelector(IExecutorQuoterRouter.quoteExecution.selector), + abi.encode(executorFee) + ); + vm.mockCall( + wormholeCore, + abi.encodeWithSelector(ICoreBridge.messageFee.selector), + abi.encode(messageFee) + ); + vm.mockCall( + wormholeCore, + messageFee, + abi.encodeWithSelector(ICoreBridge.publishMessage.selector), + abi.encode(sequence) + ); + vm.mockCall( + quoterRouter, + executorFee, + abi.encodeWithSelector(IExecutorQuoterRouter.requestExecution.selector), + abi.encode() + ); + } + + // ------------------------------------------------------------------------- + // executeVAAv1 (receive) + // ------------------------------------------------------------------------- + + function test_executeVAAv1() public { + bytes memory message = abi.encode('inbound message'); + bytes memory vaa = abi.encode('encoded-vaa'); + _mockParseAndVerify(vaa, WH_ETHEREUM, originForwarder, 7, message, true, ''); + + vm.mockCall( + crossChainController, + abi.encodeWithSelector(ICrossChainReceiver.receiveCrossChainMessage.selector), + abi.encode() + ); + // the raw VAA payload is registered as-is (no unwrapping) + vm.expectCall( + crossChainController, + 0, + abi.encodeWithSelector( + ICrossChainReceiver.receiveCrossChainMessage.selector, + message, + ChainIds.ETHEREUM + ) + ); + + adapter.executeVAAv1(vaa); + } + + function test_executeVAAv1RevertsWhenInvalidVAA() public { + bytes memory vaa = abi.encode('encoded-vaa'); + _mockParseAndVerify( + vaa, + WH_ETHEREUM, + originForwarder, + 7, + abi.encode('m'), + false, + 'bad signatures' + ); + vm.expectRevert(bytes('bad signatures')); + adapter.executeVAAv1(vaa); + } + + function test_executeVAAv1RevertsWhenRemoteNotTrusted() public { + bytes memory vaa = abi.encode('encoded-vaa'); + _mockParseAndVerify(vaa, WH_ETHEREUM, makeAddr('attacker'), 7, abi.encode('m'), true, ''); + vm.expectRevert(bytes(Errors.REMOTE_NOT_TRUSTED)); + adapter.executeVAAv1(vaa); + } + + function _mockParseAndVerify( + bytes memory vaa, + uint16 emitterChainId, + address emitter, + uint64 sequence, + bytes memory payload, + bool valid, + string memory reason + ) internal { + CoreBridgeVM memory parsed; + parsed.version = 1; + parsed.emitterChainId = emitterChainId; + parsed.emitterAddress = bytes32(uint256(uint160(emitter))); + parsed.sequence = sequence; + parsed.consistencyLevel = 1; + parsed.payload = payload; + parsed.signatures = new GuardianSignature[](0); + + vm.mockCall( + wormholeCore, + abi.encodeWithSelector(ICoreBridge.parseAndVerifyVM.selector, vaa), + abi.encode(parsed, valid, reason) + ); + } +}