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

Commit b4fb14b

Browse files
committed
WIP: Add current_relay_block to ValidationParams, resolve circular dep
Resolving the circular dependency was relatively simple: just move a bunch of parachain-specific stuff from the parachain package into the primitives package, then fix up the Cargo manifests. Then, publicly import all the stuff which was moved so that we don't break any external packages which depend on parachain. We have a deprecation notice on that `pub use` statement because downstream consumers should depend on items in the right place. Unfortunately, it doesn't actually do anything due to rust-lang/rust#47236. Still, we'll leave it in against the day that bug gets fixed. Adding current_relay_block to ValidationParams is only part of the work, of course: we now need to go back to where that struct is instantiated, and insert it there, tracing it back until we get to some kind of relay chain instance from which we can get the actual current value and insert it appropriately.
1 parent b1f88ed commit b4fb14b

File tree

4 files changed

+171
-161
lines changed

4 files changed

+171
-161
lines changed

parachain/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ sp-io = { git = "https://github.com/paritytech/substrate", branch = "cumulus-bra
1818
lazy_static = { version = "1.4.0", optional = true }
1919
parking_lot = { version = "0.10.0", optional = true }
2020
log = { version = "0.4.8", optional = true }
21+
polkadot-primitives = { path = "../primitives", default-features = false }
2122

2223
[target.'cfg(not(target_os = "unknown"))'.dependencies]
2324
shared_memory = { version = "0.10.0", optional = true }
@@ -44,4 +45,5 @@ std = [
4445
"sp-externalities",
4546
"sc-executor",
4647
"sp-io",
48+
"polkadot-primitives/std",
4749
]

parachain/src/lib.rs

+13-151
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,21 @@ mod wasm_api;
5050

5151
use rstd::vec::Vec;
5252

53-
use codec::{Encode, Decode, CompactAs};
54-
use sp_core::{RuntimeDebug, TypeId};
53+
use codec::{Encode, Decode};
5554

5655
#[cfg(all(not(feature = "std"), feature = "wasm-api"))]
5756
pub use wasm_api::*;
5857

58+
#[deprecated(note="moved to primitives package")]
59+
pub use polkadot_primitives::{BlockNumber, parachain::{
60+
AccountIdConversion,
61+
Id,
62+
IncomingMessage,
63+
LOWEST_USER_ID,
64+
ParachainDispatchOrigin,
65+
UpwardMessage,
66+
}};
67+
5968
/// Validation parameters for evaluating the parachain validity function.
6069
// TODO: balance downloads (https://github.com/paritytech/polkadot/issues/220)
6170
#[derive(PartialEq, Eq, Decode)]
@@ -65,6 +74,8 @@ pub struct ValidationParams {
6574
pub block_data: Vec<u8>,
6675
/// Previous head-data.
6776
pub parent_head: Vec<u8>,
77+
/// Number of the current relay chain block.
78+
pub current_relay_block: BlockNumber,
6879
}
6980

7081
/// The result of parachain validation.
@@ -75,152 +86,3 @@ pub struct ValidationResult {
7586
/// New head data that should be included in the relay chain state.
7687
pub head_data: Vec<u8>,
7788
}
78-
79-
/// Unique identifier of a parachain.
80-
#[derive(
81-
Clone, CompactAs, Copy, Decode, Default, Encode, Eq,
82-
Hash, Ord, PartialEq, PartialOrd, RuntimeDebug,
83-
)]
84-
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize, derive_more::Display))]
85-
pub struct Id(u32);
86-
87-
impl TypeId for Id {
88-
const TYPE_ID: [u8; 4] = *b"para";
89-
}
90-
91-
/// Type for determining the active set of parachains.
92-
pub trait ActiveThreads {
93-
/// Return the current ordered set of `Id`s of active parathreads.
94-
fn active_threads() -> Vec<Id>;
95-
}
96-
97-
impl From<Id> for u32 {
98-
fn from(x: Id) -> Self { x.0 }
99-
}
100-
101-
impl From<u32> for Id {
102-
fn from(x: u32) -> Self { Id(x) }
103-
}
104-
105-
const USER_INDEX_START: u32 = 1000;
106-
107-
/// The ID of the first user (non-system) parachain.
108-
pub const LOWEST_USER_ID: Id = Id(USER_INDEX_START);
109-
110-
impl Id {
111-
/// Create an `Id`.
112-
pub const fn new(id: u32) -> Self {
113-
Self(id)
114-
}
115-
116-
/// Returns `true` if this parachain runs with system-level privileges.
117-
pub fn is_system(&self) -> bool { self.0 < USER_INDEX_START }
118-
}
119-
120-
impl rstd::ops::Add<u32> for Id {
121-
type Output = Self;
122-
123-
fn add(self, other: u32) -> Self {
124-
Self(self.0 + other)
125-
}
126-
}
127-
128-
// TODO: Remove all of this, move sp-runtime::AccountIdConversion to own crate and and use that.
129-
// #360
130-
struct TrailingZeroInput<'a>(&'a [u8]);
131-
impl<'a> codec::Input for TrailingZeroInput<'a> {
132-
fn remaining_len(&mut self) -> Result<Option<usize>, codec::Error> {
133-
Ok(None)
134-
}
135-
136-
fn read(&mut self, into: &mut [u8]) -> Result<(), codec::Error> {
137-
let len = into.len().min(self.0.len());
138-
into[..len].copy_from_slice(&self.0[..len]);
139-
for i in &mut into[len..] {
140-
*i = 0;
141-
}
142-
self.0 = &self.0[len..];
143-
Ok(())
144-
}
145-
}
146-
147-
/// This type can be converted into and possibly from an AccountId (which itself is generic).
148-
pub trait AccountIdConversion<AccountId>: Sized {
149-
/// Convert into an account ID. This is infallible.
150-
fn into_account(&self) -> AccountId;
151-
152-
/// Try to convert an account ID into this type. Might not succeed.
153-
fn try_from_account(a: &AccountId) -> Option<Self>;
154-
}
155-
156-
/// Format is b"para" ++ encode(parachain ID) ++ 00.... where 00... is indefinite trailing
157-
/// zeroes to fill AccountId.
158-
impl<T: Encode + Decode + Default> AccountIdConversion<T> for Id {
159-
fn into_account(&self) -> T {
160-
(b"para", self).using_encoded(|b|
161-
T::decode(&mut TrailingZeroInput(b))
162-
).unwrap_or_default()
163-
}
164-
165-
fn try_from_account(x: &T) -> Option<Self> {
166-
x.using_encoded(|d| {
167-
if &d[0..4] != b"para" { return None }
168-
let mut cursor = &d[4..];
169-
let result = Decode::decode(&mut cursor).ok()?;
170-
if cursor.iter().all(|x| *x == 0) {
171-
Some(result)
172-
} else {
173-
None
174-
}
175-
})
176-
}
177-
}
178-
179-
/// Which origin a parachain's message to the relay chain should be dispatched from.
180-
#[derive(Clone, PartialEq, Eq, Encode, Decode)]
181-
#[cfg_attr(feature = "std", derive(Debug))]
182-
#[repr(u8)]
183-
pub enum ParachainDispatchOrigin {
184-
/// As a simple `Origin::Signed`, using `ParaId::account_id` as its value. This is good when
185-
/// interacting with standard modules such as `balances`.
186-
Signed,
187-
/// As the special `Origin::Parachain(ParaId)`. This is good when interacting with parachain-
188-
/// aware modules which need to succinctly verify that the origin is a parachain.
189-
Parachain,
190-
/// As the simple, superuser `Origin::Root`. This can only be done on specially permissioned
191-
/// parachains.
192-
Root,
193-
}
194-
195-
impl rstd::convert::TryFrom<u8> for ParachainDispatchOrigin {
196-
type Error = ();
197-
fn try_from(x: u8) -> core::result::Result<ParachainDispatchOrigin, ()> {
198-
const SIGNED: u8 = ParachainDispatchOrigin::Signed as u8;
199-
const PARACHAIN: u8 = ParachainDispatchOrigin::Parachain as u8;
200-
Ok(match x {
201-
SIGNED => ParachainDispatchOrigin::Signed,
202-
PARACHAIN => ParachainDispatchOrigin::Parachain,
203-
_ => return Err(()),
204-
})
205-
}
206-
}
207-
208-
/// A message from a parachain to its Relay Chain.
209-
#[derive(Clone, PartialEq, Eq, Encode, Decode, sp_runtime_interface::pass_by::PassByCodec)]
210-
#[cfg_attr(feature = "std", derive(Debug))]
211-
pub struct UpwardMessage {
212-
/// The origin for the message to be sent from.
213-
pub origin: ParachainDispatchOrigin,
214-
/// The message data.
215-
pub data: Vec<u8>,
216-
}
217-
218-
/// An incoming message.
219-
#[derive(PartialEq, Eq, Decode)]
220-
#[cfg_attr(feature = "std", derive(Debug, Encode))]
221-
pub struct IncomingMessage {
222-
/// The source parachain.
223-
pub source: Id,
224-
/// The data of the message.
225-
pub data: Vec<u8>,
226-
}

