Skip to content

Commit

Permalink
Merge pull request #2841 from subspace/xdm_fee_model
Browse files Browse the repository at this point in the history
XDM: use same fee model for channels
  • Loading branch information
vedhavyas authored Jun 13, 2024
2 parents 1a3be3b + 33bcc21 commit a4e8f68
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 87 deletions.
5 changes: 4 additions & 1 deletion crates/subspace-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ use sp_domains_fraud_proof::storage_proof::{
};
use sp_messenger::endpoint::{Endpoint, EndpointHandler as EndpointHandlerT, EndpointId};
use sp_messenger::messages::{
BlockMessagesWithStorageKey, ChainId, CrossDomainMessage, MessageId, MessageKey,
BlockMessagesWithStorageKey, ChainId, CrossDomainMessage, FeeModel, MessageId, MessageKey,
};
use sp_messenger_host_functions::{get_storage_key, StorageKeyRequest};
use sp_mmr_primitives::EncodableOpaqueLeaf;
Expand Down Expand Up @@ -516,6 +516,8 @@ parameter_types! {
// TODO: update value
pub const ChannelReserveFee: Balance = 100 * SSC;
pub const ChannelInitReservePortion: Perbill = Perbill::from_percent(20);
// TODO update the fee model
pub const ChannelFeeModel: FeeModel<Balance> = FeeModel{relay_fee: SSC};
}

pub struct DomainRegistration;
Expand Down Expand Up @@ -549,6 +551,7 @@ impl pallet_messenger::Config for Runtime {
type ChannelReserveFee = ChannelReserveFee;
type ChannelInitReservePortion = ChannelInitReservePortion;
type DomainRegistration = DomainRegistration;
type ChannelFeeModel = ChannelFeeModel;
}

