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

Send priority fees to collators #3120

Merged
merged 48 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 44 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
ae79201
feat: send eth tips to collator
TarekkMA Dec 27, 2024
ae978b8
style: fix import order
TarekkMA Dec 27, 2024
903a8a8
fix: tests
TarekkMA Dec 30, 2024
35fe6d2
fix: tests
TarekkMA Dec 30, 2024
059436a
style: fix formatting
TarekkMA Dec 30, 2024
1d80e0a
test: fix test
TarekkMA Dec 30, 2024
0835677
test: fix test
TarekkMA Dec 30, 2024
051d628
style: formatting
TarekkMA Dec 30, 2024
da52ecb
fix: test
TarekkMA Dec 30, 2024
1fb8015
fix: test
TarekkMA Dec 30, 2024
a286912
fix: test
TarekkMA Dec 31, 2024
71f0618
fix: parameters tests.
TarekkMA Dec 31, 2024
188676a
fix: contract tests.
TarekkMA Dec 31, 2024
0926425
fix: fix test-eth-pool-resubmit-txn.ts
TarekkMA Jan 3, 2025
12631ff
fix: fix test-eth-pool-error.ts
TarekkMA Jan 3, 2025
794001f
fix: Update balance transfer tests to use CHARLETH_ADDRESS in tests
TarekkMA Jan 6, 2025
bc8d687
style: fix
TarekkMA Jan 6, 2025
c6e887e
fix: Refactor xtokens test and disable tip fee burning
TarekkMA Jan 6, 2025
7ee98f5
fix: fix test-contract tests
TarekkMA Jan 6, 2025
66aff79
fix: commands in update-local-types
TarekkMA Jan 6, 2025
6ca5fce
refactor: fee calculation logic and improve modularity.
TarekkMA Jan 6, 2025
ebb4cf8
fix: FeesTreasuryProportion test logic and helpers
TarekkMA Jan 7, 2025
5b5cb0a
fix: argument order in calculateFeePortions function call
TarekkMA Jan 7, 2025
822c78e
test: Add verification for latest block fees and add a check for tips.
TarekkMA Jan 7, 2025
ccd3df9
refactor: separate fee and tip calculation
TarekkMA Jan 7, 2025
0b3adaf
fix: fee and tip handling logic for clarity and correctness
TarekkMA Jan 7, 2025
f7c64c9
refactor: clean up unused imports in Moonbeam runtime
TarekkMA Jan 7, 2025
8d71ba8
style: correct indentation in runtime lib.rs
TarekkMA Jan 7, 2025
0b11025
test: also check LatestBlockFees in eth transactions
TarekkMA Jan 8, 2025
42e0a69
tmp: check if all tests pass
TarekkMA Jan 8, 2025
6a0346b
refactor: remove unnecessary console logs in tests
TarekkMA Jan 8, 2025
7b16ab2
tmp: change default value
TarekkMA Jan 9, 2025
62301d1
Revert "tmp: change default value"
TarekkMA Jan 9, 2025
fdab76f
tmp: change default value
TarekkMA Jan 9, 2025
56b62d3
tmp: change default value
TarekkMA Jan 9, 2025
df28273
test: add comment explaining test file
TarekkMA Jan 9, 2025
fabaa99
Revert "tmp: change default value"
TarekkMA Jan 9, 2025
c41ed41
Revert "tmp: change default value"
TarekkMA Jan 9, 2025
e3b5c3d
feature: send substrate tips to block author
TarekkMA Jan 9, 2025
f623e04
fix: incorrect generics
TarekkMA Jan 9, 2025
4392ec1
style: fix formatting
TarekkMA Jan 9, 2025
213b022
test: refine fee and tip handling logic for runtime tests
TarekkMA Jan 9, 2025
58bca06
test(runtime): fix incorrect runtime references in tests
TarekkMA Jan 9, 2025
3fdb11d
refactor: move all deal with fees logic into common crate
TarekkMA Jan 14, 2025
f53feb5
refactor: remove unused imports
TarekkMA Jan 14, 2025
7001e14
style: add copyright
TarekkMA Jan 14, 2025
c199943
Update runtime/common/src/deal_with_fees.rs
RomarQ Jan 14, 2025
0980453
test: fix test
TarekkMA Jan 14, 2025
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
98 changes: 98 additions & 0 deletions runtime/common/src/deal_with_fees.rs
TarekkMA marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use frame_support::__private::Get;
use frame_support::pallet_prelude::TypedGet;
use frame_support::traits::fungible::Credit;
use frame_support::traits::tokens::imbalance::ResolveTo;
use frame_support::traits::Imbalance;
use frame_support::traits::OnUnbalanced;
use pallet_treasury::TreasuryAccountId;
use sp_runtime::Perbill;