primitives/Cargo.toml

+3-4
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ primitives = { package = "sp-core", git = "https://github.com/paritytech/substra
1111
inherents = { package = "sp-inherents", git = "https://github.com/paritytech/substrate", branch = "cumulus-branch", default-features = false }
1212
application-crypto = { package = "sp-application-crypto", git = "https://github.com/paritytech/substrate", branch = "cumulus-branch", default-features = false }
1313
sp-api = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch", default-features = false }
14+
sp-runtime-interface = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch", default-features = false }
1415
sp-version = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch", default-features = false }
1516
rstd = { package = "sp-std", git = "https://github.com/paritytech/substrate", branch = "cumulus-branch", default-features = false }
1617
runtime_primitives = { package = "sp-runtime", git = "https://github.com/paritytech/substrate", branch = "cumulus-branch", default-features = false }
17-
polkadot-parachain = { path = "../parachain", default-features = false }
1818
trie = { package = "sp-trie", git = "https://github.com/paritytech/substrate", branch = "cumulus-branch", default-features = false }
1919
bitvec = { version = "0.15.2", default-features = false, features = ["alloc"] }
20-
babe = { package = "pallet-babe", git = "https://github.com/paritytech/substrate", branch = "cumulus-branch", default-features = false }
20+
derive_more = { version = "0.99.2" }
2121

2222
[dev-dependencies]
2323
sp-serializer = { git = "https://github.com/paritytech/substrate", branch = "cumulus-branch" }
@@ -31,11 +31,10 @@ std = [
3131
"inherents/std",
3232
"trie/std",
3333
"sp-api/std",
34+
"sp-runtime-interface/std",
3435
"rstd/std",
3536
"sp-version/std",
3637
"runtime_primitives/std",
3738
"serde",
38-
"polkadot-parachain/std",
3939
"bitvec/std",
40-
"babe/std"
4140
]

0 commit comments

Comments
 (0)