forked from paritytech/polkadot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v2.rs
311 lines (283 loc) · 8.89 KB
/
v2.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
310
311
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <hello@snowfork.com>
//! # Outbound V2 primitives
use crate::outbound::{OperatingMode, SendError};
use codec::{Decode, Encode};
use frame_support::{pallet_prelude::ConstU32, BoundedVec};
use hex_literal::hex;
use scale_info::TypeInfo;
use sp_arithmetic::traits::{BaseArithmetic, Unsigned};
use sp_core::{RuntimeDebug, H160, H256};
use sp_std::{vec, vec::Vec};
use crate::outbound::v2::abi::{
CreateAgentParams, MintForeignTokenParams, RegisterForeignTokenParams, SetOperatingModeParams,
UnlockNativeTokenParams, UpgradeParams,
};
use alloy_primitives::{Address, FixedBytes};
use alloy_sol_types::SolValue;
pub mod abi {
use super::MAX_COMMANDS;
use alloy_sol_types::sol;
use codec::{Decode, Encode};
use frame_support::BoundedVec;
use scale_info::TypeInfo;
use sp_core::{ConstU32, RuntimeDebug, H256};
use sp_std::vec::Vec;
sol! {
struct InboundMessageWrapper {
// origin
bytes32 origin;
// Message nonce
uint64 nonce;
// Commands
CommandWrapper[] commands;
}
#[derive(Encode, Decode, RuntimeDebug, PartialEq,TypeInfo)]
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 CreateAgent
struct CreateAgentParams {
/// @dev The agent ID of the consensus system
bytes32 agentID;
}
// 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;
}
}
#[derive(Encode, Decode, TypeInfo, PartialEq, Clone, RuntimeDebug)]
pub struct InboundMessage {
/// Origin
pub origin: H256,
/// Nonce
pub nonce: u64,
/// Commands
pub commands: BoundedVec<CommandWrapper, ConstU32<MAX_COMMANDS>>,
}
}
pub const MAX_COMMANDS: u32 = 8;
/// A message which can be accepted by implementations of `/[`SendMessage`\]`
#[derive(Encode, Decode, 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, 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,
/// Optionally invoke an initializer in the implementation contract
initializer: Option<Initializer>,
},
/// Create an agent representing a consensus system on Polkadot
CreateAgent {
/// The ID of the agent, derived from the `MultiLocation` of the consensus system on
/// Polkadot
agent_id: H256,
},
/// Set the global operating mode of the Gateway contract
SetOperatingMode {
/// The new operating mode
mode: OperatingMode,
},
/// Unlock ERC20 tokens
UnlockNativeToken {
/// ID of the agent
agent_id: H256,
/// 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,
},
}
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::CreateAgent { .. } => 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: initializer.clone().map_or(vec![], |i| i.params),
}
.abi_encode(),
Command::CreateAgent { agent_id } =>
CreateAgentParams { agentID: FixedBytes::from(agent_id.as_fixed_bytes()) }
.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: name.to_vec(),
symbol: 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(),
}
}
}
/// Representation of a call to the initializer of an implementation contract.
/// The initializer has the following ABI signature: `initialize(bytes)`.
#[derive(Clone, Encode, Decode, 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;
type Balance: BaseArithmetic + Unsigned + Copy;
/// Validate an outbound message and return a tuple:
/// 1. Ticket for submitting the message
/// 2. Delivery fee
fn validate(message: &Message) -> Result<(Self::Ticket, Self::Balance), 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::CreateAgent { .. } => 275_000,
Command::SetOperatingMode { .. } => 40_000,
Command::Upgrade { initializer, .. } => {
let initializer_max_gas = match *initializer {
Some(Initializer { maximum_required_gas, .. }) => maximum_required_gas,
None => 0,
};
// total maximum gas must also include the gas used for updating the proxy before
// the the initializer is called.
50_000 + initializer_max_gas
},
Command::UnlockNativeToken { .. } => 100_000,
Command::RegisterForeignToken { .. } => 1_200_000,
Command::MintForeignToken { .. } => 100_000,
}
}
}
impl GasMeter for () {
fn maximum_dispatch_gas_used_at_most(_: &Command) -> u64 {
1
}
}
// Origin for high-priority governance commands
pub fn primary_governance_origin() -> H256 {
hex!("0000000000000000000000000000000000000000000000000000000000000001").into()
}
// Origin for lower-priority governance commands
pub fn second_governance_origin() -> H256 {
hex!("0000000000000000000000000000000000000000000000000000000000000002").into()
}