Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit fbff79d

Browse files
gavofyorkshawntabrizi
authored andcommitted
Land XCM Builder
1 parent f0a352a commit fbff79d

File tree

13 files changed

+598
-138
lines changed

13 files changed

+598
-138
lines changed

Cargo.lock

Lines changed: 151 additions & 134 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ members = [
4141
"service",
4242
"validation",
4343
"xcm",
44+
"xcm/xcm-builder",
4445
"xcm/xcm-executor",
4546
"node/collation-generation",
4647
"node/core/av-store",

node/collation-generation/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ mod tests {
407407
Arc::new(CollationGenerationConfig {
408408
key: CollatorPair::generate().0,
409409
collator: Box::new(|_: Hash, _vd: &ValidationData| {
410-
Box::new(TestCollator)
410+
TestCollator.boxed()
411411
}),
412412
para_id: para_id.into(),
413413
})

node/overseer/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1876,7 +1876,7 @@ mod tests {
18761876
fn test_collator_generation_msg() -> CollationGenerationMessage {
18771877
CollationGenerationMessage::Initialize(CollationGenerationConfig {
18781878
key: CollatorPair::generate().0,
1879-
collator: Box::new(|_, _| Box::new(TestCollator)),
1879+
collator: Box::new(|_, _| TestCollator.boxed()),
18801880
para_id: Default::default(),
18811881
})
18821882
}

node/primitives/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use polkadot_statement_table::{
3535
},
3636
v1::Misbehavior as TableMisbehavior,
3737
};
38+
use std::pin::Pin;
3839

3940
pub use sp_core::traits::SpawnNamed;
4041

@@ -289,7 +290,7 @@ pub struct CollationGenerationConfig {
289290
/// Will be called with the hash of the relay chain block the parachain
290291
/// block should be build on and the [`ValidationData`] that provides
291292
/// information about the state of the parachain on the relay chain.
292-
pub collator: Box<dyn Fn(Hash, &ValidationData) -> Box<dyn Future<Output = Option<Collation>> + Unpin + Send> + Send + Sync>,
293+
pub collator: Box<dyn Fn(Hash, &ValidationData) -> Pin<Box<dyn Future<Output = Option<Collation>> + Send>> + Send + Sync>,
293294
/// The parachain that this collator collates for
294295
pub para_id: ParaId,
295296
}

parachain/src/primitives.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,21 @@ impl Id {
124124
}
125125

126126
/// Returns `true` if this parachain runs with system-level privileges.
127+
/// Use IsSystem instead.
128+
#[deprecated]
127129
pub fn is_system(&self) -> bool { self.0 < USER_INDEX_START }
128130
}
129131

132+
pub trait IsSystem {
133+
fn is_system(&self) -> bool;
134+
}
135+
136+
impl IsSystem for Id {
137+
fn is_system(&self) -> bool {
138+
self.0 < USER_INDEX_START
139+
}
140+
}
141+
130142
impl sp_std::ops::Add<u32> for Id {
131143
type Output = Self;
132144

roadmap/implementers-guide/src/node/collators/collation-generation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ struct CollationGenerationConfig {
3939
/// Collate will be called with the relay chain hash the parachain should build
4040
/// a block on and the `ValidationData` that provides information about the state
4141
/// of the parachain on the relay chain.
42-
collator: Box<dyn Fn(Hash, &ValidationData) -> Box<dyn Future<Output = Option<Collation>>>>
42+
collator: Box<dyn Fn(Hash, &ValidationData) -> Pin<Box<dyn Future<Output = Option<Collation>>>>>
4343
para_id: ParaId,
4444
}
4545
```

xcm/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,10 @@ edition = "2018"
77

88
[dependencies]
99
codec = { package = "parity-scale-codec", version = "1.3.5", default-features = false, features = [ "derive" ] }
10+
11+
[features]
12+
default = ["std"]
13+
wasm-api = []
14+
std = [
15+
"codec/std",
16+
]

xcm/xcm-builder/Cargo.toml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[package]
2+
authors = ["Parity Technologies <admin@parity.io>"]
3+
edition = "2018"
4+
name = "xcm-builder"
5+
description = "Tools & types for building with XCM and its executor."
6+
version = "0.8.22"
7+
8+
[dependencies]
9+
codec = { package = "parity-scale-codec", version = "1.3.0", default-features = false, features = ["derive"] }
10+
11+
xcm = { path = "..", default-features = false }
12+
xcm-executor = { path = "../xcm-executor", default-features = false }
13+
sp-std = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
14+
sp-arithmetic = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
15+
sp-core = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
16+
sp-io = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
17+
sp-runtime = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
18+
frame-support = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
19+
frame-system = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "master" }
20+
21+
# Polkadot dependencies
22+
polkadot-parachain = { path = "../../parachain", default-features = false }
23+
24+
[features]
25+
default = ["std"]
26+
std = [
27+
"codec/std",
28+
"frame-support/std",
29+
"polkadot-parachain/std",
30+
"sp-std/std",
31+
"sp-arithmetic/std",
32+
"sp-core/std",
33+
"sp-runtime/std",
34+
"xcm/std",
35+
"xcm-executor/std",
36+
]
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2020 Parity Technologies (UK) Ltd.
2+
// This file is part of Polkadot.
3+
4+
// Polkadot is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
9+
// Polkadot is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License
15+
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
16+
17+
use sp_std::{result, convert::TryInto, marker::PhantomData};
18+
use xcm::v0::{Error, Result, MultiAsset, MultiLocation};
19+
use sp_arithmetic::traits::SaturatedConversion;
20+
use frame_support::traits::{ExistenceRequirement::AllowDeath, WithdrawReason};
21+
use xcm_executor::traits::{MatchesFungible, LocationConversion, TransactAsset};
22+
23+
pub struct CurrencyAdapter<Currency, Matcher, AccountIdConverter, AccountId>(
24+
PhantomData<Currency>,
25+
PhantomData<Matcher>,
26+
PhantomData<AccountIdConverter>,
27+
PhantomData<AccountId>,
28+
);
29+
30+
impl<
31+
Matcher: MatchesFungible<Currency::Balance>,
32+
AccountIdConverter: LocationConversion<AccountId>,
33+
Currency: frame_support::traits::Currency<AccountId>,
34+
AccountId, // can't get away without it since Currency is generic over it.
35+
> TransactAsset for CurrencyAdapter<Currency, Matcher, AccountIdConverter, AccountId> {
36+
37+
fn deposit_asset(what: &MultiAsset, who: &MultiLocation) -> Result {
38+
// Check we handle this asset.
39+
let amount = Matcher::matches_fungible(&what).ok_or(())?.saturated_into();
40+
let who = AccountIdConverter::from_location(who).ok_or(())?;
41+
let balance_amount = amount.try_into().map_err(|_| ())?;
42+
let _imbalance = Currency::deposit_creating(&who, balance_amount);
43+
Ok(())
44+
}
45+
46+
fn withdraw_asset(what: &MultiAsset, who: &MultiLocation) -> result::Result<MultiAsset, Error> {
47+
// Check we handle this asset.
48+
let amount = Matcher::matches_fungible(&what).ok_or(())?.saturated_into();
49+
let who = AccountIdConverter::from_location(who).ok_or(())?;
50+
let balance_amount = amount.try_into().map_err(|_| ())?;
51+
Currency::withdraw(&who, balance_amount, WithdrawReason::Transfer.into(), AllowDeath).map_err(|_| ())?;
52+
Ok(what.clone())
53+
}
54+
}

0 commit comments

Comments
 (0)