Add RococoBridgeHub <> WococoBridgeHub full 2 way bridge (#1663)

* Add RococoBridgeHub <> WococoBridgeHub full 2 way bridge

* Use StorageMapKeyProvider instead of account_info_storage_key()

Avoid duplicating storage_map_final_key()

* clippy + leftovers
This commit is contained in:
Serban Iorga
2022-11-25 10:56:48 +02:00
committed by Bastian Köcher
parent 786db04bbc
commit 7b74940539
13 changed files with 115 additions and 35 deletions
@@ -24,7 +24,7 @@
// Re-export only what is really needed // Re-export only what is really needed
pub use bp_bridge_hub_rococo::{ pub use bp_bridge_hub_rococo::{
account_info_storage_key, AccountId, AccountPublic, AccountSigner, Address, Balance, AccountId, AccountInfoStorageMapKeyProvider, AccountPublic, AccountSigner, Address, Balance,
BlockLength, BlockNumber, BlockWeights, Hash, Hasher, Hashing, Header, Index, Nonce, BlockLength, BlockNumber, BlockWeights, Hash, Hasher, Hashing, Header, Index, Nonce,
SS58Prefix, Signature, SignedBlock, SignedExtensions, UncheckedExtrinsic, WeightToFee, SS58Prefix, Signature, SignedBlock, SignedExtensions, UncheckedExtrinsic, WeightToFee,
MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX, MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX, MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX, MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX,
+20 -18
View File
@@ -17,7 +17,7 @@
#![cfg_attr(not(feature = "std"), no_std)] #![cfg_attr(not(feature = "std"), no_std)]
use bp_messages::MessageNonce; use bp_messages::MessageNonce;
use bp_runtime::{Chain, EncodedOrDecodedCall}; use bp_runtime::{Chain, EncodedOrDecodedCall, StorageMapKeyProvider};
use codec::Compact; use codec::Compact;
use frame_support::{ use frame_support::{
dispatch::{DispatchClass, Dispatchable}, dispatch::{DispatchClass, Dispatchable},
@@ -26,11 +26,11 @@ use frame_support::{
constants::{BlockExecutionWeight, WEIGHT_PER_SECOND}, constants::{BlockExecutionWeight, WEIGHT_PER_SECOND},
Weight, Weight,
}, },
Blake2_128Concat, RuntimeDebug, StorageHasher, Twox128, Blake2_128Concat, RuntimeDebug,
}; };
use frame_system::limits; use frame_system::limits;
use scale_info::{StaticTypeInfo, TypeInfo}; use scale_info::{StaticTypeInfo, TypeInfo};
use sp_core::Hasher as HasherT; use sp_core::{storage::StorageKey, Hasher as HasherT};
use sp_runtime::{ use sp_runtime::{
generic, generic,
traits::{BlakeTwo256, DispatchInfoOf, IdentifyAccount, Verify}, traits::{BlakeTwo256, DispatchInfoOf, IdentifyAccount, Verify},
@@ -346,26 +346,28 @@ impl Chain for PolkadotLike {
} }
} }
/// Return a storage key for account data. /// Provides a storage key for account data.
/// ///
/// This is based on FRAME storage-generation code from Substrate: /// We need to use this approach when we don't have access to the runtime.
/// [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: /// 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);` /// `let key = frame_system::Account::<Runtime>::storage_map_final_key(&account_id);`
pub fn account_info_storage_key(id: &AccountId) -> Vec<u8> { pub struct AccountInfoStorageMapKeyProvider;
let module_prefix_hashed = Twox128::hash(b"System");
let storage_prefix_hashed = Twox128::hash(b"Account");
let key_hashed = codec::Encode::using_encoded(id, Blake2_128Concat::hash);
let mut final_key = Vec::with_capacity( impl StorageMapKeyProvider for AccountInfoStorageMapKeyProvider {
module_prefix_hashed.len() + storage_prefix_hashed.len() + key_hashed.len(), const MAP_NAME: &'static str = "Account";
); type Hasher = Blake2_128Concat;
type Key = AccountId;
// This should actually be `AccountInfo`, but we don't use this property in order to decode the
// data. So we use `Vec<u8>` as if we would work with encoded data.
type Value = Vec<u8>;
}
final_key.extend_from_slice(&module_prefix_hashed[..]); impl AccountInfoStorageMapKeyProvider {
final_key.extend_from_slice(&storage_prefix_hashed[..]); const PALLET_NAME: &'static str = "System";
final_key.extend_from_slice(&key_hashed);
final_key pub fn final_key(id: &AccountId) -> StorageKey {
<Self as StorageMapKeyProvider>::final_key(Self::PALLET_NAME, id)
}
} }
#[cfg(test)] #[cfg(test)]
@@ -379,7 +381,7 @@ mod tests {
25, 26, 27, 28, 29, 30, 31, 32, 25, 26, 27, 28, 29, 30, 31, 32,
] ]
.into(); .into();
let key = account_info_storage_key(&acc); let key = AccountInfoStorageMapKeyProvider::final_key(&acc);
assert_eq!(hex::encode(key), "26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da92dccd599abfe1920a1cff8a7358231430102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20"); assert_eq!(hex::encode(key), "26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da92dccd599abfe1920a1cff8a7358231430102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20");
} }
} }
@@ -16,7 +16,7 @@
//! Wococo-to-Rococo parachains sync entrypoint. //! Wococo-to-Rococo parachains sync entrypoint.
use crate::cli::bridge::{CliBridgeBase, ParachainToRelayHeadersCliBridge}; use crate::cli::bridge::{CliBridgeBase, MessagesCliBridge, ParachainToRelayHeadersCliBridge};
use bp_polkadot_core::parachains::{ParaHash, ParaHeadsProof, ParaId}; use bp_polkadot_core::parachains::{ParaHash, ParaHeadsProof, ParaId};
use parachains_relay::ParachainsPipeline; use parachains_relay::ParachainsPipeline;
use relay_substrate_client::{CallOf, HeaderIdOf}; use relay_substrate_client::{CallOf, HeaderIdOf};
@@ -76,3 +76,10 @@ impl CliBridgeBase for BridgeHubRococoToBridgeHubWococoCliBridge {
type Source = relay_bridge_hub_rococo_client::BridgeHubRococo; type Source = relay_bridge_hub_rococo_client::BridgeHubRococo;
type Target = relay_bridge_hub_wococo_client::BridgeHubWococo; type Target = relay_bridge_hub_wococo_client::BridgeHubWococo;
} }
impl MessagesCliBridge for BridgeHubRococoToBridgeHubWococoCliBridge {
const ESTIMATE_MESSAGE_FEE_METHOD: &'static str =
bp_bridge_hub_wococo::TO_BRIDGE_HUB_WOCOCO_ESTIMATE_MESSAGE_FEE_METHOD;
type MessagesLane =
crate::chains::bridge_hub_rococo_messages_to_bridge_hub_wococo::BridgeHubRococoMessagesToBridgeHubWococoMessageLane;
}
@@ -16,7 +16,7 @@
//! Rococo-to-Wococo parachains sync entrypoint. //! Rococo-to-Wococo parachains sync entrypoint.
use crate::cli::bridge::{CliBridgeBase, ParachainToRelayHeadersCliBridge}; use crate::cli::bridge::{CliBridgeBase, MessagesCliBridge, ParachainToRelayHeadersCliBridge};
use bp_polkadot_core::parachains::{ParaHash, ParaHeadsProof, ParaId}; use bp_polkadot_core::parachains::{ParaHash, ParaHeadsProof, ParaId};
use parachains_relay::ParachainsPipeline; use parachains_relay::ParachainsPipeline;
use relay_substrate_client::{CallOf, HeaderIdOf}; use relay_substrate_client::{CallOf, HeaderIdOf};
@@ -76,3 +76,10 @@ impl CliBridgeBase for BridgeHubWococoToBridgeHubRococoCliBridge {
type Source = relay_bridge_hub_wococo_client::BridgeHubWococo; type Source = relay_bridge_hub_wococo_client::BridgeHubWococo;
type Target = relay_bridge_hub_rococo_client::BridgeHubRococo; type Target = relay_bridge_hub_rococo_client::BridgeHubRococo;
} }
impl MessagesCliBridge for BridgeHubWococoToBridgeHubRococoCliBridge {
const ESTIMATE_MESSAGE_FEE_METHOD: &'static str =
bp_bridge_hub_rococo::TO_BRIDGE_HUB_ROCOCO_ESTIMATE_MESSAGE_FEE_METHOD;
type MessagesLane =
crate::chains::bridge_hub_wococo_messages_to_bridge_hub_rococo::BridgeHubWococoMessagesToBridgeHubRococoMessageLane;
}
@@ -44,6 +44,8 @@ use crate::{
millau_headers_to_rialto_parachain::MillauToRialtoParachainCliBridge, millau_headers_to_rialto_parachain::MillauToRialtoParachainCliBridge,
rialto_headers_to_millau::RialtoToMillauCliBridge, rialto_headers_to_millau::RialtoToMillauCliBridge,
rialto_parachains_to_millau::RialtoParachainToMillauCliBridge, rialto_parachains_to_millau::RialtoParachainToMillauCliBridge,
rococo_parachains_to_bridge_hub_wococo::BridgeHubRococoToBridgeHubWococoCliBridge,
wococo_parachains_to_bridge_hub_rococo::BridgeHubWococoToBridgeHubRococoCliBridge,
}, },
cli::{ cli::{
bridge::{ bridge::{
@@ -51,6 +53,7 @@ use crate::{
RelayToRelayHeadersCliBridge, RelayToRelayHeadersCliBridge,
}, },
chain_schema::*, chain_schema::*,
relay_headers_and_messages::parachain_to_parachain::ParachainToParachainBridge,
CliChain, HexLaneId, PrometheusParams, CliChain, HexLaneId, PrometheusParams,
}, },
declare_chain_cli_schema, declare_chain_cli_schema,
@@ -190,14 +193,29 @@ where
declare_chain_cli_schema!(Millau, millau); declare_chain_cli_schema!(Millau, millau);
declare_chain_cli_schema!(Rialto, rialto); declare_chain_cli_schema!(Rialto, rialto);
declare_chain_cli_schema!(RialtoParachain, rialto_parachain); declare_chain_cli_schema!(RialtoParachain, rialto_parachain);
declare_chain_cli_schema!(Rococo, rococo);
declare_chain_cli_schema!(BridgeHubRococo, bridge_hub_rococo);
declare_chain_cli_schema!(Wococo, wococo);
declare_chain_cli_schema!(BridgeHubWococo, bridge_hub_wococo);
// Means to override signers of different layer transactions. // Means to override signers of different layer transactions.
declare_chain_cli_schema!(MillauHeadersToRialto, millau_headers_to_rialto); declare_chain_cli_schema!(MillauHeadersToRialto, millau_headers_to_rialto);
declare_chain_cli_schema!(MillauHeadersToRialtoParachain, millau_headers_to_rialto_parachain); declare_chain_cli_schema!(MillauHeadersToRialtoParachain, millau_headers_to_rialto_parachain);
declare_chain_cli_schema!(RialtoHeadersToMillau, rialto_headers_to_millau); declare_chain_cli_schema!(RialtoHeadersToMillau, rialto_headers_to_millau);
declare_chain_cli_schema!(RialtoParachainsToMillau, rialto_parachains_to_millau); declare_chain_cli_schema!(RialtoParachainsToMillau, rialto_parachains_to_millau);
declare_chain_cli_schema!(RococoHeadersToBridgeHubWococo, rococo_headers_to_bridge_hub_wococo);
declare_chain_cli_schema!(
RococoParachainsToBridgeHubWococo,
rococo_parachains_to_bridge_hub_wococo
);
declare_chain_cli_schema!(WococoHeadersToBridgeHubRococo, wococo_headers_to_bridge_hub_rococo);
declare_chain_cli_schema!(
WococoParachainsToBridgeHubRococo,
wococo_parachains_to_bridge_hub_rococo
);
// All supported bridges. // All supported bridges.
declare_relay_to_relay_bridge_schema!(Millau, Rialto); declare_relay_to_relay_bridge_schema!(Millau, Rialto);
declare_relay_to_parachain_bridge_schema!(Millau, RialtoParachain, Rialto); declare_relay_to_parachain_bridge_schema!(Millau, RialtoParachain, Rialto);
declare_parachain_to_parachain_bridge_schema!(BridgeHubRococo, Rococo, BridgeHubWococo, Wococo);
/// Base portion of the bidirectional complex relay. /// Base portion of the bidirectional complex relay.
/// ///
@@ -410,6 +428,32 @@ impl Full2WayBridge for MillauRialtoParachainFull2WayBridge {
} }
} }
/// BridgeHubRococo <> BridgeHubWococo complex relay.
pub struct BridgeHubRococoBridgeHubWococoFull2WayBridge {
base: <Self as Full2WayBridge>::Base,
}
#[async_trait]
impl Full2WayBridge for BridgeHubRococoBridgeHubWococoFull2WayBridge {
type Base = ParachainToParachainBridge<Self::L2R, Self::R2L>;
type Left = relay_bridge_hub_rococo_client::BridgeHubRococo;
type Right = relay_bridge_hub_wococo_client::BridgeHubWococo;
type L2R = BridgeHubRococoToBridgeHubWococoCliBridge;
type R2L = BridgeHubWococoToBridgeHubRococoCliBridge;
fn new(base: Self::Base) -> anyhow::Result<Self> {
Ok(Self { base })
}
fn base(&self) -> &Self::Base {
&self.base
}
fn mut_base(&mut self) -> &mut Self::Base {
&mut self.base
}
}
/// Complex headers+messages relay. /// Complex headers+messages relay.
#[derive(Debug, PartialEq, StructOpt)] #[derive(Debug, PartialEq, StructOpt)]
pub enum RelayHeadersAndMessages { pub enum RelayHeadersAndMessages {
@@ -417,6 +461,8 @@ pub enum RelayHeadersAndMessages {
MillauRialto(MillauRialtoHeadersAndMessages), MillauRialto(MillauRialtoHeadersAndMessages),
/// Millau <> RialtoParachain relay. /// Millau <> RialtoParachain relay.
MillauRialtoParachain(MillauRialtoParachainHeadersAndMessages), MillauRialtoParachain(MillauRialtoParachainHeadersAndMessages),
/// BridgeHubRococo <> BridgeHubWococo relay.
BridgeHubRococoBridgeHubWococo(BridgeHubRococoBridgeHubWococoHeadersAndMessages),
} }
impl RelayHeadersAndMessages { impl RelayHeadersAndMessages {
@@ -429,6 +475,10 @@ impl RelayHeadersAndMessages {
MillauRialtoParachainFull2WayBridge::new(params.into_bridge().await?)? MillauRialtoParachainFull2WayBridge::new(params.into_bridge().await?)?
.run() .run()
.await, .await,
RelayHeadersAndMessages::BridgeHubRococoBridgeHubWococo(params) =>
BridgeHubRococoBridgeHubWococoFull2WayBridge::new(params.into_bridge().await?)?
.run()
.await,
} }
} }
} }
@@ -118,7 +118,7 @@ macro_rules! declare_parachain_to_parachain_bridge_schema {
left_parachains_to_right_sign_override: [<$left_chain ParachainsTo $right_parachain SigningParams>], left_parachains_to_right_sign_override: [<$left_chain ParachainsTo $right_parachain SigningParams>],
} }
impl [<$left_chain $right_parachain HeadersAndMessages>] { impl [<$left_parachain $right_parachain HeadersAndMessages>] {
async fn into_bridge< async fn into_bridge<
Left: ChainWithTransactions + CliChain<KeyPair = AccountKeyPairOf<Left>>, Left: ChainWithTransactions + CliChain<KeyPair = AccountKeyPairOf<Left>>,
LeftRelay: CliChain, LeftRelay: CliChain,
@@ -132,7 +132,7 @@ macro_rules! declare_parachain_to_parachain_bridge_schema {
+ ParachainToRelayHeadersCliBridge<SourceRelay = RightRelay>, + ParachainToRelayHeadersCliBridge<SourceRelay = RightRelay>,
>( >(
self, self,
) -> anyhow::Result<RelayToParachainBridge<L2R, R2L>> { ) -> anyhow::Result<ParachainToParachainBridge<L2R, R2L>> {
Ok(ParachainToParachainBridge { Ok(ParachainToParachainBridge {
common: Full2WayBridgeCommonParams::new::<L2R>( common: Full2WayBridgeCommonParams::new::<L2R>(
self.shared, self.shared,
@@ -151,7 +151,7 @@ macro_rules! declare_parachain_to_parachain_bridge_schema {
accounts: vec![], accounts: vec![],
}, },
)?, )?,
left_relay: self.left_relay.into_client::<RightRelay>().await?, left_relay: self.left_relay.into_client::<LeftRelay>().await?,
right_relay: self.right_relay.into_client::<RightRelay>().await?, right_relay: self.right_relay.into_client::<RightRelay>().await?,
right_headers_to_left_transaction_params: self right_headers_to_left_transaction_params: self
.right_relay_headers_to_left_sign_override .right_relay_headers_to_left_sign_override
@@ -19,10 +19,10 @@
use bp_messages::{MessageNonce, Weight}; use bp_messages::{MessageNonce, Weight};
use codec::Encode; use codec::Encode;
use relay_substrate_client::{ use relay_substrate_client::{
Chain, ChainBase, ChainWithMessages, ChainWithTransactions, Error as SubstrateError, SignParam, Chain, ChainBase, ChainWithBalances, ChainWithMessages, ChainWithTransactions,
UnsignedTransaction, Error as SubstrateError, SignParam, UnsignedTransaction,
}; };
use sp_core::Pair; use sp_core::{storage::StorageKey, Pair};
use sp_runtime::{generic::SignedPayload, traits::IdentifyAccount}; use sp_runtime::{generic::SignedPayload, traits::IdentifyAccount};
use std::time::Duration; use std::time::Duration;
@@ -65,6 +65,12 @@ impl Chain for BridgeHubRococo {
type Call = runtime::Call; type Call = runtime::Call;
} }
impl ChainWithBalances for BridgeHubRococo {
fn account_info_storage_key(account_id: &Self::AccountId) -> StorageKey {
bp_bridge_hub_rococo::AccountInfoStorageMapKeyProvider::final_key(account_id)
}
}
impl ChainWithTransactions for BridgeHubRococo { impl ChainWithTransactions for BridgeHubRococo {
type AccountKeyPair = sp_core::sr25519::Pair; type AccountKeyPair = sp_core::sr25519::Pair;
type SignedTransaction = runtime::UncheckedExtrinsic; type SignedTransaction = runtime::UncheckedExtrinsic;
@@ -19,10 +19,10 @@
use bp_messages::{MessageNonce, Weight}; use bp_messages::{MessageNonce, Weight};
use codec::Encode; use codec::Encode;
use relay_substrate_client::{ use relay_substrate_client::{
Chain, ChainBase, ChainWithMessages, ChainWithTransactions, Error as SubstrateError, SignParam, Chain, ChainBase, ChainWithBalances, ChainWithMessages, ChainWithTransactions,
UnsignedTransaction, Error as SubstrateError, SignParam, UnsignedTransaction,
}; };
use sp_core::Pair; use sp_core::{storage::StorageKey, Pair};
use sp_runtime::{generic::SignedPayload, traits::IdentifyAccount}; use sp_runtime::{generic::SignedPayload, traits::IdentifyAccount};
use std::time::Duration; use std::time::Duration;
@@ -65,6 +65,12 @@ impl Chain for BridgeHubWococo {
type Call = runtime::Call; type Call = runtime::Call;
} }
impl ChainWithBalances for BridgeHubWococo {
fn account_info_storage_key(account_id: &Self::AccountId) -> StorageKey {
bp_bridge_hub_wococo::AccountInfoStorageMapKeyProvider::final_key(account_id)
}
}
impl ChainWithTransactions for BridgeHubWococo { impl ChainWithTransactions for BridgeHubWococo {
type AccountKeyPair = sp_core::sr25519::Pair; type AccountKeyPair = sp_core::sr25519::Pair;
type SignedTransaction = runtime::UncheckedExtrinsic; type SignedTransaction = runtime::UncheckedExtrinsic;
+2 -1
View File
@@ -16,6 +16,7 @@
//! Types used to connect to the Kusama chain. //! Types used to connect to the Kusama chain.
use bp_kusama::AccountInfoStorageMapKeyProvider;
use frame_support::weights::Weight; use frame_support::weights::Weight;
use relay_substrate_client::{Chain, ChainBase, ChainWithBalances, ChainWithGrandpa}; use relay_substrate_client::{Chain, ChainBase, ChainWithBalances, ChainWithGrandpa};
use sp_core::storage::StorageKey; use sp_core::storage::StorageKey;
@@ -65,7 +66,7 @@ impl ChainWithGrandpa for Kusama {
impl ChainWithBalances for Kusama { impl ChainWithBalances for Kusama {
fn account_info_storage_key(account_id: &Self::AccountId) -> StorageKey { fn account_info_storage_key(account_id: &Self::AccountId) -> StorageKey {
StorageKey(bp_kusama::account_info_storage_key(account_id)) AccountInfoStorageMapKeyProvider::final_key(account_id)
} }
} }
+2 -1
View File
@@ -16,6 +16,7 @@
//! Types used to connect to the Polkadot chain. //! Types used to connect to the Polkadot chain.
use bp_polkadot::AccountInfoStorageMapKeyProvider;
use frame_support::weights::Weight; use frame_support::weights::Weight;
use relay_substrate_client::{Chain, ChainBase, ChainWithBalances, ChainWithGrandpa}; use relay_substrate_client::{Chain, ChainBase, ChainWithBalances, ChainWithGrandpa};
use sp_core::storage::StorageKey; use sp_core::storage::StorageKey;
@@ -66,7 +67,7 @@ impl ChainWithGrandpa for Polkadot {
impl ChainWithBalances for Polkadot { impl ChainWithBalances for Polkadot {
fn account_info_storage_key(account_id: &Self::AccountId) -> StorageKey { fn account_info_storage_key(account_id: &Self::AccountId) -> StorageKey {
StorageKey(bp_polkadot::account_info_storage_key(account_id)) AccountInfoStorageMapKeyProvider::final_key(account_id)
} }
} }
+1 -1
View File
@@ -68,7 +68,7 @@ impl ChainWithGrandpa for Rococo {
impl ChainWithBalances for Rococo { impl ChainWithBalances for Rococo {
fn account_info_storage_key(account_id: &Self::AccountId) -> StorageKey { fn account_info_storage_key(account_id: &Self::AccountId) -> StorageKey {
StorageKey(bp_rococo::account_info_storage_key(account_id)) bp_rococo::AccountInfoStorageMapKeyProvider::final_key(account_id)
} }
} }
+1 -1
View File
@@ -75,7 +75,7 @@ impl ChainWithGrandpa for Westend {
impl ChainWithBalances for Westend { impl ChainWithBalances for Westend {
fn account_info_storage_key(account_id: &Self::AccountId) -> StorageKey { fn account_info_storage_key(account_id: &Self::AccountId) -> StorageKey {
StorageKey(bp_westend::account_info_storage_key(account_id)) bp_westend::AccountInfoStorageMapKeyProvider::final_key(account_id)
} }
} }
+1 -1
View File
@@ -68,7 +68,7 @@ impl ChainWithGrandpa for Wococo {
impl ChainWithBalances for Wococo { impl ChainWithBalances for Wococo {
fn account_info_storage_key(account_id: &Self::AccountId) -> StorageKey { fn account_info_storage_key(account_id: &Self::AccountId) -> StorageKey {
StorageKey(bp_wococo::account_info_storage_key(account_id)) bp_wococo::AccountInfoStorageMapKeyProvider::final_key(account_id)
} }
} }