Skip to content

Commit

Permalink
In-block quick clear for suicide contracts with limit
Browse files Browse the repository at this point in the history
  • Loading branch information
sorpaas committed Oct 14, 2023
1 parent edc1d9f commit d830572
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions frame/ethereum/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ impl AddressMapping<AccountId32> for HashedAddressMapping {
}
}

parameter_types! {
pub SuicideQuickClearLimit: u32 = 0;
}

impl pallet_evm::Config for Test {
type FeeCalculator = FixedGasPrice;
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
Expand All @@ -170,6 +174,7 @@ impl pallet_evm::Config for Test {
type OnCreate = ();
type FindAuthor = FindAuthorTruncated;
type GasLimitPovSizeRatio = GasLimitPovSizeRatio;
type SuicideQuickClearLimit = SuicideQuickClearLimit;
type Timestamp = Timestamp;
type WeightInfo = ();
}
Expand Down
2 changes: 2 additions & 0 deletions frame/evm/precompile/dispatch/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ impl FindAuthor<H160> for FindAuthorTruncated {
parameter_types! {
pub BlockGasLimit: U256 = U256::max_value();
pub WeightPerGas: Weight = Weight::from_parts(20_000, 0);
pub SuicideQuickClearLimit: u32 = 0;
}
impl pallet_evm::Config for Test {
type FeeCalculator = FixedGasPrice;
Expand All @@ -152,6 +153,7 @@ impl pallet_evm::Config for Test {
type OnChargeTransaction = ();
type OnCreate = ();
type FindAuthor = FindAuthorTruncated;
type SuicideQuickClearLimit = SuicideQuickClearLimit;
type GasLimitPovSizeRatio = ();
type Timestamp = Timestamp;
type WeightInfo = ();
Expand Down
19 changes: 19 additions & 0 deletions frame/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ use scale_info::TypeInfo;
// Substrate
use frame_support::{
dispatch::{DispatchResultWithPostInfo, Pays, PostDispatchInfo},
storage::child::KillStorageResult,
traits::{
tokens::{
currency::Currency,
Expand Down Expand Up @@ -168,6 +169,9 @@ pub mod pallet {
/// Gas limit Pov size ratio.
type GasLimitPovSizeRatio: Get<u64>;

/// Define the quick clear limit of storage clearing when a contract suicides. Set to 0 to disable it.
type SuicideQuickClearLimit: Get<u32>;

/// Get the timestamp for the current block.
type Timestamp: Time;

Expand Down Expand Up @@ -807,6 +811,21 @@ impl<T: Config> Pallet<T> {

<AccountCodes<T>>::remove(address);
<AccountCodesMetadata<T>>::remove(address);

if T::SuicideQuickClearLimit::get() > 0 {
#[allow(deprecated)]
let res = <AccountStorages<T>>::remove_prefix(address, Some(T::SuicideQuickClearLimit::get()));

match res {
KillStorageResult::AllRemoved(_) => {
<Suicided<T>>::remove(address);

let account_id = T::AddressMapping::into_account_id(*address);
let _ = frame_system::Pallet::<T>::dec_sufficients(&account_id);
}
KillStorageResult::SomeRemaining(_) => (),
}
}
}

/// Create an account.
Expand Down
2 changes: 2 additions & 0 deletions frame/evm/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ parameter_types! {
pub const GasLimitPovSizeRatio: u64 = BLOCK_GAS_LIMIT.saturating_div(MAX_POV_SIZE);
pub WeightPerGas: Weight = Weight::from_parts(20_000, 0);
pub MockPrecompiles: MockPrecompileSet = MockPrecompileSet;
pub SuicideQuickClearLimit: u32 = 0;
}
impl crate::Config for Test {
type FeeCalculator = FixedGasPrice;
Expand All @@ -151,6 +152,7 @@ impl crate::Config for Test {
type OnCreate = ();
type FindAuthor = FindAuthorTruncated;
type GasLimitPovSizeRatio = GasLimitPovSizeRatio;
type SuicideQuickClearLimit = SuicideQuickClearLimit;
type Timestamp = Timestamp;
type WeightInfo = ();
}
Expand Down
2 changes: 2 additions & 0 deletions precompiles/tests-external/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ parameter_types! {
let block_gas_limit = BlockGasLimit::get().min(u64::MAX.into()).low_u64();
block_gas_limit.saturating_div(MAX_POV_SIZE)
};
pub SuicideQuickClearLimit: u32 = 0;
}

impl pallet_evm::Config for Runtime {
Expand All @@ -240,6 +241,7 @@ impl pallet_evm::Config for Runtime {
type OnCreate = ();
type FindAuthor = ();
type GasLimitPovSizeRatio = GasLimitPovSizeRatio;
type SuicideQuickClearLimit = SuicideQuickClearLimit;
type Timestamp = Timestamp;
type WeightInfo = pallet_evm::weights::SubstrateWeight<Runtime>;
}
Expand Down
2 changes: 2 additions & 0 deletions template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ parameter_types! {
pub const GasLimitPovSizeRatio: u64 = BLOCK_GAS_LIMIT.saturating_div(MAX_POV_SIZE);
pub PrecompilesValue: FrontierPrecompiles<Runtime> = FrontierPrecompiles::<_>::new();
pub WeightPerGas: Weight = Weight::from_parts(weight_per_gas(BLOCK_GAS_LIMIT, NORMAL_DISPATCH_RATIO, WEIGHT_MILLISECS_PER_BLOCK), 0);
pub SuicideQuickClearLimit: u32 = 0;
}

impl pallet_evm::Config for Runtime {
Expand All @@ -345,6 +346,7 @@ impl pallet_evm::Config for Runtime {
type OnCreate = ();
type FindAuthor = FindAuthorTruncated<Aura>;
type GasLimitPovSizeRatio = GasLimitPovSizeRatio;
type SuicideQuickClearLimit = SuicideQuickClearLimit;
type Timestamp = Timestamp;
type WeightInfo = pallet_evm::weights::SubstrateWeight<Self>;
}
Expand Down

0 comments on commit d830572

Please sign in to comment.