From 9432eeb62e715d4dafd8e99067cc8d125317008e Mon Sep 17 00:00:00 2001 From: Guantong <04637@163.com> Date: Mon, 28 Nov 2022 13:07:12 +0800 Subject: [PATCH 1/7] Use H160 in darwinia-core --- Cargo.lock | 131 +++++++++++++++++++++++++--- primitives/darwinia-core/Cargo.toml | 4 + primitives/darwinia-core/src/lib.rs | 16 ++-- 3 files changed, 134 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8c81bccf3..461ae3ffb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12,6 +12,27 @@ dependencies = [ "regex", ] +[[package]] +name = "account" +version = "0.1.1" +source = "git+https://github.com/darwinia-network/moonbeam?branch=polkadot-v0.9.30#38c37cc41b9a97482292717aec58b305d5f627f4" +dependencies = [ + "blake2-rfc", + "hex", + "impl-serde", + "libsecp256k1 0.6.0", + "log", + "parity-scale-codec", + "scale-info", + "serde", + "sha3 0.9.1", + "sp-core", + "sp-io", + "sp-runtime", + "sp-runtime-interface", + "sp-std", +] + [[package]] name = "addr2line" version = "0.17.0" @@ -163,6 +184,12 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6107fe1be6682a68940da878d9e9f5e90ca5745b3dec9fd1bb393c8777d4f581" +[[package]] +name = "base64" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3441f0f7b02788e948e47f457ca01f1d7e6d92c693bc132c22b087d3141c03ff" + [[package]] name = "base64" version = "0.13.0" @@ -202,13 +229,23 @@ dependencies = [ "digest 0.10.5", ] +[[package]] +name = "blake2-rfc" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d6d530bdd2d52966a6d03b7a964add7ae1a288d25214066fd4b600f0f796400" +dependencies = [ + "arrayvec 0.4.12", + "constant_time_eq", +] + [[package]] name = "block-buffer" version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c0940dc441f31689269e10ac70eb1002a3a1d3ad1390e030043662eb7fe4688b" dependencies = [ - "block-padding", + "block-padding 0.1.5", "byte-tools", "byteorder", "generic-array 0.12.4", @@ -220,6 +257,7 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" dependencies = [ + "block-padding 0.2.1", "generic-array 0.14.6", ] @@ -241,10 +279,17 @@ dependencies = [ "byte-tools", ] +[[package]] +name = "block-padding" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d696c370c750c948ada61c69a0ee2cbbb9c50b1019ddb86d9317157a99c2cae" + [[package]] name = "bp-darwinia-core" version = "0.1.0" dependencies = [ + "account", "bp-messages", "bp-runtime", "frame-support", @@ -472,6 +517,12 @@ version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e4c78c047431fee22c1a7bb92e00ad095a02a983affe4d8a72e2a2c62c1b94f3" +[[package]] +name = "constant_time_eq" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" + [[package]] name = "core-foundation-sys" version = "0.8.3" @@ -1272,6 +1323,25 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "292a948cd991e376cf75541fe5b97a1081d713c618b4f1b9500f8844e49eb565" +[[package]] +name = "libsecp256k1" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9d220bc1feda2ac231cb78c3d26f27676b8cf82c96971f7aeef3d0cf2797c73" +dependencies = [ + "arrayref", + "base64 0.12.3", + "digest 0.9.0", + "hmac-drbg", + "libsecp256k1-core 0.2.2", + "libsecp256k1-gen-ecmult 0.2.1", + "libsecp256k1-gen-genmult 0.2.1", + "rand 0.7.3", + "serde", + "sha2 0.9.9", + "typenum", +] + [[package]] name = "libsecp256k1" version = "0.7.1" @@ -1279,18 +1349,29 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95b09eff1b35ed3b33b877ced3a691fc7a481919c7e29c53c906226fcf55e2a1" dependencies = [ "arrayref", - "base64", + "base64 0.13.0", "digest 0.9.0", "hmac-drbg", - "libsecp256k1-core", - "libsecp256k1-gen-ecmult", - "libsecp256k1-gen-genmult", + "libsecp256k1-core 0.3.0", + "libsecp256k1-gen-ecmult 0.3.0", + "libsecp256k1-gen-genmult 0.3.0", "rand 0.8.5", "serde", "sha2 0.9.9", "typenum", ] +[[package]] +name = "libsecp256k1-core" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0f6ab710cec28cef759c5f18671a27dae2a5f952cdaaee1d8e2908cb2478a80" +dependencies = [ + "crunchy", + "digest 0.9.0", + "subtle", +] + [[package]] name = "libsecp256k1-core" version = "0.3.0" @@ -1302,13 +1383,31 @@ dependencies = [ "subtle", ] +[[package]] +name = "libsecp256k1-gen-ecmult" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccab96b584d38fac86a83f07e659f0deafd0253dc096dab5a36d53efe653c5c3" +dependencies = [ + "libsecp256k1-core 0.2.2", +] + [[package]] name = "libsecp256k1-gen-ecmult" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3038c808c55c87e8a172643a7d87187fc6c4174468159cb3090659d55bcb4809" dependencies = [ - "libsecp256k1-core", + "libsecp256k1-core 0.3.0", +] + +[[package]] +name = "libsecp256k1-gen-genmult" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67abfe149395e3aa1c48a2beb32b068e2334402df8181f818d3aee2b304c4f5d" +dependencies = [ + "libsecp256k1-core 0.2.2", ] [[package]] @@ -1317,7 +1416,7 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3db8d6ba2cec9eacc40e6e8ccc98931840301f1006e95647ceb2dd5c3aa06f7c" dependencies = [ - "libsecp256k1-core", + "libsecp256k1-core 0.3.0", ] [[package]] @@ -2256,6 +2355,18 @@ dependencies = [ "digest 0.10.5", ] +[[package]] +name = "sha3" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f81199417d4e5de3f04b1e871023acea7389672c4135918f05aa9cbf2f2fa809" +dependencies = [ + "block-buffer 0.9.0", + "digest 0.9.0", + "keccak", + "opaque-debug 0.3.0", +] + [[package]] name = "sha3" version = "0.10.5" @@ -2387,7 +2498,7 @@ dependencies = [ "hash256-std-hasher", "impl-serde", "lazy_static", - "libsecp256k1", + "libsecp256k1 0.7.1", "log", "merlin", "num-traits", @@ -2425,7 +2536,7 @@ dependencies = [ "byteorder", "digest 0.10.5", "sha2 0.10.6", - "sha3", + "sha3 0.10.5", "sp-std", "twox-hash", ] @@ -2502,7 +2613,7 @@ dependencies = [ "bytes", "futures", "hash-db", - "libsecp256k1", + "libsecp256k1 0.7.1", "log", "parity-scale-codec", "parking_lot", diff --git a/primitives/darwinia-core/Cargo.toml b/primitives/darwinia-core/Cargo.toml index 3085b94ec..3e30370a6 100644 --- a/primitives/darwinia-core/Cargo.toml +++ b/primitives/darwinia-core/Cargo.toml @@ -10,6 +10,8 @@ version = "0.1.0" # crates.io parity-scale-codec = { version = "3.0.0", default-features = false, features = ["derive"] } scale-info = { version = "2.0.1", default-features = false, features = ["derive"] } +# moonbeam +account = { default-features = false, git = "https://github.com/darwinia-network/moonbeam", branch = "polkadot-v0.9.30" } # paritytech bp-messages = { default-features = false, path = "../messages" } bp-runtime = { default-features = false, path = "../runtime" } @@ -25,6 +27,8 @@ std = [ # crates.io "parity-scale-codec/std", "scale-info/std", + # moonbeam + "account/std", # paritytech "bp-messages/std", "bp-runtime/std", diff --git a/primitives/darwinia-core/src/lib.rs b/primitives/darwinia-core/src/lib.rs index 1c2fcd34e..eb35eb56c 100644 --- a/primitives/darwinia-core/src/lib.rs +++ b/primitives/darwinia-core/src/lib.rs @@ -19,6 +19,8 @@ #![cfg_attr(not(feature = "std"), no_std)] mod copy_paste_from_darwinia { + // moonbeam + use account::EthereumSignature; // paritytech use frame_support::{ dispatch::DispatchClass, @@ -32,16 +34,16 @@ mod copy_paste_from_darwinia { use sp_runtime::{ generic, traits::{BlakeTwo256, IdentifyAccount, Verify}, - MultiAddress, MultiSignature, OpaqueExtrinsic, Perbill, + OpaqueExtrinsic, Perbill, }; pub type BlockNumber = u32; pub type Hashing = BlakeTwo256; pub type Hash = H256; - pub type Signature = MultiSignature; + pub type Signature = EthereumSignature; pub type AccountPublic = ::Signer; pub type AccountId = ::AccountId; - pub type Address = MultiAddress; + pub type Address = AccountId; pub type Nonce = u32; pub type Balance = u128; pub type Header = generic::Header; @@ -105,7 +107,7 @@ use frame_support::{ unsigned::{TransactionValidityError, UnknownTransaction}, weights::Weight, }; -use sp_core::H256; +use sp_core::H160; use sp_runtime::{ generic, generic::Era, @@ -259,10 +261,10 @@ impl Chain for DarwiniaLike { } } -/// Convert a 256-bit hash into an AccountId. +/// Convert a 160-bit hash into an AccountId. pub struct AccountIdConverter; -impl Convert for AccountIdConverter { - fn convert(hash: H256) -> AccountId { +impl Convert for AccountIdConverter { + fn convert(hash: H160) -> AccountId { hash.to_fixed_bytes().into() } } From cc215806698823be9d36134186f87c42ffa87ceb Mon Sep 17 00:00:00 2001 From: Guantong <04637@163.com> Date: Mon, 28 Nov 2022 15:21:11 +0800 Subject: [PATCH 2/7] H256 > H160 --- modules/dispatch/src/lib.rs | 2 +- modules/messages/src/lib.rs | 6 +++--- primitives/runtime/src/lib.rs | 12 ++++++------ 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/modules/dispatch/src/lib.rs b/modules/dispatch/src/lib.rs index 1621f3590..bc7efd546 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 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/primitives/runtime/src/lib.rs b/primitives/runtime/src/lib.rs index 11ed5d8c8..4f7e085e0 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::{hash::H256, storage::StorageKey}; +use sp_core::{storage::StorageKey, H160}; use sp_io::hashing::blake2_256; use sp_runtime::{ traits::{BadOrigin, Header as HeaderT}, @@ -395,15 +395,15 @@ 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), + H160::from_slice(&(ROOT_ACCOUNT_DERIVATION_PREFIX, bridge_id).using_encoded(blake2_256)), SourceAccount::Account(id) => - (ACCOUNT_DERIVATION_PREFIX, bridge_id, id).using_encoded(blake2_256), + H160::from_slice(&(ACCOUNT_DERIVATION_PREFIX, bridge_id, id).using_encoded(blake2_256)), } .into() } @@ -414,8 +414,8 @@ 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 { + H160::from_slice(&("relayer-fund-account", bridge_id).using_encoded(blake2_256)) } /// This is a copy of the From eb246d54e063b36971e1bb1442312a5a9564bb41 Mon Sep 17 00:00:00 2001 From: Guantong <04637@163.com> Date: Mon, 28 Nov 2022 16:31:44 +0800 Subject: [PATCH 3/7] update moonbeam account --- Cargo.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 461ae3ffb..d4214b41f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -15,7 +15,7 @@ dependencies = [ [[package]] name = "account" version = "0.1.1" -source = "git+https://github.com/darwinia-network/moonbeam?branch=polkadot-v0.9.30#38c37cc41b9a97482292717aec58b305d5f627f4" +source = "git+https://github.com/darwinia-network/moonbeam?branch=polkadot-v0.9.30#9d31c6120bb0f317053da9cac7fe7cd2a44f844c" dependencies = [ "blake2-rfc", "hex", From 55563472a0eaf4a5200d236d289a04e114948411 Mon Sep 17 00:00:00 2001 From: Guantong <04637@163.com> Date: Tue, 29 Nov 2022 13:37:09 +0800 Subject: [PATCH 4/7] Fix tests --- modules/dispatch/src/lib.rs | 10 +++++++--- modules/fee-market/src/mock.rs | 6 +++--- modules/messages/src/mock.rs | 4 ++-- primitives/runtime/src/lib.rs | 12 +++++++----- 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/modules/dispatch/src/lib.rs b/modules/dispatch/src/lib.rs index bc7efd546..f5636ccbe 100644 --- a/modules/dispatch/src/lib.rs +++ b/modules/dispatch/src/lib.rs @@ -489,7 +489,7 @@ mod tests { use frame_support::{parameter_types, weights::Weight}; use frame_system::{mocking::*, EventRecord, Phase}; use scale_info::TypeInfo; - use sp_core::H256; + use sp_core::{H160, H256}; use sp_runtime::{ testing::Header, traits::{BlakeTwo256, IdentityLookup}, @@ -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() } } @@ -926,6 +926,7 @@ mod tests { #[test] fn should_emit_event_for_unpaid_calls() { new_test_ext().execute_with(|| { + println!("111"); let id = [0; 4]; let relayer_account = 1; @@ -933,10 +934,12 @@ mod tests { prepare_root_message(RuntimeCall::System(frame_system::Call::remark { remark: vec![1, 2, 3], })); + let weight = message.weight; message.dispatch_fee_payment = DispatchFeePayment::AtTargetChain; System::set_block_number(1); + println!("222"); let result = Dispatch::dispatch( SOURCE_CHAIN_ID, TARGET_CHAIN_ID, @@ -945,6 +948,7 @@ mod tests { Ok(message), |_, _| Err(()), ); + println!("333"); assert_eq!(result.unspent_weight, weight); assert!(!result.dispatch_result); 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/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/runtime/src/lib.rs b/primitives/runtime/src/lib.rs index 4f7e085e0..53eff3514 100644 --- a/primitives/runtime/src/lib.rs +++ b/primitives/runtime/src/lib.rs @@ -400,10 +400,12 @@ where AccountId: Encode, { match id { - SourceAccount::Root => - H160::from_slice(&(ROOT_ACCOUNT_DERIVATION_PREFIX, bridge_id).using_encoded(blake2_256)), - SourceAccount::Account(id) => - H160::from_slice(&(ACCOUNT_DERIVATION_PREFIX, bridge_id, id).using_encoded(blake2_256)), + SourceAccount::Root => H160::from_slice( + &(ROOT_ACCOUNT_DERIVATION_PREFIX, bridge_id).using_encoded(blake2_256)[0..20], + ), + SourceAccount::Account(id) => H160::from_slice( + &(ACCOUNT_DERIVATION_PREFIX, bridge_id, id).using_encoded(blake2_256)[0..20], + ), } .into() } @@ -415,7 +417,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: ChainId) -> H160 { - H160::from_slice(&("relayer-fund-account", bridge_id).using_encoded(blake2_256)) + H160::from_slice(&("relayer-fund-account", bridge_id).using_encoded(blake2_256)[0..20]) } /// This is a copy of the From 15203afe0482fb5afc04bb4b7d6890d1b63ad2cc Mon Sep 17 00:00:00 2001 From: Guantong <04637@163.com> Date: Tue, 29 Nov 2022 17:06:38 +0800 Subject: [PATCH 5/7] remove debug println --- modules/dispatch/src/lib.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/modules/dispatch/src/lib.rs b/modules/dispatch/src/lib.rs index f5636ccbe..b46b3d5fb 100644 --- a/modules/dispatch/src/lib.rs +++ b/modules/dispatch/src/lib.rs @@ -926,7 +926,6 @@ mod tests { #[test] fn should_emit_event_for_unpaid_calls() { new_test_ext().execute_with(|| { - println!("111"); let id = [0; 4]; let relayer_account = 1; @@ -939,7 +938,6 @@ mod tests { message.dispatch_fee_payment = DispatchFeePayment::AtTargetChain; System::set_block_number(1); - println!("222"); let result = Dispatch::dispatch( SOURCE_CHAIN_ID, TARGET_CHAIN_ID, @@ -948,7 +946,6 @@ mod tests { Ok(message), |_, _| Err(()), ); - println!("333"); assert_eq!(result.unspent_weight, weight); assert!(!result.dispatch_result); From 56ccd9703b3dc15929c7a172193f97fe3cada8c2 Mon Sep 17 00:00:00 2001 From: Guantong <04637@163.com> Date: Tue, 29 Nov 2022 17:19:51 +0800 Subject: [PATCH 6/7] Fix review --- modules/dispatch/src/lib.rs | 8 ++++---- 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 | 10 +++++----- primitives/runtime/src/lib.rs | 18 ++++++++---------- 6 files changed, 25 insertions(+), 27 deletions(-) diff --git a/modules/dispatch/src/lib.rs b/modules/dispatch/src/lib.rs index b46b3d5fb..aed33e801 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, @@ -489,7 +489,7 @@ mod tests { use frame_support::{parameter_types, weights::Weight}; use frame_system::{mocking::*, EventRecord, Phase}; use scale_info::TypeInfo; - use sp_core::{H160, H256}; + use sp_core::H256; use sp_runtime::{ testing::Header, traits::{BlakeTwo256, IdentityLookup}, @@ -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..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}, @@ -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/lib.rs b/modules/messages/src/lib.rs index 76eda2127..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; +use sp_core::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/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/darwinia-core/src/lib.rs b/primitives/darwinia-core/src/lib.rs index eb35eb56c..5c1404f73 100644 --- a/primitives/darwinia-core/src/lib.rs +++ b/primitives/darwinia-core/src/lib.rs @@ -107,7 +107,7 @@ use frame_support::{ unsigned::{TransactionValidityError, UnknownTransaction}, weights::Weight, }; -use sp_core::H160; +use sp_core::{H160, H256}; use sp_runtime::{ generic, generic::Era, @@ -261,10 +261,10 @@ impl Chain for DarwiniaLike { } } -/// Convert a 160-bit hash into an AccountId. +/// Convert a 256-bit hash into an AccountId. pub struct AccountIdConverter; -impl Convert for AccountIdConverter { - fn convert(hash: H160) -> AccountId { - hash.to_fixed_bytes().into() +impl Convert for AccountIdConverter { + fn convert(hash: H256) -> AccountId { + H160::from_slice(&hash.to_fixed_bytes()[0..20]).into() } } diff --git a/primitives/runtime/src/lib.rs b/primitives/runtime/src/lib.rs index 53eff3514..f49ddd373 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}; +use sp_core::{storage::StorageKey, H256}; use sp_io::hashing::blake2_256; use sp_runtime::{ traits::{BadOrigin, Header as HeaderT}, @@ -395,17 +395,15 @@ 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 => H160::from_slice( - &(ROOT_ACCOUNT_DERIVATION_PREFIX, bridge_id).using_encoded(blake2_256)[0..20], - ), - SourceAccount::Account(id) => H160::from_slice( - &(ACCOUNT_DERIVATION_PREFIX, bridge_id, id).using_encoded(blake2_256)[0..20], - ), + 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), } .into() } @@ -416,8 +414,8 @@ 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) -> H160 { - H160::from_slice(&("relayer-fund-account", bridge_id).using_encoded(blake2_256)[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 From 2c29db3bc1e570dc959ec2cbfe0b6d82f986ce47 Mon Sep 17 00:00:00 2001 From: Guantong <04637@163.com> Date: Wed, 30 Nov 2022 10:35:06 +0800 Subject: [PATCH 7/7] Fix review --- primitives/darwinia-core/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/primitives/darwinia-core/src/lib.rs b/primitives/darwinia-core/src/lib.rs index 5c1404f73..7f27998de 100644 --- a/primitives/darwinia-core/src/lib.rs +++ b/primitives/darwinia-core/src/lib.rs @@ -265,6 +265,7 @@ impl Chain for DarwiniaLike { pub struct AccountIdConverter; impl Convert for AccountIdConverter { fn convert(hash: H256) -> AccountId { - H160::from_slice(&hash.to_fixed_bytes()[0..20]).into() + let evm_address: H160 = hash.into(); + evm_address.into() } }