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

add pallet::call to hotfix sufficients for non-zero nonce accounts #619

Merged
merged 12 commits into from
Jun 2, 2022
Prev Previous commit
Next Next commit
add pallet-hotfix-sufficients
  • Loading branch information
nbaztec committed May 31, 2022
commit 2bdee748417cd7f0ce091155ed92e187c8864a3e
40 changes: 40 additions & 0 deletions frame/hotfix-sufficients/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[package]
name = "pallet-hotfix-sufficients"
version = "1.0.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2021"
license = "Apache-2.0"
homepage = "https://substrate.dev"
repository = "https://github.com/paritytech/frontier/"
description = "EIP-1559 fee utils"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
scale-info = { version = "2.0.0", default-features = false, features = ["derive"] }
sp-core = { version = "6.0.0", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-io = { version = "6.0.0", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-runtime = { version = "6.0.0", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-std = { version = "4.0.0", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }

frame-benchmarking = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false, optional = true }
frame-support = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
frame-system = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
pallet-evm = { path = "../evm", default-features = false }

[features]
default = ["std"]
std = [
"codec/std",
"scale-info/std",
"sp-core/std",
"sp-runtime/std",
"frame-benchmarking/std",
"frame-support/std",
"frame-system/std",
]
runtime-benchmarks = [
"frame-benchmarking",
]
6 changes: 6 additions & 0 deletions frame/hotfix-sufficients/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Hotfix Sufficients Module

The Hotfix Sufficients module allows hotfixing account inconsistency to patch existing accounts that have a non-zero `nonce` but a zero `sufficients` value.
The accounts' `sufficients` values also need to be non-zero to be consistent.

License: Apache-2.0
62 changes: 62 additions & 0 deletions frame/hotfix-sufficients/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// SPDX-License-Identifier: Apache-2.0
// This file is part of Frontier.
//
// Copyright (c) 2020-2022 Parity Technologies (UK) Ltd.
//
// 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.

#![cfg(feature = "runtime-benchmarks")]

use frame_benchmarking::{benchmarks, impl_benchmark_test_suite};

use super::*;

benchmarks! {
hotfix_inc_account_sufficients {
// This benchmark tests the resource utilization by hotfixing N number of accounts
// by incrementing their `sufficients` if `nonce` is > 0.

let n in 0 .. 1000;

use frame_benchmarking::{whitelisted_caller};
use sp_core::H160;
use frame_system::RawOrigin;

// The caller account is whitelisted for DB reads/write by the benchmarking macro.
let caller: T::AccountId = whitelisted_caller();
let addresses = (0..n as u64)
.map(H160::from_low_u64_le)
.collect::<Vec<H160>>();
let accounts = addresses
.iter()
.cloned()
.map(|addr| {
let account_id = T::AddressMapping::into_account_id(addr);
frame_system::Pallet::<T>::inc_account_nonce(&account_id);
assert_eq!(frame_system::Pallet::<T>::sufficients(&account_id), 0);

account_id
})
.collect::<Vec<_>>();

}: _(RawOrigin::Signed(caller), addresses)
verify {
accounts
.iter()
.for_each(|id| {
assert_eq!(frame_system::Pallet::<T>::sufficients(&id), 1);
});
}
}

impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::tests::Test);
94 changes: 94 additions & 0 deletions frame/hotfix-sufficients/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// SPDX-License-Identifier: Apache-2.0
// This file is part of Frontier.
//
// Copyright (c) 2021-2022 Parity Technologies (UK) Ltd.
//
// 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.

#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(test)]
mod tests;

#[cfg(test)]
mod mock;

#[cfg(any(test, feature = "runtime-benchmarks"))]
pub mod benchmarking;
pub mod weights;
pub use weights::WeightInfo;

pub use pallet_evm::AddressMapping;
use sp_runtime::traits::Zero;

pub use self::pallet::*;

#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::{dispatch::PostDispatchInfo, pallet_prelude::*};
use frame_system::pallet_prelude::*;
use sp_core::H160;

#[pallet::config]
pub trait Config: frame_system::Config {
/// Mapping from address to account id.
type AddressMapping: AddressMapping<Self::AccountId>;
/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
}

#[pallet::error]
pub enum Error<T> {
/// Maximum address count exceeded
MaxAddressCountExceeded,
}

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::without_storage_info]
pub struct Pallet<T>(_);

#[pallet::call]
impl<T: Config> Pallet<T> {
/// Increment `sufficients` for existing accounts having a nonzero `nonce` but zero `sufficients` value.
#[pallet::weight(
<T as pallet::Config>::WeightInfo::hotfix_inc_account_sufficients(addresses.len().try_into().unwrap_or(u32::MAX))
)]
pub fn hotfix_inc_account_sufficients(
origin: OriginFor<T>,
addresses: Vec<H160>,
) -> DispatchResultWithPostInfo {
const MAX_ADDRESS_COUNT: usize = 1000;

frame_system::ensure_signed(origin)?;
ensure!(
addresses.len() <= MAX_ADDRESS_COUNT,
Error::<T>::MaxAddressCountExceeded
);

for address in addresses {
let account_id = T::AddressMapping::into_account_id(address);
let nonce = frame_system::Pallet::<T>::account_nonce(&account_id);
if !nonce.is_zero() {
frame_system::Pallet::<T>::inc_sufficients(&account_id);
}
}
nbaztec marked this conversation as resolved.
Show resolved Hide resolved

Ok(PostDispatchInfo {
actual_weight: None,
pays_fee: Pays::Yes,
})
}
}
}
84 changes: 84 additions & 0 deletions frame/hotfix-sufficients/src/mock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// SPDX-License-Identifier: Apache-2.0
// This file is part of Frontier.
//
// Copyright (c) 2021-2022 Parity Technologies (UK) Ltd.
//
// 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 frame_support::{parameter_types, traits::ConstU32};
use sp_core::{H160, H256};
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
};

use super::*;
use crate as pallet_hotfix_sufficients;

pub fn new_test_ext() -> sp_io::TestExternalities {
frame_system::GenesisConfig::default()
.build_storage::<Test>()
.unwrap()
.into()
}

frame_support::construct_runtime!(
pub enum Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
HotfixSufficients: pallet_hotfix_sufficients::{Pallet, Call},
}
);

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;

parameter_types! {
pub const BlockHashCount: u64 = 250;
pub BlockWeights: frame_system::limits::BlockWeights =
frame_system::limits::BlockWeights::simple_max(1024);
}
impl frame_system::Config for Test {
type BaseCallFilter = frame_support::traits::Everything;
type BlockWeights = ();
type BlockLength = ();
type DbWeight = ();
type Origin = Origin;
type Index = u64;
type BlockNumber = u64;
type Call = Call;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = H160;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = Event;
type BlockHashCount = BlockHashCount;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = ();
type OnSetCode = ();
type MaxConsumers = ConstU32<16>;
}

impl Config for Test {
type AddressMapping = pallet_evm::IdentityAddressMapping;
type WeightInfo = ();
}
Loading