Skip to content

Commit

Permalink
Polkadot integration (paritytech#542)
Browse files Browse the repository at this point in the history
* kusama primitives + client

* polkadot primitives + client

* lost Chain definitions

* fix compilation and fmt

* Update primitives/runtime/src/lib.rs

Co-authored-by: Hernando Castano <[email protected]>

Co-authored-by: Hernando Castano <[email protected]>
  • Loading branch information
2 people authored and serban300 committed Apr 8, 2024
1 parent 757e397 commit ec6fd25
Show file tree
Hide file tree
Showing 12 changed files with 526 additions and 2 deletions.
34 changes: 34 additions & 0 deletions bridges/primitives/kusama/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[package]
name = "bp-kusama"
description = "Primitives of Kusama runtime."
version = "0.1.0"
authors = ["Parity Technologies <[email protected]>"]
edition = "2018"
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"

[dependencies]

# Bridge Dependencies

bp-message-lane = { path = "../message-lane", default-features = false }
bp-runtime = { path = "../runtime", default-features = false }

# Substrate Based Dependencies

frame-support = { git = "https://github.com/paritytech/substrate.git", branch = "master" , default-features = false }
sp-api = { git = "https://github.com/paritytech/substrate.git", branch = "master" , default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate.git", branch = "master", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate.git", branch = "master", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate.git", branch = "master", default-features = false }

[features]
default = ["std"]
std = [
"bp-message-lane/std",
"bp-runtime/std",
"frame-support/std",
"sp-api/std",
"sp-core/std",
"sp-runtime/std",
"sp-std/std",
]
150 changes: 150 additions & 0 deletions bridges/primitives/kusama/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// 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 <http://www.gnu.org/licenses/>.

#![cfg_attr(not(feature = "std"), no_std)]
// RuntimeApi generated functions
#![allow(clippy::too_many_arguments)]
// Runtime-generated DecodeLimit::decode_all_with_depth_limit
#![allow(clippy::unnecessary_mut_passed)]

use bp_message_lane::{LaneId, MessageNonce};
use bp_runtime::Chain;
use frame_support::{weights::Weight, RuntimeDebug};
use sp_core::Hasher as HasherT;
use sp_runtime::{
generic,
traits::{BlakeTwo256, IdentifyAccount, Verify},
MultiSignature, OpaqueExtrinsic as UncheckedExtrinsic,
};
use sp_std::prelude::*;

/// Block number type used in Kusama.
pub type BlockNumber = u32;

/// Hash type used in Kusama.
pub type Hash = <BlakeTwo256 as HasherT>::Out;

/// The type of an object that can produce hashes on Kusama.
pub type Hasher = BlakeTwo256;

/// The header type used by Kusama.
pub type Header = generic::Header<BlockNumber, Hasher>;

/// Signature type used by Kusama.
pub type Signature = MultiSignature;

/// Public key of account on Kusama chain.
pub type AccountPublic = <Signature as Verify>::Signer;

/// Id of account on Kusama chain.
pub type AccountId = <AccountPublic as IdentifyAccount>::AccountId;

/// Index of a transaction on the Kusama chain.
pub type Nonce = u32;

/// Block type of Kusama.
pub type Block = generic::Block<Header, UncheckedExtrinsic>;

/// Kusama block signed with a Justification.
pub type SignedBlock = generic::SignedBlock<Block>;

/// The balance of an account on Polkadot.
pub type Balance = u128;

/// Kusama chain.
#[derive(RuntimeDebug)]
pub struct Kusama;

impl Chain for Kusama {
type BlockNumber = BlockNumber;
type Hash = Hash;
type Hasher = Hasher;
type Header = Header;
}

/// Name of the `KusamaHeaderApi::best_blocks` runtime method.
pub const BEST_KUSAMA_BLOCKS_METHOD: &str = "KusamaHeaderApi_best_blocks";
/// Name of the `KusamaHeaderApi::finalized_block` runtime method.
pub const FINALIZED_KUSAMA_BLOCK_METHOD: &str = "KusamaHeaderApi_finalized_block";
/// Name of the `KusamaHeaderApi::is_known_block` runtime method.
pub const IS_KNOWN_KUSAMA_BLOCK_METHOD: &str = "KusamaHeaderApi_is_known_block";
/// Name of the `KusamaHeaderApi::incomplete_headers` runtime method.
pub const INCOMPLETE_KUSAMA_HEADERS_METHOD: &str = "KusamaHeaderApi_incomplete_headers";

/// Maximal weight of single Kusama extrinsic.
pub const MAXIMUM_EXTRINSIC_WEIGHT: Weight = 725_000_000_000;

// TODO: should be selected keeping in mind: finality delay on both chains + reward payout cost + messages throughput.
/// Maximal number of unconfirmed messages at inbound lane.
pub const MAX_UNCONFIRMED_MESSAGES_AT_INBOUND_LANE: MessageNonce = 8192;

sp_api::decl_runtime_apis! {
/// API for querying information about Kusama headers from the Bridge Pallet instance.
///
/// This API is implemented by runtimes that are bridging with Kusama chain, not the
/// Kusama runtime itself.
pub trait KusamaHeaderApi {
/// Returns number and hash of the best blocks known to the bridge module.
///
/// Will return multiple headers if there are many headers at the same "best" height.
///
/// The caller should only submit an `import_header` transaction that makes
/// (or leads to making) other header the best one.
fn best_blocks() -> Vec<(BlockNumber, Hash)>;
/// Returns number and hash of the best finalized block known to the bridge module.
fn finalized_block() -> (BlockNumber, Hash);
/// Returns numbers and hashes of headers that require finality proofs.
///
/// An empty response means that there are no headers which currently require a
/// finality proof.
fn incomplete_headers() -> Vec<(BlockNumber, Hash)>;
/// Returns true if the header is known to the runtime.
fn is_known_block(hash: Hash) -> bool;
/// Returns true if the header is considered finalized by the runtime.
fn is_finalized_block(hash: Hash) -> bool;
}

/// Outbound message lane API for messages that are sent to Kusama chain.
///
/// This API is implemented by runtimes that are sending messages to Kusama chain, not the
/// Kusama runtime itself.
pub trait ToKusamaOutboundLaneApi {
/// Returns dispatch weight of all messages in given inclusive range.
///
/// If some (or all) messages are missing from the storage, they'll also will
/// be missing from the resulting vector. The vector is ordered by the nonce.
fn messages_dispatch_weight(
lane: LaneId,
begin: MessageNonce,
end: MessageNonce,
) -> Vec<(MessageNonce, Weight)>;
/// Returns nonce of the latest message, received by bridged chain.
fn latest_received_nonce(lane: LaneId) -> MessageNonce;
/// Returns nonce of the latest message, generated by given lane.
fn latest_generated_nonce(lane: LaneId) -> MessageNonce;
}

/// Inbound message lane API for messages sent by Kusama chain.
///
/// This API is implemented by runtimes that are receiving messages from Kusama chain, not the
/// Kusama runtime itself.
pub trait FromKusamaInboundLaneApi {
/// Returns nonce of the latest message, received by given lane.
fn latest_received_nonce(lane: LaneId) -> MessageNonce;
/// Nonce of latest message that has been confirmed to the bridged chain.
fn latest_confirmed_nonce(lane: LaneId) -> MessageNonce;
}
}
34 changes: 34 additions & 0 deletions bridges/primitives/polkadot/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[package]
name = "bp-polkadot"
description = "Primitives of Polkadot runtime."
version = "0.1.0"
authors = ["Parity Technologies <[email protected]>"]
edition = "2018"
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"

[dependencies]

# Bridge Dependencies

bp-message-lane = { path = "../message-lane", default-features = false }
bp-runtime = { path = "../runtime", default-features = false }

# Substrate Based Dependencies

frame-support = { git = "https://github.com/paritytech/substrate.git", branch = "master" , default-features = false }
sp-api = { git = "https://github.com/paritytech/substrate.git", branch = "master" , default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate.git", branch = "master", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate.git", branch = "master", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate.git", branch = "master", default-features = false }

[features]
default = ["std"]
std = [
"bp-message-lane/std",
"bp-runtime/std",
"frame-support/std",
"sp-api/std",
"sp-core/std",
"sp-runtime/std",
"sp-std/std",
]
150 changes: 150 additions & 0 deletions bridges/primitives/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// 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 <http://www.gnu.org/licenses/>.

#![cfg_attr(not(feature = "std"), no_std)]
// RuntimeApi generated functions
#![allow(clippy::too_many_arguments)]
// Runtime-generated DecodeLimit::decode_all_with_depth_limit
#![allow(clippy::unnecessary_mut_passed)]

use bp_message_lane::{LaneId, MessageNonce};
use bp_runtime::Chain;
use frame_support::{weights::Weight, RuntimeDebug};
use sp_core::Hasher as HasherT;
use sp_runtime::{
generic,
traits::{BlakeTwo256, IdentifyAccount, Verify},
MultiSignature, OpaqueExtrinsic as UncheckedExtrinsic,
};
use sp_std::prelude::*;

/// Block number type used in Polkadot.
pub type BlockNumber = u32;

/// Hash type used in Polkadot.
pub type Hash = <BlakeTwo256 as HasherT>::Out;

/// The type of an object that can produce hashes on Polkadot.
pub type Hasher = BlakeTwo256;

/// The header type used by Polkadot.
pub type Header = generic::Header<BlockNumber, Hasher>;

/// Signature type used by Polkadot.
pub type Signature = MultiSignature;

/// Public key of account on Polkadot chain.
pub type AccountPublic = <Signature as Verify>::Signer;

/// Id of account on Polkadot chain.
pub type AccountId = <AccountPublic as IdentifyAccount>::AccountId;

/// Index of a transaction on the Polkadot chain.
pub type Nonce = u32;

/// Block type of Polkadot.
pub type Block = generic::Block<Header, UncheckedExtrinsic>;

/// Polkadot block signed with a Justification.
pub type SignedBlock = generic::SignedBlock<Block>;

/// The balance of an account on Polkadot.
pub type Balance = u128;

/// Polkadot chain.
#[derive(RuntimeDebug)]
pub struct Polkadot;

impl Chain for Polkadot {
type BlockNumber = BlockNumber;
type Hash = Hash;
type Hasher = Hasher;
type Header = Header;
}

/// Name of the `PolkadotHeaderApi::best_blocks` runtime method.
pub const BEST_POLKADOT_BLOCKS_METHOD: &str = "PolkadotHeaderApi_best_blocks";
/// Name of the `PolkadotHeaderApi::finalized_block` runtime method.
pub const FINALIZED_POLKADOT_BLOCK_METHOD: &str = "PolkadotHeaderApi_finalized_block";
/// Name of the `PolkadotHeaderApi::is_known_block` runtime method.
pub const IS_KNOWN_POLKADOT_BLOCK_METHOD: &str = "PolkadotHeaderApi_is_known_block";
/// Name of the `PolkadotHeaderApi::incomplete_headers` runtime method.
pub const INCOMPLETE_POLKADOT_HEADERS_METHOD: &str = "PolkadotHeaderApi_incomplete_headers";

/// Maximal weight of single Polkadot extrinsic.
pub const MAXIMUM_EXTRINSIC_WEIGHT: Weight = 725_000_000_000;

// TODO: should be selected keeping in mind: finality delay on both chains + reward payout cost + messages throughput.
/// Maximal number of unconfirmed messages at inbound lane.
pub const MAX_UNCONFIRMED_MESSAGES_AT_INBOUND_LANE: MessageNonce = 8192;

sp_api::decl_runtime_apis! {
/// API for querying information about Polkadot headers from the Bridge Pallet instance.
///
/// This API is implemented by runtimes that are bridging with Polkadot chain, not the
/// Polkadot runtime itself.
pub trait PolkadotHeaderApi {
/// Returns number and hash of the best blocks known to the bridge module.
///
/// Will return multiple headers if there are many headers at the same "best" height.
///
/// The caller should only submit an `import_header` transaction that makes
/// (or leads to making) other header the best one.
fn best_blocks() -> Vec<(BlockNumber, Hash)>;
/// Returns number and hash of the best finalized block known to the bridge module.
fn finalized_block() -> (BlockNumber, Hash);
/// Returns numbers and hashes of headers that require finality proofs.
///
/// An empty response means that there are no headers which currently require a
/// finality proof.
fn incomplete_headers() -> Vec<(BlockNumber, Hash)>;
/// Returns true if the header is known to the runtime.
fn is_known_block(hash: Hash) -> bool;
/// Returns true if the header is considered finalized by the runtime.
fn is_finalized_block(hash: Hash) -> bool;
}

/// Outbound message lane API for messages that are sent to Polkadot chain.
///
/// This API is implemented by runtimes that are sending messages to Polkadot chain, not the
/// Polkadot runtime itself.
pub trait ToPolkadotOutboundLaneApi {
/// Returns dispatch weight of all messages in given inclusive range.
///
/// If some (or all) messages are missing from the storage, they'll also will
/// be missing from the resulting vector. The vector is ordered by the nonce.
fn messages_dispatch_weight(
lane: LaneId,
begin: MessageNonce,
end: MessageNonce,
) -> Vec<(MessageNonce, Weight)>;
/// Returns nonce of the latest message, received by bridged chain.
fn latest_received_nonce(lane: LaneId) -> MessageNonce;
/// Returns nonce of the latest message, generated by given lane.
fn latest_generated_nonce(lane: LaneId) -> MessageNonce;
}

/// Inbound message lane API for messages sent by Polkadot chain.
///
/// This API is implemented by runtimes that are receiving messages from Polkadot chain, not the
/// Polkadot runtime itself.
pub trait FromPolkadotInboundLaneApi {
/// Returns nonce of the latest message, received by given lane.
fn latest_received_nonce(lane: LaneId) -> MessageNonce;
/// Nonce of latest message that has been confirmed to the bridged chain.
fn latest_confirmed_nonce(lane: LaneId) -> MessageNonce;
}
}
6 changes: 6 additions & 0 deletions bridges/primitives/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ pub const RIALTO_BRIDGE_INSTANCE: InstanceId = *b"rlto";
/// Bridge-with-Millau instance id.
pub const MILLAU_BRIDGE_INSTANCE: InstanceId = *b"mlau";

/// Bridge-with-Polkadot instance id.
pub const POLKADOT_BRIDGE_INSTANCE: InstanceId = *b"pdot";

/// Bridge-with-Kusama instance id.
pub const KUSAMA_BRIDGE_INSTANCE: InstanceId = *b"ksma";

/// Call-dispatch module prefix.
pub const CALL_DISPATCH_MODULE_PREFIX: &[u8] = b"pallet-bridge/call-dispatch";

Expand Down
Loading

0 comments on commit ec6fd25

Please sign in to comment.