From 53539ebd5d08811237e056eb0b4f1e30252d1fa0 Mon Sep 17 00:00:00 2001 From: vedhavyas Date: Thu, 15 Feb 2024 13:59:19 +0530 Subject: [PATCH] handle burned/dusted balance on domains and note the same on domains --- crates/pallet-domains/src/tests.rs | 1 + crates/sp-domains/src/lib.rs | 10 +++++++++- .../src/malicious_bundle_tamper.rs | 3 ++- domains/pallets/block-fees/src/lib.rs | 8 ++++++++ domains/runtime/evm/src/lib.rs | 13 ++++++++++++- 5 files changed, 32 insertions(+), 3 deletions(-) diff --git a/crates/pallet-domains/src/tests.rs b/crates/pallet-domains/src/tests.rs index 32d3aef790..af1457030a 100644 --- a/crates/pallet-domains/src/tests.rs +++ b/crates/pallet-domains/src/tests.rs @@ -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; diff --git a/crates/sp-domains/src/lib.rs b/crates/sp-domains/src/lib.rs index 619e25b62a..7dbfcf0e31 100644 --- a/crates/sp-domains/src/lib.rs +++ b/crates/sp-domains/src/lib.rs @@ -264,16 +264,23 @@ pub struct BlockFees { /// 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 BlockFees 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, } } @@ -281,6 +288,7 @@ where pub fn total_fees(&self) -> Option { self.consensus_storage_fee .checked_add(&self.domain_execution_fee) + .and_then(|balance| balance.checked_add(&self.burned_balance)) } } diff --git a/crates/subspace-malicious-operator/src/malicious_bundle_tamper.rs b/crates/subspace-malicious-operator/src/malicious_bundle_tamper.rs index 81cb025460..0ba3f11ffb 100644 --- a/crates/subspace-malicious-operator/src/malicious_bundle_tamper.rs +++ b/crates/subspace-malicious-operator/src/malicious_bundle_tamper.rs @@ -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 => { diff --git a/domains/pallets/block-fees/src/lib.rs b/domains/pallets/block-fees/src/lib.rs index 99370eb1d2..624b03452a 100644 --- a/domains/pallets/block-fees/src/lib.rs +++ b/domains/pallets/block-fees/src/lib.rs @@ -177,6 +177,14 @@ mod pallet { }); } + /// Note burned balance on domains + pub fn note_burned_balance(burned_balance: T::Balance) { + CollectedBlockFees::::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. diff --git a/domains/runtime/evm/src/lib.rs b/domains/runtime/evm/src/lib.rs index f22796d1a6..11c2593f04 100644 --- a/domains/runtime/evm/src/lib.rs +++ b/domains/runtime/evm/src/lib.rs @@ -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}; @@ -295,6 +297,15 @@ 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> for DustRemovalHandler { + fn on_nonzero_unbalanced(dusted_amount: Credit) { + BlockFees::note_burned_balance(dusted_amount.peek()); + } +} + impl pallet_balances::Config for Runtime { type RuntimeFreezeReason = RuntimeFreezeReason; type MaxLocks = MaxLocks; @@ -302,7 +313,7 @@ impl pallet_balances::Config for Runtime { 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;