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

Remove old routers and axelar relayer #1953

Merged
merged 4 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
clean integration testing stuff
  • Loading branch information
lemunozm committed Aug 9, 2024
commit 408be79ff6c1e94f9ad9ba7a89644ddb1c451f05
1 change: 0 additions & 1 deletion runtime/altair/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1815,7 +1815,6 @@ impl pallet_liquidity_pools_gateway::Config for Runtime {
type MaxIncomingMessageSize = MaxIncomingMessageSize;
type Message = pallet_liquidity_pools::Message;
type MessageQueue = LiquidityPoolsGatewayQueue;
type OriginRecovery = LiquidityPoolsAxelarGateway;
type Router = liquidity_pools_gateway_routers::DomainRouter<Runtime>;
type RuntimeEvent = RuntimeEvent;
type RuntimeOrigin = RuntimeOrigin;
Expand Down
1 change: 0 additions & 1 deletion runtime/centrifuge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1911,7 +1911,6 @@ impl pallet_liquidity_pools_gateway::Config for Runtime {
type MaxIncomingMessageSize = MaxIncomingMessageSize;
type Message = pallet_liquidity_pools::Message;
type MessageQueue = LiquidityPoolsGatewayQueue;
type OriginRecovery = LiquidityPoolsAxelarGateway;
type Router = liquidity_pools_gateway_routers::DomainRouter<Runtime>;
type RuntimeEvent = RuntimeEvent;
type RuntimeOrigin = RuntimeOrigin;
Expand Down
4 changes: 1 addition & 3 deletions runtime/centrifuge/src/xcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use runtime_common::{
transfer_filter::PreXcmTransfer,
xcm::{
AccountIdToLocation, Barrier, CanonicalNativePerSecond, FixedConversionRateProvider,
LocalOriginToLocation, LpInstanceRelayer, ToTreasury,
LocalOriginToLocation, ToTreasury,
},
};
use sp_core::ConstU32;
Expand Down Expand Up @@ -186,8 +186,6 @@ impl TryConvert<cfg_types::ParaId, EVMChainId> for ParaToEvm {
/// `Transact`. There is an `OriginKind` which can biases the kind of local
/// `Origin` it will become.
pub type XcmOriginToTransactDispatchOrigin = (
// A matcher that catches all Moonbeam relaying contracts to generate the right Origin
LpInstanceRelayer<ParaToEvm, Runtime>,
// Sovereign account converter; this attempts to derive an `AccountId` from the origin location
// using `LocationToAccountId` and then turn that into the usual `Signed` origin. Useful for
// foreign chains who want to have a local sovereign account on this chain which they control.
Expand Down
331 changes: 3 additions & 328 deletions runtime/common/src/xcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
use cfg_primitives::types::{AccountId, Balance};
use cfg_traits::TryConvert;
use cfg_types::{
domain_address::{Domain, DomainAddress},
tokens::{CrossChainTransferability, CurrencyId, CustomMetadata},
EVMChainId, ParaId,
};
use cfg_types::tokens::{CrossChainTransferability, CurrencyId, CustomMetadata};
use frame_support::traits::{fungibles::Mutate, Everything, Get};
use frame_system::pallet_prelude::BlockNumberFor;
use orml_traits::asset_registry::Inspect;
Expand All @@ -26,8 +21,8 @@ use sp_std::marker::PhantomData;
use staging_xcm::v4::{
Asset, AssetId,
Fungibility::Fungible,
Junction::{AccountId32, AccountKey20, GeneralKey, Parachain},
Location, NetworkId, OriginKind,
Junction::{AccountId32, GeneralKey, Parachain},
Location, NetworkId,
};
use staging_xcm_builder::{
AccountId32Aliases, AllowKnownQueryResponses, AllowSubscriptionsFrom,
Expand Down Expand Up @@ -95,42 +90,6 @@ impl<AccountId: Into<[u8; 32]>> Convert<AccountId, Location> for AccountIdToLoca
}
}

pub struct LpInstanceRelayer<ParaAsEvmChain, Runtime>(PhantomData<(ParaAsEvmChain, Runtime)>);
impl<ParaAsEvmChain, Runtime>
staging_xcm_executor::traits::ConvertOrigin<<Runtime as frame_system::Config>::RuntimeOrigin>
for LpInstanceRelayer<ParaAsEvmChain, Runtime>
where
ParaAsEvmChain: TryConvert<ParaId, EVMChainId>,
Runtime: pallet_liquidity_pools_gateway::Config,
<Runtime as frame_system::Config>::RuntimeOrigin:
From<pallet_liquidity_pools_gateway::GatewayOrigin>,
{
fn convert_origin(
origin: impl Into<Location>,
kind: OriginKind,
) -> Result<<Runtime as frame_system::Config>::RuntimeOrigin, Location> {
let location = origin.into();
match (kind, location.clone().unpack()) {
(OriginKind::SovereignAccount, (1, [Parachain(para), AccountKey20 { key, .. }])) => {
let evm_id = ParaAsEvmChain::try_convert(*para).map_err(|_| location.clone())?;
let domain_address = DomainAddress::EVM(evm_id, *key);

pallet_liquidity_pools_gateway::Pallet::<Runtime>::relayer(
Domain::EVM(evm_id),
&domain_address,
)
.ok_or(location.clone())?;

Ok(
pallet_liquidity_pools_gateway::GatewayOrigin::AxelarRelay(domain_address)
.into(),
)
}
_ => Err(location),
}
}
}

/// No local origins on this chain are allowed to dispatch XCM sends/executions.
pub type LocalOriginToLocation<R> = SignedToAccountId32<
<R as frame_system::Config>::RuntimeOrigin,
Expand Down Expand Up @@ -269,287 +228,3 @@ pub type LocationToAccountId<RelayNetwork> = (
// Generate remote accounts according to polkadot standards
HashedDescription<AccountId, DescribeFamily<DescribeAllTerminal>>,
);

#[cfg(test)]
mod test {
use cfg_mocks::{
pallet_mock_liquidity_pools, pallet_mock_liquidity_pools_gateway_queue,
pallet_mock_routers, RouterMock,
};
use frame_support::{assert_ok, derive_impl, traits::EnsureOrigin};
use frame_system::EnsureRoot;
use pallet_liquidity_pools::Message;
use pallet_liquidity_pools_gateway::{message::GatewayMessage, EnsureLocal, GatewayOrigin};
use sp_core::{ConstU32, ConstU64};
use sp_runtime::DispatchError;
use staging_xcm_executor::traits::ConvertOrigin;

use super::*;

type AccountId = u64;

// For testing the pallet, we construct a mock runtime.
frame_support::construct_runtime!(
pub enum Runtime {
System: frame_system,
Gateway: pallet_liquidity_pools_gateway,
MockLP: pallet_mock_liquidity_pools,
MockLiquidityPoolsGatewayQueue: pallet_mock_liquidity_pools_gateway_queue,
MockParaAsEvmChain: cfg_mocks::converter::pallet::<Instance1>,
MockOriginRecovery: cfg_mocks::converter::pallet::<Instance2>,
}
);

#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)]
impl frame_system::Config for Runtime {
type Block = frame_system::mocking::MockBlock<Runtime>;
}

impl cfg_mocks::converter::pallet::Config<cfg_mocks::converter::pallet::Instance1> for Runtime {
type From = ParaId;
type To = EVMChainId;
}

impl cfg_mocks::converter::pallet::Config<cfg_mocks::converter::pallet::Instance2> for Runtime {
type From = (Vec<u8>, Vec<u8>);
type To = DomainAddress;
}

impl pallet_mock_liquidity_pools::Config for Runtime {
type DomainAddress = DomainAddress;
type Message = Message;
}

impl pallet_mock_routers::Config for Runtime {}

impl pallet_mock_liquidity_pools_gateway_queue::Config for Runtime {
type Message = GatewayMessage<AccountId, Message>;
}

impl pallet_liquidity_pools_gateway::Config for Runtime {
type AdminOrigin = EnsureRoot<AccountId>;
type InboundMessageHandler = MockLP;
type LocalEVMOrigin = pallet_liquidity_pools_gateway::EnsureLocal;
type MaxIncomingMessageSize = ConstU32<1024>;
type Message = Message;
type MessageQueue = MockLiquidityPoolsGatewayQueue;
type OriginRecovery = MockOriginRecovery;
type Router = RouterMock<Runtime>;
type RuntimeEvent = RuntimeEvent;
type RuntimeOrigin = RuntimeOrigin;
type Sender = ConstU64<11>;
type WeightInfo = ();
}

const RELAYER_PARA_ID: u32 = 1000;
const RELAYER_EVM_ID: u64 = 1001;
const RELAYER_ADDRESS: [u8; 20] = [1u8; 20];

#[test]
fn lp_instance_relayer_converts_correctly() {
System::externalities().execute_with(|| {
let expected_address = DomainAddress::EVM(RELAYER_EVM_ID, RELAYER_ADDRESS);

assert_ok!(Gateway::add_relayer(
RuntimeOrigin::root(),
expected_address.clone(),
));

MockParaAsEvmChain::mock_try_convert(|from| {
assert_eq!(from, RELAYER_PARA_ID);
Ok(RELAYER_EVM_ID)
});

let location = Location::new(
1,
[
Parachain(RELAYER_PARA_ID),
AccountKey20 {
network: None,
key: RELAYER_ADDRESS,
},
],
);

let origin = LpInstanceRelayer::<MockParaAsEvmChain, Runtime>::convert_origin(
location,
OriginKind::SovereignAccount,
)
.expect("Origin conversion failed unexpectedly.");

assert_eq!(
EnsureLocal::ensure_origin(origin).expect("Generate origin must be GatewayOrigin"),
GatewayOrigin::AxelarRelay(expected_address)
)
})
}

#[test]
fn lp_instance_relayer_fails_with_wrong_location() {
System::externalities().execute_with(|| {
let expected_address = DomainAddress::EVM(RELAYER_EVM_ID, RELAYER_ADDRESS);

assert_ok!(Gateway::add_relayer(
RuntimeOrigin::root(),
expected_address.clone(),
));

MockParaAsEvmChain::mock_try_convert(|from| {
assert_eq!(from, RELAYER_PARA_ID);
Ok(RELAYER_EVM_ID)
});

let location = Location::new(1, Parachain(RELAYER_PARA_ID));

assert_eq!(
LpInstanceRelayer::<MockParaAsEvmChain, Runtime>::convert_origin(
location.clone(),
OriginKind::SovereignAccount,
)
.unwrap_err(),
location
);
})
}

#[test]
fn lp_instance_relayer_fails_if_relayer_not_set() {
System::externalities().execute_with(|| {
MockParaAsEvmChain::mock_try_convert(|from| {
assert_eq!(from, RELAYER_PARA_ID);
Ok(RELAYER_EVM_ID)
});

let location = Location::new(
1,
[
Parachain(RELAYER_PARA_ID),
AccountKey20 {
network: None,
key: RELAYER_ADDRESS,
},
],
);

assert_eq!(
LpInstanceRelayer::<MockParaAsEvmChain, Runtime>::convert_origin(
location.clone(),
OriginKind::SovereignAccount,
)
.unwrap_err(),
location
);
})
}

#[test]
fn lp_instance_relayer_fails_if_para_to_evm_fails() {
System::externalities().execute_with(|| {
let expected_address = DomainAddress::EVM(RELAYER_EVM_ID, RELAYER_ADDRESS);

assert_ok!(Gateway::add_relayer(
RuntimeOrigin::root(),
expected_address.clone(),
));

MockParaAsEvmChain::mock_try_convert(|from| {
assert_eq!(from, RELAYER_PARA_ID);
Err(DispatchError::Other(""))
});

let location = Location::new(
1,
[
Parachain(RELAYER_PARA_ID),
AccountKey20 {
network: None,
key: RELAYER_ADDRESS,
},
],
);

assert_eq!(
LpInstanceRelayer::<MockParaAsEvmChain, Runtime>::convert_origin(
location.clone(),
OriginKind::SovereignAccount,
)
.unwrap_err(),
location
);
})
}

#[test]
fn lp_instance_relayer_fails_if_wrong_para() {
System::externalities().execute_with(|| {
let expected_address = DomainAddress::EVM(RELAYER_EVM_ID, RELAYER_ADDRESS);

assert_ok!(Gateway::add_relayer(
RuntimeOrigin::root(),
expected_address.clone(),
));

MockParaAsEvmChain::mock_try_convert(|from| {
assert_eq!(from, 1);
Err(DispatchError::Other(""))
});

let location = Location::new(
1,
[
Parachain(1),
AccountKey20 {
network: None,
key: RELAYER_ADDRESS,
},
],
);

assert_eq!(
LpInstanceRelayer::<MockParaAsEvmChain, Runtime>::convert_origin(
location.clone(),
OriginKind::SovereignAccount,
)
.unwrap_err(),
location
);
})
}

#[test]
fn lp_instance_relayer_fails_if_wrong_address() {
System::externalities().execute_with(|| {
let expected_address = DomainAddress::EVM(RELAYER_EVM_ID, RELAYER_ADDRESS);

assert_ok!(Gateway::add_relayer(
RuntimeOrigin::root(),
expected_address.clone(),
));

MockParaAsEvmChain::mock_try_convert(|from| {
assert_eq!(from, RELAYER_PARA_ID);
Ok(RELAYER_EVM_ID)
});

let location = Location::new(
1,
[
Parachain(RELAYER_PARA_ID),
AccountKey20 {
network: None,
key: [0u8; 20],
},
],
);

assert_eq!(
LpInstanceRelayer::<MockParaAsEvmChain, Runtime>::convert_origin(
location.clone(),
OriginKind::SovereignAccount,
)
.unwrap_err(),
location
);
})
}
}
1 change: 0 additions & 1 deletion runtime/development/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1917,7 +1917,6 @@ impl pallet_liquidity_pools_gateway::Config for Runtime {
type MaxIncomingMessageSize = MaxIncomingMessageSize;
type Message = pallet_liquidity_pools::Message;
type MessageQueue = LiquidityPoolsGatewayQueue;
type OriginRecovery = LiquidityPoolsAxelarGateway;
type Router = liquidity_pools_gateway_routers::DomainRouter<Runtime>;
type RuntimeEvent = RuntimeEvent;
type RuntimeOrigin = RuntimeOrigin;
Expand Down
Loading
Loading