Skip to content

Commit

Permalink
Use hashed description (#884)
Browse files Browse the repository at this point in the history
* switch out implementation

* rust fmt

* update contract addresses

* fix toml

* use ok_or

* updated cumulus

* rustfmt

* Update parachain/primitives/router/src/inbound/mod.rs

Co-authored-by: David Dunn <[email protected]>

* update cumulus

* updated cumulus

---------

Co-authored-by: David Dunn <[email protected]>
  • Loading branch information
alistair-singh and doubledup authored Jul 25, 2023
1 parent 6bceb86 commit d26678d
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 45 deletions.
2 changes: 1 addition & 1 deletion cumulus
Submodule cumulus updated 1 files
+1 −0 Cargo.lock
60 changes: 50 additions & 10 deletions parachain/Cargo.lock

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

3 changes: 3 additions & 0 deletions parachain/primitives/router/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ sp-std = { git = "https://github.com/paritytech/substrate.git", branch = "master

xcm = { git = "https://github.com/paritytech/polkadot.git", branch = "master", default-features = false }
xcm-executor = { git = "https://github.com/paritytech/polkadot.git", branch = "master", default-features = false }
xcm-builder = { git = "https://github.com/paritytech/polkadot.git", branch = "master", default-features = false }

snowbridge-core = { path = "../../primitives/core", default-features = false }

Expand All @@ -42,9 +43,11 @@ std = [
"sp-std/std",
"xcm/std",
"xcm-executor/std",
"xcm-builder/std",
"snowbridge-core/std",
"ethabi/std",
]
runtime-benchmarks = [
"snowbridge-core/runtime-benchmarks",
"xcm-builder/runtime-benchmarks",
]
58 changes: 29 additions & 29 deletions parachain/primitives/router/src/inbound/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use codec::{Decode, Encode};
use core::marker::PhantomData;
use frame_support::{traits::ContainsPair, weights::Weight};
use sp_core::{Get, RuntimeDebug, H160};
use sp_io::hashing::blake2_256;
use sp_runtime::MultiAddress;
use sp_std::prelude::*;
use xcm::v3::{prelude::*, Junction::AccountKey20};
use xcm_builder::{DescribeLocation, HashedDescription};
use xcm_executor::traits::ConvertLocation;

const MINIMUM_DEPOSIT: u128 = 1;
Expand Down Expand Up @@ -62,6 +62,8 @@ pub enum NativeTokensMessage {
pub enum ConvertError {
/// Message is in the wrong format
BadFormat,
// Origin conversion failed.
BadOrigin,
}

impl TryInto<Xcm<()>> for MessageV1 {
Expand Down Expand Up @@ -101,10 +103,17 @@ impl NativeTokensMessage {
create_call_index,
set_metadata_call_index,
} => {
let owner = GlobalConsensusEthereumAccountConvertsFor::<[u8; 32]>::from_params(
&chain_id,
origin.as_fixed_bytes(),
);
let owner =
GlobalConsensusEthereumAccountConvertsFor::<[u8; 32]>::convert_location(
&MultiLocation::new(
2,
(
GlobalConsensus(network),
AccountKey20 { network: None, key: *origin.as_fixed_bytes() },
),
),
)
.ok_or(ConvertError::BadOrigin)?;

let origin_location = Junction::AccountKey20 { network: None, key: origin.into() };

Expand Down Expand Up @@ -224,6 +233,21 @@ impl NativeTokensMessage {
}
}

pub struct DescribeEthereumAccountKey20Terminal;
impl DescribeLocation for DescribeEthereumAccountKey20Terminal {
fn describe_location(l: &MultiLocation) -> Option<Vec<u8>> {
match (l.parents, &l.interior) {
(_, X2(GlobalConsensus(Ethereum { chain_id }), AccountKey20 { key, .. })) => {
Some((b"ethereum", chain_id, key).encode())
},
_ => return None,
}
}
}

pub type GlobalConsensusEthereumAccountConvertsFor<AccountId> =
HashedDescription<AccountId, DescribeEthereumAccountKey20Terminal>;

pub struct FromEthereumGlobalConsensus<EthereumBridgeLocation>(PhantomData<EthereumBridgeLocation>);
impl<EthereumBridgeLocation> ContainsPair<MultiLocation, MultiLocation>
for FromEthereumGlobalConsensus<EthereumBridgeLocation>
Expand All @@ -235,30 +259,6 @@ where
}
}

pub struct GlobalConsensusEthereumAccountConvertsFor<AccountId>(PhantomData<AccountId>);
impl<AccountId> ConvertLocation<AccountId> for GlobalConsensusEthereumAccountConvertsFor<AccountId>
where
AccountId: From<[u8; 32]> + Clone,
{
fn convert_location(location: &MultiLocation) -> Option<AccountId> {
if let MultiLocation {
interior: X2(GlobalConsensus(Ethereum { chain_id }), AccountKey20 { key, .. }),
..
} = location
{
Some(Self::from_params(chain_id, key).into())
} else {
None
}
}
}

impl<AccountId> GlobalConsensusEthereumAccountConvertsFor<AccountId> {
fn from_params(chain_id: &u64, key: &[u8; 20]) -> [u8; 32] {
(b"ethereum", chain_id, key).using_encoded(blake2_256)
}
}

#[cfg(test)]
mod tests {
use super::{FromEthereumGlobalConsensus, GlobalConsensusEthereumAccountConvertsFor};
Expand Down
4 changes: 2 additions & 2 deletions smoketest/tests/create_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use ethers::{
// contracts are deployed in DeployScript.sol.
const ETHEREUM_API: &str = "http://localhost:8545";
const ETHEREUM_KEY: &str = "0x5e002a1af63fd31f1c25258f3082dc889762664cb8f218d86da85dff8b07b342";
const NATIVE_TOKENS_CONTRACT: &str = "0x8cF6147918A5CBb672703F879f385036f8793a24";
const WETH_CONTRACT: &str = "0x440eDFFA1352B13227e8eE646f3Ea37456deC701";
const NATIVE_TOKENS_CONTRACT: &str = "0xB8EA8cB425d85536b158d661da1ef0895Bb92F1D";
const WETH_CONTRACT: &str = "0x3f0839385DB9cBEa8E73AdA6fa0CFe07E321F61d";

#[tokio::test]
async fn create_tokens() {
Expand Down
6 changes: 3 additions & 3 deletions smoketest/tests/lock_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ use xcm::v3::{MultiLocation, Junction, Junctions::X1};
// contracts are deployed in DeployScript.sol.
const ETHEREUM_API: &str = "http://localhost:8545";
const ETHEREUM_KEY: &str = "0x5e002a1af63fd31f1c25258f3082dc889762664cb8f218d86da85dff8b07b342";
const TOKEN_VAULT_CONTRACT: &str = "0xB8EA8cB425d85536b158d661da1ef0895Bb92F1D";
const NATIVE_TOKENS_CONTRACT: &str = "0x8cF6147918A5CBb672703F879f385036f8793a24";
const WETH_CONTRACT: &str = "0x440eDFFA1352B13227e8eE646f3Ea37456deC701";
const TOKEN_VAULT_CONTRACT: &str = "0xB1185EDE04202fE62D38F5db72F71e38Ff3E8305";
const NATIVE_TOKENS_CONTRACT: &str = "0xB8EA8cB425d85536b158d661da1ef0895Bb92F1D";
const WETH_CONTRACT: &str = "0x3f0839385DB9cBEa8E73AdA6fa0CFe07E321F61d";

// SS58: DE14BzQ1bDXWPKeLoAqdLAm1GpyAWaWF1knF74cEZeomTBM
const FERDIE: [u8; 32] = hex!("1cbd2d43530a44705ad088af313e18f80b53ef16b36177cd4b77b846f2a5f07c");
Expand Down

0 comments on commit d26678d

Please sign in to comment.