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

Fee handler #363

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
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
32 changes: 32 additions & 0 deletions modules/ismp/pallets/pallet/src/fee.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2024 Polytope Labs.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::{weights::get_weight, Config};
use frame_support::dispatch::{DispatchResultWithPostInfo, Pays, PostDispatchInfo};
use ismp::messaging::Message;

pub trait HandleFee<T: Config> {
fn handle_fee(messages: &[Message], signer: T::AccountId) -> DispatchResultWithPostInfo;
}
Comment on lines +20 to +22
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets add a get_weight method here instead of using the weight router

Copy link
Author

@kku9706 kku9706 Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest leaving weight to benchmark in a separate trait, as the convention FeeHandler focuses on custom fee logic(e.g., rewarding the signer).

Some refactoring accordingly in d03c72b

@seunlanlege Please let me know WDYT.


impl<T: Config> HandleFee<T> for () {
fn handle_fee(messages: &[Message], _signer: T::AccountId) -> DispatchResultWithPostInfo {
//Todo: reward the signer
Ok(PostDispatchInfo {
actual_weight: Some(get_weight::<T>(&messages)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should remove the WeightRouter from the Config trait as that is now replaced by this FeeHandler

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eliminate the WeightRouter entirely in d03c72b

pays_fee: Pays::Yes,
})
}
}
10 changes: 3 additions & 7 deletions modules/ismp/pallets/pallet/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ use crate::{
child_trie::{RequestCommitments, ResponseCommitments},
dispatcher::{FeeMetadata, RequestMetadata},
offchain::{self, ForkIdentifier, Leaf, LeafIndexAndPos, OffchainDBProvider, Proof, ProofKeys},
weights::get_weight,
Config, Error, Event, Pallet, Responded,
};
use alloc::{string::ToString, vec, vec::Vec};
use codec::Decode;
use frame_support::dispatch::{DispatchResultWithPostInfo, Pays, PostDispatchInfo};
use frame_support::dispatch::DispatchResult;
use frame_system::Phase;
use ismp::{
handlers::{handle_incoming_message, MessageResult},
Expand Down Expand Up @@ -80,7 +79,7 @@ impl<T: Config> Pallet<T> {
}

/// Execute the provided ISMP datagrams, this will short circuit if any messages are invalid.
pub fn execute(messages: Vec<Message>) -> DispatchResultWithPostInfo {
pub fn execute(messages: Vec<Message>) -> DispatchResult {
// Define a host
let host = Pallet::<T>::default();
let events = messages
Expand Down Expand Up @@ -116,10 +115,7 @@ impl<T: Config> Pallet<T> {
Pallet::<T>::deposit_event(event.into())
}

Ok(PostDispatchInfo {
actual_weight: Some(get_weight::<T>(&messages)),
pays_fee: Pays::Yes,
})
Ok(())
}

/// Dispatch an outgoing request, returns the request commitment
Expand Down
32 changes: 20 additions & 12 deletions modules/ismp/pallets/pallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ pub mod child_trie;
pub mod dispatcher;
pub mod errors;
pub mod events;
mod fee;
pub mod host;
mod impls;
pub mod offchain;
Expand All @@ -197,12 +198,13 @@ pub mod pallet {
use crate::{
child_trie::{RequestCommitments, ResponseCommitments, CHILD_TRIE_PREFIX},
errors::HandlingError,
fee::HandleFee,
weights::{get_weight, WeightProvider},
};
use codec::{Codec, Encode};
use core::fmt::Debug;
use frame_support::{
dispatch::{DispatchResult, DispatchResultWithPostInfo},
dispatch::{DispatchResult, DispatchResultWithPostInfo, PostDispatchInfo},
pallet_prelude::*,
traits::{fungible::Mutate, tokens::Preservation, Get, UnixTime},
PalletId,
Expand All @@ -220,13 +222,12 @@ pub mod pallet {
router::IsmpRouter,
};
use sp_core::{storage::ChildInfo, H256};
#[cfg(feature = "unsigned")]
use sp_runtime::transaction_validity::{
InvalidTransaction, TransactionSource, TransactionValidity, TransactionValidityError,
ValidTransaction,
};
use sp_runtime::{
traits::{AccountIdConversion, AtLeast32BitUnsigned},
transaction_validity::{
InvalidTransaction, TransactionSource, TransactionValidity, TransactionValidityError,
ValidTransaction,
},
FixedPointOperand,
};
use sp_std::prelude::*;
Expand Down Expand Up @@ -304,6 +305,9 @@ pub mod pallet {
/// This offchain DB is also allowed to "merkelize" and "generate proofs" for messages.
/// Most state machines will likey not need this and can just provide `()`
type OffchainDB: OffchainDBProvider<Leaf = Leaf>;

/// FeeHandler with custom fee logic
type FeeHandler: HandleFee<Self>;
}

// Simple declaration of the `Pallet` type. It is placeholder we use to implement traits and
Expand Down Expand Up @@ -427,7 +431,6 @@ pub mod pallet {
/// - `messages`: the messages to handle or process.
///
/// Emits different message events based on the Message received if successful.
#[cfg(feature = "unsigned")]
#[pallet::weight(get_weight::<T>(&messages))]
#[pallet::call_index(0)]
#[frame_support::transactional]
Expand All @@ -437,7 +440,12 @@ pub mod pallet {
) -> DispatchResultWithPostInfo {
ensure_none(origin)?;

Self::execute(messages)
Self::execute(messages.clone())?;

Ok(PostDispatchInfo {
actual_weight: Some(get_weight::<T>(&messages)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
actual_weight: Some(get_weight::<T>(&messages)),
actual_weight: Some(T::HandleFee::get_weight(&messages)),

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pays_fee: Pays::No,
})
}

/// Execute the provided batch of ISMP messages. This call will short-circuit and revert if
Expand All @@ -448,14 +456,15 @@ pub mod pallet {
/// - `messages`: A set of ISMP [`Message`]s to handle or process.
///
/// Emits different message events based on the Message received if successful.
#[cfg(not(feature = "unsigned"))]
#[pallet::weight(get_weight::<T>(&messages))]
#[pallet::call_index(1)]
#[frame_support::transactional]
pub fn handle(origin: OriginFor<T>, messages: Vec<Message>) -> DispatchResultWithPostInfo {
ensure_signed(origin)?;
let signer = ensure_signed(origin)?;

Self::execute(messages.clone())?;

Self::execute(messages)
T::FeeHandler::handle_fee(messages.as_slice(), signer)
}

/// Create a consensus client, using a subjectively chosen consensus state. This can also
Expand Down Expand Up @@ -644,7 +653,6 @@ pub mod pallet {
}

/// This allows users execute ISMP datagrams for free. Use with caution.
#[cfg(feature = "unsigned")]
#[pallet::validate_unsigned]
impl<T: Config> ValidateUnsigned for Pallet<T> {
type Call = Call<T>;
Expand Down
2 changes: 1 addition & 1 deletion parachain/runtimes/nexus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ parachains-common = { workspace = true }

# ismp modules
ismp = { workspace = true }
pallet-ismp = { workspace = true, features = ["unsigned"] }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's revert this

pallet-ismp = { workspace = true }
pallet-ismp-demo = { workspace = true }
pallet-ismp-runtime-api = { workspace = true }
ismp-parachain = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions parachain/runtimes/nexus/src/ismp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ impl pallet_ismp::Config for Runtime {
);
type OffchainDB = Mmr;
type WeightProvider = ();
type FeeHandler = ();
}

impl pallet_ismp_relayer::Config for Runtime {
Expand Down