-
Notifications
You must be signed in to change notification settings - Fork 509
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
sorpaas
merged 12 commits into
polkadot-evm:master
from
moonbeam-foundation:nish-upstream-hotfix
Jun 2, 2022
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
fc65741
add pallet::call to hotfix sufficients for non-zero nonce accounts
tgmichel 57cdd4b
make hotfix to fix sufficients payable
nbaztec ab3b902
Merge branch 'master' into nish-upstream-hotfix
nbaztec 5bf2723
cargo fmt
nbaztec 8a2b6d1
Merge branch 'master' into nish-upstream-hotfix
nbaztec c52d1ac
use distinct pallet for hotfix
nbaztec 8a7177c
cleanup traces from pallet evm
nbaztec 2bdee74
add pallet-hotfix-sufficients
nbaztec fb507fa
fix description
nbaztec 411ca3d
fix benchmarking
nbaztec b121bdf
update test condition, add docs
nbaztec 748ef65
use safe add
nbaztec File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// This file is part of Frontier. | ||
// | ||
// Copyright (c) 2020 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. | ||
|
||
//! Autogenerated weights for {{pallet}} | ||
//! | ||
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION {{version}} | ||
//! DATE: {{date}}, STEPS: `{{cmd.steps}}`, REPEAT: {{cmd.repeat}}, LOW RANGE: `{{cmd.lowest_range_values}}`, HIGH RANGE: `{{cmd.highest_range_values}}` | ||
//! EXECUTION: {{cmd.execution}}, WASM-EXECUTION: {{cmd.wasm_execution}}, CHAIN: {{cmd.chain}}, DB CACHE: {{cmd.db_cache}} | ||
|
||
// Executed Command: | ||
{{#each args as |arg|~}} | ||
// {{arg}} | ||
{{/each}} | ||
|
||
#![allow(unused_parens)] | ||
#![allow(unused_imports)] | ||
|
||
use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}}; | ||
use sp_std::marker::PhantomData; | ||
|
||
/// Weight functions needed for {{pallet}}. | ||
pub trait WeightInfo { | ||
{{#each benchmarks as |benchmark|}} | ||
fn {{benchmark.name~}} | ||
( | ||
{{~#each benchmark.components as |c| ~}} | ||
{{c.name}}: u32, {{/each~}} | ||
) -> Weight; | ||
{{/each}} | ||
} | ||
|
||
/// Weights for {{pallet}} using the Substrate node and recommended hardware. | ||
pub struct SubstrateWeight<T>(PhantomData<T>); | ||
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> { | ||
{{#each benchmarks as |benchmark|}} | ||
{{#each benchmark.comments as |comment|}} | ||
// {{comment}} | ||
{{/each}} | ||
fn {{benchmark.name~}} | ||
( | ||
{{~#each benchmark.components as |c| ~}} | ||
{{~#if (not c.is_used)}}_{{/if}}{{c.name}}: u32, {{/each~}} | ||
) -> Weight { | ||
({{underscore benchmark.base_weight}} as Weight) | ||
{{#each benchmark.component_weight as |cw|}} | ||
// Standard Error: {{underscore cw.error}} | ||
.saturating_add(({{underscore cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight)) | ||
{{/each}} | ||
{{#if (ne benchmark.base_reads "0")}} | ||
.saturating_add(T::DbWeight::get().reads({{benchmark.base_reads}} as Weight)) | ||
{{/if}} | ||
{{#each benchmark.component_reads as |cr|}} | ||
.saturating_add(T::DbWeight::get().reads(({{cr.slope}} as Weight).saturating_mul({{cr.name}} as Weight))) | ||
{{/each}} | ||
{{#if (ne benchmark.base_writes "0")}} | ||
.saturating_add(T::DbWeight::get().writes({{benchmark.base_writes}} as Weight)) | ||
{{/if}} | ||
{{#each benchmark.component_writes as |cw|}} | ||
.saturating_add(T::DbWeight::get().writes(({{cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight))) | ||
{{/each}} | ||
} | ||
{{/each}} | ||
} | ||
|
||
// For backwards compatibility and tests | ||
impl WeightInfo for () { | ||
{{#each benchmarks as |benchmark|}} | ||
{{#each benchmark.comments as |comment|}} | ||
// {{comment}} | ||
{{/each}} | ||
fn {{benchmark.name~}} | ||
( | ||
{{~#each benchmark.components as |c| ~}} | ||
{{~#if (not c.is_used)}}_{{/if}}{{c.name}}: u32, {{/each~}} | ||
) -> Weight { | ||
({{underscore benchmark.base_weight}} as Weight) | ||
{{#each benchmark.component_weight as |cw|}} | ||
// Standard Error: {{underscore cw.error}} | ||
.saturating_add(({{underscore cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight)) | ||
{{/each}} | ||
{{#if (ne benchmark.base_reads "0")}} | ||
.saturating_add(RocksDbWeight::get().reads({{benchmark.base_reads}} as Weight)) | ||
{{/if}} | ||
{{#each benchmark.component_reads as |cr|}} | ||
.saturating_add(RocksDbWeight::get().reads(({{cr.slope}} as Weight).saturating_mul({{cr.name}} as Weight))) | ||
{{/each}} | ||
{{#if (ne benchmark.base_writes "0")}} | ||
.saturating_add(RocksDbWeight::get().writes({{benchmark.base_writes}} as Weight)) | ||
{{/if}} | ||
{{#each benchmark.component_writes as |cw|}} | ||
.saturating_add(RocksDbWeight::get().writes(({{cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight))) | ||
{{/each}} | ||
} | ||
{{/each}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "Hotfix zero-value account sufficients with non-zero-value nonces" | ||
|
||
[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", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# 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. | ||
|
||
## Description | ||
|
||
This pallet can be used to hotfix the account state where a previous bug in EVM create account, lead to accounts being created with `0` references (consumers + providers + sufficients) | ||
but a non-zero nonce. | ||
|
||
The dispatchable `hotfix_inc_account_sufficients` fixes this by taking a list of account addresses that have zero reference counts (consumers, providers, sufficients are all `0`), | ||
and incrementing their `sufficients` reference counter. | ||
|
||
Any addresses that do not have a zero reference count, will be unaffected. | ||
|
||
License: Apache-2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// 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(feature = "runtime-benchmarks")] | ||
pub mod benchmarking; | ||
pub mod weights; | ||
pub use weights::WeightInfo; | ||
|
||
pub use pallet_evm::AddressMapping; | ||
use sp_runtime::traits::Zero; | ||
use sp_std::vec::Vec; | ||
|
||
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`, `consumers` and `providers` value. | ||
/// This state was caused by a previous bug in EVM create account dispatchable. | ||
/// | ||
/// Any accounts in the input list not satisfying the above condition will remain unaffected. | ||
#[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); | ||
let refs = frame_system::Pallet::<T>::consumers(&account_id) | ||
+ frame_system::Pallet::<T>::providers(&account_id) | ||
+ frame_system::Pallet::<T>::sufficients(&account_id); | ||
|
||
if !nonce.is_zero() && refs.is_zero() { | ||
frame_system::Pallet::<T>::inc_sufficients(&account_id); | ||
} | ||
} | ||
|
||
Ok(PostDispatchInfo { | ||
actual_weight: None, | ||
pays_fee: Pays::Yes, | ||
}) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might want to use safe math here
(That amount of references would be absurd, but better safe than sorry :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done