referrerpolicy=no-referrer-when-downgrade

snowbridge_outbound_queue_primitives/v2/
message.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <hello@snowfork.com>
//! # Outbound V2 primitives

use codec::{Decode, DecodeWithMemTracking, Encode};
use frame_support::{pallet_prelude::ConstU32, BoundedVec};
use scale_info::TypeInfo;
use sp_core::{RuntimeDebug, H160, H256};
use sp_std::vec::Vec;

use crate::{OperatingMode, SendError};
use abi::{
	CallContractParams, MintForeignTokenParams, RegisterForeignTokenParams, SetOperatingModeParams,
	UnlockNativeTokenParams, UpgradeParams,
};
use alloy_core::{
	primitives::{Address, Bytes, FixedBytes, U256},
	sol_types::SolValue,
};

pub mod abi {
	use alloy_core::sol;

	sol! {
		struct OutboundMessageWrapper {
			// origin
			bytes32 origin;
			// Message nonce
			uint64 nonce;
			// Topic
			bytes32 topic;
			// Commands
			CommandWrapper[] commands;
		}

		struct CommandWrapper {
			uint8 kind;
			uint64 gas;
			bytes payload;
		}

		// Payload for Upgrade
		struct UpgradeParams {
			// The address of the implementation contract
			address implAddress;
			// Codehash of the new implementation contract.
			bytes32 implCodeHash;
			// Parameters used to upgrade storage of the gateway
			bytes initParams;
		}

		// Payload for SetOperatingMode instruction
		struct SetOperatingModeParams {
			/// The new operating mode
			uint8 mode;
		}

		// Payload for NativeTokenUnlock instruction
		struct UnlockNativeTokenParams {
			// Token address
			address token;
			// Recipient address
			address recipient;
			// Amount to unlock
			uint128 amount;
		}

		// Payload for RegisterForeignToken
		struct RegisterForeignTokenParams {
			/// @dev The token ID (hash of stable location id of token)
			bytes32 foreignTokenID;
			/// @dev The name of the token
			bytes name;
			/// @dev The symbol of the token
			bytes symbol;
			/// @dev The decimal of the token
			uint8 decimals;
		}

		// Payload for MintForeignTokenParams instruction
		struct MintForeignTokenParams {
			// Foreign token ID
			bytes32 foreignTokenID;
			// Recipient address
			address recipient;
			// Amount to mint
			uint128 amount;
		}

		// Payload for CallContract
		struct CallContractParams {
			// target contract
			address target;
			// Call data
			bytes data;
			// Ether value
			uint256 value;
		}
	}
}

#[derive(Encode, Decode, TypeInfo, PartialEq, Clone, RuntimeDebug)]
pub struct OutboundCommandWrapper {
	pub kind: u8,
	pub gas: u64,
	pub payload: Vec<u8>,
}

#[derive(Encode, Decode, TypeInfo, PartialEq, Clone, RuntimeDebug)]
pub struct OutboundMessage {
	/// Origin
	pub origin: H256,
	/// Nonce
	pub nonce: u64,
	/// Topic
	pub topic: H256,
	/// Commands
	pub commands: BoundedVec<OutboundCommandWrapper, ConstU32<MAX_COMMANDS>>,
}

pub const MAX_COMMANDS: u32 = 8;

/// A message which can be accepted by implementations of `/[`SendMessage`\]`
#[derive(Encode, Decode, DecodeWithMemTracking, TypeInfo, PartialEq, Clone, RuntimeDebug)]
pub struct Message {
	/// Origin
	pub origin: H256,
	/// ID
	pub id: H256,
	/// Fee
	pub fee: u128,
	/// Commands
	pub commands: BoundedVec<Command, ConstU32<MAX_COMMANDS>>,
}

/// A command which is executable by the Gateway contract on Ethereum
#[derive(Clone, Encode, Decode, DecodeWithMemTracking, PartialEq, RuntimeDebug, TypeInfo)]
pub enum Command {
	/// Upgrade the Gateway contract
	Upgrade {
		/// Address of the new implementation contract
		impl_address: H160,
		/// Codehash of the implementation contract
		impl_code_hash: H256,
		/// Invoke an initializer in the implementation contract
		initializer: Initializer,
	},
	/// Set the global operating mode of the Gateway contract
	SetOperatingMode {
		/// The new operating mode
		mode: OperatingMode,
	},
	/// Unlock ERC20 tokens
	UnlockNativeToken {
		/// Address of the ERC20 token
		token: H160,
		/// The recipient of the tokens
		recipient: H160,
		/// The amount of tokens to transfer
		amount: u128,
	},
	/// Register foreign token from Polkadot
	RegisterForeignToken {
		/// ID for the token
		token_id: H256,
		/// Name of the token
		name: Vec<u8>,
		/// Short symbol for the token
		symbol: Vec<u8>,
		/// Number of decimal places
		decimals: u8,
	},
	/// Mint foreign token from Polkadot
	MintForeignToken {
		/// ID for the token
		token_id: H256,
		/// The recipient of the newly minted tokens
		recipient: H160,
		/// The amount of tokens to mint
		amount: u128,
	},
	/// Call Contract on Ethereum
	CallContract {
		/// Target contract address
		target: H160,
		/// ABI-encoded calldata
		calldata: Vec<u8>,
		/// Maximum gas to forward to target contract
		gas: u64,
		/// Include ether held by agent contract
		value: u128,
	},
}

