Skip to content

Commit

Permalink
Derive AgentId for BridgeHub in a consistent manner (#1022)
Browse files Browse the repository at this point in the history
* Add DescribeHere

* Update sdk

* Fix breaking test

* Update BRIDGE_HUB_AGENT_ID with the new value

* Update Cargo.lock

* Change tuple order
  • Loading branch information
yrong authored Nov 28, 2023
1 parent a2034a1 commit 2033389
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 16 deletions.
1 change: 1 addition & 0 deletions parachain/Cargo.lock

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

10 changes: 6 additions & 4 deletions parachain/pallets/control/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ use frame_system::pallet_prelude::*;
use snowbridge_core::{
outbound::{Command, Initializer, Message, OperatingMode, SendError, SendMessage},
sibling_sovereign_account, AgentId, Channel, ChannelId, ChannelLookup, ParaId,
BRIDGE_HUB_AGENT_ID, PRIMARY_GOVERNANCE_CHANNEL, SECONDARY_GOVERNANCE_CHANNEL,
PRIMARY_GOVERNANCE_CHANNEL, SECONDARY_GOVERNANCE_CHANNEL,
};

#[cfg(feature = "runtime-benchmarks")]
Expand Down Expand Up @@ -210,19 +210,21 @@ pub mod pallet {
#[pallet::genesis_build]
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
fn build(&self) {
let bridge_hub_agent_id =
agent_id_of::<T>(&MultiLocation::here()).expect("infallible; qed");
// Agent for BridgeHub
Agents::<T>::insert(BRIDGE_HUB_AGENT_ID, ());
Agents::<T>::insert(bridge_hub_agent_id, ());

// Primary governance channel
Channels::<T>::insert(
PRIMARY_GOVERNANCE_CHANNEL,
Channel { agent_id: BRIDGE_HUB_AGENT_ID, para_id: self.para_id },
Channel { agent_id: bridge_hub_agent_id, para_id: self.para_id },
);

// Secondary governance channel
Channels::<T>::insert(
SECONDARY_GOVERNANCE_CHANNEL,
Channel { agent_id: BRIDGE_HUB_AGENT_ID, para_id: self.para_id },
Channel { agent_id: bridge_hub_agent_id, para_id: self.para_id },
);

// Asset Hub
Expand Down
8 changes: 5 additions & 3 deletions parachain/pallets/control/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use sp_core::H256;
use xcm_executor::traits::ConvertLocation;

use snowbridge_core::{
outbound::ConstantGasMeter, sibling_sovereign_account, AgentId, AllowSiblingsOnly, ParaId,
outbound::ConstantGasMeter, sibling_sovereign_account, AgentId, AllowSiblingsOnly,
DescribeHere, ParaId,
};
use sp_runtime::{
traits::{AccountIdConversion, BlakeTwo256, IdentityLookup, Keccak256},
Expand Down Expand Up @@ -208,7 +209,8 @@ impl crate::Config for Test {
type RuntimeEvent = RuntimeEvent;
type OutboundQueue = OutboundQueue;
type SiblingOrigin = pallet_xcm_origin::EnsureXcm<AllowSiblingsOnly>;
type AgentIdOf = HashedDescription<AgentId, DescribeFamily<DescribeAllTerminal>>;
type AgentIdOf =
HashedDescription<AgentId, (DescribeHere, DescribeFamily<DescribeAllTerminal>)>;
type TreasuryAccount = TreasuryAccount;
type Token = Balances;
type WeightInfo = ();
Expand Down Expand Up @@ -249,6 +251,6 @@ pub fn make_xcm_origin(location: MultiLocation) -> RuntimeOrigin {
}

pub fn make_agent_id(location: MultiLocation) -> AgentId {
HashedDescription::<AgentId, DescribeFamily<DescribeAllTerminal>>::convert_location(&location)
<Test as snowbridge_control::Config>::AgentIdOf::convert_location(&location)
.expect("convert location")
}
13 changes: 13 additions & 0 deletions parachain/pallets/control/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
use crate::{mock::*, *};
use frame_support::{assert_noop, assert_ok};
use hex_literal::hex;
use snowbridge_core::sibling_sovereign_account_raw;
use sp_core::H256;
use sp_runtime::{AccountId32, DispatchError::BadOrigin, TokenError};
Expand All @@ -26,6 +27,18 @@ fn create_agent() {
});
}

#[test]
fn test_agent_for_here() {
new_test_ext().execute_with(|| {
let origin_location = MultiLocation::here();
let agent_id = make_agent_id(origin_location);
assert_eq!(
agent_id,
hex!("03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314").into(),
)
});
}

#[test]
fn create_agent_fails_on_funds_unavailable() {
new_test_ext().execute_with(|| {
Expand Down
3 changes: 2 additions & 1 deletion parachain/pallets/ethereum-beacon-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"hex-literal"
"hex-literal",
"snowbridge-core/runtime-benchmarks",
]
beacon-spec-mainnet = []
6 changes: 5 additions & 1 deletion parachain/primitives/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ hex-literal = { version = "0.4.1" }

polkadot-parachain-primitives = { path = "../../../polkadot-sdk/polkadot/parachain", default-features = false }
xcm = { package = "staging-xcm", path = "../../../polkadot-sdk/polkadot/xcm", default-features = false }
xcm-builder = { package = "staging-xcm-builder", path = "../../../polkadot-sdk/polkadot/xcm/xcm-builder", default-features = false }

frame-support = { path = "../../../polkadot-sdk/substrate/frame/support", default-features = false }
frame-system = { path = "../../../polkadot-sdk/substrate/frame/system", default-features = false }
Expand Down Expand Up @@ -44,6 +45,9 @@ std = [
"snowbridge-beacon-primitives/std",
"xcm/std",
"ethabi/std",
"xcm-builder/std",
]
serde = ["dep:serde", "scale-info/serde"]
runtime-benchmarks = []
runtime-benchmarks = [
"xcm-builder/runtime-benchmarks",
]
19 changes: 15 additions & 4 deletions parachain/primitives/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ use sp_core::H256;
use sp_io::hashing::keccak_256;
use sp_runtime::{traits::AccountIdConversion, RuntimeDebug};
use sp_std::prelude::*;
use xcm::prelude::{Junction::Parachain, Junctions::X1, MultiLocation};
use xcm::prelude::{
Junction::Parachain,
Junctions::{Here, X1},
MultiLocation,
};
use xcm_builder::DescribeLocation;

/// The ID of an agent contract
pub type AgentId = H256;
Expand Down Expand Up @@ -135,6 +140,12 @@ pub const PRIMARY_GOVERNANCE_CHANNEL: ChannelId =
pub const SECONDARY_GOVERNANCE_CHANNEL: ChannelId =
ChannelId::new(hex!("0000000000000000000000000000000000000000000000000000000000000002"));

/// Agent ID for BridgeHub
pub const BRIDGE_HUB_AGENT_ID: AgentId =
H256(hex!("0000000000000000000000000000000000000000000000000000000000000001"));
pub struct DescribeHere;
impl DescribeLocation for DescribeHere {
fn describe_location(l: &MultiLocation) -> Option<Vec<u8>> {
match (l.parents, l.interior) {
(0, Here) => Some(Vec::<u8>::new().encode()),
_ => None,
}
}
}
3 changes: 2 additions & 1 deletion smoketest/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use hex_literal::hex;

// Todo: load all configs from env in consistent with set-env.sh
pub const ASSET_HUB_PARA_ID: u32 = 1000;
pub const BRIDGE_HUB_PARA_ID: u32 = 1013;
pub const PENPAL_PARA_ID: u32 = 2000;
Expand All @@ -24,7 +25,7 @@ pub const WETH_CONTRACT: [u8; 20] = hex!("87d1f7fdfEe7f651FaBc8bFCB6E086C278b77A

// Agent for bridge hub parachain 1013
pub const BRIDGE_HUB_AGENT_ID: [u8; 32] =
hex!("05f0ced792884ed09997292bd95f8d0d1094bb3bded91ec3f2f08531624037d6");
hex!("03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314");
// Agent for asset hub parachain 1000
pub const ASSET_HUB_AGENT_ID: [u8; 32] =
hex!("72456f48efed08af20e5b317abf8648ac66e86bb90a411d9b0b713f7364b75b4");
Expand Down
2 changes: 1 addition & 1 deletion web/packages/test/scripts/set-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ bridgehub_ws_url="${BRIDGE_HUB_WS_URL:-ws://127.0.0.1:11144}"
bridgehub_seed="${BRIDGE_HUB_SEED:-//Alice}"
bridgehub_pallets_owner="${BRIDGE_HUB_PALLETS_OWNER:-0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d}"
export BRIDGE_HUB_PARAID="${BRIDGE_HUB_PARAID:-1013}"
export BRIDGE_HUB_AGENT_ID="${BRIDGE_HUB_AGENT_ID:-0x0000000000000000000000000000000000000000000000000000000000000001}"
export BRIDGE_HUB_AGENT_ID="${BRIDGE_HUB_AGENT_ID:-0x03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314}"

assethub_ws_url="${ASSET_HUB_WS_URL:-ws://127.0.0.1:12144}"
assethub_seed="${ASSET_HUB_SEED:-//Alice}"
Expand Down

0 comments on commit 2033389

Please sign in to comment.