Skip to content

Commit 228e8b0

Browse files
committed
evm: Add MsgManager
1 parent 3d799d7 commit 228e8b0

File tree

7 files changed

+406
-100
lines changed

7 files changed

+406
-100
lines changed

evm/NOTES.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,34 @@ evm (main)$ forge build --sizes --via-ir --skip test
113113
|-----------------------------------------+------------------+-------------------+--------------------+---------------------|
114114

115115
```
116+
117+
#### Creating MsgManagerBase
118+
119+
#### Before
120+
121+
```bash
122+
evm (main)$ forge build --sizes --via-ir --skip test
123+
124+
╭-----------------------------------------+------------------+-------------------+--------------------+---------------------╮
125+
| Contract | Runtime Size (B) | Initcode Size (B) | Runtime Margin (B) | Initcode Margin (B) |
126+
+===========================================================================================================================+
127+
|-----------------------------------------+------------------+-------------------+--------------------+---------------------|
128+
| NttManager | 24,066 | 25,673 | 510 | 23,479 |
129+
|-----------------------------------------+------------------+-------------------+--------------------+---------------------|
130+
| NttManagerNoRateLimiting | 17,141 | 18,557 | 7,435 | 30,595 |
131+
|-----------------------------------------+------------------+-------------------+--------------------+---------------------|
132+
133+
```
134+
135+
#### After
136+
137+
```bash
138+
evm (main)$ forge build --sizes --via-ir --skip test
139+
140+
|-----------------------------------------+------------------+-------------------+--------------------+---------------------|
141+
| NttManager | 24,076 | 25,719 | 500 | 23,433 |
142+
|-----------------------------------------+------------------+-------------------+--------------------+---------------------|
143+
| NttManagerNoRateLimiting | 18,496 | 19,949 | 6,080 | 29,203 |
144+
|-----------------------------------------+------------------+-------------------+--------------------+---------------------|
145+
146+
```

evm/src/NttManager/MsgManager.sol

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// SPDX-License-Identifier: Apache 2
2+
pragma solidity >=0.8.8 <0.9.0;
3+
4+
import "wormhole-solidity-sdk/Utils.sol";
5+
import "wormhole-solidity-sdk/libraries/BytesParsing.sol";
6+
7+
import "../interfaces/IMsgManager.sol";
8+
import "../interfaces/ITransceiver.sol";
9+
import "../libraries/TransceiverHelpers.sol";
10+
11+
import {MsgManagerBase} from "./MsgManagerBase.sol";
12+
13+
contract MsgManager is IMsgManager, MsgManagerBase {
14+
string public constant MSG_MANAGER_VERSION = "1.0.0";
15+
16+
// =============== Setup =================================================================
17+
18+
constructor(
19+
uint16 _chainId
20+
) MsgManagerBase(address(0), Mode.LOCKING, _chainId) {}
21+
22+
function _initialize() internal virtual override {
23+
_init();
24+
_checkThresholdInvariants();
25+
_checkTransceiversInvariants();
26+
}
27+
28+
function _init() internal onlyInitializing {
29+
// check if the owner is the deployer of this contract
30+
if (msg.sender != deployer) {
31+
revert UnexpectedDeployer(deployer, msg.sender);
32+
}
33+
if (msg.value != 0) {
34+
revert UnexpectedMsgValue();
35+
}
36+
__PausedOwnable_init(msg.sender, msg.sender);
37+
__ReentrancyGuard_init();
38+
}
39+
40+
// =============== Storage ==============================================================
41+
42+
bytes32 private constant PEERS_SLOT = bytes32(uint256(keccak256("mmgr.peers")) - 1);
43+
44+
// =============== Storage Getters/Setters ==============================================
45+
46+
function _getPeersStorage()
47+
internal
48+
pure
49+
returns (mapping(uint16 => MsgManagerPeer) storage $)
50+
{
51+
uint256 slot = uint256(PEERS_SLOT);
52+
assembly ("memory-safe") {
53+
$.slot := slot
54+
}
55+
}
56+
57+
// =============== Public Getters ========================================================
58+
59+
/// @inheritdoc IMsgManager
60+
function getPeer(
61+
uint16 chainId_
62+
) external view returns (MsgManagerPeer memory) {
63+
return _getPeersStorage()[chainId_];
64+
}
65+
66+
// =============== Admin ==============================================================
67+
68+
/// @inheritdoc IMsgManager
69+
function setPeer(uint16 peerChainId, bytes32 peerAddress) public onlyOwner {
70+
if (peerChainId == 0) {
71+
revert InvalidPeerChainIdZero();
72+
}
73+
if (peerAddress == bytes32(0)) {
74+
revert InvalidPeerZeroAddress();
75+
}
76+
if (peerChainId == chainId) {
77+
revert InvalidPeerSameChainId();
78+
}
79+
80+
MsgManagerPeer memory oldPeer = _getPeersStorage()[peerChainId];
81+
82+
_getPeersStorage()[peerChainId].peerAddress = peerAddress;
83+
84+
emit PeerUpdated(peerChainId, oldPeer.peerAddress, peerAddress);
85+
}
86+
87+
/// ============== Invariants =============================================
88+
89+
/// @dev When we add new immutables, this function should be updated
90+
function _checkImmutables() internal view virtual override {
91+
super._checkImmutables();
92+
}
93+
94+
// ==================== External Interface ===============================================
95+
96+
/// @inheritdoc IMsgManager
97+
function sendMessage(
98+
uint16 recipientChain,
99+
bytes calldata payload,
100+
bytes memory transceiverInstructions
101+
) external payable nonReentrant whenNotPaused returns (uint64 sequence) {
102+
sequence = _useMessageSequence();
103+
104+
bytes32 recipientAddress = _getPeersStorage()[recipientChain].peerAddress;
105+
106+
(uint256 totalPriceQuote, bytes memory encodedNttManagerPayload) = _sendMessage(
107+
recipientChain, recipientAddress, sequence, payload, transceiverInstructions
108+
);
109+
110+
emit MessageSent(
111+
recipientChain, recipientAddress, sequence, totalPriceQuote, encodedNttManagerPayload
112+
);
113+
}
114+
115+
/// @dev Override this function to handle your messages.
116+
function _handleMsg(
117+
uint16 sourceChainId,
118+
bytes32 sourceManagerAddress,
119+
TransceiverStructs.NttManagerMessage memory message,
120+
bytes32 digest
121+
) internal virtual override {}
122+
123+
// ==================== Internal Helpers ===============================================
124+
125+
/// @dev Verify that the peer address saved for `sourceChainId` matches the `peerAddress`.
126+
function _verifyPeer(uint16 sourceChainId, bytes32 peerAddress) internal view override {
127+
if (_getPeersStorage()[sourceChainId].peerAddress != peerAddress) {
128+
revert InvalidPeer(sourceChainId, peerAddress);
129+
}
130+
}
131+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// SPDX-License-Identifier: Apache 2
2+
pragma solidity >=0.8.8 <0.9.0;
3+
4+
import "wormhole-solidity-sdk/Utils.sol";
5+
import "wormhole-solidity-sdk/libraries/BytesParsing.sol";
6+
7+
import "../interfaces/IMsgReceiver.sol";
8+
import "../interfaces/ITransceiver.sol";
9+
import "../libraries/TransceiverHelpers.sol";
10+
11+
import "./ManagerBase.sol";
12+
13+
abstract contract MsgManagerBase is ManagerBase, IMsgReceiver {
14+
// =============== Setup =================================================================
15+
16+
constructor(address _token, Mode _mode, uint16 _chainId) ManagerBase(_token, _mode, _chainId) {}
17+
18+
// ==================== External Interface ===============================================
19+
20+
function attestationReceived(
21+
uint16 sourceChainId,
22+
bytes32 sourceManagerAddress,
23+
TransceiverStructs.NttManagerMessage memory payload
24+
) external onlyTransceiver whenNotPaused {
25+
_verifyPeer(sourceChainId, sourceManagerAddress);
26+
27+
// Compute manager message digest and record transceiver attestation.
28+
bytes32 nttManagerMessageHash = _recordTransceiverAttestation(sourceChainId, payload);
29+
30+
if (isMessageApproved(nttManagerMessageHash)) {
31+
executeMsg(sourceChainId, sourceManagerAddress, payload);
32+
}
33+
}
34+
35+
function executeMsg(
36+
uint16 sourceChainId,
37+
bytes32 sourceNttManagerAddress,
38+
TransceiverStructs.NttManagerMessage memory message
39+
) public whenNotPaused {
40+
(bytes32 digest, bool alreadyExecuted) =
41+
_isMessageExecuted(sourceChainId, sourceNttManagerAddress, message);
42+
43+
if (alreadyExecuted) {
44+
return;
45+
}
46+
47+
_handleMsg(sourceChainId, sourceNttManagerAddress, message, digest);
48+
}
49+
50+
// ==================== Internal Helpers ===============================================
51+
52+
/// @dev Override this function to handle your messages.
53+
function _handleMsg(
54+
uint16 sourceChainId,
55+
bytes32 sourceManagerAddress,
56+
TransceiverStructs.NttManagerMessage memory message,
57+
bytes32 digest
58+
) internal virtual {}
59+
60+
function _sendMessage(
61+
uint16 recipientChain,
62+
bytes32 recipientManagerAddress,
63+
uint64 sequence,
64+
bytes memory payload,
65+
bytes memory transceiverInstructions
66+
) internal returns (uint256 totalPriceQuote, bytes memory encodedNttManagerPayload) {
67+
// verify chain has not forked
68+
checkFork(evmChainId);
69+
70+
address[] memory enabledTransceivers;
71+
TransceiverStructs.TransceiverInstruction[] memory instructions;
72+
uint256[] memory priceQuotes;
73+
(enabledTransceivers, instructions, priceQuotes, totalPriceQuote) =
74+
_prepareForTransfer(recipientChain, transceiverInstructions);
75+
(recipientChain, transceiverInstructions);
76+
77+
// construct the NttManagerMessage payload
78+
encodedNttManagerPayload = TransceiverStructs.encodeNttManagerMessage(
79+
TransceiverStructs.NttManagerMessage(
80+
// TODO: Should we use `address(this)` instead of `msg.sender`?
81+
bytes32(uint256(sequence)),
82+
toWormholeFormat(msg.sender),
83+
payload
84+
)
85+
);
86+
87+
// send the message
88+
_sendMessageToTransceivers(
89+
recipientChain,
90+
recipientManagerAddress, // refundAddress
91+
recipientManagerAddress,
92+
priceQuotes,
93+
instructions,
94+
enabledTransceivers,
95+
encodedNttManagerPayload
96+
);
97+
}
98+
99+
/// @dev Verify that the peer address saved for `sourceChainId` matches the `peerAddress`.
100+
function _verifyPeer(uint16 sourceChainId, bytes32 peerAddress) internal view virtual;
101+
}

0 commit comments

Comments
 (0)