From ab6d7a00e6888aacba8032cb39c524801f714391 Mon Sep 17 00:00:00 2001 From: Alistair Singh Date: Fri, 3 Nov 2023 15:10:02 +0200 Subject: [PATCH] Remove gateway contract locations from Assets (#986) * move trait for re-use * fix mocks and tests * fix clippy error * move AllowSiblingsOnly to core * update cumulus * remove gateway from inbound channel * remove uneeded function * fix failing test * fixed tests * remove redundant clone * updates inbound benchmark fixtures * more fixes * fix transfer token * remove unused code * update polkadot-sdk * Enable bridge on start up --------- Co-authored-by: claravanstaden --- contracts/src/Assets.sol | 6 +- contracts/src/SubstrateTypes.sol | 18 +- contracts/test/Gateway.t.sol | 10 +- .../src/benchmarking/fixtures.rs | 22 ++- parachain/pallets/inbound-queue/src/test.rs | 22 ++- .../primitives/router/src/inbound/mod.rs | 162 ++++++------------ polkadot-sdk | 2 +- smoketest/src/constants.rs | 9 + smoketest/src/helper.rs | 6 + smoketest/tests/register_token.rs | 44 ++--- smoketest/tests/send_token.rs | 44 ++--- smoketest/tests/transfer_token.rs | 18 +- .../test/scripts/configure-bridgehub.sh | 4 +- web/packages/test/scripts/set-env.sh | 4 +- .../test/scripts/set_operating_mode.sh | 12 +- 15 files changed, 138 insertions(+), 245 deletions(-) diff --git a/contracts/src/Assets.sol b/contracts/src/Assets.sol index a07397eacf..5a312c9cba 100644 --- a/contracts/src/Assets.sol +++ b/contracts/src/Assets.sol @@ -49,9 +49,9 @@ library Assets { _transferToAgent(assetHubAgent, token, sender, amount); if (destinationChain == assetHubParaID) { - payload = SubstrateTypes.SendToken(address(this), token, destinationAddress, amount); + payload = SubstrateTypes.SendToken(token, destinationAddress, amount); } else { - payload = SubstrateTypes.SendToken(address(this), token, destinationChain, destinationAddress, amount); + payload = SubstrateTypes.SendToken(token, destinationChain, destinationAddress, amount); } extraFee = $.sendTokenFee; @@ -102,7 +102,7 @@ library Assets { revert InvalidToken(); } - payload = SubstrateTypes.RegisterToken(address(this), token); + payload = SubstrateTypes.RegisterToken(token); extraFee = $.registerTokenFee; emit TokenRegistrationSent(token); diff --git a/contracts/src/SubstrateTypes.sol b/contracts/src/SubstrateTypes.sol index 48a77a7b48..b962c63d60 100644 --- a/contracts/src/SubstrateTypes.sol +++ b/contracts/src/SubstrateTypes.sol @@ -56,13 +56,9 @@ library SubstrateTypes { * `NativeTokensMessage::Create` */ // solhint-disable-next-line func-name-mixedcase - function RegisterToken(address gateway, address token) internal view returns (bytes memory) { + function RegisterToken(address token) internal view returns (bytes memory) { return bytes.concat( - bytes1(0x00), - ScaleCodec.encodeU64(uint64(block.chainid)), - bytes1(0x00), - SubstrateTypes.H160(gateway), - SubstrateTypes.H160(token) + bytes1(0x00), ScaleCodec.encodeU64(uint64(block.chainid)), bytes1(0x00), SubstrateTypes.H160(token) ); } @@ -71,16 +67,11 @@ library SubstrateTypes { * `NativeTokensMessage::Mint` */ // solhint-disable-next-line func-name-mixedcase - function SendToken(address gateway, address token, bytes32 recipient, uint128 amount) - internal - view - returns (bytes memory) - { + function SendToken(address token, bytes32 recipient, uint128 amount) internal view returns (bytes memory) { return bytes.concat( bytes1(0x00), ScaleCodec.encodeU64(uint64(block.chainid)), bytes1(0x01), - SubstrateTypes.H160(gateway), SubstrateTypes.H160(token), bytes1(0x00), recipient, @@ -88,7 +79,7 @@ library SubstrateTypes { ); } - function SendToken(address gateway, address token, ParaID paraID, bytes32 recipient, uint128 amount) + function SendToken(address token, ParaID paraID, bytes32 recipient, uint128 amount) internal view returns (bytes memory) @@ -97,7 +88,6 @@ library SubstrateTypes { bytes1(0x00), ScaleCodec.encodeU64(uint64(block.chainid)), bytes1(0x01), - SubstrateTypes.H160(gateway), SubstrateTypes.H160(token), bytes1(0x01), ScaleCodec.encodeU32(uint32(ParaID.unwrap(paraID))), diff --git a/contracts/test/Gateway.t.sol b/contracts/test/Gateway.t.sol index 4660ce9d44..3076cfdbf6 100644 --- a/contracts/test/Gateway.t.sol +++ b/contracts/test/Gateway.t.sol @@ -535,7 +535,7 @@ contract GatewayTest is Test { emit TokenRegistrationSent(address(token)); vm.expectEmit(true, false, false, false); - emit OutboundMessageAccepted(assetHubParaID, 1, SubstrateTypes.RegisterToken(address(gateway), address(token))); + emit OutboundMessageAccepted(assetHubParaID, 1, SubstrateTypes.RegisterToken(address(token))); IGateway(address(gateway)).registerToken{value: 2 ether}(address(token)); } @@ -545,7 +545,7 @@ contract GatewayTest is Test { emit TokenRegistrationSent(address(token)); vm.expectEmit(true, false, false, false); - emit OutboundMessageAccepted(assetHubParaID, 1, SubstrateTypes.RegisterToken(address(gateway), address(token))); + emit OutboundMessageAccepted(assetHubParaID, 1, SubstrateTypes.RegisterToken(address(token))); uint256 totalFee = baseFee + registerNativeTokenFee; uint256 balanceBefore = address(this).balance; @@ -572,7 +572,7 @@ contract GatewayTest is Test { // Expect the gateway to emit `OutboundMessageAccepted` vm.expectEmit(true, false, false, false); emit OutboundMessageAccepted( - assetHubParaID, 1, SubstrateTypes.SendToken(address(gateway), address(token), destPara, destAddress, 1) + assetHubParaID, 1, SubstrateTypes.SendToken(address(token), destPara, destAddress, 1) ); IGateway(address(gateway)).sendToken{value: 2 ether}(address(token), destPara, destAddress, 1); @@ -591,9 +591,7 @@ contract GatewayTest is Test { // Expect the gateway to emit `OutboundMessageAccepted` vm.expectEmit(true, false, false, false); - emit OutboundMessageAccepted( - assetHubParaID, 1, SubstrateTypes.SendToken(address(gateway), address(token), destAddress, 1) - ); + emit OutboundMessageAccepted(assetHubParaID, 1, SubstrateTypes.SendToken(address(token), destAddress, 1)); IGateway(address(gateway)).sendToken{value: 2 ether}(address(token), destPara, destAddress, 1); } diff --git a/parachain/pallets/inbound-queue/src/benchmarking/fixtures.rs b/parachain/pallets/inbound-queue/src/benchmarking/fixtures.rs index 954a4e62e4..ae1e00fb2a 100644 --- a/parachain/pallets/inbound-queue/src/benchmarking/fixtures.rs +++ b/parachain/pallets/inbound-queue/src/benchmarking/fixtures.rs @@ -11,24 +11,22 @@ pub struct InboundQueueTest { pub fn make_create_message() -> InboundQueueTest { InboundQueueTest{ execution_header: CompactExecutionHeader{ - parent_hash: hex!("de4afc4d33a83815cde0a6dd95c8e52d9e01231d67d5b5a387ec70994a96108f").into(), - block_number: 61, - state_root: hex!("7bfc6d7a49869863d621989dad7fa26c610470e65473a7e5cab87af67fc6976c").into(), - receipts_root: hex!("a2686fe1aa2f66ada774373ced7952f3591a7830b9161eec5d16149747253779").into(), + parent_hash: hex!("9e2078694f20148b48e938a5b35a4cca79e19a05b7f27c7b3daae11a2ab57524").into(), + block_number: 55, + state_root: hex!("74865f49fe887e1b9df502282b1e99ccf563861a0ed58e9e541d966207d11f3f").into(), + receipts_root: hex!("0115ab735d37c5e4cdb0374d8bb547c6dd6ccaa996d996d1eabc5399a719219e").into(), }, message: Message { - data: hex!("f8fb94eda338e4dc46038493b885327842fd3e301cab39f842a0d56f1b8dfd3ba41f19c499ceec5f9546f61befa5f10398a75d7dba53a219fecea000000000000000000000000000000000000000000000000000000000000003e8b8a0000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000032000f0000000000000000eda338e4dc46038493b885327842fd3e301cab3987d1f7fdfee7f651fabc8bfcb6e086c278b77a7d0000000000000000000000000000").to_vec(), + data: hex!("f8db94eda338e4dc46038493b885327842fd3e301cab39f842a0d56f1b8dfd3ba41f19c499ceec5f9546f61befa5f10398a75d7dba53a219fecea000000000000000000000000000000000000000000000000000000000000003e8b88000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000001e000f000000000000000087d1f7fdfee7f651fabc8bfcb6e086c278b77a7d0000").to_vec(), proof: Proof { - block_hash: hex!("565f5dc872af9351abd1ba4974753169a98f028ef2c2ef3bb6d4eea4c6519eac").into(), + block_hash: hex!("5f465744c166e9d10dc0031942a59ff82b640053253da517a1b576afdadb0363").into(), tx_index: 0, data: (vec![ - hex!("a2686fe1aa2f66ada774373ced7952f3591a7830b9161eec5d16149747253779").to_vec(), - hex!("77cc88c126ea5be62d3caea280da54cd051a05dadc03e4ecf69621da86ff9e12").to_vec(), - hex!("61ced6f6c7533351a0e60916ee60bbe4f7c49efae74bb898d06b1ae975b38c51").to_vec(), + hex!("0115ab735d37c5e4cdb0374d8bb547c6dd6ccaa996d996d1eabc5399a719219e").to_vec(), + hex!("caf5ee6beba6a6db5e2a0714a98f65ac4365c4a24e56ce033f19c7f8a2abb06a").to_vec(), ], vec![ - hex!("f90131a0697ffa69627df903e12004769f4a763d81cc2d947fa75e47996112144c7d07d1a0a755881c8727579c18865b63c20117773ddb5579acdb3ed493557bb6aa5a1117a0ba0c4975a96270e63526f20073d129931e4b1dfc4dbd477a92c7d587afaad6a1a0e2c388369c489466c7c03d0a9f3cb294c8fc659015a395f7dc7b1b28eaf18ef4a027fcb43431cd39b449624b39ceb0017fce919e14e2ca77c28f8ab981b01aa637a05f3c3aac495f2fbc982da582c8e62ac05adb1e0552a1b9e81be96028094cbcc4a0a3f115af7400065f142ec8edea4f2000a4bcf6394869df85ecb9d27d75e344daa0b5f12f70fc3e4078273e59aba72683905d11e1c4b1a68858a6e53d3c50f65b93a077cc88c126ea5be62d3caea280da54cd051a05dadc03e4ecf69621da86ff9e128080808080808080").to_vec(), - hex!("f851a061ced6f6c7533351a0e60916ee60bbe4f7c49efae74bb898d06b1ae975b38c51a0f0baa5ff24cde5e3bcc6e5394b30e10e97cca31d002d68769883293d0ebbd569808080808080808080808080808080").to_vec(), - hex!("f9026a20b9026602f902620183013defb9010000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000400000000000000000000000000000000000000000000000000000000000000000000000020200000000000000000000000000000000000040000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100010000000000000000000000000014f90157f85894eda338e4dc46038493b885327842fd3e301cab39e1a0f78bb28d4b1d7da699e5c0bc2be29c2b04b5aab6aacf6298fe5304f9db9c6d7ea000000000000000000000000087d1f7fdfee7f651fabc8bfcb6e086c278b77a7df8fb94eda338e4dc46038493b885327842fd3e301cab39f842a0d56f1b8dfd3ba41f19c499ceec5f9546f61befa5f10398a75d7dba53a219fecea000000000000000000000000000000000000000000000000000000000000003e8b8a0000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000034000f000000000000000057a2d4ff0c3866d96556884bf09fecdd7ccd530c87d1f7fdfee7f651fabc8bfcb6e086c278b77a7d3500000000000000000000000000").to_vec(), + hex!("5e2a0714a98f65ac4365c4a24e56ce033f19c7f8a2abb06a8080808080808080").to_vec(), + hex!("000f000000000000000087d1f7fdfee7f651fabc8bfcb6e086c278b77a7d0000").to_vec(), ]), }, }, diff --git a/parachain/pallets/inbound-queue/src/test.rs b/parachain/pallets/inbound-queue/src/test.rs index 36a70ae912..59db735ffa 100644 --- a/parachain/pallets/inbound-queue/src/test.rs +++ b/parachain/pallets/inbound-queue/src/test.rs @@ -238,32 +238,36 @@ fn parse_dest(message: Message) -> ParaId { } // dest para is 1000 -const OUTBOUND_QUEUE_EVENT_LOG: [u8; 253] = hex!( +const OUTBOUND_QUEUE_EVENT_LOG: [u8; 221] = hex!( " - f8fb94eda338e4dc46038493b885327842fd3e301cab39f842a0d56f1b8dfd3ba41f19c499ceec5f9546f61befa5f10398a75d7dba53a219fecea000000000000000000000000000000000000000000000000000000000000003e8b8a0000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000032000f0000000000000000eda338e4dc46038493b885327842fd3e301cab3987d1f7fdfee7f651fabc8bfcb6e086c278b77a7d0000000000000000000000000000 + f8db94eda338e4dc46038493b885327842fd3e301cab39f842a0d56f1b8dfd3ba41f19c499ceec5f9546f61befa5f10398a75d7dba53a219fecea000000000000000000000000000000000000000000000000000000000000003e8b88000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000001e000f000000000000000087d1f7fdfee7f651fabc8bfcb6e086c278b77a7d0000 " ); // dest para is 1001 -const OUTBOUND_QUEUE_EVENT_LOG_INVALID_DEST: [u8; 253] = hex!( +const OUTBOUND_QUEUE_EVENT_LOG_INVALID_DEST: [u8; 221] = hex!( " - f8fb94eda338e4dc46038493b885327842fd3e301cab39f842a0d56f1b8dfd3ba41f19c499ceec5f9546f61befa5f10398a75d7dba53a219fecea000000000000000000000000000000000000000000000000000000000000003e9b8a0000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000032000f0000000000000000eda338e4dc46038493b885327842fd3e301cab3987d1f7fdfee7f651fabc8bfcb6e086c278b77a7d0000000000000000000000000000 + f8db94eda338e4dc46038493b885327842fd3e301cab39f842a0d56f1b8dfd3ba41f19c499ceec5f9546f61befa5f10398a75d7dba53a219fecea000000000000000000000000000000000000000000000000000000000000003e9b88000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000001e000f000000000000000087d1f7fdfee7f651fabc8bfcb6e086c278b77a7d0000 " ); // gateway in message does not match configured gateway in runtime -const BAD_OUTBOUND_QUEUE_EVENT_LOG: [u8; 253] = hex!( +const BAD_OUTBOUND_QUEUE_EVENT_LOG: [u8; 221] = hex!( " - f8fb940000000000000000000000000000000000000000f842a0d56f1b8dfd3ba41f19c499ceec5f9546f61befa5f10398a75d7dba53a219fecea000000000000000000000000000000000000000000000000000000000000003e9b8a0000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000032000f0000000000000000eda338e4dc46038493b885327842fd3e301cab3987d1f7fdfee7f651fabc8bfcb6e086c278b77a7d0000000000000000000000000000 + f8db940000000000000000000000000000000000000000f842a0d56f1b8dfd3ba41f19c499ceec5f9546f61befa5f10398a75d7dba53a219fecea000000000000000000000000000000000000000000000000000000000000003e8b88000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000001e000f000000000000000087d1f7fdfee7f651fabc8bfcb6e086c278b77a7d0000 " ); // invalid payload with unsupported version -const BAD_OUTBOUND_QUEUE_LOG_UNSUPPORTED_VERSION: [u8; 253] = hex!("f8fb94eda338e4dc46038493b885327842fd3e301cab39f842a0d56f1b8dfd3ba41f19c499ceec5f9546f61befa5f10398a75d7dba53a219fecea000000000000000000000000000000000000000000000000000000000000003e8b8a0000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000032010f0000000000000000eda338e4dc46038493b885327842fd3e301cab3987d1f7fdfee7f651fabc8bfcb6e086c278b77a7d0000000000000000000000000000"); +const BAD_OUTBOUND_QUEUE_LOG_UNSUPPORTED_VERSION: [u8; 221] = hex!( + " + f8db94eda338e4dc46038493b885327842fd3e301cab39f842a0d56f1b8dfd3ba41f19c499ceec5f9546f61befa5f10398a75d7dba53a219fecea000000000000000000000000000000000000000000000000000000000000003e8b88000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000001e010f000000000000000087d1f7fdfee7f651fabc8bfcb6e086c278b77a7d0000 + " +); const XCM_HASH: [u8; 32] = [ - 232, 213, 62, 94, 48, 47, 152, 37, 168, 162, 89, 100, 6, 74, 63, 95, 211, 11, 222, 210, 1, 209, - 126, 44, 164, 122, 166, 156, 208, 228, 209, 9, + 186, 27, 67, 39, 117, 164, 224, 191, 202, 232, 218, 108, 34, 65, 36, 199, 247, 19, 150, 198, + 182, 180, 39, 112, 150, 64, 84, 15, 174, 213, 183, 207, ]; const ASSET_HUB_PARAID: u32 = 1000u32; const TEMPLATE_PARAID: u32 = 1001u32; diff --git a/parachain/primitives/router/src/inbound/mod.rs b/parachain/primitives/router/src/inbound/mod.rs index 85b76a4e6d..54447cb01f 100644 --- a/parachain/primitives/router/src/inbound/mod.rs +++ b/parachain/primitives/router/src/inbound/mod.rs @@ -36,15 +36,11 @@ pub struct MessageV1 { pub enum Command { /// Register a wrapped token on the AssetHub `ForeignAssets` pallet RegisterToken { - /// The address of the gateway - gateway: H160, /// The address of the ERC20 token to be bridged over to AssetHub token: H160, }, /// Send a token to AssetHub or another parachain SendToken { - /// The address of the gateway - gateway: H160, /// The address of the ERC20 token to be bridged over to AssetHub token: H160, /// The destination for the transfer @@ -112,44 +108,28 @@ where fun: Fungible(buy_execution_fee_amount), }; - let create_instructions = |origin_location: Junction| -> Vec> { - vec![ - UniversalOrigin(GlobalConsensus(network)), - DescendOrigin(X1(origin_location)), - WithdrawAsset(buy_execution_fee.clone().into()), - BuyExecution { fees: buy_execution_fee.clone(), weight_limit: Unlimited }, - SetAppendix( - vec![ - RefundSurplus, - DepositAsset { - assets: Wild(AllCounted(1)), - beneficiary: ( - Parent, - Parent, - GlobalConsensus(network), - origin_location, - ) - .into(), - }, - ] - .into(), - ), - ] - }; + let mut instructions = vec![ + UniversalOrigin(GlobalConsensus(network)), + WithdrawAsset(buy_execution_fee.clone().into()), + BuyExecution { fees: buy_execution_fee, weight_limit: Unlimited }, + SetAppendix( + vec![ + RefundSurplus, + DepositAsset { + assets: Wild(AllCounted(1)), + beneficiary: (Parent, Parent, GlobalConsensus(network)).into(), + }, + ] + .into(), + ), + ]; let xcm = match command { - Command::RegisterToken { gateway, token, .. } => { + Command::RegisterToken { token, .. } => { let owner = - GlobalConsensusEthereumAccountConvertsFor::<[u8; 32]>::from_params( - &chain_id, - gateway.as_fixed_bytes(), - ); - - let origin_location = AccountKey20 { network: None, key: gateway.into() }; + GlobalConsensusEthereumConvertsFor::<[u8; 32]>::from_params(&chain_id); - let asset_id = Self::convert_token_address(network, gateway, token); - - let mut instructions = create_instructions(origin_location); + let asset_id = Self::convert_token_address(network, token); let create_call_index: [u8; 2] = CreateAssetCall::get(); instructions.extend(vec![ @@ -169,15 +149,10 @@ where ]); instructions.into() }, - Command::SendToken { gateway, token, destination, amount } => { - let asset = MultiAsset::from(( - Self::convert_token_address(network, gateway, token), - amount, - )); - - let origin_location = AccountKey20 { network: None, key: gateway.into() }; + Command::SendToken { token, destination, amount, .. } => { + let asset = + MultiAsset::from((Self::convert_token_address(network, token), amount)); - let mut instructions = create_instructions(origin_location); instructions.extend(vec![ ReserveAssetDeposited(vec![asset.clone()].into()), ClearOrigin, @@ -242,12 +217,11 @@ where SendTokenExecutionFee: Get, { // Convert ERC20 token address to a Multilocation that can be understood by Assets Hub. - fn convert_token_address(network: NetworkId, origin: H160, token: H160) -> MultiLocation { + fn convert_token_address(network: NetworkId, token: H160) -> MultiLocation { MultiLocation { parents: 2, - interior: X3( + interior: X2( GlobalConsensus(network), - AccountKey20 { network: None, key: origin.into() }, AccountKey20 { network: None, key: token.into() }, ), } @@ -265,66 +239,56 @@ where } } -pub struct GlobalConsensusEthereumAccountConvertsFor(PhantomData); -impl ConvertLocation for GlobalConsensusEthereumAccountConvertsFor +pub struct GlobalConsensusEthereumConvertsFor(PhantomData); +impl ConvertLocation for GlobalConsensusEthereumConvertsFor where AccountId: From<[u8; 32]> + Clone, { fn convert_location(location: &MultiLocation) -> Option { - if let MultiLocation { - interior: X2(GlobalConsensus(Ethereum { chain_id }), AccountKey20 { key, .. }), - .. - } = location + if let MultiLocation { interior: X1(GlobalConsensus(Ethereum { chain_id })), .. } = location { - Some(Self::from_params(chain_id, key).into()) + Some(Self::from_params(chain_id).into()) } else { None } } } -impl GlobalConsensusEthereumAccountConvertsFor { - fn from_params(chain_id: &u64, key: &[u8; 20]) -> [u8; 32] { - (b"ethereum", chain_id, key).using_encoded(blake2_256) +impl GlobalConsensusEthereumConvertsFor { + fn from_params(chain_id: &u64) -> [u8; 32] { + (b"ethereum-chain", chain_id).using_encoded(blake2_256) } } #[cfg(test)] mod tests { - use super::{FromEthereumGlobalConsensus, GlobalConsensusEthereumAccountConvertsFor}; + use super::{FromEthereumGlobalConsensus, GlobalConsensusEthereumConvertsFor}; use frame_support::{parameter_types, traits::ContainsPair}; use hex_literal::hex; use sp_core::crypto::Ss58Codec; use xcm::v3::prelude::*; use xcm_executor::traits::ConvertLocation; - const CONTRACT_ADDRESS: [u8; 20] = hex!("EDa338E4dC46038493b885327842fD3E301CaB39"); const NETWORK: NetworkId = Ethereum { chain_id: 15 }; const SS58_FORMAT: u16 = 2; const EXPECTED_SOVEREIGN_KEY: [u8; 32] = - hex!("c9794dd8013efb2ad83f668845c62b373c16ad33971745731408058e4d0c6ff5"); + hex!("da4d66c3651dc151264eee5460493210338e41a7bbfca91a520e438daf180bf5"); const EXPECTED_SOVEREIGN_ADDRESS: &'static str = - "H8VBFC4LG91ByxMG6GwsCcAacjitnzGmGbqnvSEQFBywJEL"; + "HWYx2xgcdpSjJQicUUZFRR1EJNPVEQoUDSUB29rfxF617nv"; parameter_types! { pub EthereumNetwork: NetworkId = NETWORK; - pub EthereumLocation: MultiLocation = MultiLocation::new(2, X2(GlobalConsensus(EthereumNetwork::get()), AccountKey20 { network: None, key: CONTRACT_ADDRESS })); + pub EthereumLocation: MultiLocation = MultiLocation::new(2, X1(GlobalConsensus(EthereumNetwork::get()))); } #[test] fn test_contract_location_without_network_converts_successfully() { - let contract_location = MultiLocation { - parents: 2, - interior: X2( - GlobalConsensus(NETWORK), - AccountKey20 { network: None, key: CONTRACT_ADDRESS }, - ), - }; + let contract_location = + MultiLocation { parents: 2, interior: X1(GlobalConsensus(NETWORK)) }; - let account = GlobalConsensusEthereumAccountConvertsFor::<[u8; 32]>::convert_location( - &contract_location, - ) - .unwrap(); + let account = + GlobalConsensusEthereumConvertsFor::<[u8; 32]>::convert_location(&contract_location) + .unwrap(); let address = frame_support::sp_runtime::AccountId32::new(account) .to_ss58check_with_version(SS58_FORMAT.into()); @@ -336,18 +300,12 @@ mod tests { #[test] fn test_contract_location_with_network_converts_successfully() { - let contract_location = MultiLocation { - parents: 2, - interior: X2( - GlobalConsensus(NETWORK), - AccountKey20 { network: Some(NETWORK), key: CONTRACT_ADDRESS }, - ), - }; + let contract_location = + MultiLocation { parents: 2, interior: X1(GlobalConsensus(NETWORK)) }; - let account = GlobalConsensusEthereumAccountConvertsFor::<[u8; 32]>::convert_location( - &contract_location, - ) - .unwrap(); + let account = + GlobalConsensusEthereumConvertsFor::<[u8; 32]>::convert_location(&contract_location) + .unwrap(); let address = frame_support::sp_runtime::AccountId32::new(account) .to_ss58check_with_version(SS58_FORMAT.into()); assert_eq!(account, EXPECTED_SOVEREIGN_KEY); @@ -362,42 +320,24 @@ mod tests { MultiLocation { parents: 2, interior: X2(GlobalConsensus(Polkadot), Parachain(1000)) }; assert_eq!( - GlobalConsensusEthereumAccountConvertsFor::<[u8; 32]>::convert_location( - &contract_location - ), + GlobalConsensusEthereumConvertsFor::<[u8; 32]>::convert_location(&contract_location), None, ); } #[test] fn test_from_ethereum_global_consensus_with_containing_asset_yields_true() { - let origin = MultiLocation { - parents: 2, - interior: X2( - GlobalConsensus(NETWORK), - AccountKey20 { network: None, key: CONTRACT_ADDRESS }, - ), - }; + let origin = MultiLocation { parents: 2, interior: X1(GlobalConsensus(NETWORK)) }; let asset = MultiLocation { parents: 2, - interior: X3( - GlobalConsensus(NETWORK), - AccountKey20 { network: None, key: CONTRACT_ADDRESS }, - AccountKey20 { network: None, key: [0; 20] }, - ), + interior: X2(GlobalConsensus(NETWORK), AccountKey20 { network: None, key: [0; 20] }), }; assert!(FromEthereumGlobalConsensus::::contains(&asset, &origin)); } #[test] fn test_from_ethereum_global_consensus_without_containing_asset_yields_false() { - let origin = MultiLocation { - parents: 2, - interior: X2( - GlobalConsensus(NETWORK), - AccountKey20 { network: None, key: CONTRACT_ADDRESS }, - ), - }; + let origin = MultiLocation { parents: 2, interior: X1(GlobalConsensus(NETWORK)) }; let asset = MultiLocation { parents: 2, interior: X2(GlobalConsensus(Polkadot), Parachain(1000)) }; assert!(!FromEthereumGlobalConsensus::::contains(&asset, &origin)); @@ -409,11 +349,7 @@ mod tests { MultiLocation { parents: 2, interior: X2(GlobalConsensus(Polkadot), Parachain(1000)) }; let asset = MultiLocation { parents: 2, - interior: X3( - GlobalConsensus(NETWORK), - AccountKey20 { network: None, key: CONTRACT_ADDRESS }, - AccountKey20 { network: None, key: [0; 20] }, - ), + interior: X2(GlobalConsensus(NETWORK), AccountKey20 { network: None, key: [0; 20] }), }; assert!(!FromEthereumGlobalConsensus::::contains(&asset, &origin)); } diff --git a/polkadot-sdk b/polkadot-sdk index 7414de6892..dab8646cfd 160000 --- a/polkadot-sdk +++ b/polkadot-sdk @@ -1 +1 @@ -Subproject commit 7414de6892e80bde96937f37bb48ecf19569d3f4 +Subproject commit dab8646cfdd5fe8ffe82335feda064cd4b8a5f2c diff --git a/smoketest/src/constants.rs b/smoketest/src/constants.rs index c23dc0225e..f76a175a45 100644 --- a/smoketest/src/constants.rs +++ b/smoketest/src/constants.rs @@ -30,3 +30,12 @@ pub const ASSET_HUB_AGENT_ID: [u8; 32] = // Agent for template parachain 1001 pub const SIBLING_AGENT_ID: [u8; 32] = hex!("e01018a3378502770faff44fbef3910d120a0353d18be653625b8daa88a86453"); + +// The deployment addresses of the following contracts are stable in our E2E env, unless we modify +// the order in contracts are deployed in DeployScript.sol. +pub const SNOWBRIDGE_SOVEREIGN: [u8; 32] = + hex!("da4d66c3651dc151264eee5460493210338e41a7bbfca91a520e438daf180bf5"); + +// SS58: DE14BzQ1bDXWPKeLoAqdLAm1GpyAWaWF1knF74cEZeomTBM +pub const FERDIE: [u8; 32] = + hex!("1cbd2d43530a44705ad088af313e18f80b53ef16b36177cd4b77b846f2a5f07c"); diff --git a/smoketest/src/helper.rs b/smoketest/src/helper.rs index 7c097d5317..3000748170 100644 --- a/smoketest/src/helper.rs +++ b/smoketest/src/helper.rs @@ -86,6 +86,7 @@ impl Config for AssetHubConfig { } pub struct TestClients { + pub asset_hub_client: Box>, pub bridge_hub_client: Box>, pub template_client: Box>, pub relaychain_client: Box>, @@ -98,6 +99,10 @@ pub async fn initial_clients() -> Result .await .expect("can not connect to bridgehub"); + let asset_hub_client: OnlineClient = OnlineClient::from_url(ASSET_HUB_WS_URL) + .await + .expect("can not connect to bridgehub"); + let template_client: OnlineClient = OnlineClient::from_url(TEMPLATE_NODE_WS_URL) .await @@ -118,6 +123,7 @@ pub async fn initial_clients() -> Result let ethereum_signed_client = initialize_wallet().await.expect("initialize wallet"); Ok(TestClients { + asset_hub_client: Box::new(asset_hub_client), bridge_hub_client: Box::new(bridge_hub_client), template_client: Box::new(template_client), relaychain_client: Box::new(relaychain_client), diff --git a/smoketest/tests/register_token.rs b/smoketest/tests/register_token.rs index d3d2721051..d8d4af6469 100644 --- a/smoketest/tests/register_token.rs +++ b/smoketest/tests/register_token.rs @@ -1,15 +1,13 @@ use codec::Encode; use ethers::{ core::types::Address, - middleware::SignerMiddleware, - providers::{Http, Provider}, - signers::{LocalWallet, Signer}, utils::{parse_units, rlp::Encodable}, }; use futures::StreamExt; -use hex_literal::hex; use snowbridge_smoketest::{ + constants::{GATEWAY_PROXY_CONTRACT, SNOWBRIDGE_SOVEREIGN, WETH_CONTRACT}, contracts::{i_gateway, weth9}, + helper::initial_clients, parachains::assethub::api::{ foreign_assets::events::Created, runtime_types::{ @@ -19,41 +17,24 @@ use snowbridge_smoketest::{ Junction::{AccountKey20, GlobalConsensus}, NetworkId, }, - junctions::Junctions::X3, + junctions::Junctions::X2, }, }, }, }; -use std::{sync::Arc, time::Duration}; -use subxt::{utils::AccountId32, OnlineClient, PolkadotConfig}; - -// The deployment addresses of the following contracts are stable in our E2E env, unless we modify -// the order in contracts are deployed in DeployScript.sol. -const ASSET_HUB_WS_URL: &str = "ws://127.0.0.1:12144"; -const ETHEREUM_API: &str = "http://localhost:8545"; -const ETHEREUM_KEY: &str = "0x5e002a1af63fd31f1c25258f3082dc889762664cb8f218d86da85dff8b07b342"; -const GATEWAY_PROXY_CONTRACT: [u8; 20] = hex!("EDa338E4dC46038493b885327842fD3E301CaB39"); -const WETH_CONTRACT: [u8; 20] = hex!("87d1f7fdfEe7f651FaBc8bFCB6E086C278b77A7d"); -const GATEWAY_PROXY_SOVEREIGN: [u8; 32] = - hex!("c9794dd8013efb2ad83f668845c62b373c16ad33971745731408058e4d0c6ff5"); +use subxt::utils::AccountId32; #[tokio::test] async fn register_token() { - let provider = Provider::::try_from(ETHEREUM_API) - .unwrap() - .interval(Duration::from_millis(10u64)); - let wallet: LocalWallet = ETHEREUM_KEY.parse::().unwrap().with_chain_id(15u64); - let client = SignerMiddleware::new(provider, wallet); - let client = Arc::new(client); + let test_clients = initial_clients().await.expect("initialize clients"); + let ethereum_client = *(test_clients.ethereum_signed_client.clone()); + let assethub = *(test_clients.asset_hub_client.clone()); let gateway_addr: Address = GATEWAY_PROXY_CONTRACT.into(); - let gateway = i_gateway::IGateway::new(gateway_addr, client.clone()); + let gateway = i_gateway::IGateway::new(gateway_addr, ethereum_client.clone()); let weth_addr: Address = WETH_CONTRACT.into(); - let weth = weth9::WETH9::new(weth_addr, client.clone()); - - let assethub: OnlineClient = - OnlineClient::from_url(ASSET_HUB_WS_URL).await.unwrap(); + let weth = weth9::WETH9::new(weth_addr, ethereum_client.clone()); let fee = parse_units(2, "ether").unwrap(); @@ -86,14 +67,13 @@ async fn register_token() { let expected_asset_id: MultiLocation = MultiLocation { parents: 2, - interior: X3( + interior: X2( GlobalConsensus(NetworkId::Ethereum { chain_id: 15 }), - AccountKey20 { network: None, key: GATEWAY_PROXY_CONTRACT.into() }, AccountKey20 { network: None, key: WETH_CONTRACT.into() }, ), }; - let expected_creator: AccountId32 = GATEWAY_PROXY_SOVEREIGN.into(); - let expected_owner: AccountId32 = GATEWAY_PROXY_SOVEREIGN.into(); + let expected_creator: AccountId32 = SNOWBRIDGE_SOVEREIGN.into(); + let expected_owner: AccountId32 = SNOWBRIDGE_SOVEREIGN.into(); let mut created_event_found = false; while let Some(Ok(block)) = blocks.next().await { diff --git a/smoketest/tests/send_token.rs b/smoketest/tests/send_token.rs index cf45119526..85b2e1461a 100644 --- a/smoketest/tests/send_token.rs +++ b/smoketest/tests/send_token.rs @@ -1,14 +1,12 @@ use ethers::{ core::types::{Address, U256}, - middleware::SignerMiddleware, - providers::{Http, Provider}, - signers::{LocalWallet, Signer}, utils::parse_units, }; use futures::StreamExt; -use hex_literal::hex; use snowbridge_smoketest::{ + constants::{FERDIE, GATEWAY_PROXY_CONTRACT, WETH_CONTRACT}, contracts::{i_gateway, weth9}, + helper::initial_clients, parachains::assethub::api::{ foreign_assets::events::Issued, runtime_types::{ @@ -18,46 +16,25 @@ use snowbridge_smoketest::{ Junction::{AccountKey20, GlobalConsensus}, NetworkId, }, - junctions::Junctions::X3, + junctions::Junctions::X2, }, }, }, }; use sp_core::Encode; -use std::{sync::Arc, time::Duration}; -use subxt::{utils::AccountId32, OnlineClient, PolkadotConfig}; +use subxt::utils::AccountId32; -// The deployment addresses of the following contracts are stable in our E2E env, unless we modify -// the order in contracts are deployed in DeployScript.sol. -const ASSET_HUB_WS_URL: &str = "ws://127.0.0.1:12144"; -const ETHEREUM_API: &str = "http://localhost:8545"; -const ETHEREUM_KEY: &str = "0x5e002a1af63fd31f1c25258f3082dc889762664cb8f218d86da85dff8b07b342"; -const WETH_CONTRACT: [u8; 20] = hex!("87d1f7fdfEe7f651FaBc8bFCB6E086C278b77A7d"); -const GATEWAY_PROXY_CONTRACT: [u8; 20] = hex!("EDa338E4dC46038493b885327842fD3E301CaB39"); - -// SS58: DE14BzQ1bDXWPKeLoAqdLAm1GpyAWaWF1knF74cEZeomTBM -const FERDIE: [u8; 32] = hex!("1cbd2d43530a44705ad088af313e18f80b53ef16b36177cd4b77b846f2a5f07c"); - -// TODO: test sendNativeToken #[tokio::test] async fn send_token() { - let provider = Provider::::try_from(ETHEREUM_API) - .unwrap() - .interval(Duration::from_millis(10u64)); - - let wallet: LocalWallet = ETHEREUM_KEY.parse::().unwrap().with_chain_id(15u64); - - let client = SignerMiddleware::new(provider.clone(), wallet.clone()); - let client = Arc::new(client); + let test_clients = initial_clients().await.expect("initialize clients"); + let ethereum_client = *(test_clients.ethereum_signed_client.clone()); + let assethub = *(test_clients.asset_hub_client.clone()); let gateway_addr: Address = GATEWAY_PROXY_CONTRACT.into(); - let gateway = i_gateway::IGateway::new(gateway_addr, client.clone()); + let gateway = i_gateway::IGateway::new(gateway_addr, ethereum_client.clone()); let weth_addr: Address = WETH_CONTRACT.into(); - let weth = weth9::WETH9::new(weth_addr, client.clone()); - - let assethub: OnlineClient = - OnlineClient::from_url(ASSET_HUB_WS_URL).await.unwrap(); + let weth = weth9::WETH9::new(weth_addr, ethereum_client.clone()); // Mint WETH tokens let value = parse_units("1", "ether").unwrap(); @@ -100,9 +77,8 @@ async fn send_token() { let expected_asset_id: MultiLocation = MultiLocation { parents: 2, - interior: X3( + interior: X2( GlobalConsensus(NetworkId::Ethereum { chain_id: 15 }), - AccountKey20 { network: None, key: GATEWAY_PROXY_CONTRACT.into() }, AccountKey20 { network: None, key: WETH_CONTRACT.into() }, ), }; diff --git a/smoketest/tests/transfer_token.rs b/smoketest/tests/transfer_token.rs index 2fdecdc255..a2a3bac992 100644 --- a/smoketest/tests/transfer_token.rs +++ b/smoketest/tests/transfer_token.rs @@ -21,7 +21,7 @@ use snowbridge_smoketest::{ xcm::{ v3::{ junction::{Junction, NetworkId}, - junctions::{Junctions, Junctions::Here}, + junctions::Junctions, multiasset::{AssetId, Fungibility, MultiAsset, MultiAssets}, }, VersionedMultiAssets, VersionedMultiLocation, @@ -59,19 +59,13 @@ async fn transfer_token() { let signer: PairSigner = PairSigner::new(keypair); - let fee: u128 = 1000_800_566_581; let amount: u128 = 1_000_000_000; let assets = VersionedMultiAssets::V3(MultiAssets(vec![ - MultiAsset { - id: AssetId::Concrete(MultiLocation { parents: 1, interior: Here }), - fun: Fungibility::Fungible(fee), - }, MultiAsset { id: AssetId::Concrete(MultiLocation { parents: 2, - interior: Junctions::X3( + interior: Junctions::X2( Junction::GlobalConsensus(NetworkId::Ethereum { chain_id: 15 }), - Junction::AccountKey20 { network: None, key: GATEWAY_PROXY_CONTRACT.into() }, Junction::AccountKey20 { network: None, key: WETH_CONTRACT.into() }, ), }), @@ -81,16 +75,14 @@ async fn transfer_token() { let destination = VersionedMultiLocation::V3(MultiLocation { parents: 2, - interior: Junctions::X2( + interior: Junctions::X1( Junction::GlobalConsensus(NetworkId::Ethereum { chain_id: 15 }), - Junction::AccountKey20 { network: None, key: DESTINATION_ADDRESS.into() }, ), }); let beneficiary = VersionedMultiLocation::V3(MultiLocation { - parents: 2, - interior: Junctions::X2( - Junction::GlobalConsensus(NetworkId::Ethereum { chain_id: 15 }), + parents: 0, + interior: Junctions::X1( Junction::AccountKey20 { network: None, key: DESTINATION_ADDRESS.into() }, ), }); diff --git a/web/packages/test/scripts/configure-bridgehub.sh b/web/packages/test/scripts/configure-bridgehub.sh index 1c578f2387..83fb369362 100755 --- a/web/packages/test/scripts/configure-bridgehub.sh +++ b/web/packages/test/scripts/configure-bridgehub.sh @@ -3,6 +3,7 @@ set -eu source scripts/set-env.sh source scripts/xcm-helper.sh +source scripts/set_operating_mode.sh config_beacon_checkpoint() { pushd $root_dir @@ -27,7 +28,7 @@ fund_accounts() { transfer_balance $relaychain_ws_url "//Charlie" 1013 1000000000000000 $template_sovereign_account transfer_balance $relaychain_ws_url "//Charlie" 1013 1000000000000000 $beacon_relayer_pub_key transfer_balance $relaychain_ws_url "//Charlie" 1013 1000000000000000 $execution_relayer_pub_key - transfer_balance $relaychain_ws_url "//Charlie" 1000 1000000000000000 $gateway_contract_sovereign_account + transfer_balance $relaychain_ws_url "//Charlie" 1000 1000000000000000 $snowbridge_sovereign_account } open_hrmp_channel() @@ -73,6 +74,7 @@ configure_bridgehub() { wait_beacon_chain_ready config_beacon_checkpoint open_hrmp_channels + enable_gateway } if [ -z "${from_start_services:-}" ]; then diff --git a/web/packages/test/scripts/set-env.sh b/web/packages/test/scripts/set-env.sh index 5b7cac834f..74a703ced2 100755 --- a/web/packages/test/scripts/set-env.sh +++ b/web/packages/test/scripts/set-env.sh @@ -60,8 +60,8 @@ template_sovereign_account="${TEMPLATE_SOVEREIGN_ACCOUNT:-0x7369626ce90300000000 beacon_relayer_pub_key="${BEACON_RELAYER_PUB_KEY:-0xc46e141b5083721ad5f5056ba1cded69dce4a65f027ed3362357605b1687986a}" # Execution relay account (//ExecutionRelay 5CFNWKMFPsw5Cs2Teo6Pvg7rWyjKiFfqPZs8U4MZXzMYFwXL in testnet) execution_relayer_pub_key="${EXECUTION_RELAYER_PUB_KEY:-0x08228efd065c58a043da95c8bf177659fc587643e71e7ed1534666177730196f}" -# Gateway contract account (H8VBFC4LG91ByxMG6GwsCcAacjitnzGmGbqnvSEQFBywJEL in testnet) -gateway_contract_sovereign_account="${GATEWAY_CONTRACT_SOVEREIGN_ACCOUNT:-0xc9794dd8013efb2ad83f668845c62b373c16ad33971745731408058e4d0c6ff5}" +# Snowbridge account (HWYx2xgcdpSjJQicUUZFRR1EJNPVEQoUDSUB29rfxF617nv in testnet) +snowbridge_sovereign_account="${SNOWBRIDGE_SOVEREIGN_ACCOUNT:-0xda4d66c3651dc151264eee5460493210338e41a7bbfca91a520e438daf180bf5}" # Config for deploying contracts diff --git a/web/packages/test/scripts/set_operating_mode.sh b/web/packages/test/scripts/set_operating_mode.sh index 7edc8c29c1..72bd376fe3 100755 --- a/web/packages/test/scripts/set_operating_mode.sh +++ b/web/packages/test/scripts/set_operating_mode.sh @@ -14,10 +14,12 @@ disable_gateway() { send_governance_transact_from_relaychain $BRIDGE_HUB_PARAID "$transact_call" } -read -p "Enable gateway? (Y/N): " confirm +if [ -z "${from_start_services:-}" ]; then + read -p "Enable gateway? (Y/N): " confirm -if [[ $confirm == [yY] || $confirm == [yY][eE][sS] ]]; then - enable_gateway -else - disable_gateway + if [[ $confirm == [yY] || $confirm == [yY][eE][sS] ]]; then + enable_gateway + else + disable_gateway + fi fi