From c310a8915eb2cc749b9ba6caca3edbec6729e2c6 Mon Sep 17 00:00:00 2001 From: Freddy Li Date: Mon, 15 Jan 2024 12:04:48 -0500 Subject: [PATCH] adject the local mock runtime impl for xcm bridge --- Cargo.lock | 1 - bridge/src/mock.rs | 4 +- xcm-bridge/Cargo.toml | 1 - xcm-bridge/src/lib.rs | 3 +- xcm-bridge/src/mock/mod.rs | 95 ++++++++++++++++++++++++++++++------- xcm-bridge/src/mock/para.rs | 1 - 6 files changed, 80 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 664b418..317f4fc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10243,7 +10243,6 @@ dependencies = [ "staging-xcm", "staging-xcm-builder", "staging-xcm-executor", - "sygma-bridge", "sygma-bridge-forwarder", "sygma-traits", "xcm-simulator", diff --git a/bridge/src/mock.rs b/bridge/src/mock.rs index ce172c9..aef7c56 100644 --- a/bridge/src/mock.rs +++ b/bridge/src/mock.rs @@ -48,8 +48,8 @@ frame_support::construct_runtime!( SygmaBridge: sygma_bridge::{Pallet, Call, Storage, Event} = 6, SygmaPercentageFeeHandler: sygma_percentage_feehandler::{Pallet, Call, Storage, Event} = 7, SygmaFeeHandlerRouter: sygma_fee_handler_router::{Pallet, Call, Storage, Event} = 8, - SygmaBridgeForwarder: sygma_bridge_forwarder::{Pallet, Call, Storage, Event} = 9, - SygmaXcmBridge: sygma_xcm_bridge::{Pallet, Call, Storage, Event} = 10, + SygmaBridgeForwarder: sygma_bridge_forwarder::{Pallet, Event} = 9, + SygmaXcmBridge: sygma_xcm_bridge::{Pallet, Event} = 10, } ); diff --git a/xcm-bridge/Cargo.toml b/xcm-bridge/Cargo.toml index 2d0f91d..7387b29 100644 --- a/xcm-bridge/Cargo.toml +++ b/xcm-bridge/Cargo.toml @@ -64,7 +64,6 @@ cumulus-primitives-utility = { git = "https://github.com/paritytech/polkadot-sdk # Local sygma-traits = { path = "../traits" } pallet-parachain-info = { path = "../parachain-info" } -sygma-bridge = { path = "../bridge" } [features] default = ["std"] diff --git a/xcm-bridge/src/lib.rs b/xcm-bridge/src/lib.rs index d00a5ef..ed32d91 100644 --- a/xcm-bridge/src/lib.rs +++ b/xcm-bridge/src/lib.rs @@ -324,10 +324,9 @@ pub mod pallet { use crate::mock::{ ALICE, BOB, ENDOWED_BALANCE, - ParaA, ParaAssets, ParaB, ParaBalances, TestNet, + ParaA, ParaAssets, ParaB, ParaBalances, TestNet, UsdtLocation }; use crate::mock::para::Runtime; - use sygma_bridge::mock::UsdtLocation; #[test] fn test_transfer_self_reserve_asset_to_parachain() { diff --git a/xcm-bridge/src/mock/mod.rs b/xcm-bridge/src/mock/mod.rs index 00c42ec..4197d1f 100644 --- a/xcm-bridge/src/mock/mod.rs +++ b/xcm-bridge/src/mock/mod.rs @@ -3,6 +3,9 @@ #![cfg(test)] +use std::marker::PhantomData; +use std::result; + use frame_support::{construct_runtime, pallet_prelude::ConstU32, parameter_types, sp_runtime::{ AccountId32, BuildStorage, @@ -13,15 +16,13 @@ use polkadot_parachain_primitives::primitives::Sibling; use sp_io::TestExternalities; use xcm::latest::{prelude::*, Weight as XCMWeight}; use xcm_builder::{AccountId32Aliases, AllowTopLevelPaidExecutionFrom, AllowUnpaidExecutionFrom, CurrencyAdapter, FixedWeightBounds, FungiblesAdapter, IsConcrete, NativeAsset, NoChecking, ParentIsPreset, RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia, SignedAccountId32AsNative, SovereignSignedViaLocation, TakeWeightCredit}; -use xcm_executor::{traits::WithOriginFilter, XcmExecutor}; +use xcm_executor::{Config, traits::{Error as ExecutionError, MatchesFungibles, WithOriginFilter}, XcmExecutor}; use xcm_simulator::{decl_test_network, decl_test_parachain, decl_test_relay_chain, TestExt}; -use sygma_bridge::XCMAssetTransactor; - use crate as sygma_xcm_bridge; -mod relay; -pub(crate) mod para; +pub mod relay; +pub mod para; type Block = frame_system::mocking::MockBlock; @@ -34,9 +35,10 @@ construct_runtime!( System: frame_system::{Pallet, Call, Storage, Config, Event}, Assets: pallet_assets::{Pallet, Call, Storage, Event}, Balances: pallet_balances::{Pallet, Call, Storage, Config, Event}, + ParachainInfo: pallet_parachain_info::{Pallet, Storage, Config}, CumulusXcm: cumulus_pallet_xcm::{Pallet, Event, Origin}, - SygmaXcmBridge: sygma_xcm_bridge::{Pallet, Storage, Event}, - SygmaBridgeForwarder: sygma_bridge_forwarder::{Pallet, Call, Storage, Event}, + SygmaXcmBridge: sygma_xcm_bridge::{Pallet, Event}, + SygmaBridgeForwarder: sygma_bridge_forwarder::{Pallet, Event}, } ); @@ -109,10 +111,12 @@ parameter_types! { pub const MetadataDepositPerByte: Balance = 1; } +pub type AssetId = u32; + impl pallet_assets::Config for Runtime { type RuntimeEvent = RuntimeEvent; type Balance = Balance; - type AssetId = u32; + type AssetId = AssetId; type AssetIdParameter = codec::Compact; type Currency = Balances; type CreateOrigin = AsEnsureOriginWithArg>; @@ -141,17 +145,20 @@ impl sygma_xcm_bridge::Config for Runtime { type SelfLocation = SelfLocation; } +impl sygma_bridge_forwarder::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + type SygmaBridge = (); + type XCMBridge = (); +} + +impl pallet_parachain_info::Config for Runtime {} + pub struct XcmConfig; impl Config for XcmConfig { type RuntimeCall = RuntimeCall; type XcmSender = XcmRouter; - type AssetTransactor = XCMAssetTransactor< - CurrencyTransactor, - FungiblesTransactor, - SygmaXcmBridge, - SygmaBridgeForwarder, - >; + type AssetTransactor = (CurrencyTransactor, FungiblesTransactor); type OriginConverter = XcmOriginToTransactDispatchOrigin; type IsReserve = NativeAsset; type IsTeleporter = (); @@ -209,6 +216,42 @@ parameter_types! { pub const MaxInstructions: u32 = 100; } +impl cumulus_pallet_xcm::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + type XcmExecutor = XcmExecutor; +} + +parameter_types! { + pub NativeLocation: MultiLocation = MultiLocation::here(); + pub UsdtAssetId: AssetId = 0; + pub UsdtLocation: MultiLocation = MultiLocation::new( + 1, + X3( + Parachain(2005), + slice_to_generalkey(b"sygma"), + slice_to_generalkey(b"usdt"), + ), + ); + pub CheckingAccount: AccountId32 = AccountId32::new([102u8; 32]); +} + +pub struct SimpleForeignAssetConverter(PhantomData<()>); + +impl MatchesFungibles for SimpleForeignAssetConverter { + fn matches_fungibles(a: &MultiAsset) -> result::Result<(AssetId, Balance), ExecutionError> { + match (&a.fun, &a.id) { + (Fungible(ref amount), Concrete(ref id)) => { + if id == &UsdtLocation::get() { + Ok((UsdtAssetId::get(), *amount)) + } else { + Err(ExecutionError::AssetNotHandled) + } + } + _ => Err(ExecutionError::AssetNotHandled), + } + } +} + pub type CurrencyTransactor = CurrencyAdapter< // Use this currency: Balances, @@ -227,7 +270,7 @@ pub type FungiblesTransactor = FungiblesAdapter< // Use this fungibles implementation: Assets, // Use this currency when it is a fungible asset matching the given location or name: - sygma_bridge::SimpleForeignAssetConverter, + SimpleForeignAssetConverter, // Convert an XCM MultiLocation into a local account id: LocationToAccountId, // Our chain's account ID type (we can't get away without mentioning it explicitly): @@ -239,11 +282,11 @@ pub type FungiblesTransactor = FungiblesAdapter< >; parameter_types! { - pub SelfLocation: MultiLocation = MultiLocation::new(1, X1(Parachain(ParachainInfo::get().into()))); + pub SelfLocation: MultiLocation = MultiLocation::new(1, X1(Parachain(ParachainInfo::parachain_id().into()))); } pub fn new_test_ext() -> sp_io::TestExternalities { - let t = frame_system::GenesisConfig::::default().build_storage().unwrap(); + let t = frame_system::GenesisConfig::::default().build_storage().unwrap(); let mut ext = sp_io::TestExternalities::new(t); ext.execute_with(|| System::set_block_number(1)); ext @@ -253,7 +296,7 @@ pub fn new_test_ext() -> sp_io::TestExternalities { // include the most recent event, but do not have to include every past event. pub fn assert_events(mut expected: Vec) { let mut actual: Vec = - system::Pallet::::events().iter().map(|e| e.event.clone()).collect(); + system::Pallet::::events().iter().map(|e| e.event.clone()).collect(); expected.reverse(); @@ -268,6 +311,9 @@ pub const BOB: AccountId32 = AccountId32::new([1u8; 32]); pub const ENDOWED_BALANCE: u128 = 100_000_000; pub const TEST_THRESHOLD: u32 = 2; +pub type ParaBalances = pallet_balances::Pallet; +pub type ParaAssets = pallet_assets::Pallet; + decl_test_parachain! { pub struct ParaA { Runtime = para::Runtime, @@ -359,4 +405,17 @@ pub fn relay_ext() -> sp_io::TestExternalities { let mut ext = sp_io::TestExternalities::new(t); ext.execute_with(|| System::set_block_number(1)); ext +} + +pub fn slice_to_generalkey(key: &[u8]) -> Junction { + let len = key.len(); + assert!(len <= 32); + GeneralKey { + length: len as u8, + data: { + let mut data = [0u8; 32]; + data[..len].copy_from_slice(key); + data + }, + } } \ No newline at end of file diff --git a/xcm-bridge/src/mock/para.rs b/xcm-bridge/src/mock/para.rs index 4ead721..710f4ab 100644 --- a/xcm-bridge/src/mock/para.rs +++ b/xcm-bridge/src/mock/para.rs @@ -20,7 +20,6 @@ construct_runtime!( Balances: pallet_balances::{Pallet, Call, Config, Storage, Event}, Assets: pallet_assets::{Pallet, Call, Storage, Event}, - ParachainInfo: pallet_parachain_info::{Pallet, Storage, Config}, ParachainSystem: cumulus_pallet_parachain_system::{Pallet, Storage, Config}, XcmpQueue: cumulus_pallet_xcmp_queue::{Pallet, Call, Storage, Event},