From 0f4245fbe9268b6027577b7cd28fef9b40f18b75 Mon Sep 17 00:00:00 2001 From: Svyatoslav Nikolsky Date: Thu, 13 May 2021 13:04:11 +0300 Subject: [PATCH] pass source and target chain ids to account_ownership_proof (#963) --- bridges/bin/millau/node/src/chain_spec.rs | 2 +- bridges/bin/millau/runtime/src/lib.rs | 5 +- .../bin/millau/runtime/src/rialto_messages.rs | 8 +- bridges/bin/rialto/node/src/chain_spec.rs | 2 +- bridges/bin/rialto/runtime/src/lib.rs | 5 +- .../bin/rialto/runtime/src/millau_messages.rs | 8 +- bridges/bin/runtime-common/src/messages.rs | 17 ++- bridges/modules/dispatch/src/lib.rs | 136 ++++++++++-------- bridges/primitives/chain-kusama/src/lib.rs | 2 +- bridges/primitives/chain-millau/src/lib.rs | 2 +- bridges/primitives/chain-polkadot/src/lib.rs | 2 +- bridges/primitives/chain-rialto/src/lib.rs | 2 +- bridges/primitives/chain-westend/src/lib.rs | 2 +- bridges/primitives/chain-wococo/src/lib.rs | 2 +- .../primitives/message-dispatch/src/lib.rs | 7 +- bridges/primitives/runtime/src/lib.rs | 30 ++-- .../src/chains/millau_messages_to_rialto.rs | 6 +- .../relays/bin-substrate/src/chains/mod.rs | 4 +- .../src/chains/rialto_messages_to_millau.rs | 6 +- .../relays/bin-substrate/src/cli/bridge.rs | 4 +- .../bin-substrate/src/messages_source.rs | 6 +- .../bin-substrate/src/messages_target.rs | 6 +- 22 files changed, 149 insertions(+), 115 deletions(-) diff --git a/bridges/bin/millau/node/src/chain_spec.rs b/bridges/bin/millau/node/src/chain_spec.rs index b0720344b62e..872b81a88ce3 100644 --- a/bridges/bin/millau/node/src/chain_spec.rs +++ b/bridges/bin/millau/node/src/chain_spec.rs @@ -72,7 +72,7 @@ impl Alternative { "tokenDecimals": 9, "tokenSymbol": "MLAU", "bridgeIds": { - "Rialto": bp_runtime::RIALTO_BRIDGE_INSTANCE, + "Rialto": bp_runtime::RIALTO_CHAIN_ID, } }) .as_object() diff --git a/bridges/bin/millau/runtime/src/lib.rs b/bridges/bin/millau/runtime/src/lib.rs index 4f5e08363200..8553045419e0 100644 --- a/bridges/bin/millau/runtime/src/lib.rs +++ b/bridges/bin/millau/runtime/src/lib.rs @@ -642,7 +642,7 @@ impl_runtime_apis! { /// The byte vector returned by this function should be signed with a Rialto account private key. /// This way, the owner of `millau_account_id` on Millau proves that the Rialto account private key /// is also under his control. -pub fn rialto_account_ownership_digest( +pub fn millau_to_rialto_account_ownership_digest( rialto_call: &Call, millau_account_id: AccountId, rialto_spec_version: SpecVersion, @@ -656,7 +656,8 @@ where rialto_call, millau_account_id, rialto_spec_version, - bp_runtime::MILLAU_BRIDGE_INSTANCE, + bp_runtime::MILLAU_CHAIN_ID, + bp_runtime::RIALTO_CHAIN_ID, ) } diff --git a/bridges/bin/millau/runtime/src/rialto_messages.rs b/bridges/bin/millau/runtime/src/rialto_messages.rs index a61d7cce2978..6641dc01b7db 100644 --- a/bridges/bin/millau/runtime/src/rialto_messages.rs +++ b/bridges/bin/millau/runtime/src/rialto_messages.rs @@ -23,7 +23,7 @@ use bp_messages::{ target_chain::{ProvedMessages, SourceHeaderChain}, InboundLaneData, LaneId, Message, MessageNonce, Parameter as MessagesParameter, }; -use bp_runtime::{InstanceId, RIALTO_BRIDGE_INSTANCE}; +use bp_runtime::{ChainId, MILLAU_CHAIN_ID, RIALTO_CHAIN_ID}; use bridge_runtime_common::messages::{self, MessageBridge, MessageTransaction}; use codec::{Decode, Encode}; use frame_support::{ @@ -72,8 +72,6 @@ pub type FromRialtoMessageDispatch = messages::target::FromBridgedChainMessageDi pub struct WithRialtoMessageBridge; impl MessageBridge for WithRialtoMessageBridge { - const INSTANCE: InstanceId = RIALTO_BRIDGE_INSTANCE; - const RELAYER_FEE_PERCENT: u32 = 10; type ThisChain = Millau; @@ -90,6 +88,8 @@ impl MessageBridge for WithRialtoMessageBridge { pub struct Millau; impl messages::ChainWithMessages for Millau { + const ID: ChainId = MILLAU_CHAIN_ID; + type Hash = bp_millau::Hash; type AccountId = bp_millau::AccountId; type Signer = bp_millau::AccountSigner; @@ -141,6 +141,8 @@ impl messages::ThisChainWithMessages for Millau { pub struct Rialto; impl messages::ChainWithMessages for Rialto { + const ID: ChainId = RIALTO_CHAIN_ID; + type Hash = bp_rialto::Hash; type AccountId = bp_rialto::AccountId; type Signer = bp_rialto::AccountSigner; diff --git a/bridges/bin/rialto/node/src/chain_spec.rs b/bridges/bin/rialto/node/src/chain_spec.rs index db2ef4c60bc3..17b13c88fbb3 100644 --- a/bridges/bin/rialto/node/src/chain_spec.rs +++ b/bridges/bin/rialto/node/src/chain_spec.rs @@ -73,7 +73,7 @@ impl Alternative { "tokenDecimals": 9, "tokenSymbol": "RLT", "bridgeIds": { - "Millau": bp_runtime::MILLAU_BRIDGE_INSTANCE, + "Millau": bp_runtime::MILLAU_CHAIN_ID, } }) .as_object() diff --git a/bridges/bin/rialto/runtime/src/lib.rs b/bridges/bin/rialto/runtime/src/lib.rs index 5259a10493a7..e67a1c64cd41 100644 --- a/bridges/bin/rialto/runtime/src/lib.rs +++ b/bridges/bin/rialto/runtime/src/lib.rs @@ -1026,7 +1026,7 @@ impl_runtime_apis! { /// The byte vector returned by this function should be signed with a Millau account private key. /// This way, the owner of `rialto_account_id` on Rialto proves that the 'millau' account private key /// is also under his control. -pub fn millau_account_ownership_digest( +pub fn rialto_to_millau_account_ownership_digest( millau_call: &Call, rialto_account_id: AccountId, millau_spec_version: SpecVersion, @@ -1040,7 +1040,8 @@ where millau_call, rialto_account_id, millau_spec_version, - bp_runtime::RIALTO_BRIDGE_INSTANCE, + bp_runtime::RIALTO_CHAIN_ID, + bp_runtime::MILLAU_CHAIN_ID, ) } diff --git a/bridges/bin/rialto/runtime/src/millau_messages.rs b/bridges/bin/rialto/runtime/src/millau_messages.rs index 30a34b9276ce..5c1779a7fea9 100644 --- a/bridges/bin/rialto/runtime/src/millau_messages.rs +++ b/bridges/bin/rialto/runtime/src/millau_messages.rs @@ -23,7 +23,7 @@ use bp_messages::{ target_chain::{ProvedMessages, SourceHeaderChain}, InboundLaneData, LaneId, Message, MessageNonce, Parameter as MessagesParameter, }; -use bp_runtime::{InstanceId, MILLAU_BRIDGE_INSTANCE}; +use bp_runtime::{ChainId, MILLAU_CHAIN_ID, RIALTO_CHAIN_ID}; use bridge_runtime_common::messages::{self, MessageBridge, MessageTransaction}; use codec::{Decode, Encode}; use frame_support::{ @@ -72,8 +72,6 @@ pub type ToMillauMessagesDeliveryProof = messages::source::FromBridgedChainMessa pub struct WithMillauMessageBridge; impl MessageBridge for WithMillauMessageBridge { - const INSTANCE: InstanceId = MILLAU_BRIDGE_INSTANCE; - const RELAYER_FEE_PERCENT: u32 = 10; type ThisChain = Rialto; @@ -90,6 +88,8 @@ impl MessageBridge for WithMillauMessageBridge { pub struct Rialto; impl messages::ChainWithMessages for Rialto { + const ID: ChainId = RIALTO_CHAIN_ID; + type Hash = bp_rialto::Hash; type AccountId = bp_rialto::AccountId; type Signer = bp_rialto::AccountSigner; @@ -141,6 +141,8 @@ impl messages::ThisChainWithMessages for Rialto { pub struct Millau; impl messages::ChainWithMessages for Millau { + const ID: ChainId = MILLAU_CHAIN_ID; + type Hash = bp_millau::Hash; type AccountId = bp_millau::AccountId; type Signer = bp_millau::AccountSigner; diff --git a/bridges/bin/runtime-common/src/messages.rs b/bridges/bin/runtime-common/src/messages.rs index e879aa17bfb2..487fb356f9f3 100644 --- a/bridges/bin/runtime-common/src/messages.rs +++ b/bridges/bin/runtime-common/src/messages.rs @@ -26,7 +26,7 @@ use bp_messages::{ target_chain::{DispatchMessage, MessageDispatch, ProvedLaneMessages, ProvedMessages}, InboundLaneData, LaneId, Message, MessageData, MessageKey, MessageNonce, OutboundLaneData, }; -use bp_runtime::{InstanceId, Size, StorageProofChecker}; +use bp_runtime::{ChainId, Size, StorageProofChecker}; use codec::{Decode, Encode}; use frame_support::{traits::Instance, weights::Weight, RuntimeDebug}; use hash_db::Hasher; @@ -39,9 +39,6 @@ use sp_trie::StorageProof; /// Bidirectional message bridge. pub trait MessageBridge { - /// Instance id of this bridge. - const INSTANCE: InstanceId; - /// Relayer interest (in percents). const RELAYER_FEE_PERCENT: u32; @@ -56,6 +53,9 @@ pub trait MessageBridge { /// Chain that has `pallet-bridge-messages` and `dispatch` modules. pub trait ChainWithMessages { + /// Identifier of this chain. + const ID: ChainId; + /// Hash used in the chain. type Hash: Decode; /// Accound id on the chain. @@ -483,7 +483,8 @@ pub mod target { fn dispatch(message: DispatchMessage>>) { let message_id = (message.key.lane_id, message.key.nonce); pallet_bridge_dispatch::Pallet::::dispatch( - B::INSTANCE, + B::BridgedChain::ID, + B::ThisChain::ID, message_id, message.data.payload.map_err(drop), ); @@ -692,7 +693,6 @@ mod tests { struct OnThisChainBridge; impl MessageBridge for OnThisChainBridge { - const INSTANCE: InstanceId = *b"this"; const RELAYER_FEE_PERCENT: u32 = 10; type ThisChain = ThisChain; @@ -708,7 +708,6 @@ mod tests { struct OnBridgedChainBridge; impl MessageBridge for OnBridgedChainBridge { - const INSTANCE: InstanceId = *b"brdg"; const RELAYER_FEE_PERCENT: u32 = 20; type ThisChain = BridgedChain; @@ -809,6 +808,8 @@ mod tests { struct ThisChain; impl ChainWithMessages for ThisChain { + const ID: ChainId = *b"this"; + type Hash = (); type AccountId = ThisChainAccountId; type Signer = ThisChainSigner; @@ -866,6 +867,8 @@ mod tests { struct BridgedChain; impl ChainWithMessages for BridgedChain { + const ID: ChainId = *b"brdg"; + type Hash = (); type AccountId = BridgedChainAccountId; type Signer = BridgedChainSigner; diff --git a/bridges/modules/dispatch/src/lib.rs b/bridges/modules/dispatch/src/lib.rs index 0b0074c20b76..7803c1e540e1 100644 --- a/bridges/modules/dispatch/src/lib.rs +++ b/bridges/modules/dispatch/src/lib.rs @@ -25,7 +25,7 @@ #![warn(missing_docs)] use bp_message_dispatch::{CallOrigin, MessageDispatch, MessagePayload, SpecVersion, Weight}; -use bp_runtime::{derive_account_id, InstanceId, SourceAccount}; +use bp_runtime::{derive_account_id, ChainId, SourceAccount}; use codec::{Decode, Encode}; use frame_support::{ decl_event, decl_module, decl_storage, @@ -90,21 +90,21 @@ decl_event!( >::MessageId { /// Message has been rejected before reaching dispatch. - MessageRejected(InstanceId, MessageId), + MessageRejected(ChainId, MessageId), /// Message has been rejected by dispatcher because of spec version mismatch. /// Last two arguments are: expected and passed spec version. - MessageVersionSpecMismatch(InstanceId, MessageId, SpecVersion, SpecVersion), + MessageVersionSpecMismatch(ChainId, MessageId, SpecVersion, SpecVersion), /// Message has been rejected by dispatcher because of weight mismatch. /// Last two arguments are: expected and passed call weight. - MessageWeightMismatch(InstanceId, MessageId, Weight, Weight), + MessageWeightMismatch(ChainId, MessageId, Weight, Weight), /// Message signature mismatch. - MessageSignatureMismatch(InstanceId, MessageId), + MessageSignatureMismatch(ChainId, MessageId), /// Message has been dispatched with given result. - MessageDispatched(InstanceId, MessageId, DispatchResult), + MessageDispatched(ChainId, MessageId, DispatchResult), /// We have failed to decode Call from the message. - MessageCallDecodeFailed(InstanceId, MessageId), + MessageCallDecodeFailed(ChainId, MessageId), /// The call from the message has been rejected by the call filter. - MessageCallRejected(InstanceId, MessageId), + MessageCallRejected(ChainId, MessageId), /// Phantom member, never used. Needed to handle multiple pallet instances. _Dummy(PhantomData), } @@ -126,13 +126,18 @@ impl, I: Instance> MessageDispatch for Pallet { message.weight } - fn dispatch(bridge: InstanceId, id: T::MessageId, message: Result) { + fn dispatch(source_chain: ChainId, target_chain: ChainId, id: T::MessageId, message: Result) { // emit special even if message has been rejected by external component let message = match message { Ok(message) => message, Err(_) => { - log::trace!(target: "runtime::bridge-dispatch", "Message {:?}/{:?}: rejected before actual dispatch", bridge, id); - Self::deposit_event(RawEvent::MessageRejected(bridge, id)); + log::trace!( + target: "runtime::bridge-dispatch", + "Message {:?}/{:?}: rejected before actual dispatch", + source_chain, + id, + ); + Self::deposit_event(RawEvent::MessageRejected(source_chain, id)); return; } }; @@ -143,13 +148,13 @@ impl, I: Instance> MessageDispatch for Pallet { if message.spec_version != expected_version { log::trace!( "Message {:?}/{:?}: spec_version mismatch. Expected {:?}, got {:?}", - bridge, + source_chain, id, expected_version, message.spec_version, ); Self::deposit_event(RawEvent::MessageVersionSpecMismatch( - bridge, + source_chain, id, expected_version, message.spec_version, @@ -161,8 +166,13 @@ impl, I: Instance> MessageDispatch for Pallet { let call = match message.call.into() { Ok(call) => call, Err(_) => { - log::trace!(target: "runtime::bridge-dispatch", "Failed to decode Call from message {:?}/{:?}", bridge, id,); - Self::deposit_event(RawEvent::MessageCallDecodeFailed(bridge, id)); + log::trace!( + target: "runtime::bridge-dispatch", + "Failed to decode Call from message {:?}/{:?}", + source_chain, + id, + ); + Self::deposit_event(RawEvent::MessageCallDecodeFailed(source_chain, id)); return; } }; @@ -170,25 +180,31 @@ impl, I: Instance> MessageDispatch for Pallet { // prepare dispatch origin let origin_account = match message.origin { CallOrigin::SourceRoot => { - let hex_id = derive_account_id::(bridge, SourceAccount::Root); + let hex_id = derive_account_id::(source_chain, SourceAccount::Root); let target_id = T::AccountIdConverter::convert(hex_id); log::trace!(target: "runtime::bridge-dispatch", "Root Account: {:?}", &target_id); target_id } CallOrigin::TargetAccount(source_account_id, target_public, target_signature) => { - let digest = account_ownership_digest(&call, source_account_id, message.spec_version, bridge); + let digest = account_ownership_digest( + &call, + source_account_id, + message.spec_version, + source_chain, + target_chain, + ); let target_account = target_public.into_account(); if !target_signature.verify(&digest[..], &target_account) { log::trace!( target: "runtime::bridge-dispatch", "Message {:?}/{:?}: origin proof is invalid. Expected account: {:?} from signature: {:?}", - bridge, + source_chain, id, target_account, target_signature, ); - Self::deposit_event(RawEvent::MessageSignatureMismatch(bridge, id)); + Self::deposit_event(RawEvent::MessageSignatureMismatch(source_chain, id)); return; } @@ -196,7 +212,7 @@ impl, I: Instance> MessageDispatch for Pallet { target_account } CallOrigin::SourceAccount(source_account_id) => { - let hex_id = derive_account_id(bridge, SourceAccount::Account(source_account_id)); + let hex_id = derive_account_id(source_chain, SourceAccount::Account(source_account_id)); let target_id = T::AccountIdConverter::convert(hex_id); log::trace!(target: "runtime::bridge-dispatch", "Source Account: {:?}", &target_id); target_id @@ -208,11 +224,11 @@ impl, I: Instance> MessageDispatch for Pallet { log::trace!( target: "runtime::bridge-dispatch", "Message {:?}/{:?}: the call ({:?}) is rejected by filter", - bridge, + source_chain, id, call, ); - Self::deposit_event(RawEvent::MessageCallRejected(bridge, id)); + Self::deposit_event(RawEvent::MessageCallRejected(source_chain, id)); return; } @@ -225,13 +241,13 @@ impl, I: Instance> MessageDispatch for Pallet { log::trace!( target: "runtime::bridge-dispatch", "Message {:?}/{:?}: passed weight is too low. Expected at least {:?}, got {:?}", - bridge, + source_chain, id, expected_weight, message.weight, ); Self::deposit_event(RawEvent::MessageWeightMismatch( - bridge, + source_chain, id, expected_weight, message.weight, @@ -248,7 +264,7 @@ impl, I: Instance> MessageDispatch for Pallet { log::trace!( target: "runtime::bridge-dispatch", "Message {:?}/{:?} has been dispatched. Weight: {} of {}. Result: {:?}", - bridge, + source_chain, id, actual_call_weight, message.weight, @@ -256,7 +272,7 @@ impl, I: Instance> MessageDispatch for Pallet { ); Self::deposit_event(RawEvent::MessageDispatched( - bridge, + source_chain, id, dispatch_result.map(drop).map_err(|e| e.error), )); @@ -303,23 +319,24 @@ where /// The byte vector returned by this function will be signed with a target chain account /// private key. This way, the owner of `source_account_id` on the source chain proves that /// the target chain account private key is also under his control. -pub fn account_ownership_digest( +pub fn account_ownership_digest( call: &Call, source_account_id: AccountId, target_spec_version: SpecVersion, - source_instance_id: BridgeId, + source_chain_id: ChainId, + target_chain_id: ChainId, ) -> Vec where Call: Encode, AccountId: Encode, SpecVersion: Encode, - BridgeId: Encode, { let mut proof = Vec::new(); call.encode_to(&mut proof); source_account_id.encode_to(&mut proof); target_spec_version.encode_to(&mut proof); - source_instance_id.encode_to(&mut proof); + source_chain_id.encode_to(&mut proof); + target_chain_id.encode_to(&mut proof); proof } @@ -342,6 +359,9 @@ mod tests { type AccountId = u64; type MessageId = [u8; 4]; + const SOURCE_CHAIN_ID: ChainId = *b"srce"; + const TARGET_CHAIN_ID: ChainId = *b"trgt"; + #[derive(Debug, Encode, Decode, Clone, PartialEq, Eq)] pub struct TestAccountPublic(AccountId); @@ -495,7 +515,6 @@ mod tests { #[test] fn should_fail_on_spec_version_mismatch() { new_test_ext().execute_with(|| { - let bridge = b"ethb".to_owned(); let id = [0; 4]; const BAD_SPEC_VERSION: SpecVersion = 99; @@ -504,14 +523,14 @@ mod tests { message.spec_version = BAD_SPEC_VERSION; System::set_block_number(1); - Dispatch::dispatch(bridge, id, Ok(message)); + Dispatch::dispatch(SOURCE_CHAIN_ID, TARGET_CHAIN_ID, id, Ok(message)); assert_eq!( System::events(), vec![EventRecord { phase: Phase::Initialization, event: Event::call_dispatch(call_dispatch::Event::::MessageVersionSpecMismatch( - bridge, + SOURCE_CHAIN_ID, id, TEST_SPEC_VERSION, BAD_SPEC_VERSION @@ -525,21 +544,23 @@ mod tests { #[test] fn should_fail_on_weight_mismatch() { new_test_ext().execute_with(|| { - let bridge = b"ethb".to_owned(); let id = [0; 4]; let mut message = prepare_root_message(Call::System(>::remark(vec![1, 2, 3]))); message.weight = 0; System::set_block_number(1); - Dispatch::dispatch(bridge, id, Ok(message)); + Dispatch::dispatch(SOURCE_CHAIN_ID, TARGET_CHAIN_ID, id, Ok(message)); assert_eq!( System::events(), vec![EventRecord { phase: Phase::Initialization, event: Event::call_dispatch(call_dispatch::Event::::MessageWeightMismatch( - bridge, id, 1345000, 0, + SOURCE_CHAIN_ID, + id, + 1345000, + 0, )), topics: vec![], }], @@ -550,7 +571,6 @@ mod tests { #[test] fn should_fail_on_signature_mismatch() { new_test_ext().execute_with(|| { - let bridge = b"ethb".to_owned(); let id = [0; 4]; let call_origin = CallOrigin::TargetAccount(1, TestAccountPublic(1), TestSignature(99)); @@ -560,14 +580,15 @@ mod tests { ); System::set_block_number(1); - Dispatch::dispatch(bridge, id, Ok(message)); + Dispatch::dispatch(SOURCE_CHAIN_ID, TARGET_CHAIN_ID, id, Ok(message)); assert_eq!( System::events(), vec![EventRecord { phase: Phase::Initialization, event: Event::call_dispatch(call_dispatch::Event::::MessageSignatureMismatch( - bridge, id + SOURCE_CHAIN_ID, + id )), topics: vec![], }], @@ -578,17 +599,19 @@ mod tests { #[test] fn should_emit_event_for_rejected_messages() { new_test_ext().execute_with(|| { - let bridge = b"ethb".to_owned(); let id = [0; 4]; System::set_block_number(1); - Dispatch::dispatch(bridge, id, Err(())); + Dispatch::dispatch(SOURCE_CHAIN_ID, TARGET_CHAIN_ID, id, Err(())); assert_eq!( System::events(), vec![EventRecord { phase: Phase::Initialization, - event: Event::call_dispatch(call_dispatch::Event::::MessageRejected(bridge, id)), + event: Event::call_dispatch(call_dispatch::Event::::MessageRejected( + SOURCE_CHAIN_ID, + id + )), topics: vec![], }], ); @@ -598,7 +621,6 @@ mod tests { #[test] fn should_fail_on_call_decode() { new_test_ext().execute_with(|| { - let bridge = b"ethb".to_owned(); let id = [0; 4]; let mut message = @@ -606,14 +628,15 @@ mod tests { message.call.0 = vec![]; System::set_block_number(1); - Dispatch::dispatch(bridge, id, Ok(message)); + Dispatch::dispatch(SOURCE_CHAIN_ID, TARGET_CHAIN_ID, id, Ok(message)); assert_eq!( System::events(), vec![EventRecord { phase: Phase::Initialization, event: Event::call_dispatch(call_dispatch::Event::::MessageCallDecodeFailed( - bridge, id + SOURCE_CHAIN_ID, + id )), topics: vec![], }], @@ -624,7 +647,6 @@ mod tests { #[test] fn should_emit_event_for_rejected_calls() { new_test_ext().execute_with(|| { - let bridge = b"ethb".to_owned(); let id = [0; 4]; let call = Call::System(>::fill_block(Perbill::from_percent(75))); @@ -633,13 +655,16 @@ mod tests { message.weight = weight; System::set_block_number(1); - Dispatch::dispatch(bridge, id, Ok(message)); + Dispatch::dispatch(SOURCE_CHAIN_ID, TARGET_CHAIN_ID, id, Ok(message)); assert_eq!( System::events(), vec![EventRecord { phase: Phase::Initialization, - event: Event::call_dispatch(call_dispatch::Event::::MessageCallRejected(bridge, id)), + event: Event::call_dispatch(call_dispatch::Event::::MessageCallRejected( + SOURCE_CHAIN_ID, + id + )), topics: vec![], }], ); @@ -649,19 +674,18 @@ mod tests { #[test] fn should_dispatch_bridge_message_from_root_origin() { new_test_ext().execute_with(|| { - let bridge = b"ethb".to_owned(); let id = [0; 4]; let message = prepare_root_message(Call::System(>::remark(vec![1, 2, 3]))); System::set_block_number(1); - Dispatch::dispatch(bridge, id, Ok(message)); + Dispatch::dispatch(SOURCE_CHAIN_ID, TARGET_CHAIN_ID, id, Ok(message)); assert_eq!( System::events(), vec![EventRecord { phase: Phase::Initialization, event: Event::call_dispatch(call_dispatch::Event::::MessageDispatched( - bridge, + SOURCE_CHAIN_ID, id, Ok(()) )), @@ -675,20 +699,19 @@ mod tests { fn should_dispatch_bridge_message_from_target_origin() { new_test_ext().execute_with(|| { let id = [0; 4]; - let bridge = b"ethb".to_owned(); let call = Call::System(>::remark(vec![])); let message = prepare_target_message(call); System::set_block_number(1); - Dispatch::dispatch(bridge, id, Ok(message)); + Dispatch::dispatch(SOURCE_CHAIN_ID, TARGET_CHAIN_ID, id, Ok(message)); assert_eq!( System::events(), vec![EventRecord { phase: Phase::Initialization, event: Event::call_dispatch(call_dispatch::Event::::MessageDispatched( - bridge, + SOURCE_CHAIN_ID, id, Ok(()) )), @@ -702,20 +725,19 @@ mod tests { fn should_dispatch_bridge_message_from_source_origin() { new_test_ext().execute_with(|| { let id = [0; 4]; - let bridge = b"ethb".to_owned(); let call = Call::System(>::remark(vec![])); let message = prepare_source_message(call); System::set_block_number(1); - Dispatch::dispatch(bridge, id, Ok(message)); + Dispatch::dispatch(SOURCE_CHAIN_ID, TARGET_CHAIN_ID, id, Ok(message)); assert_eq!( System::events(), vec![EventRecord { phase: Phase::Initialization, event: Event::call_dispatch(call_dispatch::Event::::MessageDispatched( - bridge, + SOURCE_CHAIN_ID, id, Ok(()) )), diff --git a/bridges/primitives/chain-kusama/src/lib.rs b/bridges/primitives/chain-kusama/src/lib.rs index 7163d15ef137..758bba5a718b 100644 --- a/bridges/primitives/chain-kusama/src/lib.rs +++ b/bridges/primitives/chain-kusama/src/lib.rs @@ -31,7 +31,7 @@ pub type Kusama = PolkadotLike; // 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 { - let encoded_id = bp_runtime::derive_account_id(bp_runtime::POLKADOT_BRIDGE_INSTANCE, id); + let encoded_id = bp_runtime::derive_account_id(bp_runtime::POLKADOT_CHAIN_ID, id); AccountIdConverter::convert(encoded_id) } diff --git a/bridges/primitives/chain-millau/src/lib.rs b/bridges/primitives/chain-millau/src/lib.rs index a5f3a888f9ad..7b88b7c49ad0 100644 --- a/bridges/primitives/chain-millau/src/lib.rs +++ b/bridges/primitives/chain-millau/src/lib.rs @@ -201,7 +201,7 @@ impl sp_runtime::traits::Convert for AccountIdConverte /// /// Note that this should only be used for testing. pub fn derive_account_from_rialto_id(id: bp_runtime::SourceAccount) -> AccountId { - let encoded_id = bp_runtime::derive_account_id(bp_runtime::RIALTO_BRIDGE_INSTANCE, id); + let encoded_id = bp_runtime::derive_account_id(bp_runtime::RIALTO_CHAIN_ID, id); AccountIdConverter::convert(encoded_id) } diff --git a/bridges/primitives/chain-polkadot/src/lib.rs b/bridges/primitives/chain-polkadot/src/lib.rs index 8e0d30cdb607..7cd155d64a8f 100644 --- a/bridges/primitives/chain-polkadot/src/lib.rs +++ b/bridges/primitives/chain-polkadot/src/lib.rs @@ -31,7 +31,7 @@ pub type Polkadot = PolkadotLike; // 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 { - let encoded_id = bp_runtime::derive_account_id(bp_runtime::KUSAMA_BRIDGE_INSTANCE, id); + let encoded_id = bp_runtime::derive_account_id(bp_runtime::KUSAMA_CHAIN_ID, id); AccountIdConverter::convert(encoded_id) } diff --git a/bridges/primitives/chain-rialto/src/lib.rs b/bridges/primitives/chain-rialto/src/lib.rs index c063dc72a7f2..3cd19739d46f 100644 --- a/bridges/primitives/chain-rialto/src/lib.rs +++ b/bridges/primitives/chain-rialto/src/lib.rs @@ -162,7 +162,7 @@ impl Convert for AccountIdConverter { // // Note that this should only be used for testing. pub fn derive_account_from_millau_id(id: bp_runtime::SourceAccount) -> AccountId { - let encoded_id = bp_runtime::derive_account_id(bp_runtime::MILLAU_BRIDGE_INSTANCE, id); + let encoded_id = bp_runtime::derive_account_id(bp_runtime::MILLAU_CHAIN_ID, id); AccountIdConverter::convert(encoded_id) } diff --git a/bridges/primitives/chain-westend/src/lib.rs b/bridges/primitives/chain-westend/src/lib.rs index db97364ef419..42298100f670 100644 --- a/bridges/primitives/chain-westend/src/lib.rs +++ b/bridges/primitives/chain-westend/src/lib.rs @@ -86,7 +86,7 @@ impl sp_runtime::traits::Dispatchable for Call { // We use this to get the account on Westend (target) which is derived from Rococo's (source) // account. pub fn derive_account_from_rococo_id(id: bp_runtime::SourceAccount) -> AccountId { - let encoded_id = bp_runtime::derive_account_id(bp_runtime::ROCOCO_BRIDGE_INSTANCE, id); + let encoded_id = bp_runtime::derive_account_id(bp_runtime::ROCOCO_CHAIN_ID, id); AccountIdConverter::convert(encoded_id) } diff --git a/bridges/primitives/chain-wococo/src/lib.rs b/bridges/primitives/chain-wococo/src/lib.rs index 13462a820b89..bcb238e88ba3 100644 --- a/bridges/primitives/chain-wococo/src/lib.rs +++ b/bridges/primitives/chain-wococo/src/lib.rs @@ -86,7 +86,7 @@ impl sp_runtime::traits::Dispatchable for Call { // We use this to get the account on Wococo (target) which is derived from Rococo's (source) // account. pub fn derive_account_from_rococo_id(id: bp_runtime::SourceAccount) -> AccountId { - let encoded_id = bp_runtime::derive_account_id(bp_runtime::ROCOCO_BRIDGE_INSTANCE, id); + let encoded_id = bp_runtime::derive_account_id(bp_runtime::ROCOCO_CHAIN_ID, id); AccountIdConverter::convert(encoded_id) } diff --git a/bridges/primitives/message-dispatch/src/lib.rs b/bridges/primitives/message-dispatch/src/lib.rs index 124437123964..a7b07abc9836 100644 --- a/bridges/primitives/message-dispatch/src/lib.rs +++ b/bridges/primitives/message-dispatch/src/lib.rs @@ -19,7 +19,7 @@ #![cfg_attr(not(feature = "std"), no_std)] #![warn(missing_docs)] -use bp_runtime::{InstanceId, Size}; +use bp_runtime::{ChainId, Size}; use codec::{Decode, Encode}; use frame_support::RuntimeDebug; use sp_std::prelude::*; @@ -43,7 +43,8 @@ pub trait MessageDispatch { /// Dispatches the message internally. /// - /// `bridge` indicates instance of deployed bridge where the message came from. + /// `source_chain` indicates the chain where the message came from. + /// `target_chain` indicates the chain where message dispatch happens. /// /// `id` is a short unique identifier of the message. /// @@ -51,7 +52,7 @@ pub trait MessageDispatch { /// a sign that some other component has rejected the message even before it has /// reached `dispatch` method (right now this may only be caused if we fail to decode /// the whole message). - fn dispatch(bridge: InstanceId, id: MessageId, message: Result); + fn dispatch(source_chain: ChainId, target_chain: ChainId, id: MessageId, message: Result); } /// Origin of a Call when it is dispatched on the target chain. diff --git a/bridges/primitives/runtime/src/lib.rs b/bridges/primitives/runtime/src/lib.rs index aa457f1d0b0b..80f644a588b9 100644 --- a/bridges/primitives/runtime/src/lib.rs +++ b/bridges/primitives/runtime/src/lib.rs @@ -33,25 +33,25 @@ mod chain; mod storage_proof; /// Use this when something must be shared among all instances. -pub const NO_INSTANCE_ID: InstanceId = [0, 0, 0, 0]; +pub const NO_INSTANCE_ID: ChainId = [0, 0, 0, 0]; /// Bridge-with-Rialto instance id. -pub const RIALTO_BRIDGE_INSTANCE: InstanceId = *b"rlto"; +pub const RIALTO_CHAIN_ID: ChainId = *b"rlto"; /// Bridge-with-Millau instance id. -pub const MILLAU_BRIDGE_INSTANCE: InstanceId = *b"mlau"; +pub const MILLAU_CHAIN_ID: ChainId = *b"mlau"; /// Bridge-with-Polkadot instance id. -pub const POLKADOT_BRIDGE_INSTANCE: InstanceId = *b"pdot"; +pub const POLKADOT_CHAIN_ID: ChainId = *b"pdot"; /// Bridge-with-Kusama instance id. -pub const KUSAMA_BRIDGE_INSTANCE: InstanceId = *b"ksma"; +pub const KUSAMA_CHAIN_ID: ChainId = *b"ksma"; /// Bridge-with-Rococo instance id. -pub const ROCOCO_BRIDGE_INSTANCE: InstanceId = *b"roco"; +pub const ROCOCO_CHAIN_ID: ChainId = *b"roco"; /// Bridge-with-Wococo instance id. -pub const WOCOCO_BRIDGE_INSTANCE: InstanceId = *b"woco"; +pub const WOCOCO_CHAIN_ID: ChainId = *b"woco"; /// Call-dispatch module prefix. pub const CALL_DISPATCH_MODULE_PREFIX: &[u8] = b"pallet-bridge/dispatch"; @@ -62,11 +62,13 @@ 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"; -/// Id of deployed 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 is used for that. -pub type InstanceId = [u8; 4]; +/// 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. +pub type ChainId = [u8; 4]; /// Type of accounts on the source chain. pub enum SourceAccount { @@ -90,7 +92,7 @@ pub enum SourceAccount { /// Note: If the same `bridge_id` is used across different chains (for example, if one source chain /// is bridged to multiple target chains), then all the derived accounts would be the same across /// the different chains. This could negatively impact users' privacy across chains. -pub fn derive_account_id(bridge_id: InstanceId, id: SourceAccount) -> H256 +pub fn derive_account_id(bridge_id: ChainId, id: SourceAccount) -> H256 where AccountId: Encode, { @@ -107,7 +109,7 @@ where /// /// 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: InstanceId) -> H256 { +pub fn derive_relayer_fund_account_id(bridge_id: ChainId) -> H256 { ("relayer-fund-account", bridge_id).using_encoded(blake2_256).into() } diff --git a/bridges/relays/bin-substrate/src/chains/millau_messages_to_rialto.rs b/bridges/relays/bin-substrate/src/chains/millau_messages_to_rialto.rs index d96fa7b79720..65dbe30d5a4a 100644 --- a/bridges/relays/bin-substrate/src/chains/millau_messages_to_rialto.rs +++ b/bridges/relays/bin-substrate/src/chains/millau_messages_to_rialto.rs @@ -23,7 +23,7 @@ use crate::messages_source::SubstrateMessagesSource; use crate::messages_target::SubstrateMessagesTarget; use bp_messages::MessageNonce; -use bp_runtime::{MILLAU_BRIDGE_INSTANCE, RIALTO_BRIDGE_INSTANCE}; +use bp_runtime::{MILLAU_CHAIN_ID, RIALTO_CHAIN_ID}; use bridge_runtime_common::messages::target::FromBridgedChainMessagesProof; use codec::Encode; use frame_support::dispatch::GetDispatchInfo; @@ -200,14 +200,14 @@ pub async fn run( source_client.clone(), lane.clone(), lane_id, - RIALTO_BRIDGE_INSTANCE, + RIALTO_CHAIN_ID, params.target_to_source_headers_relay, ), RialtoTargetClient::new( params.target_client, lane, lane_id, - MILLAU_BRIDGE_INSTANCE, + MILLAU_CHAIN_ID, params.source_to_target_headers_relay, ), relay_utils::relay_metrics( diff --git a/bridges/relays/bin-substrate/src/chains/mod.rs b/bridges/relays/bin-substrate/src/chains/mod.rs index b4061ced37f7..7b7b76863581 100644 --- a/bridges/relays/bin-substrate/src/chains/mod.rs +++ b/bridges/relays/bin-substrate/src/chains/mod.rs @@ -86,7 +86,7 @@ mod tests { let millau_public: bp_millau::AccountSigner = millau_sign.public().into(); let millau_account_id: bp_millau::AccountId = millau_public.into_account(); - let digest = millau_runtime::rialto_account_ownership_digest( + let digest = millau_runtime::millau_to_rialto_account_ownership_digest( &call, millau_account_id, rialto_runtime::VERSION.spec_version, @@ -107,7 +107,7 @@ mod tests { let rialto_public: bp_rialto::AccountSigner = rialto_sign.public().into(); let rialto_account_id: bp_rialto::AccountId = rialto_public.into_account(); - let digest = rialto_runtime::millau_account_ownership_digest( + let digest = rialto_runtime::rialto_to_millau_account_ownership_digest( &call, rialto_account_id, millau_runtime::VERSION.spec_version, diff --git a/bridges/relays/bin-substrate/src/chains/rialto_messages_to_millau.rs b/bridges/relays/bin-substrate/src/chains/rialto_messages_to_millau.rs index ec39a4caa3fc..3637e771e5b1 100644 --- a/bridges/relays/bin-substrate/src/chains/rialto_messages_to_millau.rs +++ b/bridges/relays/bin-substrate/src/chains/rialto_messages_to_millau.rs @@ -23,7 +23,7 @@ use crate::messages_source::SubstrateMessagesSource; use crate::messages_target::SubstrateMessagesTarget; use bp_messages::MessageNonce; -use bp_runtime::{MILLAU_BRIDGE_INSTANCE, RIALTO_BRIDGE_INSTANCE}; +use bp_runtime::{MILLAU_CHAIN_ID, RIALTO_CHAIN_ID}; use bridge_runtime_common::messages::target::FromBridgedChainMessagesProof; use codec::Encode; use frame_support::dispatch::GetDispatchInfo; @@ -199,14 +199,14 @@ pub async fn run( source_client.clone(), lane.clone(), lane_id, - MILLAU_BRIDGE_INSTANCE, + MILLAU_CHAIN_ID, params.target_to_source_headers_relay, ), MillauTargetClient::new( params.target_client, lane, lane_id, - RIALTO_BRIDGE_INSTANCE, + RIALTO_CHAIN_ID, params.source_to_target_headers_relay, ), relay_utils::relay_metrics( diff --git a/bridges/relays/bin-substrate/src/cli/bridge.rs b/bridges/relays/bin-substrate/src/cli/bridge.rs index 996edb49942a..b54d3a72ad3b 100644 --- a/bridges/relays/bin-substrate/src/cli/bridge.rs +++ b/bridges/relays/bin-substrate/src/cli/bridge.rs @@ -64,7 +64,7 @@ macro_rules! select_full_bridge { use bp_rialto::TO_RIALTO_ESTIMATE_MESSAGE_FEE_METHOD as ESTIMATE_MESSAGE_FEE_METHOD; // Send-message #[allow(unused_imports)] - use millau_runtime::rialto_account_ownership_digest as account_ownership_digest; + use millau_runtime::millau_to_rialto_account_ownership_digest as account_ownership_digest; $generic } @@ -87,7 +87,7 @@ macro_rules! select_full_bridge { // Send-message #[allow(unused_imports)] - use rialto_runtime::millau_account_ownership_digest as account_ownership_digest; + use rialto_runtime::rialto_to_millau_account_ownership_digest as account_ownership_digest; $generic } diff --git a/bridges/relays/bin-substrate/src/messages_source.rs b/bridges/relays/bin-substrate/src/messages_source.rs index 0ccf8bbde885..36f8b6d85cde 100644 --- a/bridges/relays/bin-substrate/src/messages_source.rs +++ b/bridges/relays/bin-substrate/src/messages_source.rs @@ -23,7 +23,7 @@ use crate::on_demand_headers::OnDemandHeadersRelay; use async_trait::async_trait; use bp_messages::{LaneId, MessageNonce}; -use bp_runtime::InstanceId; +use bp_runtime::ChainId; use bridge_runtime_common::messages::target::FromBridgedChainMessagesProof; use codec::{Decode, Encode}; use frame_support::{traits::Instance, weights::Weight}; @@ -50,7 +50,7 @@ pub struct SubstrateMessagesSource { client: Client, lane: P, lane_id: LaneId, - instance: InstanceId, + instance: ChainId, target_to_source_headers_relay: Option>, _phantom: PhantomData<(R, I)>, } @@ -61,7 +61,7 @@ impl SubstrateMessagesSource, lane: P, lane_id: LaneId, - instance: InstanceId, + instance: ChainId, target_to_source_headers_relay: Option>, ) -> Self { SubstrateMessagesSource { diff --git a/bridges/relays/bin-substrate/src/messages_target.rs b/bridges/relays/bin-substrate/src/messages_target.rs index 39f638d7e91c..004bb47db518 100644 --- a/bridges/relays/bin-substrate/src/messages_target.rs +++ b/bridges/relays/bin-substrate/src/messages_target.rs @@ -24,7 +24,7 @@ use crate::on_demand_headers::OnDemandHeadersRelay; use async_trait::async_trait; use bp_messages::{LaneId, MessageNonce, UnrewardedRelayersState}; -use bp_runtime::InstanceId; +use bp_runtime::ChainId; use bridge_runtime_common::messages::source::FromBridgedChainMessagesDeliveryProof; use codec::{Decode, Encode}; use frame_support::traits::Instance; @@ -50,7 +50,7 @@ pub struct SubstrateMessagesTarget { client: Client, lane: P, lane_id: LaneId, - instance: InstanceId, + instance: ChainId, source_to_target_headers_relay: Option>, _phantom: PhantomData<(R, I)>, } @@ -61,7 +61,7 @@ impl SubstrateMessagesTarget, lane: P, lane_id: LaneId, - instance: InstanceId, + instance: ChainId, source_to_target_headers_relay: Option>, ) -> Self { SubstrateMessagesTarget {