Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Make fee asset as configuration parameter of the Channel
Browse files Browse the repository at this point in the history
  • Loading branch information
yrong committed Apr 26, 2024
1 parent 1cab94c commit 5a22972
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 59 deletions.
7 changes: 4 additions & 3 deletions bridges/snowbridge/pallets/inbound-queue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ pub mod pallet {

use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use xcm::VersionedLocation;

#[pallet::pallet]
pub struct Pallet<T>(_);
Expand Down Expand Up @@ -281,7 +282,7 @@ pub mod pallet {

// Convert VersionMessage to XCM
let (xcm, fee) =
Self::do_convert(channel.para_id, envelope.message_id, message.clone())?;
Self::do_convert(channel.fee_asset_id, envelope.message_id, message.clone())?;

log::info!(
target: LOG_TARGET,
Expand Down Expand Up @@ -330,11 +331,11 @@ pub mod pallet {

impl<T: Config> Pallet<T> {
pub fn do_convert(
para_id: ParaId,
fee_asset_id: VersionedLocation,
message_id: H256,
message: VersionedMessage,
) -> Result<(Xcm<()>, BalanceOf<T>), Error<T>> {
let (xcm, fee) = T::MessageConverter::convert(para_id, message_id, message)
let (xcm, fee) = T::MessageConverter::convert(fee_asset_id, message_id, message)
.map_err(|e| Error::<T>::ConvertMessage(e))?;
Ok((xcm, fee))
}
Expand Down
6 changes: 5 additions & 1 deletion bridges/snowbridge/pallets/inbound-queue/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,11 @@ impl StaticLookup for MockChannelLookup {
{
return None
}
Some(Channel { agent_id: H256::zero(), para_id: ASSET_HUB_PARAID.into() })
Some(Channel {
agent_id: H256::zero(),
para_id: ASSET_HUB_PARAID.into(),
fee_asset_id: VersionedLocation::from(Location::here()),
})
}
}

Expand Down
12 changes: 10 additions & 2 deletions bridges/snowbridge/pallets/inbound-queue/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use snowbridge_core::{inbound::Proof, ChannelId};
use sp_keyring::AccountKeyring as Keyring;
use sp_runtime::DispatchError;
use sp_std::convert::From;
use xcm::prelude::{OriginKind, Transact};
use xcm::{
prelude::{OriginKind, Transact},
VersionedLocation,
};

use crate::{Error, Event as InboundQueueEvent};

Expand Down Expand Up @@ -265,7 +268,12 @@ fn test_convert_transact() {
},
});
// Convert the message to XCM
let (xcm, dest_fee) = InboundQueue::do_convert(1000.into(), message_id, message).unwrap();
let (xcm, dest_fee) = InboundQueue::do_convert(
VersionedLocation::from(Location::parent()),
message_id,
message,
)
.unwrap();
let instructions = xcm.into_inner();
assert_eq!(instructions.len(), 7);
assert_eq!(dest_fee, fee.into());
Expand Down
37 changes: 31 additions & 6 deletions bridges/snowbridge/pallets/system/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ mod benchmarks {
SnowbridgeControl::<T>::create_agent(origin.clone())?;

#[extrinsic_call]
_(origin as T::RuntimeOrigin, OperatingMode::Normal);
_(
origin as T::RuntimeOrigin,
OperatingMode::Normal,
VersionedLocation::from(Location::here()),
);

Ok(())
}
Expand All @@ -95,10 +99,18 @@ mod benchmarks {
let origin = T::Helper::make_xcm_origin(origin_location);
fund_sovereign_account::<T>(origin_para_id.into())?;
SnowbridgeControl::<T>::create_agent(origin.clone())?;
SnowbridgeControl::<T>::create_channel(origin.clone(), OperatingMode::Normal)?;
SnowbridgeControl::<T>::create_channel(
origin.clone(),
OperatingMode::Normal,
VersionedLocation::from(Location::here()),
)?;

#[extrinsic_call]
_(origin as T::RuntimeOrigin, OperatingMode::RejectingOutboundMessages);
_(
origin as T::RuntimeOrigin,
OperatingMode::RejectingOutboundMessages,
VersionedLocation::from(Location::here()),
);

Ok(())
}
Expand All @@ -112,10 +124,19 @@ mod benchmarks {

fund_sovereign_account::<T>(origin_para_id.into())?;
SnowbridgeControl::<T>::create_agent(origin.clone())?;
SnowbridgeControl::<T>::create_channel(origin.clone(), OperatingMode::Normal)?;
SnowbridgeControl::<T>::create_channel(
origin.clone(),
OperatingMode::Normal,
VersionedLocation::from(Location::here()),
)?;

#[extrinsic_call]
_(RawOrigin::Root, channel_id, OperatingMode::RejectingOutboundMessages);
_(
RawOrigin::Root,
channel_id,
OperatingMode::RejectingOutboundMessages,
VersionedLocation::from(Location::here()),
);

Ok(())
}
Expand All @@ -127,7 +148,11 @@ mod benchmarks {
let origin = T::Helper::make_xcm_origin(origin_location);
fund_sovereign_account::<T>(origin_para_id.into())?;
SnowbridgeControl::<T>::create_agent(origin.clone())?;
SnowbridgeControl::<T>::create_channel(origin.clone(), OperatingMode::Normal)?;
SnowbridgeControl::<T>::create_channel(
origin.clone(),
OperatingMode::Normal,
VersionedLocation::from(Location::here()),
)?;

#[extrinsic_call]
_(origin as T::RuntimeOrigin, H160::default(), 1);
Expand Down
41 changes: 35 additions & 6 deletions bridges/snowbridge/pallets/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,11 @@ pub mod pallet {
/// - `mode`: Initial operating mode of the channel
#[pallet::call_index(4)]
#[pallet::weight(T::WeightInfo::create_channel())]
pub fn create_channel(origin: OriginFor<T>, mode: OperatingMode) -> DispatchResult {
pub fn create_channel(
origin: OriginFor<T>,
mode: OperatingMode,
fee_asset_id: VersionedLocation,
) -> DispatchResult {
let origin_location: Location = T::SiblingOrigin::ensure_origin(origin)?;

// Ensure that origin location is a sibling parachain
Expand All @@ -396,7 +400,7 @@ pub mod pallet {
ensure!(Agents::<T>::contains_key(agent_id), Error::<T>::NoAgent);
ensure!(!Channels::<T>::contains_key(channel_id), Error::<T>::ChannelAlreadyCreated);

let channel = Channel { agent_id, para_id };
let channel = Channel { agent_id, para_id, fee_asset_id };
Channels::<T>::insert(channel_id, channel);

let command = Command::CreateChannel { channel_id, agent_id, mode };
Expand All @@ -417,7 +421,11 @@ pub mod pallet {
/// - `mode`: Initial operating mode of the channel
#[pallet::call_index(5)]
#[pallet::weight(T::WeightInfo::update_channel())]
pub fn update_channel(origin: OriginFor<T>, mode: OperatingMode) -> DispatchResult {
pub fn update_channel(
origin: OriginFor<T>,
mode: OperatingMode,
fee_asset_id: VersionedLocation,
) -> DispatchResult {
let origin_location: Location = T::SiblingOrigin::ensure_origin(origin)?;

// Ensure that origin location is a sibling parachain
Expand All @@ -427,6 +435,10 @@ pub mod pallet {

ensure!(Channels::<T>::contains_key(channel_id), Error::<T>::NoChannel);

Channels::<T>::mutate_extant(channel_id, |channel| {
channel.fee_asset_id = fee_asset_id;
});

let command = Command::UpdateChannel { channel_id, mode };
let pays_fee = PaysFee::<T>::Partial(sibling_sovereign_account::<T>(para_id));

Expand All @@ -451,11 +463,16 @@ pub mod pallet {
origin: OriginFor<T>,
channel_id: ChannelId,
mode: OperatingMode,
fee_asset_id: VersionedLocation,
) -> DispatchResult {
ensure_root(origin)?;

ensure!(Channels::<T>::contains_key(channel_id), Error::<T>::NoChannel);

Channels::<T>::mutate_extant(channel_id, |channel| {
channel.fee_asset_id = fee_asset_id;
});

let command = Command::UpdateChannel { channel_id, mode };
Self::send(PRIMARY_GOVERNANCE_CHANNEL, command, PaysFee::<T>::No)?;

Expand Down Expand Up @@ -634,7 +651,11 @@ pub mod pallet {
Agents::<T>::insert(asset_hub_agent_id, ());
Channels::<T>::insert(
asset_hub_channel_id,
Channel { agent_id: asset_hub_agent_id, para_id: asset_hub_para_id },
Channel {
agent_id: asset_hub_agent_id,
para_id: asset_hub_para_id,
fee_asset_id: VersionedLocation::from(Location::parent()),
},
);

// Governance channels
Expand All @@ -645,13 +666,21 @@ pub mod pallet {
// Primary governance channel
Channels::<T>::insert(
PRIMARY_GOVERNANCE_CHANNEL,
Channel { agent_id: bridge_hub_agent_id, para_id },
Channel {
agent_id: bridge_hub_agent_id,
para_id,
fee_asset_id: VersionedLocation::from(Location::parent()),
},
);

// Secondary governance channel
Channels::<T>::insert(
SECONDARY_GOVERNANCE_CHANNEL,
Channel { agent_id: bridge_hub_agent_id, para_id },
Channel {
agent_id: bridge_hub_agent_id,
para_id,
fee_asset_id: VersionedLocation::from(Location::parent()),
},
);

Ok(())
Expand Down
Loading

0 comments on commit 5a22972

Please sign in to comment.