Merge commit '392447f5c8f986ded2559a78457f4cd87942f393' into update-bridges-subtree-r/w

This commit is contained in:
antonio-dropulic
2021-12-01 09:46:14 +01:00
321 changed files with 28385 additions and 10466 deletions
@@ -7,15 +7,20 @@ edition = "2018"
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
[dependencies]
smallvec = "1.7"
# Bridge Dependencies
bp-messages = { path = "../messages", default-features = false }
bp-polkadot-core = { path = "../polkadot-core", default-features = false }
bp-runtime = { path = "../runtime", default-features = false }
# Substrate Based Dependencies
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-version = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
[features]
default = ["std"]
@@ -23,6 +28,8 @@ std = [
"bp-messages/std",
"bp-polkadot-core/std",
"bp-runtime/std",
"frame-support/std",
"sp-api/std",
"sp-std/std",
"sp-version/std",
]
@@ -21,13 +21,46 @@
#![allow(clippy::unnecessary_mut_passed)]
use bp_messages::{LaneId, MessageDetails, MessageNonce, UnrewardedRelayersState};
use frame_support::weights::{
WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial,
};
use sp_std::prelude::*;
use sp_version::RuntimeVersion;
pub use bp_polkadot_core::*;
/// Kusama Chain
pub type Kusama = PolkadotLike;
// NOTE: This needs to be kept up to date with the Kusama runtime found in the Polkadot repo.
pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: sp_version::create_runtime_str!("kusama"),
impl_name: sp_version::create_runtime_str!("parity-kusama"),
authoring_version: 2,
spec_version: 9100,
impl_version: 0,
apis: sp_version::create_apis_vec![[]],
transaction_version: 5,
};
// NOTE: This needs to be kept up to date with the Kusama runtime found in the Polkadot repo.
pub struct WeightToFee;
impl WeightToFeePolynomial for WeightToFee {
type Balance = Balance;
fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
const CENTS: Balance = 1_000_000_000_000 / 30_000;
// in Kusama, extrinsic base weight (smallest non-zero weight) is mapped to 1/10 CENT:
let p = CENTS;
let q = 10 * Balance::from(ExtrinsicBaseWeight::get());
smallvec::smallvec![WeightToFeeCoefficient {
degree: 1,
negative: false,
coeff_frac: Perbill::from_rational(p % q, q),
coeff_integer: p / q,
}]
}
}
// We use this to get the account on Kusama (target) which is derived from Polkadot's (source)
// account.
pub fn derive_account_from_polkadot_id(id: bp_runtime::SourceAccount<AccountId>) -> AccountId {
@@ -35,27 +68,53 @@ pub fn derive_account_from_polkadot_id(id: bp_runtime::SourceAccount<AccountId>)
AccountIdConverter::convert(encoded_id)
}
/// Per-byte fee for Kusama transactions.
pub const TRANSACTION_BYTE_FEE: Balance = 10 * 1_000_000_000_000 / 30_000 / 1_000;
/// Existential deposit on Kusama.
pub const EXISTENTIAL_DEPOSIT: Balance = 1_000_000_000_000 / 30_000;
/// The target length of a session (how often authorities change) on Kusama measured in of number of
/// blocks.
///
/// Note that since this is a target sessions may change before/after this time depending on network
/// conditions.
pub const SESSION_LENGTH: BlockNumber = time_units::HOURS;
/// Name of the With-Polkadot messages pallet instance in the Kusama runtime.
pub const WITH_POLKADOT_MESSAGES_PALLET_NAME: &str = "BridgePolkadotMessages";
/// Name of the DOT->KSM conversion rate stored in the Kusama runtime.
pub const POLKADOT_TO_KUSAMA_CONVERSION_RATE_PARAMETER_NAME: &str =
"PolkadotToKusamaConversionRate";
/// Name of the `KusamaFinalityApi::best_finalized` runtime method.
pub const BEST_FINALIZED_KUSAMA_HEADER_METHOD: &str = "KusamaFinalityApi_best_finalized";
/// Name of the `KusamaFinalityApi::is_known_header` runtime method.
pub const IS_KNOWN_KUSAMA_HEADER_METHOD: &str = "KusamaFinalityApi_is_known_header";
/// Name of the `ToKusamaOutboundLaneApi::estimate_message_delivery_and_dispatch_fee` runtime method.
/// Name of the `ToKusamaOutboundLaneApi::estimate_message_delivery_and_dispatch_fee` runtime
/// method.
pub const TO_KUSAMA_ESTIMATE_MESSAGE_FEE_METHOD: &str =
"ToKusamaOutboundLaneApi_estimate_message_delivery_and_dispatch_fee";
/// Name of the `ToKusamaOutboundLaneApi::message_details` runtime method.
pub const TO_KUSAMA_MESSAGE_DETAILS_METHOD: &str = "ToKusamaOutboundLaneApi_message_details";
/// Name of the `ToKusamaOutboundLaneApi::latest_generated_nonce` runtime method.
pub const TO_KUSAMA_LATEST_GENERATED_NONCE_METHOD: &str = "ToKusamaOutboundLaneApi_latest_generated_nonce";
pub const TO_KUSAMA_LATEST_GENERATED_NONCE_METHOD: &str =
"ToKusamaOutboundLaneApi_latest_generated_nonce";
/// Name of the `ToKusamaOutboundLaneApi::latest_received_nonce` runtime method.
pub const TO_KUSAMA_LATEST_RECEIVED_NONCE_METHOD: &str = "ToKusamaOutboundLaneApi_latest_received_nonce";
pub const TO_KUSAMA_LATEST_RECEIVED_NONCE_METHOD: &str =
"ToKusamaOutboundLaneApi_latest_received_nonce";
/// Name of the `FromKusamaInboundLaneApi::latest_received_nonce` runtime method.
pub const FROM_KUSAMA_LATEST_RECEIVED_NONCE_METHOD: &str = "FromKusamaInboundLaneApi_latest_received_nonce";
pub const FROM_KUSAMA_LATEST_RECEIVED_NONCE_METHOD: &str =
"FromKusamaInboundLaneApi_latest_received_nonce";
/// Name of the `FromKusamaInboundLaneApi::latest_onfirmed_nonce` runtime method.
pub const FROM_KUSAMA_LATEST_CONFIRMED_NONCE_METHOD: &str = "FromKusamaInboundLaneApi_latest_confirmed_nonce";
pub const FROM_KUSAMA_LATEST_CONFIRMED_NONCE_METHOD: &str =
"FromKusamaInboundLaneApi_latest_confirmed_nonce";
/// Name of the `FromKusamaInboundLaneApi::unrewarded_relayers_state` runtime method.
pub const FROM_KUSAMA_UNREWARDED_RELAYERS_STATE: &str = "FromKusamaInboundLaneApi_unrewarded_relayers_state";
pub const FROM_KUSAMA_UNREWARDED_RELAYERS_STATE: &str =
"FromKusamaInboundLaneApi_unrewarded_relayers_state";
sp_api::decl_runtime_apis! {
/// API for querying information about the finalized Kusama headers.
@@ -16,19 +16,20 @@ fixed-hash = { version = "0.7.0", default-features = false }
hash256-std-hasher = { version = "0.15.2", default-features = false }
impl-codec = { version = "0.5.1", default-features = false }
impl-serde = { version = "0.3.1", optional = true }
parity-util-mem = { version = "0.10.0", default-features = false, features = ["primitive-types"] }
serde = { version = "1.0.101", optional = true, features = ["derive"] }
parity-util-mem = { version = "0.10", default-features = false, features = ["primitive-types"] }
scale-info = { version = "1.0", default-features = false, features = ["derive"] }
serde = { version = "1.0", optional = true, features = ["derive"] }
# Substrate Based Dependencies
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-trie = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-trie = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
[features]
default = ["std"]
@@ -42,6 +43,7 @@ std = [
"impl-codec/std",
"impl-serde",
"parity-util-mem/std",
"scale-info/std",
"serde",
"sp-api/std",
"sp-core/std",
@@ -25,14 +25,14 @@ mod millau_hash;
use bp_messages::{LaneId, MessageDetails, MessageNonce, UnrewardedRelayersState};
use bp_runtime::Chain;
use frame_support::{
weights::{constants::WEIGHT_PER_SECOND, DispatchClass, Weight},
weights::{constants::WEIGHT_PER_SECOND, DispatchClass, IdentityFee, Weight},
Parameter, RuntimeDebug,
};
use frame_system::limits;
use scale_info::TypeInfo;
use sp_core::Hasher as HasherT;
use sp_runtime::traits::Convert;
use sp_runtime::{
traits::{IdentifyAccount, Verify},
traits::{Convert, IdentifyAccount, Verify},
MultiSignature, MultiSigner, Perbill,
};
use sp_std::prelude::*;
@@ -77,29 +77,32 @@ pub const MAX_UNCONFIRMED_MESSAGES_AT_INBOUND_LANE: MessageNonce = 1024;
/// Weight of single regular message delivery transaction on Millau chain.
///
/// This value is a result of `pallet_bridge_messages::Pallet::receive_messages_proof_weight()` call
/// for the case when single message of `pallet_bridge_messages::EXPECTED_DEFAULT_MESSAGE_LENGTH` bytes is delivered.
/// The message must have dispatch weight set to zero. The result then must be rounded up to account
/// possible future runtime upgrades.
/// for the case when single message of `pallet_bridge_messages::EXPECTED_DEFAULT_MESSAGE_LENGTH`
/// bytes is delivered. The message must have dispatch weight set to zero. The result then must be
/// rounded up to account possible future runtime upgrades.
pub const DEFAULT_MESSAGE_DELIVERY_TX_WEIGHT: Weight = 1_500_000_000;
/// Increase of delivery transaction weight on Millau chain with every additional message byte.
///
/// This value is a result of `pallet_bridge_messages::WeightInfoExt::storage_proof_size_overhead(1)` call. The
/// result then must be rounded up to account possible future runtime upgrades.
/// This value is a result of
/// `pallet_bridge_messages::WeightInfoExt::storage_proof_size_overhead(1)` call. The result then
/// must be rounded up to account possible future runtime upgrades.
pub const ADDITIONAL_MESSAGE_BYTE_DELIVERY_WEIGHT: Weight = 25_000;
/// Maximal weight of single message delivery confirmation transaction on Millau chain.
///
/// This value is a result of `pallet_bridge_messages::Pallet::receive_messages_delivery_proof` weight formula computation
/// for the case when single message is confirmed. The result then must be rounded up to account possible future
/// runtime upgrades.
/// This value is a result of `pallet_bridge_messages::Pallet::receive_messages_delivery_proof`
/// weight formula computation for the case when single message is confirmed. The result then must
/// be rounded up to account possible future runtime upgrades.
pub const MAX_SINGLE_MESSAGE_DELIVERY_CONFIRMATION_TX_WEIGHT: Weight = 2_000_000_000;
/// Weight of pay-dispatch-fee operation for inbound messages at Millau chain.
///
/// This value corresponds to the result of `pallet_bridge_messages::WeightInfoExt::pay_inbound_dispatch_fee_overhead()`
/// call for your chain. Don't put too much reserve there, because it is used to **decrease**
/// `DEFAULT_MESSAGE_DELIVERY_TX_WEIGHT` cost. So putting large reserve would make delivery transactions cheaper.
/// This value corresponds to the result of
/// `pallet_bridge_messages::WeightInfoExt::pay_inbound_dispatch_fee_overhead()` call for your
/// chain. Don't put too much reserve there, because it is used to **decrease**
/// `DEFAULT_MESSAGE_DELIVERY_TX_WEIGHT` cost. So putting large reserve would make delivery
/// transactions cheaper.
pub const PAY_INBOUND_DISPATCH_FEE_WEIGHT: Weight = 600_000_000;
/// The target length of a session (how often authorities change) on Millau measured in of number of
@@ -130,7 +133,7 @@ pub type BlockNumber = u64;
/// Hash type used in Millau.
pub type Hash = <BlakeTwoAndKeccak256 as HasherT>::Out;
/// The type of an object that can produce hashes on Millau.
/// Type of object that can produce hashes on Millau.
pub type Hasher = BlakeTwoAndKeccak256;
/// The header type used by Millau.
@@ -149,6 +152,12 @@ pub type AccountSigner = MultiSigner;
/// Balance of an account.
pub type Balance = u64;
/// Index of a transaction in the chain.
pub type Index = u32;
/// Weight-to-Fee type used by Millau.
pub type WeightToFee = IdentityFee<Balance>;
/// Millau chain.
#[derive(RuntimeDebug)]
pub struct Millau;
@@ -158,10 +167,15 @@ impl Chain for Millau {
type Hash = Hash;
type Hasher = Hasher;
type Header = Header;
type AccountId = AccountId;
type Balance = Balance;
type Index = Index;
type Signature = Signature;
}
/// Millau Hasher (Blake2-256 ++ Keccak-256) implementation.
#[derive(PartialEq, Eq, Clone, Copy, RuntimeDebug)]
#[derive(PartialEq, Eq, Clone, Copy, RuntimeDebug, TypeInfo)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct BlakeTwoAndKeccak256;
@@ -245,25 +259,36 @@ pub fn max_extrinsic_size() -> u32 {
*BlockLength::get().max.get(DispatchClass::Normal)
}
/// Name of the With-Rialto messages pallet instance in the Millau runtime.
pub const WITH_RIALTO_MESSAGES_PALLET_NAME: &str = "BridgeRialtoMessages";
/// Name of the With-Rialto token swap pallet instance in the Millau runtime.
pub const WITH_RIALTO_TOKEN_SWAP_PALLET_NAME: &str = "BridgeRialtoTokenSwap";
/// Name of the `MillauFinalityApi::best_finalized` runtime method.
pub const BEST_FINALIZED_MILLAU_HEADER_METHOD: &str = "MillauFinalityApi_best_finalized";
/// Name of the `ToMillauOutboundLaneApi::estimate_message_delivery_and_dispatch_fee` runtime method.
/// Name of the `ToMillauOutboundLaneApi::estimate_message_delivery_and_dispatch_fee` runtime
/// method.
pub const TO_MILLAU_ESTIMATE_MESSAGE_FEE_METHOD: &str =
"ToMillauOutboundLaneApi_estimate_message_delivery_and_dispatch_fee";
/// Name of the `ToMillauOutboundLaneApi::message_details` runtime method.
pub const TO_MILLAU_MESSAGE_DETAILS_METHOD: &str = "ToMillauOutboundLaneApi_message_details";
/// Name of the `ToMillauOutboundLaneApi::latest_received_nonce` runtime method.
pub const TO_MILLAU_LATEST_RECEIVED_NONCE_METHOD: &str = "ToMillauOutboundLaneApi_latest_received_nonce";
pub const TO_MILLAU_LATEST_RECEIVED_NONCE_METHOD: &str =
"ToMillauOutboundLaneApi_latest_received_nonce";
/// Name of the `ToMillauOutboundLaneApi::latest_generated_nonce` runtime method.
pub const TO_MILLAU_LATEST_GENERATED_NONCE_METHOD: &str = "ToMillauOutboundLaneApi_latest_generated_nonce";
pub const TO_MILLAU_LATEST_GENERATED_NONCE_METHOD: &str =
"ToMillauOutboundLaneApi_latest_generated_nonce";
/// Name of the `FromMillauInboundLaneApi::latest_received_nonce` runtime method.
pub const FROM_MILLAU_LATEST_RECEIVED_NONCE_METHOD: &str = "FromMillauInboundLaneApi_latest_received_nonce";
pub const FROM_MILLAU_LATEST_RECEIVED_NONCE_METHOD: &str =
"FromMillauInboundLaneApi_latest_received_nonce";
/// Name of the `FromMillauInboundLaneApi::latest_onfirmed_nonce` runtime method.
pub const FROM_MILLAU_LATEST_CONFIRMED_NONCE_METHOD: &str = "FromMillauInboundLaneApi_latest_confirmed_nonce";
pub const FROM_MILLAU_LATEST_CONFIRMED_NONCE_METHOD: &str =
"FromMillauInboundLaneApi_latest_confirmed_nonce";
/// Name of the `FromMillauInboundLaneApi::unrewarded_relayers_state` runtime method.
pub const FROM_MILLAU_UNREWARDED_RELAYERS_STATE: &str = "FromMillauInboundLaneApi_unrewarded_relayers_state";
pub const FROM_MILLAU_UNREWARDED_RELAYERS_STATE: &str =
"FromMillauInboundLaneApi_unrewarded_relayers_state";
sp_api::decl_runtime_apis! {
/// API for querying information about the finalized Millau headers.
@@ -287,7 +312,7 @@ sp_api::decl_runtime_apis! {
///
/// Returns `None` if message is too expensive to be sent to Millau from this chain.
///
/// Please keep in mind that this method returns lowest message fee required for message
/// Please keep in mind that this method returns the lowest message fee required for message
/// to be accepted to the lane. It may be good idea to pay a bit over this price to account
/// future exchange rate changes and guarantee that relayer would deliver your message
/// to the target chain.
@@ -318,7 +343,7 @@ sp_api::decl_runtime_apis! {
pub trait FromMillauInboundLaneApi {
/// 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.
/// Nonce of the latest message that has been confirmed to the bridged chain.
fn latest_confirmed_nonce(lane: LaneId) -> MessageNonce;
/// State of the unrewarded relayers set at given lane.
fn unrewarded_relayers_state(lane: LaneId) -> UnrewardedRelayersState;
@@ -15,6 +15,7 @@
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
use parity_util_mem::MallocSizeOf;
use scale_info::TypeInfo;
use sp_runtime::traits::CheckEqual;
// `sp_core::H512` can't be used, because it doesn't implement `CheckEqual`, which is required
@@ -22,7 +23,7 @@ use sp_runtime::traits::CheckEqual;
fixed_hash::construct_fixed_hash! {
/// Hash type used in Millau chain.
#[derive(MallocSizeOf)]
#[derive(MallocSizeOf, TypeInfo)]
pub struct MillauHash(64);
}
@@ -7,16 +7,20 @@ edition = "2018"
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
[dependencies]
smallvec = "1.7"
# Bridge Dependencies
bp-messages = { path = "../messages", default-features = false }
bp-polkadot-core = { path = "../polkadot-core", default-features = false }
bp-runtime = { path = "../runtime", default-features = false }
# Substrate Based Dependencies
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-version = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
[features]
default = ["std"]
@@ -24,6 +28,8 @@ std = [
"bp-messages/std",
"bp-polkadot-core/std",
"bp-runtime/std",
"frame-support/std",
"sp-api/std",
"sp-std/std",
"sp-version/std",
]
@@ -21,13 +21,46 @@
#![allow(clippy::unnecessary_mut_passed)]
use bp_messages::{LaneId, MessageDetails, MessageNonce, UnrewardedRelayersState};
use frame_support::weights::{
WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial,
};
use sp_std::prelude::*;
use sp_version::RuntimeVersion;
pub use bp_polkadot_core::*;
/// Polkadot Chain
pub type Polkadot = PolkadotLike;
// NOTE: This needs to be kept up to date with the Polkadot runtime found in the Polkadot repo.
pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: sp_version::create_runtime_str!("polkadot"),
impl_name: sp_version::create_runtime_str!("parity-polkadot"),
authoring_version: 0,
spec_version: 9100,
impl_version: 0,
apis: sp_version::create_apis_vec![[]],
transaction_version: 7,
};
// NOTE: This needs to be kept up to date with the Polkadot runtime found in the Polkadot repo.
pub struct WeightToFee;
impl WeightToFeePolynomial for WeightToFee {
type Balance = Balance;
fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
const CENTS: Balance = 10_000_000_000 / 100;
// in Polkadot, extrinsic base weight (smallest non-zero weight) is mapped to 1/10 CENT:
let p = CENTS;
let q = 10 * Balance::from(ExtrinsicBaseWeight::get());
smallvec::smallvec![WeightToFeeCoefficient {
degree: 1,
negative: false,
coeff_frac: Perbill::from_rational(p % q, q),
coeff_integer: p / q,
}]
}
}
// We use this to get the account on Polkadot (target) which is derived from Kusama's (source)
// account.
pub fn derive_account_from_kusama_id(id: bp_runtime::SourceAccount<AccountId>) -> AccountId {
@@ -35,27 +68,53 @@ pub fn derive_account_from_kusama_id(id: bp_runtime::SourceAccount<AccountId>) -
AccountIdConverter::convert(encoded_id)
}
/// Per-byte fee for Polkadot transactions.
pub const TRANSACTION_BYTE_FEE: Balance = 10 * 10_000_000_000 / 100 / 1_000;
/// Existential deposit on Polkadot.
pub const EXISTENTIAL_DEPOSIT: Balance = 10_000_000_000;
/// The target length of a session (how often authorities change) on Polkadot measured in of number
/// of blocks.
///
/// Note that since this is a target sessions may change before/after this time depending on network
/// conditions.
pub const SESSION_LENGTH: BlockNumber = 4 * time_units::HOURS;
/// Name of the With-Kusama messages pallet instance in the Polkadot runtime.
pub const WITH_KUSAMA_MESSAGES_PALLET_NAME: &str = "BridgeKusamaMessages";
/// Name of the KSM->DOT conversion rate stored in the Polkadot runtime.
pub const KUSAMA_TO_POLKADOT_CONVERSION_RATE_PARAMETER_NAME: &str =
"KusamaToPolkadotConversionRate";
/// Name of the `PolkadotFinalityApi::best_finalized` runtime method.
pub const BEST_FINALIZED_POLKADOT_HEADER_METHOD: &str = "PolkadotFinalityApi_best_finalized";
/// Name of the `PolkadotFinalityApi::is_known_header` runtime method.
pub const IS_KNOWN_POLKADOT_HEADER_METHOD: &str = "PolkadotFinalityApi_is_known_header";
/// Name of the `ToPolkadotOutboundLaneApi::estimate_message_delivery_and_dispatch_fee` runtime method.
/// Name of the `ToPolkadotOutboundLaneApi::estimate_message_delivery_and_dispatch_fee` runtime
/// method.
pub const TO_POLKADOT_ESTIMATE_MESSAGE_FEE_METHOD: &str =
"ToPolkadotOutboundLaneApi_estimate_message_delivery_and_dispatch_fee";
/// Name of the `ToPolkadotOutboundLaneApi::message_details` runtime method.
pub const TO_POLKADOT_MESSAGE_DETAILS_METHOD: &str = "ToPolkadotOutboundLaneApi_message_details";
/// Name of the `ToPolkadotOutboundLaneApi::latest_generated_nonce` runtime method.
pub const TO_POLKADOT_LATEST_GENERATED_NONCE_METHOD: &str = "ToPolkadotOutboundLaneApi_latest_generated_nonce";
pub const TO_POLKADOT_LATEST_GENERATED_NONCE_METHOD: &str =
"ToPolkadotOutboundLaneApi_latest_generated_nonce";
/// Name of the `ToPolkadotOutboundLaneApi::latest_received_nonce` runtime method.
pub const TO_POLKADOT_LATEST_RECEIVED_NONCE_METHOD: &str = "ToPolkadotOutboundLaneApi_latest_received_nonce";
pub const TO_POLKADOT_LATEST_RECEIVED_NONCE_METHOD: &str =
"ToPolkadotOutboundLaneApi_latest_received_nonce";
/// Name of the `FromPolkadotInboundLaneApi::latest_received_nonce` runtime method.
pub const FROM_POLKADOT_LATEST_RECEIVED_NONCE_METHOD: &str = "FromPolkadotInboundLaneApi_latest_received_nonce";
pub const FROM_POLKADOT_LATEST_RECEIVED_NONCE_METHOD: &str =
"FromPolkadotInboundLaneApi_latest_received_nonce";
/// Name of the `FromPolkadotInboundLaneApi::latest_onfirmed_nonce` runtime method.
pub const FROM_POLKADOT_LATEST_CONFIRMED_NONCE_METHOD: &str = "FromPolkadotInboundLaneApi_latest_confirmed_nonce";
pub const FROM_POLKADOT_LATEST_CONFIRMED_NONCE_METHOD: &str =
"FromPolkadotInboundLaneApi_latest_confirmed_nonce";
/// Name of the `FromPolkadotInboundLaneApi::unrewarded_relayers_state` runtime method.
pub const FROM_POLKADOT_UNREWARDED_RELAYERS_STATE: &str = "FromPolkadotInboundLaneApi_unrewarded_relayers_state";
pub const FROM_POLKADOT_UNREWARDED_RELAYERS_STATE: &str =
"FromPolkadotInboundLaneApi_unrewarded_relayers_state";
sp_api::decl_runtime_apis! {
/// API for querying information about the finalized Polkadot headers.
@@ -0,0 +1,36 @@
[package]
name = "bp-rialto-parachain"
description = "Primitives of Rialto parachain runtime."
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
[dependencies]
# Bridge Dependencies
bp-messages = { path = "../messages", default-features = false }
bp-runtime = { path = "../runtime", default-features = false }
# Substrate Based Dependencies
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
[features]
default = ["std"]
std = [
"bp-messages/std",
"bp-runtime/std",
"frame-support/std",
"frame-system/std",
"sp-api/std",
"sp-core/std",
"sp-runtime/std",
"sp-std/std",
]
@@ -0,0 +1,128 @@
// Copyright 2019-2021 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_runtime::Chain;
use frame_support::{
weights::{constants::WEIGHT_PER_SECOND, DispatchClass, IdentityFee, Weight},
RuntimeDebug,
};
use frame_system::limits;
use sp_core::Hasher as HasherT;
use sp_runtime::{
traits::{BlakeTwo256, IdentifyAccount, Verify},
MultiSignature, MultiSigner, Perbill,
};
/// Maximal weight of single Rialto parachain block.
///
/// This represents two seconds of compute assuming a target block time of six seconds.
pub const MAXIMUM_BLOCK_WEIGHT: Weight = 2 * WEIGHT_PER_SECOND;
/// Represents the average portion of a block's weight that will be used by an
/// `on_initialize()` runtime call.
pub const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(10);
/// Represents the portion of a block that will be used by Normal extrinsics.
pub const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
/// Block number type used in Rialto.
pub type BlockNumber = u32;
/// Hash type used in Rialto.
pub type Hash = <BlakeTwo256 as HasherT>::Out;
/// The type of object that can produce hashes on Rialto.
pub type Hasher = BlakeTwo256;
/// The header type used by Rialto.
pub type Header = sp_runtime::generic::Header<BlockNumber, Hasher>;
/// Alias to 512-bit hash when used in the context of a transaction signature on the chain.
pub type Signature = MultiSignature;
/// Some way of identifying an account on the chain. We intentionally make it equivalent
/// to the public key of our transaction signing scheme.
pub type AccountId = <<Signature as Verify>::Signer as IdentifyAccount>::AccountId;
/// Public key of the chain account that may be used to verify signatures.
pub type AccountSigner = MultiSigner;
/// Balance of an account.
pub type Balance = u128;
/// An instant or duration in time.
pub type Moment = u64;
/// Index of a transaction in the parachain.
pub type Index = u32;
/// Weight-to-Fee type used by Rialto parachain.
pub type WeightToFee = IdentityFee<Balance>;
/// Rialto parachain.
#[derive(RuntimeDebug)]
pub struct RialtoParachain;
impl Chain for RialtoParachain {
type BlockNumber = BlockNumber;
type Hash = Hash;
type Hasher = Hasher;
type Header = Header;
type AccountId = AccountId;
type Balance = Balance;
type Index = Index;
type Signature = Signature;
}
frame_support::parameter_types! {
pub BlockLength: limits::BlockLength =
limits::BlockLength::max_with_normal_ratio(5 * 1024 * 1024, NORMAL_DISPATCH_RATIO);
pub BlockWeights: limits::BlockWeights = limits::BlockWeights::builder()
// Allowance for Normal class
.for_class(DispatchClass::Normal, |weights| {
weights.max_total = Some(NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT);
})
// Allowance for Operational class
.for_class(DispatchClass::Operational, |weights| {
weights.max_total = Some(MAXIMUM_BLOCK_WEIGHT);
// Extra reserved space for Operational class
weights.reserved = Some(MAXIMUM_BLOCK_WEIGHT - NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT);
})
// By default Mandatory class is not limited at all.
// This parameter is used to derive maximal size of a single extrinsic.
.avg_block_initialization(AVERAGE_ON_INITIALIZE_RATIO)
.build_or_panic();
}
/// Get the maximum weight (compute time) that a Normal extrinsic on the Millau chain can use.
pub fn max_extrinsic_weight() -> Weight {
BlockWeights::get()
.get(DispatchClass::Normal)
.max_extrinsic
.unwrap_or(Weight::MAX)
}
/// Get the maximum length in bytes that a Normal extrinsic on the Millau chain requires.
pub fn max_extrinsic_size() -> u32 {
*BlockLength::get().max.get(DispatchClass::Normal)
}
@@ -15,12 +15,12 @@ bp-runtime = { path = "../runtime", default-features = false }
# Substrate Based Dependencies
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
[features]
default = ["std"]
@@ -23,7 +23,7 @@
use bp_messages::{LaneId, MessageDetails, MessageNonce, UnrewardedRelayersState};
use bp_runtime::Chain;
use frame_support::{
weights::{constants::WEIGHT_PER_SECOND, DispatchClass, Weight},
weights::{constants::WEIGHT_PER_SECOND, DispatchClass, IdentityFee, Weight},
Parameter, RuntimeDebug,
};
use frame_system::limits;
@@ -42,7 +42,7 @@ pub const EXTRA_STORAGE_PROOF_SIZE: u32 = 1024;
/// Number of bytes, included in the signed Rialto transaction apart from the encoded call itself.
///
/// Can be computed by subtracting encoded call size from raw transaction size.
pub const TX_EXTRA_BYTES: u32 = 103;
pub const TX_EXTRA_BYTES: u32 = 104;
/// Maximal size (in bytes) of encoded (using `Encode::encode()`) account id.
pub const MAXIMAL_ENCODED_ACCOUNT_ID_SIZE: u32 = 32;
@@ -68,29 +68,32 @@ pub const MAX_UNCONFIRMED_MESSAGES_AT_INBOUND_LANE: MessageNonce = 128;
/// Weight of single regular message delivery transaction on Rialto chain.
///
/// This value is a result of `pallet_bridge_messages::Pallet::receive_messages_proof_weight()` call
/// for the case when single message of `pallet_bridge_messages::EXPECTED_DEFAULT_MESSAGE_LENGTH` bytes is delivered.
/// The message must have dispatch weight set to zero. The result then must be rounded up to account
/// possible future runtime upgrades.
/// for the case when single message of `pallet_bridge_messages::EXPECTED_DEFAULT_MESSAGE_LENGTH`
/// bytes is delivered. The message must have dispatch weight set to zero. The result then must be
/// rounded up to account possible future runtime upgrades.
pub const DEFAULT_MESSAGE_DELIVERY_TX_WEIGHT: Weight = 1_500_000_000;
/// Increase of delivery transaction weight on Rialto chain with every additional message byte.
///
/// This value is a result of `pallet_bridge_messages::WeightInfoExt::storage_proof_size_overhead(1)` call. The
/// result then must be rounded up to account possible future runtime upgrades.
/// This value is a result of
/// `pallet_bridge_messages::WeightInfoExt::storage_proof_size_overhead(1)` call. The result then
/// must be rounded up to account possible future runtime upgrades.
pub const ADDITIONAL_MESSAGE_BYTE_DELIVERY_WEIGHT: Weight = 25_000;
/// Maximal weight of single message delivery confirmation transaction on Rialto chain.
///
/// This value is a result of `pallet_bridge_messages::Pallet::receive_messages_delivery_proof` weight formula computation
/// for the case when single message is confirmed. The result then must be rounded up to account possible future
/// runtime upgrades.
/// This value is a result of `pallet_bridge_messages::Pallet::receive_messages_delivery_proof`
/// weight formula computation for the case when single message is confirmed. The result then must
/// be rounded up to account possible future runtime upgrades.
pub const MAX_SINGLE_MESSAGE_DELIVERY_CONFIRMATION_TX_WEIGHT: Weight = 2_000_000_000;
/// Weight of pay-dispatch-fee operation for inbound messages at Rialto chain.
///
/// This value corresponds to the result of `pallet_bridge_messages::WeightInfoExt::pay_inbound_dispatch_fee_overhead()`
/// call for your chain. Don't put too much reserve there, because it is used to **decrease**
/// `DEFAULT_MESSAGE_DELIVERY_TX_WEIGHT` cost. So putting large reserve would make delivery transactions cheaper.
/// This value corresponds to the result of
/// `pallet_bridge_messages::WeightInfoExt::pay_inbound_dispatch_fee_overhead()` call for your
/// chain. Don't put too much reserve there, because it is used to **decrease**
/// `DEFAULT_MESSAGE_DELIVERY_TX_WEIGHT` cost. So putting large reserve would make delivery
/// transactions cheaper.
pub const PAY_INBOUND_DISPATCH_FEE_WEIGHT: Weight = 600_000_000;
/// The target length of a session (how often authorities change) on Rialto measured in of number of
@@ -105,7 +108,7 @@ pub use time_units::*;
/// Human readable time units defined in terms of number of blocks.
pub mod time_units {
use super::BlockNumber;
use super::{BlockNumber, SESSION_LENGTH};
pub const MILLISECS_PER_BLOCK: u64 = 6000;
pub const SLOT_DURATION: u64 = MILLISECS_PER_BLOCK;
@@ -113,6 +116,11 @@ pub mod time_units {
pub const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber);
pub const HOURS: BlockNumber = MINUTES * 60;
pub const DAYS: BlockNumber = HOURS * 24;
pub const EPOCH_DURATION_IN_SLOTS: BlockNumber = SESSION_LENGTH;
// 1 in 4 blocks (on average, not counting collisions) will be primary babe blocks.
pub const PRIMARY_PROBABILITY: (u64, u64) = (1, 4);
}
/// Block number type used in Rialto.
@@ -121,7 +129,7 @@ pub type BlockNumber = u32;
/// Hash type used in Rialto.
pub type Hash = <BlakeTwo256 as HasherT>::Out;
/// The type of an object that can produce hashes on Rialto.
/// The type of object that can produce hashes on Rialto.
pub type Hasher = BlakeTwo256;
/// The header type used by Rialto.
@@ -140,6 +148,15 @@ pub type AccountSigner = MultiSigner;
/// Balance of an account.
pub type Balance = u128;
/// An instant or duration in time.
pub type Moment = u64;
/// Index of a transaction in the chain.
pub type Index = u32;
/// Weight-to-Fee type used by Rialto.
pub type WeightToFee = IdentityFee<Balance>;
/// Rialto chain.
#[derive(RuntimeDebug)]
pub struct Rialto;
@@ -149,6 +166,11 @@ impl Chain for Rialto {
type Hash = Hash;
type Hasher = Hasher;
type Header = Header;
type AccountId = AccountId;
type Balance = Balance;
type Index = Index;
type Signature = Signature;
}
/// Convert a 256-bit hash into an AccountId.
@@ -206,25 +228,40 @@ pub fn max_extrinsic_size() -> u32 {
*BlockLength::get().max.get(DispatchClass::Normal)
}
/// Name of the With-Millau messages pallet instance in the Rialto runtime.
pub const WITH_MILLAU_MESSAGES_PALLET_NAME: &str = "BridgeMillauMessages";
/// Name of the parachain registrar pallet in the Rialto runtime.
pub const PARAS_REGISTRAR_PALLET_NAME: &str = "Registrar";
/// Name of the parachains pallet in the Rialto runtime.
pub const PARAS_PALLET_NAME: &str = "Paras";
/// Name of the `RialtoFinalityApi::best_finalized` runtime method.
pub const BEST_FINALIZED_RIALTO_HEADER_METHOD: &str = "RialtoFinalityApi_best_finalized";
/// Name of the `ToRialtoOutboundLaneApi::estimate_message_delivery_and_dispatch_fee` runtime method.
/// Name of the `ToRialtoOutboundLaneApi::estimate_message_delivery_and_dispatch_fee` runtime
/// method.
pub const TO_RIALTO_ESTIMATE_MESSAGE_FEE_METHOD: &str =
"ToRialtoOutboundLaneApi_estimate_message_delivery_and_dispatch_fee";
/// Name of the `ToRialtoOutboundLaneApi::message_details` runtime method.
pub const TO_RIALTO_MESSAGE_DETAILS_METHOD: &str = "ToRialtoOutboundLaneApi_message_details";
/// Name of the `ToRialtoOutboundLaneApi::latest_generated_nonce` runtime method.
pub const TO_RIALTO_LATEST_GENERATED_NONCE_METHOD: &str = "ToRialtoOutboundLaneApi_latest_generated_nonce";
pub const TO_RIALTO_LATEST_GENERATED_NONCE_METHOD: &str =
"ToRialtoOutboundLaneApi_latest_generated_nonce";
/// Name of the `ToRialtoOutboundLaneApi::latest_received_nonce` runtime method.
pub const TO_RIALTO_LATEST_RECEIVED_NONCE_METHOD: &str = "ToRialtoOutboundLaneApi_latest_received_nonce";
pub const TO_RIALTO_LATEST_RECEIVED_NONCE_METHOD: &str =
"ToRialtoOutboundLaneApi_latest_received_nonce";
/// Name of the `FromRialtoInboundLaneApi::latest_received_nonce` runtime method.
pub const FROM_RIALTO_LATEST_RECEIVED_NONCE_METHOD: &str = "FromRialtoInboundLaneApi_latest_received_nonce";
pub const FROM_RIALTO_LATEST_RECEIVED_NONCE_METHOD: &str =
"FromRialtoInboundLaneApi_latest_received_nonce";
/// Name of the `FromRialtoInboundLaneApi::latest_onfirmed_nonce` runtime method.
pub const FROM_RIALTO_LATEST_CONFIRMED_NONCE_METHOD: &str = "FromRialtoInboundLaneApi_latest_confirmed_nonce";
pub const FROM_RIALTO_LATEST_CONFIRMED_NONCE_METHOD: &str =
"FromRialtoInboundLaneApi_latest_confirmed_nonce";
/// Name of the `FromRialtoInboundLaneApi::unrewarded_relayers_state` runtime method.
pub const FROM_RIALTO_UNREWARDED_RELAYERS_STATE: &str = "FromRialtoInboundLaneApi_unrewarded_relayers_state";
pub const FROM_RIALTO_UNREWARDED_RELAYERS_STATE: &str =
"FromRialtoInboundLaneApi_unrewarded_relayers_state";
sp_api::decl_runtime_apis! {
/// API for querying information about the finalized Rialto headers.
@@ -248,7 +285,7 @@ sp_api::decl_runtime_apis! {
///
/// Returns `None` if message is too expensive to be sent to Rialto from this chain.
///
/// Please keep in mind that this method returns lowest message fee required for message
/// Please keep in mind that this method returns the lowest message fee required for message
/// to be accepted to the lane. It may be good idea to pay a bit over this price to account
/// future exchange rate changes and guarantee that relayer would deliver your message
/// to the target chain.
@@ -279,7 +316,7 @@ sp_api::decl_runtime_apis! {
pub trait FromRialtoInboundLaneApi {
/// 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.
/// Nonce of the latest message that has been confirmed to the bridged chain.
fn latest_confirmed_nonce(lane: LaneId) -> MessageNonce;
/// State of the unrewarded relayers set at given lane.
fn unrewarded_relayers_state(lane: LaneId) -> UnrewardedRelayersState;
@@ -7,8 +7,8 @@ edition = "2018"
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
[dependencies]
parity-scale-codec = { version = "2.0.0", default-features = false, features = ["derive"] }
smallvec = "1.6"
parity-scale-codec = { version = "2.2.0", default-features = false, features = ["derive"] }
smallvec = "1.7"
# Bridge Dependencies
bp-messages = { path = "../messages", default-features = false }
@@ -16,8 +16,8 @@ bp-polkadot-core = { path = "../polkadot-core", default-features = false }
bp-runtime = { path = "../runtime", default-features = false }
# Substrate Based Dependencies
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-version = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
@@ -21,7 +21,9 @@
#![allow(clippy::unnecessary_mut_passed)]
use bp_messages::{LaneId, MessageDetails, MessageNonce, UnrewardedRelayersState};
use frame_support::weights::{WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial};
use frame_support::weights::{
Weight, WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial,
};
use sp_std::prelude::*;
use sp_version::RuntimeVersion;
@@ -30,8 +32,8 @@ pub use bp_polkadot_core::*;
/// Rococo Chain
pub type Rococo = PolkadotLike;
/// The target length of a session (how often authorities change) on Westend measured in of number of
/// blocks.
/// The target length of a session (how often authorities change) on Westend measured in of number
/// of blocks.
///
/// Note that since this is a target sessions may change before/after this time depending on network
/// conditions.
@@ -72,27 +74,45 @@ pub fn derive_account_from_wococo_id(id: bp_runtime::SourceAccount<AccountId>) -
AccountIdConverter::convert(encoded_id)
}
/// Name of the With-Wococo messages pallet instance in the Rococo runtime.
pub const WITH_WOCOCO_MESSAGES_PALLET_NAME: &str = "BridgeWococoMessages";
/// Name of the `RococoFinalityApi::best_finalized` runtime method.
pub const BEST_FINALIZED_ROCOCO_HEADER_METHOD: &str = "RococoFinalityApi_best_finalized";
/// Name of the `RococoFinalityApi::is_known_header` runtime method.
pub const IS_KNOWN_ROCOCO_HEADER_METHOD: &str = "RococoFinalityApi_is_known_header";
/// Name of the `ToRococoOutboundLaneApi::estimate_message_delivery_and_dispatch_fee` runtime method.
/// Name of the `ToRococoOutboundLaneApi::estimate_message_delivery_and_dispatch_fee` runtime
/// method.
pub const TO_ROCOCO_ESTIMATE_MESSAGE_FEE_METHOD: &str =
"ToRococoOutboundLaneApi_estimate_message_delivery_and_dispatch_fee";
/// Name of the `ToRococoOutboundLaneApi::message_details` runtime method.
pub const TO_ROCOCO_MESSAGE_DETAILS_METHOD: &str = "ToRococoOutboundLaneApi_message_details";
/// Name of the `ToRococoOutboundLaneApi::latest_generated_nonce` runtime method.
pub const TO_ROCOCO_LATEST_GENERATED_NONCE_METHOD: &str = "ToRococoOutboundLaneApi_latest_generated_nonce";
pub const TO_ROCOCO_LATEST_GENERATED_NONCE_METHOD: &str =
"ToRococoOutboundLaneApi_latest_generated_nonce";
/// Name of the `ToRococoOutboundLaneApi::latest_received_nonce` runtime method.
pub const TO_ROCOCO_LATEST_RECEIVED_NONCE_METHOD: &str = "ToRococoOutboundLaneApi_latest_received_nonce";
pub const TO_ROCOCO_LATEST_RECEIVED_NONCE_METHOD: &str =
"ToRococoOutboundLaneApi_latest_received_nonce";
/// Name of the `FromRococoInboundLaneApi::latest_received_nonce` runtime method.
pub const FROM_ROCOCO_LATEST_RECEIVED_NONCE_METHOD: &str = "FromRococoInboundLaneApi_latest_received_nonce";
pub const FROM_ROCOCO_LATEST_RECEIVED_NONCE_METHOD: &str =
"FromRococoInboundLaneApi_latest_received_nonce";
/// Name of the `FromRococoInboundLaneApi::latest_onfirmed_nonce` runtime method.
pub const FROM_ROCOCO_LATEST_CONFIRMED_NONCE_METHOD: &str = "FromRococoInboundLaneApi_latest_confirmed_nonce";
pub const FROM_ROCOCO_LATEST_CONFIRMED_NONCE_METHOD: &str =
"FromRococoInboundLaneApi_latest_confirmed_nonce";
/// Name of the `FromRococoInboundLaneApi::unrewarded_relayers_state` runtime method.
pub const FROM_ROCOCO_UNREWARDED_RELAYERS_STATE: &str = "FromRococoInboundLaneApi_unrewarded_relayers_state";
pub const FROM_ROCOCO_UNREWARDED_RELAYERS_STATE: &str =
"FromRococoInboundLaneApi_unrewarded_relayers_state";
/// Weight of pay-dispatch-fee operation for inbound messages at Rococo chain.
///
/// This value corresponds to the result of
/// `pallet_bridge_messages::WeightInfoExt::pay_inbound_dispatch_fee_overhead()` call for your
/// chain. Don't put too much reserve there, because it is used to **decrease**
/// `DEFAULT_MESSAGE_DELIVERY_TX_WEIGHT` cost. So putting large reserve would make delivery
/// transactions cheaper.
pub const PAY_INBOUND_DISPATCH_FEE_WEIGHT: Weight = 600_000_000;
sp_api::decl_runtime_apis! {
/// API for querying information about the finalized Rococo headers.
@@ -7,16 +7,21 @@ edition = "2018"
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
[dependencies]
parity-scale-codec = { version = "2.0.0", default-features = false, features = ["derive"] }
parity-scale-codec = { version = "2.2.0", default-features = false, features = ["derive"] }
scale-info = { version = "1.0", default-features = false, features = ["derive"] }
smallvec = "1.7"
# Bridge Dependencies
bp-header-chain = { path = "../header-chain", default-features = false }
bp-messages = { path = "../messages", default-features = false }
bp-polkadot-core = { path = "../polkadot-core", default-features = false }
bp-runtime = { path = "../runtime", default-features = false }
# Substrate Based Dependencies
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-version = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
@@ -28,7 +33,9 @@ std = [
"bp-messages/std",
"bp-polkadot-core/std",
"bp-runtime/std",
"frame-support/std",
"parity-scale-codec/std",
"scale-info/std",
"sp-api/std",
"sp-runtime/std",
"sp-std/std",
@@ -21,7 +21,10 @@
#![allow(clippy::unnecessary_mut_passed)]
use bp_messages::{LaneId, MessageDetails, MessageNonce, UnrewardedRelayersState};
use bp_runtime::Chain;
use frame_support::weights::{
WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial,
};
use scale_info::TypeInfo;
use sp_std::prelude::*;
use sp_version::RuntimeVersion;
@@ -30,7 +33,23 @@ pub use bp_polkadot_core::*;
/// Westend Chain
pub type Westend = PolkadotLike;
pub type UncheckedExtrinsic = bp_polkadot_core::UncheckedExtrinsic<Call>;
// NOTE: This needs to be kept up to date with the Westend runtime found in the Polkadot repo.
pub struct WeightToFee;
impl WeightToFeePolynomial for WeightToFee {
type Balance = Balance;
fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
const CENTS: Balance = 1_000_000_000_000 / 1_000;
// in Westend, extrinsic base weight (smallest non-zero weight) is mapped to 1/10 CENT:
let p = CENTS;
let q = 10 * Balance::from(ExtrinsicBaseWeight::get());
smallvec::smallvec![WeightToFeeCoefficient {
degree: 1,
negative: false,
coeff_frac: Perbill::from_rational(p % q, q),
coeff_integer: p / q,
}]
}
}
// NOTE: This needs to be kept up to date with the Westend runtime found in the Polkadot repo.
pub const VERSION: RuntimeVersion = RuntimeVersion {
@@ -45,32 +64,11 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
/// Westend Runtime `Call` enum.
///
/// The enum represents a subset of possible `Call`s we can send to Westend chain.
/// Ideally this code would be auto-generated from Metadata, because we want to
/// avoid depending directly on the ENTIRE runtime just to get the encoding of `Dispatchable`s.
///
/// All entries here (like pretty much in the entire file) must be kept in sync with Westend
/// `construct_runtime`, so that we maintain SCALE-compatibility.
///
/// See: https://github.com/paritytech/polkadot/blob/master/runtime/westend/src/lib.rs
#[derive(parity_scale_codec::Encode, parity_scale_codec::Decode, Debug, PartialEq, Eq, Clone)]
pub enum Call {
/// Rococo bridge pallet.
#[codec(index = 40)]
BridgeGrandpaRococo(BridgeGrandpaRococoCall),
}
#[derive(parity_scale_codec::Encode, parity_scale_codec::Decode, Debug, PartialEq, Eq, Clone)]
#[allow(non_camel_case_types)]
pub enum BridgeGrandpaRococoCall {
#[codec(index = 0)]
submit_finality_proof(
<PolkadotLike as Chain>::Header,
bp_header_chain::justification::GrandpaJustification<<PolkadotLike as Chain>::Header>,
),
#[codec(index = 1)]
initialize(bp_header_chain::InitializationData<<PolkadotLike as Chain>::Header>),
}
/// We are not currently submitting any Westend transactions => it is empty.
#[derive(
parity_scale_codec::Encode, parity_scale_codec::Decode, Debug, PartialEq, Eq, Clone, TypeInfo,
)]
pub enum Call {}
impl sp_runtime::traits::Dispatchable for Call {
type Origin = ();
@@ -95,25 +93,31 @@ pub const BEST_FINALIZED_WESTEND_HEADER_METHOD: &str = "WestendFinalityApi_best_
/// Name of the `WestendFinalityApi::is_known_header` runtime method.
pub const IS_KNOWN_WESTEND_HEADER_METHOD: &str = "WestendFinalityApi_is_known_header";
/// Name of the `ToWestendOutboundLaneApi::estimate_message_delivery_and_dispatch_fee` runtime method.
/// Name of the `ToWestendOutboundLaneApi::estimate_message_delivery_and_dispatch_fee` runtime
/// method.
pub const TO_WESTEND_ESTIMATE_MESSAGE_FEE_METHOD: &str =
"ToWestendOutboundLaneApi_estimate_message_delivery_and_dispatch_fee";
/// Name of the `ToWestendOutboundLaneApi::message_details` runtime method.
pub const TO_WESTEND_MESSAGE_DETAILS_METHOD: &str = "ToWestendOutboundLaneApi_message_details";
/// Name of the `ToWestendOutboundLaneApi::latest_generated_nonce` runtime method.
pub const TO_WESTEND_LATEST_GENERATED_NONCE_METHOD: &str = "ToWestendOutboundLaneApi_latest_generated_nonce";
pub const TO_WESTEND_LATEST_GENERATED_NONCE_METHOD: &str =
"ToWestendOutboundLaneApi_latest_generated_nonce";
/// Name of the `ToWestendOutboundLaneApi::latest_received_nonce` runtime method.
pub const TO_WESTEND_LATEST_RECEIVED_NONCE_METHOD: &str = "ToWestendOutboundLaneApi_latest_received_nonce";
pub const TO_WESTEND_LATEST_RECEIVED_NONCE_METHOD: &str =
"ToWestendOutboundLaneApi_latest_received_nonce";
/// Name of the `FromWestendInboundLaneApi::latest_received_nonce` runtime method.
pub const FROM_WESTEND_LATEST_RECEIVED_NONCE_METHOD: &str = "FromWestendInboundLaneApi_latest_received_nonce";
pub const FROM_WESTEND_LATEST_RECEIVED_NONCE_METHOD: &str =
"FromWestendInboundLaneApi_latest_received_nonce";
/// Name of the `FromWestendInboundLaneApi::latest_onfirmed_nonce` runtime method.
pub const FROM_WESTEND_LATEST_CONFIRMED_NONCE_METHOD: &str = "FromWestendInboundLaneApi_latest_confirmed_nonce";
pub const FROM_WESTEND_LATEST_CONFIRMED_NONCE_METHOD: &str =
"FromWestendInboundLaneApi_latest_confirmed_nonce";
/// Name of the `FromWestendInboundLaneApi::unrewarded_relayers_state` runtime method.
pub const FROM_WESTEND_UNREWARDED_RELAYERS_STATE: &str = "FromWestendInboundLaneApi_unrewarded_relayers_state";
pub const FROM_WESTEND_UNREWARDED_RELAYERS_STATE: &str =
"FromWestendInboundLaneApi_unrewarded_relayers_state";
/// The target length of a session (how often authorities change) on Westend measured in of number of
/// blocks.
/// The target length of a session (how often authorities change) on Westend measured in of number
/// of blocks.
///
/// Note that since this is a target sessions may change before/after this time depending on network
/// conditions.
@@ -7,7 +7,7 @@ edition = "2018"
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
[dependencies]
parity-scale-codec = { version = "2.0.0", default-features = false, features = ["derive"] }
parity-scale-codec = { version = "2.2.0", default-features = false, features = ["derive"] }
# Bridge Dependencies
bp-messages = { path = "../messages", default-features = false }
@@ -16,7 +16,7 @@ bp-rococo = { path = "../chain-rococo", default-features = false }
bp-runtime = { path = "../runtime", default-features = false }
# Substrate Based Dependencies
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
@@ -25,7 +25,7 @@ use sp_std::prelude::*;
pub use bp_polkadot_core::*;
// Rococo runtime = Wococo runtime
pub use bp_rococo::{WeightToFee, SESSION_LENGTH, VERSION};
pub use bp_rococo::{WeightToFee, PAY_INBOUND_DISPATCH_FEE_WEIGHT, SESSION_LENGTH, VERSION};
/// Wococo Chain
pub type Wococo = PolkadotLike;
@@ -37,27 +37,36 @@ pub fn derive_account_from_rococo_id(id: bp_runtime::SourceAccount<AccountId>) -
AccountIdConverter::convert(encoded_id)
}
/// Name of the With-Rococo messages pallet instance in the Wococo runtime.
pub const WITH_ROCOCO_MESSAGES_PALLET_NAME: &str = "BridgeRococoMessages";
/// Name of the `WococoFinalityApi::best_finalized` runtime method.
pub const BEST_FINALIZED_WOCOCO_HEADER_METHOD: &str = "WococoFinalityApi_best_finalized";
/// Name of the `WococoFinalityApi::is_known_header` runtime method.
pub const IS_KNOWN_WOCOCO_HEADER_METHOD: &str = "WococoFinalityApi_is_known_header";
/// Name of the `ToWococoOutboundLaneApi::estimate_message_delivery_and_dispatch_fee` runtime method.
/// Name of the `ToWococoOutboundLaneApi::estimate_message_delivery_and_dispatch_fee` runtime
/// method.
pub const TO_WOCOCO_ESTIMATE_MESSAGE_FEE_METHOD: &str =
"ToWococoOutboundLaneApi_estimate_message_delivery_and_dispatch_fee";
/// Name of the `ToWococoOutboundLaneApi::message_details` runtime method.
pub const TO_WOCOCO_MESSAGE_DETAILS_METHOD: &str = "ToWococoOutboundLaneApi_message_details";
/// Name of the `ToWococoOutboundLaneApi::latest_generated_nonce` runtime method.
pub const TO_WOCOCO_LATEST_GENERATED_NONCE_METHOD: &str = "ToWococoOutboundLaneApi_latest_generated_nonce";
pub const TO_WOCOCO_LATEST_GENERATED_NONCE_METHOD: &str =
"ToWococoOutboundLaneApi_latest_generated_nonce";
/// Name of the `ToWococoOutboundLaneApi::latest_received_nonce` runtime method.
pub const TO_WOCOCO_LATEST_RECEIVED_NONCE_METHOD: &str = "ToWococoOutboundLaneApi_latest_received_nonce";
pub const TO_WOCOCO_LATEST_RECEIVED_NONCE_METHOD: &str =
"ToWococoOutboundLaneApi_latest_received_nonce";
/// Name of the `FromWococoInboundLaneApi::latest_received_nonce` runtime method.
pub const FROM_WOCOCO_LATEST_RECEIVED_NONCE_METHOD: &str = "FromWococoInboundLaneApi_latest_received_nonce";
pub const FROM_WOCOCO_LATEST_RECEIVED_NONCE_METHOD: &str =
"FromWococoInboundLaneApi_latest_received_nonce";
/// Name of the `FromWococoInboundLaneApi::latest_onfirmed_nonce` runtime method.
pub const FROM_WOCOCO_LATEST_CONFIRMED_NONCE_METHOD: &str = "FromWococoInboundLaneApi_latest_confirmed_nonce";
pub const FROM_WOCOCO_LATEST_CONFIRMED_NONCE_METHOD: &str =
"FromWococoInboundLaneApi_latest_confirmed_nonce";
/// Name of the `FromWococoInboundLaneApi::unrewarded_relayers_state` runtime method.
pub const FROM_WOCOCO_UNREWARDED_RELAYERS_STATE: &str = "FromWococoInboundLaneApi_unrewarded_relayers_state";
pub const FROM_WOCOCO_UNREWARDED_RELAYERS_STATE: &str =
"FromWococoInboundLaneApi_unrewarded_relayers_state";
sp_api::decl_runtime_apis! {
/// API for querying information about the finalized Wococo headers.
@@ -81,7 +90,7 @@ sp_api::decl_runtime_apis! {
///
/// Returns `None` if message is too expensive to be sent to Wococo from this chain.
///
/// Please keep in mind that this method returns lowest message fee required for message
/// Please keep in mind that this method returns the lowest message fee required for message
/// to be accepted to the lane. It may be good idea to pay a bit over this price to account
/// future exchange rate changes and guarantee that relayer would deliver your message
/// to the target chain.
@@ -112,7 +121,7 @@ sp_api::decl_runtime_apis! {
pub trait FromWococoInboundLaneApi {
/// 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.
/// Nonce of the latest message that has been confirmed to the bridged chain.
fn latest_confirmed_nonce(lane: LaneId) -> MessageNonce;
/// State of the unrewarded relayers set at given lane.
fn unrewarded_relayers_state(lane: LaneId) -> UnrewardedRelayersState;
@@ -7,19 +7,21 @@ edition = "2018"
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
[dependencies]
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false }
codec = { package = "parity-scale-codec", version = "2.2.0", default-features = false }
scale-info = { version = "1.0", default-features = false, features = ["derive"] }
# Substrate Dependencies
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
[features]
default = ["std"]
std = [
"codec/std",
"frame-support/std",
"scale-info/std",
"sp-api/std",
"sp-std/std",
]
@@ -22,6 +22,7 @@
use codec::{Decode, Encode, EncodeLike};
use frame_support::{Parameter, RuntimeDebug};
use scale_info::TypeInfo;
use sp_api::decl_runtime_apis;
use sp_std::marker::PhantomData;
@@ -36,7 +37,7 @@ pub enum Error {
InvalidRecipient,
/// Cannot map from peer recipient to this blockchain recipient.
FailedToMapRecipients,
/// Failed to convert from peer blockchain currency to this blockhain currency.
/// Failed to convert from peer blockchain currency to this blockchain currency.
FailedToConvertCurrency,
/// Deposit has failed.
DepositFailed,
@@ -48,7 +49,7 @@ pub enum Error {
pub type Result<T> = sp_std::result::Result<T, Error>;
/// Peer blockchain lock funds transaction.
#[derive(Encode, Decode, Clone, RuntimeDebug, PartialEq, Eq)]
#[derive(Encode, Decode, Clone, RuntimeDebug, PartialEq, Eq, TypeInfo)]
pub struct LockFundsTransaction<TransferId, Recipient, Amount> {
/// Something that uniquely identifies this transfer.
pub id: TransferId,
@@ -63,7 +64,7 @@ pub trait MaybeLockFundsTransaction {
/// Transaction type.
type Transaction;
/// Identifier that uniquely identifies this transfer.
type Id: Decode + Encode + EncodeLike + sp_std::fmt::Debug;
type Id: Decode + Encode + TypeInfo + EncodeLike + sp_std::fmt::Debug;
/// Peer recipient type.
type Recipient;
/// Peer currency amount type.
@@ -71,7 +72,9 @@ pub trait MaybeLockFundsTransaction {
/// Parse lock funds transaction of the peer blockchain. Returns None if
/// transaction format is unknown, or it isn't a lock funds transaction.
fn parse(tx: &Self::Transaction) -> Result<LockFundsTransaction<Self::Id, Self::Recipient, Self::Amount>>;
fn parse(
tx: &Self::Transaction,
) -> Result<LockFundsTransaction<Self::Id, Self::Recipient, Self::Amount>>;
}
/// Map that maps recipients from peer blockchain to this blockchain recipients.
@@ -7,27 +7,28 @@ edition = "2018"
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
[dependencies]
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false }
codec = { package = "parity-scale-codec", version = "2.2.0", default-features = false }
ethbloom = { version = "0.10.0", default-features = false, features = ["rlp"] }
fixed-hash = { version = "0.7", default-features = false }
hash-db = { version = "0.15.2", default-features = false }
impl-rlp = { version = "0.3", default-features = false }
impl-serde = { version = "0.3.1", optional = true }
libsecp256k1 = { version = "0.3.4", default-features = false, features = ["hmac"] }
libsecp256k1 = { version = "0.7", default-features = false, features = ["hmac", "static-context"] }
parity-bytes = { version = "0.1", default-features = false }
plain_hasher = { version = "0.2.2", default-features = false }
primitive-types = { version = "0.9", default-features = false, features = ["codec", "rlp"] }
primitive-types = { version = "0.10", default-features = false, features = ["codec", "rlp"] }
rlp = { version = "0.5", default-features = false }
scale-info = { version = "1.0", default-features = false, features = ["derive"] }
serde = { version = "1.0", optional = true }
serde-big-array = { version = "0.2", optional = true }
triehash = { version = "0.8.2", default-features = false }
# Substrate Dependencies
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
[dev-dependencies]
hex-literal = "0.2"
@@ -47,6 +48,7 @@ std = [
"primitive-types/std",
"primitive-types/serde",
"rlp/std",
"scale-info/std",
"serde/std",
"serde-big-array",
"sp-api/std",
@@ -28,6 +28,7 @@ use codec::{Decode, Encode};
use ethbloom::{Bloom as EthBloom, Input as BloomInput};
use fixed_hash::construct_fixed_hash;
use rlp::{Decodable, DecoderError, Rlp, RlpStream};
use scale_info::TypeInfo;
use sp_io::hashing::keccak_256;
use sp_runtime::RuntimeDebug;
use sp_std::prelude::*;
@@ -57,7 +58,7 @@ pub type Address = H160;
pub mod signatures;
/// Complete header id.
#[derive(Encode, Decode, Default, RuntimeDebug, PartialEq, Clone, Copy)]
#[derive(Encode, Decode, Default, RuntimeDebug, PartialEq, Clone, Copy, TypeInfo)]
pub struct HeaderId {
/// Header number.
pub number: u64,
@@ -66,7 +67,7 @@ pub struct HeaderId {
}
/// An Aura header.
#[derive(Clone, Default, Encode, Decode, PartialEq, RuntimeDebug)]
#[derive(Clone, Default, Encode, Decode, PartialEq, RuntimeDebug, TypeInfo)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct AuraHeader {
/// Parent block hash.
@@ -129,7 +130,7 @@ pub struct UnsignedTransaction {
}
/// Information describing execution of a transaction.
#[derive(Clone, Encode, Decode, PartialEq, RuntimeDebug)]
#[derive(Clone, Encode, Decode, PartialEq, RuntimeDebug, TypeInfo)]
pub struct Receipt {
/// The total gas used in the block following execution of the transaction.
pub gas_used: U256,
@@ -142,7 +143,7 @@ pub struct Receipt {
}
/// Transaction outcome store in the receipt.
#[derive(Clone, Encode, Decode, PartialEq, RuntimeDebug)]
#[derive(Clone, Encode, Decode, PartialEq, RuntimeDebug, TypeInfo)]
pub enum TransactionOutcome {
/// Status and state root are unknown under EIP-98 rules.
Unknown,
@@ -153,7 +154,7 @@ pub enum TransactionOutcome {
}
/// A record of execution for a `LOG` operation.
#[derive(Clone, Encode, Decode, PartialEq, RuntimeDebug)]
#[derive(Clone, Encode, Decode, PartialEq, RuntimeDebug, TypeInfo)]
pub struct LogEntry {
/// The address of the contract executing at the point of the `LOG` operation.
pub address: Address,
@@ -164,7 +165,7 @@ pub struct LogEntry {
}
/// Logs bloom.
#[derive(Clone, Encode, Decode)]
#[derive(Clone, Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct Bloom(#[cfg_attr(feature = "std", serde(with = "BigArray"))] [u8; 256]);
@@ -185,10 +186,7 @@ pub struct SealedEmptyStep {
impl AuraHeader {
/// Compute id of this header.
pub fn compute_id(&self) -> HeaderId {
HeaderId {
number: self.number,
hash: self.compute_hash(),
}
HeaderId { number: self.number, hash: self.compute_hash() }
}
/// Compute hash of this header (keccak of the RLP with seal).
@@ -198,10 +196,9 @@ impl AuraHeader {
/// Get id of this header' parent. Returns None if this is genesis header.
pub fn parent_id(&self) -> Option<HeaderId> {
self.number.checked_sub(1).map(|parent_number| HeaderId {
number: parent_number,
hash: self.parent_hash,
})
self.number
.checked_sub(1)
.map(|parent_number| HeaderId { number: parent_number, hash: self.parent_hash })
}
/// Check if passed transactions receipts are matching receipts root in this header.
@@ -238,7 +235,7 @@ impl AuraHeader {
let mut message = self.compute_hash().as_bytes().to_vec();
message.extend_from_slice(self.seal.get(2)?);
keccak_256(&message).into()
}
},
false => keccak_256(&self.rlp(false)).into(),
})
}
@@ -255,9 +252,7 @@ impl AuraHeader {
/// Extracts the empty steps from the header seal.
pub fn empty_steps(&self) -> Option<Vec<SealedEmptyStep>> {
self.seal
.get(2)
.and_then(|x| Rlp::new(x).as_list::<SealedEmptyStep>().ok())
self.seal.get(2).and_then(|x| Rlp::new(x).as_list::<SealedEmptyStep>().ok())
}
/// Returns header RLP with or without seals.
@@ -323,7 +318,7 @@ impl UnsignedTransaction {
stream.out().to_vec()
}
/// Encode to given rlp stream.
/// Encode to given RLP stream.
pub fn rlp_to(&self, chain_id: Option<u64>, stream: &mut RlpStream) {
stream.append(&self.nonce);
stream.append(&self.gas_price);
@@ -368,15 +363,15 @@ impl Receipt {
match self.outcome {
TransactionOutcome::Unknown => {
s.begin_list(3);
}
},
TransactionOutcome::StateRoot(ref root) => {
s.begin_list(4);
s.append(root);
}
},
TransactionOutcome::StatusCode(ref status_code) => {
s.begin_list(4);
s.append(status_code);
}
},
}
s.append(&self.gas_used);
s.append(&EthBloom::from(self.log_bloom.0));
@@ -405,7 +400,7 @@ impl SealedEmptyStep {
keccak_256(&message.out()).into()
}
/// Returns rlp for the vector of empty steps (we only do encoding in tests).
/// Returns RLP for the vector of empty steps (we only do encoding in tests).
pub fn rlp_of(empty_steps: &[SealedEmptyStep]) -> Bytes {
let mut s = RlpStream::new();
s.begin_list(empty_steps.len());
@@ -428,13 +423,13 @@ impl Decodable for SealedEmptyStep {
impl LogEntry {
/// Calculates the bloom of this log entry.
pub fn bloom(&self) -> Bloom {
let eth_bloom =
self.topics
.iter()
.fold(EthBloom::from(BloomInput::Raw(self.address.as_bytes())), |mut b, t| {
b.accrue(BloomInput::Raw(t.as_bytes()));
b
});
let eth_bloom = self.topics.iter().fold(
EthBloom::from(BloomInput::Raw(self.address.as_bytes())),
|mut b, t| {
b.accrue(BloomInput::Raw(t.as_bytes()));
b
},
);
Bloom(*eth_bloom.data())
}
}
@@ -458,6 +453,8 @@ impl PartialEq<Bloom> for Bloom {
}
}
// there's no default for [_; 256], but clippy still complains
#[allow(clippy::derivable_impls)]
impl Default for Bloom {
fn default() -> Self {
Bloom([0; 256])
@@ -496,14 +493,12 @@ pub fn transaction_decode_rlp(raw_tx: &[u8]) -> Result<Transaction, DecoderError
let message = unsigned.message(chain_id);
// recover tx sender
let sender_public = sp_io::crypto::secp256k1_ecdsa_recover(&signature, message.as_fixed_bytes())
.map_err(|_| rlp::DecoderError::Custom("Failed to recover transaction sender"))?;
let sender_public =
sp_io::crypto::secp256k1_ecdsa_recover(&signature, message.as_fixed_bytes())
.map_err(|_| rlp::DecoderError::Custom("Failed to recover transaction sender"))?;
let sender_address = public_to_address(&sender_public);
Ok(Transaction {
sender: sender_address,
unsigned,
})
Ok(Transaction { sender: sender_address, unsigned })
}
/// Convert public key into corresponding ethereum address.
@@ -517,7 +512,10 @@ pub fn public_to_address(public: &[u8; 64]) -> Address {
/// Check ethereum merkle proof.
/// Returns Ok(computed-root) if check succeeds.
/// Returns Err(computed-root) if check fails.
fn check_merkle_proof<T: AsRef<[u8]>>(expected_root: H256, items: impl Iterator<Item = T>) -> Result<H256, H256> {
fn check_merkle_proof<T: AsRef<[u8]>>(
expected_root: H256,
items: impl Iterator<Item = T>,
) -> Result<H256, H256> {
let computed_root = compute_merkle_root(items);
if computed_root == expected_root {
Ok(computed_root)
@@ -20,14 +20,14 @@
//! Used for testing and benchmarking.
// reexport to avoid direct secp256k1 deps by other crates
pub use secp256k1::SecretKey;
pub use libsecp256k1::SecretKey;
use crate::{
public_to_address, rlp_encode, step_validator, Address, AuraHeader, RawTransaction, UnsignedTransaction, H256,
H520, U256,
public_to_address, rlp_encode, step_validator, Address, AuraHeader, RawTransaction,
UnsignedTransaction, H256, H520, U256,
};
use secp256k1::{Message, PublicKey};
use libsecp256k1::{Message, PublicKey};
/// Utilities for signing headers.
pub trait SignHeader {
@@ -80,7 +80,8 @@ impl SignTransaction for UnsignedTransaction {
/// Return author's signature over given message.
pub fn sign(author: &SecretKey, message: H256) -> H520 {
let (signature, recovery_id) = secp256k1::sign(&Message::parse(message.as_fixed_bytes()), author);
let (signature, recovery_id) =
libsecp256k1::sign(&Message::parse(message.as_fixed_bytes()), author);
let mut raw_signature = [0u8; 65];
raw_signature[..64].copy_from_slice(&signature.serialize());
raw_signature[64] = recovery_id.serialize();
@@ -116,10 +117,7 @@ mod tests {
let raw_tx = unsigned.clone().sign_by(&signer, Some(42));
assert_eq!(
transaction_decode_rlp(&raw_tx),
Ok(Transaction {
sender: signer_address,
unsigned,
}),
Ok(Transaction { sender: signer_address, unsigned }),
);
// case2: without chain_id replay protection + contract creation
@@ -134,10 +132,7 @@ mod tests {
let raw_tx = unsigned.clone().sign_by(&signer, None);
assert_eq!(
transaction_decode_rlp(&raw_tx),
Ok(Transaction {
sender: signer_address,
unsigned,
}),
Ok(Transaction { sender: signer_address, unsigned }),
);
}
}
@@ -7,18 +7,18 @@ edition = "2018"
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
[dependencies]
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false }
finality-grandpa = { version = "0.14.4", default-features = false }
codec = { package = "parity-scale-codec", version = "2.2.0", default-features = false }
finality-grandpa = { version = "0.14.0", default-features = false }
scale-info = { version = "1.0", default-features = false, features = ["derive"] }
serde = { version = "1.0", optional = true }
# Substrate Dependencies
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-finality-grandpa = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-finality-grandpa = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
[dev-dependencies]
assert_matches = "1.5"
@@ -32,6 +32,7 @@ std = [
"scale-info/std",
"serde/std",
"frame-support/std",
"scale-info/std",
"sp-core/std",
"sp-finality-grandpa/std",
"sp-runtime/std",
@@ -25,8 +25,10 @@ use frame_support::RuntimeDebug;
use scale_info::TypeInfo;
use sp_finality_grandpa::{AuthorityId, AuthoritySignature, SetId};
use sp_runtime::traits::Header as HeaderT;
use sp_std::collections::{btree_map::BTreeMap, btree_set::BTreeSet};
use sp_std::prelude::*;
use sp_std::{
collections::{btree_map::BTreeMap, btree_set::BTreeSet},
prelude::*,
};
/// A GRANDPA Justification is a proof that a given header was finalized
/// at a certain height and with a certain set of authorities.
@@ -38,7 +40,8 @@ pub struct GrandpaJustification<Header: HeaderT> {
/// The round (voting period) this justification is valid for.
pub round: u64,
/// The set of votes for the chain which is to be finalized.
pub commit: finality_grandpa::Commit<Header::Hash, Header::Number, AuthoritySignature, AuthorityId>,
pub commit:
finality_grandpa::Commit<Header::Hash, Header::Number, AuthoritySignature, AuthorityId>,
/// A proof that the chain of blocks in the commit are related to each other.
pub votes_ancestries: Vec<Header>,
}
@@ -58,7 +61,8 @@ pub enum Error {
InvalidJustificationTarget,
/// The authority has provided an invalid signature.
InvalidAuthoritySignature,
/// The justification contains pre-commit for header that is not a descendant of the commit header.
/// The justification contains precommit for header that is not a descendant of the commit
/// header.
PrecommitIsNotCommitDescendant,
/// The cumulative weight of all votes in the justification is not enough to justify commit
/// header finalization.
@@ -88,7 +92,7 @@ where
{
// ensure that it is justification for the expected header
if (justification.commit.target_hash, justification.commit.target_number) != finalized_target {
return Err(Error::InvalidJustificationTarget);
return Err(Error::InvalidJustificationTarget)
}
let mut chain = AncestryChain::new(&justification.votes_ancestries);
@@ -100,30 +104,32 @@ where
let authority_info = match authorities_set.get(&signed.id) {
Some(authority_info) => authority_info,
None => {
// just ignore precommit from unknown authority as `finality_grandpa::import_precommit` does
continue;
}
// just ignore precommit from unknown authority as
// `finality_grandpa::import_precommit` does
continue
},
};
// check if authority has already voted in the same round.
//
// there's a lot of code in `validate_commit` and `import_precommit` functions inside
// `finality-grandpa` crate (mostly related to reporing equivocations). But the only thing that we
// care about is that only first vote from the authority is accepted
// `finality-grandpa` crate (mostly related to reporing equivocations). But the only thing
// that we care about is that only first vote from the authority is accepted
if !votes.insert(signed.id.clone()) {
continue;
continue
}
// everything below this line can't just `continue`, because state is already altered
// all precommits must be for block higher than the target
if signed.precommit.target_number < justification.commit.target_number {
return Err(Error::PrecommitIsNotCommitDescendant);
return Err(Error::PrecommitIsNotCommitDescendant)
}
// all precommits must be for target block descendants
chain = chain.ensure_descendant(&justification.commit.target_hash, &signed.precommit.target_hash)?;
// since we know now that the precommit target is the descendant of the justification target,
// we may increase 'weight' of the justification target
// all precommits must be for target block descendents
chain = chain
.ensure_descendant(&justification.commit.target_hash, &signed.precommit.target_hash)?;
// since we know now that the precommit target is the descendant of the justification
// target, we may increase 'weight' of the justification target
//
// there's a lot of code in the `VoteGraph::insert` method inside `finality-grandpa` crate,
// but in the end it is only used to find GHOST, which we don't care about. The only thing
@@ -145,13 +151,13 @@ where
authorities_set_id,
&mut signature_buffer,
) {
return Err(Error::InvalidAuthoritySignature);
return Err(Error::InvalidAuthoritySignature)
}
}
// check that there are no extra headers in the justification
if !chain.unvisited.is_empty() {
return Err(Error::ExtraHeadersInVotesAncestries);
return Err(Error::ExtraHeadersInVotesAncestries)
}
// check that the cumulative weight of validators voted for the justification target (or one
@@ -169,7 +175,7 @@ where
pub struct AncestryChain<Header: HeaderT> {
/// Header hash => parent header hash mapping.
pub parents: BTreeMap<Header::Hash, Header::Hash>,
/// Hashes of headers that weren't visited by `is_ancestor` method.
/// Hashes of headers that were not visited by `is_ancestor` method.
pub unvisited: BTreeSet<Header::Hash>,
}
@@ -187,7 +193,8 @@ impl<Header: HeaderT> AncestryChain<Header> {
AncestryChain { parents, unvisited }
}
/// Returns `Err(_)` if `precommit_target` is a descendant of the `commit_target` block and `Ok(_)` otherwise.
/// Returns `Err(_)` if `precommit_target` is a descendant of the `commit_target` block and
/// `Ok(_)` otherwise.
pub fn ensure_descendant(
mut self,
commit_target: &Header::Hash,
@@ -196,22 +203,22 @@ impl<Header: HeaderT> AncestryChain<Header> {
let mut current_hash = *precommit_target;
loop {
if current_hash == *commit_target {
break;
break
}
let is_visited_before = !self.unvisited.remove(&current_hash);
current_hash = match self.parents.get(&current_hash) {
Some(parent_hash) => {
if is_visited_before {
// `Some(parent_hash)` means that the `current_hash` is in the `parents` container
// `is_visited_before` means that it has been visited before in some of previous calls
// => since we assume that previous call has finished with `true`, this also will
// be finished with `true`
return Ok(self);
// `Some(parent_hash)` means that the `current_hash` is in the `parents`
// container `is_visited_before` means that it has been visited before in
// some of previous calls => since we assume that previous call has finished
// with `true`, this also will be finished with `true`
return Ok(self)
}
*parent_hash
}
},
None => return Err(Error::PrecommitIsNotCommitDescendant),
};
}
@@ -20,24 +20,21 @@
#![cfg_attr(not(feature = "std"), no_std)]
use codec::{Codec, Decode, Encode, EncodeLike};
use core::clone::Clone;
use core::cmp::Eq;
use core::default::Default;
use core::fmt::Debug;
use core::{clone::Clone, cmp::Eq, default::Default, fmt::Debug};
use scale_info::TypeInfo;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use sp_finality_grandpa::{AuthorityList, ConsensusLog, SetId, GRANDPA_ENGINE_ID};
use sp_runtime::RuntimeDebug;
use sp_runtime::{generic::OpaqueDigestItemId, traits::Header as HeaderT};
use sp_runtime::{generic::OpaqueDigestItemId, traits::Header as HeaderT, RuntimeDebug};
use sp_std::boxed::Box;
pub mod justification;
/// A type that can be used as a parameter in a dispatchable function.
///
/// When using `decl_module` all arguments for call functions must implement this trait.
pub trait Parameter: Codec + EncodeLike + Clone + Eq + Debug {}
impl<T> Parameter for T where T: Codec + EncodeLike + Clone + Eq + Debug {}
pub trait Parameter: Codec + EncodeLike + Clone + Eq + Debug + TypeInfo {}
impl<T> Parameter for T where T: Codec + EncodeLike + Clone + Eq + Debug + TypeInfo {}
/// A GRANDPA Authority List and ID.
#[derive(Default, Encode, Decode, RuntimeDebug, PartialEq, Clone, TypeInfo)]
@@ -63,7 +60,7 @@ impl AuthoritySet {
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct InitializationData<H: HeaderT> {
/// The header from which we should start syncing.
pub header: H,
pub header: Box<H>,
/// The initial authorities of the pallet.
pub authority_list: AuthorityList,
/// The ID of the initial authority set.
@@ -82,7 +79,9 @@ pub trait InclusionProofVerifier {
/// Verify that transaction is a part of given block.
///
/// Returns Some(transaction) if proof is valid and None otherwise.
fn verify_transaction_inclusion_proof(proof: &Self::TransactionInclusionProof) -> Option<Self::Transaction>;
fn verify_transaction_inclusion_proof(
proof: &Self::TransactionInclusionProof,
) -> Option<Self::Transaction>;
}
/// A trait for pallets which want to keep track of finalized headers from a bridged chain.
@@ -23,8 +23,8 @@
use assert_matches::assert_matches;
use bp_header_chain::justification::{verify_justification, Error, GrandpaJustification};
use bp_test_utils::{
header_id, make_justification_for_header, signed_precommit, test_header, Account, JustificationGeneratorParams,
ALICE, BOB, CHARLIE, DAVE, EVE, TEST_GRANDPA_SET_ID,
header_id, make_justification_for_header, signed_precommit, test_header, Account,
JustificationGeneratorParams, ALICE, BOB, CHARLIE, DAVE, EVE, TEST_GRANDPA_SET_ID,
};
use finality_grandpa::voter_set::VoterSet;
use sp_finality_grandpa::{AuthorityId, AuthorityWeight};
@@ -44,18 +44,22 @@ impl AncestryChain {
}
impl finality_grandpa::Chain<TestHash, TestNumber> for AncestryChain {
fn ancestry(&self, base: TestHash, block: TestHash) -> Result<Vec<TestHash>, finality_grandpa::Error> {
fn ancestry(
&self,
base: TestHash,
block: TestHash,
) -> Result<Vec<TestHash>, finality_grandpa::Error> {
let mut route = Vec::new();
let mut current_hash = block;
loop {
if current_hash == base {
break;
break
}
match self.0.parents.get(&current_hash).cloned() {
Some(parent_hash) => {
current_hash = parent_hash;
route.push(current_hash);
}
},
_ => return Err(finality_grandpa::Error::NotDescendent),
}
}
@@ -81,14 +85,11 @@ fn minimal_accounts_set() -> Vec<(Account, AuthorityWeight)> {
vec![(ALICE, 1), (BOB, 1), (CHARLIE, 1), (DAVE, 1)]
}
/// Get a minimal subset of GRANDPA authorities that have enough cumulative vote weight to justify a header finality.
/// Get a minimal subset of GRANDPA authorities that have enough cumulative vote weight to justify a
/// header finality.
pub fn minimal_voter_set() -> VoterSet<AuthorityId> {
VoterSet::new(
minimal_accounts_set()
.iter()
.map(|(id, w)| (AuthorityId::from(*id), *w)),
)
.unwrap()
VoterSet::new(minimal_accounts_set().iter().map(|(id, w)| (AuthorityId::from(*id), *w)))
.unwrap()
}
/// Make a valid GRANDPA justification with sensible defaults.
@@ -174,14 +175,8 @@ fn same_result_when_justification_contains_duplicate_vote() {
let mut justification = make_default_justification(&test_header(1));
// the justification may contain exactly the same vote (i.e. same precommit and same signature)
// multiple times && it isn't treated as an error by original implementation
justification
.commit
.precommits
.push(justification.commit.precommits[0].clone());
justification
.commit
.precommits
.push(justification.commit.precommits[0].clone());
justification.commit.precommits.push(justification.commit.precommits[0].clone());
justification.commit.precommits.push(justification.commit.precommits[0].clone());
// our implementation succeeds
assert_eq!(
@@ -8,13 +8,13 @@ license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
[dependencies]
bp-runtime = { path = "../runtime", default-features = false }
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false }
codec = { package = "parity-scale-codec", version = "2.2.0", default-features = false }
scale-info = { version = "1.0", default-features = false, features = ["derive"] }
# Substrate Dependencies
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
[features]
default = ["std"]
@@ -35,7 +35,7 @@ pub type Weight = u64;
pub type SpecVersion = u32;
/// A generic trait to dispatch arbitrary messages delivered over the bridge.
pub trait MessageDispatch<AccountId, MessageId> {
pub trait MessageDispatch<AccountId, BridgeMessageId> {
/// A type of the message to be dispatched.
type Message: codec::Decode;
@@ -61,7 +61,7 @@ pub trait MessageDispatch<AccountId, MessageId> {
fn dispatch<P: FnOnce(&AccountId, Weight) -> Result<(), ()>>(
source_chain: ChainId,
target_chain: ChainId,
id: MessageId,
id: BridgeMessageId,
message: Result<Self::Message, ()>,
pay_dispatch_fee: P,
) -> MessageDispatchResult;
@@ -78,7 +78,7 @@ pub enum CallOrigin<SourceChainAccountId, TargetChainAccountPublic, TargetChainS
/// from a derived account.
///
/// The derived account represents the source Root account on the target chain. This is useful
/// if the target chain needs some way of knowing that a call came from a priviledged origin on
/// if the target chain needs some way of knowing that a call came from a privileged origin on
/// the source chain (maybe to allow a configuration change for example).
SourceRoot,
@@ -113,7 +113,12 @@ pub enum CallOrigin<SourceChainAccountId, TargetChainAccountPublic, TargetChainS
/// Message payload type used by dispatch module.
#[derive(RuntimeDebug, Encode, Decode, Clone, PartialEq, Eq, TypeInfo)]
pub struct MessagePayload<SourceChainAccountId, TargetChainAccountPublic, TargetChainSignature, Call> {
pub struct MessagePayload<
SourceChainAccountId,
TargetChainAccountPublic,
TargetChainSignature,
Call,
> {
/// Runtime specification version. We only dispatch messages that have the same
/// runtime version. Otherwise we risk to misinterpret encoded calls.
pub spec_version: SpecVersion,
@@ -8,10 +8,10 @@ license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
[dependencies]
bitvec = { version = "0.20", default-features = false, features = ["alloc"] }
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive", "bit-vec"] }
codec = { package = "parity-scale-codec", version = "2.2.0", default-features = false, features = ["derive", "bit-vec"] }
impl-trait-for-tuples = "0.2"
scale-info = { version = "1.0", default-features = false, features = ["derive"] }
serde = { version = "1.0.101", optional = true, features = ["derive"] }
scale-info = { version = "1.0", default-features = false, features = ["bit-vec", "derive"] }
serde = { version = "1.0", optional = true, features = ["derive"] }
# Bridge dependencies
@@ -19,9 +19,9 @@ bp-runtime = { path = "../runtime", default-features = false }
# Substrate Dependencies
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
[features]
default = ["std"]
+51 -37
View File
@@ -76,7 +76,7 @@ pub type LaneId = [u8; 4];
pub type MessageNonce = u64;
/// Message id as a tuple.
pub type MessageId = (LaneId, MessageNonce);
pub type BridgeMessageId = (LaneId, MessageNonce);
/// Opaque message payload. We only decode this payload when it is dispatched.
pub type MessagePayload = Vec<u8>;
@@ -111,22 +111,23 @@ pub struct Message<Fee> {
/// Inbound lane data.
#[derive(Encode, Decode, Clone, RuntimeDebug, PartialEq, Eq, TypeInfo)]
pub struct InboundLaneData<RelayerId> {
/// Identifiers of relayers and messages that they have delivered to this lane (ordered by message nonce).
/// Identifiers of relayers and messages that they have delivered to this lane (ordered by
/// message nonce).
///
/// This serves as a helper storage item, to allow the source chain to easily pay rewards
/// to the relayers who succesfuly delivered messages to the target chain (inbound lane).
/// to the relayers who successfully delivered messages to the target chain (inbound lane).
///
/// It is guaranteed to have at most N entries, where N is configured at the module level.
/// If there are N entries in this vec, then:
/// 1) all incoming messages are rejected if they're missing corresponding `proof-of(outbound-lane.state)`;
/// 2) all incoming messages are rejected if `proof-of(outbound-lane.state).last_delivered_nonce` is
/// equal to `self.last_confirmed_nonce`.
/// Given what is said above, all nonces in this queue are in range:
/// `(self.last_confirmed_nonce; self.last_delivered_nonce()]`.
/// 1) all incoming messages are rejected if they're missing corresponding
/// `proof-of(outbound-lane.state)`; 2) all incoming messages are rejected if
/// `proof-of(outbound-lane.state).last_delivered_nonce` is equal to
/// `self.last_confirmed_nonce`. Given what is said above, all nonces in this queue are in
/// range: `(self.last_confirmed_nonce; self.last_delivered_nonce()]`.
///
/// When a relayer sends a single message, both of MessageNonces are the same.
/// When relayer sends messages in a batch, the first arg is the lowest nonce, second arg the highest nonce.
/// Multiple dispatches from the same relayer are allowed.
/// When relayer sends messages in a batch, the first arg is the lowest nonce, second arg the
/// highest nonce. Multiple dispatches from the same relayer are allowed.
pub relayers: VecDeque<UnrewardedRelayer<RelayerId>>,
/// Nonce of the last message that
@@ -142,24 +143,26 @@ pub struct InboundLaneData<RelayerId> {
impl<RelayerId> Default for InboundLaneData<RelayerId> {
fn default() -> Self {
InboundLaneData {
relayers: VecDeque::new(),
last_confirmed_nonce: 0,
}
InboundLaneData { relayers: VecDeque::new(), last_confirmed_nonce: 0 }
}
}
impl<RelayerId> InboundLaneData<RelayerId> {
/// Returns approximate size of the struct, given number of entries in the `relayers` set and
/// Returns approximate size of the struct, given a number of entries in the `relayers` set and
/// size of each entry.
///
/// Returns `None` if size overflows `u32` limits.
pub fn encoded_size_hint(relayer_id_encoded_size: u32, relayers_entries: u32, messages_count: u32) -> Option<u32> {
pub fn encoded_size_hint(
relayer_id_encoded_size: u32,
relayers_entries: u32,
messages_count: u32,
) -> Option<u32> {
let message_nonce_size = 8;
let relayers_entry_size = relayer_id_encoded_size.checked_add(2 * message_nonce_size)?;
let relayers_size = relayers_entries.checked_mul(relayers_entry_size)?;
let dispatch_results_per_byte = 8;
let dispatch_result_size = sp_std::cmp::max(relayers_entries, messages_count / dispatch_results_per_byte);
let dispatch_result_size =
sp_std::cmp::max(relayers_entries, messages_count / dispatch_results_per_byte);
relayers_size
.checked_add(message_nonce_size)
.and_then(|result| result.checked_add(dispatch_result_size))
@@ -194,8 +197,8 @@ pub type DispatchResultsBitVec = BitVec<Msb0, u8>;
/// Unrewarded relayer entry stored in the inbound lane data.
///
/// This struct represents a continuous range of messages that have been delivered by the same relayer
/// and whose confirmations are still pending.
/// This struct represents a continuous range of messages that have been delivered by the same
/// relayer and whose confirmations are still pending.
#[derive(Encode, Decode, Clone, RuntimeDebug, PartialEq, Eq, TypeInfo)]
pub struct UnrewardedRelayer<RelayerId> {
/// Identifier of the relayer.
@@ -218,7 +221,8 @@ pub struct DeliveredMessages {
}
impl DeliveredMessages {
/// Create new `DeliveredMessages` struct that confirms delivery of single nonce with given dispatch result.
/// Create new `DeliveredMessages` struct that confirms delivery of single nonce with given
/// dispatch result.
pub fn new(nonce: MessageNonce, dispatch_result: bool) -> Self {
DeliveredMessages {
begin: nonce,
@@ -227,6 +231,15 @@ impl DeliveredMessages {
}
}
/// Return total count of delivered messages.
pub fn total_messages(&self) -> MessageNonce {
if self.end >= self.begin {
self.end - self.begin + 1
} else {
0
}
}
/// Note new dispatched message.
pub fn note_dispatched_message(&mut self, dispatch_result: bool) {
self.end += 1;
@@ -269,19 +282,20 @@ pub struct UnrewardedRelayersState {
/// Outbound lane data.
#[derive(Encode, Decode, Clone, RuntimeDebug, PartialEq, Eq, TypeInfo)]
pub struct OutboundLaneData {
/// Nonce of oldest message that we haven't yet pruned. May point to not-yet-generated message if
/// all sent messages are already pruned.
/// Nonce of the oldest message that we haven't yet pruned. May point to not-yet-generated
/// message if all sent messages are already pruned.
pub oldest_unpruned_nonce: MessageNonce,
/// Nonce of latest message, received by bridged chain.
/// Nonce of the latest message, received by bridged chain.
pub latest_received_nonce: MessageNonce,
/// Nonce of latest message, generated by us.
/// Nonce of the latest message, generated by us.
pub latest_generated_nonce: MessageNonce,
}
impl Default for OutboundLaneData {
fn default() -> Self {
OutboundLaneData {
// it is 1 because we're pruning everything in [oldest_unpruned_nonce; latest_received_nonce]
// it is 1 because we're pruning everything in [oldest_unpruned_nonce;
// latest_received_nonce]
oldest_unpruned_nonce: 1,
latest_received_nonce: 0,
latest_generated_nonce: 0,
@@ -292,7 +306,9 @@ impl Default for OutboundLaneData {
/// Returns total number of messages in the `InboundLaneData::relayers` vector.
///
/// Returns `None` if there are more messages that `MessageNonce` may fit (i.e. `MessageNonce + 1`).
pub fn total_unrewarded_messages<RelayerId>(relayers: &VecDeque<UnrewardedRelayer<RelayerId>>) -> Option<MessageNonce> {
pub fn total_unrewarded_messages<RelayerId>(
relayers: &VecDeque<UnrewardedRelayer<RelayerId>>,
) -> Option<MessageNonce> {
match (relayers.front(), relayers.back()) {
(Some(front), Some(back)) => {
if let Some(difference) = back.messages.end.checked_sub(front.messages.begin) {
@@ -300,7 +316,7 @@ pub fn total_unrewarded_messages<RelayerId>(relayers: &VecDeque<UnrewardedRelaye
} else {
Some(0)
}
}
},
_ => Some(0),
}
}
@@ -314,10 +330,7 @@ mod tests {
assert_eq!(
total_unrewarded_messages(
&vec![
UnrewardedRelayer {
relayer: 1,
messages: DeliveredMessages::new(0, true)
},
UnrewardedRelayer { relayer: 1, messages: DeliveredMessages::new(0, true) },
UnrewardedRelayer {
relayer: 2,
messages: DeliveredMessages::new(MessageNonce::MAX, true)
@@ -341,7 +354,11 @@ mod tests {
(13u8, 128u8),
];
for (relayer_entries, messages_count) in test_cases {
let expected_size = InboundLaneData::<u8>::encoded_size_hint(1, relayer_entries as _, messages_count as _);
let expected_size = InboundLaneData::<u8>::encoded_size_hint(
1,
relayer_entries as _,
messages_count as _,
);
let actual_size = InboundLaneData {
relayers: (1u8..=relayer_entries)
.map(|i| {
@@ -375,11 +392,8 @@ mod tests {
#[test]
fn message_dispatch_result_works() {
let delivered_messages = DeliveredMessages {
begin: 100,
end: 150,
dispatch_results: bitvec![Msb0, u8; 1; 151],
};
let delivered_messages =
DeliveredMessages { begin: 100, end: 150, dispatch_results: bitvec![Msb0, u8; 1; 151] };
assert!(!delivered_messages.contains_message(99));
assert!(delivered_messages.contains_message(100));
@@ -18,9 +18,14 @@
use crate::{DeliveredMessages, InboundLaneData, LaneId, MessageNonce, OutboundLaneData};
use crate::UnrewardedRelayer;
use bp_runtime::Size;
use frame_support::{Parameter, RuntimeDebug};
use sp_std::{collections::btree_map::BTreeMap, fmt::Debug};
use frame_support::{weights::Weight, Parameter, RuntimeDebug};
use sp_std::{
collections::{btree_map::BTreeMap, vec_deque::VecDeque},
fmt::Debug,
ops::RangeInclusive,
};
/// The sender of the message on the source chain.
pub type Sender<AccountId> = frame_system::RawOrigin<AccountId>;
@@ -56,14 +61,14 @@ pub trait TargetHeaderChain<Payload, AccountId> {
///
/// The proper implementation must ensure that the delivery-transaction with this
/// payload would (at least) be accepted into target chain transaction pool AND
/// eventually will be successfully 'mined'. The most obvious incorrect implementation
/// eventually will be successfully mined. The most obvious incorrect implementation
/// example would be implementation for BTC chain that accepts payloads larger than
/// 1MB. BTC nodes aren't accepting transactions that are larger than 1MB, so relayer
/// will be unable to craft valid transaction => this (and all subsequent) messages will
/// never be delivered.
fn verify_message(payload: &Payload) -> Result<(), Self::Error>;
/// Verify messages delivery proof and return lane && nonce of the latest recevied message.
/// Verify messages delivery proof and return lane && nonce of the latest received message.
fn verify_messages_delivery_proof(
proof: Self::MessagesDeliveryProof,
) -> Result<(LaneId, InboundLaneData<AccountId>), Self::Error>;
@@ -81,7 +86,8 @@ pub trait LaneMessageVerifier<Submitter, Payload, Fee> {
/// Error type.
type Error: Debug + Into<&'static str>;
/// Verify message payload and return Ok(()) if message is valid and allowed to be sent over the lane.
/// Verify message payload and return Ok(()) if message is valid and allowed to be sent over the
/// lane.
fn verify_message(
submitter: &Sender<Submitter>,
delivery_and_dispatch_fee: &Fee,
@@ -95,14 +101,14 @@ pub trait LaneMessageVerifier<Submitter, Payload, Fee> {
/// submitter is paying (in source chain tokens/assets) for:
///
/// 1) submit-message-transaction-fee itself. This fee is not included in the
/// `delivery_and_dispatch_fee` and is witheld by the regular transaction payment mechanism;
/// `delivery_and_dispatch_fee` and is withheld by the regular transaction payment mechanism;
/// 2) message-delivery-transaction-fee. It is submitted to the target node by relayer;
/// 3) message-dispatch fee. It is paid by relayer for processing message by target chain;
/// 4) message-receiving-delivery-transaction-fee. It is submitted to the source node
/// by relayer.
///
/// So to be sure that any non-altruist relayer would agree to deliver message, submitter
/// should set `delivery_and_dispatch_fee` to at least (equialent of): sum of fees from (2)
/// should set `delivery_and_dispatch_fee` to at least (equivalent of): sum of fees from (2)
/// to (4) above, plus some interest for the relayer.
pub trait MessageDeliveryAndDispatchPayment<AccountId, Balance> {
/// Error type.
@@ -121,27 +127,98 @@ pub trait MessageDeliveryAndDispatchPayment<AccountId, Balance> {
/// The implementation may also choose to pay reward to the `confirmation_relayer`, which is
/// a relayer that has submitted delivery confirmation transaction.
fn pay_relayers_rewards(
lane_id: LaneId,
messages_relayers: VecDeque<UnrewardedRelayer<AccountId>>,
confirmation_relayer: &AccountId,
relayers_rewards: RelayersRewards<AccountId, Balance>,
received_range: &RangeInclusive<MessageNonce>,
relayer_fund_account: &AccountId,
);
}
/// Perform some initialization in externalities-provided environment.
/// Send message artifacts.
#[derive(RuntimeDebug, PartialEq)]
pub struct SendMessageArtifacts {
/// Nonce of the message.
pub nonce: MessageNonce,
/// Actual weight of send message call.
pub weight: Weight,
}
/// Messages bridge API to be used from other pallets.
pub trait MessagesBridge<AccountId, Balance, Payload> {
/// Error type.
type Error: Debug;
/// Send message over the bridge.
///
/// For instance you may ensure that particular required accounts or storage items are present.
/// Returns the number of storage reads performed.
fn initialize(_relayer_fund_account: &AccountId) -> usize {
0
/// Returns unique message nonce or error if send has failed.
fn send_message(
sender: Sender<AccountId>,
lane: LaneId,
message: Payload,
delivery_and_dispatch_fee: Balance,
) -> Result<SendMessageArtifacts, Self::Error>;
}
/// Bridge that does nothing when message is being sent.
#[derive(RuntimeDebug, PartialEq)]
pub struct NoopMessagesBridge;
impl<AccountId, Balance, Payload> MessagesBridge<AccountId, Balance, Payload>
for NoopMessagesBridge
{
type Error = &'static str;
fn send_message(
_sender: Sender<AccountId>,
_lane: LaneId,
_message: Payload,
_delivery_and_dispatch_fee: Balance,
) -> Result<SendMessageArtifacts, Self::Error> {
Ok(SendMessageArtifacts { nonce: 0, weight: 0 })
}
}
/// Handler for messages delivery confirmation.
#[impl_trait_for_tuples::impl_for_tuples(30)]
pub trait OnDeliveryConfirmed {
/// Called when we receive confirmation that our messages have been delivered to the
/// target chain. The confirmation also has single bit dispatch result for every
/// confirmed message (see `DeliveredMessages` for details).
fn on_messages_delivered(_lane: &LaneId, _messages: &DeliveredMessages) {}
/// confirmed message (see `DeliveredMessages` for details). Guaranteed to be called
/// only when at least one message is delivered.
///
/// Should return total weight consumed by the call.
///
/// NOTE: messages pallet assumes that maximal weight that may be spent on processing
/// single message is single DB read + single DB write. So this function shall never
/// return weight that is larger than total number of messages * (db read + db write).
/// If your pallet needs more time for processing single message, please do it
/// from `on_initialize` call(s) of the next block(s).
fn on_messages_delivered(_lane: &LaneId, _messages: &DeliveredMessages) -> Weight;
}
#[impl_trait_for_tuples::impl_for_tuples(30)]
impl OnDeliveryConfirmed for Tuple {
fn on_messages_delivered(lane: &LaneId, messages: &DeliveredMessages) -> Weight {
let mut total_weight: Weight = 0;
for_tuples!(
#(
total_weight = total_weight.saturating_add(Tuple::on_messages_delivered(lane, messages));
)*
);
total_weight
}
}
/// Handler for messages have been accepted
pub trait OnMessageAccepted {
/// Called when a message has been accepted by message pallet.
fn on_messages_accepted(lane: &LaneId, message: &MessageNonce) -> Weight;
}
impl OnMessageAccepted for () {
fn on_messages_accepted(_lane: &LaneId, _message: &MessageNonce) -> Weight {
0
}
}
/// Structure that may be used in place of `TargetHeaderChain`, `LaneMessageVerifier` and
@@ -149,7 +226,8 @@ pub trait OnDeliveryConfirmed {
pub struct ForbidOutboundMessages;
/// Error message that is used in `ForbidOutboundMessages` implementation.
const ALL_OUTBOUND_MESSAGES_REJECTED: &str = "This chain is configured to reject all outbound messages";
const ALL_OUTBOUND_MESSAGES_REJECTED: &str =
"This chain is configured to reject all outbound messages";
impl<Payload, AccountId> TargetHeaderChain<Payload, AccountId> for ForbidOutboundMessages {
type Error = &'static str;
@@ -167,7 +245,9 @@ impl<Payload, AccountId> TargetHeaderChain<Payload, AccountId> for ForbidOutboun
}
}
impl<Submitter, Payload, Fee> LaneMessageVerifier<Submitter, Payload, Fee> for ForbidOutboundMessages {
impl<Submitter, Payload, Fee> LaneMessageVerifier<Submitter, Payload, Fee>
for ForbidOutboundMessages
{
type Error = &'static str;
fn verify_message(
@@ -181,7 +261,9 @@ impl<Submitter, Payload, Fee> LaneMessageVerifier<Submitter, Payload, Fee> for F
}
}
impl<AccountId, Balance> MessageDeliveryAndDispatchPayment<AccountId, Balance> for ForbidOutboundMessages {
impl<AccountId, Balance> MessageDeliveryAndDispatchPayment<AccountId, Balance>
for ForbidOutboundMessages
{
type Error = &'static str;
fn pay_delivery_and_dispatch_fee(
@@ -193,8 +275,10 @@ impl<AccountId, Balance> MessageDeliveryAndDispatchPayment<AccountId, Balance> f
}
fn pay_relayers_rewards(
_lane_id: LaneId,
_messages_relayers: VecDeque<UnrewardedRelayer<AccountId>>,
_confirmation_relayer: &AccountId,
_relayers_rewards: RelayersRewards<AccountId, Balance>,
_received_range: &RangeInclusive<MessageNonce>,
_relayer_fund_account: &AccountId,
) {
}
@@ -76,7 +76,7 @@ pub trait SourceHeaderChain<Fee> {
/// messages will be rejected.
///
/// The `messages_count` argument verification (sane limits) is supposed to be made
/// outside of this function. This function only verifies that the proof declares exactly
/// outside this function. This function only verifies that the proof declares exactly
/// `messages_count` messages.
fn verify_messages_proof(
proof: Self::MessagesProof,
@@ -112,23 +112,19 @@ pub trait MessageDispatch<AccountId, Fee> {
impl<Message> Default for ProvedLaneMessages<Message> {
fn default() -> Self {
ProvedLaneMessages {
lane_state: None,
messages: Vec::new(),
}
ProvedLaneMessages { lane_state: None, messages: Vec::new() }
}
}
impl<DispatchPayload: Decode, Fee> From<Message<Fee>> for DispatchMessage<DispatchPayload, Fee> {
fn from(message: Message<Fee>) -> Self {
DispatchMessage {
key: message.key,
data: message.data.into(),
}
DispatchMessage { key: message.key, data: message.data.into() }
}
}
impl<DispatchPayload: Decode, Fee> From<MessageData<Fee>> for DispatchMessageData<DispatchPayload, Fee> {
impl<DispatchPayload: Decode, Fee> From<MessageData<Fee>>
for DispatchMessageData<DispatchPayload, Fee>
{
fn from(data: MessageData<Fee>) -> Self {
DispatchMessageData {
payload: DispatchPayload::decode(&mut &data.payload[..]),
@@ -142,7 +138,8 @@ impl<DispatchPayload: Decode, Fee> From<MessageData<Fee>> for DispatchMessageDat
pub struct ForbidInboundMessages;
/// Error message that is used in `ForbidOutboundMessages` implementation.
const ALL_INBOUND_MESSAGES_REJECTED: &str = "This chain is configured to reject all inbound messages";
const ALL_INBOUND_MESSAGES_REJECTED: &str =
"This chain is configured to reject all inbound messages";
impl<Fee> SourceHeaderChain<Fee> for ForbidInboundMessages {
type Error = &'static str;
@@ -163,7 +160,10 @@ impl<AccountId, Fee> MessageDispatch<AccountId, Fee> for ForbidInboundMessages {
Weight::MAX
}
fn dispatch(_: &AccountId, _: DispatchMessage<Self::DispatchPayload, Fee>) -> MessageDispatchResult {
fn dispatch(
_: &AccountId,
_: DispatchMessage<Self::DispatchPayload, Fee>,
) -> MessageDispatchResult {
MessageDispatchResult {
dispatch_result: false,
unspent_weight: 0,
@@ -7,7 +7,7 @@ edition = "2018"
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
[dependencies]
parity-scale-codec = { version = "2.0.0", default-features = false, features = ["derive"] }
parity-scale-codec = { version = "2.2.0", default-features = false, features = ["derive"] }
scale-info = { version = "1.0", default-features = false, features = ["derive"] }
# Bridge Dependencies
@@ -17,9 +17,9 @@ bp-runtime = { path = "../runtime", default-features = false }
# Substrate Based Dependencies
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
@@ -29,6 +29,7 @@ use frame_support::{
};
use frame_system::limits;
use parity_scale_codec::Compact;
use scale_info::{StaticTypeInfo, TypeInfo};
use sp_core::Hasher as HasherT;
use sp_runtime::{
generic,
@@ -66,18 +67,19 @@ pub const EXTRA_STORAGE_PROOF_SIZE: u32 = 1024;
/// All polkadot-like chains are using same crypto.
pub const MAXIMAL_ENCODED_ACCOUNT_ID_SIZE: u32 = 32;
/// All Polkadot-like chains allow normal extrinsics to fill block up to 75%.
/// All Polkadot-like chains allow normal extrinsics to fill block up to 75 percent.
///
/// This is a copy-paste from the Polkadot repo's `polkadot-runtime-common` crate.
const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
/// All Polkadot-like chains allow 2 seconds of compute with a 6 second average block time.
/// All Polkadot-like chains allow 2 seconds of compute with a 6-second average block time.
///
/// This is a copy-paste from the Polkadot repo's `polkadot-runtime-common` crate.
pub const MAXIMUM_BLOCK_WEIGHT: Weight = 2 * WEIGHT_PER_SECOND;
/// All Polkadot-like chains assume that an on-initialize consumes 1% of the weight on average,
/// hence a single extrinsic will not be allowed to consume more than `AvailableBlockRatio - 1%`.
/// All Polkadot-like chains assume that an on-initialize consumes 1 percent of the weight on
/// average, hence a single extrinsic will not be allowed to consume more than
/// `AvailableBlockRatio - 1 percent`.
///
/// This is a copy-paste from the Polkadot repo's `polkadot-runtime-common` crate.
pub const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(1);
@@ -113,7 +115,8 @@ parameter_types! {
.build_or_panic();
}
/// Get the maximum weight (compute time) that a Normal extrinsic on the Polkadot-like chain can use.
/// Get the maximum weight (compute time) that a Normal extrinsic on the Polkadot-like chain can
/// use.
pub fn max_extrinsic_weight() -> Weight {
BlockWeights::get()
.get(DispatchClass::Normal)
@@ -138,6 +141,48 @@ pub const MAX_UNREWARDED_RELAYER_ENTRIES_AT_INBOUND_LANE: MessageNonce = 128;
/// Maximal number of unconfirmed messages at inbound lane.
pub const MAX_UNCONFIRMED_MESSAGES_AT_INBOUND_LANE: MessageNonce = 8192;
// One important thing about weight-related constants here is that actually we may have
// different weights on different Polkadot-like chains. But now all deployments are
// almost the same, so we're exporting constants from this crate.
/// Maximal weight of single message delivery confirmation transaction on Polkadot-like chain.
///
/// This value is a result of `pallet_bridge_messages::Pallet::receive_messages_delivery_proof`
/// weight formula computation for the case when single message is confirmed. The result then must
/// be rounded up to account possible future runtime upgrades.
pub const MAX_SINGLE_MESSAGE_DELIVERY_CONFIRMATION_TX_WEIGHT: Weight = 2_000_000_000;
/// Increase of delivery transaction weight on Polkadot-like chain with every additional message
/// byte.
///
/// This value is a result of
/// `pallet_bridge_messages::WeightInfoExt::storage_proof_size_overhead(1)` call. The result then
/// must be rounded up to account possible future runtime upgrades.
pub const ADDITIONAL_MESSAGE_BYTE_DELIVERY_WEIGHT: Weight = 25_000;
/// Maximal number of bytes, included in the signed Polkadot-like transaction apart from the encoded
/// call itself.
///
/// Can be computed by subtracting encoded call size from raw transaction size.
pub const TX_EXTRA_BYTES: u32 = 256;
/// Weight of single regular message delivery transaction on Polkadot-like chain.
///
/// This value is a result of `pallet_bridge_messages::Pallet::receive_messages_proof_weight()` call
/// for the case when single message of `pallet_bridge_messages::EXPECTED_DEFAULT_MESSAGE_LENGTH`
/// bytes is delivered. The message must have dispatch weight set to zero. The result then must be
/// rounded up to account possible future runtime upgrades.
pub const DEFAULT_MESSAGE_DELIVERY_TX_WEIGHT: Weight = 1_500_000_000;
/// Weight of pay-dispatch-fee operation for inbound messages at Polkadot-like chain.
///
/// This value corresponds to the result of
/// `pallet_bridge_messages::WeightInfoExt::pay_inbound_dispatch_fee_overhead()` call for your
/// chain. Don't put too much reserve there, because it is used to **decrease**
/// `DEFAULT_MESSAGE_DELIVERY_TX_WEIGHT` cost. So putting large reserve would make delivery
/// transactions cheaper.
pub const PAY_INBOUND_DISPATCH_FEE_WEIGHT: Weight = 600_000_000;
/// Re-export `time_units` to make usage easier.
pub use time_units::*;
@@ -165,7 +210,7 @@ pub type Index = u32;
/// Hashing type.
pub type Hashing = BlakeTwo256;
/// The type of an object that can produce hashes on Polkadot-like chains.
/// The type of object that can produce hashes on Polkadot-like chains.
pub type Hasher = BlakeTwo256;
/// The header type used by Polkadot-like chains.
@@ -180,6 +225,9 @@ pub type AccountPublic = <Signature as Verify>::Signer;
/// Id of account on Polkadot-like chains.
pub type AccountId = <AccountPublic as IdentifyAccount>::AccountId;
/// Address of account on Polkadot-like chains.
pub type AccountAddress = MultiAddress<AccountId, ()>;
/// Index of a transaction on the Polkadot-like chains.
pub type Nonce = u32;
@@ -194,18 +242,13 @@ pub type Balance = u128;
/// Unchecked Extrinsic type.
pub type UncheckedExtrinsic<Call> =
generic::UncheckedExtrinsic<MultiAddress<AccountId, ()>, Call, Signature, SignedExtensions<Call>>;
generic::UncheckedExtrinsic<AccountAddress, Call, Signature, SignedExtensions<Call>>;
/// Account address, used by the Polkadot-like chain.
pub type Address = MultiAddress<AccountId, ()>;
/// A type of the data encoded as part of the transaction.
pub type SignedExtra = (
(),
(),
(),
sp_runtime::generic::Era,
Compact<Nonce>,
(),
Compact<Balance>,
);
pub type SignedExtra = ((), (), (), sp_runtime::generic::Era, Compact<Nonce>, (), Compact<Balance>);
/// Parameters which are part of the payload used to produce transaction signature,
/// but don't end up in the transaction itself (i.e. inherent part of the runtime).
@@ -213,7 +256,7 @@ pub type AdditionalSigned = (u32, u32, Hash, Hash, (), (), ());
/// A simplified version of signed extensions meant for producing signed transactions
/// and signed payload in the client code.
#[derive(PartialEq, Eq, Clone, RuntimeDebug, scale_info::TypeInfo)]
#[derive(PartialEq, Eq, Clone, RuntimeDebug, TypeInfo)]
pub struct SignedExtensions<Call> {
encode_payload: SignedExtra,
additional_signed: AdditionalSigned,
@@ -227,7 +270,9 @@ impl<Call> parity_scale_codec::Encode for SignedExtensions<Call> {
}
impl<Call> parity_scale_codec::Decode for SignedExtensions<Call> {
fn decode<I: parity_scale_codec::Input>(_input: &mut I) -> Result<Self, parity_scale_codec::Error> {
fn decode<I: parity_scale_codec::Input>(
_input: &mut I,
) -> Result<Self, parity_scale_codec::Error> {
unimplemented!("SignedExtensions are never meant to be decoded, they are only used to create transaction");
}
}
@@ -235,26 +280,26 @@ impl<Call> parity_scale_codec::Decode for SignedExtensions<Call> {
impl<Call> SignedExtensions<Call> {
pub fn new(
version: sp_version::RuntimeVersion,
era: sp_runtime::generic::Era,
era: bp_runtime::TransactionEraOf<PolkadotLike>,
genesis_hash: Hash,
nonce: Nonce,
tip: Balance,
) -> Self {
Self {
encode_payload: (
(), // spec version
(), // tx version
(), // genesis
era, // era
nonce.into(), // nonce (compact encoding)
(), // Check weight
tip.into(), // transaction payment / tip (compact encoding)
(), // spec version
(), // tx version
(), // genesis
era.frame_era(), // era
nonce.into(), // nonce (compact encoding)
(), // Check weight
tip.into(), // transaction payment / tip (compact encoding)
),
additional_signed: (
version.spec_version,
version.transaction_version,
genesis_hash,
genesis_hash,
era.signed_payload(genesis_hash),
(),
(),
(),
@@ -264,6 +309,18 @@ impl<Call> SignedExtensions<Call> {
}
}
impl<Call> SignedExtensions<Call> {
/// Return signer nonce, used to craft transaction.
pub fn nonce(&self) -> Nonce {
self.encode_payload.4.into()
}
/// Return transaction tip.
pub fn tip(&self) -> Balance {
self.encode_payload.6.into()
}
}
impl<Call> sp_runtime::traits::SignedExtension for SignedExtensions<Call>
where
Call: parity_scale_codec::Codec
@@ -273,7 +330,7 @@ where
+ Clone
+ Eq
+ PartialEq
+ scale_info::StaticTypeInfo,
+ StaticTypeInfo,
Call: Dispatchable,
{
const IDENTIFIER: &'static str = "Not needed.";
@@ -283,7 +340,9 @@ where
type AdditionalSigned = AdditionalSigned;
type Pre = ();
fn additional_signed(&self) -> Result<Self::AdditionalSigned, frame_support::unsigned::TransactionValidityError> {
fn additional_signed(
&self,
) -> Result<Self::AdditionalSigned, frame_support::unsigned::TransactionValidityError> {
Ok(self.additional_signed)
}
}
@@ -297,6 +356,11 @@ impl Chain for PolkadotLike {
type Hash = Hash;
type Hasher = Hasher;
type Header = Header;
type AccountId = AccountId;
type Balance = Balance;
type Index = Index;
type Signature = Signature;
}
/// Convert a 256-bit hash into an AccountId.
@@ -311,7 +375,7 @@ impl Convert<sp_core::H256, AccountId> for AccountIdConverter {
/// Return a storage key for account data.
///
/// This is based on FRAME storage-generation code from Substrate:
/// https://github.com/paritytech/substrate/blob/c939ceba381b6313462d47334f775e128ea4e95d/frame/support/src/storage/generator/map.rs#L74
/// [link](https://github.com/paritytech/substrate/blob/c939ceba381b6313462d47334f775e128ea4e95d/frame/support/src/storage/generator/map.rs#L74)
/// The equivalent command to invoke in case full `Runtime` is known is this:
/// `let key = frame_system::Account::<Runtime>::storage_map_final_key(&account_id);`
pub fn account_info_storage_key(id: &AccountId) -> Vec<u8> {
@@ -319,7 +383,9 @@ pub fn account_info_storage_key(id: &AccountId) -> Vec<u8> {
let storage_prefix_hashed = Twox128::hash(b"Account");
let key_hashed = parity_scale_codec::Encode::using_encoded(id, Blake2_128Concat::hash);
let mut final_key = Vec::with_capacity(module_prefix_hashed.len() + storage_prefix_hashed.len() + key_hashed.len());
let mut final_key = Vec::with_capacity(
module_prefix_hashed.len() + storage_prefix_hashed.len() + key_hashed.len(),
);
final_key.extend_from_slice(&module_prefix_hashed[..]);
final_key.extend_from_slice(&storage_prefix_hashed[..]);
@@ -347,8 +413,8 @@ mod tests {
#[test]
fn should_generate_storage_key() {
let acc = [
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,
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,
]
.into();
let key = account_info_storage_key(&acc);
+7 -11
View File
@@ -7,24 +7,20 @@ edition = "2018"
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
[dependencies]
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false }
codec = { package = "parity-scale-codec", version = "2.2.0", default-features = false }
hash-db = { version = "0.15.2", default-features = false }
num-traits = { version = "0.2", default-features = false }
scale-info = { version = "1.0", default-features = false, features = ["derive"] }
# Substrate Dependencies
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-state-machine = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-trie = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
[dev-dependencies]
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-trie = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
[features]
default = ["std"]
@@ -15,12 +15,15 @@
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
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 num_traits::{AsPrimitive, Bounded, CheckedSub, SaturatingAdd, Zero};
use sp_runtime::{
traits::{
AtLeast32Bit, AtLeast32BitUnsigned, Hash as HashT, Header as HeaderT, MaybeDisplay,
MaybeMallocSizeOf, MaybeSerialize, MaybeSerializeDeserialize, Member, SimpleBitOps, Verify,
},
FixedPointOperand,
};
use sp_std::str::FromStr;
use sp_std::{convert::TryFrom, fmt::Debug, hash::Hash, str::FromStr};
/// Minimal Substrate-based chain representation that may be used from no_std environment.
pub trait Chain: Send + Sync + 'static {
@@ -34,7 +37,7 @@ pub trait Chain: Send + Sync + 'static {
type BlockNumber: Parameter
+ Member
+ MaybeSerializeDeserialize
+ sp_std::hash::Hash
+ Hash
+ Copy
+ Default
+ MaybeDisplay
@@ -42,7 +45,10 @@ pub trait Chain: Send + Sync + 'static {
+ FromStr
+ MaybeMallocSizeOf
+ AsPrimitive<usize>
+ Default;
+ Default
// original `sp_runtime::traits::Header::BlockNumber` doesn't have this trait, but
// `sp_runtime::generic::Era` requires block number -> `u64` conversion.
+ Into<u64>;
/// 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`
@@ -51,7 +57,7 @@ pub trait Chain: Send + Sync + 'static {
type Hash: Parameter
+ Member
+ MaybeSerializeDeserialize
+ sp_std::hash::Hash
+ Hash
+ Ord
+ Copy
+ MaybeDisplay
@@ -71,7 +77,48 @@ pub trait Chain: Send + Sync + 'static {
/// 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<Number = Self::BlockNumber, Hash = Self::Hash> + MaybeSerializeDeserialize;
type Header: Parameter
+ HeaderT<Number = Self::BlockNumber, Hash = Self::Hash>
+ MaybeSerializeDeserialize;
/// The user account identifier type for the runtime.
type AccountId: Parameter
+ Member
+ MaybeSerializeDeserialize
+ Debug
+ MaybeDisplay
+ Ord
+ Default;
/// Balance of an account in native tokens.
///
/// The chain may support multiple tokens, but this particular type is for token that is used
/// to pay for transaction dispatch, to reward different relayers (headers, messages), etc.
type Balance: AtLeast32BitUnsigned
+ FixedPointOperand
+ Parameter
+ Parameter
+ Member
+ MaybeSerializeDeserialize
+ Clone
+ Copy
+ Bounded
+ CheckedSub
+ PartialOrd
+ SaturatingAdd
+ Zero
+ TryFrom<sp_core::U256>;
/// Index of a transaction used by the chain.
type Index: Parameter
+ Member
+ MaybeSerialize
+ Debug
+ Default
+ MaybeDisplay
+ MaybeSerializeDeserialize
+ AtLeast32Bit
+ Copy;
/// Signature type, used on this chain.
type Signature: Parameter + Verify;
}
/// Block number used by the chain.
@@ -85,3 +132,21 @@ pub type HasherOf<C> = <C as Chain>::Hasher;
/// Header type used by the chain.
pub type HeaderOf<C> = <C as Chain>::Header;
/// Account id type used by the chain.
pub type AccountIdOf<C> = <C as Chain>::AccountId;
/// Balance type used by the chain.
pub type BalanceOf<C> = <C as Chain>::Balance;
/// Transaction index type used by the chain.
pub type IndexOf<C> = <C as Chain>::Index;
/// Signature type used by the chain.
pub type SignatureOf<C> = <C as Chain>::Signature;
/// Account public type used by the chain.
pub type AccountPublicOf<C> = <SignatureOf<C> as Verify>::Signer;
/// Transaction era used by the chain.
pub type TransactionEraOf<C> = crate::TransactionEra<BlockNumberOf<C>, HashOf<C>>;
+135 -10
View File
@@ -19,11 +19,16 @@
#![cfg_attr(not(feature = "std"), no_std)]
use codec::Encode;
use sp_core::hash::H256;
use frame_support::{RuntimeDebug, StorageHasher};
use sp_core::{hash::H256, storage::StorageKey};
use sp_io::hashing::blake2_256;
use sp_std::convert::TryFrom;
use sp_std::{convert::TryFrom, vec::Vec};
pub use chain::{BlockNumberOf, Chain, HashOf, HasherOf, HeaderOf};
pub use chain::{
AccountIdOf, AccountPublicOf, BalanceOf, BlockNumberOf, Chain, HashOf, HasherOf, HeaderOf,
IndexOf, SignatureOf, TransactionEraOf,
};
pub use frame_support::storage::storage_prefix as storage_value_final_key;
pub use storage_proof::{Error as StorageProofError, StorageProofChecker};
#[cfg(feature = "std")]
@@ -64,19 +69,24 @@ pub const ACCOUNT_DERIVATION_PREFIX: &[u8] = b"pallet-bridge/account-derivation/
/// A unique prefix for entropy when generating a cross-chain account ID for the Root account.
pub const ROOT_ACCOUNT_DERIVATION_PREFIX: &[u8] = b"pallet-bridge/account-derivation/root";
/// Generic header Id.
#[derive(RuntimeDebug, Default, Clone, Copy, Eq, Hash, PartialEq)]
pub struct HeaderId<Hash, Number>(pub Number, pub Hash);
/// Unique identifier of the chain.
///
/// In addition to its main function (identifying the chain), this type may also be used to
/// identify module instance. We have a bunch of pallets that may be used in different bridges. E.g.
/// messages pallet may be deployed twice in the same runtime to bridge ThisChain with Chain1 and Chain2.
/// Sometimes we need to be able to identify deployed instance dynamically. This type may be used for that.
/// messages pallet may be deployed twice in the same runtime to bridge ThisChain with Chain1 and
/// Chain2. Sometimes we need to be able to identify deployed instance dynamically. This type may be
/// used for that.
pub type ChainId = [u8; 4];
/// Type of accounts on the source chain.
pub enum SourceAccount<T> {
/// An account that belongs to Root (privileged origin).
Root,
/// A non-priviledged account.
/// A non-privileged account.
///
/// The embedded account ID may or may not have a private key depending on the "owner" of the
/// account (private key, pallet, proxy, etc.).
@@ -99,8 +109,10 @@ where
AccountId: Encode,
{
match id {
SourceAccount::Root => (ROOT_ACCOUNT_DERIVATION_PREFIX, bridge_id).using_encoded(blake2_256),
SourceAccount::Account(id) => (ACCOUNT_DERIVATION_PREFIX, bridge_id, id).using_encoded(blake2_256),
SourceAccount::Root =>
(ROOT_ACCOUNT_DERIVATION_PREFIX, bridge_id).using_encoded(blake2_256),
SourceAccount::Account(id) =>
(ACCOUNT_DERIVATION_PREFIX, bridge_id, id).using_encoded(blake2_256),
}
.into()
}
@@ -109,8 +121,8 @@ where
///
/// This account is used to collect fees for relayers that are passing messages across the bridge.
///
/// The account ID can be the same across different instances of `pallet-bridge-messages` if the same
/// `bridge_id` is used.
/// The account ID can be the same across different instances of `pallet-bridge-messages` if the
/// same `bridge_id` is used.
pub fn derive_relayer_fund_account_id(bridge_id: ChainId) -> H256 {
("relayer-fund-account", bridge_id).using_encoded(blake2_256).into()
}
@@ -124,6 +136,12 @@ pub trait Size {
fn size_hint(&self) -> u32;
}
impl Size for &[u8] {
fn size_hint(&self) -> u32 {
self.len() as _
}
}
impl Size for () {
fn size_hint(&self) -> u32 {
0
@@ -138,3 +156,110 @@ impl Size for PreComputedSize {
u32::try_from(self.0).unwrap_or(u32::MAX)
}
}
/// Era of specific transaction.
#[derive(RuntimeDebug, Clone, Copy)]
pub enum TransactionEra<BlockNumber, BlockHash> {
/// Transaction is immortal.
Immortal,
/// Transaction is valid for a given number of blocks, starting from given block.
Mortal(HeaderId<BlockHash, BlockNumber>, u32),
}
impl<BlockNumber: Copy + Into<u64>, BlockHash: Copy> TransactionEra<BlockNumber, BlockHash> {
/// Prepare transaction era, based on mortality period and current best block number.
pub fn new(
best_block_id: HeaderId<BlockHash, BlockNumber>,
mortality_period: Option<u32>,
) -> Self {
mortality_period
.map(|mortality_period| TransactionEra::Mortal(best_block_id, mortality_period))
.unwrap_or(TransactionEra::Immortal)
}
/// Create new immortal transaction era.
pub fn immortal() -> Self {
TransactionEra::Immortal
}
/// Returns era that is used by FRAME-based runtimes.
pub fn frame_era(&self) -> sp_runtime::generic::Era {
match *self {
TransactionEra::Immortal => sp_runtime::generic::Era::immortal(),
TransactionEra::Mortal(header_id, period) =>
sp_runtime::generic::Era::mortal(period as _, header_id.0.into()),
}
}
/// Returns header hash that needs to be included in the signature payload.
pub fn signed_payload(&self, genesis_hash: BlockHash) -> BlockHash {
match *self {
TransactionEra::Immortal => genesis_hash,
TransactionEra::Mortal(header_id, _) => header_id.1,
}
}
}
/// This is a copy of the
/// `frame_support::storage::generator::StorageMap::storage_map_final_key` for `Blake2_128Concat`
/// maps.
///
/// We're using it because to call `storage_map_final_key` directly, we need access to the runtime
/// and pallet instance, which (sometimes) is impossible.
pub fn storage_map_final_key_blake2_128concat(
pallet_prefix: &str,
map_name: &str,
key: &[u8],
) -> StorageKey {
storage_map_final_key_identity(
pallet_prefix,
map_name,
&frame_support::Blake2_128Concat::hash(key),
)
}
///
pub fn storage_map_final_key_twox64_concat(
pallet_prefix: &str,
map_name: &str,
key: &[u8],
) -> StorageKey {
storage_map_final_key_identity(pallet_prefix, map_name, &frame_support::Twox64Concat::hash(key))
}
/// This is a copy of the
/// `frame_support::storage::generator::StorageMap::storage_map_final_key` for `Identity` maps.
///
/// We're using it because to call `storage_map_final_key` directly, we need access to the runtime
/// and pallet instance, which (sometimes) is impossible.
pub fn storage_map_final_key_identity(
pallet_prefix: &str,
map_name: &str,
key_hashed: &[u8],
) -> StorageKey {
let pallet_prefix_hashed = frame_support::Twox128::hash(pallet_prefix.as_bytes());
let storage_prefix_hashed = frame_support::Twox128::hash(map_name.as_bytes());
let mut final_key = Vec::with_capacity(
pallet_prefix_hashed.len() + storage_prefix_hashed.len() + key_hashed.len(),
);
final_key.extend_from_slice(&pallet_prefix_hashed[..]);
final_key.extend_from_slice(&storage_prefix_hashed[..]);
final_key.extend_from_slice(key_hashed.as_ref());
StorageKey(final_key)
}
/// This is how a storage key of storage parameter (`parameter_types! { storage Param: bool = false;
/// }`) is computed.
///
/// Copied from `frame_support::parameter_types` macro
pub fn storage_parameter_key(parameter_name: &str) -> StorageKey {
let mut buffer = Vec::with_capacity(1 + parameter_name.len() + 1 + 1);
buffer.push(b':');
buffer.extend_from_slice(parameter_name.as_bytes());
buffer.push(b':');
buffer.push(0);
StorageKey(sp_io::hashing::twox_128(&buffer).to_vec())
}
@@ -23,7 +23,7 @@ use scale_info::TypeInfo;
/// Where message dispatch fee is paid?
#[derive(Encode, Decode, RuntimeDebug, Clone, Copy, PartialEq, Eq, TypeInfo)]
pub enum DispatchFeePayment {
/// The dispacth fee is paid at the source chain.
/// The dispatch fee is paid at the source chain.
AtSourceChain,
/// The dispatch fee is paid at the target chain.
///
@@ -51,7 +51,7 @@ pub struct MessageDispatchResult {
/// 2) if message has not been dispatched at all.
pub unspent_weight: Weight,
/// Whether the message dispatch fee has been paid during dispatch. This will be true if your
/// configuration supports pay-dispatch-fee-at-target-chain option and message sender has enabled
/// this option.
/// configuration supports pay-dispatch-fee-at-target-chain option and message sender has
/// enabled this option.
pub dispatch_fee_paid_during_dispatch: bool,
}
@@ -42,7 +42,7 @@ where
pub fn new(root: H::Out, proof: StorageProof) -> Result<Self, Error> {
let db = proof.into_memory_db();
if !db.contains(&root, EMPTY_PREFIX) {
return Err(Error::StorageRootMismatch);
return Err(Error::StorageRootMismatch)
}
let checker = StorageProofChecker { root, db };
@@ -52,7 +52,8 @@ where
/// Reads a value from the available subset of storage. If the value cannot be read due to an
/// incomplete or otherwise invalid proof, this returns an error.
pub fn read_value(&self, key: &[u8]) -> Result<Option<Vec<u8>>, Error> {
read_trie_value::<Layout<H>, _>(&self.db, &self.root, key).map_err(|_| Error::StorageValueUnavailable)
read_trie_value::<Layout<H>, _>(&self.db, &self.root, key)
.map_err(|_| Error::StorageValueUnavailable)
}
}
@@ -97,7 +98,8 @@ pub mod tests {
let (root, proof) = craft_valid_storage_proof();
// check proof in runtime
let checker = <StorageProofChecker<sp_core::Blake2Hasher>>::new(root, proof.clone()).unwrap();
let checker =
<StorageProofChecker<sp_core::Blake2Hasher>>::new(root, proof.clone()).unwrap();
assert_eq!(checker.read_value(b"key1"), Ok(Some(b"value1".to_vec())));
assert_eq!(checker.read_value(b"key2"), Ok(Some(b"value2".to_vec())));
assert_eq!(checker.read_value(b"key11111"), Err(Error::StorageValueUnavailable));
@@ -7,7 +7,7 @@ license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
[dependencies]
bp-header-chain = { path = "../header-chain", default-features = false }
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false }
codec = { package = "parity-scale-codec", version = "2.2.0", default-features = false }
ed25519-dalek = { version = "1.0", default-features = false, features = ["u64_backend"] }
finality-grandpa = { version = "0.14.4", default-features = false }
sp-application-crypto = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
@@ -45,7 +45,8 @@ impl Account {
let data = self.0.encode();
let mut bytes = [0_u8; 32];
bytes[0..data.len()].copy_from_slice(&*data);
SecretKey::from_bytes(&bytes).expect("A static array of the correct length is a known good.")
SecretKey::from_bytes(&bytes)
.expect("A static array of the correct length is a known good.")
}
pub fn pair(&self) -> Keypair {
@@ -57,7 +58,8 @@ impl Account {
let public = self.public();
pair[32..].copy_from_slice(&public.to_bytes());
Keypair::from_bytes(&pair).expect("We expect the SecretKey to be good, so this must also be good.")
Keypair::from_bytes(&pair)
.expect("We expect the SecretKey to be good, so this must also be good.")
}
pub fn sign(&self, msg: &[u8]) -> Signature {
@@ -79,10 +81,7 @@ pub fn voter_set() -> VoterSet<AuthorityId> {
/// Convenience function to get a list of Grandpa authorities.
pub fn authority_list() -> AuthorityList {
test_keyring()
.iter()
.map(|(id, w)| (AuthorityId::from(*id), *w))
.collect()
test_keyring().iter().map(|(id, w)| (AuthorityId::from(*id), *w)).collect()
}
/// Get the corresponding identities from the keyring for the "standard" authority set.
@@ -21,8 +21,7 @@
use bp_header_chain::justification::GrandpaJustification;
use codec::Encode;
use sp_application_crypto::TryFrom;
use sp_finality_grandpa::{AuthorityId, AuthorityWeight};
use sp_finality_grandpa::{AuthoritySignature, SetId};
use sp_finality_grandpa::{AuthorityId, AuthoritySignature, AuthorityWeight, SetId};
use sp_runtime::traits::{Header as HeaderT, One, Zero};
use sp_std::prelude::*;
@@ -49,7 +48,7 @@ pub struct JustificationGeneratorParams<H> {
pub authorities: Vec<(Account, AuthorityWeight)>,
/// The total number of precommit ancestors in the `votes_ancestries` field our justification.
///
/// These may be distributed among many different forks.
/// These may be distributed among many forks.
pub ancestors: u32,
/// The number of forks.
///
@@ -72,10 +71,7 @@ impl<H: HeaderT> Default for JustificationGeneratorParams<H> {
/// Make a valid GRANDPA justification with sensible defaults
pub fn make_default_justification<H: HeaderT>(header: &H) -> GrandpaJustification<H> {
let params = JustificationGeneratorParams::<H> {
header: header.clone(),
..Default::default()
};
let params = JustificationGeneratorParams::<H> { header: header.clone(), ..Default::default() };
make_justification_for_header(params)
}
@@ -89,15 +85,11 @@ pub fn make_default_justification<H: HeaderT>(header: &H) -> GrandpaJustificatio
///
/// Note: This needs at least three authorities or else the verifier will complain about
/// being given an invalid commit.
pub fn make_justification_for_header<H: HeaderT>(params: JustificationGeneratorParams<H>) -> GrandpaJustification<H> {
let JustificationGeneratorParams {
header,
round,
set_id,
authorities,
mut ancestors,
forks,
} = params;
pub fn make_justification_for_header<H: HeaderT>(
params: JustificationGeneratorParams<H>,
) -> GrandpaJustification<H> {
let JustificationGeneratorParams { header, round, set_id, authorities, mut ancestors, forks } =
params;
let (target_hash, target_number) = (header.hash(), *header.number());
let mut votes_ancestries = vec![];
let mut precommits = vec![];
@@ -144,11 +136,7 @@ pub fn make_justification_for_header<H: HeaderT>(params: JustificationGeneratorP
GrandpaJustification {
round,
commit: finality_grandpa::Commit {
target_hash,
target_number,
precommits,
},
commit: finality_grandpa::Commit { target_hash, target_number, precommits },
votes_ancestries,
}
}
@@ -165,10 +153,7 @@ fn generate_chain<H: HeaderT>(fork_id: u32, depth: u32, ancestor: &H) -> Vec<H>
// Modifying the digest so headers at the same height but in different forks have different
// hashes
header
.digest_mut()
.logs
.push(sp_runtime::DigestItem::Other(fork_id.encode()));
header.digest_mut().logs.push(sp_runtime::DigestItem::Other(fork_id.encode()));
headers.push(header);
}
@@ -183,29 +168,26 @@ pub fn signed_precommit<H: HeaderT>(
round: u64,
set_id: SetId,
) -> finality_grandpa::SignedPrecommit<H::Hash, H::Number, AuthoritySignature, AuthorityId> {
let precommit = finality_grandpa::Precommit {
target_hash: target.0,
target_number: target.1,
};
let precommit = finality_grandpa::Precommit { target_hash: target.0, target_number: target.1 };
let encoded =
sp_finality_grandpa::localized_payload(round, set_id, &finality_grandpa::Message::Precommit(precommit.clone()));
let encoded = sp_finality_grandpa::localized_payload(
round,
set_id,
&finality_grandpa::Message::Precommit(precommit.clone()),
);
let signature = signer.sign(&encoded);
let raw_signature: Vec<u8> = signature.to_bytes().into();
// Need to wrap our signature and id types that they match what our `SignedPrecommit` is expecting
// Need to wrap our signature and id types that they match what our `SignedPrecommit` is
// expecting
let signature = AuthoritySignature::try_from(raw_signature).expect(
"We know our Keypair is good,
so our signature must also be good.",
);
let id = (*signer).into();
finality_grandpa::SignedPrecommit {
precommit,
signature,
id,
}
finality_grandpa::SignedPrecommit { precommit, signature, id }
}
/// Get a header for testing.
@@ -213,13 +195,7 @@ pub fn signed_precommit<H: HeaderT>(
/// The correct parent hash will be used if given a non-zero header.
pub fn test_header<H: HeaderT>(number: H::Number) -> H {
let default = |num| {
H::new(
num,
Default::default(),
Default::default(),
Default::default(),
Default::default(),
)
H::new(num, Default::default(), Default::default(), Default::default(), Default::default())
};
let mut header = default(number);
@@ -0,0 +1,27 @@
[package]
name = "bp-token-swap"
description = "Primitives of the pallet-bridge-token-swap pallet"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
[dependencies]
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false }
scale-info = { version = "1.0", default-features = false, features = ["derive"] }
# Substrate Dependencies
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
[features]
default = ["std"]
std = [
"codec/std",
"frame-support/std",
"scale-info/std",
"sp-core/std",
"sp-std/std",
]
@@ -0,0 +1,109 @@
// Copyright 2019-2021 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)]
use codec::{Decode, Encode};
use frame_support::{weights::Weight, RuntimeDebug};
use scale_info::TypeInfo;
use sp_core::U256;
use sp_std::vec::Vec;
/// Pending token swap state.
#[derive(Encode, Decode, Clone, RuntimeDebug, PartialEq, Eq, TypeInfo)]
pub enum TokenSwapState {
/// The swap has been started using the `start_claim` call, but we have no proof that it has
/// happened at the Bridged chain.
Started,
/// The swap has happened at the Bridged chain and may be claimed by the Bridged chain party
/// using the `claim_swap` call.
Confirmed,
/// The swap has failed at the Bridged chain and This chain party may cancel it using the
/// `cancel_swap` call.
Failed,
}
/// Token swap type.
///
/// Different swap types give a different guarantees regarding possible swap
/// replay protection.
#[derive(Encode, Decode, Clone, RuntimeDebug, PartialEq, Eq, TypeInfo)]
pub enum TokenSwapType<ThisBlockNumber> {
/// The `target_account_at_bridged_chain` is temporary and only have funds for single swap.
///
/// ***WARNING**: if `target_account_at_bridged_chain` still exists after the swap has been
/// completed (either by claiming or canceling), the `source_account_at_this_chain` will be
/// able to restart the swap again and repeat the swap until `target_account_at_bridged_chain`
/// depletes.
TemporaryTargetAccountAtBridgedChain,
/// This swap type prevents `source_account_at_this_chain` from restarting the swap after it
/// has been completed. There are two consequences:
///
/// 1) the `source_account_at_this_chain` won't be able to call `start_swap` after given
/// <ThisBlockNumber>; 2) the `target_account_at_bridged_chain` won't be able to call
/// `claim_swap` (over the bridge) before block `<ThisBlockNumber + 1>`.
///
/// The second element is the nonce of the swap. You must care about its uniqueness if you're
/// planning to perform another swap with exactly the same parameters (i.e. same amount, same
/// accounts, same `ThisBlockNumber`) to avoid collisions.
LockClaimUntilBlock(ThisBlockNumber, U256),
}
/// An intention to swap `source_balance_at_this_chain` owned by `source_account_at_this_chain`
/// to `target_balance_at_bridged_chain` owned by `target_account_at_bridged_chain`.
///
/// **IMPORTANT NOTE**: this structure is always the same during single token swap. So even
/// when chain changes, the meaning of This and Bridged are still used to point to the same chains.
/// This chain is always the chain where swap has been started. And the Bridged chain is the other
/// chain.
#[derive(Encode, Decode, Clone, RuntimeDebug, PartialEq, Eq, TypeInfo)]
pub struct TokenSwap<ThisBlockNumber, ThisBalance, ThisAccountId, BridgedBalance, BridgedAccountId>
{
/// The type of the swap.
pub swap_type: TokenSwapType<ThisBlockNumber>,
/// This chain balance to be swapped with `target_balance_at_bridged_chain`.
pub source_balance_at_this_chain: ThisBalance,
/// Account id of the party acting at This chain and owning the `source_account_at_this_chain`.
pub source_account_at_this_chain: ThisAccountId,
/// Bridged chain balance to be swapped with `source_balance_at_this_chain`.
pub target_balance_at_bridged_chain: BridgedBalance,
/// Account id of the party acting at the Bridged chain and owning the
/// `target_balance_at_bridged_chain`.
pub target_account_at_bridged_chain: BridgedAccountId,
}
/// SCALE-encoded `Currency::transfer` call on the bridged chain.
pub type RawBridgedTransferCall = Vec<u8>;
/// Token swap creation parameters.
#[derive(Encode, Decode, Clone, RuntimeDebug, PartialEq, Eq, TypeInfo)]
pub struct TokenSwapCreation<BridgedAccountPublic, ThisChainBalance, BridgedAccountSignature> {
/// Public key of the `target_account_at_bridged_chain` account used to verify
/// `bridged_currency_transfer_signature`.
pub target_public_at_bridged_chain: BridgedAccountPublic,
/// Fee that the `source_account_at_this_chain` is ready to pay for the tokens
/// transfer message delivery and dispatch.
pub swap_delivery_and_dispatch_fee: ThisChainBalance,
/// Specification version of the Bridged chain.
pub bridged_chain_spec_version: u32,
/// SCALE-encoded tokens transfer call at the Bridged chain.
pub bridged_currency_transfer: RawBridgedTransferCall,
/// Dispatch weight of the tokens transfer call at the Bridged chain.
pub bridged_currency_transfer_weight: Weight,
/// The signature of the `target_account_at_bridged_chain` for the message
/// returned by the `pallet_bridge_dispatch::account_ownership_digest()` function call.
pub bridged_currency_transfer_signature: BridgedAccountSignature,
}