Skip to content

Commit

Permalink
Remove Fee for V2
Browse files Browse the repository at this point in the history
  • Loading branch information
yrong committed Nov 14, 2024
1 parent 9847bc9 commit fb3b30c
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 76 deletions.
2 changes: 1 addition & 1 deletion bridges/snowbridge/pallets/outbound-queue-v2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ use frame_support::{
pub use pallet::*;
use snowbridge_core::{
inbound::{Message as DeliveryMessage, VerificationError, Verifier},
outbound::v2::{CommandWrapper, Fee, GasMeter, InboundMessage, InboundMessageWrapper, Message},
outbound::v2::{CommandWrapper, GasMeter, InboundMessage, InboundMessageWrapper, Message},
BasicOperatingMode, RewardLedger, TokenId,
};
use snowbridge_merkle_tree::merkle_root;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ where
{
type Ticket = Message;

fn validate(
message: &Message,
) -> Result<(Self::Ticket, Fee<<Self as SendMessageFeeProvider>::Balance>), SendError> {
type Balance = T::Balance;

fn validate(message: &Message) -> Result<(Self::Ticket, Self::Balance), SendError> {
// The inner payload should not be too large
let payload = message.encode();
ensure!(
payload.len() < T::MaxMessagePayloadSize::get() as usize,
SendError::MessageTooLarge
);

let fee = Fee::from(Self::calculate_local_fee());
let fee = Self::calculate_local_fee();

Ok((message.clone(), fee))
}
Expand Down
2 changes: 1 addition & 1 deletion bridges/snowbridge/pallets/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ pub mod pallet {
T::OutboundQueueV2::validate(&message).map_err(|err| Error::<T>::Send(err))?;

let payment = match pays_fee {
PaysFee::Yes(account) | PaysFee::Partial(account) => Some((account, fee.total())),
PaysFee::Yes(account) | PaysFee::Partial(account) => Some((account, fee)),
PaysFee::No => None,
};

Expand Down
31 changes: 29 additions & 2 deletions bridges/snowbridge/pallets/system/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ use xcm_executor::traits::ConvertLocation;

use snowbridge_core::{
gwei, meth,
outbound::{v1::ConstantGasMeter, v2::DefaultOutboundQueue},
outbound::{
v1::ConstantGasMeter,
v2::{Message, SendMessage},
SendError as OutboundSendError, SendMessageFeeProvider,
},
sibling_sovereign_account, AgentId, AllowSiblingsOnly, ParaId, PricingParameters, Rewards,
};
use sp_runtime::{
Expand Down Expand Up @@ -200,6 +204,29 @@ impl BenchmarkHelper<RuntimeOrigin> for () {
}
}

pub struct MockOkOutboundQueue;
impl SendMessage for MockOkOutboundQueue {
type Ticket = ();

type Balance = u128;

fn validate(_: &Message) -> Result<(Self::Ticket, Self::Balance), OutboundSendError> {
Ok(((), 1_u128))
}

fn deliver(_: Self::Ticket) -> Result<H256, OutboundSendError> {
Ok(H256::zero())
}
}

impl SendMessageFeeProvider for MockOkOutboundQueue {
type Balance = u128;

fn local_fee() -> Self::Balance {
1
}
}

impl crate::Config for Test {
type RuntimeEvent = RuntimeEvent;
type OutboundQueue = OutboundQueue;
Expand All @@ -214,7 +241,7 @@ impl crate::Config for Test {
type EthereumLocation = EthereumDestination;
#[cfg(feature = "runtime-benchmarks")]
type Helper = ();
type OutboundQueueV2 = DefaultOutboundQueue;
type OutboundQueueV2 = MockOkOutboundQueue;
}

// Build genesis storage according to the mock runtime.
Expand Down
60 changes: 5 additions & 55 deletions bridges/snowbridge/primitives/core/src/outbound/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
//! # Outbound V2 primitives

use crate::outbound::{OperatingMode, SendError, SendMessageFeeProvider};
use crate::outbound::{OperatingMode, SendError};
use alloy_sol_types::sol;
use codec::{Decode, Encode};
use frame_support::{pallet_prelude::ConstU32, BoundedVec};
Expand Down Expand Up @@ -230,70 +230,20 @@ pub struct Initializer {
pub maximum_required_gas: u64,
}

#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo)]
#[cfg_attr(feature = "std", derive(PartialEq))]
/// Fee for delivering message
pub struct Fee<Balance>
where
Balance: BaseArithmetic + Unsigned + Copy,
{
/// Fee to cover cost of processing the message locally
pub local: Balance,
}

impl<Balance> Fee<Balance>
where
Balance: BaseArithmetic + Unsigned + Copy,
{
pub fn total(&self) -> Balance {
self.local
}
}

impl<Balance> From<Balance> for Fee<Balance>
where
Balance: BaseArithmetic + Unsigned + Copy,
{
fn from(local: Balance) -> Self {
Self { local }
}
}

pub trait SendMessage: SendMessageFeeProvider {
pub trait SendMessage {
type Ticket: Clone + Encode + Decode;

type Balance: BaseArithmetic + Unsigned + Copy;

/// Validate an outbound message and return a tuple:
/// 1. Ticket for submitting the message
/// 2. Delivery fee
fn validate(
message: &Message,
) -> Result<(Self::Ticket, Fee<<Self as SendMessageFeeProvider>::Balance>), SendError>;
fn validate(message: &Message) -> Result<(Self::Ticket, Self::Balance), SendError>;

/// Submit the message ticket for eventual delivery to Ethereum
fn deliver(ticket: Self::Ticket) -> Result<H256, SendError>;
}

pub struct DefaultOutboundQueue;
impl SendMessage for DefaultOutboundQueue {
type Ticket = ();

fn validate(_: &Message) -> Result<(Self::Ticket, Fee<Self::Balance>), SendError> {
Ok(((), Fee { local: Default::default() }))
}

fn deliver(_: Self::Ticket) -> Result<H256, SendError> {
Ok(H256::zero())
}
}

impl SendMessageFeeProvider for DefaultOutboundQueue {
type Balance = u128;

fn local_fee() -> Self::Balance {
Default::default()
}
}

pub trait GasMeter {
/// Measures the maximum amount of gas a command payload will require to *dispatch*, NOT
/// including validation & verification.
Expand Down
12 changes: 8 additions & 4 deletions bridges/snowbridge/primitives/router/src/outbound/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ mod tests {
use frame_support::parameter_types;
use hex_literal::hex;
use snowbridge_core::{
outbound::{v2::Fee, SendError, SendMessageFeeProvider},
outbound::{SendError, SendMessageFeeProvider},
AgentIdOf,
};
use sp_std::default::Default;
Expand All @@ -504,8 +504,10 @@ mod tests {
impl SendMessage for MockOkOutboundQueue {
type Ticket = ();

fn validate(_: &Message) -> Result<(Self::Ticket, Fee<Self::Balance>), SendError> {
Ok(((), Fee { local: 1 }))
type Balance = u128;

fn validate(_: &Message) -> Result<(Self::Ticket, Self::Balance), SendError> {
Ok(((), 1_u128))
}

fn deliver(_: Self::Ticket) -> Result<H256, SendError> {
Expand All @@ -524,7 +526,9 @@ mod tests {
impl SendMessage for MockErrOutboundQueue {
type Ticket = ();

fn validate(_: &Message) -> Result<(Self::Ticket, Fee<Self::Balance>), SendError> {
type Balance = u128;

fn validate(_: &Message) -> Result<(Self::Ticket, Self::Balance), SendError> {
Err(SendError::MessageTooLarge)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use parachains_common::{AccountId, Balance};
use snowbridge_beacon_primitives::{Fork, ForkVersions};
use snowbridge_core::{gwei, meth, AllowSiblingsOnly, PricingParameters, Rewards};
use snowbridge_router_primitives::{inbound::v1::MessageToXcm, outbound::v1::EthereumBlobExporter};
use sp_core::H160;
use sp_core::{H160, H256};
use testnet_parachains_constants::rococo::{
currency::*,
fee::WeightToFee,
Expand All @@ -37,7 +37,10 @@ use crate::xcm_config::RelayNetwork;
use benchmark_helpers::DoNothingRouter;
use frame_support::{parameter_types, weights::ConstantMultiplier};
use pallet_xcm::EnsureXcm;
use snowbridge_core::outbound::v2::DefaultOutboundQueue;
use snowbridge_core::outbound::{
v2::{Message, SendMessage},
SendError, SendMessageFeeProvider,
};
use sp_runtime::{
traits::{ConstU32, ConstU8, Keccak256},
FixedU128,
Expand Down Expand Up @@ -178,6 +181,29 @@ impl snowbridge_pallet_ethereum_client::Config for Runtime {
type WeightInfo = crate::weights::snowbridge_pallet_ethereum_client::WeightInfo<Runtime>;
}

pub struct DefaultOutboundQueue;
impl SendMessage for DefaultOutboundQueue {
type Ticket = ();

type Balance = Balance;

fn validate(_: &Message) -> Result<(Self::Ticket, Self::Balance), SendError> {
Ok(((), Default::default()))
}

fn deliver(_: Self::Ticket) -> Result<H256, SendError> {
Ok(H256::zero())
}
}

impl SendMessageFeeProvider for DefaultOutboundQueue {
type Balance = Balance;

fn local_fee() -> Self::Balance {
Default::default()
}
}

impl snowbridge_pallet_system::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type OutboundQueue = EthereumOutboundQueue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,16 @@ use parachains_common::{
AVERAGE_ON_INITIALIZE_RATIO, NORMAL_DISPATCH_RATIO,
};
use snowbridge_core::{
outbound::v1::{Command, Fee},
outbound::{
v1::{Command, Fee},
v2::InboundMessage,
DryRunError,
},
AgentId, PricingParameters,
};
use testnet_parachains_constants::westend::{consensus::*, currency::*, fee::WeightToFee, time::*};
use xcm::VersionedLocation;

use westend_runtime_constants::system_parachain::{ASSET_HUB_ID, BRIDGE_HUB_ID};

use snowbridge_core::outbound::v2::{Fee as FeeV2, InboundMessage};

use snowbridge_core::outbound::DryRunError;
use xcm::VersionedLocation;

/// The address format for describing accounts.
pub type Address = MultiAddress<AccountId, ()>;
Expand Down

0 comments on commit fb3b30c

Please sign in to comment.