Skip to content

Commit

Permalink
Add RococoBridgeHub <> WococoBridgeHub full 2 way bridge (paritytech#…
Browse files Browse the repository at this point in the history
…1663)

* Add RococoBridgeHub <> WococoBridgeHub full 2 way bridge

* Use StorageMapKeyProvider instead of account_info_storage_key()

Avoid duplicating storage_map_final_key()

* clippy + leftovers
  • Loading branch information
serban300 committed Apr 9, 2024
1 parent 4404eed commit bdc6dee
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 36 deletions.
2 changes: 1 addition & 1 deletion bridges/primitives/chain-bridge-hub-wococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

// Re-export only what is really needed
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,
SS58Prefix, Signature, SignedBlock, SignedExtensions, UncheckedExtrinsic, WeightToFee,
MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX, MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX,
Expand Down
40 changes: 21 additions & 19 deletions bridges/primitives/polkadot-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#![cfg_attr(not(feature = "std"), no_std)]

use bp_messages::MessageNonce;
use bp_runtime::{Chain, EncodedOrDecodedCall};
use bp_runtime::{Chain, EncodedOrDecodedCall, StorageMapKeyProvider};
use codec::Compact;
use frame_support::{
dispatch::{DispatchClass, Dispatchable},
Expand All @@ -26,11 +26,11 @@ use frame_support::{
constants::{BlockExecutionWeight, WEIGHT_PER_SECOND},
Weight,
},
Blake2_128Concat, RuntimeDebug, StorageHasher, Twox128,
Blake2_128Concat, RuntimeDebug,
};
use frame_system::limits;
use scale_info::{StaticTypeInfo, TypeInfo};
use sp_core::Hasher as HasherT;
use sp_core::{storage::StorageKey, Hasher as HasherT};
use sp_runtime::{
generic,
traits::{BlakeTwo256, DispatchInfoOf, IdentifyAccount, Verify},
Expand Down Expand Up @@ -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:
/// [link](https://github.com/paritytech/substrate/blob/c939ceba381b6313462d47334f775e128ea4e95d/frame/support/src/storage/generator/map.rs#L74)
/// We need to use this approach when we don't have access to the runtime.
/// 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> {
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(
module_prefix_hashed.len() + storage_prefix_hashed.len() + key_hashed.len(),
);
pub struct AccountInfoStorageMapKeyProvider;

impl StorageMapKeyProvider for AccountInfoStorageMapKeyProvider {
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[..]);
final_key.extend_from_slice(&storage_prefix_hashed[..]);
final_key.extend_from_slice(&key_hashed);
impl AccountInfoStorageMapKeyProvider {
const PALLET_NAME: &'static str = "System";

final_key
pub fn final_key(id: &AccountId) -> StorageKey {
<Self as StorageMapKeyProvider>::final_key(Self::PALLET_NAME, id)
}
}

#[cfg(test)]
Expand All @@ -379,7 +381,7 @@ mod tests {
25, 26, 27, 28, 29, 30, 31, 32,
]
.into();
let key = account_info_storage_key(&acc);
let key = AccountInfoStorageMapKeyProvider::final_key(&acc);
assert_eq!(hex::encode(key), "26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da92dccd599abfe1920a1cff8a7358231430102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

//! 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 parachains_relay::ParachainsPipeline;
use relay_substrate_client::{CallOf, HeaderIdOf};
Expand Down Expand Up @@ -76,3 +76,10 @@ impl CliBridgeBase for BridgeHubRococoToBridgeHubWococoCliBridge {
type Source = relay_bridge_hub_rococo_client::BridgeHubRococo;
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

//! 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 parachains_relay::ParachainsPipeline;
use relay_substrate_client::{CallOf, HeaderIdOf};
Expand Down Expand Up @@ -76,3 +76,10 @@ impl CliBridgeBase for BridgeHubWococoToBridgeHubRococoCliBridge {
type Source = relay_bridge_hub_wococo_client::BridgeHubWococo;
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@ use crate::{
millau_headers_to_rialto_parachain::MillauToRialtoParachainCliBridge,
rialto_headers_to_millau::RialtoToMillauCliBridge,
rialto_parachains_to_millau::RialtoParachainToMillauCliBridge,
rococo_parachains_to_bridge_hub_wococo::BridgeHubRococoToBridgeHubWococoCliBridge,
wococo_parachains_to_bridge_hub_rococo::BridgeHubWococoToBridgeHubRococoCliBridge,
},
cli::{
bridge::{
CliBridgeBase, MessagesCliBridge, ParachainToRelayHeadersCliBridge,
RelayToRelayHeadersCliBridge,
},
chain_schema::*,
relay_headers_and_messages::parachain_to_parachain::ParachainToParachainBridge,
CliChain, HexLaneId, PrometheusParams,
},
declare_chain_cli_schema,
Expand Down Expand Up @@ -190,14 +193,29 @@ where
declare_chain_cli_schema!(Millau, millau);
declare_chain_cli_schema!(Rialto, rialto);
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.
declare_chain_cli_schema!(MillauHeadersToRialto, millau_headers_to_rialto);
declare_chain_cli_schema!(MillauHeadersToRialtoParachain, millau_headers_to_rialto_parachain);
declare_chain_cli_schema!(RialtoHeadersToMillau, rialto_headers_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.
declare_relay_to_relay_bridge_schema!(Millau, 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.
///
Expand Down Expand Up @@ -410,13 +428,41 @@ 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.
#[derive(Debug, PartialEq, StructOpt)]
pub enum RelayHeadersAndMessages {
/// Millau <> Rialto relay.
MillauRialto(MillauRialtoHeadersAndMessages),
/// Millau <> RialtoParachain relay.
MillauRialtoParachain(MillauRialtoParachainHeadersAndMessages),
/// BridgeHubRococo <> BridgeHubWococo relay.
BridgeHubRococoBridgeHubWococo(BridgeHubRococoBridgeHubWococoHeadersAndMessages),
}

impl RelayHeadersAndMessages {
Expand All @@ -429,6 +475,10 @@ impl RelayHeadersAndMessages {
MillauRialtoParachainFull2WayBridge::new(params.into_bridge().await?)?
.run()
.await,
RelayHeadersAndMessages::BridgeHubRococoBridgeHubWococo(params) =>
BridgeHubRococoBridgeHubWococoFull2WayBridge::new(params.into_bridge().await?)?
.run()
.await,
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ macro_rules! declare_parachain_to_parachain_bridge_schema {
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<
Left: ChainWithTransactions + CliChain<KeyPair = AccountKeyPairOf<Left>>,
LeftRelay: CliChain,
Expand All @@ -132,7 +132,7 @@ macro_rules! declare_parachain_to_parachain_bridge_schema {
+ ParachainToRelayHeadersCliBridge<SourceRelay = RightRelay>,
>(
self,
) -> anyhow::Result<RelayToParachainBridge<L2R, R2L>> {
) -> anyhow::Result<ParachainToParachainBridge<L2R, R2L>> {
Ok(ParachainToParachainBridge {
common: Full2WayBridgeCommonParams::new::<L2R>(
self.shared,
Expand All @@ -151,7 +151,7 @@ macro_rules! declare_parachain_to_parachain_bridge_schema {
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_headers_to_left_transaction_params: self
.right_relay_headers_to_left_sign_override
Expand Down
12 changes: 9 additions & 3 deletions bridges/relays/client-bridge-hub-rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
use bp_messages::{MessageNonce, Weight};
use codec::Encode;
use relay_substrate_client::{
Chain, ChainBase, ChainWithMessages, ChainWithTransactions, Error as SubstrateError, SignParam,
UnsignedTransaction,
Chain, ChainBase, ChainWithBalances, ChainWithMessages, ChainWithTransactions,
Error as SubstrateError, SignParam, UnsignedTransaction,
};
use sp_core::Pair;
use sp_core::{storage::StorageKey, Pair};
use sp_runtime::{generic::SignedPayload, traits::IdentifyAccount};
use std::time::Duration;

Expand Down Expand Up @@ -65,6 +65,12 @@ impl Chain for BridgeHubRococo {
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 {
type AccountKeyPair = sp_core::sr25519::Pair;
type SignedTransaction = runtime::UncheckedExtrinsic;
Expand Down
12 changes: 9 additions & 3 deletions bridges/relays/client-bridge-hub-wococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
use bp_messages::{MessageNonce, Weight};
use codec::Encode;
use relay_substrate_client::{
Chain, ChainBase, ChainWithMessages, ChainWithTransactions, Error as SubstrateError, SignParam,
UnsignedTransaction,
Chain, ChainBase, ChainWithBalances, ChainWithMessages, ChainWithTransactions,
Error as SubstrateError, SignParam, UnsignedTransaction,
};
use sp_core::Pair;
use sp_core::{storage::StorageKey, Pair};
use sp_runtime::{generic::SignedPayload, traits::IdentifyAccount};
use std::time::Duration;

Expand Down Expand Up @@ -65,6 +65,12 @@ impl Chain for BridgeHubWococo {
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 {
type AccountKeyPair = sp_core::sr25519::Pair;
type SignedTransaction = runtime::UncheckedExtrinsic;
Expand Down
3 changes: 2 additions & 1 deletion bridges/relays/client-kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

//! Types used to connect to the Kusama chain.
use bp_kusama::AccountInfoStorageMapKeyProvider;
use frame_support::weights::Weight;
use relay_substrate_client::{Chain, ChainBase, ChainWithBalances, ChainWithGrandpa};
use sp_core::storage::StorageKey;
Expand Down Expand Up @@ -65,7 +66,7 @@ impl ChainWithGrandpa for Kusama {

impl ChainWithBalances for Kusama {
fn account_info_storage_key(account_id: &Self::AccountId) -> StorageKey {
StorageKey(bp_kusama::account_info_storage_key(account_id))
AccountInfoStorageMapKeyProvider::final_key(account_id)
}
}

Expand Down
3 changes: 2 additions & 1 deletion bridges/relays/client-polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

//! Types used to connect to the Polkadot chain.
use bp_polkadot::AccountInfoStorageMapKeyProvider;
use frame_support::weights::Weight;
use relay_substrate_client::{Chain, ChainBase, ChainWithBalances, ChainWithGrandpa};
use sp_core::storage::StorageKey;
Expand Down Expand Up @@ -66,7 +67,7 @@ impl ChainWithGrandpa for Polkadot {

impl ChainWithBalances for Polkadot {
fn account_info_storage_key(account_id: &Self::AccountId) -> StorageKey {
StorageKey(bp_polkadot::account_info_storage_key(account_id))
AccountInfoStorageMapKeyProvider::final_key(account_id)
}
}

Expand Down
2 changes: 1 addition & 1 deletion bridges/relays/client-rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl ChainWithGrandpa for Rococo {

impl ChainWithBalances for Rococo {
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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion bridges/relays/client-westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl ChainWithGrandpa for Westend {

impl ChainWithBalances for Westend {
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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion bridges/relays/client-wococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl ChainWithGrandpa for Wococo {

impl ChainWithBalances for Wococo {
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)
}
}

Expand Down

0 comments on commit bdc6dee

Please sign in to comment.