impl<C> frame_system::offchain::SendTransactionTypes<C> for Runtime
Expand Down
6 changes: 2 additions & 4 deletions domains/client/domain-operator/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use sp_domains_fraud_proof::fraud_proof::{
InvalidExtrinsicsRootProof, InvalidTransfersProof,
};
use sp_domains_fraud_proof::InvalidTransactionCode;
use sp_messenger::messages::{CrossDomainMessage, FeeModel, InitiateChannelParams, Proof};
use sp_messenger::messages::{CrossDomainMessage, Proof};
use sp_mmr_primitives::{EncodableOpaqueLeaf, Proof as MmrProof};
use sp_runtime::generic::{BlockId, DigestItem};
use sp_runtime::traits::{
Expand Down Expand Up @@ -3474,14 +3474,12 @@ async fn test_cross_domains_messages_should_work() {
produce_blocks!(ferdie, alice, 1).await.unwrap();

// Open channel between the Consensus chain and EVM domains
let fee_model = FeeModel { relay_fee: 1 };
alice
.construct_and_send_extrinsic(evm_domain_test_runtime::RuntimeCall::Messenger(
pallet_messenger::Call::initiate_channel {
dst_chain_id: ChainId::Consensus,
params: InitiateChannelParams {
params: pallet_messenger::InitiateChannelParams {
max_outgoing_messages: 100,
fee_model,
},
},
))
Expand Down
12 changes: 7 additions & 5 deletions domains/pallets/messenger/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use frame_support::traits::Get;
use frame_system::RawOrigin;
use sp_messenger::endpoint::{Endpoint, EndpointRequest};
use sp_messenger::messages::{
CrossDomainMessage, InitiateChannelParams, Message, MessageWeightTag, Payload, Proof,
ChannelOpenParams, CrossDomainMessage, Message, MessageWeightTag, Payload, Proof,
RequestResponse, VersionedPayload,
};
use sp_mmr_primitives::{EncodableOpaqueLeaf, Proof as MmrProof};
Expand All @@ -32,7 +32,9 @@ mod benchmarks {
fn initiate_channel() {
let dst_chain_id: ChainId = u32::MAX.into();
assert_ne!(T::SelfChainId::get(), dst_chain_id);
let channel_params = dummy_channel_params::<T>();
let channel_params = InitiateChannelParams {
max_outgoing_messages: 100,
};
let channel_id = NextChannelId::<T>::get(dst_chain_id);
let account = account("account", 0, 0);
T::Currency::set_balance(
Expand Down Expand Up @@ -232,19 +234,19 @@ mod benchmarks {
);
}

fn dummy_channel_params<T: Config>() -> InitiateChannelParams<BalanceOf<T>> {
fn dummy_channel_params<T: Config>() -> ChannelOpenParams<BalanceOf<T>> {
let fee_model = FeeModel {
relay_fee: 1u32.into(),
};
InitiateChannelParams {
ChannelOpenParams {
max_outgoing_messages: 100,
fee_model,
}
}

fn open_channel<T: Config>(
dst_chain_id: ChainId,
params: InitiateChannelParams<BalanceOf<T>>,
params: ChannelOpenParams<BalanceOf<T>>,
) -> ChannelId {
let channel_id = NextChannelId::<T>::get(dst_chain_id);
let list = BTreeSet::from([dst_chain_id]);
Expand Down
34 changes: 25 additions & 9 deletions domains/pallets/messenger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ pub(crate) enum CloseChannelBy<AccountId> {
Sudo,
}

/// Parameters for a new channel between two chains.
#[derive(Default, Debug, Encode, Decode, Clone, Eq, PartialEq, TypeInfo, Copy)]
pub struct InitiateChannelParams {
pub max_outgoing_messages: u32,
}

/// Hold identifier trait for messenger specific balance holds
pub trait HoldIdentifier<T: Config> {
fn messenger_channel(dst_chain_id: ChainId, channel_id: ChannelId) -> FungibleHoldId<T>;
Expand All @@ -135,8 +141,8 @@ mod pallet {
use crate::weights::WeightInfo;
use crate::{
BalanceOf, ChainAllowlistUpdate, Channel, ChannelId, ChannelState, CloseChannelBy,
FeeModel, HoldIdentifier, Nonce, OutboxMessageResult, StateRootOf, ValidatedRelayMessage,
U256,
FeeModel, HoldIdentifier, InitiateChannelParams, Nonce, OutboxMessageResult, StateRootOf,
ValidatedRelayMessage, U256,
};
#[cfg(not(feature = "std"))]
use alloc::boxed::Box;
Expand All @@ -155,7 +161,7 @@ mod pallet {
use sp_domains::{DomainAllowlistUpdates, DomainId, DomainOwner};
use sp_messenger::endpoint::{Endpoint, EndpointHandler, EndpointRequest, Sender};
use sp_messenger::messages::{
ChainId, CrossDomainMessage, InitiateChannelParams, Message, MessageId, MessageKey,
ChainId, ChannelOpenParams, CrossDomainMessage, Message, MessageId, MessageKey,
MessageWeightTag, Payload, ProtocolMessageRequest, RequestResponse, VersionedPayload,
};
use sp_messenger::{
Expand Down Expand Up @@ -208,6 +214,8 @@ mod pallet {
type ChannelInitReservePortion: Get<Perbill>;
/// Type to check if a given domain is registered on Consensus chain.
type DomainRegistration: DomainRegistration;
/// Channels fee model
type ChannelFeeModel: Get<FeeModel<BalanceOf<Self>>>;
}

/// Pallet messenger used to communicate between chains and other blockchains.
Expand Down Expand Up @@ -564,13 +572,21 @@ mod pallet {
pub fn initiate_channel(
origin: OriginFor<T>,
dst_chain_id: ChainId,
params: InitiateChannelParams<BalanceOf<T>>,
params: InitiateChannelParams,
) -> DispatchResult {
let owner = ensure_signed(origin)?;
let channel_open_params = ChannelOpenParams {
max_outgoing_messages: params.max_outgoing_messages,
fee_model: T::ChannelFeeModel::get(),
};

// initiate the channel config
let channel_id =
Self::do_init_channel(dst_chain_id, params, Some(owner.clone()), true)?;
let channel_id = Self::do_init_channel(
dst_chain_id,
channel_open_params,
Some(owner.clone()),
true,
)?;

// reserve channel open fees
let hold_id = T::HoldIdentifier::messenger_channel(dst_chain_id, channel_id);
Expand All @@ -590,7 +606,7 @@ mod pallet {
dst_chain_id,
channel_id,
VersionedPayload::V0(Payload::Protocol(RequestResponse::Request(
ProtocolMessageRequest::ChannelOpen(params),
ProtocolMessageRequest::ChannelOpen(channel_open_params),
))),
)?;

Expand Down Expand Up @@ -862,7 +878,7 @@ mod pallet {
let fee_model = FeeModel {
relay_fee: Default::default(),
};
let init_params = InitiateChannelParams {
let init_params = ChannelOpenParams {
max_outgoing_messages: 100,
fee_model,
};
Expand Down Expand Up @@ -992,7 +1008,7 @@ mod pallet {

pub(crate) fn do_init_channel(
dst_chain_id: ChainId,
init_params: InitiateChannelParams<BalanceOf<T>>,
init_params: ChannelOpenParams<BalanceOf<T>>,
maybe_owner: Option<T::AccountId>,
check_allowlist: bool,
) -> Result<ChannelId, DispatchError> {
Expand Down
4 changes: 3 additions & 1 deletion domains/pallets/messenger/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ macro_rules! impl_runtime {
use pallet_balances::AccountData;
use sp_core::H256;
use sp_messenger::endpoint::{Endpoint, EndpointHandler, EndpointId};
use sp_messenger::messages::ChainId;
use sp_messenger::messages::{ChainId, FeeModel};
use sp_runtime::traits::Convert;
use sp_runtime::BuildStorage;
use crate::HoldIdentifier;
Expand Down Expand Up @@ -62,6 +62,7 @@ macro_rules! impl_runtime {
pub SelfChainId: ChainId = $chain_id.into();
pub const ChannelReserveFee: Balance = 10;
pub const ChannelInitReservePortion: Perbill = Perbill::from_percent(20);
pub const ChannelFeeModel: FeeModel<Balance> = FeeModel{relay_fee: 1};
}

#[derive(
Expand Down Expand Up @@ -103,6 +104,7 @@ macro_rules! impl_runtime {
type ChannelInitReservePortion = ChannelInitReservePortion;
type HoldIdentifier = MockHoldIdentifer;
type DomainRegistration = DomainRegistration;
type ChannelFeeModel = ChannelFeeModel;
/// function to fetch endpoint response handler by Endpoint.
fn get_endpoint_handler(
#[allow(unused_variables)] endpoint: &Endpoint,
Expand Down
Loading

0 comments on commit a4e8f68

Please sign in to comment.