Skip to content

Commit

Permalink
Fix compile with benchmark feature (paritytech#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
yrong authored Feb 25, 2025
1 parent 88ee535 commit 889bd86
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
24 changes: 14 additions & 10 deletions bridges/snowbridge/pallets/inbound-queue-v2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ use snowbridge_inbound_queue_primitives::{
EventProof, VerificationError, Verifier,
};
use sp_core::H160;
use sp_runtime::traits::TryConvert;
use sp_std::prelude::*;
use xcm::prelude::{ExecuteXcm, Junction::*, Location, SendXcm, *};

use bp_relayers::RewardLedger;
Expand All @@ -75,7 +77,6 @@ pub mod pallet {

use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use sp_std::prelude::*;

#[pallet::pallet]
pub struct Pallet<T>(_);
Expand Down Expand Up @@ -118,6 +119,8 @@ pub mod pallet {
/// Convert a weight value into deductible balance type.
type WeightToFee: WeightToFee<Balance = BalanceOf<Self>>;
type Token: Mutate<Self::AccountId> + Inspect<Self::AccountId>;
/// AccountId to Location converter
type AccountToLocation: for<'a> TryConvert<&'a Self::AccountId, Location>;
}

#[pallet::event]
Expand Down Expand Up @@ -203,10 +206,7 @@ pub mod pallet {
pub type OperatingMode<T: Config> = StorageValue<_, BasicOperatingMode, ValueQuery>;

#[pallet::call]
impl<T: Config> Pallet<T>
where
T::AccountId: Into<Location>,
{
impl<T: Config> Pallet<T> {
/// Submit an inbound message originating from the Gateway contract on Ethereum
#[pallet::call_index(0)]
#[pallet::weight(T::WeightInfo::submit())]
Expand Down Expand Up @@ -239,10 +239,7 @@ pub mod pallet {
}
}

impl<T: Config> Pallet<T>
where
T::AccountId: Into<Location>,
{
impl<T: Config> Pallet<T> {
pub fn process_message(relayer: T::AccountId, message: Message) -> DispatchResult {
// Verify that the message was submitted from the known Gateway contract
ensure!(T::GatewayAddress::get() == message.gateway, Error::<T>::InvalidGateway);
Expand Down Expand Up @@ -282,6 +279,13 @@ pub mod pallet {
xcm: Xcm<()>,
) -> Result<XcmHash, SendError> {
let (ticket, fee) = validate_send::<T::XcmSender>(dest, xcm)?;
let fee_payer = T::AccountToLocation::try_convert(&fee_payer).map_err(|err| {
log::error!(
target: LOG_TARGET,
"Failed to convert account to XCM location: {err:?}",
);
SendError::NotApplicable
})?;
T::XcmExecutor::charge_fees(fee_payer.clone(), fee.clone()).map_err(|error| {
tracing::error!(
target: LOG_TARGET,
Expand All @@ -290,7 +294,7 @@ pub mod pallet {
);
SendError::Fees
})?;
Self::deposit_event(Event::FeesPaid { paying: fee_payer.into(), fees: fee });
Self::deposit_event(Event::FeesPaid { paying: fee_payer, fees: fee });
T::XcmSender::deliver(ticket)
}
}
Expand Down
27 changes: 26 additions & 1 deletion bridges/snowbridge/pallets/inbound-queue-v2/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use sp_runtime::{
traits::{IdentifyAccount, IdentityLookup, MaybeEquivalence, Verify},
BuildStorage, MultiSignature,
};
use sp_std::{convert::From, default::Default};
use sp_std::{convert::From, default::Default, marker::PhantomData};
use xcm::{latest::SendXcm, opaque::latest::WESTEND_GENESIS_HASH, prelude::*};
type Block = frame_system::mocking::MockBlock<Test>;

Expand Down Expand Up @@ -170,6 +170,15 @@ impl MaybeEquivalence<TokenId, Location> for MockTokenIdConvert {
}
}

pub struct MockAccountLocationConverter<AccountId>(PhantomData<AccountId>);
impl<'a, AccountId: Clone + Clone> TryConvert<&'a AccountId, Location>
for MockAccountLocationConverter<AccountId>
{
fn try_convert(_who: &AccountId) -> Result<Location, &AccountId> {
Ok(Location::here())
}
}

parameter_types! {
pub const EthereumNetwork: xcm::v5::NetworkId = xcm::v5::NetworkId::Ethereum { chain_id: 11155111 };
pub const GatewayAddress: H160 = H160(GATEWAY_ADDRESS);
Expand Down Expand Up @@ -204,6 +213,7 @@ impl inbound_queue_v2::Config for Test {
type WeightInfo = ();
type WeightToFee = IdentityFee<u128>;
type Token = Balances;
type AccountToLocation = MockAccountLocationConverter<AccountId>;
}

pub fn setup() {
Expand Down Expand Up @@ -345,6 +355,7 @@ pub mod mock_xcm_send_failure {
type WeightInfo = ();
type WeightToFee = IdentityFee<u128>;
type Token = Balances;
type AccountToLocation = MockAccountLocationConverter<AccountId>;
}

impl snowbridge_pallet_ethereum_client::Config for TestXcmSendFailure {
Expand Down Expand Up @@ -390,6 +401,12 @@ pub mod mock_xcm_send_failure {
pub mod mock_xcm_validate_failure {
use super::*;

#[cfg(feature = "runtime-benchmarks")]
impl<T: snowbridge_pallet_ethereum_client::Config> BenchmarkHelper<T> for Test {
// not implemented since the MockVerifier is used for tests
fn initialize_storage(_: BeaconHeader, _: H256) {}
}

frame_support::construct_runtime!(
pub enum Test
{
Expand Down Expand Up @@ -438,6 +455,7 @@ pub mod mock_xcm_validate_failure {
type WeightInfo = ();
type WeightToFee = IdentityFee<u128>;
type Token = Balances;
type AccountToLocation = MockAccountLocationConverter<AccountId>;
}

impl snowbridge_pallet_ethereum_client::Config for Test {
Expand Down Expand Up @@ -475,6 +493,12 @@ pub mod mock_xcm_validate_failure {
pub mod mock_charge_fees_failure {
use super::*;

#[cfg(feature = "runtime-benchmarks")]
impl<T: snowbridge_pallet_ethereum_client::Config> BenchmarkHelper<T> for Test {
// not implemented since the MockVerifier is used for tests
fn initialize_storage(_: BeaconHeader, _: H256) {}
}

frame_support::construct_runtime!(
pub enum Test
{
Expand Down Expand Up @@ -523,6 +547,7 @@ pub mod mock_charge_fees_failure {
type WeightInfo = ();
type WeightToFee = IdentityFee<u128>;
type Token = Balances;
type AccountToLocation = MockAccountLocationConverter<AccountId>;
}

impl snowbridge_pallet_ethereum_client::Config for Test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,11 @@ impl snowbridge_pallet_inbound_queue_v2::Config for Runtime {
EthereumUniversalLocation,
AssetHubFromEthereum,
>;
type AccountToLocation = xcm_builder::AliasesIntoAccountId32<
xcm_config::RelayNetwork,
<Runtime as frame_system::Config>::AccountId,
>;
type RewardKind = BridgeReward;

type DefaultRewardKind = DefaultMyRewardKind;
type RewardPayment = BridgeRelayers;
}
Expand Down

0 comments on commit 889bd86

Please # to comment.