Skip to content

Commit

Permalink
handle burned/dusted balance on domains and note the same on domains
Browse files Browse the repository at this point in the history
  • Loading branch information
vedhavyas committed Feb 15, 2024
1 parent 799b68c commit 53539eb
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
1 change: 1 addition & 0 deletions crates/pallet-domains/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,7 @@ fn test_invalid_block_fees_fraud_proof() {
.block_fees
.consensus_storage_fee
+ 1,
domain_block.execution_receipt.block_fees.burned_balance + 1,
),
);
domain_block.execution_receipt.final_state_root = root;
Expand Down
10 changes: 9 additions & 1 deletion crates/sp-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,23 +264,31 @@ pub struct BlockFees<Balance> {
/// The domain execution fee including the storage and compute fee on domain chain,
/// tip, and the XDM reward.
pub domain_execution_fee: Balance,
/// Burned balances on domain chain
pub burned_balance: Balance,
}

impl<Balance> BlockFees<Balance>
where
Balance: CheckedAdd,
{
pub fn new(domain_execution_fee: Balance, consensus_storage_fee: Balance) -> Self {
pub fn new(
domain_execution_fee: Balance,
consensus_storage_fee: Balance,
burned_balance: Balance,
) -> Self {
BlockFees {
consensus_storage_fee,
domain_execution_fee,
burned_balance,
}
}

/// Returns the total fees that was collected and burned on the Domain.
pub fn total_fees(&self) -> Option<Balance> {
self.consensus_storage_fee
.checked_add(&self.domain_execution_fee)
.and_then(|balance| balance.checked_add(&self.burned_balance))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ where

match bad_receipt_type {
BadReceiptType::BlockFees => {
receipt.block_fees = BlockFees::new(random_seed.into(), random_seed.into());
receipt.block_fees =
BlockFees::new(random_seed.into(), random_seed.into(), random_seed.into());
}
// TODO: modify the length of `execution_trace` once the honest operator can handle
BadReceiptType::ExecutionTrace => {
Expand Down
8 changes: 8 additions & 0 deletions domains/pallets/block-fees/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ mod pallet {
});
}

/// Note burned balance on domains
pub fn note_burned_balance(burned_balance: T::Balance) {
CollectedBlockFees::<T>::mutate(|block_fees| {
block_fees.burned_balance =
block_fees.burned_balance.saturating_add(burned_balance);
});
}

/// Return the final domain transaction byte fee, which consist of:
/// - The `ConsensusChainByteFee` for the consensus chain storage cost since the domain
/// transaction need to be bundled and submitted to the consensus chain first.
Expand Down
13 changes: 12 additions & 1 deletion domains/runtime/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ use fp_account::EthereumSignature;
use fp_self_contained::{CheckedSignature, SelfContainedCall};
use frame_support::dispatch::{DispatchClass, DispatchInfo, GetDispatchInfo};
use frame_support::inherent::ProvideInherent;
use frame_support::traits::fungible::Credit;
use frame_support::traits::{
ConstU16, ConstU32, ConstU64, Currency, Everything, FindAuthor, Imbalance, OnFinalize,
OnUnbalanced,
};
use frame_support::weights::constants::{ParityDbWeight, WEIGHT_REF_TIME_PER_SECOND};
use frame_support::weights::{ConstantMultiplier, IdentityFee, Weight};
Expand Down Expand Up @@ -295,14 +297,23 @@ parameter_types! {
pub const MaxReserves: u32 = 50;
}

/// `DustRemovalHandler` used to collect all the SSC dust left when the account is reaped.
pub struct DustRemovalHandler;

impl OnUnbalanced<Credit<AccountId, Balances>> for DustRemovalHandler {
fn on_nonzero_unbalanced(dusted_amount: Credit<AccountId, Balances>) {
BlockFees::note_burned_balance(dusted_amount.peek());
}
}

impl pallet_balances::Config for Runtime {
type RuntimeFreezeReason = RuntimeFreezeReason;
type MaxLocks = MaxLocks;
/// The type for recording an account's balance.
type Balance = Balance;
/// The ubiquitous event type.
type RuntimeEvent = RuntimeEvent;
type DustRemoval = ();
type DustRemoval = DustRemovalHandler;
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System;
type WeightInfo = pallet_balances::weights::SubstrateWeight<Runtime>;
Expand Down

0 comments on commit 53539eb

Please sign in to comment.