Skip to content

Commit f37aebe

Browse files
committed
forge install: example-messaging-executor
1 parent 228e8b0 commit f37aebe

File tree

7 files changed

+788
-4
lines changed

7 files changed

+788
-4
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@
1010
[submodule "evm/lib/solidity-bytes-utils"]
1111
path = evm/lib/solidity-bytes-utils
1212
url = https://github.com/GNSPS/solidity-bytes-utils
13+
[submodule "evm/lib/example-messaging-executor"]
14+
path = evm/lib/example-messaging-executor
15+
url = https://github.com/wormholelabs-xyz/example-messaging-executor

evm/lib/example-messaging-executor

evm/src/NttManager/MsgManagerBase.sol

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,7 @@ abstract contract MsgManagerBase is ManagerBase, IMsgReceiver {
7777
// construct the NttManagerMessage payload
7878
encodedNttManagerPayload = TransceiverStructs.encodeNttManagerMessage(
7979
TransceiverStructs.NttManagerMessage(
80-
// TODO: Should we use `address(this)` instead of `msg.sender`?
81-
bytes32(uint256(sequence)),
82-
toWormholeFormat(msg.sender),
83-
payload
80+
bytes32(uint256(sequence)), toWormholeFormat(msg.sender), payload
8481
)
8582
);
8683

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
// SPDX-License-Identifier: Apache 2
2+
pragma solidity >=0.8.8 <0.9.0;
3+
4+
import "example-messaging-executor/evm/src/interfaces/IExecutor.sol";
5+
import "example-messaging-executor/evm/src/libraries/ExecutorMessages.sol";
6+
import "wormhole-solidity-sdk/Utils.sol";
7+
import "wormhole-solidity-sdk/libraries/BytesParsing.sol";
8+
9+
import "../interfaces/IMsgManagerWithExecutor.sol";
10+
import "../interfaces/ITransceiver.sol";
11+
import "../libraries/TransceiverHelpers.sol";
12+
13+
import {MsgManagerBase} from "./MsgManagerBase.sol";
14+
15+
contract MsgManagerWithExecutor is IMsgManagerWithExecutor, MsgManagerBase {
16+
string public constant MSG_MANAGER_VERSION = "1.0.0";
17+
18+
IExecutor public immutable executor;
19+
20+
// =============== Setup =================================================================
21+
22+
constructor(
23+
uint16 _chainId,
24+
address _executor
25+
) MsgManagerBase(address(0), Mode.LOCKING, _chainId) {
26+
assert(_executor != address(0));
27+
executor = IExecutor(_executor);
28+
}
29+
30+
function _initialize() internal virtual override {
31+
_init();
32+
_checkThresholdInvariants();
33+
_checkTransceiversInvariants();
34+
}
35+
36+
function _init() internal onlyInitializing {
37+
// check if the owner is the deployer of this contract
38+
if (msg.sender != deployer) {
39+
revert UnexpectedDeployer(deployer, msg.sender);
40+
}
41+
if (msg.value != 0) {
42+
revert UnexpectedMsgValue();
43+
}
44+
__PausedOwnable_init(msg.sender, msg.sender);
45+
__ReentrancyGuard_init();
46+
}
47+
48+
// =============== Storage ==============================================================
49+
50+
bytes32 private constant PEERS_SLOT = bytes32(uint256(keccak256("mmgr.peers")) - 1);
51+
52+
// =============== Storage Getters/Setters ==============================================
53+
54+
function _getPeersStorage()
55+
internal
56+
pure
57+
returns (mapping(uint16 => MsgManagerPeer) storage $)
58+
{
59+
uint256 slot = uint256(PEERS_SLOT);
60+
assembly ("memory-safe") {
61+
$.slot := slot
62+
}
63+
}
64+
65+
// =============== Public Getters ========================================================
66+
67+
/// @inheritdoc IMsgManagerWithExecutor
68+
function getPeer(
69+
uint16 chainId_
70+
) external view returns (MsgManagerPeer memory) {
71+
return _getPeersStorage()[chainId_];
72+
}
73+
74+
// =============== Admin ==============================================================
75+
76+
/// @inheritdoc IMsgManagerWithExecutor
77+
function setPeer(uint16 peerChainId, bytes32 peerAddress) public onlyOwner {
78+
if (peerChainId == 0) {
79+
revert InvalidPeerChainIdZero();
80+
}
81+
if (peerAddress == bytes32(0)) {
82+
revert InvalidPeerZeroAddress();
83+
}
84+
if (peerChainId == chainId) {
85+
revert InvalidPeerSameChainId();
86+
}
87+
88+
MsgManagerPeer memory oldPeer = _getPeersStorage()[peerChainId];
89+
90+
_getPeersStorage()[peerChainId].peerAddress = peerAddress;
91+
92+
emit PeerUpdated(peerChainId, oldPeer.peerAddress, peerAddress);
93+
}
94+
95+
/// ============== Invariants =============================================
96+
97+
/// @dev When we add new immutables, this function should be updated
98+
function _checkImmutables() internal view virtual override {
99+
super._checkImmutables();
100+
}
101+
102+
// ==================== External Interface ===============================================
103+
104+
/// @inheritdoc IMsgManagerWithExecutor
105+
function sendMessage(
106+
uint16 recipientChain,
107+
bytes calldata payload,
108+
bytes memory transceiverInstructions,
109+
ExecutorArgs calldata executorArgs
110+
) external payable nonReentrant whenNotPaused returns (uint64 sequence) {
111+
sequence = _useMessageSequence();
112+
113+
bytes32 recipientAddress = _getPeersStorage()[recipientChain].peerAddress;
114+
115+
(uint256 totalPriceQuote,) = _sendMessage(
116+
recipientChain, recipientAddress, sequence, payload, transceiverInstructions
117+
);
118+
119+
if (totalPriceQuote + executorArgs.value > msg.value) {
120+
revert InsufficientMsgValue(msg.value, totalPriceQuote, executorArgs.value);
121+
}
122+
123+
// emit MessageSent(recipientChain, recipientAddress, sequence, totalPriceQuote);
124+
125+
// Generate the executor event.
126+
// TODO: Not sure we want to use `makeNTTv1Request` since it doesn't have the payload.
127+
executor.requestExecution{value: executorArgs.value}(
128+
recipientChain,
129+
recipientAddress,
130+
executorArgs.refundAddress,
131+
executorArgs.signedQuote,
132+
ExecutorMessages.makeNTTv1Request(
133+
chainId, bytes32(uint256(uint160(address(this)))), bytes32(uint256(sequence))
134+
),
135+
executorArgs.instructions
136+
);
137+
}
138+
139+
/// @dev Override this function to handle your messages.
140+
function _handleMsg(
141+
uint16 sourceChainId,
142+
bytes32 sourceManagerAddress,
143+
TransceiverStructs.NttManagerMessage memory message,
144+
bytes32 digest
145+
) internal virtual override {}
146+
147+
// ==================== Internal Helpers ===============================================
148+
149+
/// @dev Verify that the peer address saved for `sourceChainId` matches the `peerAddress`.
150+
function _verifyPeer(uint16 sourceChainId, bytes32 peerAddress) internal view override {
151+
if (_getPeersStorage()[sourceChainId].peerAddress != peerAddress) {
152+
revert InvalidPeer(sourceChainId, peerAddress);
153+
}
154+
}
155+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// SPDX-License-Identifier: Apache 2
2+
pragma solidity >=0.8.8 <0.9.0;
3+
4+
import "../libraries/TrimmedAmount.sol";
5+
import "../libraries/TransceiverStructs.sol";
6+
7+
import "./IManagerBase.sol";
8+
import "./IMsgReceiver.sol";
9+
10+
interface IMsgManagerWithExecutor is IManagerBase, IMsgReceiver {
11+
/// @dev The peer on another chain.
12+
struct MsgManagerPeer {
13+
bytes32 peerAddress;
14+
}
15+
16+
/// @dev Instructions for the executor to perform relaying.
17+
struct ExecutorArgs {
18+
// The msg value to be passed into the Executor.
19+
uint256 value;
20+
// The refund address used by the Executor.
21+
address refundAddress;
22+
// The signed quote to be passed into the Executor.
23+
bytes signedQuote;
24+
// The relay instructions to be passed into the Executor.
25+
bytes instructions;
26+
}
27+
28+
/// @notice Emitted when the peer contract is updated.
29+
/// @dev Topic0
30+
/// TODO.
31+
/// @param chainId_ The chain ID of the peer contract.
32+
/// @param oldPeerContract The old peer contract address.
33+
/// @param peerContract The new peer contract address.
34+
event PeerUpdated(uint16 indexed chainId_, bytes32 oldPeerContract, bytes32 peerContract);
35+
36+
/// @notice Emitted when a message is sent from the manager.
37+
/// @dev Topic0
38+
/// 0xe54e51e42099622516fa3b48e9733581c9dbdcb771cafb093f745a0532a35982.
39+
/// @param recipientChain The chain ID of the recipient.
40+
/// @param recipientAddress The recipient of the message.
41+
/// @param sequence The unique sequence ID of the message.
42+
/// @param fee The amount of ether sent along with the tx to cover the delivery fee.
43+
event MessageSent(
44+
uint16 recipientChain, bytes32 indexed recipientAddress, uint64 sequence, uint256 fee
45+
);
46+
47+
/// @notice Error when trying to execute a message on an unintended target chain.
48+
/// @dev Selector 0x3dcb204a.
49+
/// @param targetChain The target chain.
50+
/// @param thisChain The current chain.
51+
error InvalidTargetChain(uint16 targetChain, uint16 thisChain);
52+
53+
/// @notice Peer for the chain does not match the configuration.
54+
/// @param chainId ChainId of the source chain.
55+
/// @param peerAddress Address of the peer nttManager contract.
56+
error InvalidPeer(uint16 chainId, bytes32 peerAddress);
57+
58+
/// @notice Peer chain ID cannot be zero.
59+
error InvalidPeerChainIdZero();
60+
61+
/// @notice Peer cannot be the zero address.
62+
error InvalidPeerZeroAddress();
63+
64+
/// @notice Peer cannot be on the same chain
65+
/// @dev Selector 0x20371f2a.
66+
error InvalidPeerSameChainId();
67+
68+
/// @notice The caller is not the deployer.
69+
error UnexpectedDeployer(address expectedOwner, address owner);
70+
71+
/// @notice An unexpected msg.value was passed with the call
72+
/// @dev Selector 0xbd28e889.
73+
error UnexpectedMsgValue();
74+
75+
/// @notice The message value is insufficient.
76+
/// @dev Selector TODO.
77+
error InsufficientMsgValue(uint256 value, uint256 totalPriceQuote, uint256 executorValue);
78+
79+
/// @notice Sends a message to the remote peer on the specified recipient chain.
80+
/// @dev This function enforces attestation threshold and replay logic for messages. Once all
81+
/// validations are complete, this function calls `executeMsg` to execute the command specified
82+
/// by the message.
83+
/// @param recipientChain The Wormhole chain id of the recipient.
84+
/// @param transceiverInstructions Instructions to be passed to the transceiver, if any.
85+
/// @param payload The message to be sent.
86+
function sendMessage(
87+
uint16 recipientChain,
88+
bytes calldata payload,
89+
bytes memory transceiverInstructions,
90+
ExecutorArgs calldata executorArgs
91+
) external payable returns (uint64);
92+
93+
/// @notice Returns registered peer contract for a given chain.
94+
/// @param chainId_ Wormhole chain ID.
95+
function getPeer(
96+
uint16 chainId_
97+
) external view returns (MsgManagerPeer memory);
98+
99+
/// @notice Sets the corresponding peer.
100+
/// @dev The msgManager that executes the message sets the source msgManager as the peer.
101+
/// @param peerChainId The Wormhole chain ID of the peer.
102+
/// @param peerContract The address of the peer nttManager contract.
103+
/// Set to zero if not needed.
104+
function setPeer(uint16 peerChainId, bytes32 peerContract) external;
105+
}

0 commit comments

Comments
 (0)