From 03ca806dc55cf119f0d1e3e344e540fe5028d41a Mon Sep 17 00:00:00 2001 From: Aki Wu Date: Wed, 14 Dec 2022 11:37:22 +0800 Subject: [PATCH 1/7] new derive_account_id --- modules/dispatch/src/lib.rs | 6 ++-- modules/fee-market/src/mock.rs | 6 ++-- modules/messages/src/lib.rs | 6 ++-- modules/messages/src/mock.rs | 4 +-- primitives/darwinia-core/src/lib.rs | 19 ++++++------- primitives/runtime/src/lib.rs | 43 +++++++++++++++++++++++------ 6 files changed, 54 insertions(+), 30 deletions(-) diff --git a/modules/dispatch/src/lib.rs b/modules/dispatch/src/lib.rs index aed33e801..dbc5e4443 100644 --- a/modules/dispatch/src/lib.rs +++ b/modules/dispatch/src/lib.rs @@ -105,7 +105,7 @@ pub mod pallet { /// A type which can be turned into an AccountId from a 256-bit hash. /// /// Used when deriving target chain AccountIds from source chain AccountIds. - type AccountIdConverter: sp_runtime::traits::Convert; + type AccountIdConverter: sp_runtime::traits::Convert; /// The type is used to customize the dispatch call origin. type IntoDispatchOrigin: IntoDispatchOrigin< Self::AccountId, @@ -530,8 +530,8 @@ mod tests { } pub struct AccountIdConverter; - impl sp_runtime::traits::Convert for AccountIdConverter { - fn convert(hash: H256) -> AccountId { + impl sp_runtime::traits::Convert for AccountIdConverter { + fn convert(hash: H160) -> AccountId { hash.to_low_u64_ne() } } diff --git a/modules/fee-market/src/mock.rs b/modules/fee-market/src/mock.rs index bb7306c5e..c49c33627 100644 --- a/modules/fee-market/src/mock.rs +++ b/modules/fee-market/src/mock.rs @@ -44,7 +44,7 @@ use frame_support::{ }; use frame_system::mocking::*; use pallet_bridge_messages::outbound_lane; -use sp_core::H256; +use sp_core::{H160, H256}; use sp_runtime::{ testing::Header, traits::{AccountIdConversion, BlakeTwo256, IdentityLookup, UniqueSaturatedInto}, @@ -399,8 +399,8 @@ impl MessageDispatch for TestMessageDispatch { } pub struct AccountIdConverter; -impl sp_runtime::traits::Convert for AccountIdConverter { - fn convert(hash: H256) -> AccountId { +impl sp_runtime::traits::Convert for AccountIdConverter { + fn convert(hash: H160) -> AccountId { hash.to_low_u64_ne() } } diff --git a/modules/messages/src/lib.rs b/modules/messages/src/lib.rs index 8061b3eec..76eda2127 100644 --- a/modules/messages/src/lib.rs +++ b/modules/messages/src/lib.rs @@ -81,7 +81,7 @@ use bp_messages::{ use bp_runtime::{BasicOperatingMode, ChainId, OwnedBridgeModule, Size}; // paritytech use frame_support::{dispatch::PostDispatchInfo, ensure, fail, log, traits::Get}; -use sp_core::H256; +use sp_core::H160; use sp_runtime::traits::Convert; use sp_std::{cell::RefCell, marker::PhantomData, prelude::*}; @@ -167,7 +167,7 @@ pub mod pallet { /// A type which can be turned into an AccountId from a 256-bit hash. /// /// Used when deriving the shared relayer fund account. - type AccountIdConverter: sp_runtime::traits::Convert; + type AccountIdConverter: sp_runtime::traits::Convert; // Types that are used by outbound_lane (on source chain). @@ -692,7 +692,7 @@ pub use pallet::*; /// This account is passed to `MessageDeliveryAndDispatchPayment` trait, and depending /// on the implementation it can be used to store relayers rewards. /// See [`InstantCurrencyPayments`] for a concrete implementation. -pub fn relayer_fund_account_id>( +pub fn relayer_fund_account_id>( ) -> AccountId { let encoded_id = bp_runtime::derive_relayer_fund_account_id(bp_runtime::NO_INSTANCE_ID); AccountIdConverter::convert(encoded_id) diff --git a/modules/messages/src/mock.rs b/modules/messages/src/mock.rs index 5bdaecece..e21eb8ea4 100644 --- a/modules/messages/src/mock.rs +++ b/modules/messages/src/mock.rs @@ -109,8 +109,8 @@ pub struct TestPayload { } pub struct AccountIdConverter; -impl sp_runtime::traits::Convert for AccountIdConverter { - fn convert(hash: H256) -> AccountId { +impl sp_runtime::traits::Convert for AccountIdConverter { + fn convert(hash: H160) -> AccountId { hash.to_low_u64_ne() } } diff --git a/primitives/darwinia-core/src/lib.rs b/primitives/darwinia-core/src/lib.rs index 7f27998de..36ae3663b 100644 --- a/primitives/darwinia-core/src/lib.rs +++ b/primitives/darwinia-core/src/lib.rs @@ -107,11 +107,10 @@ use frame_support::{ unsigned::{TransactionValidityError, UnknownTransaction}, weights::Weight, }; -use sp_core::{H160, H256}; use sp_runtime::{ generic, generic::Era, - traits::{Convert, DispatchInfoOf, Dispatchable, SignedExtension as SignedExtensionT}, + traits::{DispatchInfoOf, Dispatchable, SignedExtension as SignedExtensionT}, RuntimeDebug, }; use sp_std::prelude::*; @@ -261,11 +260,11 @@ impl Chain for DarwiniaLike { } } -/// Convert a 256-bit hash into an AccountId. -pub struct AccountIdConverter; -impl Convert for AccountIdConverter { - fn convert(hash: H256) -> AccountId { - let evm_address: H160 = hash.into(); - evm_address.into() - } -} +// Convert a 256-bit hash into an AccountId. +// pub struct AccountIdConverter; +// impl Convert for AccountIdConverter { +// fn convert(hash: H256) -> AccountId { +// let evm_address: H160 = hash.into(); +// evm_address.into() +// } +// } diff --git a/primitives/runtime/src/lib.rs b/primitives/runtime/src/lib.rs index f49ddd373..cce15a735 100644 --- a/primitives/runtime/src/lib.rs +++ b/primitives/runtime/src/lib.rs @@ -48,7 +48,7 @@ use frame_support::{ log, pallet_prelude::DispatchResult, PalletError, RuntimeDebug, StorageHasher, StorageValue, }; use frame_system::RawOrigin; -use sp_core::{storage::StorageKey, H256}; +use sp_core::{storage::StorageKey, H256, H160}; use sp_io::hashing::blake2_256; use sp_runtime::{ traits::{BadOrigin, Header as HeaderT}, @@ -395,17 +395,41 @@ impl Size for PreComputedSize { /// 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: ChainId, id: SourceAccount) -> H256 +pub fn derive_account_id(bridge_id: ChainId, id: SourceAccount) -> H160 where AccountId: Encode, { match id { - SourceAccount::Root => - (ROOT_ACCOUNT_DERIVATION_PREFIX, bridge_id).using_encoded(blake2_256), - SourceAccount::Account(id) => - (ACCOUNT_DERIVATION_PREFIX, bridge_id, id).using_encoded(blake2_256), + SourceAccount::Root => { + let h256 = (ROOT_ACCOUNT_DERIVATION_PREFIX, bridge_id).using_encoded(blake2_256); + H160::from_slice(&h256[0..20]) + }, + SourceAccount::Account(id) => { + // convert id to h160 + let source_h160 = H160::from_slice(&id.encode()[0..20]); + + // derive target h160 + to_target_h160(bridge_id, &source_h160) + } } - .into() +} + +fn to_h256_starting_with_dvm(address: &H160) -> H256 { + let mut raw_account = [0u8; 32]; + raw_account[0..4].copy_from_slice(b"dvm:"); + raw_account[11..31].copy_from_slice(&address[..]); + raw_account[31] = checksum_of(&raw_account); + raw_account.into() +} + +fn checksum_of(account_id: &[u8; 32]) -> u8 { + account_id[1..31].iter().fold(account_id[0], |sum, &byte| sum ^ byte) +} + +fn to_target_h160(source_chain_id: ChainId, source_h160: &H160) -> H160 { + let h256_dvm = to_h256_starting_with_dvm(source_h160); + let h256_derived = (ACCOUNT_DERIVATION_PREFIX, source_chain_id, h256_dvm).using_encoded(blake2_256); + return H160::from_slice(&h256_derived[0..20]); } /// Derive the account ID of the shared relayer fund account. @@ -414,8 +438,9 @@ 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: ChainId) -> H256 { - ("relayer-fund-account", bridge_id).using_encoded(blake2_256).into() +pub fn derive_relayer_fund_account_id(bridge_id: ChainId) -> H160 { + let h256 = ("relayer-fund-account", bridge_id).using_encoded(blake2_256); + H160::from_slice(&h256[0..20]) } /// This is a copy of the From 66b4c39d3710c8cffad6c22389fef99b93bea69d Mon Sep 17 00:00:00 2001 From: Aki Wu Date: Wed, 14 Dec 2022 11:47:48 +0800 Subject: [PATCH 2/7] adjust --- primitives/runtime/src/lib.rs | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/primitives/runtime/src/lib.rs b/primitives/runtime/src/lib.rs index cce15a735..b0503790f 100644 --- a/primitives/runtime/src/lib.rs +++ b/primitives/runtime/src/lib.rs @@ -414,24 +414,20 @@ where } } -fn to_h256_starting_with_dvm(address: &H160) -> H256 { - let mut raw_account = [0u8; 32]; - raw_account[0..4].copy_from_slice(b"dvm:"); - raw_account[11..31].copy_from_slice(&address[..]); - raw_account[31] = checksum_of(&raw_account); - raw_account.into() -} - -fn checksum_of(account_id: &[u8; 32]) -> u8 { - account_id[1..31].iter().fold(account_id[0], |sum, &byte| sum ^ byte) -} - fn to_target_h160(source_chain_id: ChainId, source_h160: &H160) -> H160 { let h256_dvm = to_h256_starting_with_dvm(source_h160); let h256_derived = (ACCOUNT_DERIVATION_PREFIX, source_chain_id, h256_dvm).using_encoded(blake2_256); return H160::from_slice(&h256_derived[0..20]); } +fn to_h256_starting_with_dvm(address: &H160) -> H256 { + let mut result = [0u8; 32]; + result[0..4].copy_from_slice(b"dvm:"); + result[11..31].copy_from_slice(&address[..]); + result[31] = result[1..31].iter().fold(result[0], |sum, &byte| sum ^ byte); + result.into() +} + /// Derive the account ID of the shared relayer fund account. /// /// This account is used to collect fees for relayers that are passing messages across the bridge. From 4655b8540d599294b6711089b4e6440ce4964d6c Mon Sep 17 00:00:00 2001 From: Aki Wu Date: Wed, 14 Dec 2022 13:57:16 +0800 Subject: [PATCH 3/7] add test --- primitives/runtime/src/lib.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/primitives/runtime/src/lib.rs b/primitives/runtime/src/lib.rs index b0503790f..e71e6a4cf 100644 --- a/primitives/runtime/src/lib.rs +++ b/primitives/runtime/src/lib.rs @@ -493,6 +493,8 @@ pub fn storage_value_key(pallet_prefix: &str, value_name: &str) -> StorageKey { #[cfg(test)] mod tests { + use core::str::FromStr; + use super::*; #[test] @@ -512,4 +514,23 @@ mod tests { )), ); } + + #[test] + fn deriving_from_source_h160_to_target_h160_works() { + let source_chain_id: [u8; 4] = [0, 0, 0, 0]; + let source_sender = H160::from_str("0x61dC46385a09E7ed7688aBE6f66Bf3d8653618fD").unwrap(); + let target_sender = to_target_h160(source_chain_id, &source_sender); + + let expect = H160::from_str("0x95804eb66d1944a85d73FbA99465cB33C715D516").unwrap(); + assert_eq!(target_sender, expect); + } + + #[test] + fn compare_two_ways_of_converting_from_h256_to_h160() { + let h256: H256 = H256::from_str("0x64766d3a0000000000000061dc46385a09e7ed7688abe6f66bf3d8653618fd6c").unwrap(); + + let h160_1: H160 = h256.into(); // this is the wrong way + let h160_2: H160 = H160::from_slice(&h256[0..20]); + assert_ne!(h160_1, h160_2); + } } From a0d7759fdfefc85b052d5b0d8dfb99e3450145d9 Mon Sep 17 00:00:00 2001 From: bear Date: Wed, 14 Dec 2022 15:17:20 +0800 Subject: [PATCH 4/7] Keep compatible of the derive way --- modules/dispatch/src/lib.rs | 2 +- modules/messages/src/lib.rs | 6 ++--- primitives/darwinia-core/src/lib.rs | 19 ++++++++------- primitives/runtime/src/lib.rs | 37 ++++++++++++++--------------- 4 files changed, 32 insertions(+), 32 deletions(-) diff --git a/modules/dispatch/src/lib.rs b/modules/dispatch/src/lib.rs index dbc5e4443..fa402ec71 100644 --- a/modules/dispatch/src/lib.rs +++ b/modules/dispatch/src/lib.rs @@ -105,7 +105,7 @@ pub mod pallet { /// A type which can be turned into an AccountId from a 256-bit hash. /// /// Used when deriving target chain AccountIds from source chain AccountIds. - type AccountIdConverter: sp_runtime::traits::Convert; + type AccountIdConverter: sp_runtime::traits::Convert; /// The type is used to customize the dispatch call origin. type IntoDispatchOrigin: IntoDispatchOrigin< Self::AccountId, diff --git a/modules/messages/src/lib.rs b/modules/messages/src/lib.rs index 76eda2127..3546ced20 100644 --- a/modules/messages/src/lib.rs +++ b/modules/messages/src/lib.rs @@ -81,7 +81,7 @@ use bp_messages::{ use bp_runtime::{BasicOperatingMode, ChainId, OwnedBridgeModule, Size}; // paritytech use frame_support::{dispatch::PostDispatchInfo, ensure, fail, log, traits::Get}; -use sp_core::H160; +use sp_core::{H160, H256}; use sp_runtime::traits::Convert; use sp_std::{cell::RefCell, marker::PhantomData, prelude::*}; @@ -167,7 +167,7 @@ pub mod pallet { /// A type which can be turned into an AccountId from a 256-bit hash. /// /// Used when deriving the shared relayer fund account. - type AccountIdConverter: sp_runtime::traits::Convert; + type AccountIdConverter: sp_runtime::traits::Convert; // Types that are used by outbound_lane (on source chain). @@ -692,7 +692,7 @@ pub use pallet::*; /// This account is passed to `MessageDeliveryAndDispatchPayment` trait, and depending /// on the implementation it can be used to store relayers rewards. /// See [`InstantCurrencyPayments`] for a concrete implementation. -pub fn relayer_fund_account_id>( +pub fn relayer_fund_account_id>( ) -> AccountId { let encoded_id = bp_runtime::derive_relayer_fund_account_id(bp_runtime::NO_INSTANCE_ID); AccountIdConverter::convert(encoded_id) diff --git a/primitives/darwinia-core/src/lib.rs b/primitives/darwinia-core/src/lib.rs index 36ae3663b..7f27998de 100644 --- a/primitives/darwinia-core/src/lib.rs +++ b/primitives/darwinia-core/src/lib.rs @@ -107,10 +107,11 @@ use frame_support::{ unsigned::{TransactionValidityError, UnknownTransaction}, weights::Weight, }; +use sp_core::{H160, H256}; use sp_runtime::{ generic, generic::Era, - traits::{DispatchInfoOf, Dispatchable, SignedExtension as SignedExtensionT}, + traits::{Convert, DispatchInfoOf, Dispatchable, SignedExtension as SignedExtensionT}, RuntimeDebug, }; use sp_std::prelude::*; @@ -260,11 +261,11 @@ impl Chain for DarwiniaLike { } } -// Convert a 256-bit hash into an AccountId. -// pub struct AccountIdConverter; -// impl Convert for AccountIdConverter { -// fn convert(hash: H256) -> AccountId { -// let evm_address: H160 = hash.into(); -// evm_address.into() -// } -// } +/// Convert a 256-bit hash into an AccountId. +pub struct AccountIdConverter; +impl Convert for AccountIdConverter { + fn convert(hash: H256) -> AccountId { + let evm_address: H160 = hash.into(); + evm_address.into() + } +} diff --git a/primitives/runtime/src/lib.rs b/primitives/runtime/src/lib.rs index e71e6a4cf..cea247018 100644 --- a/primitives/runtime/src/lib.rs +++ b/primitives/runtime/src/lib.rs @@ -48,7 +48,7 @@ use frame_support::{ log, pallet_prelude::DispatchResult, PalletError, RuntimeDebug, StorageHasher, StorageValue, }; use frame_system::RawOrigin; -use sp_core::{storage::StorageKey, H256, H160}; +use sp_core::{storage::StorageKey, H160, H256}; use sp_io::hashing::blake2_256; use sp_runtime::{ traits::{BadOrigin, Header as HeaderT}, @@ -395,28 +395,26 @@ impl Size for PreComputedSize { /// 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: ChainId, id: SourceAccount) -> H160 +pub fn derive_account_id(bridge_id: ChainId, id: SourceAccount) -> H256 where AccountId: Encode, { match id { - SourceAccount::Root => { - let h256 = (ROOT_ACCOUNT_DERIVATION_PREFIX, bridge_id).using_encoded(blake2_256); - H160::from_slice(&h256[0..20]) - }, + SourceAccount::Root => + (ROOT_ACCOUNT_DERIVATION_PREFIX, bridge_id).using_encoded(blake2_256), SourceAccount::Account(id) => { - // convert id to h160 let source_h160 = H160::from_slice(&id.encode()[0..20]); - - // derive target h160 - to_target_h160(bridge_id, &source_h160) - } + let h256_dvm = to_h256_starting_with_dvm(&source_h160); + (ACCOUNT_DERIVATION_PREFIX, bridge_id, h256_dvm).using_encoded(blake2_256) + }, } + .into() } fn to_target_h160(source_chain_id: ChainId, source_h160: &H160) -> H160 { let h256_dvm = to_h256_starting_with_dvm(source_h160); - let h256_derived = (ACCOUNT_DERIVATION_PREFIX, source_chain_id, h256_dvm).using_encoded(blake2_256); + let h256_derived = + (ACCOUNT_DERIVATION_PREFIX, source_chain_id, h256_dvm).using_encoded(blake2_256); return H160::from_slice(&h256_derived[0..20]); } @@ -434,9 +432,8 @@ fn to_h256_starting_with_dvm(address: &H160) -> H256 { /// /// The account ID can be the same across different instances of `pallet-bridge-messages` if the /// same `bridge_id` is used. -pub fn derive_relayer_fund_account_id(bridge_id: ChainId) -> H160 { - let h256 = ("relayer-fund-account", bridge_id).using_encoded(blake2_256); - H160::from_slice(&h256[0..20]) +pub fn derive_relayer_fund_account_id(bridge_id: ChainId) -> H256 { + ("relayer-fund-account", bridge_id).using_encoded(blake2_256).into() } /// This is a copy of the @@ -518,16 +515,18 @@ mod tests { #[test] fn deriving_from_source_h160_to_target_h160_works() { let source_chain_id: [u8; 4] = [0, 0, 0, 0]; - let source_sender = H160::from_str("0x61dC46385a09E7ed7688aBE6f66Bf3d8653618fD").unwrap(); - let target_sender = to_target_h160(source_chain_id, &source_sender); + let source_sender = H160::from_str("0x61dC46385a09E7ed7688aBE6f66Bf3d8653618fD").unwrap(); + let target_sender = to_target_h160(source_chain_id, &source_sender); let expect = H160::from_str("0x95804eb66d1944a85d73FbA99465cB33C715D516").unwrap(); - assert_eq!(target_sender, expect); + assert_eq!(target_sender, expect); } #[test] fn compare_two_ways_of_converting_from_h256_to_h160() { - let h256: H256 = H256::from_str("0x64766d3a0000000000000061dc46385a09e7ed7688abe6f66bf3d8653618fd6c").unwrap(); + let h256: H256 = + H256::from_str("0x64766d3a0000000000000061dc46385a09e7ed7688abe6f66bf3d8653618fd6c") + .unwrap(); let h160_1: H160 = h256.into(); // this is the wrong way let h160_2: H160 = H160::from_slice(&h256[0..20]); From 1192cd425a5ef6cb40df0684f7c12a4addd2d3ad Mon Sep 17 00:00:00 2001 From: bear Date: Wed, 14 Dec 2022 16:09:30 +0800 Subject: [PATCH 5/7] Fix test --- modules/dispatch/src/lib.rs | 4 +- modules/fee-market/src/mock.rs | 4 +- modules/messages/src/mock.rs | 4 +- primitives/runtime/src/lib.rs | 72 ++++++++++++++++------------------ 4 files changed, 40 insertions(+), 44 deletions(-) diff --git a/modules/dispatch/src/lib.rs b/modules/dispatch/src/lib.rs index fa402ec71..aed33e801 100644 --- a/modules/dispatch/src/lib.rs +++ b/modules/dispatch/src/lib.rs @@ -530,8 +530,8 @@ mod tests { } pub struct AccountIdConverter; - impl sp_runtime::traits::Convert for AccountIdConverter { - fn convert(hash: H160) -> AccountId { + impl sp_runtime::traits::Convert for AccountIdConverter { + fn convert(hash: H256) -> AccountId { hash.to_low_u64_ne() } } diff --git a/modules/fee-market/src/mock.rs b/modules/fee-market/src/mock.rs index c49c33627..3332fbbca 100644 --- a/modules/fee-market/src/mock.rs +++ b/modules/fee-market/src/mock.rs @@ -399,8 +399,8 @@ impl MessageDispatch for TestMessageDispatch { } pub struct AccountIdConverter; -impl sp_runtime::traits::Convert for AccountIdConverter { - fn convert(hash: H160) -> AccountId { +impl sp_runtime::traits::Convert for AccountIdConverter { + fn convert(hash: H256) -> AccountId { hash.to_low_u64_ne() } } diff --git a/modules/messages/src/mock.rs b/modules/messages/src/mock.rs index e21eb8ea4..5bdaecece 100644 --- a/modules/messages/src/mock.rs +++ b/modules/messages/src/mock.rs @@ -109,8 +109,8 @@ pub struct TestPayload { } pub struct AccountIdConverter; -impl sp_runtime::traits::Convert for AccountIdConverter { - fn convert(hash: H160) -> AccountId { +impl sp_runtime::traits::Convert for AccountIdConverter { + fn convert(hash: H256) -> AccountId { hash.to_low_u64_ne() } } diff --git a/primitives/runtime/src/lib.rs b/primitives/runtime/src/lib.rs index cea247018..8d6d946a4 100644 --- a/primitives/runtime/src/lib.rs +++ b/primitives/runtime/src/lib.rs @@ -403,29 +403,25 @@ where SourceAccount::Root => (ROOT_ACCOUNT_DERIVATION_PREFIX, bridge_id).using_encoded(blake2_256), SourceAccount::Account(id) => { - let source_h160 = H160::from_slice(&id.encode()[0..20]); - let h256_dvm = to_h256_starting_with_dvm(&source_h160); - (ACCOUNT_DERIVATION_PREFIX, bridge_id, h256_dvm).using_encoded(blake2_256) + let to_darwinia_old_account_id = |address: &[u8]| -> H256 { + let mut result = [0u8; 32]; + result[0..4].copy_from_slice(b"dvm:"); + result[11..31].copy_from_slice(address); + result[31] = result[1..31].iter().fold(result[0], |sum, &byte| sum ^ byte); + result.into() + }; + + if id.encode().len() == 20 { + let account_id = to_darwinia_old_account_id(&id.encode()); + (ACCOUNT_DERIVATION_PREFIX, bridge_id, account_id).using_encoded(blake2_256) + } else { + (ACCOUNT_DERIVATION_PREFIX, bridge_id, id).using_encoded(blake2_256) + } }, } .into() } -fn to_target_h160(source_chain_id: ChainId, source_h160: &H160) -> H160 { - let h256_dvm = to_h256_starting_with_dvm(source_h160); - let h256_derived = - (ACCOUNT_DERIVATION_PREFIX, source_chain_id, h256_dvm).using_encoded(blake2_256); - return H160::from_slice(&h256_derived[0..20]); -} - -fn to_h256_starting_with_dvm(address: &H160) -> H256 { - let mut result = [0u8; 32]; - result[0..4].copy_from_slice(b"dvm:"); - result[11..31].copy_from_slice(&address[..]); - result[31] = result[1..31].iter().fold(result[0], |sum, &byte| sum ^ byte); - result.into() -} - /// Derive the account ID of the shared relayer fund account. /// /// This account is used to collect fees for relayers that are passing messages across the bridge. @@ -512,24 +508,24 @@ mod tests { ); } - #[test] - fn deriving_from_source_h160_to_target_h160_works() { - let source_chain_id: [u8; 4] = [0, 0, 0, 0]; - let source_sender = H160::from_str("0x61dC46385a09E7ed7688aBE6f66Bf3d8653618fD").unwrap(); - let target_sender = to_target_h160(source_chain_id, &source_sender); - - let expect = H160::from_str("0x95804eb66d1944a85d73FbA99465cB33C715D516").unwrap(); - assert_eq!(target_sender, expect); - } - - #[test] - fn compare_two_ways_of_converting_from_h256_to_h160() { - let h256: H256 = - H256::from_str("0x64766d3a0000000000000061dc46385a09e7ed7688abe6f66bf3d8653618fd6c") - .unwrap(); - - let h160_1: H160 = h256.into(); // this is the wrong way - let h160_2: H160 = H160::from_slice(&h256[0..20]); - assert_ne!(h160_1, h160_2); - } + // #[test] + // fn deriving_from_source_h160_to_target_h160_works() { + // let source_chain_id: [u8; 4] = [0, 0, 0, 0]; + // let source_sender = H160::from_str("0x61dC46385a09E7ed7688aBE6f66Bf3d8653618fD").unwrap(); + // let target_sender = to_target_h160(source_chain_id, &source_sender); + + // let expect = H160::from_str("0x95804eb66d1944a85d73FbA99465cB33C715D516").unwrap(); + // assert_eq!(target_sender, expect); + // } + + // #[test] + // fn compare_two_ways_of_converting_from_h256_to_h160() { + // let h256: H256 = + // H256::from_str("0x64766d3a0000000000000061dc46385a09e7ed7688abe6f66bf3d8653618fd6c") + // .unwrap(); + + // let h160_1: H160 = h256.into(); // this is the wrong way + // let h160_2: H160 = H160::from_slice(&h256[0..20]); + // assert_ne!(h160_1, h160_2); + // } } From 921ea10a1bb47e48a4b6cf1915f448787783be6d Mon Sep 17 00:00:00 2001 From: bear Date: Wed, 14 Dec 2022 16:23:44 +0800 Subject: [PATCH 6/7] Update converter --- modules/fee-market/src/mock.rs | 2 +- modules/messages/src/lib.rs | 2 +- primitives/darwinia-core/src/lib.rs | 3 ++- primitives/runtime/src/lib.rs | 25 +++---------------------- 4 files changed, 7 insertions(+), 25 deletions(-) diff --git a/modules/fee-market/src/mock.rs b/modules/fee-market/src/mock.rs index 3332fbbca..bb7306c5e 100644 --- a/modules/fee-market/src/mock.rs +++ b/modules/fee-market/src/mock.rs @@ -44,7 +44,7 @@ use frame_support::{ }; use frame_system::mocking::*; use pallet_bridge_messages::outbound_lane; -use sp_core::{H160, H256}; +use sp_core::H256; use sp_runtime::{ testing::Header, traits::{AccountIdConversion, BlakeTwo256, IdentityLookup, UniqueSaturatedInto}, diff --git a/modules/messages/src/lib.rs b/modules/messages/src/lib.rs index 3546ced20..8061b3eec 100644 --- a/modules/messages/src/lib.rs +++ b/modules/messages/src/lib.rs @@ -81,7 +81,7 @@ use bp_messages::{ use bp_runtime::{BasicOperatingMode, ChainId, OwnedBridgeModule, Size}; // paritytech use frame_support::{dispatch::PostDispatchInfo, ensure, fail, log, traits::Get}; -use sp_core::{H160, H256}; +use sp_core::H256; use sp_runtime::traits::Convert; use sp_std::{cell::RefCell, marker::PhantomData, prelude::*}; diff --git a/primitives/darwinia-core/src/lib.rs b/primitives/darwinia-core/src/lib.rs index 7f27998de..a4252b247 100644 --- a/primitives/darwinia-core/src/lib.rs +++ b/primitives/darwinia-core/src/lib.rs @@ -265,7 +265,8 @@ impl Chain for DarwiniaLike { pub struct AccountIdConverter; impl Convert for AccountIdConverter { fn convert(hash: H256) -> AccountId { - let evm_address: H160 = hash.into(); + // This way keep compatible with darwinia 1.0 substrate to evm account rule. + let evm_address = H160::from_slice(&hash.as_bytes()[0..20]); evm_address.into() } } diff --git a/primitives/runtime/src/lib.rs b/primitives/runtime/src/lib.rs index 8d6d946a4..1266cf152 100644 --- a/primitives/runtime/src/lib.rs +++ b/primitives/runtime/src/lib.rs @@ -48,7 +48,7 @@ use frame_support::{ log, pallet_prelude::DispatchResult, PalletError, RuntimeDebug, StorageHasher, StorageValue, }; use frame_system::RawOrigin; -use sp_core::{storage::StorageKey, H160, H256}; +use sp_core::{storage::StorageKey, H256}; use sp_io::hashing::blake2_256; use sp_runtime::{ traits::{BadOrigin, Header as HeaderT}, @@ -411,6 +411,8 @@ where result.into() }; + // The aim is to keep the accounts derived from the evm account compatible with the + // darwinia 1.0 account id. if id.encode().len() == 20 { let account_id = to_darwinia_old_account_id(&id.encode()); (ACCOUNT_DERIVATION_PREFIX, bridge_id, account_id).using_encoded(blake2_256) @@ -507,25 +509,4 @@ mod tests { )), ); } - - // #[test] - // fn deriving_from_source_h160_to_target_h160_works() { - // let source_chain_id: [u8; 4] = [0, 0, 0, 0]; - // let source_sender = H160::from_str("0x61dC46385a09E7ed7688aBE6f66Bf3d8653618fD").unwrap(); - // let target_sender = to_target_h160(source_chain_id, &source_sender); - - // let expect = H160::from_str("0x95804eb66d1944a85d73FbA99465cB33C715D516").unwrap(); - // assert_eq!(target_sender, expect); - // } - - // #[test] - // fn compare_two_ways_of_converting_from_h256_to_h160() { - // let h256: H256 = - // H256::from_str("0x64766d3a0000000000000061dc46385a09e7ed7688abe6f66bf3d8653618fd6c") - // .unwrap(); - - // let h160_1: H160 = h256.into(); // this is the wrong way - // let h160_2: H160 = H160::from_slice(&h256[0..20]); - // assert_ne!(h160_1, h160_2); - // } } From 4bd453d5d220320d3f4e8c4c2c0f4ac1326de61b Mon Sep 17 00:00:00 2001 From: bear Date: Wed, 14 Dec 2022 16:26:26 +0800 Subject: [PATCH 7/7] Self review --- primitives/runtime/src/lib.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/primitives/runtime/src/lib.rs b/primitives/runtime/src/lib.rs index 1266cf152..7c9220517 100644 --- a/primitives/runtime/src/lib.rs +++ b/primitives/runtime/src/lib.rs @@ -403,7 +403,7 @@ where SourceAccount::Root => (ROOT_ACCOUNT_DERIVATION_PREFIX, bridge_id).using_encoded(blake2_256), SourceAccount::Account(id) => { - let to_darwinia_old_account_id = |address: &[u8]| -> H256 { + let to_darwinia_old_account_id = |address| -> H256 { let mut result = [0u8; 32]; result[0..4].copy_from_slice(b"dvm:"); result[11..31].copy_from_slice(address); @@ -488,8 +488,6 @@ pub fn storage_value_key(pallet_prefix: &str, value_name: &str) -> StorageKey { #[cfg(test)] mod tests { - use core::str::FromStr; - use super::*; #[test]