Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use hashed description #884

Merged
merged 10 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When would we set a network id in an account key?

Copy link
Contributor

@yrong yrong Jul 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guess already set in GlobalConsensus(network) above so can just ignore here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An example of where we "could" use network id is if we are specifying an ERC20 token contract on an EVM based parachain and potentially transferring it over the bridge and we would want a way to specify its Network as Kusama so we could handle it specially in some way. But I say "could" here because it's hypothetical.

If you look at some places in the source code, we often ignore network id. Such as here.
There are also some places where it makes sense to validate it if present like here.

In this case yrong is correct in that it is set in GlobalConsensus so we can set it as None on the key itself.

),
),
)
.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 {
yrong marked this conversation as resolved.
Show resolved Hide resolved
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
Loading