diff --git a/bridges/bin/millau/runtime/src/lib.rs b/bridges/bin/millau/runtime/src/lib.rs index 656b0c292b..b9d0f638a8 100644 --- a/bridges/bin/millau/runtime/src/lib.rs +++ b/bridges/bin/millau/runtime/src/lib.rs @@ -32,9 +32,7 @@ use pallet_grandpa::{fg_primitives, AuthorityId as GrandpaId, AuthorityList as G use sp_api::impl_runtime_apis; use sp_consensus_aura::sr25519::AuthorityId as AuraId; use sp_core::{crypto::KeyTypeId, OpaqueMetadata}; -use sp_runtime::traits::{ - BlakeTwo256, Block as BlockT, IdentifyAccount, IdentityLookup, NumberFor, OpaqueKeys, Saturating, Verify, -}; +use sp_runtime::traits::{Block as BlockT, IdentifyAccount, IdentityLookup, NumberFor, OpaqueKeys, Saturating, Verify}; use sp_runtime::{ create_runtime_str, generic, impl_opaque_keys, transaction_validity::{TransactionSource, TransactionValidity}, @@ -84,6 +82,9 @@ pub type Index = u32; /// A hash of some data used by the chain. pub type Hash = bp_millau::Hash; +/// Hashing algorithm used by the chain. +pub type Hashing = bp_millau::Hasher; + /// Digest item type. pub type DigestItem = generic::DigestItem; @@ -97,7 +98,7 @@ pub mod opaque { pub use sp_runtime::OpaqueExtrinsic as UncheckedExtrinsic; /// Opaque block header type. - pub type Header = generic::Header; + pub type Header = generic::Header; /// Opaque block type. pub type Block = generic::Block; /// Opaque block identifier type. @@ -172,9 +173,9 @@ impl frame_system::Trait for Runtime { /// The type for hashing blocks and tries. type Hash = Hash; /// The hashing algorithm used. - type Hashing = BlakeTwo256; + type Hashing = Hashing; /// The header type. - type Header = generic::Header; + type Header = generic::Header; /// The ubiquitous event type. type Event = Event; /// The ubiquitous origin type. @@ -309,10 +310,7 @@ impl pallet_session::Trait for Runtime { } impl pallet_substrate_bridge::Trait for Runtime { - type BridgedHeader = bp_rialto::Header; - type BridgedBlockNumber = bp_rialto::BlockNumber; - type BridgedBlockHash = bp_rialto::Hash; - type BridgedBlockHasher = bp_rialto::Hasher; + type BridgedChain = bp_rialto::Rialto; } impl pallet_shift_session_manager::Trait for Runtime {} @@ -341,7 +339,7 @@ construct_runtime!( /// The address format for describing accounts. pub type Address = AccountId; /// Block header type as expected by this runtime. -pub type Header = generic::Header; +pub type Header = generic::Header; /// Block type as expected by this runtime. pub type Block = generic::Block; /// A Block signed with a Justification diff --git a/bridges/bin/rialto/runtime/src/lib.rs b/bridges/bin/rialto/runtime/src/lib.rs index 5ee7ff9246..6259e99a02 100644 --- a/bridges/bin/rialto/runtime/src/lib.rs +++ b/bridges/bin/rialto/runtime/src/lib.rs @@ -39,9 +39,7 @@ use pallet_grandpa::{fg_primitives, AuthorityId as GrandpaId, AuthorityList as G use sp_api::impl_runtime_apis; use sp_consensus_aura::sr25519::AuthorityId as AuraId; use sp_core::{crypto::KeyTypeId, OpaqueMetadata}; -use sp_runtime::traits::{ - BlakeTwo256, Block as BlockT, IdentifyAccount, IdentityLookup, NumberFor, OpaqueKeys, Saturating, Verify, -}; +use sp_runtime::traits::{Block as BlockT, IdentifyAccount, IdentityLookup, NumberFor, OpaqueKeys, Saturating, Verify}; use sp_runtime::{ create_runtime_str, generic, impl_opaque_keys, transaction_validity::{TransactionSource, TransactionValidity}, @@ -92,6 +90,9 @@ pub type Index = u32; /// A hash of some data used by the chain. pub type Hash = bp_rialto::Hash; +/// Hashing algorithm used by the chain. +pub type Hashing = bp_rialto::Hasher; + /// Digest item type. pub type DigestItem = generic::DigestItem; @@ -105,7 +106,7 @@ pub mod opaque { pub use sp_runtime::OpaqueExtrinsic as UncheckedExtrinsic; /// Opaque block header type. - pub type Header = generic::Header; + pub type Header = generic::Header; /// Opaque block type. pub type Block = generic::Block; /// Opaque block identifier type. @@ -180,9 +181,9 @@ impl frame_system::Trait for Runtime { /// The type for hashing blocks and tries. type Hash = Hash; /// The hashing algorithm used. - type Hashing = BlakeTwo256; + type Hashing = Hashing; /// The header type. - type Header = generic::Header; + type Header = generic::Header; /// The ubiquitous event type. type Event = Event; /// The ubiquitous origin type. @@ -415,10 +416,7 @@ impl pallet_session::Trait for Runtime { } impl pallet_substrate_bridge::Trait for Runtime { - type BridgedHeader = bp_millau::Header; - type BridgedBlockNumber = bp_millau::BlockNumber; - type BridgedBlockHash = bp_millau::Hash; - type BridgedBlockHasher = bp_millau::Hasher; + type BridgedChain = bp_millau::Millau; } impl pallet_shift_session_manager::Trait for Runtime {} @@ -451,7 +449,7 @@ construct_runtime!( /// The address format for describing accounts. pub type Address = AccountId; /// Block header type as expected by this runtime. -pub type Header = generic::Header; +pub type Header = generic::Header; /// Block type as expected by this runtime. pub type Block = generic::Block; /// A Block signed with a Justification diff --git a/bridges/modules/substrate/Cargo.toml b/bridges/modules/substrate/Cargo.toml index 3b474e643f..82740fc1b4 100644 --- a/bridges/modules/substrate/Cargo.toml +++ b/bridges/modules/substrate/Cargo.toml @@ -11,9 +11,12 @@ license = "GPL-3.0-or-later WITH Classpath-exception-2.0" codec = { package = "parity-scale-codec", version = "1.3.1", default-features = false } finality-grandpa = { version = "0.12.3", default-features = false } hash-db = { version = "0.15.2", default-features = false } -num-traits = { version = "0.2", default-features = false } serde = { version = "1.0", optional = true } +# Bridge Dependencies + +bp-runtime = { path = "../../primitives/runtime", default-features = false } + # Substrate Dependencies frame-support = { version = "2.0", default-features = false } @@ -32,12 +35,12 @@ sp-state-machine = "0.8" [features] default = ["std"] std = [ + "bp-runtime/std", "codec/std", "finality-grandpa/std", "frame-support/std", "frame-system/std", "hash-db/std", - "num-traits/std", "serde", "sp-finality-grandpa/std", "sp-runtime/std", diff --git a/bridges/modules/substrate/src/lib.rs b/bridges/modules/substrate/src/lib.rs index 4b55bd41d7..25276ace09 100644 --- a/bridges/modules/substrate/src/lib.rs +++ b/bridges/modules/substrate/src/lib.rs @@ -32,18 +32,11 @@ #![allow(clippy::large_enum_variant)] use crate::storage::{AuthoritySet, ImportedHeader, ScheduledChange}; -use codec::{Codec, EncodeLike}; -use frame_support::{ - decl_error, decl_module, decl_storage, - dispatch::{DispatchResult, Parameter}, -}; +use bp_runtime::{BlockNumberOf, Chain, HashOf, HeaderOf}; +use frame_support::{decl_error, decl_module, decl_storage, dispatch::DispatchResult}; use frame_system::ensure_signed; -use num_traits::AsPrimitive; -use sp_runtime::traits::{ - AtLeast32BitUnsigned, Hash as HashT, Header as HeaderT, MaybeDisplay, MaybeMallocSizeOf, MaybeSerializeDeserialize, - Member, SimpleBitOps, -}; -use sp_std::{fmt::Debug, marker::PhantomData, prelude::*, str::FromStr}; +use sp_runtime::traits::Header as HeaderT; +use sp_std::{marker::PhantomData, prelude::*}; mod justification; mod storage; @@ -54,84 +47,42 @@ mod verifier; mod mock; pub trait Trait: frame_system::Trait { - /// A type that fulfills the abstract idea of what a Substrate header is. - // See here for more info: - // https://crates.parity.io/sp_runtime/traits/trait.Header.html - type BridgedHeader: Parameter + HeaderT; - - /// A type that fulfills the abstract idea of what a Substrate block number is. - // Constraits come from the associated Number type of `sp_runtime::traits::Header` - // See here for more info: - // https://crates.parity.io/sp_runtime/traits/trait.Header.html#associatedtype.Number - // - // Note that the `AsPrimitive` trait is required by the Grandpa justification - // verifier, and is not usually part of a Substrate Header's Number type. - type BridgedBlockNumber: Parameter - + Member - + MaybeSerializeDeserialize - + Debug - + sp_std::hash::Hash - + Copy - + MaybeDisplay - + AtLeast32BitUnsigned - + Codec - + FromStr - + MaybeMallocSizeOf - + AsPrimitive; - - /// A type that fulfills the abstract idea of what a Substrate hash is. - // Constraits come from the associated Hash type of `sp_runtime::traits::Header` - // See here for more info: - // https://crates.parity.io/sp_runtime/traits/trait.Header.html#associatedtype.Hash - type BridgedBlockHash: Parameter - + Member - + MaybeSerializeDeserialize - + Debug - + sp_std::hash::Hash - + Ord - + Copy - + MaybeDisplay - + Default - + SimpleBitOps - + Codec - + AsRef<[u8]> - + AsMut<[u8]> - + MaybeMallocSizeOf - + EncodeLike; - - /// A type that fulfills the abstract idea of what a Substrate hasher (a type - /// that produces hashes) is. - // Constraits come from the associated Hashing type of `sp_runtime::traits::Header` - // See here for more info: - // https://crates.parity.io/sp_runtime/traits/trait.Header.html#associatedtype.Hashing - type BridgedBlockHasher: HashT; + /// Chain that we are bridging here. + type BridgedChain: Chain; } +/// Block number of the bridged chain. +pub(crate) type BridgedBlockNumber = BlockNumberOf<::BridgedChain>; +/// Block hash of the bridged chain. +pub(crate) type BridgedBlockHash = HashOf<::BridgedChain>; +/// Header of the bridged chain. +pub(crate) type BridgedHeader = HeaderOf<::BridgedChain>; + decl_storage! { trait Store for Module as SubstrateBridge { /// Hash of the header at the highest known height. - BestHeader: T::BridgedBlockHash; + BestHeader: BridgedBlockHash; /// Hash of the best finalized header. - BestFinalized: T::BridgedBlockHash; + BestFinalized: BridgedBlockHash; /// A header which enacts an authority set change and therefore /// requires a Grandpa justification. // Since we won't always have an authority set change scheduled we // won't always have a header which needs a justification. - RequiresJustification: Option; + RequiresJustification: Option>; /// Headers which have been imported into the pallet. - ImportedHeaders: map hasher(identity) T::BridgedBlockHash => Option>; + ImportedHeaders: map hasher(identity) BridgedBlockHash => Option>>; /// The current Grandpa Authority set. CurrentAuthoritySet: AuthoritySet; /// The next scheduled authority set change. // Grandpa doesn't require there to always be a pending change. In fact, most of the time // there will be no pending change available. - NextScheduledChange: Option>; + NextScheduledChange: Option>>; } add_extra_genesis { - config(initial_header): Option; + config(initial_header): Option>; config(initial_authority_list): sp_finality_grandpa::AuthorityList; config(initial_set_id): sp_finality_grandpa::SetId; - config(first_scheduled_change): Option>; + config(first_scheduled_change): Option>>; build(|config| { assert!( !config.initial_authority_list.is_empty(), @@ -187,7 +138,7 @@ decl_module! { #[weight = 0] pub fn import_signed_header( origin, - header: T::BridgedHeader, + header: BridgedHeader, ) -> DispatchResult { let _ = ensure_signed(origin)?; frame_support::debug::trace!(target: "sub-bridge", "Got header {:?}", header); @@ -212,7 +163,7 @@ decl_module! { #[weight = 0] pub fn finalize_header( origin, - hash: T::BridgedBlockHash, + hash: BridgedBlockHash, finality_proof: Vec, ) -> DispatchResult { let _ = ensure_signed(origin)?; @@ -235,7 +186,7 @@ impl Module { /// Get the highest header that the pallet knows of. // In a future where we support forks this could be a Vec of headers // since we may have multiple headers at the same height. - pub fn best_header() -> T::BridgedHeader { + pub fn best_header() -> BridgedHeader { PalletStorage::::new().best_header().header } @@ -244,12 +195,12 @@ impl Module { /// Since this has been finalized correctly a user of the bridge /// pallet should be confident that any transactions that were /// included in this or any previous header will not be reverted. - pub fn best_finalized() -> T::BridgedHeader { + pub fn best_finalized() -> BridgedHeader { PalletStorage::::new().best_finalized_header().header } /// Check if a particular header is known to the bridge pallet. - pub fn is_known_header(hash: T::BridgedBlockHash) -> bool { + pub fn is_known_header(hash: BridgedBlockHash) -> bool { PalletStorage::::new().header_exists(hash) } @@ -259,7 +210,7 @@ impl Module { // One thing worth noting here is that this approach won't work well // once we track forks since there could be an older header on a // different fork which isn't an ancestor of our best finalized header. - pub fn is_finalized_header(hash: T::BridgedBlockHash) -> bool { + pub fn is_finalized_header(hash: BridgedBlockHash) -> bool { let storage = PalletStorage::::new(); if let Some(header) = storage.header_by_hash(hash) { header.number() <= storage.best_finalized_header().number() @@ -272,7 +223,7 @@ impl Module { /// and still needs a finality proof. /// /// Will return None if there are no headers which are missing finality proofs. - pub fn requires_justification() -> Option { + pub fn requires_justification() -> Option> { let storage = PalletStorage::::new(); let hash = storage.unfinalized_header()?; let imported_header = storage.header_by_hash(hash).expect( @@ -352,42 +303,42 @@ impl PalletStorage { } impl BridgeStorage for PalletStorage { - type Header = T::BridgedHeader; + type Header = BridgedHeader; - fn write_header(&mut self, header: &ImportedHeader) { + fn write_header(&mut self, header: &ImportedHeader>) { let hash = header.header.hash(); >::insert(hash, header); } - fn best_header(&self) -> ImportedHeader { + fn best_header(&self) -> ImportedHeader> { let hash = >::get(); self.header_by_hash(hash) .expect("A header must have been written at genesis, therefore this must always exist") } - fn update_best_header(&mut self, hash: T::BridgedBlockHash) { + fn update_best_header(&mut self, hash: BridgedBlockHash) { >::put(hash) } - fn best_finalized_header(&self) -> ImportedHeader { + fn best_finalized_header(&self) -> ImportedHeader> { let hash = >::get(); self.header_by_hash(hash) .expect("A finalized header was added at genesis, therefore this must always exist") } - fn update_best_finalized(&self, hash: T::BridgedBlockHash) { + fn update_best_finalized(&self, hash: BridgedBlockHash) { >::put(hash) } - fn header_exists(&self, hash: T::BridgedBlockHash) -> bool { + fn header_exists(&self, hash: BridgedBlockHash) -> bool { >::contains_key(hash) } - fn header_by_hash(&self, hash: T::BridgedBlockHash) -> Option> { + fn header_by_hash(&self, hash: BridgedBlockHash) -> Option>> { >::get(hash) } - fn unfinalized_header(&self) -> Option { + fn unfinalized_header(&self) -> Option> { >::get() } @@ -420,11 +371,11 @@ impl BridgeStorage for PalletStorage { } } - fn scheduled_set_change(&self) -> Option> { + fn scheduled_set_change(&self) -> Option>> { >::get() } - fn schedule_next_set_change(&self, next_change: ScheduledChange) { + fn schedule_next_set_change(&self, next_change: ScheduledChange>) { >::put(next_change) } } diff --git a/bridges/modules/substrate/src/mock.rs b/bridges/modules/substrate/src/mock.rs index b6cc8bd31c..f16ea476bb 100644 --- a/bridges/modules/substrate/src/mock.rs +++ b/bridges/modules/substrate/src/mock.rs @@ -21,6 +21,7 @@ #![cfg(test)] use crate::Trait; +use bp_runtime::Chain; use frame_support::{impl_outer_origin, parameter_types, weights::Weight}; use sp_runtime::{ testing::{Header, H256}, @@ -73,10 +74,17 @@ impl frame_system::Trait for TestRuntime { } impl Trait for TestRuntime { - type BridgedHeader = ::Header; - type BridgedBlockNumber = ::BlockNumber; - type BridgedBlockHash = ::Hash; - type BridgedBlockHasher = ::Hashing; + type BridgedChain = TestBridgedChain; +} + +#[derive(Debug)] +pub struct TestBridgedChain; + +impl Chain for TestBridgedChain { + type BlockNumber = ::BlockNumber; + type Hash = ::Hash; + type Hasher = ::Hashing; + type Header = ::Header; } pub fn run_test(test: impl FnOnce() -> T) -> T { @@ -85,13 +93,14 @@ pub fn run_test(test: impl FnOnce() -> T) -> T { pub mod helpers { use super::*; + use crate::{BridgedBlockHash, BridgedBlockNumber, BridgedHeader}; use finality_grandpa::voter_set::VoterSet; use sp_finality_grandpa::{AuthorityId, AuthorityList}; use sp_keyring::Ed25519Keyring; - pub type TestHeader = ::BridgedHeader; - pub type TestNumber = ::BridgedBlockNumber; - pub type TestHash = ::BridgedBlockHash; + pub type TestHeader = BridgedHeader; + pub type TestNumber = BridgedBlockNumber; + pub type TestHash = BridgedBlockHash; pub type HeaderId = (TestHash, TestNumber); pub fn test_header(num: TestNumber) -> TestHeader { diff --git a/bridges/primitives/millau/Cargo.toml b/bridges/primitives/millau/Cargo.toml index 2125fa8205..47b7474184 100644 --- a/bridges/primitives/millau/Cargo.toml +++ b/bridges/primitives/millau/Cargo.toml @@ -8,7 +8,13 @@ license = "GPL-3.0-or-later WITH Classpath-exception-2.0" [dependencies] +# Bridge Dependencies + +bp-runtime = { path = "../runtime", default-features = false } + # Substrate Based Dependencies + +frame-support = { version = "2.0", default-features = false } sp-api = { version = "2.0", default-features = false } sp-core = { version = "2.0", default-features = false } sp-runtime = { version = "2.0", default-features = false } @@ -17,6 +23,8 @@ sp-std = { version = "2.0", default-features = false } [features] default = ["std"] std = [ + "bp-runtime/std", + "frame-support/std", "sp-api/std", "sp-core/std", "sp-runtime/std", diff --git a/bridges/primitives/millau/src/lib.rs b/bridges/primitives/millau/src/lib.rs index dc233caab4..99bb0207f5 100644 --- a/bridges/primitives/millau/src/lib.rs +++ b/bridges/primitives/millau/src/lib.rs @@ -20,6 +20,8 @@ // Runtime-generated DecodeLimit::decode_all_With_depth_limit #![allow(clippy::unnecessary_mut_passed)] +use bp_runtime::Chain; +use frame_support::RuntimeDebug; use sp_core::Hasher as HasherT; use sp_runtime::traits::BlakeTwo256; use sp_std::prelude::*; @@ -36,6 +38,17 @@ pub type Hasher = BlakeTwo256; /// The header type used by Millau. pub type Header = sp_runtime::generic::Header; +/// Millau chain. +#[derive(RuntimeDebug)] +pub struct Millau; + +impl Chain for Millau { + type BlockNumber = BlockNumber; + type Hash = Hash; + type Hasher = Hasher; + type Header = Header; +} + sp_api::decl_runtime_apis! { /// API for querying information about Millau headers from the Bridge Pallet instance. /// diff --git a/bridges/primitives/rialto/Cargo.toml b/bridges/primitives/rialto/Cargo.toml index 80616f3863..b038069051 100644 --- a/bridges/primitives/rialto/Cargo.toml +++ b/bridges/primitives/rialto/Cargo.toml @@ -8,15 +8,23 @@ license = "GPL-3.0-or-later WITH Classpath-exception-2.0" [dependencies] +# Bridge Dependencies + +bp-runtime = { path = "../runtime", default-features = false } + # Substrate Based Dependencies -sp-api = { version = "2.0.0", default-features = false } -sp-core = { version = "2.0.0", default-features = false } -sp-runtime = { version = "2.0.0", default-features = false } -sp-std = { version = "2.0.0", default-features = false } + +frame-support = { version = "2.0", default-features = false } +sp-api = { version = "2.0", default-features = false } +sp-core = { version = "2.0", default-features = false } +sp-runtime = { version = "2.0", default-features = false } +sp-std = { version = "2.0", default-features = false } [features] default = ["std"] std = [ + "bp-runtime/std", + "frame-support/std", "sp-api/std", "sp-core/std", "sp-runtime/std", diff --git a/bridges/primitives/rialto/src/lib.rs b/bridges/primitives/rialto/src/lib.rs index 0a3fddbc0b..ea20d72ab0 100644 --- a/bridges/primitives/rialto/src/lib.rs +++ b/bridges/primitives/rialto/src/lib.rs @@ -20,6 +20,8 @@ // Runtime-generated DecodeLimit::decode_all_With_depth_limit #![allow(clippy::unnecessary_mut_passed)] +use bp_runtime::Chain; +use frame_support::RuntimeDebug; use sp_core::Hasher as HasherT; use sp_runtime::traits::BlakeTwo256; use sp_std::prelude::*; @@ -36,6 +38,17 @@ pub type Hasher = BlakeTwo256; /// The header type used by Rialto. pub type Header = sp_runtime::generic::Header; +/// Rialto chain. +#[derive(RuntimeDebug)] +pub struct Rialto; + +impl Chain for Rialto { + type BlockNumber = BlockNumber; + type Hash = Hash; + type Hasher = Hasher; + type Header = Header; +} + sp_api::decl_runtime_apis! { /// API for querying information about Rialto headers from the Bridge Pallet instance. /// diff --git a/bridges/primitives/runtime/Cargo.toml b/bridges/primitives/runtime/Cargo.toml index b182f4e4e4..b42fa4bf3e 100644 --- a/bridges/primitives/runtime/Cargo.toml +++ b/bridges/primitives/runtime/Cargo.toml @@ -8,14 +8,22 @@ license = "GPL-3.0-or-later WITH Classpath-exception-2.0" [dependencies] codec = { package = "parity-scale-codec", version = "1.3.1", default-features = false } +num-traits = { version = "0.2", default-features = false } # Substrate Dependencies +frame-support = { version = "2.0", default-features = false } sp-io = { version = "2.0", default-features = false } +sp-runtime = { version = "2.0", default-features = false } +sp-std = { version = "2.0", default-features = false } [features] default = ["std"] std = [ "codec/std", + "frame-support/std", + "num-traits/std", "sp-io/std", + "sp-runtime/std", + "sp-std/std", ] diff --git a/bridges/primitives/runtime/src/chain.rs b/bridges/primitives/runtime/src/chain.rs new file mode 100644 index 0000000000..096db8397b --- /dev/null +++ b/bridges/primitives/runtime/src/chain.rs @@ -0,0 +1,85 @@ +// Copyright 2019-2020 Parity Technologies (UK) Ltd. +// This file is part of Parity Bridges Common. + +// Parity Bridges Common is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity Bridges Common is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity Bridges Common. If not, see . + +use frame_support::Parameter; +use num_traits::AsPrimitive; +use sp_runtime::traits::{ + AtLeast32BitUnsigned, Hash as HashT, Header as HeaderT, MaybeDisplay, MaybeMallocSizeOf, MaybeSerializeDeserialize, + Member, SimpleBitOps, +}; +use sp_std::str::FromStr; + +/// Minimal Substrate-based chain representation that may be used from no_std environment. +pub trait Chain { + /// A type that fulfills the abstract idea of what a Substrate block number is. + // Constraits come from the associated Number type of `sp_runtime::traits::Header` + // See here for more info: + // https://crates.parity.io/sp_runtime/traits/trait.Header.html#associatedtype.Number + // + // Note that the `AsPrimitive` trait is required by the Grandpa justification + // verifier, and is not usually part of a Substrate Header's Number type. + type BlockNumber: Parameter + + Member + + MaybeSerializeDeserialize + + sp_std::hash::Hash + + Copy + + MaybeDisplay + + AtLeast32BitUnsigned + + FromStr + + MaybeMallocSizeOf + + AsPrimitive; + + /// A type that fulfills the abstract idea of what a Substrate hash is. + // Constraits come from the associated Hash type of `sp_runtime::traits::Header` + // See here for more info: + // https://crates.parity.io/sp_runtime/traits/trait.Header.html#associatedtype.Hash + type Hash: Parameter + + Member + + MaybeSerializeDeserialize + + sp_std::hash::Hash + + Ord + + Copy + + MaybeDisplay + + Default + + SimpleBitOps + + AsRef<[u8]> + + AsMut<[u8]> + + MaybeMallocSizeOf; + + /// A type that fulfills the abstract idea of what a Substrate hasher (a type + /// that produces hashes) is. + // Constraits come from the associated Hashing type of `sp_runtime::traits::Header` + // See here for more info: + // https://crates.parity.io/sp_runtime/traits/trait.Header.html#associatedtype.Hashing + type Hasher: HashT; + + /// A type that fulfills the abstract idea of what a Substrate header is. + // See here for more info: + // https://crates.parity.io/sp_runtime/traits/trait.Header.html + type Header: Parameter + HeaderT; +} + +/// Block number used by the chain. +pub type BlockNumberOf = ::BlockNumber; + +/// Hash type used by the chain. +pub type HashOf = ::Hash; + +/// Hasher type used by the chain. +pub type HasherOf = ::Header; + +/// Header type used by the chain. +pub type HeaderOf = ::Header; diff --git a/bridges/primitives/runtime/src/lib.rs b/bridges/primitives/runtime/src/lib.rs index 45097ea1b6..6a826da3af 100644 --- a/bridges/primitives/runtime/src/lib.rs +++ b/bridges/primitives/runtime/src/lib.rs @@ -21,6 +21,10 @@ use codec::{Decode, Encode}; use sp_io::hashing::blake2_256; +pub use chain::{BlockNumberOf, Chain, HashOf, HasherOf, HeaderOf}; + +mod chain; + /// Call-dispatch module prefix. pub const CALL_DISPATCH_MODULE_PREFIX: &[u8] = b"pallet-bridge/call-dispatch"; diff --git a/bridges/relays/millau-client/src/lib.rs b/bridges/relays/millau-client/src/lib.rs index a901be18ee..291954cbea 100644 --- a/bridges/relays/millau-client/src/lib.rs +++ b/bridges/relays/millau-client/src/lib.rs @@ -16,7 +16,7 @@ //! Types used to connect to the Millau-Substrate chain. -use relay_substrate_client::Chain; +use relay_substrate_client::{Chain, ChainBase}; use headers_relay::sync_types::SourceHeader; use sp_runtime::traits::Header as HeaderT; @@ -28,10 +28,14 @@ pub type HeaderId = relay_utils::HeaderId - + AsMut<[u8]> - + MaybeMallocSizeOf; - /// The block header. - type Header: Parameter + HeaderT; +pub trait Chain: ChainBase { /// The user account identifier type for the runtime. type AccountId: Parameter + Member + MaybeSerializeDeserialize + Debug + MaybeDisplay + Ord + Default; /// Account index (aka nonce) type. This stores the number of previous transactions associated @@ -96,15 +64,6 @@ pub trait TransactionSignScheme { ) -> Self::SignedTransaction; } -/// Header type used by the chain. -pub type HeaderOf = ::Header; - -/// Hash type used by the chain. -pub type HashOf = ::Hash; - -/// Block number used by the chain. -pub type BlockNumberOf = ::BlockNumber; - impl BlockWithJustification for SignedBlock { fn justification(&self) -> Option<&Justification> { self.justification.as_ref() diff --git a/bridges/relays/substrate-client/src/lib.rs b/bridges/relays/substrate-client/src/lib.rs index 7e44653db8..6c978f4652 100644 --- a/bridges/relays/substrate-client/src/lib.rs +++ b/bridges/relays/substrate-client/src/lib.rs @@ -25,9 +25,10 @@ mod rpc; pub mod headers_source; -pub use crate::chain::{BlockNumberOf, BlockWithJustification, Chain, HashOf, HeaderOf, TransactionSignScheme}; +pub use crate::chain::{BlockWithJustification, Chain, TransactionSignScheme}; pub use crate::client::{Client, OpaqueGrandpaAuthoritiesSet}; pub use crate::error::{Error, Result}; +pub use bp_runtime::{BlockNumberOf, Chain as ChainBase, HashOf, HeaderOf}; /// Substrate connection params. #[derive(Debug, Clone)]