Skip to content

Commit

Permalink
Convert the rest locations to H160 by hashing it (#138)
Browse files Browse the repository at this point in the history
* Convert the rest locations to H160 by hashing it

* format

* fix review
  • Loading branch information
jiguantong authored Dec 20, 2022
1 parent 02b2ccb commit ba6a50c
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 13 additions & 3 deletions runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ repository = "https://github.com/darwinia-network/darwinia"
version = "6.0.0"

[dependencies]
# crates.io
codec = { package = "parity-scale-codec", version = "3.2", default-features = false }

# darwinia
dc-primitives = { default-features = false, path = "../../core/primitives" }

Expand All @@ -21,13 +24,18 @@ xcm = { default-features = false, git = "https://github.com/paritytech/
xcm-executor = { default-features = false, git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.30" }

# substrate
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
pallet-collective = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
pallet-collective = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
sp-io = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }

[features]
default = ["std"]
std = [
# crates.io
"codec/std",

# darwinia
"dc-primitives/std",

Expand All @@ -39,4 +47,6 @@ std = [
"frame-support/std",
"frame-system/std",
"pallet-collective/std",
"sp-io/std",
"sp-std/std",
]
24 changes: 23 additions & 1 deletion runtime/common/src/xcm_configs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@

// core
use core::marker::PhantomData;
// crates.io
use codec::Encode;
// darwinia
use dc_primitives::GWEI;
// polkadot
use xcm::latest::{prelude::*, Weight as XCMWeight};
use xcm_executor::traits::ShouldExecute;
use xcm_executor::traits::{Convert, ShouldExecute};
// substrate
use frame_support::{log, traits::ConstU128};
use sp_io::hashing::blake2_256;
use sp_std::borrow::Borrow;

/// Base balance required for the XCM unit weight.
pub type XcmBaseWeightFee = ConstU128<GWEI>;
Expand Down Expand Up @@ -104,3 +108,21 @@ impl ShouldExecute for DenyReserveTransferToRelayChain {
Ok(())
}
}

/// Struct that converts a given MultiLocation into a 20 bytes account id by hashing
/// with blake2_256 and taking the first 20 bytes
pub struct Account20Hash<AccountId>(PhantomData<AccountId>);
impl<AccountId: From<[u8; 20]> + Into<[u8; 20]> + Clone> Convert<MultiLocation, AccountId>
for Account20Hash<AccountId>
{
fn convert_ref(location: impl Borrow<MultiLocation>) -> Result<AccountId, ()> {
let hash: [u8; 32] = ("multiloc", location.borrow()).borrow().using_encoded(blake2_256);
let mut account_id = [0u8; 20];
account_id.copy_from_slice(&hash[0..20]);
Ok(account_id.into())
}

fn reverse_ref(_: impl Borrow<AccountId>) -> Result<MultiLocation, ()> {
Err(())
}
}
2 changes: 2 additions & 0 deletions runtime/crab/src/pallets/polkadot_xcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ pub type LocationToAccountId = (
xcm_builder::SiblingParachainConvertsVia<polkadot_parachain::primitives::Sibling, AccountId>,
// Straight up local `AccountId20` origins just alias directly to `AccountId`.
xcm_builder::AccountKey20Aliases<RelayNetwork, AccountId>,
// The rest of locations are converted via hashing it.
darwinia_common_runtime::xcm_configs::Account20Hash<AccountId>,
);
/// This is the type we use to convert an (incoming) XCM origin into a local `Origin` instance,
/// ready for dispatching a transaction with Xcm's `Transact`. There is an `OriginKind` which can
Expand Down
2 changes: 2 additions & 0 deletions runtime/darwinia/src/pallets/polkadot_xcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ pub type LocationToAccountId = (
xcm_builder::SiblingParachainConvertsVia<polkadot_parachain::primitives::Sibling, AccountId>,
// Straight up local `AccountId20` origins just alias directly to `AccountId`.
xcm_builder::AccountKey20Aliases<RelayNetwork, AccountId>,
// The rest of locations are converted via hashing it.
darwinia_common_runtime::xcm_configs::Account20Hash<AccountId>,
);
/// This is the type we use to convert an (incoming) XCM origin into a local `Origin` instance,
/// ready for dispatching a transaction with Xcm's `Transact`. There is an `OriginKind` which can
Expand Down
2 changes: 2 additions & 0 deletions runtime/pangolin/src/pallets/polkadot_xcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ pub type LocationToAccountId = (
xcm_builder::SiblingParachainConvertsVia<polkadot_parachain::primitives::Sibling, AccountId>,
// Straight up local `AccountId20` origins just alias directly to `AccountId`.
xcm_builder::AccountKey20Aliases<RelayNetwork, AccountId>,
// The rest of locations are converted via hashing it.
darwinia_common_runtime::xcm_configs::Account20Hash<AccountId>,
);
/// This is the type we use to convert an (incoming) XCM origin into a local `Origin` instance,
/// ready for dispatching a transaction with Xcm's `Transact`. There is an `OriginKind` which can
Expand Down

0 comments on commit ba6a50c

Please sign in to comment.