impl Command {
	/// Compute the enum variant index
	pub fn index(&self) -> u8 {
		match self {
			Command::Upgrade { .. } => 0,
			Command::SetOperatingMode { .. } => 1,
			Command::UnlockNativeToken { .. } => 2,
			Command::RegisterForeignToken { .. } => 3,
			Command::MintForeignToken { .. } => 4,
			Command::CallContract { .. } => 5,
		}
	}

	/// ABI-encode the Command.
	pub fn abi_encode(&self) -> Vec<u8> {
		match self {
			Command::Upgrade { impl_address, impl_code_hash, initializer, .. } => UpgradeParams {
				implAddress: Address::from(impl_address.as_fixed_bytes()),
				implCodeHash: FixedBytes::from(impl_code_hash.as_fixed_bytes()),
				initParams: Bytes::from(initializer.params.clone()),
			}
			.abi_encode(),
			Command::SetOperatingMode { mode } =>
				SetOperatingModeParams { mode: (*mode) as u8 }.abi_encode(),
			Command::UnlockNativeToken { token, recipient, amount, .. } =>
				UnlockNativeTokenParams {
					token: Address::from(token.as_fixed_bytes()),
					recipient: Address::from(recipient.as_fixed_bytes()),
					amount: *amount,
				}
				.abi_encode(),
			Command::RegisterForeignToken { token_id, name, symbol, decimals } =>
				RegisterForeignTokenParams {
					foreignTokenID: FixedBytes::from(token_id.as_fixed_bytes()),
					name: Bytes::from(name.to_vec()),
					symbol: Bytes::from(symbol.to_vec()),
					decimals: *decimals,
				}
				.abi_encode(),
			Command::MintForeignToken { token_id, recipient, amount } => MintForeignTokenParams {
				foreignTokenID: FixedBytes::from(token_id.as_fixed_bytes()),
				recipient: Address::from(recipient.as_fixed_bytes()),
				amount: *amount,
			}
			.abi_encode(),
			Command::CallContract { target, calldata: data, value, .. } => CallContractParams {
				target: Address::from(target.as_fixed_bytes()),
				data: Bytes::from(data.to_vec()),
				value: U256::try_from(*value).unwrap(),
			}
			.abi_encode(),
		}
	}
}

/// Representation of a call to the initializer of an implementation contract.
/// The initializer has the following ABI signature: `initialize(bytes)`.
#[derive(Clone, Encode, Decode, DecodeWithMemTracking, PartialEq, RuntimeDebug, TypeInfo)]
pub struct Initializer {
	/// ABI-encoded params of type `bytes` to pass to the initializer
	pub params: Vec<u8>,
	/// The initializer is allowed to consume this much gas at most.
	pub maximum_required_gas: u64,
}

pub trait SendMessage {
	type Ticket: Clone + Encode + Decode;

	/// Validate an outbound message and return a tuple:
	/// 1. Ticket for submitting the message
	/// 2. Delivery fee
	fn validate(message: &Message) -> Result<Self::Ticket, SendError>;

	/// Submit the message ticket for eventual delivery to Ethereum
	fn deliver(ticket: Self::Ticket) -> Result<H256, SendError>;
}

pub trait GasMeter {
	/// Measures the maximum amount of gas a command payload will require to *dispatch*, NOT
	/// including validation & verification.
	fn maximum_dispatch_gas_used_at_most(command: &Command) -> u64;
}

/// A meter that assigns a constant amount of gas for the execution of a command
///
/// The gas figures are extracted from this report:
/// > forge test --match-path test/Gateway.t.sol --gas-report
///
/// A healthy buffer is added on top of these figures to account for:
/// * The EIP-150 63/64 rule
/// * Future EVM upgrades that may increase gas cost
pub struct ConstantGasMeter;

impl GasMeter for ConstantGasMeter {
	fn maximum_dispatch_gas_used_at_most(command: &Command) -> u64 {
		match command {
			Command::SetOperatingMode { .. } => 40_000,
			Command::Upgrade { initializer, .. } => {
				// total maximum gas must also include the gas used for updating the proxy before
				// the the initializer is called.
				50_000 + initializer.maximum_required_gas
			},
			Command::UnlockNativeToken { .. } => 100_000,
			Command::RegisterForeignToken { .. } => 1_200_000,
			Command::MintForeignToken { .. } => 100_000,
			Command::CallContract { gas: gas_limit, .. } => *gas_limit,
		}
	}
}

impl GasMeter for () {
	fn maximum_dispatch_gas_used_at_most(_: &Command) -> u64 {
		1
	}
}