From b1658df4af63ba9d14579992bd60fc761ec68bc8 Mon Sep 17 00:00:00 2001 From: Serban Iorga Date: Mon, 22 Aug 2022 15:46:19 +0300 Subject: [PATCH] Split decl_bridge_runtime_apis!() (#1553) Signed-off-by: Serban Iorga Signed-off-by: Serban Iorga --- bridges/primitives/chain-kusama/src/lib.rs | 6 +- bridges/primitives/chain-millau/src/lib.rs | 7 +- bridges/primitives/chain-polkadot/src/lib.rs | 6 +- .../chain-rialto-parachain/src/lib.rs | 7 +- bridges/primitives/chain-rialto/src/lib.rs | 7 +- bridges/primitives/chain-rococo/src/lib.rs | 6 +- bridges/primitives/chain-westend/Cargo.toml | 1 + bridges/primitives/chain-westend/src/lib.rs | 29 +-- bridges/primitives/chain-wococo/src/lib.rs | 6 +- bridges/primitives/runtime/src/chain.rs | 177 +++++++++++------- 10 files changed, 143 insertions(+), 109 deletions(-) diff --git a/bridges/primitives/chain-kusama/src/lib.rs b/bridges/primitives/chain-kusama/src/lib.rs index ab011d814c0dd..598e5730253d6 100644 --- a/bridges/primitives/chain-kusama/src/lib.rs +++ b/bridges/primitives/chain-kusama/src/lib.rs @@ -29,7 +29,9 @@ use sp_std::prelude::*; use sp_version::RuntimeVersion; pub use bp_polkadot_core::*; -use bp_runtime::declare_bridge_chain_runtime_apis; +use bp_runtime::{ + decl_bridge_finality_runtime_apis, decl_bridge_messages_runtime_apis, decl_bridge_runtime_apis, +}; /// Kusama Chain pub type Kusama = PolkadotLike; @@ -91,4 +93,4 @@ pub const POLKADOT_TO_KUSAMA_CONVERSION_RATE_PARAMETER_NAME: &str = /// Name of the Polkadot fee multiplier parameter, stored in the Polkadot runtime. pub const POLKADOT_FEE_MULTIPLIER_PARAMETER_NAME: &str = "PolkadotFeeMultiplier"; -declare_bridge_chain_runtime_apis!(kusama); +decl_bridge_runtime_apis!(kusama); diff --git a/bridges/primitives/chain-millau/src/lib.rs b/bridges/primitives/chain-millau/src/lib.rs index 4efaeac0f8ce6..536a9216c80e4 100644 --- a/bridges/primitives/chain-millau/src/lib.rs +++ b/bridges/primitives/chain-millau/src/lib.rs @@ -23,7 +23,10 @@ mod millau_hash; use bp_messages::{ InboundMessageDetails, LaneId, MessageNonce, MessagePayload, OutboundMessageDetails, }; -use bp_runtime::{declare_bridge_chain_runtime_apis, Chain}; +use bp_runtime::{ + decl_bridge_finality_runtime_apis, decl_bridge_messages_runtime_apis, decl_bridge_runtime_apis, + Chain, +}; use frame_support::{ weights::{constants::WEIGHT_PER_SECOND, DispatchClass, IdentityFee, Weight}, Parameter, RuntimeDebug, @@ -254,4 +257,4 @@ pub const RIALTO_PARACHAIN_TO_MILLAU_CONVERSION_RATE_PARAMETER_NAME: &str = /// Name of the RialtoParachain fee multiplier parameter, stored in the Millau runtime. pub const RIALTO_PARACHAIN_FEE_MULTIPLIER_PARAMETER_NAME: &str = "RialtoParachainFeeMultiplier"; -declare_bridge_chain_runtime_apis!(millau); +decl_bridge_runtime_apis!(millau); diff --git a/bridges/primitives/chain-polkadot/src/lib.rs b/bridges/primitives/chain-polkadot/src/lib.rs index 90184d1feb4a3..7519c38878dfe 100644 --- a/bridges/primitives/chain-polkadot/src/lib.rs +++ b/bridges/primitives/chain-polkadot/src/lib.rs @@ -29,7 +29,9 @@ use sp_std::prelude::*; use sp_version::RuntimeVersion; pub use bp_polkadot_core::*; -use bp_runtime::declare_bridge_chain_runtime_apis; +use bp_runtime::{ + decl_bridge_finality_runtime_apis, decl_bridge_messages_runtime_apis, decl_bridge_runtime_apis, +}; /// Polkadot Chain pub type Polkadot = PolkadotLike; @@ -91,4 +93,4 @@ pub const KUSAMA_TO_POLKADOT_CONVERSION_RATE_PARAMETER_NAME: &str = /// Name of the Kusama fee multiplier parameter, stored in the Polkadot runtime. pub const KUSAMA_FEE_MULTIPLIER_PARAMETER_NAME: &str = "KusamaFeeMultiplier"; -declare_bridge_chain_runtime_apis!(polkadot); +decl_bridge_runtime_apis!(polkadot); diff --git a/bridges/primitives/chain-rialto-parachain/src/lib.rs b/bridges/primitives/chain-rialto-parachain/src/lib.rs index 847ce68ef8e81..285f7f0f184bd 100644 --- a/bridges/primitives/chain-rialto-parachain/src/lib.rs +++ b/bridges/primitives/chain-rialto-parachain/src/lib.rs @@ -21,7 +21,10 @@ use bp_messages::{ InboundMessageDetails, LaneId, MessageNonce, MessagePayload, OutboundMessageDetails, }; -use bp_runtime::{declare_bridge_chain_runtime_apis, Chain}; +use bp_runtime::{ + decl_bridge_finality_runtime_apis, decl_bridge_messages_runtime_apis, decl_bridge_runtime_apis, + Chain, +}; use frame_support::{ weights::{constants::WEIGHT_PER_SECOND, DispatchClass, IdentityFee, Weight}, Parameter, RuntimeDebug, @@ -193,4 +196,4 @@ pub const MILLAU_TO_RIALTO_PARACHAIN_CONVERSION_RATE_PARAMETER_NAME: &str = /// Name of the Millau fee multiplier parameter, stored in the Rialto parachain runtime. pub const MILLAU_FEE_MULTIPLIER_PARAMETER_NAME: &str = "MillauFeeMultiplier"; -declare_bridge_chain_runtime_apis!(rialto_parachain); +decl_bridge_runtime_apis!(rialto_parachain); diff --git a/bridges/primitives/chain-rialto/src/lib.rs b/bridges/primitives/chain-rialto/src/lib.rs index f2293bace0fa5..cd4a81264d192 100644 --- a/bridges/primitives/chain-rialto/src/lib.rs +++ b/bridges/primitives/chain-rialto/src/lib.rs @@ -21,7 +21,10 @@ use bp_messages::{ InboundMessageDetails, LaneId, MessageNonce, MessagePayload, OutboundMessageDetails, }; -use bp_runtime::{declare_bridge_chain_runtime_apis, Chain}; +use bp_runtime::{ + decl_bridge_finality_runtime_apis, decl_bridge_messages_runtime_apis, decl_bridge_runtime_apis, + Chain, +}; use frame_support::{ weights::{constants::WEIGHT_PER_SECOND, DispatchClass, IdentityFee, Weight}, Parameter, RuntimeDebug, @@ -217,4 +220,4 @@ pub const PARAS_REGISTRAR_PALLET_NAME: &str = "Registrar"; /// Name of the parachains pallet in the Rialto runtime. pub const PARAS_PALLET_NAME: &str = "Paras"; -declare_bridge_chain_runtime_apis!(rialto); +decl_bridge_runtime_apis!(rialto); diff --git a/bridges/primitives/chain-rococo/src/lib.rs b/bridges/primitives/chain-rococo/src/lib.rs index feee87c2e5d2e..dfc31c827376d 100644 --- a/bridges/primitives/chain-rococo/src/lib.rs +++ b/bridges/primitives/chain-rococo/src/lib.rs @@ -29,7 +29,9 @@ use sp_std::prelude::*; use sp_version::RuntimeVersion; pub use bp_polkadot_core::*; -use bp_runtime::declare_bridge_chain_runtime_apis; +use bp_runtime::{ + decl_bridge_finality_runtime_apis, decl_bridge_messages_runtime_apis, decl_bridge_runtime_apis, +}; /// Rococo Chain pub type Rococo = PolkadotLike; @@ -87,4 +89,4 @@ pub const EXISTENTIAL_DEPOSIT: Balance = 1_000_000_000_000 / 100; /// transactions cheaper. pub const PAY_INBOUND_DISPATCH_FEE_WEIGHT: Weight = 600_000_000; -declare_bridge_chain_runtime_apis!(rococo); +decl_bridge_runtime_apis!(rococo); diff --git a/bridges/primitives/chain-westend/Cargo.toml b/bridges/primitives/chain-westend/Cargo.toml index f37aa3b0e397e..fb2f846f7efd3 100644 --- a/bridges/primitives/chain-westend/Cargo.toml +++ b/bridges/primitives/chain-westend/Cargo.toml @@ -8,6 +8,7 @@ license = "GPL-3.0-or-later WITH Classpath-exception-2.0" [dependencies] codec = { package = "parity-scale-codec", version = "3.1.5", default-features = false, features = ["derive"] } +paste = "1.0" scale-info = { version = "2.1.1", default-features = false, features = ["derive"] } smallvec = "1.7" diff --git a/bridges/primitives/chain-westend/src/lib.rs b/bridges/primitives/chain-westend/src/lib.rs index c914cf0f0c347..1e023295a8a0f 100644 --- a/bridges/primitives/chain-westend/src/lib.rs +++ b/bridges/primitives/chain-westend/src/lib.rs @@ -26,6 +26,7 @@ use sp_std::prelude::*; use sp_version::RuntimeVersion; pub use bp_polkadot_core::*; +use bp_runtime::decl_bridge_finality_runtime_apis; /// Westend Chain pub type Westend = PolkadotLike; @@ -85,9 +86,6 @@ pub const WITH_WESTEND_GRANDPA_PALLET_NAME: &str = "BridgeWestendGrandpa"; /// Name of the With-Westend parachains bridge pallet instance that is deployed at bridged chains. pub const WITH_WESTEND_BRIDGE_PARAS_PALLET_NAME: &str = "BridgeWestendParachains"; -/// Name of the `WestendFinalityApi::best_finalized` runtime method. -pub const BEST_FINALIZED_WESTEND_HEADER_METHOD: &str = "WestendFinalityApi_best_finalized"; - /// The target length of a session (how often authorities change) on Westend measured in of number /// of blocks. /// @@ -95,28 +93,9 @@ pub const BEST_FINALIZED_WESTEND_HEADER_METHOD: &str = "WestendFinalityApi_best_ /// conditions. pub const SESSION_LENGTH: BlockNumber = 10 * time_units::MINUTES; -sp_api::decl_runtime_apis! { - /// API for querying information about the finalized Westend headers. - /// - /// This API is implemented by runtimes that are bridging with the Westend chain, not the - /// Westend runtime itself. - pub trait WestendFinalityApi { - /// Returns number and hash of the best finalized header known to the bridge module. - fn best_finalized() -> Option>; - } - - /// API for querying information about the finalized Westmint headers. - /// - /// This API is implemented by runtimes that are bridging with the Westmint chain, not the - /// Westmint runtime itself. - pub trait WestmintFinalityApi { - /// Returns number and hash of the best finalized header known to the bridge module. - fn best_finalized() -> Option>; - } -} - /// Identifier of Westmint parachain at the Westend relay chain. pub const WESTMINT_PARACHAIN_ID: u32 = 2000; -/// Name of the `WestmintFinalityApi::best_finalized` runtime method. -pub const BEST_FINALIZED_WESTMINT_HEADER_METHOD: &str = "WestmintFinalityApi_best_finalized"; +decl_bridge_finality_runtime_apis!(westend); + +decl_bridge_finality_runtime_apis!(westmint); diff --git a/bridges/primitives/chain-wococo/src/lib.rs b/bridges/primitives/chain-wococo/src/lib.rs index a2fd5d7c6dacf..6db6e2fe6fd95 100644 --- a/bridges/primitives/chain-wococo/src/lib.rs +++ b/bridges/primitives/chain-wococo/src/lib.rs @@ -27,7 +27,9 @@ use sp_std::prelude::*; pub use bp_polkadot_core::*; // Rococo runtime = Wococo runtime pub use bp_rococo::{WeightToFee, EXISTENTIAL_DEPOSIT, PAY_INBOUND_DISPATCH_FEE_WEIGHT, VERSION}; -use bp_runtime::declare_bridge_chain_runtime_apis; +use bp_runtime::{ + decl_bridge_finality_runtime_apis, decl_bridge_messages_runtime_apis, decl_bridge_runtime_apis, +}; /// Wococo Chain pub type Wococo = PolkadotLike; @@ -44,4 +46,4 @@ pub const WITH_WOCOCO_GRANDPA_PALLET_NAME: &str = "BridgeWococoGrandpa"; /// Name of the With-Wococo messages pallet instance that is deployed at bridged chains. pub const WITH_WOCOCO_MESSAGES_PALLET_NAME: &str = "BridgeWococoMessages"; -declare_bridge_chain_runtime_apis!(wococo); +decl_bridge_runtime_apis!(wococo); diff --git a/bridges/primitives/runtime/src/chain.rs b/bridges/primitives/runtime/src/chain.rs index c8d5d9b94aacc..154603ccd3a10 100644 --- a/bridges/primitives/runtime/src/chain.rs +++ b/bridges/primitives/runtime/src/chain.rs @@ -215,94 +215,131 @@ pub type AccountPublicOf = as Verify>::Signer; /// Transaction era used by the chain. pub type TransactionEraOf = crate::TransactionEra, HashOf>; -/// Convenience macro that declares runtime apis and related constants for a bridge chain. +/// Convenience macro that declares bridge finality runtime apis and related constants for a chain. /// This includes: /// - chain-specific bridge runtime APIs: /// - `FinalityApi` +/// - constants that are stringified names of runtime API methods: +/// - `BEST_FINALIZED__HEADER_METHOD` +/// The name of the chain has to be specified in snake case (e.g. `rialto_parachain`). +#[macro_export] +macro_rules! decl_bridge_finality_runtime_apis { + ($chain: ident) => { + paste::item! { + mod [<$chain _finality_api>] { + use super::*; + + /// Name of the `FinalityApi::best_finalized` runtime method. + pub const []: &str = + stringify!([<$chain:camel FinalityApi_best_finalized>]); + + sp_api::decl_runtime_apis! { + /// API for querying information about the finalized chain headers. + /// + /// This API is implemented by runtimes that are receiving messages from this chain, not by this + /// chain's runtime itself. + pub trait [<$chain:camel FinalityApi>] { + /// Returns number and hash of the best finalized header known to the bridge module. + fn best_finalized() -> Option>; + } + } + } + + pub use [<$chain _finality_api>]::*; + } + }; +} + +/// Convenience macro that declares bridge messages runtime apis and related constants for a chain. +/// This includes: +/// - chain-specific bridge runtime APIs: /// - `ToOutboundLaneApi` /// - `FromInboundLaneApi` /// - constants that are stringified names of runtime API methods: -/// - `BEST_FINALIZED__HEADER_METHOD` /// - `TO__ESTIMATE_MESSAGE_FEE_METHOD` /// - `TO__MESSAGE_DETAILS_METHOD` /// - `FROM__MESSAGE_DETAILS_METHOD`, /// The name of the chain has to be specified in snake case (e.g. `rialto_parachain`). #[macro_export] -macro_rules! declare_bridge_chain_runtime_apis { +macro_rules! decl_bridge_messages_runtime_apis { ($chain: ident) => { paste::item! { - /// Name of the `FinalityApi::best_finalized` runtime method. - pub const []: &str = - stringify!([<$chain:camel FinalityApi_best_finalized>]); - - /// Name of the `ToOutboundLaneApi::estimate_message_delivery_and_dispatch_fee` runtime - /// method. - pub const []: &str = - stringify!([]); - /// Name of the `ToOutboundLaneApi::message_details` runtime method. - pub const []: &str = - stringify!([]); - - /// Name of the `FromInboundLaneApi::message_details` runtime method. - pub const []: &str = - stringify!([]); - - sp_api::decl_runtime_apis! { - /// API for querying information about the finalized chain headers. - /// - /// This API is implemented by runtimes that are receiving messages from this chain, not by this - /// chain's runtime itself. - pub trait [<$chain:camel FinalityApi>] { - /// Returns number and hash of the best finalized header known to the bridge module. - fn best_finalized() -> Option>; - } - - /// Outbound message lane API for messages that are sent to this chain. - /// - /// This API is implemented by runtimes that are receiving messages from this chain, not by this - /// chain's runtime itself. - pub trait [] { - /// Estimate message delivery and dispatch fee that needs to be paid by the sender on - /// this chain. + mod [<$chain _messages_api>] { + use super::*; + + /// Name of the `ToOutboundLaneApi::estimate_message_delivery_and_dispatch_fee` runtime + /// method. + pub const []: &str = + stringify!([]); + /// Name of the `ToOutboundLaneApi::message_details` runtime method. + pub const []: &str = + stringify!([]); + + /// Name of the `FromInboundLaneApi::message_details` runtime method. + pub const []: &str = + stringify!([]); + + sp_api::decl_runtime_apis! { + /// Outbound message lane API for messages that are sent to this chain. /// - /// Returns `None` if message is too expensive to be sent to this chain from the bridged chain. + /// This API is implemented by runtimes that are receiving messages from this chain, not by this + /// chain's runtime itself. + pub trait [] { + /// Estimate message delivery and dispatch fee that needs to be paid by the sender on + /// this chain. + /// + /// Returns `None` if message is too expensive to be sent to this chain from the bridged chain. + /// + /// Please keep in mind that this method returns the lowest message fee required for message + /// to be accepted to the lane. It may be a good idea to pay a bit over this price to account + /// for future exchange rate changes and guarantee that relayer would deliver your message + /// to the target chain. + fn estimate_message_delivery_and_dispatch_fee( + lane_id: LaneId, + payload: OutboundPayload, + [<$chain:lower _to_this_conversion_rate>]: Option, + ) -> Option; + /// Returns dispatch weight, encoded payload size and delivery+dispatch fee of all + /// messages in given inclusive range. + /// + /// If some (or all) messages are missing from the storage, they'll also will + /// be missing from the resulting vector. The vector is ordered by the nonce. + fn message_details( + lane: LaneId, + begin: MessageNonce, + end: MessageNonce, + ) -> Vec>; + } + + /// Inbound message lane API for messages sent by this chain. /// - /// Please keep in mind that this method returns the lowest message fee required for message - /// to be accepted to the lane. It may be a good idea to pay a bit over this price to account - /// for future exchange rate changes and guarantee that relayer would deliver your message - /// to the target chain. - fn estimate_message_delivery_and_dispatch_fee( - lane_id: LaneId, - payload: OutboundPayload, - [<$chain:lower _to_this_conversion_rate>]: Option, - ) -> Option; - /// Returns dispatch weight, encoded payload size and delivery+dispatch fee of all - /// messages in given inclusive range. + /// This API is implemented by runtimes that are receiving messages from this chain, not by this + /// chain's runtime itself. /// - /// If some (or all) messages are missing from the storage, they'll also will - /// be missing from the resulting vector. The vector is ordered by the nonce. - fn message_details( - lane: LaneId, - begin: MessageNonce, - end: MessageNonce, - ) -> Vec>; - } - - /// Inbound message lane API for messages sent by this chain. - /// - /// This API is implemented by runtimes that are receiving messages from this chain, not by this - /// chain's runtime itself. - /// - /// Entries of the resulting vector are matching entries of the `messages` vector. Entries of the - /// `messages` vector may (and need to) be read using `ToOutboundLaneApi::message_details`. - pub trait [] { - /// Return details of given inbound messages. - fn message_details( - lane: LaneId, - messages: Vec<(MessagePayload, OutboundMessageDetails)>, - ) -> Vec; + /// Entries of the resulting vector are matching entries of the `messages` vector. Entries of the + /// `messages` vector may (and need to) be read using `ToOutboundLaneApi::message_details`. + pub trait [] { + /// Return details of given inbound messages. + fn message_details( + lane: LaneId, + messages: Vec<(MessagePayload, OutboundMessageDetails)>, + ) -> Vec; + } } } + + pub use [<$chain _messages_api>]::*; } } } + +/// Convenience macro that declares bridge finality runtime apis, bridge messages runtime apis +/// and related constants for a chain. +/// The name of the chain has to be specified in snake case (e.g. `rialto_parachain`). +#[macro_export] +macro_rules! decl_bridge_runtime_apis { + ($chain: ident) => { + decl_bridge_finality_runtime_apis!($chain); + decl_bridge_messages_runtime_apis!($chain); + }; +}