-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathismp.rs
280 lines (243 loc) · 8.52 KB
/
ismp.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
// Copyright (C) Polytope Labs Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::{
alloc::{boxed::Box, string::ToString},
weights, AccountId, Assets, Balance, Balances, Ismp, IsmpParachain, Mmr, ParachainInfo,
Runtime, RuntimeEvent, Timestamp, TokenGatewayInspector, TokenGovernor, TreasuryPalletId,
XcmGateway, EXISTENTIAL_DEPOSIT,
};
use frame_support::{
pallet_prelude::{ConstU32, Get},
parameter_types,
traits::AsEnsureOriginWithArg,
PalletId,
};
use frame_system::EnsureRoot;
use hyperbridge_client_machine::HyperbridgeClientMachine;
use ismp::{
error::Error,
host::StateMachine,
module::IsmpModule,
router::{IsmpRouter, PostRequest, Request, Response},
};
#[cfg(feature = "runtime-benchmarks")]
use pallet_assets::BenchmarkHelper;
use pallet_xcm_gateway::AssetGatewayParams;
use sp_core::{crypto::AccountId32, H256};
use anyhow::anyhow;
use ismp::router::Timeout;
use ismp_sync_committee::constants::{gnosis, mainnet::Mainnet};
use pallet_ismp::{dispatcher::FeeMetadata, ModuleId};
use sp_runtime::Permill;
use sp_std::prelude::*;
#[cfg(feature = "runtime-benchmarks")]
use staging_xcm::latest::Location;
#[derive(Default)]
pub struct ProxyModule;
pub struct HostStateMachine;
impl Get<StateMachine> for HostStateMachine {
fn get() -> StateMachine {
StateMachine::Polkadot(ParachainInfo::get().into())
}
}
pub type Ethereum = ismp_sync_committee::pallet::Instance1;
pub type Gnosis = ismp_sync_committee::pallet::Instance2;
impl ismp_sync_committee::pallet::Config<Ethereum> for Runtime {
type AdminOrigin = EnsureRoot<AccountId>;
type IsmpHost = Ismp;
}
impl ismp_sync_committee::pallet::Config<Gnosis> for Runtime {
type AdminOrigin = EnsureRoot<AccountId>;
type IsmpHost = Ismp;
}
impl pallet_state_coprocessor::Config for Runtime {
type IsmpHost = Ismp;
type Mmr = Mmr;
}
pub struct Coprocessor;
impl Get<Option<StateMachine>> for Coprocessor {
fn get() -> Option<StateMachine> {
Some(HostStateMachine::get())
}
}
impl ismp_grandpa::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type IsmpHost = pallet_ismp::Pallet<Runtime>;
type WeightInfo = weights::ismp_grandpa::WeightInfo<Runtime>;
}
impl pallet_ismp::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type AdminOrigin = EnsureRoot<AccountId>;
type HostStateMachine = HostStateMachine;
type TimestampProvider = Timestamp;
type Router = Router;
type Balance = Balance;
type Currency = Balances;
type Coprocessor = Coprocessor;
type ConsensusClients = (
ismp_bsc::BscClient<Ismp, Runtime, ismp_bsc::Mainnet>,
ismp_sync_committee::SyncCommitteeConsensusClient<Ismp, Mainnet, Runtime, Ethereum>,
ismp_sync_committee::SyncCommitteeConsensusClient<Ismp, gnosis::Mainnet, Runtime, Gnosis>,
ismp_parachain::ParachainConsensusClient<
Runtime,
IsmpParachain,
HyperbridgeClientMachine<Runtime, Ismp>,
>,
ismp_grandpa::consensus::GrandpaConsensusClient<Runtime>,
);
type OffchainDB = Mmr;
type WeightProvider = ();
type FeeHandler = ();
}
impl pallet_ismp_relayer::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type IsmpHost = Ismp;
}
impl pallet_ismp_host_executive::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type IsmpHost = Ismp;
}
impl pallet_call_decompressor::Config for Runtime {
type MaxCallSize = ConstU32<3>;
}
impl pallet_fishermen::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type IsmpHost = Ismp;
}
impl ismp_parachain::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type IsmpHost = Ismp;
type WeightInfo = weights::ismp_parachain::WeightInfo<Runtime>;
}
parameter_types! {
pub const AssetPalletId: PalletId = PalletId(*b"asset-tx");
pub const TransferParams: AssetGatewayParams = AssetGatewayParams::from_parts(Permill::from_parts(1_000)); // 0.1%
}
impl pallet_xcm_gateway::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type PalletId = AssetPalletId;
type Params = TransferParams;
type IsmpHost = Ismp;
type Assets = Assets;
}
impl pallet_token_governor::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Dispatcher = Ismp;
type TreasuryAccount = TreasuryPalletId;
}
#[cfg(feature = "runtime-benchmarks")]
pub struct XcmBenchmarkHelper;
#[cfg(feature = "runtime-benchmarks")]
impl BenchmarkHelper<H256> for XcmBenchmarkHelper {
fn create_asset_id_parameter(id: u32) -> H256 {
use codec::Encode;
use staging_xcm::v4::Junction::Parachain;
sp_io::hashing::keccak_256(&Location::new(1, Parachain(id)).encode()).into()
}
}
parameter_types! {
pub const AssetDeposit: Balance = EXISTENTIAL_DEPOSIT;
pub const AssetAccountDeposit: Balance = EXISTENTIAL_DEPOSIT * 2;
pub const MetadataDepositBase: Balance = EXISTENTIAL_DEPOSIT * 2;
pub const MetadataDepositPerByte: Balance = EXISTENTIAL_DEPOSIT / 2;
pub const ApprovalDeposit: Balance = EXISTENTIAL_DEPOSIT * 2;
}
impl pallet_assets::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Balance = Balance;
type AssetId = H256;
type AssetIdParameter = H256;
type Currency = Balances;
type CreateOrigin = AsEnsureOriginWithArg<frame_system::EnsureSigned<AccountId32>>;
type ForceOrigin = EnsureRoot<AccountId32>;
type AssetDeposit = AssetDeposit;
type AssetAccountDeposit = AssetAccountDeposit;
type MetadataDepositBase = MetadataDepositBase;
type MetadataDepositPerByte = MetadataDepositPerByte;
type ApprovalDeposit = ApprovalDeposit;
type StringLimit = ConstU32<50>;
type Freezer = ();
type WeightInfo = weights::pallet_assets::WeightInfo<Runtime>;
type CallbackHandle = ();
type Extra = ();
type RemoveItemsLimit = ConstU32<5>;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = XcmBenchmarkHelper;
}
impl pallet_token_gateway_inspector::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
}
impl IsmpModule for ProxyModule {
fn on_accept(&self, request: PostRequest) -> Result<(), anyhow::Error> {
if request.dest != HostStateMachine::get() {
TokenGatewayInspector::inspect_request(&request)?;
Ismp::dispatch_request(
Request::Post(request),
FeeMetadata::<Runtime> { payer: [0u8; 32].into(), fee: Default::default() },
)?;
return Ok(());
}
let pallet_id =
ModuleId::from_bytes(&request.to).map_err(|err| Error::Custom(err.to_string()))?;
let xcm_gateway = ModuleId::Evm(XcmGateway::token_gateway_address(&request.source));
let token_governor = ModuleId::Pallet(PalletId(pallet_token_governor::PALLET_ID));
match pallet_id {
id if id == xcm_gateway =>
pallet_xcm_gateway::Module::<Runtime>::default().on_accept(request),
id if id == token_governor => TokenGovernor::default().on_accept(request),
_ => Err(anyhow!("Destination module not found")),
}
}
fn on_response(&self, response: Response) -> Result<(), anyhow::Error> {
if response.dest_chain() != HostStateMachine::get() {
Ismp::dispatch_response(
response,
FeeMetadata::<Runtime> { payer: [0u8; 32].into(), fee: Default::default() },
)?;
return Ok(());
}
Err(anyhow!("Destination module not found"))
}
fn on_timeout(&self, timeout: Timeout) -> Result<(), anyhow::Error> {
let (from, source, dest) = match &timeout {
Timeout::Request(Request::Post(post)) => {
if post.source != HostStateMachine::get() {
TokenGatewayInspector::handle_timeout(post)?;
}
(&post.from, &post.source, &post.dest)
},
Timeout::Request(Request::Get(get)) => (&get.from, &get.source, &get.dest),
Timeout::Response(res) =>
(&res.source_module(), &res.source_chain(), &res.dest_chain()),
};
if *source != HostStateMachine::get() {
return Ok(());
}
let pallet_id = ModuleId::from_bytes(from).map_err(|err| Error::Custom(err.to_string()))?;
let xcm_gateway = ModuleId::Evm(XcmGateway::token_gateway_address(dest));
match pallet_id {
id if id == xcm_gateway =>
pallet_xcm_gateway::Module::<Runtime>::default().on_timeout(timeout),
// instead of returning an error, do nothing. The timeout is for a connected chain.
_ => Ok(()),
}
}
}
#[derive(Default)]
pub struct Router;
impl IsmpRouter for Router {
fn module_for_id(&self, _bytes: Vec<u8>) -> Result<Box<dyn IsmpModule>, anyhow::Error> {
Ok(Box::new(ProxyModule::default()))
}
}