/// Deal with substrate based fees and tip. This should be used with pallet_transaction_payment.
pub struct DealWithSubstrateFeesAndTip<R, FeesTreasuryProportion>(
sp_std::marker::PhantomData<(R, FeesTreasuryProportion)>,
);
impl<R, FeesTreasuryProportion> DealWithSubstrateFeesAndTip<R, FeesTreasuryProportion>
where
R: pallet_balances::Config + pallet_treasury::Config + pallet_author_inherent::Config,
pallet_author_inherent::Pallet<R>: Get<R::AccountId>,
FeesTreasuryProportion: Get<Perbill>,
{
fn deal_with_fees(amount: Credit<R::AccountId, pallet_balances::Pallet<R>>) {
// Balances pallet automatically burns dropped Credits by decreasing
// total_supply accordingly
let treasury_proportion = FeesTreasuryProportion::get();
let treasury_part = treasury_proportion.deconstruct();
let burn_part = Perbill::one().deconstruct() - treasury_part;
let (_, to_treasury) = amount.ration(burn_part, treasury_part);
ResolveTo::<TreasuryAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(to_treasury);
}

fn deal_with_tip(amount: Credit<R::AccountId, pallet_balances::Pallet<R>>) {
ResolveTo::<BlockAuthorAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(amount);
}
}

impl<R, FeesTreasuryProportion> OnUnbalanced<Credit<R::AccountId, pallet_balances::Pallet<R>>>
for DealWithSubstrateFeesAndTip<R, FeesTreasuryProportion>
where
R: pallet_balances::Config + pallet_treasury::Config + pallet_author_inherent::Config,
pallet_author_inherent::Pallet<R>: Get<R::AccountId>,
FeesTreasuryProportion: Get<Perbill>,
{
fn on_unbalanceds(
mut fees_then_tips: impl Iterator<Item = Credit<R::AccountId, pallet_balances::Pallet<R>>>,
) {
if let Some(fees) = fees_then_tips.next() {
Self::deal_with_fees(fees);
if let Some(tip) = fees_then_tips.next() {
Self::deal_with_tip(tip);
}
}
}
}

/// Deal with ethereum based fees. To handle tips/priority fees, use DealWithEthereumPriorityFees.
pub struct DealWithEthereumBaseFees<R, FeesTreasuryProportion>(
sp_std::marker::PhantomData<(R, FeesTreasuryProportion)>,
);
impl<R, FeesTreasuryProportion> OnUnbalanced<Credit<R::AccountId, pallet_balances::Pallet<R>>>
for DealWithEthereumBaseFees<R, FeesTreasuryProportion>
where
R: pallet_balances::Config + pallet_treasury::Config,
FeesTreasuryProportion: Get<Perbill>,
{
fn on_nonzero_unbalanced(amount: Credit<R::AccountId, pallet_balances::Pallet<R>>) {
// Balances pallet automatically burns dropped Credits by decreasing
// total_supply accordingly
let treasury_proportion = FeesTreasuryProportion::get();
let treasury_part = treasury_proportion.deconstruct();
let burn_part = Perbill::one().deconstruct() - treasury_part;
let (_, to_treasury) = amount.ration(burn_part, treasury_part);
ResolveTo::<TreasuryAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(to_treasury);
}
}

pub struct BlockAuthorAccountId<R>(sp_std::marker::PhantomData<R>);
impl<R> TypedGet for BlockAuthorAccountId<R>
where
R: frame_system::Config + pallet_author_inherent::Config,
pallet_author_inherent::Pallet<R>: Get<R::AccountId>,
{
type Type = R::AccountId;
fn get() -> Self::Type {
<pallet_author_inherent::Pallet<R> as Get<R::AccountId>>::get()
}
}

/// Deal with ethereum based priority fees/tips. See DealWithEthereumBaseFees for base fees.
pub struct DealWithEthereumPriorityFees<R>(sp_std::marker::PhantomData<R>);
impl<R> OnUnbalanced<Credit<R::AccountId, pallet_balances::Pallet<R>>>
for DealWithEthereumPriorityFees<R>
where
R: pallet_balances::Config + pallet_author_inherent::Config,
pallet_author_inherent::Pallet<R>: Get<R::AccountId>,
{
fn on_nonzero_unbalanced(amount: Credit<R::AccountId, pallet_balances::Pallet<R>>) {
ResolveTo::<BlockAuthorAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(amount);
}
}
14 changes: 9 additions & 5 deletions runtime/common/src/impl_on_charge_evm_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ macro_rules! impl_on_charge_evm_transaction {
type BalanceFor<T> =
<<T as pallet_evm::Config>::Currency as Inspect<FungibleAccountId<T>>>::Balance;

pub struct OnChargeEVMTransaction<OU>(sp_std::marker::PhantomData<OU>);
pub struct OnChargeEVMTransaction<BaseFeesOU, PriorityFeesOU>(
sp_std::marker::PhantomData<(BaseFeesOU, PriorityFeesOU)>
);

impl<T, OU> OnChargeEVMTransactionT<T> for OnChargeEVMTransaction<OU>
impl<T, BaseFeesOU, PriorityFeesOU> OnChargeEVMTransactionT<T>
for OnChargeEVMTransaction<BaseFeesOU, PriorityFeesOU>
where
T: pallet_evm::Config,
T::Currency: Balanced<pallet_evm::AccountIdOf<T>>,
OU: OnUnbalanced<Credit<pallet_evm::AccountIdOf<T>, T::Currency>>,
BaseFeesOU: OnUnbalanced<Credit<pallet_evm::AccountIdOf<T>, T::Currency>>,
PriorityFeesOU: OnUnbalanced<Credit<pallet_evm::AccountIdOf<T>, T::Currency>>,
U256: UniqueSaturatedInto<<T::Currency as Inspect<pallet_evm::AccountIdOf<T>>>::Balance>,
T::AddressMapping: pallet_evm::AddressMapping<T::AccountId>,
{
Expand All @@ -48,14 +52,14 @@ macro_rules! impl_on_charge_evm_transaction {
base_fee: U256,
already_withdrawn: Self::LiquidityInfo,
) -> Self::LiquidityInfo {
<EVMFungibleAdapter<<T as pallet_evm::Config>::Currency, OU> as OnChargeEVMTransactionT<
<EVMFungibleAdapter<<T as pallet_evm::Config>::Currency, BaseFeesOU> as OnChargeEVMTransactionT<
T,
>>::correct_and_deposit_fee(who, corrected_fee, base_fee, already_withdrawn)
}

fn pay_priority_fee(tip: Self::LiquidityInfo) {
if let Some(tip) = tip {
OU::on_unbalanced(tip);
PriorityFeesOU::on_unbalanced(tip);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
mod apis;
#[cfg(feature = "runtime-benchmarks")]
pub mod benchmarking;
pub mod deal_with_fees;
mod impl_moonbeam_xcm_call;
mod impl_moonbeam_xcm_call_tracing;
mod impl_on_charge_evm_transaction;
Expand Down
66 changes: 17 additions & 49 deletions runtime/moonbase/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ use frame_support::{
construct_runtime,
dispatch::{DispatchClass, GetDispatchInfo, PostDispatchInfo},
ensure,
pallet_prelude::DispatchResult,
pallet_prelude::{DispatchResult, PhantomData},
parameter_types,
traits::{
fungible::{Balanced, Credit, HoldConsideration, Inspect},
Expand Down Expand Up @@ -125,6 +125,7 @@ use xcm_runtime_apis::{
use smallvec::smallvec;
use sp_runtime::serde::{Deserialize, Serialize};

use sp_runtime::traits::TypedGet;
#[cfg(any(feature = "std", test))]
pub use sp_runtime::BuildStorage;

Expand Down Expand Up @@ -338,52 +339,6 @@ impl pallet_balances::Config for Runtime {
type WeightInfo = moonbase_weights::pallet_balances::WeightInfo<Runtime>;
}

pub struct DealWithFees<R>(sp_std::marker::PhantomData<R>);
impl<R> OnUnbalanced<Credit<R::AccountId, pallet_balances::Pallet<R>>> for DealWithFees<R>
where
R: pallet_balances::Config + pallet_treasury::Config,
{
// this seems to be called for substrate-based transactions
fn on_unbalanceds(
mut fees_then_tips: impl Iterator<Item = Credit<R::AccountId, pallet_balances::Pallet<R>>>,
) {
if let Some(fees) = fees_then_tips.next() {
let treasury_proportion =
runtime_params::dynamic_params::runtime_config::FeesTreasuryProportion::get();
let treasury_part = treasury_proportion.deconstruct();
let burn_part = Perbill::one().deconstruct() - treasury_part;
let (_, to_treasury) = fees.ration(burn_part, treasury_part);
// Balances pallet automatically burns dropped Credits by decreasing
// total_supply accordingly
ResolveTo::<TreasuryAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(
to_treasury,
);

// handle tip if there is one
if let Some(tip) = fees_then_tips.next() {
// for now we use the same burn/treasury strategy used for regular fees
let (_, to_treasury) = tip.ration(burn_part, treasury_part);
ResolveTo::<TreasuryAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(
to_treasury,
);
}
}
}

// this is called from pallet_evm for Ethereum-based transactions
// (technically, it calls on_unbalanced, which calls this when non-zero)
fn on_nonzero_unbalanced(amount: Credit<R::AccountId, pallet_balances::Pallet<R>>) {
// Balances pallet automatically burns dropped Credits by decreasing
// total_supply accordingly
let treasury_proportion =
runtime_params::dynamic_params::runtime_config::FeesTreasuryProportion::get();
let treasury_part = treasury_proportion.deconstruct();
let burn_part = Perbill::one().deconstruct() - treasury_part;
let (_, to_treasury) = amount.ration(burn_part, treasury_part);
ResolveTo::<TreasuryAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(to_treasury);
}
}

pub struct LengthToFee;
impl WeightToFeePolynomial for LengthToFee {
type Balance = Balance;
Expand All @@ -408,7 +363,13 @@ impl WeightToFeePolynomial for LengthToFee {

impl pallet_transaction_payment::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type OnChargeTransaction = FungibleAdapter<Balances, DealWithFees<Runtime>>;
type OnChargeTransaction = FungibleAdapter<
Balances,
DealWithSubstrateFeesAndTip<
Runtime,
dynamic_params::runtime_config::FeesTreasuryProportion,
>,
>;
type OperationalFeeMultiplier = ConstU8<5>;
type WeightToFee = ConstantMultiplier<Balance, ConstU128<{ currency::WEIGHT_FEE }>>;
type LengthToFee = LengthToFee;
Expand Down Expand Up @@ -542,7 +503,10 @@ impl pallet_evm::Config for Runtime {
type PrecompilesType = MoonbasePrecompiles<Self>;
type PrecompilesValue = PrecompilesValue;
type ChainId = EthereumChainId;
type OnChargeTransaction = OnChargeEVMTransaction<DealWithFees<Runtime>>;
type OnChargeTransaction = OnChargeEVMTransaction<
DealWithEthereumBaseFees<Runtime, dynamic_params::runtime_config::FeesTreasuryProportion>,
DealWithEthereumPriorityFees<Runtime>,
>;
type BlockGasLimit = BlockGasLimit;
type FindAuthor = FindAuthorAdapter<AccountId20, H160, AuthorInherent>;
type OnCreate = ();
Expand Down Expand Up @@ -1496,6 +1460,10 @@ pub type Executive = frame_executive::Executive<

#[cfg(feature = "runtime-benchmarks")]
use moonbeam_runtime_common::benchmarking::BenchmarkHelper;
use moonbeam_runtime_common::deal_with_fees::{
DealWithEthereumBaseFees, DealWithEthereumPriorityFees, DealWithSubstrateFeesAndTip,
};

#[cfg(feature = "runtime-benchmarks")]
mod benches {
frame_support::parameter_types! {
Expand Down
3 changes: 3 additions & 0 deletions runtime/moonbase/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,9 @@ pub fn set_parachain_inherent_data() {
use cumulus_primitives_core::PersistedValidationData;
use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder;

let author = AccountId::from(<pallet_evm::Pallet<Runtime>>::find_author());
pallet_author_inherent::Author::<Runtime>::put(author);

let mut relay_sproof = RelayStateSproofBuilder::default();
relay_sproof.para_id = 100u32.into();
relay_sproof.included_para_head = Some(HeadData(vec![1, 2, 3]));
Expand Down
Loading
Loading