From 00302afa8662616b214e569c0711cd722bf72583 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Wed, 3 Mar 2021 09:28:02 -0400 Subject: [PATCH 01/31] Initial setup --- Cargo.lock | 22 +++++++++ Cargo.toml | 1 + frame/accounts/Cargo.toml | 52 ++++++++++++++++++++++ frame/accounts/src/lib.rs | 84 +++++++++++++++++++++++++++++++++++ frame/support/src/traits.rs | 16 +++++++ frame/system/Cargo.toml | 1 + frame/system/src/lib.rs | 4 +- test-utils/runtime/Cargo.toml | 2 + test-utils/runtime/src/lib.rs | 5 +++ 9 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 frame/accounts/Cargo.toml create mode 100644 frame/accounts/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index e468e9852bcc5..8a3d07f166ba5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1681,6 +1681,26 @@ dependencies = [ "percent-encoding 2.1.0", ] +[[package]] +name = "frame-accounts" +version = "3.0.0" +dependencies = [ + "criterion", + "frame-support", + "frame-system", + "impl-trait-for-tuples", + "log", + "parity-scale-codec", + "serde", + "sp-core", + "sp-externalities", + "sp-io", + "sp-runtime", + "sp-std", + "sp-version", + "substrate-test-runtime-client", +] + [[package]] name = "frame-benchmarking" version = "3.1.0" @@ -1837,6 +1857,7 @@ name = "frame-system" version = "3.0.0" dependencies = [ "criterion", + "frame-accounts", "frame-support", "impl-trait-for-tuples", "log", @@ -9283,6 +9304,7 @@ name = "substrate-test-runtime" version = "2.0.0" dependencies = [ "cfg-if 1.0.0", + "frame-accounts", "frame-support", "frame-system", "frame-system-rpc-runtime-api", diff --git a/Cargo.toml b/Cargo.toml index 9a494d6aff39f..417e40ba574e0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -59,6 +59,7 @@ members = [ "client/tracing/proc-macro", "client/transaction-pool", "client/transaction-pool/graph", + "frame/accounts", "frame/assets", "frame/atomic-swap", "frame/aura", diff --git a/frame/accounts/Cargo.toml b/frame/accounts/Cargo.toml new file mode 100644 index 0000000000000..c4d4860a14d8f --- /dev/null +++ b/frame/accounts/Cargo.toml @@ -0,0 +1,52 @@ +[package] +name = "frame-accounts" +version = "3.0.0" +authors = ["Parity Technologies "] +edition = "2018" +license = "Apache-2.0" +homepage = "https://substrate.dev" +repository = "https://github.com/paritytech/substrate/" +description = "FRAME accounts module" +readme = "README.md" + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[dependencies] +serde = { version = "1.0.101", optional = true, features = ["derive"] } +codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } +sp-core = { version = "3.0.0", default-features = false, path = "../../primitives/core" } +sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } +sp-io = { version = "3.0.0", path = "../../primitives/io", default-features = false } +sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } +sp-version = { version = "3.0.0", default-features = false, path = "../../primitives/version" } +frame-support = { version = "3.0.0", default-features = false, path = "../support" } +frame-system = { version = "3.0.0", default-features = false, path = "../system" } +impl-trait-for-tuples = "0.2.1" +log = { version = "0.4.14", default-features = false } + +[dev-dependencies] +criterion = "0.3.3" +sp-externalities = { version = "0.9.0", path = "../../primitives/externalities" } +substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/runtime/client" } + +[features] +default = ["std"] +std = [ + "serde", + "codec/std", + "sp-core/std", + "sp-std/std", + "sp-io/std", + "frame-support/std", + "frame-system/std", + "sp-runtime/std", + "sp-version/std", + "log/std", +] +runtime-benchmarks = [ + "frame-system/runtime-benchmarks", + "sp-runtime/runtime-benchmarks", + "frame-support/runtime-benchmarks", +] +try-runtime = ["frame-support/try-runtime"] diff --git a/frame/accounts/src/lib.rs b/frame/accounts/src/lib.rs new file mode 100644 index 0000000000000..d607550bb9c44 --- /dev/null +++ b/frame/accounts/src/lib.rs @@ -0,0 +1,84 @@ +#![cfg_attr(not(feature = "std"), no_std)] + +use sp_runtime::RuntimeDebug; +use codec::{Encode, Decode, FullCodec}; + +/// Type used to encode the number of references an account has. +pub type RefCount = u32; + +/// Information of an account. +#[derive(Clone, Eq, PartialEq, Default, RuntimeDebug, Encode, Decode)] +pub struct AccountInfo { + /// The number of transactions this account has sent. + pub nonce: Index, + /// The number of other modules that currently depend on this account's existence. The account + /// cannot be reaped until this is zero. + pub consumers: RefCount, + /// The number of other modules that allow this account to exist. The account may not be reaped + /// until this is zero. + pub providers: RefCount, + /// The additional data that belongs to this account. Used to store the balance(s) in a lot of + /// chains. + pub data: AccountData, +} + +pub use pallet::*; + +#[frame_support::pallet] +pub mod pallet { + use crate::*; + + use frame_system::pallet_prelude::*; + use frame_support::pallet_prelude::*; + use frame_support::traits::AccountApi; + use sp_runtime::traits::One; + + /// System configuration trait. Implemented by runtime. + #[pallet::config] + pub trait Config: frame_system::Config { + /// Data to be associated with an account (other than nonce/transaction counter, which this + /// pallet does regardless). + type AccountData: Member + FullCodec + Clone + Default; + } + + /// The full account information for a particular account ID. + #[pallet::storage] + #[pallet::getter(fn account)] + pub type Account = StorageMap< + _, + Blake2_128Concat, + T::AccountId, + AccountInfo::AccountData>, + ValueQuery, + >; + + #[pallet::pallet] + #[pallet::generate_store(pub (super) trait Store)] + pub struct Pallet(_); + + #[pallet::hooks] + impl Hooks> for Pallet {} + + #[pallet::call] + impl Pallet {} + + impl AccountApi for Pallet { + type AccountId = ::AccountId; + type Index = ::Index; + + /// Return whether an account exists in storage. + fn account_exists(who: &Self::AccountId) -> bool { + Account::::contains_key(who) + } + + /// Retrieve the account transaction counter from storage. + fn account_nonce(who: Self::AccountId) -> Self::Index { + Account::::get(who).nonce + } + + /// Increment a particular account's nonce by 1. + fn inc_account_nonce(who: Self::AccountId) { + Account::::mutate(who, |a| a.nonce += Self::Index::one()); + } + } +} diff --git a/frame/support/src/traits.rs b/frame/support/src/traits.rs index ae0f5b8343157..dc18c6196cdc5 100644 --- a/frame/support/src/traits.rs +++ b/frame/support/src/traits.rs @@ -2245,6 +2245,22 @@ pub trait ExecuteBlock { fn execute_block(block: Block) -> Block::Header; } +/// A minimal API for creating an account system compatible with FRAME System. +pub trait AccountApi { + type AccountId; + type Index; + + /// Return whether an account exists in storage. + fn account_exists(who: &Self::AccountId) -> bool; + + /// Retrieve the account transaction counter from storage. + fn account_nonce(who: Self::AccountId) -> Self::Index; + + /// Increment a particular account's nonce by 1. + fn inc_account_nonce(who: Self::AccountId); + +} + #[cfg(test)] mod tests { use super::*; diff --git a/frame/system/Cargo.toml b/frame/system/Cargo.toml index 4306dbd644815..d5e6456f21960 100644 --- a/frame/system/Cargo.toml +++ b/frame/system/Cargo.toml @@ -28,6 +28,7 @@ log = { version = "0.4.14", default-features = false } criterion = "0.3.3" sp-externalities = { version = "0.9.0", path = "../../primitives/externalities" } substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/runtime/client" } +frame-accounts = { version = "3.0.0", path = "../accounts" } [features] default = ["std"] diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index a99184650cf55..69d64171b018a 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -88,7 +88,7 @@ use frame_support::{ Parameter, storage, traits::{ Contains, Get, PalletInfo, OnNewAccount, OnKilledAccount, HandleLifetime, - StoredMap, EnsureOrigin, OriginTrait, Filter, + StoredMap, EnsureOrigin, OriginTrait, Filter, AccountApi, }, weights::{ Weight, RuntimeDbWeight, DispatchInfo, DispatchClass, @@ -253,6 +253,8 @@ pub mod pallet { /// an identifier of the chain. #[pallet::constant] type SS58Prefix: Get; + + type AccountStorage: AccountApi; } #[pallet::pallet] diff --git a/test-utils/runtime/Cargo.toml b/test-utils/runtime/Cargo.toml index 89da7929e64b8..06fdff3380af6 100644 --- a/test-utils/runtime/Cargo.toml +++ b/test-utils/runtime/Cargo.toml @@ -32,6 +32,7 @@ sp-session = { version = "3.0.0", default-features = false, path = "../../primit sp-api = { version = "3.0.0", default-features = false, path = "../../primitives/api" } sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } pallet-babe = { version = "3.0.0", default-features = false, path = "../../frame/babe" } +frame-accounts = { version = "3.0.0", default-features = false, path = "../../frame/accounts" } frame-system = { version = "3.0.0", default-features = false, path = "../../frame/system" } frame-system-rpc-runtime-api = { version = "3.0.0", default-features = false, path = "../../frame/system/rpc/runtime-api" } pallet-timestamp = { version = "3.0.0", default-features = false, path = "../../frame/timestamp" } @@ -88,6 +89,7 @@ std = [ "pallet-babe/std", "frame-system-rpc-runtime-api/std", "frame-system/std", + "frame-accounts/std", "pallet-timestamp/std", "sc-service", "sp-finality-grandpa/std", diff --git a/test-utils/runtime/src/lib.rs b/test-utils/runtime/src/lib.rs index 5f80dc93a95f2..76a330312b46b 100644 --- a/test-utils/runtime/src/lib.rs +++ b/test-utils/runtime/src/lib.rs @@ -492,6 +492,10 @@ parameter_types! { BlockWeights::with_sensible_defaults(4 * 1024 * 1024, Perbill::from_percent(75)); } +impl frame_accounts::Config for Runtime { + type AccountData = (); +} + impl frame_system::Config for Runtime { type BaseCallFilter = (); type BlockWeights = RuntimeBlockWeights; @@ -515,6 +519,7 @@ impl frame_system::Config for Runtime { type OnKilledAccount = (); type SystemWeightInfo = (); type SS58Prefix = (); + type AccountStorage = frame_accounts::Pallet::; } impl pallet_timestamp::Config for Runtime { From b650e71e1e6db095fecba38d3e4ca0237040de5d Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Wed, 3 Mar 2021 11:15:11 -0400 Subject: [PATCH 02/31] more --- Cargo.lock | 1 + bin/node-template/runtime/Cargo.toml | 1 + bin/node-template/runtime/src/lib.rs | 7 + frame/accounts/src/lib.rs | 273 ++++++++++++++++++++- frame/support/src/traits.rs | 14 +- frame/system/src/extensions/check_nonce.rs | 20 +- frame/system/src/lib.rs | 267 +------------------- frame/system/src/offchain.rs | 11 +- 8 files changed, 300 insertions(+), 294 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8a3d07f166ba5..976586d2e2aef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4225,6 +4225,7 @@ dependencies = [ name = "node-template-runtime" version = "2.0.0" dependencies = [ + "frame-accounts", "frame-benchmarking", "frame-executive", "frame-support", diff --git a/bin/node-template/runtime/Cargo.toml b/bin/node-template/runtime/Cargo.toml index d4e202d688c87..5e96a63f80f7e 100644 --- a/bin/node-template/runtime/Cargo.toml +++ b/bin/node-template/runtime/Cargo.toml @@ -15,6 +15,7 @@ codec = { package = "parity-scale-codec", version = "2.0.0", default-features = pallet-aura = { version = "3.0.0", default-features = false, path = "../../../frame/aura" } pallet-balances = { version = "3.0.0", default-features = false, path = "../../../frame/balances" } +frame-accounts = { version = "3.0.0", default-features = false, path = "../../../frame/accounts" } frame-support = { version = "3.0.0", default-features = false, path = "../../../frame/support" } pallet-grandpa = { version = "3.0.0", default-features = false, path = "../../../frame/grandpa" } pallet-randomness-collective-flip = { version = "3.0.0", default-features = false, path = "../../../frame/randomness-collective-flip" } diff --git a/bin/node-template/runtime/src/lib.rs b/bin/node-template/runtime/src/lib.rs index a7372d5d02314..2a5f441f7fa4d 100644 --- a/bin/node-template/runtime/src/lib.rs +++ b/bin/node-template/runtime/src/lib.rs @@ -188,6 +188,12 @@ impl frame_system::Config for Runtime { type SystemWeightInfo = (); /// This is used as an identifier of the chain. 42 is the generic substrate prefix. type SS58Prefix = SS58Prefix; + /// This is where we store account information. + type AccountStorage = Accounts; +} + +impl frame_accounts::Config for Runtime { + type AccountData = pallet_balances::AccountData; } impl pallet_aura::Config for Runtime { @@ -271,6 +277,7 @@ construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: frame_accounts::{Module, Call, Storage}, RandomnessCollectiveFlip: pallet_randomness_collective_flip::{Module, Call, Storage}, Timestamp: pallet_timestamp::{Module, Call, Storage, Inherent}, Aura: pallet_aura::{Module, Config}, diff --git a/frame/accounts/src/lib.rs b/frame/accounts/src/lib.rs index d607550bb9c44..b83f724d3d836 100644 --- a/frame/accounts/src/lib.rs +++ b/frame/accounts/src/lib.rs @@ -1,6 +1,13 @@ #![cfg_attr(not(feature = "std"), no_std)] -use sp_runtime::RuntimeDebug; +use sp_std::marker::PhantomData; +use sp_runtime::{ + RuntimeDebug, + traits::StoredMapError, +}; +use frame_support::traits::{ + HandleLifetime, StoredMap, OnNewAccount, OnKilledAccount, +}; use codec::{Encode, Decode, FullCodec}; /// Type used to encode the number of references an account has. @@ -22,6 +29,45 @@ pub struct AccountInfo { pub data: AccountData, } +/// Reference status; can be either referenced or unreferenced. +#[derive(RuntimeDebug)] +pub enum RefStatus { + Referenced, + Unreferenced, +} + +/// Some resultant status relevant to incrementing a provider reference. +#[derive(RuntimeDebug)] +pub enum IncRefStatus { + /// Account was created. + Created, + /// Account already existed. + Existed, +} + +/// Some resultant status relevant to decrementing a provider reference. +#[derive(RuntimeDebug)] +pub enum DecRefStatus { + /// Account was destroyed. + Reaped, + /// Account still exists. + Exists, +} + +/// Some resultant status relevant to decrementing a provider reference. +#[derive(RuntimeDebug)] +pub enum DecRefError { + /// Account cannot have the last provider reference removed while there is a consumer. + ConsumerRemaining, +} + +/// Some resultant status relevant to incrementing a provider reference. +#[derive(RuntimeDebug)] +pub enum IncRefError { + /// Account cannot introduce a consumer while there are no providers. + NoProviders, +} + pub use pallet::*; #[frame_support::pallet] @@ -36,9 +82,30 @@ pub mod pallet { /// System configuration trait. Implemented by runtime. #[pallet::config] pub trait Config: frame_system::Config { + /// The overarching event type. + type Event: From> + IsType<::Event>; + /// Data to be associated with an account (other than nonce/transaction counter, which this /// pallet does regardless). type AccountData: Member + FullCodec + Clone + Default; + + /// Handler for when a new account has just been created. + type OnNewAccount: OnNewAccount; + + /// A function that is invoked when an account has been determined to be dead. + /// + /// All resources should be cleaned up associated with the given account. + type OnKilledAccount: OnKilledAccount; + } + + /// Event for the Accounts pallet. + #[pallet::event] + #[pallet::metadata(T::AccountId = "AccountId")] + pub enum Event { + /// A new \[account\] was created. + NewAccount(T::AccountId), + /// An \[account\] was reaped. + KilledAccount(T::AccountId), } /// The full account information for a particular account ID. @@ -62,23 +129,213 @@ pub mod pallet { #[pallet::call] impl Pallet {} - impl AccountApi for Pallet { - type AccountId = ::AccountId; - type Index = ::Index; + impl AccountApi for Pallet { + type AccountData = T::AccountData; /// Return whether an account exists in storage. - fn account_exists(who: &Self::AccountId) -> bool { + fn account_exists(who: &T::AccountId) -> bool { Account::::contains_key(who) } + /// Return the data for an account + fn get(who: &T::AccountId) -> Self::AccountData { + Account::::get(who) + } + /// Retrieve the account transaction counter from storage. - fn account_nonce(who: Self::AccountId) -> Self::Index { + fn account_nonce(who: T::AccountId) -> T::Index { Account::::get(who).nonce } /// Increment a particular account's nonce by 1. - fn inc_account_nonce(who: Self::AccountId) { - Account::::mutate(who, |a| a.nonce += Self::Index::one()); + fn inc_account_nonce(who: T::AccountId) { + Account::::mutate(who, |a| a.nonce += T::Index::one()); + } + } + + impl Pallet { + /// An account is being created. + pub fn on_created_account(who: T::AccountId, _a: &mut AccountInfo) { + T::OnNewAccount::on_new_account(&who); + Self::deposit_event(Event::NewAccount(who)); + } + + /// Do anything that needs to be done after an account has been killed. + fn on_killed_account(who: T::AccountId) { + T::OnKilledAccount::on_killed_account(&who); + Self::deposit_event(Event::KilledAccount(who)); + } + + /// Increment the reference counter on an account. + /// + /// The account `who`'s `providers` must be non-zero or this will return an error. + pub fn inc_providers(who: &T::AccountId) -> IncRefStatus { + Account::::mutate(who, |a| if a.providers == 0 { + // Account is being created. + a.providers = 1; + frame_system::Module::::on_created_account(who.clone(), a); + IncRefStatus::Created + } else { + a.providers = a.providers.saturating_add(1); + IncRefStatus::Existed + }) + } + + /// Decrement the reference counter on an account. This *MUST* only be done once for every time + /// you called `inc_consumers` on `who`. + pub fn dec_providers(who: &T::AccountId) -> Result { + Account::::try_mutate_exists(who, |maybe_account| { + if let Some(mut account) = maybe_account.take() { + match (account.providers, account.consumers) { + (0, _) => { + // Logic error - cannot decrement beyond zero and no item should + // exist with zero providers. + log::error!( + target: "runtime::system", + "Logic error: Unexpected underflow in reducing provider", + ); + Ok(DecRefStatus::Reaped) + }, + (1, 0) => { + frame_system::Module::::on_killed_account(who.clone()); + Ok(DecRefStatus::Reaped) + } + (1, _) => { + // Cannot remove last provider if there are consumers. + Err(DecRefError::ConsumerRemaining) + } + (x, _) => { + account.providers = x - 1; + *maybe_account = Some(account); + Ok(DecRefStatus::Exists) + } + } + } else { + log::error!( + target: "runtime::system", + "Logic error: Account already dead when reducing provider", + ); + Ok(DecRefStatus::Reaped) + } + }) + } + + /// The number of outstanding references for the account `who`. + pub fn providers(who: &T::AccountId) -> RefCount { + Account::::get(who).providers } + + /// Increment the reference counter on an account. + /// + /// The account `who`'s `providers` must be non-zero or this will return an error. + pub fn inc_consumers(who: &T::AccountId) -> Result<(), IncRefError> { + Account::::try_mutate(who, |a| if a.providers > 0 { + a.consumers = a.consumers.saturating_add(1); + Ok(()) + } else { + Err(IncRefError::NoProviders) + }) + } + + /// Decrement the reference counter on an account. This *MUST* only be done once for every time + /// you called `inc_consumers` on `who`. + pub fn dec_consumers(who: &T::AccountId) { + Account::::mutate(who, |a| if a.consumers > 0 { + a.consumers -= 1; + } else { + log::error!( + target: "runtime::system", + "Logic error: Unexpected underflow in reducing consumer", + ); + }) + } + + /// The number of outstanding references for the account `who`. + pub fn consumers(who: &T::AccountId) -> RefCount { + Account::::get(who).consumers + } + + /// True if the account has some outstanding references. + pub fn is_provider_required(who: &T::AccountId) -> bool { + Account::::get(who).consumers != 0 + } + + + pub fn insert(who: T::AccountId, account: T::AccountData) { + Account::::insert(who, account) + } + } +} + +/// Event handler which registers a provider when created. +pub struct Provider(PhantomData); +impl HandleLifetime for Provider { + fn created(t: &T::AccountId) -> Result<(), StoredMapError> { + Module::::inc_providers(t); + Ok(()) } + fn killed(t: &T::AccountId) -> Result<(), StoredMapError> { + Module::::dec_providers(t) + .map(|_| ()) + .or_else(|e| match e { + DecRefError::ConsumerRemaining => Err(StoredMapError::ConsumerRemaining), + }) + } +} + +/// Event handler which registers a consumer when created. +pub struct Consumer(PhantomData); +impl HandleLifetime for Consumer { + fn created(t: &T::AccountId) -> Result<(), StoredMapError> { + Module::::inc_consumers(t) + .map_err(|e| match e { + IncRefError::NoProviders => StoredMapError::NoProviders + }) + } + fn killed(t: &T::AccountId) -> Result<(), StoredMapError> { + Module::::dec_consumers(t); + Ok(()) + } +} + + +/// Implement StoredMap for a simple single-item, provide-when-not-default system. This works fine +/// for storing a single item which allows the account to continue existing as long as it's not +/// empty/default. +/// +/// Anything more complex will need more sophisticated logic. +impl StoredMap::AccountData> for Pallet { + fn get(k: &T::AccountId) -> ::AccountData { + Account::::get(k).data + } + + fn try_mutate_exists>( + k: &T::AccountId, + f: impl FnOnce(&mut Option<::AccountData>) -> Result, + ) -> Result { + let account = Account::::get(k); + let was_providing = is_providing(&account.data); + let mut some_data = if was_providing { Some(account.data) } else { None }; + let result = f(&mut some_data)?; + let is_providing = some_data.is_some(); + if !was_providing && is_providing { + Self::inc_providers(k); + } else if was_providing && !is_providing { + match Self::dec_providers(k) { + Err(DecRefError::ConsumerRemaining) => Err(StoredMapError::ConsumerRemaining)?, + Ok(DecRefStatus::Reaped) => return Ok(result), + Ok(DecRefStatus::Exists) => { + // Update value as normal... + } + } + } else if !was_providing && !is_providing { + return Ok(result) + } + Account::::mutate(k, |a| a.data = some_data.unwrap_or_default()); + Ok(result) + } +} + +fn is_providing(d: &T) -> bool { + d != &T::default() } diff --git a/frame/support/src/traits.rs b/frame/support/src/traits.rs index dc18c6196cdc5..064478ff9b412 100644 --- a/frame/support/src/traits.rs +++ b/frame/support/src/traits.rs @@ -2246,18 +2246,20 @@ pub trait ExecuteBlock { } /// A minimal API for creating an account system compatible with FRAME System. -pub trait AccountApi { - type AccountId; - type Index; +pub trait AccountApi { + type AccountData; /// Return whether an account exists in storage. - fn account_exists(who: &Self::AccountId) -> bool; + fn account_exists(who: &AccountId) -> bool; + + /// Return the data for an account. + fn get(who: &AccountId) -> Self::AccountData; /// Retrieve the account transaction counter from storage. - fn account_nonce(who: Self::AccountId) -> Self::Index; + fn account_nonce(who: AccountId) -> Index; /// Increment a particular account's nonce by 1. - fn inc_account_nonce(who: Self::AccountId); + fn inc_account_nonce(who: AccountId); } diff --git a/frame/system/src/extensions/check_nonce.rs b/frame/system/src/extensions/check_nonce.rs index bc48be925bc0d..fd3b58dc77bfc 100644 --- a/frame/system/src/extensions/check_nonce.rs +++ b/frame/system/src/extensions/check_nonce.rs @@ -17,7 +17,10 @@ use codec::{Encode, Decode}; use crate::Config; -use frame_support::weights::DispatchInfo; +use frame_support::{ + traits::AccountApi, + weights::DispatchInfo, +}; use sp_runtime::{ traits::{SignedExtension, DispatchInfoOf, Dispatchable, One}, transaction_validity::{ @@ -71,18 +74,17 @@ impl SignedExtension for CheckNonce where _info: &DispatchInfoOf, _len: usize, ) -> Result<(), TransactionValidityError> { - let mut account = crate::Account::::get(who); - if self.0 != account.nonce { + let account_nonce = T::AccountStorage::account_nonce(who.clone()); + if self.0 != account_nonce { return Err( - if self.0 < account.nonce { + if self.0 < account_nonce { InvalidTransaction::Stale } else { InvalidTransaction::Future }.into() ) } - account.nonce += T::Index::one(); - crate::Account::::insert(who, account); + T::AccountStorage::inc_account_nonce(who.clone()); Ok(()) } @@ -94,13 +96,13 @@ impl SignedExtension for CheckNonce where _len: usize, ) -> TransactionValidity { // check index - let account = crate::Account::::get(who); - if self.0 < account.nonce { + let account_nonce = T::AccountStorage::account_nonce(who.clone()); + if self.0 < account_nonce { return InvalidTransaction::Stale.into() } let provides = vec![Encode::encode(&(who, self.0))]; - let requires = if account.nonce < self.0 { + let requires = if account_nonce < self.0 { vec![Encode::encode(&(who, self.0 - One::one()))] } else { vec![] diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index 69d64171b018a..fdef5fd3030bf 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -232,18 +232,6 @@ pub mod pallet { /// For tests it is okay to use `()` as type, however it will provide "useless" data. type PalletInfo: PalletInfo; - /// Data to be associated with an account (other than nonce/transaction counter, which this - /// pallet does regardless). - type AccountData: Member + FullCodec + Clone + Default; - - /// Handler for when a new account has just been created. - type OnNewAccount: OnNewAccount; - - /// A function that is invoked when an account has been determined to be dead. - /// - /// All resources should be cleaned up associated with the given account. - type OnKilledAccount: OnKilledAccount; - type SystemWeightInfo: WeightInfo; /// The designated SS85 prefix of this chain. @@ -254,7 +242,7 @@ pub mod pallet { #[pallet::constant] type SS58Prefix: Get; - type AccountStorage: AccountApi; + type AccountStorage: AccountApi; } #[pallet::pallet] @@ -264,12 +252,7 @@ pub mod pallet { #[pallet::hooks] impl Hooks> for Pallet { fn on_runtime_upgrade() -> frame_support::weights::Weight { - if !UpgradedToDualRefCount::::get() { - UpgradedToDualRefCount::::put(true); - migrations::migrate_to_dual_ref_count::() - } else { - 0 - } + 0 } fn integrity_test() { @@ -511,17 +494,6 @@ pub mod pallet { #[pallet::origin] pub type Origin = RawOrigin<::AccountId>; - /// The full account information for a particular account ID. - #[pallet::storage] - #[pallet::getter(fn account)] - pub type Account = StorageMap< - _, - Blake2_128Concat, - T::AccountId, - AccountInfo, - ValueQuery, - >; - /// Total extrinsics count for the current block. #[pallet::storage] pub(super) type ExtrinsicCount = StorageValue<_, u32>; @@ -640,25 +612,6 @@ pub mod pallet { } } -mod migrations { - use super::*; - - #[allow(dead_code)] - pub fn migrate_all() -> frame_support::weights::Weight { - Account::::translate::<(T::Index, u8, T::AccountData), _>(|_key, (nonce, rc, data)| - Some(AccountInfo { nonce, consumers: rc as RefCount, providers: 1, data }) - ); - T::BlockWeights::get().max_block - } - - pub fn migrate_to_dual_ref_count() -> frame_support::weights::Weight { - Account::::translate::<(T::Index, RefCount, T::AccountData), _>(|_key, (nonce, rc, data)| - Some(AccountInfo { nonce, consumers: rc as RefCount, providers: 1, data }) - ); - T::BlockWeights::get().max_block - } -} - #[cfg(feature = "std")] impl GenesisConfig { /// Direct implementation of `GenesisBuild::build_storage`. @@ -1009,128 +962,6 @@ pub enum IncRefError { } impl Module { - pub fn account_exists(who: &T::AccountId) -> bool { - Account::::contains_key(who) - } - - /// Increment the reference counter on an account. - #[deprecated = "Use `inc_consumers` instead"] - pub fn inc_ref(who: &T::AccountId) { - let _ = Self::inc_consumers(who); - } - - /// Decrement the reference counter on an account. This *MUST* only be done once for every time - /// you called `inc_consumers` on `who`. - #[deprecated = "Use `dec_consumers` instead"] - pub fn dec_ref(who: &T::AccountId) { - let _ = Self::dec_consumers(who); - } - - /// The number of outstanding references for the account `who`. - #[deprecated = "Use `consumers` instead"] - pub fn refs(who: &T::AccountId) -> RefCount { - Self::consumers(who) - } - - /// True if the account has no outstanding references. - #[deprecated = "Use `!is_provider_required` instead"] - pub fn allow_death(who: &T::AccountId) -> bool { - !Self::is_provider_required(who) - } - - /// Increment the reference counter on an account. - /// - /// The account `who`'s `providers` must be non-zero or this will return an error. - pub fn inc_providers(who: &T::AccountId) -> IncRefStatus { - Account::::mutate(who, |a| if a.providers == 0 { - // Account is being created. - a.providers = 1; - Self::on_created_account(who.clone(), a); - IncRefStatus::Created - } else { - a.providers = a.providers.saturating_add(1); - IncRefStatus::Existed - }) - } - - /// Decrement the reference counter on an account. This *MUST* only be done once for every time - /// you called `inc_consumers` on `who`. - pub fn dec_providers(who: &T::AccountId) -> Result { - Account::::try_mutate_exists(who, |maybe_account| { - if let Some(mut account) = maybe_account.take() { - match (account.providers, account.consumers) { - (0, _) => { - // Logic error - cannot decrement beyond zero and no item should - // exist with zero providers. - log::error!( - target: "runtime::system", - "Logic error: Unexpected underflow in reducing provider", - ); - Ok(DecRefStatus::Reaped) - }, - (1, 0) => { - Module::::on_killed_account(who.clone()); - Ok(DecRefStatus::Reaped) - } - (1, _) => { - // Cannot remove last provider if there are consumers. - Err(DecRefError::ConsumerRemaining) - } - (x, _) => { - account.providers = x - 1; - *maybe_account = Some(account); - Ok(DecRefStatus::Exists) - } - } - } else { - log::error!( - target: "runtime::system", - "Logic error: Account already dead when reducing provider", - ); - Ok(DecRefStatus::Reaped) - } - }) - } - - /// The number of outstanding references for the account `who`. - pub fn providers(who: &T::AccountId) -> RefCount { - Account::::get(who).providers - } - - /// Increment the reference counter on an account. - /// - /// The account `who`'s `providers` must be non-zero or this will return an error. - pub fn inc_consumers(who: &T::AccountId) -> Result<(), IncRefError> { - Account::::try_mutate(who, |a| if a.providers > 0 { - a.consumers = a.consumers.saturating_add(1); - Ok(()) - } else { - Err(IncRefError::NoProviders) - }) - } - - /// Decrement the reference counter on an account. This *MUST* only be done once for every time - /// you called `inc_consumers` on `who`. - pub fn dec_consumers(who: &T::AccountId) { - Account::::mutate(who, |a| if a.consumers > 0 { - a.consumers -= 1; - } else { - log::error!( - target: "runtime::system", - "Logic error: Unexpected underflow in reducing consumer", - ); - }) - } - - /// The number of outstanding references for the account `who`. - pub fn consumers(who: &T::AccountId) -> RefCount { - Account::::get(who).consumers - } - - /// True if the account has some outstanding references. - pub fn is_provider_required(who: &T::AccountId) -> bool { - Account::::get(who).consumers != 0 - } /// Deposits an event into this block's event record. pub fn deposit_event(event: impl Into) { @@ -1350,16 +1181,6 @@ impl Module { /// Return the chain's current runtime version. pub fn runtime_version() -> RuntimeVersion { T::Version::get() } - /// Retrieve the account transaction counter from storage. - pub fn account_nonce(who: impl EncodeLike) -> T::Index { - Account::::get(who).nonce - } - - /// Increment a particular account's nonce by 1. - pub fn inc_account_nonce(who: impl EncodeLike) { - Account::::mutate(who, |a| a.nonce += T::Index::one()); - } - /// Note what the extrinsic data of the current extrinsic index is. /// /// This is required to be called before applying an extrinsic. The data will used @@ -1402,18 +1223,6 @@ impl Module { ExecutionPhase::::put(Phase::ApplyExtrinsic(0)) } - /// An account is being created. - pub fn on_created_account(who: T::AccountId, _a: &mut AccountInfo) { - T::OnNewAccount::on_new_account(&who); - Self::deposit_event(Event::NewAccount(who)); - } - - /// Do anything that needs to be done after an account has been killed. - fn on_killed_account(who: T::AccountId) { - T::OnKilledAccount::on_killed_account(&who); - Self::deposit_event(Event::KilledAccount(who)); - } - /// Determine whether or not it is possible to update the code. /// /// Checks the given code if it is a valid runtime wasm blob by instantianting @@ -1437,37 +1246,6 @@ impl Module { } } -/// Event handler which registers a provider when created. -pub struct Provider(PhantomData); -impl HandleLifetime for Provider { - fn created(t: &T::AccountId) -> Result<(), StoredMapError> { - Module::::inc_providers(t); - Ok(()) - } - fn killed(t: &T::AccountId) -> Result<(), StoredMapError> { - Module::::dec_providers(t) - .map(|_| ()) - .or_else(|e| match e { - DecRefError::ConsumerRemaining => Err(StoredMapError::ConsumerRemaining), - }) - } -} - -/// Event handler which registers a consumer when created. -pub struct Consumer(PhantomData); -impl HandleLifetime for Consumer { - fn created(t: &T::AccountId) -> Result<(), StoredMapError> { - Module::::inc_consumers(t) - .map_err(|e| match e { - IncRefError::NoProviders => StoredMapError::NoProviders - }) - } - fn killed(t: &T::AccountId) -> Result<(), StoredMapError> { - Module::::dec_consumers(t); - Ok(()) - } -} - impl BlockNumberProvider for Pallet { type BlockNumber = ::BlockNumber; @@ -1477,47 +1255,6 @@ impl BlockNumberProvider for Pallet } } -fn is_providing(d: &T) -> bool { - d != &T::default() -} - -/// Implement StoredMap for a simple single-item, provide-when-not-default system. This works fine -/// for storing a single item which allows the account to continue existing as long as it's not -/// empty/default. -/// -/// Anything more complex will need more sophisticated logic. -impl StoredMap for Pallet { - fn get(k: &T::AccountId) -> T::AccountData { - Account::::get(k).data - } - - fn try_mutate_exists>( - k: &T::AccountId, - f: impl FnOnce(&mut Option) -> Result, - ) -> Result { - let account = Account::::get(k); - let was_providing = is_providing(&account.data); - let mut some_data = if was_providing { Some(account.data) } else { None }; - let result = f(&mut some_data)?; - let is_providing = some_data.is_some(); - if !was_providing && is_providing { - Self::inc_providers(k); - } else if was_providing && !is_providing { - match Self::dec_providers(k) { - Err(DecRefError::ConsumerRemaining) => Err(StoredMapError::ConsumerRemaining)?, - Ok(DecRefStatus::Reaped) => return Ok(result), - Ok(DecRefStatus::Exists) => { - // Update value as normal... - } - } - } else if !was_providing && !is_providing { - return Ok(result) - } - Account::::mutate(k, |a| a.data = some_data.unwrap_or_default()); - Ok(result) - } -} - /// Split an `option` into two constituent options, as defined by a `splitter` function. pub fn split_inner(option: Option, splitter: impl FnOnce(T) -> (R, S)) -> (Option, Option) diff --git a/frame/system/src/offchain.rs b/frame/system/src/offchain.rs index aa8bce966192e..f9231e0ac4cea 100644 --- a/frame/system/src/offchain.rs +++ b/frame/system/src/offchain.rs @@ -63,7 +63,7 @@ use sp_std::convert::{TryInto, TryFrom}; use sp_std::prelude::{Box, Vec}; use sp_runtime::app_crypto::RuntimeAppPublic; use sp_runtime::traits::{Extrinsic as ExtrinsicT, IdentifyAccount, One}; -use frame_support::RuntimeDebug; +use frame_support::{RuntimeDebug, traits::AccountApi}; /// Marker struct used to flag using all supported keys to sign a payload. pub struct ForAll {} @@ -549,18 +549,18 @@ pub trait SendSignedTransaction< account: &Account, call: LocalCall, ) -> Option> { - let mut account_data = crate::Account::::get(&account.id); + let account_nonce = T::AccountStorage::account_nonce(account.id.clone()); log::debug!( target: "runtime::offchain", "Creating signed transaction from account: {:?} (nonce: {:?})", account.id, - account_data.nonce, + account_nonce, ); let (call, signature) = T::create_transaction::( call.into(), account.public.clone(), account.id.clone(), - account_data.nonce + account_nonce, )?; let res = SubmitTransaction:: ::submit_transaction(call, Some(signature)); @@ -568,8 +568,7 @@ pub trait SendSignedTransaction< if res.is_ok() { // increment the nonce. This is fine, since the code should always // be running in off-chain context, so we NEVER persists data. - account_data.nonce += One::one(); - crate::Account::::insert(&account.id, account_data); + T::AccountStorage::inc_account_nonce(account.id.clone()); } Some(res) From 4f3634e60415e4f31dfe5149b858ad895e4f09ce Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Wed, 3 Mar 2021 22:07:04 -0400 Subject: [PATCH 03/31] fixes --- Cargo.lock | 1 + frame/accounts/src/lib.rs | 137 +++++++++++++++++++++++++++------- frame/session/Cargo.toml | 3 + frame/session/src/lib.rs | 10 +-- frame/support/src/traits.rs | 4 +- test-utils/runtime/src/lib.rs | 12 ++- 6 files changed, 128 insertions(+), 39 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 976586d2e2aef..6cc920e8c8a0c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5175,6 +5175,7 @@ dependencies = [ name = "pallet-session" version = "3.0.0" dependencies = [ + "frame-accounts", "frame-support", "frame-system", "impl-trait-for-tuples", diff --git a/frame/accounts/src/lib.rs b/frame/accounts/src/lib.rs index b83f724d3d836..1bb5f96a397aa 100644 --- a/frame/accounts/src/lib.rs +++ b/frame/accounts/src/lib.rs @@ -22,8 +22,11 @@ pub struct AccountInfo { /// cannot be reaped until this is zero. pub consumers: RefCount, /// The number of other modules that allow this account to exist. The account may not be reaped - /// until this is zero. + /// until this and `sufficients` are both zero. pub providers: RefCount, + /// The number of modules that allow this account to exist for their own purposes only. The + /// account may not be reaped until this and `providers` are both zero. + pub sufficients: RefCount, /// The additional data that belongs to this account. Used to store the balance(s) in a lot of /// chains. pub data: AccountData, @@ -100,6 +103,7 @@ pub mod pallet { /// Event for the Accounts pallet. #[pallet::event] + #[pallet::generate_deposit(pub(super) fn deposit_event)] #[pallet::metadata(T::AccountId = "AccountId")] pub enum Event { /// A new \[account\] was created. @@ -130,7 +134,7 @@ pub mod pallet { impl Pallet {} impl AccountApi for Pallet { - type AccountData = T::AccountData; + type AccountInfo = AccountInfo::AccountData>; /// Return whether an account exists in storage. fn account_exists(who: &T::AccountId) -> bool { @@ -138,7 +142,7 @@ pub mod pallet { } /// Return the data for an account - fn get(who: &T::AccountId) -> Self::AccountData { + fn get(who: &T::AccountId) -> Self::AccountInfo { Account::::get(who) } @@ -166,14 +170,16 @@ pub mod pallet { Self::deposit_event(Event::KilledAccount(who)); } - /// Increment the reference counter on an account. - /// - /// The account `who`'s `providers` must be non-zero or this will return an error. + pub fn account_exists(who: &T::AccountId) -> bool { + Account::::contains_key(who) + } + + /// Increment the provider reference counter on an account. pub fn inc_providers(who: &T::AccountId) -> IncRefStatus { - Account::::mutate(who, |a| if a.providers == 0 { + Account::::mutate(who, |a| if a.providers == 0 && a.sufficients == 0 { // Account is being created. a.providers = 1; - frame_system::Module::::on_created_account(who.clone(), a); + Self::on_created_account(who.clone(), a); IncRefStatus::Created } else { a.providers = a.providers.saturating_add(1); @@ -181,30 +187,34 @@ pub mod pallet { }) } - /// Decrement the reference counter on an account. This *MUST* only be done once for every time - /// you called `inc_consumers` on `who`. + /// Decrement the provider reference counter on an account. + /// + /// This *MUST* only be done once for every time you called `inc_providers` on `who`. pub fn dec_providers(who: &T::AccountId) -> Result { Account::::try_mutate_exists(who, |maybe_account| { if let Some(mut account) = maybe_account.take() { - match (account.providers, account.consumers) { - (0, _) => { - // Logic error - cannot decrement beyond zero and no item should - // exist with zero providers. - log::error!( - target: "runtime::system", - "Logic error: Unexpected underflow in reducing provider", - ); - Ok(DecRefStatus::Reaped) - }, - (1, 0) => { - frame_system::Module::::on_killed_account(who.clone()); + if account.providers == 0 { + // Logic error - cannot decrement beyond zero. + log::error!( + target: "runtime::system", + "Logic error: Unexpected underflow in reducing provider", + ); + account.providers = 1; + } + match (account.providers, account.consumers, account.sufficients) { + (1, 0, 0) => { + // No providers left (and no consumers) and no sufficients. Account dead. + + Module::::on_killed_account(who.clone()); Ok(DecRefStatus::Reaped) } - (1, _) => { + (1, c, _) if c > 0 => { // Cannot remove last provider if there are consumers. Err(DecRefError::ConsumerRemaining) } - (x, _) => { + (x, _, _) => { + // Account will continue to exist as there is either > 1 provider or + // > 0 sufficients. account.providers = x - 1; *maybe_account = Some(account); Ok(DecRefStatus::Exists) @@ -220,11 +230,69 @@ pub mod pallet { }) } - /// The number of outstanding references for the account `who`. + /// Increment the self-sufficient reference counter on an account. + pub fn inc_sufficients(who: &T::AccountId) -> IncRefStatus { + Account::::mutate(who, |a| if a.providers + a.sufficients == 0 { + // Account is being created. + a.sufficients = 1; + Self::on_created_account(who.clone(), a); + IncRefStatus::Created + } else { + a.sufficients = a.sufficients.saturating_add(1); + IncRefStatus::Existed + }) + } + + /// Decrement the sufficients reference counter on an account. + /// + /// This *MUST* only be done once for every time you called `inc_sufficients` on `who`. + pub fn dec_sufficients(who: &T::AccountId) -> DecRefStatus { + Account::::mutate_exists(who, |maybe_account| { + if let Some(mut account) = maybe_account.take() { + if account.sufficients == 0 { + // Logic error - cannot decrement beyond zero. + log::error!( + target: "runtime::system", + "Logic error: Unexpected underflow in reducing sufficients", + ); + } + match (account.sufficients, account.providers) { + (0, 0) | (1, 0) => { + Module::::on_killed_account(who.clone()); + DecRefStatus::Reaped + } + (x, _) => { + account.sufficients = x - 1; + *maybe_account = Some(account); + DecRefStatus::Exists + } + } + } else { + log::error!( + target: "runtime::system", + "Logic error: Account already dead when reducing provider", + ); + DecRefStatus::Reaped + } + }) + } + + /// The number of outstanding provider references for the account `who`. pub fn providers(who: &T::AccountId) -> RefCount { Account::::get(who).providers } + /// The number of outstanding sufficient references for the account `who`. + pub fn sufficients(who: &T::AccountId) -> RefCount { + Account::::get(who).sufficients + } + + /// The number of outstanding provider and sufficient references for the account `who`. + pub fn reference_count(who: &T::AccountId) -> RefCount { + let a = Account::::get(who); + a.providers + a.sufficients + } + /// Increment the reference counter on an account. /// /// The account `who`'s `providers` must be non-zero or this will return an error. @@ -260,13 +328,10 @@ pub mod pallet { Account::::get(who).consumers != 0 } - - pub fn insert(who: T::AccountId, account: T::AccountData) { - Account::::insert(who, account) - } } } + /// Event handler which registers a provider when created. pub struct Provider(PhantomData); impl HandleLifetime for Provider { @@ -283,6 +348,19 @@ impl HandleLifetime for Provider { } } +/// Event handler which registers a self-sufficient when created. +pub struct SelfSufficient(PhantomData); +impl HandleLifetime for SelfSufficient { + fn created(t: &T::AccountId) -> Result<(), StoredMapError> { + Module::::inc_sufficients(t); + Ok(()) + } + fn killed(t: &T::AccountId) -> Result<(), StoredMapError> { + Module::::dec_sufficients(t); + Ok(()) + } +} + /// Event handler which registers a consumer when created. pub struct Consumer(PhantomData); impl HandleLifetime for Consumer { @@ -299,6 +377,7 @@ impl HandleLifetime for Consumer { } + /// Implement StoredMap for a simple single-item, provide-when-not-default system. This works fine /// for storing a single item which allows the account to continue existing as long as it's not /// empty/default. diff --git a/frame/session/Cargo.toml b/frame/session/Cargo.toml index 52b8ebbdf4780..817ece0645845 100644 --- a/frame/session/Cargo.toml +++ b/frame/session/Cargo.toml @@ -23,6 +23,7 @@ sp-session = { version = "3.0.0", default-features = false, path = "../../primit sp-staking = { version = "3.0.0", default-features = false, path = "../../primitives/staking" } frame-support = { version = "3.0.0", default-features = false, path = "../support" } frame-system = { version = "3.0.0", default-features = false, path = "../system" } +frame-accounts = { version = "3.0.0", default-features = false, path = "../accounts" } pallet-timestamp = { version = "3.0.0", default-features = false, path = "../timestamp" } sp-trie = { version = "3.0.0", optional = true, default-features = false, path = "../../primitives/trie" } impl-trait-for-tuples = "0.2.1" @@ -40,6 +41,8 @@ std = [ "sp-std/std", "sp-io/std", "frame-support/std", + "frame-system/std", + "frame-accounts/std", "sp-core/std", "sp-runtime/std", "sp-session/std", diff --git a/frame/session/src/lib.rs b/frame/session/src/lib.rs index d95d99389f732..fb2e21a2f135a 100644 --- a/frame/session/src/lib.rs +++ b/frame/session/src/lib.rs @@ -329,7 +329,7 @@ impl ValidatorRegistration for Module { } } -pub trait Config: frame_system::Config { +pub trait Config: frame_system::Config + frame_accounts::Config { /// The overarching event type. type Event: From + Into<::Event>; @@ -415,7 +415,7 @@ decl_storage! { for (account, val, keys) in config.keys.iter().cloned() { >::inner_set_keys(&val, keys) .expect("genesis config must not contain duplicates; qed"); - assert!(frame_system::Module::::inc_consumers(&account).is_ok()); + assert!(frame_accounts::Module::::inc_consumers(&account).is_ok()); } let initial_validators_0 = T::SessionManager::new_session(0) @@ -719,10 +719,10 @@ impl Module { let who = T::ValidatorIdOf::convert(account.clone()) .ok_or(Error::::NoAssociatedValidatorId)?; - frame_system::Module::::inc_consumers(&account).map_err(|_| Error::::NoAccount)?; + frame_accounts::Module::::inc_consumers(&account).map_err(|_| Error::::NoAccount)?; let old_keys = Self::inner_set_keys(&who, keys)?; if old_keys.is_some() { - let _ = frame_system::Module::::dec_consumers(&account); + let _ = frame_accounts::Module::::dec_consumers(&account); // ^^^ Defensive only; Consumers were incremented just before, so should never fail. } @@ -771,7 +771,7 @@ impl Module { let key_data = old_keys.get_raw(*id); Self::clear_key_owner(*id, key_data); } - frame_system::Module::::dec_consumers(&account); + frame_accounts::Module::::dec_consumers(&account); Ok(()) } diff --git a/frame/support/src/traits.rs b/frame/support/src/traits.rs index f1f3301fb770d..c4ce565f8ae23 100644 --- a/frame/support/src/traits.rs +++ b/frame/support/src/traits.rs @@ -2245,13 +2245,13 @@ pub trait ExecuteBlock { /// A minimal API for creating an account system compatible with FRAME System. pub trait AccountApi { - type AccountData; + type AccountInfo; /// Return whether an account exists in storage. fn account_exists(who: &AccountId) -> bool; /// Return the data for an account. - fn get(who: &AccountId) -> Self::AccountData; + fn get(who: &AccountId) -> Self::AccountInfo; /// Retrieve the account transaction counter from storage. fn account_nonce(who: AccountId) -> Index; diff --git a/test-utils/runtime/src/lib.rs b/test-utils/runtime/src/lib.rs index 76a330312b46b..9a1f331ae1ef2 100644 --- a/test-utils/runtime/src/lib.rs +++ b/test-utils/runtime/src/lib.rs @@ -448,6 +448,12 @@ impl From> for Event { } } +impl From> for Event { + fn from(_evt: frame_accounts::Event) -> Self { + unimplemented!("Not required in tests!") + } +} + impl frame_support::traits::PalletInfo for Runtime { fn index() -> Option { let type_id = sp_std::any::TypeId::of::

(); @@ -493,7 +499,10 @@ parameter_types! { } impl frame_accounts::Config for Runtime { + type Event = Event; type AccountData = (); + type OnNewAccount = (); + type OnKilledAccount = (); } impl frame_system::Config for Runtime { @@ -514,9 +523,6 @@ impl frame_system::Config for Runtime { type DbWeight = (); type Version = (); type PalletInfo = Self; - type AccountData = (); - type OnNewAccount = (); - type OnKilledAccount = (); type SystemWeightInfo = (); type SS58Prefix = (); type AccountStorage = frame_accounts::Pallet::; From f0b52de620ce31f54ef52ecd1972678769e93acb Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Wed, 3 Mar 2021 22:20:52 -0400 Subject: [PATCH 04/31] updates --- Cargo.lock | 25 ++++++++--------- Cargo.toml | 2 +- bin/node-template/runtime/Cargo.toml | 4 +-- bin/node-template/runtime/src/lib.rs | 22 +++++++-------- bin/node/runtime/Cargo.toml | 4 +-- bin/node/runtime/src/lib.rs | 2 +- .../rpc/runtime-api/Cargo.toml | 4 +-- .../rpc/runtime-api/README.md | 6 ++--- .../rpc/runtime-api/src/lib.rs | 4 +-- frame/balances/Cargo.toml | 2 ++ frame/balances/src/lib.rs | 9 +++---- frame/system/src/lib.rs | 27 +++---------------- frame/system/src/offchain.rs | 2 +- test-utils/runtime/Cargo.toml | 4 +-- test-utils/runtime/src/lib.rs | 4 +-- utils/frame/rpc/system/Cargo.toml | 2 +- utils/frame/rpc/system/src/lib.rs | 2 +- 17 files changed, 54 insertions(+), 71 deletions(-) rename frame/{system => accounts}/rpc/runtime-api/Cargo.toml (82%) rename frame/{system => accounts}/rpc/runtime-api/README.md (50%) rename frame/{system => accounts}/rpc/runtime-api/src/lib.rs (92%) diff --git a/Cargo.lock b/Cargo.lock index 6cc920e8c8a0c..0c02ab48e8078 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1701,6 +1701,14 @@ dependencies = [ "substrate-test-runtime-client", ] +[[package]] +name = "frame-accounts-rpc-runtime-api" +version = "3.0.0" +dependencies = [ + "parity-scale-codec", + "sp-api", +] + [[package]] name = "frame-benchmarking" version = "3.1.0" @@ -1887,14 +1895,6 @@ dependencies = [ "sp-std", ] -[[package]] -name = "frame-system-rpc-runtime-api" -version = "3.0.0" -dependencies = [ - "parity-scale-codec", - "sp-api", -] - [[package]] name = "frame-try-runtime" version = "0.9.0" @@ -4112,12 +4112,12 @@ dependencies = [ name = "node-runtime" version = "2.0.1" dependencies = [ + "frame-accounts-rpc-runtime-api", "frame-benchmarking", "frame-executive", "frame-support", "frame-system", "frame-system-benchmarking", - "frame-system-rpc-runtime-api", "frame-try-runtime", "hex-literal", "log", @@ -4226,12 +4226,12 @@ name = "node-template-runtime" version = "2.0.0" dependencies = [ "frame-accounts", + "frame-accounts-rpc-runtime-api", "frame-benchmarking", "frame-executive", "frame-support", "frame-system", "frame-system-benchmarking", - "frame-system-rpc-runtime-api", "hex-literal", "pallet-aura", "pallet-balances", @@ -4575,6 +4575,7 @@ dependencies = [ name = "pallet-balances" version = "3.0.0" dependencies = [ + "frame-accounts", "frame-benchmarking", "frame-support", "frame-system", @@ -9241,7 +9242,7 @@ dependencies = [ name = "substrate-frame-rpc-system" version = "3.0.0" dependencies = [ - "frame-system-rpc-runtime-api", + "frame-accounts-rpc-runtime-api", "futures 0.3.12", "jsonrpc-core", "jsonrpc-core-client", @@ -9307,9 +9308,9 @@ version = "2.0.0" dependencies = [ "cfg-if 1.0.0", "frame-accounts", + "frame-accounts-rpc-runtime-api", "frame-support", "frame-system", - "frame-system-rpc-runtime-api", "log", "memory-db", "pallet-babe", diff --git a/Cargo.toml b/Cargo.toml index 417e40ba574e0..e6b6be19602bd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -60,6 +60,7 @@ members = [ "client/transaction-pool", "client/transaction-pool/graph", "frame/accounts", + "frame/accounts/rpc/runtime-api", "frame/assets", "frame/atomic-swap", "frame/aura", @@ -114,7 +115,6 @@ members = [ "frame/support/test", "frame/system", "frame/system/benchmarking", - "frame/system/rpc/runtime-api", "frame/timestamp", "frame/transaction-payment", "frame/transaction-payment/rpc", diff --git a/bin/node-template/runtime/Cargo.toml b/bin/node-template/runtime/Cargo.toml index 5e96a63f80f7e..a4ac5f6ac8be8 100644 --- a/bin/node-template/runtime/Cargo.toml +++ b/bin/node-template/runtime/Cargo.toml @@ -38,7 +38,7 @@ sp-transaction-pool = { version = "3.0.0", default-features = false, path = "../ sp-version = { version = "3.0.0", default-features = false, path = "../../../primitives/version" } # Used for the node template's RPCs -frame-system-rpc-runtime-api = { version = "3.0.0", default-features = false, path = "../../../frame/system/rpc/runtime-api/" } +frame-accounts-rpc-runtime-api = { version = "3.0.0", default-features = false, path = "../../../frame/accounts/rpc/runtime-api/" } pallet-transaction-payment-rpc-runtime-api = { version = "3.0.0", default-features = false, path = "../../../frame/transaction-payment/rpc/runtime-api/" } # Used for runtime benchmarking @@ -78,7 +78,7 @@ std = [ "sp-transaction-pool/std", "sp-version/std", "frame-system/std", - "frame-system-rpc-runtime-api/std", + "frame-accounts-rpc-runtime-api/std", "template/std", ] runtime-benchmarks = [ diff --git a/bin/node-template/runtime/src/lib.rs b/bin/node-template/runtime/src/lib.rs index 2a5f441f7fa4d..e04cd6a8b6a61 100644 --- a/bin/node-template/runtime/src/lib.rs +++ b/bin/node-template/runtime/src/lib.rs @@ -140,7 +140,6 @@ parameter_types! { } // Configure FRAME pallets to include in runtime. - impl frame_system::Config for Runtime { /// The basic call filter to use in dispatchable. type BaseCallFilter = (); @@ -178,12 +177,6 @@ impl frame_system::Config for Runtime { /// /// This type is being generated by `construct_runtime!`. type PalletInfo = PalletInfo; - /// What to do if a new account is created. - type OnNewAccount = (); - /// What to do if an account is fully reaped from the system. - type OnKilledAccount = (); - /// The data to be stored in an account. - type AccountData = pallet_balances::AccountData; /// Weight information for the extrinsics of this pallet. type SystemWeightInfo = (); /// This is used as an identifier of the chain. 42 is the generic substrate prefix. @@ -193,6 +186,12 @@ impl frame_system::Config for Runtime { } impl frame_accounts::Config for Runtime { + type Event = Event; + /// What to do if a new account is created. + type OnNewAccount = (); + /// What to do if an account is fully reaped from the system. + type OnKilledAccount = (); + /// The data to be stored in an account. type AccountData = pallet_balances::AccountData; } @@ -244,7 +243,7 @@ impl pallet_balances::Config for Runtime { type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; + type AccountStore = Accounts; type WeightInfo = pallet_balances::weights::SubstrateWeight; } @@ -277,7 +276,7 @@ construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic { System: frame_system::{Module, Call, Config, Storage, Event}, - Accounts: frame_accounts::{Module, Call, Storage}, + Accounts: frame_accounts::{Module, Call, Storage, Event}, RandomnessCollectiveFlip: pallet_randomness_collective_flip::{Module, Call, Storage}, Timestamp: pallet_timestamp::{Module, Call, Storage, Inherent}, Aura: pallet_aura::{Module, Config}, @@ -432,9 +431,10 @@ impl_runtime_apis! { } } - impl frame_system_rpc_runtime_api::AccountNonceApi for Runtime { + impl frame_accounts_rpc_runtime_api::AccountNonceApi for Runtime { fn account_nonce(account: AccountId) -> Index { - System::account_nonce(account) + use frame_support::traits::AccountApi; + Accounts::account_nonce(account) } } diff --git a/bin/node/runtime/Cargo.toml b/bin/node/runtime/Cargo.toml index dc8ce8bace809..2f10e3c1bb420 100644 --- a/bin/node/runtime/Cargo.toml +++ b/bin/node/runtime/Cargo.toml @@ -43,7 +43,7 @@ frame-benchmarking = { version = "3.1.0", default-features = false, path = "../. frame-support = { version = "3.0.0", default-features = false, path = "../../../frame/support" } frame-system = { version = "3.0.0", default-features = false, path = "../../../frame/system" } frame-system-benchmarking = { version = "3.0.0", default-features = false, path = "../../../frame/system/benchmarking", optional = true } -frame-system-rpc-runtime-api = { version = "3.0.0", default-features = false, path = "../../../frame/system/rpc/runtime-api/" } +frame-accounts-rpc-runtime-api = { version = "3.0.0", default-features = false, path = "../../../frame/accounts/rpc/runtime-api/" } frame-try-runtime = { version = "0.9.0", default-features = false, path = "../../../frame/try-runtime", optional = true } pallet-assets = { version = "3.0.0", default-features = false, path = "../../../frame/assets" } pallet-authority-discovery = { version = "3.0.0", default-features = false, path = "../../../frame/authority-discovery" } @@ -143,7 +143,7 @@ std = [ "pallet-sudo/std", "frame-support/std", "frame-benchmarking/std", - "frame-system-rpc-runtime-api/std", + "frame-accounts-rpc-runtime-api/std", "frame-system/std", "pallet-election-provider-multi-phase/std", "pallet-timestamp/std", diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 20abb9b54ff08..f0ef84dab61e8 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -1321,7 +1321,7 @@ impl_runtime_apis! { } } - impl frame_system_rpc_runtime_api::AccountNonceApi for Runtime { + impl frame_accounts_rpc_runtime_api::AccountNonceApi for Runtime { fn account_nonce(account: AccountId) -> Index { System::account_nonce(account) } diff --git a/frame/system/rpc/runtime-api/Cargo.toml b/frame/accounts/rpc/runtime-api/Cargo.toml similarity index 82% rename from frame/system/rpc/runtime-api/Cargo.toml rename to frame/accounts/rpc/runtime-api/Cargo.toml index 56619d59ddcad..d7313db74729f 100644 --- a/frame/system/rpc/runtime-api/Cargo.toml +++ b/frame/accounts/rpc/runtime-api/Cargo.toml @@ -1,12 +1,12 @@ [package] -name = "frame-system-rpc-runtime-api" +name = "frame-accounts-rpc-runtime-api" version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" homepage = "https://substrate.dev" repository = "https://github.com/paritytech/substrate/" -description = "Runtime API definition required by System RPC extensions." +description = "Runtime API definition required by Accounts RPC extensions." readme = "README.md" [package.metadata.docs.rs] diff --git a/frame/system/rpc/runtime-api/README.md b/frame/accounts/rpc/runtime-api/README.md similarity index 50% rename from frame/system/rpc/runtime-api/README.md rename to frame/accounts/rpc/runtime-api/README.md index ab46c22a8be33..4adfb89c0cdbc 100644 --- a/frame/system/rpc/runtime-api/README.md +++ b/frame/accounts/rpc/runtime-api/README.md @@ -1,7 +1,7 @@ -Runtime API definition required by System RPC extensions. +Runtime API definition required by Accounts RPC extensions. This API should be imported and implemented by the runtime, of a node that wants to use the custom RPC extension -adding System access methods. +adding Accounts access methods. -License: Apache-2.0 \ No newline at end of file +License: Apache-2.0 diff --git a/frame/system/rpc/runtime-api/src/lib.rs b/frame/accounts/rpc/runtime-api/src/lib.rs similarity index 92% rename from frame/system/rpc/runtime-api/src/lib.rs rename to frame/accounts/rpc/runtime-api/src/lib.rs index 319883c36d748..e04c6cea03b10 100644 --- a/frame/system/rpc/runtime-api/src/lib.rs +++ b/frame/accounts/rpc/runtime-api/src/lib.rs @@ -15,11 +15,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -//! Runtime API definition required by System RPC extensions. +//! Runtime API definition required by Accounts RPC extensions. //! //! This API should be imported and implemented by the runtime, //! of a node that wants to use the custom RPC extension -//! adding System access methods. +//! adding Accounts access methods. #![cfg_attr(not(feature = "std"), no_std)] diff --git a/frame/balances/Cargo.toml b/frame/balances/Cargo.toml index 22c4ef0976f5f..9e5cfe4259391 100644 --- a/frame/balances/Cargo.toml +++ b/frame/balances/Cargo.toml @@ -20,6 +20,7 @@ sp-runtime = { version = "3.0.0", default-features = false, path = "../../primit frame-benchmarking = { version = "3.1.0", default-features = false, path = "../benchmarking", optional = true } frame-support = { version = "3.0.0", default-features = false, path = "../support" } frame-system = { version = "3.0.0", default-features = false, path = "../system" } +frame-accounts = { version = "3.0.0", default-features = false, path = "../accounts" } log = { version = "0.4.14", default-features = false } [dev-dependencies] @@ -37,6 +38,7 @@ std = [ "frame-benchmarking/std", "frame-support/std", "frame-system/std", + "frame-accounts/std", "log/std", ] runtime-benchmarks = ["frame-benchmarking"] diff --git a/frame/balances/src/lib.rs b/frame/balances/src/lib.rs index cc7b6351c2584..285a996aa5017 100644 --- a/frame/balances/src/lib.rs +++ b/frame/balances/src/lib.rs @@ -176,7 +176,6 @@ use sp_runtime::{ MaybeSerializeDeserialize, Saturating, Bounded, StoredMapError, }, }; -use frame_system as system; pub use self::imbalances::{PositiveImbalance, NegativeImbalance}; pub use weights::WeightInfo; @@ -189,7 +188,7 @@ pub mod pallet { use super::*; #[pallet::config] - pub trait Config: frame_system::Config { + pub trait Config: frame_system::Config + frame_accounts::Config { /// The balance of an account. type Balance: Parameter + Member + AtLeast32BitUnsigned + Codec + Default + Copy + MaybeSerializeDeserialize + Debug; @@ -782,12 +781,12 @@ impl, I: 'static> Pallet { if existed { // TODO: use Locks::::hashed_key // https://github.com/paritytech/substrate/issues/4969 - system::Pallet::::dec_consumers(who); + frame_accounts::Pallet::::dec_consumers(who); } } else { Locks::::insert(who, locks); if !existed { - if system::Pallet::::inc_consumers(who).is_err() { + if frame_accounts::Pallet::::inc_consumers(who).is_err() { // No providers for the locks. This is impossible under normal circumstances // since the funds that are under the lock will themselves be stored in the // account and therefore will need a reference. @@ -1070,7 +1069,7 @@ impl, I: 'static> Currency for Pallet where // TODO: This is over-conservative. There may now be other providers, and this pallet // may not even be a provider. let allow_death = existence_requirement == ExistenceRequirement::AllowDeath; - let allow_death = allow_death && !system::Pallet::::is_provider_required(transactor); + let allow_death = allow_death && !frame_accounts::Pallet::::is_provider_required(transactor); ensure!(allow_death || from_account.total() >= ed, Error::::KeepAlive); Ok(()) diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index 854126675d8b2..2efb6f6607244 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -78,7 +78,7 @@ use sp_runtime::{ self, CheckEqual, AtLeast32Bit, Zero, Lookup, LookupError, SimpleBitOps, Hash, Member, MaybeDisplay, BadOrigin, MaybeSerializeDeserialize, MaybeMallocSizeOf, StaticLookup, One, Bounded, - Dispatchable, AtLeast32BitUnsigned, Saturating, StoredMapError, + Dispatchable, AtLeast32BitUnsigned, Saturating, }, offchain::storage_lock::BlockNumberProvider, }; @@ -87,8 +87,8 @@ use sp_core::{ChangesTrieConfiguration, storage::well_known_keys}; use frame_support::{ Parameter, storage, traits::{ - Contains, Get, PalletInfo, OnNewAccount, OnKilledAccount, HandleLifetime, - StoredMap, EnsureOrigin, OriginTrait, Filter, AccountApi, + Contains, Get, PalletInfo, + EnsureOrigin, OriginTrait, Filter, AccountApi, }, weights::{ Weight, RuntimeDbWeight, DispatchInfo, DispatchClass, @@ -96,7 +96,7 @@ use frame_support::{ }, dispatch::DispatchResultWithPostInfo, }; -use codec::{Encode, Decode, FullCodec, EncodeLike}; +use codec::{Encode, Decode}; #[cfg(feature = "std")] use frame_support::traits::GenesisBuild; @@ -708,25 +708,6 @@ type EventIndex = u32; /// Type used to encode the number of references an account has. pub type RefCount = u32; -/// Information of an account. -#[derive(Clone, Eq, PartialEq, Default, RuntimeDebug, Encode, Decode)] -pub struct AccountInfo { - /// The number of transactions this account has sent. - pub nonce: Index, - /// The number of other modules that currently depend on this account's existence. The account - /// cannot be reaped until this is zero. - pub consumers: RefCount, - /// The number of other modules that allow this account to exist. The account may not be reaped - /// until this and `sufficients` are both zero. - pub providers: RefCount, - /// The number of modules that allow this account to exist for their own purposes only. The - /// account may not be reaped until this and `providers` are both zero. - pub sufficients: RefCount, - /// The additional data that belongs to this account. Used to store the balance(s) in a lot of - /// chains. - pub data: AccountData, -} - /// Stores the `spec_version` and `spec_name` of when the last runtime upgrade /// happened. #[derive(sp_runtime::RuntimeDebug, Encode, Decode)] diff --git a/frame/system/src/offchain.rs b/frame/system/src/offchain.rs index f9231e0ac4cea..73a02e10e72c3 100644 --- a/frame/system/src/offchain.rs +++ b/frame/system/src/offchain.rs @@ -62,7 +62,7 @@ use sp_std::collections::btree_set::BTreeSet; use sp_std::convert::{TryInto, TryFrom}; use sp_std::prelude::{Box, Vec}; use sp_runtime::app_crypto::RuntimeAppPublic; -use sp_runtime::traits::{Extrinsic as ExtrinsicT, IdentifyAccount, One}; +use sp_runtime::traits::{Extrinsic as ExtrinsicT, IdentifyAccount}; use frame_support::{RuntimeDebug, traits::AccountApi}; /// Marker struct used to flag using all supported keys to sign a payload. diff --git a/test-utils/runtime/Cargo.toml b/test-utils/runtime/Cargo.toml index 06fdff3380af6..28dd886e8fddb 100644 --- a/test-utils/runtime/Cargo.toml +++ b/test-utils/runtime/Cargo.toml @@ -34,7 +34,7 @@ sp-runtime = { version = "3.0.0", default-features = false, path = "../../primit pallet-babe = { version = "3.0.0", default-features = false, path = "../../frame/babe" } frame-accounts = { version = "3.0.0", default-features = false, path = "../../frame/accounts" } frame-system = { version = "3.0.0", default-features = false, path = "../../frame/system" } -frame-system-rpc-runtime-api = { version = "3.0.0", default-features = false, path = "../../frame/system/rpc/runtime-api" } +frame-accounts-rpc-runtime-api = { version = "3.0.0", default-features = false, path = "../../frame/accounts/rpc/runtime-api" } pallet-timestamp = { version = "3.0.0", default-features = false, path = "../../frame/timestamp" } sp-finality-grandpa = { version = "3.0.0", default-features = false, path = "../../primitives/finality-grandpa" } sp-trie = { version = "3.0.0", default-features = false, path = "../../primitives/trie" } @@ -87,7 +87,7 @@ std = [ "sp-externalities/std", "sp-state-machine/std", "pallet-babe/std", - "frame-system-rpc-runtime-api/std", + "frame-accounts-rpc-runtime-api/std", "frame-system/std", "frame-accounts/std", "pallet-timestamp/std", diff --git a/test-utils/runtime/src/lib.rs b/test-utils/runtime/src/lib.rs index 9a1f331ae1ef2..dcc74ea669e34 100644 --- a/test-utils/runtime/src/lib.rs +++ b/test-utils/runtime/src/lib.rs @@ -866,7 +866,7 @@ cfg_if! { } } - impl frame_system_rpc_runtime_api::AccountNonceApi for Runtime { + impl frame_accounts_rpc_runtime_api::AccountNonceApi for Runtime { fn account_nonce(_account: AccountId) -> Index { 0 } @@ -1101,7 +1101,7 @@ cfg_if! { } } - impl frame_system_rpc_runtime_api::AccountNonceApi for Runtime { + impl frame_accounts_rpc_runtime_api::AccountNonceApi for Runtime { fn account_nonce(_account: AccountId) -> Index { 0 } diff --git a/utils/frame/rpc/system/Cargo.toml b/utils/frame/rpc/system/Cargo.toml index ea8d97a82ad34..2e2ce63ece4f2 100644 --- a/utils/frame/rpc/system/Cargo.toml +++ b/utils/frame/rpc/system/Cargo.toml @@ -23,7 +23,7 @@ log = "0.4.8" serde = { version = "1.0.101", features = ["derive"] } sp-runtime = { version = "3.0.0", path = "../../../../primitives/runtime" } sp-api = { version = "3.0.0", path = "../../../../primitives/api" } -frame-system-rpc-runtime-api = { version = "3.0.0", path = "../../../../frame/system/rpc/runtime-api" } +frame-accounts-rpc-runtime-api = { version = "3.0.0", path = "../../../../frame/accounts/rpc/runtime-api" } sp-core = { version = "3.0.0", path = "../../../../primitives/core" } sp-blockchain = { version = "3.0.0", path = "../../../../primitives/blockchain" } sp-transaction-pool = { version = "3.0.0", path = "../../../../primitives/transaction-pool" } diff --git a/utils/frame/rpc/system/src/lib.rs b/utils/frame/rpc/system/src/lib.rs index 57c0cda9cca3a..8442db03e593d 100644 --- a/utils/frame/rpc/system/src/lib.rs +++ b/utils/frame/rpc/system/src/lib.rs @@ -40,7 +40,7 @@ use sp_transaction_pool::{TransactionPool, InPoolTransaction}; use sp_block_builder::BlockBuilder; use sc_rpc_api::DenyUnsafe; -pub use frame_system_rpc_runtime_api::AccountNonceApi; +pub use frame_accounts_rpc_runtime_api::AccountNonceApi; pub use self::gen_client::Client as SystemClient; /// Future that resolves to account nonce. From 6dc817f7939c3fa76158d876b45fe6be1cb40b0c Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Wed, 3 Mar 2021 22:42:47 -0400 Subject: [PATCH 05/31] more fixes --- Cargo.lock | 4 ++++ bin/node/runtime/Cargo.toml | 1 + bin/node/runtime/src/lib.rs | 15 +++++++++++---- frame/assets/Cargo.toml | 1 + frame/assets/src/lib.rs | 12 ++++++------ frame/recovery/Cargo.toml | 2 ++ frame/recovery/src/lib.rs | 6 +++--- frame/staking/Cargo.toml | 2 ++ frame/staking/src/lib.rs | 8 ++++---- 9 files changed, 34 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0c02ab48e8078..d31872811fb84 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4112,6 +4112,7 @@ dependencies = [ name = "node-runtime" version = "2.0.1" dependencies = [ + "frame-accounts", "frame-accounts-rpc-runtime-api", "frame-benchmarking", "frame-executive", @@ -4458,6 +4459,7 @@ dependencies = [ name = "pallet-assets" version = "3.0.0" dependencies = [ + "frame-accounts", "frame-benchmarking", "frame-support", "frame-system", @@ -5129,6 +5131,7 @@ name = "pallet-recovery" version = "3.0.0" dependencies = [ "enumflags2", + "frame-accounts", "frame-support", "frame-system", "pallet-balances", @@ -5237,6 +5240,7 @@ dependencies = [ name = "pallet-staking" version = "3.0.0" dependencies = [ + "frame-accounts", "frame-benchmarking", "frame-support", "frame-system", diff --git a/bin/node/runtime/Cargo.toml b/bin/node/runtime/Cargo.toml index 2f10e3c1bb420..ad68e19b3416e 100644 --- a/bin/node/runtime/Cargo.toml +++ b/bin/node/runtime/Cargo.toml @@ -38,6 +38,7 @@ sp-transaction-pool = { version = "3.0.0", default-features = false, path = "../ sp-version = { version = "3.0.0", default-features = false, path = "../../../primitives/version" } # frame dependencies +frame-accounts = { version = "3.0.0", default-features = false, path = "../../../frame/accounts" } frame-executive = { version = "3.0.0", default-features = false, path = "../../../frame/executive" } frame-benchmarking = { version = "3.1.0", default-features = false, path = "../../../frame/benchmarking", optional = true } frame-support = { version = "3.0.0", default-features = false, path = "../../../frame/support" } diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index f0ef84dab61e8..a8795e2e2ed60 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -201,11 +201,16 @@ impl frame_system::Config for Runtime { type BlockHashCount = BlockHashCount; type Version = Version; type PalletInfo = PalletInfo; + type SystemWeightInfo = frame_system::weights::SubstrateWeight; + type SS58Prefix = SS58Prefix; + type AccountStorage = Accounts; +} + +impl frame_accounts::Config for Runtime { + type Event = Event; type AccountData = pallet_balances::AccountData; type OnNewAccount = (); type OnKilledAccount = (); - type SystemWeightInfo = frame_system::weights::SubstrateWeight; - type SS58Prefix = SS58Prefix; } impl pallet_utility::Config for Runtime { @@ -372,7 +377,7 @@ impl pallet_balances::Config for Runtime { type DustRemoval = (); type Event = Event; type ExistentialDeposit = ExistentialDeposit; - type AccountStore = frame_system::Module; + type AccountStore = Accounts; type WeightInfo = pallet_balances::weights::SubstrateWeight; } @@ -1078,6 +1083,7 @@ construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: frame_accounts::{Module, Call, Storage, Event}, Utility: pallet_utility::{Module, Call, Event}, Babe: pallet_babe::{Module, Call, Storage, Config, ValidateUnsigned}, Timestamp: pallet_timestamp::{Module, Call, Storage, Inherent}, @@ -1323,7 +1329,8 @@ impl_runtime_apis! { impl frame_accounts_rpc_runtime_api::AccountNonceApi for Runtime { fn account_nonce(account: AccountId) -> Index { - System::account_nonce(account) + use frame_support::traits::AccountApi; + Accounts::account_nonce(account) } } diff --git a/frame/assets/Cargo.toml b/frame/assets/Cargo.toml index b62e8bac8ccc6..32c1f9a089e96 100644 --- a/frame/assets/Cargo.toml +++ b/frame/assets/Cargo.toml @@ -22,6 +22,7 @@ sp-runtime = { version = "3.0.0", default-features = false, path = "../../primit frame-support = { version = "3.0.0", default-features = false, path = "../support" } # `system` module provides us with all sorts of useful stuff and macros depend on it being around. frame-system = { version = "3.0.0", default-features = false, path = "../system" } +frame-accounts = { version = "3.0.0", default-features = false, path = "../accounts" } frame-benchmarking = { version = "3.1.0", default-features = false, path = "../benchmarking", optional = true } [dev-dependencies] diff --git a/frame/assets/src/lib.rs b/frame/assets/src/lib.rs index 7b04ea11bafed..0aeaedf51c244 100644 --- a/frame/assets/src/lib.rs +++ b/frame/assets/src/lib.rs @@ -147,7 +147,7 @@ pub mod pallet { #[pallet::config] /// The module configuration trait. - pub trait Config: frame_system::Config { + pub trait Config: frame_system::Config + frame_accounts::Config { /// The overarching event type. type Event: From> + IsType<::Event>; @@ -1098,8 +1098,8 @@ impl Pallet { d: &mut AssetDetails>, ) -> Result { let accounts = d.accounts.checked_add(1).ok_or(Error::::Overflow)?; - let r = Ok(if frame_system::Module::::account_exists(who) { - frame_system::Module::::inc_consumers(who).map_err(|_| Error::::BadState)?; + let r = Ok(if frame_accounts::Module::::account_exists(who) { + frame_accounts::Module::::inc_consumers(who).map_err(|_| Error::::BadState)?; false } else { ensure!(d.zombies < d.max_zombies, Error::::TooManyZombies); @@ -1116,10 +1116,10 @@ impl Pallet { d: &mut AssetDetails>, is_zombie: &mut bool, ) { - if *is_zombie && frame_system::Module::::account_exists(who) { + if *is_zombie && frame_accounts::Module::::account_exists(who) { // If the account exists, then it should have at least one provider // so this cannot fail... but being defensive anyway. - let _ = frame_system::Module::::inc_consumers(who); + let _ = frame_accounts::Module::::inc_consumers(who); *is_zombie = false; d.zombies = d.zombies.saturating_sub(1); } @@ -1133,7 +1133,7 @@ impl Pallet { if is_zombie { d.zombies = d.zombies.saturating_sub(1); } else { - frame_system::Module::::dec_consumers(who); + frame_accounts::Module::::dec_consumers(who); } d.accounts = d.accounts.saturating_sub(1); } diff --git a/frame/recovery/Cargo.toml b/frame/recovery/Cargo.toml index 1f8003bd4d056..ced2a03210bc9 100644 --- a/frame/recovery/Cargo.toml +++ b/frame/recovery/Cargo.toml @@ -21,6 +21,7 @@ sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/ sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } frame-support = { version = "3.0.0", default-features = false, path = "../support" } frame-system = { version = "3.0.0", default-features = false, path = "../system" } +frame-accounts = { version = "3.0.0", default-features = false, path = "../accounts" } [dev-dependencies] sp-core = { version = "3.0.0", path = "../../primitives/core" } @@ -36,5 +37,6 @@ std = [ "sp-runtime/std", "frame-support/std", "frame-system/std", + "frame-accounts/std", ] try-runtime = ["frame-support/try-runtime"] diff --git a/frame/recovery/src/lib.rs b/frame/recovery/src/lib.rs index 00cd6ff2a7f79..c9a604067dcd5 100644 --- a/frame/recovery/src/lib.rs +++ b/frame/recovery/src/lib.rs @@ -175,7 +175,7 @@ type BalanceOf = <::Currency as Currency<::AccountId>>::Balance; /// Configuration trait. -pub trait Config: frame_system::Config { +pub trait Config: frame_system::Config + frame_accounts::Config { /// The overarching event type. type Event: From> + Into<::Event>; @@ -588,7 +588,7 @@ decl_module! { recovery_config.threshold as usize <= active_recovery.friends.len(), Error::::Threshold ); - system::Module::::inc_consumers(&who).map_err(|_| Error::::BadState)?; + frame_accounts::Module::::inc_consumers(&who).map_err(|_| Error::::BadState)?; // Create the recovery storage item Proxy::::insert(&who, &account); Self::deposit_event(RawEvent::AccountRecovered(account, who)); @@ -677,7 +677,7 @@ decl_module! { // Check `who` is allowed to make a call on behalf of `account` ensure!(Self::proxy(&who) == Some(account), Error::::NotAllowed); Proxy::::remove(&who); - system::Module::::dec_consumers(&who); + frame_accounts::Module::::dec_consumers(&who); } } } diff --git a/frame/staking/Cargo.toml b/frame/staking/Cargo.toml index 1f9f29570a223..1d76b6f3cd58d 100644 --- a/frame/staking/Cargo.toml +++ b/frame/staking/Cargo.toml @@ -24,6 +24,7 @@ sp-runtime = { version = "3.0.0", default-features = false, path = "../../primit sp-staking = { version = "3.0.0", default-features = false, path = "../../primitives/staking" } frame-support = { version = "3.0.0", default-features = false, path = "../support" } frame-system = { version = "3.0.0", default-features = false, path = "../system" } +frame-accounts = { version = "3.0.0", default-features = false, path = "../accounts" } pallet-session = { version = "3.0.0", default-features = false, features = ["historical"], path = "../session" } pallet-authorship = { version = "3.0.0", default-features = false, path = "../authorship" } sp-application-crypto = { version = "3.0.0", default-features = false, path = "../../primitives/application-crypto" } @@ -61,6 +62,7 @@ std = [ "sp-staking/std", "pallet-session/std", "frame-system/std", + "frame-accounts/std", "pallet-authorship/std", "sp-application-crypto/std", "log/std", diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index ed8a2efbd45a4..afcdb59a0a30b 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -323,7 +323,7 @@ use sp_staking::{ #[cfg(feature = "std")] use sp_runtime::{Serialize, Deserialize}; use frame_system::{ - self as system, ensure_signed, ensure_root, ensure_none, + ensure_signed, ensure_root, ensure_none, offchain::SendTransactionTypes, }; use sp_npos_elections::{ @@ -780,7 +780,7 @@ impl SessionInterface<::AccountId> for T w } } -pub trait Config: frame_system::Config + SendTransactionTypes> { +pub trait Config: frame_system::Config + frame_accounts::Config + SendTransactionTypes> { /// The staking balance. type Currency: LockableCurrency; @@ -1500,7 +1500,7 @@ decl_module! { Err(Error::::InsufficientValue)? } - system::Module::::inc_consumers(&stash).map_err(|_| Error::::BadState)?; + frame_accounts::Module::::inc_consumers(&stash).map_err(|_| Error::::BadState)?; // You're auto-bonded forever, here. We might improve this by only bonding when // you actually validate/nominate and remove once you unbond __everything__. @@ -3202,7 +3202,7 @@ impl Module { >::remove(stash); >::remove(stash); - system::Module::::dec_consumers(stash); + frame_accounts::Module::::dec_consumers(stash); Ok(()) } From c9be89085458abb4f484cc72e56bad7d7a36de28 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Wed, 3 Mar 2021 23:11:07 -0400 Subject: [PATCH 06/31] expose hashed key for --- Cargo.lock | 1 + frame/accounts/src/lib.rs | 10 +++++++++- frame/benchmarking/src/lib.rs | 5 ++++- frame/benchmarking/src/utils.rs | 7 ++++++- frame/session/benchmarking/src/lib.rs | 8 +++----- frame/staking/fuzzer/Cargo.toml | 1 + frame/staking/fuzzer/src/mock.rs | 13 ++++++++++--- frame/support/src/traits.rs | 2 ++ 8 files changed, 36 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d31872811fb84..dbfa3f3facc0b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5273,6 +5273,7 @@ dependencies = [ name = "pallet-staking-fuzz" version = "0.0.0" dependencies = [ + "frame-accounts", "frame-support", "frame-system", "honggfuzz", diff --git a/frame/accounts/src/lib.rs b/frame/accounts/src/lib.rs index 1bb5f96a397aa..b93108932965c 100644 --- a/frame/accounts/src/lib.rs +++ b/frame/accounts/src/lib.rs @@ -1,6 +1,9 @@ #![cfg_attr(not(feature = "std"), no_std)] -use sp_std::marker::PhantomData; +use sp_std::{ + vec::Vec, + marker::PhantomData, +}; use sp_runtime::{ RuntimeDebug, traits::StoredMapError, @@ -155,6 +158,11 @@ pub mod pallet { fn inc_account_nonce(who: T::AccountId) { Account::::mutate(who, |a| a.nonce += T::Index::one()); } + + /// Return the storage key for an account. + fn hashed_key_for(who: T::AccountId) -> Vec { + Account::::hashed_key_for(who) + } } impl Pallet { diff --git a/frame/benchmarking/src/lib.rs b/frame/benchmarking/src/lib.rs index c2f60a5e13c41..0f974fa7a05a4 100644 --- a/frame/benchmarking/src/lib.rs +++ b/frame/benchmarking/src/lib.rs @@ -712,7 +712,10 @@ macro_rules! impl_benchmark { // Add whitelist to DB including whitelisted caller let mut whitelist = whitelist.to_vec(); let whitelisted_caller_key = - as $crate::frame_support::storage::StorageMap<_,_>>::hashed_key_for( + < + ::AccountStorage as + $crate::frame_support::traits::AccountApi + >::hashed_key_for( $crate::whitelisted_caller::() ); whitelist.push(whitelisted_caller_key.into()); diff --git a/frame/benchmarking/src/utils.rs b/frame/benchmarking/src/utils.rs index 1574e47454b59..25d45f8ed7ff4 100644 --- a/frame/benchmarking/src/utils.rs +++ b/frame/benchmarking/src/utils.rs @@ -225,7 +225,12 @@ pub fn whitelisted_caller() -> AccountId { macro_rules! whitelist_account { ($acc:ident) => { frame_benchmarking::benchmarking::add_to_whitelist( - frame_system::Account::::hashed_key_for(&$acc).into() + < + ::AccountStorage as + $crate::frame_support::traits::AccountApi + >::hashed_key_for( + $crate::whitelisted_caller::() + ).into() ); } } diff --git a/frame/session/benchmarking/src/lib.rs b/frame/session/benchmarking/src/lib.rs index 8546800ee4fdc..40f73ec6f22c1 100644 --- a/frame/session/benchmarking/src/lib.rs +++ b/frame/session/benchmarking/src/lib.rs @@ -25,7 +25,7 @@ mod mock; use sp_std::prelude::*; use sp_std::vec; -use frame_benchmarking::{benchmarks, impl_benchmark_test_suite}; +use frame_benchmarking::{benchmarks, impl_benchmark_test_suite, whitelist_account}; use frame_support::{ codec::Decode, storage::StorageValue, @@ -63,8 +63,7 @@ benchmarks! { let keys = T::Keys::default(); let proof: Vec = vec![0,1,2,3]; // Whitelist controller account from further DB operations. - let v_controller_key = frame_system::Account::::hashed_key_for(&v_controller); - frame_benchmarking::benchmarking::add_to_whitelist(v_controller_key.into()); + whitelist_account!(v_controller); }: _(RawOrigin::Signed(v_controller), keys, proof) purge_keys { @@ -80,8 +79,7 @@ benchmarks! { let proof: Vec = vec![0,1,2,3]; Session::::set_keys(RawOrigin::Signed(v_controller.clone()).into(), keys, proof)?; // Whitelist controller account from further DB operations. - let v_controller_key = frame_system::Account::::hashed_key_for(&v_controller); - frame_benchmarking::benchmarking::add_to_whitelist(v_controller_key.into()); + whitelist_account!(v_controller); }: _(RawOrigin::Signed(v_controller)) #[extra] diff --git a/frame/staking/fuzzer/Cargo.toml b/frame/staking/fuzzer/Cargo.toml index 84758c6bf65ce..7212c41f983a2 100644 --- a/frame/staking/fuzzer/Cargo.toml +++ b/frame/staking/fuzzer/Cargo.toml @@ -22,6 +22,7 @@ pallet-indices = { version = "3.0.0", path = "../../indices" } pallet-balances = { version = "3.0.0", path = "../../balances" } pallet-timestamp = { version = "3.0.0", path = "../../timestamp" } frame-system = { version = "3.0.0", path = "../../system" } +frame-accounts = { version = "3.0.0", path = "../../accounts" } frame-support = { version = "3.0.0", path = "../../support" } sp-std = { version = "3.0.0", path = "../../../primitives/std" } sp-io ={ version = "3.0.0", path = "../../../primitives/io" } diff --git a/frame/staking/fuzzer/src/mock.rs b/frame/staking/fuzzer/src/mock.rs index 05d001d23858e..073c28ef30400 100644 --- a/frame/staking/fuzzer/src/mock.rs +++ b/frame/staking/fuzzer/src/mock.rs @@ -34,6 +34,7 @@ frame_support::construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: frame_accounts::{Module, Call, Storage, Event}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, Staking: pallet_staking::{Module, Call, Config, Storage, Event, ValidateUnsigned}, Indices: pallet_indices::{Module, Call, Storage, Config, Event}, @@ -59,12 +60,18 @@ impl frame_system::Config for Test { type BlockHashCount = (); type Version = (); type PalletInfo = PalletInfo; + type SystemWeightInfo = (); + type SS58Prefix = (); + type AccountStorage = Accounts; +} + +impl frame_accounts::Config for Test { + type Event = Event; type AccountData = pallet_balances::AccountData; type OnNewAccount = (); type OnKilledAccount = (); - type SystemWeightInfo = (); - type SS58Prefix = (); } + parameter_types! { pub const ExistentialDeposit: Balance = 10; } @@ -74,7 +81,7 @@ impl pallet_balances::Config for Test { type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; + type AccountStore = Accounts; type WeightInfo = (); } impl pallet_indices::Config for Test { diff --git a/frame/support/src/traits.rs b/frame/support/src/traits.rs index c4ce565f8ae23..8479a4092c466 100644 --- a/frame/support/src/traits.rs +++ b/frame/support/src/traits.rs @@ -2259,6 +2259,8 @@ pub trait AccountApi { /// Increment a particular account's nonce by 1. fn inc_account_nonce(who: AccountId); + /// Return the storage key for an account. + fn hashed_key_for(who: AccountId) -> Vec; } #[cfg(test)] From 48e20028c25d89dbb163eb39253d3dd77de2e26d Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Wed, 3 Mar 2021 23:37:34 -0400 Subject: [PATCH 07/31] Introduce ReferencedAccount --- Cargo.lock | 7 --- bin/node-template/runtime/src/lib.rs | 2 +- bin/node/runtime/src/lib.rs | 2 +- frame/accounts/Cargo.toml | 12 ----- frame/accounts/src/lib.rs | 51 +++++++++++---------- frame/benchmarking/src/lib.rs | 2 +- frame/benchmarking/src/utils.rs | 2 +- frame/session/Cargo.toml | 2 - frame/session/src/lib.rs | 15 ++++--- frame/support/src/traits.rs | 52 +++++++++++++++++++++- frame/system/src/extensions/check_nonce.rs | 2 +- frame/system/src/lib.rs | 4 +- frame/system/src/offchain.rs | 2 +- 13 files changed, 95 insertions(+), 60 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dbfa3f3facc0b..88afab7caf367 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1688,16 +1688,10 @@ dependencies = [ "criterion", "frame-support", "frame-system", - "impl-trait-for-tuples", "log", "parity-scale-codec", - "serde", - "sp-core", "sp-externalities", - "sp-io", - "sp-runtime", "sp-std", - "sp-version", "substrate-test-runtime-client", ] @@ -5179,7 +5173,6 @@ dependencies = [ name = "pallet-session" version = "3.0.0" dependencies = [ - "frame-accounts", "frame-support", "frame-system", "impl-trait-for-tuples", diff --git a/bin/node-template/runtime/src/lib.rs b/bin/node-template/runtime/src/lib.rs index e04cd6a8b6a61..2ba6d2a3d35bc 100644 --- a/bin/node-template/runtime/src/lib.rs +++ b/bin/node-template/runtime/src/lib.rs @@ -433,7 +433,7 @@ impl_runtime_apis! { impl frame_accounts_rpc_runtime_api::AccountNonceApi for Runtime { fn account_nonce(account: AccountId) -> Index { - use frame_support::traits::AccountApi; + use frame_support::traits::BasicAccount; Accounts::account_nonce(account) } } diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index a8795e2e2ed60..72663e8fc2244 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -1329,7 +1329,7 @@ impl_runtime_apis! { impl frame_accounts_rpc_runtime_api::AccountNonceApi for Runtime { fn account_nonce(account: AccountId) -> Index { - use frame_support::traits::AccountApi; + use frame_support::traits::BasicAccount; Accounts::account_nonce(account) } } diff --git a/frame/accounts/Cargo.toml b/frame/accounts/Cargo.toml index c4d4860a14d8f..0878254622611 100644 --- a/frame/accounts/Cargo.toml +++ b/frame/accounts/Cargo.toml @@ -13,16 +13,10 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -serde = { version = "1.0.101", optional = true, features = ["derive"] } codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } -sp-core = { version = "3.0.0", default-features = false, path = "../../primitives/core" } sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" } -sp-io = { version = "3.0.0", path = "../../primitives/io", default-features = false } -sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } -sp-version = { version = "3.0.0", default-features = false, path = "../../primitives/version" } frame-support = { version = "3.0.0", default-features = false, path = "../support" } frame-system = { version = "3.0.0", default-features = false, path = "../system" } -impl-trait-for-tuples = "0.2.1" log = { version = "0.4.14", default-features = false } [dev-dependencies] @@ -33,20 +27,14 @@ substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/ru [features] default = ["std"] std = [ - "serde", "codec/std", - "sp-core/std", "sp-std/std", - "sp-io/std", "frame-support/std", "frame-system/std", - "sp-runtime/std", - "sp-version/std", "log/std", ] runtime-benchmarks = [ "frame-system/runtime-benchmarks", - "sp-runtime/runtime-benchmarks", "frame-support/runtime-benchmarks", ] try-runtime = ["frame-support/try-runtime"] diff --git a/frame/accounts/src/lib.rs b/frame/accounts/src/lib.rs index b93108932965c..40783afd72d5b 100644 --- a/frame/accounts/src/lib.rs +++ b/frame/accounts/src/lib.rs @@ -4,12 +4,14 @@ use sp_std::{ vec::Vec, marker::PhantomData, }; -use sp_runtime::{ - RuntimeDebug, - traits::StoredMapError, -}; -use frame_support::traits::{ - HandleLifetime, StoredMap, OnNewAccount, OnKilledAccount, +use frame_support::{ + sp_runtime::{ + RuntimeDebug, + traits::{StoredMapError, One}, + }, + traits::{ + HandleLifetime, StoredMap, OnNewAccount, OnKilledAccount, ReferencedAccount, BasicAccount, + } }; use codec::{Encode, Decode, FullCodec}; @@ -82,8 +84,6 @@ pub mod pallet { use frame_system::pallet_prelude::*; use frame_support::pallet_prelude::*; - use frame_support::traits::AccountApi; - use sp_runtime::traits::One; /// System configuration trait. Implemented by runtime. #[pallet::config] @@ -136,7 +136,7 @@ pub mod pallet { #[pallet::call] impl Pallet {} - impl AccountApi for Pallet { + impl BasicAccount for Pallet { type AccountInfo = AccountInfo::AccountData>; /// Return whether an account exists in storage. @@ -177,13 +177,16 @@ pub mod pallet { T::OnKilledAccount::on_killed_account(&who); Self::deposit_event(Event::KilledAccount(who)); } + } - pub fn account_exists(who: &T::AccountId) -> bool { - Account::::contains_key(who) - } - + impl ReferencedAccount for Pallet { + type RefCount = RefCount; + type IncRefStatus = IncRefStatus; + type DecRefStatus = DecRefStatus; + type IncRefError = IncRefError; + type DecRefError = DecRefError; /// Increment the provider reference counter on an account. - pub fn inc_providers(who: &T::AccountId) -> IncRefStatus { + fn inc_providers(who: &T::AccountId) -> IncRefStatus { Account::::mutate(who, |a| if a.providers == 0 && a.sufficients == 0 { // Account is being created. a.providers = 1; @@ -198,7 +201,7 @@ pub mod pallet { /// Decrement the provider reference counter on an account. /// /// This *MUST* only be done once for every time you called `inc_providers` on `who`. - pub fn dec_providers(who: &T::AccountId) -> Result { + fn dec_providers(who: &T::AccountId) -> Result { Account::::try_mutate_exists(who, |maybe_account| { if let Some(mut account) = maybe_account.take() { if account.providers == 0 { @@ -239,7 +242,7 @@ pub mod pallet { } /// Increment the self-sufficient reference counter on an account. - pub fn inc_sufficients(who: &T::AccountId) -> IncRefStatus { + fn inc_sufficients(who: &T::AccountId) -> IncRefStatus { Account::::mutate(who, |a| if a.providers + a.sufficients == 0 { // Account is being created. a.sufficients = 1; @@ -254,7 +257,7 @@ pub mod pallet { /// Decrement the sufficients reference counter on an account. /// /// This *MUST* only be done once for every time you called `inc_sufficients` on `who`. - pub fn dec_sufficients(who: &T::AccountId) -> DecRefStatus { + fn dec_sufficients(who: &T::AccountId) -> DecRefStatus { Account::::mutate_exists(who, |maybe_account| { if let Some(mut account) = maybe_account.take() { if account.sufficients == 0 { @@ -286,17 +289,17 @@ pub mod pallet { } /// The number of outstanding provider references for the account `who`. - pub fn providers(who: &T::AccountId) -> RefCount { + fn providers(who: &T::AccountId) -> RefCount { Account::::get(who).providers } /// The number of outstanding sufficient references for the account `who`. - pub fn sufficients(who: &T::AccountId) -> RefCount { + fn sufficients(who: &T::AccountId) -> RefCount { Account::::get(who).sufficients } /// The number of outstanding provider and sufficient references for the account `who`. - pub fn reference_count(who: &T::AccountId) -> RefCount { + fn reference_count(who: &T::AccountId) -> RefCount { let a = Account::::get(who); a.providers + a.sufficients } @@ -304,7 +307,7 @@ pub mod pallet { /// Increment the reference counter on an account. /// /// The account `who`'s `providers` must be non-zero or this will return an error. - pub fn inc_consumers(who: &T::AccountId) -> Result<(), IncRefError> { + fn inc_consumers(who: &T::AccountId) -> Result<(), IncRefError> { Account::::try_mutate(who, |a| if a.providers > 0 { a.consumers = a.consumers.saturating_add(1); Ok(()) @@ -315,7 +318,7 @@ pub mod pallet { /// Decrement the reference counter on an account. This *MUST* only be done once for every time /// you called `inc_consumers` on `who`. - pub fn dec_consumers(who: &T::AccountId) { + fn dec_consumers(who: &T::AccountId) { Account::::mutate(who, |a| if a.consumers > 0 { a.consumers -= 1; } else { @@ -327,12 +330,12 @@ pub mod pallet { } /// The number of outstanding references for the account `who`. - pub fn consumers(who: &T::AccountId) -> RefCount { + fn consumers(who: &T::AccountId) -> RefCount { Account::::get(who).consumers } /// True if the account has some outstanding references. - pub fn is_provider_required(who: &T::AccountId) -> bool { + fn is_provider_required(who: &T::AccountId) -> bool { Account::::get(who).consumers != 0 } diff --git a/frame/benchmarking/src/lib.rs b/frame/benchmarking/src/lib.rs index 0f974fa7a05a4..b99437f2a6e33 100644 --- a/frame/benchmarking/src/lib.rs +++ b/frame/benchmarking/src/lib.rs @@ -714,7 +714,7 @@ macro_rules! impl_benchmark { let whitelisted_caller_key = < ::AccountStorage as - $crate::frame_support::traits::AccountApi + $crate::frame_support::traits::BasicAccount >::hashed_key_for( $crate::whitelisted_caller::() ); diff --git a/frame/benchmarking/src/utils.rs b/frame/benchmarking/src/utils.rs index 25d45f8ed7ff4..f0c294a33670a 100644 --- a/frame/benchmarking/src/utils.rs +++ b/frame/benchmarking/src/utils.rs @@ -227,7 +227,7 @@ macro_rules! whitelist_account { frame_benchmarking::benchmarking::add_to_whitelist( < ::AccountStorage as - $crate::frame_support::traits::AccountApi + $crate::frame_support::traits::BasicAccount >::hashed_key_for( $crate::whitelisted_caller::() ).into() diff --git a/frame/session/Cargo.toml b/frame/session/Cargo.toml index 817ece0645845..91c5993438b12 100644 --- a/frame/session/Cargo.toml +++ b/frame/session/Cargo.toml @@ -23,7 +23,6 @@ sp-session = { version = "3.0.0", default-features = false, path = "../../primit sp-staking = { version = "3.0.0", default-features = false, path = "../../primitives/staking" } frame-support = { version = "3.0.0", default-features = false, path = "../support" } frame-system = { version = "3.0.0", default-features = false, path = "../system" } -frame-accounts = { version = "3.0.0", default-features = false, path = "../accounts" } pallet-timestamp = { version = "3.0.0", default-features = false, path = "../timestamp" } sp-trie = { version = "3.0.0", optional = true, default-features = false, path = "../../primitives/trie" } impl-trait-for-tuples = "0.2.1" @@ -42,7 +41,6 @@ std = [ "sp-io/std", "frame-support/std", "frame-system/std", - "frame-accounts/std", "sp-core/std", "sp-runtime/std", "sp-session/std", diff --git a/frame/session/src/lib.rs b/frame/session/src/lib.rs index fb2e21a2f135a..0dc04d6233560 100644 --- a/frame/session/src/lib.rs +++ b/frame/session/src/lib.rs @@ -123,7 +123,7 @@ use frame_support::{ ensure, decl_module, decl_event, decl_storage, decl_error, ConsensusEngineId, Parameter, traits::{ Get, FindAuthor, ValidatorRegistration, EstimateNextSessionRotation, EstimateNextNewSession, - OneSessionHandler, ValidatorSet, + OneSessionHandler, ValidatorSet, ReferencedAccount, }, dispatch::{self, DispatchResult, DispatchError}, weights::Weight, @@ -329,7 +329,7 @@ impl ValidatorRegistration for Module { } } -pub trait Config: frame_system::Config + frame_accounts::Config { +pub trait Config: frame_system::Config { /// The overarching event type. type Event: From + Into<::Event>; @@ -366,6 +366,9 @@ pub trait Config: frame_system::Config + frame_accounts::Config { /// Weight information for extrinsics in this pallet. type WeightInfo: WeightInfo; + + /// A way to add reference counters to accounts; + type ReferencedAccount: ReferencedAccount; } decl_storage! { @@ -415,7 +418,7 @@ decl_storage! { for (account, val, keys) in config.keys.iter().cloned() { >::inner_set_keys(&val, keys) .expect("genesis config must not contain duplicates; qed"); - assert!(frame_accounts::Module::::inc_consumers(&account).is_ok()); + assert!(T::ReferencedAccount::inc_consumers(&account).is_ok()); } let initial_validators_0 = T::SessionManager::new_session(0) @@ -719,10 +722,10 @@ impl Module { let who = T::ValidatorIdOf::convert(account.clone()) .ok_or(Error::::NoAssociatedValidatorId)?; - frame_accounts::Module::::inc_consumers(&account).map_err(|_| Error::::NoAccount)?; + T::ReferencedAccount::inc_consumers(&account).map_err(|_| Error::::NoAccount)?; let old_keys = Self::inner_set_keys(&who, keys)?; if old_keys.is_some() { - let _ = frame_accounts::Module::::dec_consumers(&account); + let _ = T::ReferencedAccount::dec_consumers(&account); // ^^^ Defensive only; Consumers were incremented just before, so should never fail. } @@ -771,7 +774,7 @@ impl Module { let key_data = old_keys.get_raw(*id); Self::clear_key_owner(*id, key_data); } - frame_accounts::Module::::dec_consumers(&account); + T::ReferencedAccount::dec_consumers(&account); Ok(()) } diff --git a/frame/support/src/traits.rs b/frame/support/src/traits.rs index 8479a4092c466..eccfa2417e376 100644 --- a/frame/support/src/traits.rs +++ b/frame/support/src/traits.rs @@ -2244,7 +2244,7 @@ pub trait ExecuteBlock { } /// A minimal API for creating an account system compatible with FRAME System. -pub trait AccountApi { +pub trait BasicAccount { type AccountInfo; /// Return whether an account exists in storage. @@ -2263,6 +2263,56 @@ pub trait AccountApi { fn hashed_key_for(who: AccountId) -> Vec; } +/// An API on top of `BasicAccount` which provides the ability for users to place +/// reference counters on an account, preventing account deletion. +pub trait ReferencedAccount: BasicAccount { + type RefCount; + type IncRefStatus; + type DecRefStatus; + type IncRefError; + type DecRefError; + + /// Increment the provider reference counter on an account. + fn inc_providers(who: &AccountId) -> Self::IncRefStatus; + + /// Decrement the provider reference counter on an account. + /// + /// This *MUST* only be done once for every time you called `inc_providers` on `who`. + fn dec_providers(who: &AccountId) -> Result; + + /// Increment the self-sufficient reference counter on an account. + fn inc_sufficients(who: &AccountId) -> Self::IncRefStatus; + + /// Decrement the sufficients reference counter on an account. + /// + /// This *MUST* only be done once for every time you called `inc_sufficients` on `who`. + fn dec_sufficients(who: &AccountId) -> Self::DecRefStatus; + + /// The number of outstanding provider references for the account `who`. + fn providers(who: &AccountId) -> Self::RefCount; + + /// The number of outstanding sufficient references for the account `who`. + fn sufficients(who: &AccountId) -> Self::RefCount; + + /// The number of outstanding provider and sufficient references for the account `who`. + fn reference_count(who: &AccountId) -> Self::RefCount; + + /// Increment the reference counter on an account. + /// + /// The account `who`'s `providers` must be non-zero or this will return an error. + fn inc_consumers(who: &AccountId) -> Result<(), Self::IncRefError>; + + /// Decrement the reference counter on an account. This *MUST* only be done once for every time + /// you called `inc_consumers` on `who`. + fn dec_consumers(who: &AccountId); + + /// The number of outstanding references for the account `who`. + fn consumers(who: &AccountId) -> Self::RefCount; + + /// True if the account has some outstanding references. + fn is_provider_required(who: &AccountId) -> bool; +} + #[cfg(test)] mod tests { use super::*; diff --git a/frame/system/src/extensions/check_nonce.rs b/frame/system/src/extensions/check_nonce.rs index 1c6031a98e660..39d807db445a4 100644 --- a/frame/system/src/extensions/check_nonce.rs +++ b/frame/system/src/extensions/check_nonce.rs @@ -18,7 +18,7 @@ use codec::{Encode, Decode}; use crate::Config; use frame_support::{ - traits::AccountApi, + traits::BasicAccount, weights::DispatchInfo, }; use sp_runtime::{ diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index 2efb6f6607244..13c582a1ea141 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -88,7 +88,7 @@ use frame_support::{ Parameter, storage, traits::{ Contains, Get, PalletInfo, - EnsureOrigin, OriginTrait, Filter, AccountApi, + EnsureOrigin, OriginTrait, Filter, BasicAccount, }, weights::{ Weight, RuntimeDbWeight, DispatchInfo, DispatchClass, @@ -242,7 +242,7 @@ pub mod pallet { #[pallet::constant] type SS58Prefix: Get; - type AccountStorage: AccountApi; + type AccountStorage: BasicAccount; } #[pallet::pallet] diff --git a/frame/system/src/offchain.rs b/frame/system/src/offchain.rs index 73a02e10e72c3..b91bb6e889f81 100644 --- a/frame/system/src/offchain.rs +++ b/frame/system/src/offchain.rs @@ -63,7 +63,7 @@ use sp_std::convert::{TryInto, TryFrom}; use sp_std::prelude::{Box, Vec}; use sp_runtime::app_crypto::RuntimeAppPublic; use sp_runtime::traits::{Extrinsic as ExtrinsicT, IdentifyAccount}; -use frame_support::{RuntimeDebug, traits::AccountApi}; +use frame_support::{RuntimeDebug, traits::BasicAccount}; /// Marker struct used to flag using all supported keys to sign a payload. pub struct ForAll {} From bfd00160990d8ccb4adff4978016146aafaced6e Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Wed, 3 Mar 2021 23:48:21 -0400 Subject: [PATCH 08/31] more ReferencedAccount --- bin/node/runtime/src/lib.rs | 5 +++++ frame/assets/src/lib.rs | 17 ++++++++++------- frame/balances/src/lib.rs | 13 ++++++++----- frame/recovery/src/lib.rs | 5 ++++- frame/staking/src/lib.rs | 9 ++++++--- 5 files changed, 33 insertions(+), 16 deletions(-) diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 72663e8fc2244..bf56db5509e74 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -378,6 +378,7 @@ impl pallet_balances::Config for Runtime { type Event = Event; type ExistentialDeposit = ExistentialDeposit; type AccountStore = Accounts; + type ReferencedAccount = Accounts; type WeightInfo = pallet_balances::weights::SubstrateWeight; } @@ -441,6 +442,7 @@ impl pallet_session::Config for Runtime { type SessionHandler = ::KeyTypeIdProviders; type Keys = SessionKeys; type DisabledValidatorsThreshold = DisabledValidatorsThreshold; + type ReferencedAccount = Accounts; type WeightInfo = pallet_session::weights::SubstrateWeight; } @@ -506,6 +508,7 @@ impl pallet_staking::Config for Runtime { // a single extrinsic. type OffchainSolutionWeightLimit = OffchainSolutionWeightLimit; type ElectionProvider = ElectionProviderMultiPhase; + type ReferencedAccount = Accounts; type WeightInfo = pallet_staking::weights::SubstrateWeight; } @@ -957,6 +960,7 @@ impl pallet_recovery::Config for Runtime { type ConfigDepositBase = ConfigDepositBase; type FriendDepositFactor = FriendDepositFactor; type MaxFriends = MaxFriends; + type ReferencedAccount = Accounts; type RecoveryDeposit = RecoveryDeposit; } @@ -1047,6 +1051,7 @@ impl pallet_assets::Config for Runtime { type StringLimit = StringLimit; type MetadataDepositBase = MetadataDepositBase; type MetadataDepositPerByte = MetadataDepositPerByte; + type ReferencedAccount = Accounts; type WeightInfo = pallet_assets::weights::SubstrateWeight; } diff --git a/frame/assets/src/lib.rs b/frame/assets/src/lib.rs index 0aeaedf51c244..aba9e062b8c44 100644 --- a/frame/assets/src/lib.rs +++ b/frame/assets/src/lib.rs @@ -123,7 +123,7 @@ use sp_runtime::{ use codec::{Encode, Decode, HasCompact}; use frame_support::{ ensure, - traits::{Currency, ReservableCurrency, BalanceStatus::Reserved}, + traits::{Currency, ReservableCurrency, BalanceStatus::Reserved, BasicAccount, ReferencedAccount}, dispatch::DispatchError, }; pub use weights::WeightInfo; @@ -147,7 +147,7 @@ pub mod pallet { #[pallet::config] /// The module configuration trait. - pub trait Config: frame_system::Config + frame_accounts::Config { + pub trait Config: frame_system::Config { /// The overarching event type. type Event: From> + IsType<::Event>; @@ -182,6 +182,9 @@ pub mod pallet { /// Weight information for extrinsics in this pallet. type WeightInfo: WeightInfo; + + /// A way to place reference counters on accounts. + type ReferencedAccount: ReferencedAccount; } #[pallet::hooks] @@ -1098,8 +1101,8 @@ impl Pallet { d: &mut AssetDetails>, ) -> Result { let accounts = d.accounts.checked_add(1).ok_or(Error::::Overflow)?; - let r = Ok(if frame_accounts::Module::::account_exists(who) { - frame_accounts::Module::::inc_consumers(who).map_err(|_| Error::::BadState)?; + let r = Ok(if T::ReferencedAccount::account_exists(who) { + T::ReferencedAccount::inc_consumers(who).map_err(|_| Error::::BadState)?; false } else { ensure!(d.zombies < d.max_zombies, Error::::TooManyZombies); @@ -1116,10 +1119,10 @@ impl Pallet { d: &mut AssetDetails>, is_zombie: &mut bool, ) { - if *is_zombie && frame_accounts::Module::::account_exists(who) { + if *is_zombie && T::ReferencedAccount::account_exists(who) { // If the account exists, then it should have at least one provider // so this cannot fail... but being defensive anyway. - let _ = frame_accounts::Module::::inc_consumers(who); + let _ = T::ReferencedAccount::inc_consumers(who); *is_zombie = false; d.zombies = d.zombies.saturating_sub(1); } @@ -1133,7 +1136,7 @@ impl Pallet { if is_zombie { d.zombies = d.zombies.saturating_sub(1); } else { - frame_accounts::Module::::dec_consumers(who); + T::ReferencedAccount::dec_consumers(who); } d.accounts = d.accounts.saturating_sub(1); } diff --git a/frame/balances/src/lib.rs b/frame/balances/src/lib.rs index 285a996aa5017..308a3fdc17ab4 100644 --- a/frame/balances/src/lib.rs +++ b/frame/balances/src/lib.rs @@ -164,7 +164,7 @@ use frame_support::{ Currency, OnUnbalanced, TryDrop, StoredMap, WithdrawReasons, LockIdentifier, LockableCurrency, ExistenceRequirement, Imbalance, SignedImbalance, ReservableCurrency, Get, ExistenceRequirement::KeepAlive, - ExistenceRequirement::AllowDeath, BalanceStatus as Status, + ExistenceRequirement::AllowDeath, BalanceStatus as Status, ReferencedAccount, } }; #[cfg(feature = "std")] @@ -188,7 +188,7 @@ pub mod pallet { use super::*; #[pallet::config] - pub trait Config: frame_system::Config + frame_accounts::Config { + pub trait Config: frame_system::Config { /// The balance of an account. type Balance: Parameter + Member + AtLeast32BitUnsigned + Codec + Default + Copy + MaybeSerializeDeserialize + Debug; @@ -212,6 +212,9 @@ pub mod pallet { /// The maximum number of locks that should exist on an account. /// Not strictly enforced, but used for weight estimation. type MaxLocks: Get; + + /// A way to place reference counters on an account. + type ReferencedAccount: ReferencedAccount; } #[pallet::pallet] @@ -781,12 +784,12 @@ impl, I: 'static> Pallet { if existed { // TODO: use Locks::::hashed_key // https://github.com/paritytech/substrate/issues/4969 - frame_accounts::Pallet::::dec_consumers(who); + T::ReferencedAccount::dec_consumers(who); } } else { Locks::::insert(who, locks); if !existed { - if frame_accounts::Pallet::::inc_consumers(who).is_err() { + if T::ReferencedAccount::inc_consumers(who).is_err() { // No providers for the locks. This is impossible under normal circumstances // since the funds that are under the lock will themselves be stored in the // account and therefore will need a reference. @@ -1069,7 +1072,7 @@ impl, I: 'static> Currency for Pallet where // TODO: This is over-conservative. There may now be other providers, and this pallet // may not even be a provider. let allow_death = existence_requirement == ExistenceRequirement::AllowDeath; - let allow_death = allow_death && !frame_accounts::Pallet::::is_provider_required(transactor); + let allow_death = allow_death && !T::ReferencedAccount::is_provider_required(transactor); ensure!(allow_death || from_account.total() >= ed, Error::::KeepAlive); Ok(()) diff --git a/frame/recovery/src/lib.rs b/frame/recovery/src/lib.rs index c9a604067dcd5..0f2182d1a1c95 100644 --- a/frame/recovery/src/lib.rs +++ b/frame/recovery/src/lib.rs @@ -161,7 +161,7 @@ use codec::{Encode, Decode}; use frame_support::{ decl_module, decl_event, decl_storage, decl_error, ensure, Parameter, RuntimeDebug, weights::GetDispatchInfo, - traits::{Currency, ReservableCurrency, Get, BalanceStatus}, + traits::{Currency, ReservableCurrency, Get, BalanceStatus, ReferencedAccount}, dispatch::PostDispatchInfo, }; use frame_system::{self as system, ensure_signed, ensure_root}; @@ -207,6 +207,9 @@ pub trait Config: frame_system::Config + frame_accounts::Config { /// `sizeof(BlockNumber, Balance + T * AccountId)` bytes. Where T is a configurable /// threshold. type RecoveryDeposit: Get>; + + /// A way to place reference counters on accounts. + type ReferencedAccount: ReferencedAccount; } /// An active recovery process. diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index afcdb59a0a30b..0c4e038dca43a 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -300,7 +300,7 @@ use frame_support::{ }, traits::{ Currency, LockIdentifier, LockableCurrency, WithdrawReasons, OnUnbalanced, Imbalance, Get, - UnixTime, EstimateNextNewSession, EnsureOrigin, CurrencyToVote, IsSubType, + UnixTime, EstimateNextNewSession, EnsureOrigin, CurrencyToVote, IsSubType, ReferencedAccount, } }; use pallet_session::historical; @@ -884,6 +884,9 @@ pub trait Config: frame_system::Config + frame_accounts::Config + SendTransactio /// Weight information for extrinsics in this pallet. type WeightInfo: WeightInfo; + + /// A way to place reference counters on an account. + type ReferencedAccount: ReferencedAccount; } /// Mode of era-forcing. @@ -1500,7 +1503,7 @@ decl_module! { Err(Error::::InsufficientValue)? } - frame_accounts::Module::::inc_consumers(&stash).map_err(|_| Error::::BadState)?; + T::ReferencedAccount::inc_consumers(&stash).map_err(|_| Error::::BadState)?; // You're auto-bonded forever, here. We might improve this by only bonding when // you actually validate/nominate and remove once you unbond __everything__. @@ -3202,7 +3205,7 @@ impl Module { >::remove(stash); >::remove(stash); - frame_accounts::Module::::dec_consumers(stash); + T::ReferencedAccount::dec_consumers(stash); Ok(()) } From b3045430c17c4d7687e4c13e3278f143759e60ec Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Wed, 3 Mar 2021 23:49:55 -0400 Subject: [PATCH 09/31] node-template fix --- bin/node-template/runtime/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/bin/node-template/runtime/src/lib.rs b/bin/node-template/runtime/src/lib.rs index 2ba6d2a3d35bc..788c4bcbf1fec 100644 --- a/bin/node-template/runtime/src/lib.rs +++ b/bin/node-template/runtime/src/lib.rs @@ -244,6 +244,7 @@ impl pallet_balances::Config for Runtime { type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; type AccountStore = Accounts; + type ReferencedAccount = Accounts; type WeightInfo = pallet_balances::weights::SubstrateWeight; } From 81a774906ce9d4dc3549e6387674bf5a141b2d63 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Thu, 4 Mar 2021 00:13:28 -0400 Subject: [PATCH 10/31] Add Basic Account to FRAME System --- frame/accounts/src/lib.rs | 2 -- frame/system/src/lib.rs | 40 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/frame/accounts/src/lib.rs b/frame/accounts/src/lib.rs index 40783afd72d5b..95a51bd32eccf 100644 --- a/frame/accounts/src/lib.rs +++ b/frame/accounts/src/lib.rs @@ -387,8 +387,6 @@ impl HandleLifetime for Consumer { } } - - /// Implement StoredMap for a simple single-item, provide-when-not-default system. This works fine /// for storing a single item which allows the account to continue existing as long as it's not /// empty/default. diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index 13c582a1ea141..519658be2fe9e 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -494,6 +494,17 @@ pub mod pallet { #[pallet::origin] pub type Origin = RawOrigin<::AccountId>; + /// Basic account information for a particular account ID. + #[pallet::storage] + #[pallet::getter(fn account)] + pub type Account = StorageMap< + _, + Blake2_128Concat, + T::AccountId, + T::Index, + ValueQuery, + >; + /// Total extrinsics count for the current block. #[pallet::storage] pub(super) type ExtrinsicCount = StorageValue<_, u32>; @@ -1268,6 +1279,35 @@ impl Lookup for ChainContext { } } +impl BasicAccount for Pallet { + type AccountInfo = T::Index; + + /// Return whether an account exists in storage. + fn account_exists(who: &T::AccountId) -> bool { + Account::::contains_key(who) + } + + /// Return the data for an account + fn get(who: &T::AccountId) -> Self::AccountInfo { + Account::::get(who) + } + + /// Retrieve the account transaction counter from storage. + fn account_nonce(who: T::AccountId) -> T::Index { + Account::::get(who) + } + + /// Increment a particular account's nonce by 1. + fn inc_account_nonce(who: T::AccountId) { + Account::::mutate(who, |a| *a += T::Index::one()); + } + + /// Return the storage key for an account. + fn hashed_key_for(who: T::AccountId) -> Vec { + Account::::hashed_key_for(who) + } +} + /// Prelude to be used alongside pallet macro, for ease of use. pub mod pallet_prelude { pub use crate::{ensure_signed, ensure_none, ensure_root}; From 7e1e4c380c8e38c11555dc8085018444b5d97ccc Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Thu, 4 Mar 2021 00:44:42 -0400 Subject: [PATCH 11/31] start moving tests --- Cargo.lock | 6 +- frame/accounts/Cargo.toml | 6 +- frame/accounts/src/lib.rs | 26 +++-- frame/accounts/src/mock.rs | 84 ++++++++++++++++ frame/accounts/src/tests.rs | 108 +++++++++++++++++++++ frame/support/src/traits.rs | 6 +- frame/system/src/extensions/check_nonce.rs | 6 +- frame/system/src/lib.rs | 6 +- frame/system/src/offchain.rs | 4 +- frame/system/src/tests.rs | 85 ---------------- 10 files changed, 225 insertions(+), 112 deletions(-) create mode 100644 frame/accounts/src/mock.rs create mode 100644 frame/accounts/src/tests.rs diff --git a/Cargo.lock b/Cargo.lock index 88afab7caf367..cccfdaf6051f6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1685,14 +1685,14 @@ dependencies = [ name = "frame-accounts" version = "3.0.0" dependencies = [ - "criterion", "frame-support", "frame-system", "log", "parity-scale-codec", - "sp-externalities", + "serde", + "sp-core", + "sp-io", "sp-std", - "substrate-test-runtime-client", ] [[package]] diff --git a/frame/accounts/Cargo.toml b/frame/accounts/Cargo.toml index 0878254622611..4b050014c8625 100644 --- a/frame/accounts/Cargo.toml +++ b/frame/accounts/Cargo.toml @@ -20,9 +20,9 @@ frame-system = { version = "3.0.0", default-features = false, path = "../system" log = { version = "0.4.14", default-features = false } [dev-dependencies] -criterion = "0.3.3" -sp-externalities = { version = "0.9.0", path = "../../primitives/externalities" } -substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/runtime/client" } +serde = { version = "1.0.101", features = ["derive"] } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +sp-io = { version = "3.0.0", path = "../../primitives/io" } [features] default = ["std"] diff --git a/frame/accounts/src/lib.rs b/frame/accounts/src/lib.rs index 95a51bd32eccf..da1546733448f 100644 --- a/frame/accounts/src/lib.rs +++ b/frame/accounts/src/lib.rs @@ -15,6 +15,12 @@ use frame_support::{ }; use codec::{Encode, Decode, FullCodec}; +#[cfg(test)] +mod mock; + +#[cfg(test)] +mod tests; + /// Type used to encode the number of references an account has. pub type RefCount = u32; @@ -44,8 +50,8 @@ pub enum RefStatus { Unreferenced, } -/// Some resultant status relevant to incrementing a provider reference. -#[derive(RuntimeDebug)] +/// Some resultant status relevant to incrementing a provider/self-sufficient reference. +#[derive(Eq, PartialEq, RuntimeDebug)] pub enum IncRefStatus { /// Account was created. Created, @@ -53,8 +59,8 @@ pub enum IncRefStatus { Existed, } -/// Some resultant status relevant to decrementing a provider reference. -#[derive(RuntimeDebug)] +/// Some resultant status relevant to decrementing a provider/self-sufficient reference. +#[derive(Eq, PartialEq, RuntimeDebug)] pub enum DecRefStatus { /// Account was destroyed. Reaped, @@ -63,14 +69,14 @@ pub enum DecRefStatus { } /// Some resultant status relevant to decrementing a provider reference. -#[derive(RuntimeDebug)] +#[derive(Eq, PartialEq, RuntimeDebug)] pub enum DecRefError { /// Account cannot have the last provider reference removed while there is a consumer. ConsumerRemaining, } -/// Some resultant status relevant to incrementing a provider reference. -#[derive(RuntimeDebug)] +/// Some resultant status relevant to incrementing a consumer reference. +#[derive(Eq, PartialEq, RuntimeDebug)] pub enum IncRefError { /// Account cannot introduce a consumer while there are no providers. NoProviders, @@ -150,17 +156,17 @@ pub mod pallet { } /// Retrieve the account transaction counter from storage. - fn account_nonce(who: T::AccountId) -> T::Index { + fn account_nonce(who: &T::AccountId) -> T::Index { Account::::get(who).nonce } /// Increment a particular account's nonce by 1. - fn inc_account_nonce(who: T::AccountId) { + fn inc_account_nonce(who: &T::AccountId) { Account::::mutate(who, |a| a.nonce += T::Index::one()); } /// Return the storage key for an account. - fn hashed_key_for(who: T::AccountId) -> Vec { + fn hashed_key_for(who: &T::AccountId) -> Vec { Account::::hashed_key_for(who) } } diff --git a/frame/accounts/src/mock.rs b/frame/accounts/src/mock.rs new file mode 100644 index 0000000000000..de7dffc77d815 --- /dev/null +++ b/frame/accounts/src/mock.rs @@ -0,0 +1,84 @@ +// This file is part of Substrate. + +// Copyright (C) 2017-2021 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// 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 crate::{self as frame_accounts, *}; +use sp_std::cell::RefCell; +use sp_core::H256; +use frame_support::{ + sp_runtime::{ + traits::{BlakeTwo256, IdentityLookup}, + testing::Header, + }, +}; + +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +type Block = frame_system::mocking::MockBlock; + +frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: frame_accounts::{Module, Call, Storage, Event}, + } +); + +thread_local!{ + pub static KILLED: RefCell> = RefCell::new(vec![]); +} + +pub struct RecordKilled; +impl OnKilledAccount for RecordKilled { + fn on_killed_account(who: &u64) { KILLED.with(|r| r.borrow_mut().push(*who)) } +} + +impl frame_system::Config for Test { + type BaseCallFilter = (); + type BlockWeights = (); + type BlockLength = (); + type DbWeight = (); + type Origin = Origin; + type Index = u64; + type Call = Call; + type BlockNumber = u64; + type Hash = H256; + type Hashing = BlakeTwo256; + type AccountId = u64; + type Lookup = IdentityLookup; + type Header = Header; + type Event = Event; + type BlockHashCount = (); + type Version = (); + type PalletInfo = PalletInfo; + type SystemWeightInfo = (); + type SS58Prefix = (); + type AccountStorage = Accounts; +} + +impl frame_accounts::Config for Test { + type Event = Event; + type AccountData = u32; + type OnNewAccount = (); + type OnKilledAccount = RecordKilled; +} + +/// Create new externalities for `System` module tests. +pub fn new_test_ext() -> sp_io::TestExternalities { + frame_system::GenesisConfig::default().build_storage::().unwrap().into() +} diff --git a/frame/accounts/src/tests.rs b/frame/accounts/src/tests.rs new file mode 100644 index 0000000000000..cb624b28d2cb1 --- /dev/null +++ b/frame/accounts/src/tests.rs @@ -0,0 +1,108 @@ +// This file is part of Substrate. + +// Copyright (C) 2017-2021 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// 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 crate::*; +use mock::*; +use frame_support::{ + assert_noop, weights::WithPostDispatchInfo, dispatch::PostDispatchInfo, + sp_runtime::{DispatchError, DispatchErrorWithPostInfo, traits::{Header, BlakeTwo256}}, +}; + +#[test] +fn stored_map_works() { + new_test_ext().execute_with(|| { + assert!(Accounts::insert(&0, 42).is_ok()); + assert!(!Accounts::is_provider_required(&0)); + + assert_eq!(Account::::get(0), AccountInfo { + nonce: 0, + providers: 1, + consumers: 0, + sufficients: 0, + data: 42, + }); + + assert!(Accounts::inc_consumers(&0).is_ok()); + assert!(Accounts::is_provider_required(&0)); + + assert!(Accounts::insert(&0, 69).is_ok()); + assert!(Accounts::is_provider_required(&0)); + + Accounts::dec_consumers(&0); + assert!(!Accounts::is_provider_required(&0)); + + assert!(KILLED.with(|r| r.borrow().is_empty())); + assert!(Accounts::remove(&0).is_ok()); + assert_eq!(KILLED.with(|r| r.borrow().clone()), vec![0u64]); + }); +} + +#[test] +fn provider_ref_handover_to_self_sufficient_ref_works() { + new_test_ext().execute_with(|| { + assert_eq!(Accounts::inc_providers(&0), IncRefStatus::Created); + Accounts::inc_account_nonce(&0); + assert_eq!(Accounts::account_nonce(&0), 1); + + // a second reference coming and going doesn't change anything. + assert_eq!(Accounts::inc_sufficients(&0), IncRefStatus::Existed); + assert_eq!(Accounts::dec_sufficients(&0), DecRefStatus::Exists); + assert_eq!(Accounts::account_nonce(&0), 1); + + // a provider reference coming and going doesn't change anything. + assert_eq!(Accounts::inc_providers(&0), IncRefStatus::Existed); + assert_eq!(Accounts::dec_providers(&0).unwrap(), DecRefStatus::Exists); + assert_eq!(Accounts::account_nonce(&0), 1); + + // decreasing the providers with a self-sufficient present should not delete the account + assert_eq!(Accounts::inc_sufficients(&0), IncRefStatus::Existed); + assert_eq!(Accounts::dec_providers(&0).unwrap(), DecRefStatus::Exists); + assert_eq!(Accounts::account_nonce(&0), 1); + + // decreasing the sufficients should delete the account + assert_eq!(Accounts::dec_sufficients(&0), DecRefStatus::Reaped); + assert_eq!(Accounts::account_nonce(&0), 0); + }); +} + +#[test] +fn self_sufficient_ref_handover_to_provider_ref_works() { + new_test_ext().execute_with(|| { + assert_eq!(Accounts::inc_sufficients(&0), IncRefStatus::Created); + Accounts::inc_account_nonce(&0); + assert_eq!(Accounts::account_nonce(&0), 1); + + // a second reference coming and going doesn't change anything. + assert_eq!(Accounts::inc_providers(&0), IncRefStatus::Existed); + assert_eq!(Accounts::dec_providers(&0).unwrap(), DecRefStatus::Exists); + assert_eq!(Accounts::account_nonce(&0), 1); + + // a sufficient reference coming and going doesn't change anything. + assert_eq!(Accounts::inc_sufficients(&0), IncRefStatus::Existed); + assert_eq!(Accounts::dec_sufficients(&0), DecRefStatus::Exists); + assert_eq!(Accounts::account_nonce(&0), 1); + + // decreasing the sufficients with a provider present should not delete the account + assert_eq!(Accounts::inc_providers(&0), IncRefStatus::Existed); + assert_eq!(Accounts::dec_sufficients(&0), DecRefStatus::Exists); + assert_eq!(Accounts::account_nonce(&0), 1); + + // decreasing the providers should delete the account + assert_eq!(Accounts::dec_providers(&0).unwrap(), DecRefStatus::Reaped); + assert_eq!(Accounts::account_nonce(&0), 0); + }); +} diff --git a/frame/support/src/traits.rs b/frame/support/src/traits.rs index eccfa2417e376..53f5b0476ff4a 100644 --- a/frame/support/src/traits.rs +++ b/frame/support/src/traits.rs @@ -2254,13 +2254,13 @@ pub trait BasicAccount { fn get(who: &AccountId) -> Self::AccountInfo; /// Retrieve the account transaction counter from storage. - fn account_nonce(who: AccountId) -> Index; + fn account_nonce(who: &AccountId) -> Index; /// Increment a particular account's nonce by 1. - fn inc_account_nonce(who: AccountId); + fn inc_account_nonce(who: &AccountId); /// Return the storage key for an account. - fn hashed_key_for(who: AccountId) -> Vec; + fn hashed_key_for(who: &AccountId) -> Vec; } /// An API on top of `BasicAccount` which provides the ability for users to place diff --git a/frame/system/src/extensions/check_nonce.rs b/frame/system/src/extensions/check_nonce.rs index 39d807db445a4..07a51a1d3ae34 100644 --- a/frame/system/src/extensions/check_nonce.rs +++ b/frame/system/src/extensions/check_nonce.rs @@ -74,7 +74,7 @@ impl SignedExtension for CheckNonce where _info: &DispatchInfoOf, _len: usize, ) -> Result<(), TransactionValidityError> { - let account_nonce = T::AccountStorage::account_nonce(who.clone()); + let account_nonce = T::AccountStorage::account_nonce(who); if self.0 != account_nonce { return Err( if self.0 < account_nonce { @@ -84,7 +84,7 @@ impl SignedExtension for CheckNonce where }.into() ) } - T::AccountStorage::inc_account_nonce(who.clone()); + T::AccountStorage::inc_account_nonce(who); Ok(()) } @@ -96,7 +96,7 @@ impl SignedExtension for CheckNonce where _len: usize, ) -> TransactionValidity { // check index - let account_nonce = T::AccountStorage::account_nonce(who.clone()); + let account_nonce = T::AccountStorage::account_nonce(who); if self.0 < account_nonce { return InvalidTransaction::Stale.into() } diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index 519658be2fe9e..f6623fac8a25b 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -1293,17 +1293,17 @@ impl BasicAccount for Pallet { } /// Retrieve the account transaction counter from storage. - fn account_nonce(who: T::AccountId) -> T::Index { + fn account_nonce(who: &T::AccountId) -> T::Index { Account::::get(who) } /// Increment a particular account's nonce by 1. - fn inc_account_nonce(who: T::AccountId) { + fn inc_account_nonce(who: &T::AccountId) { Account::::mutate(who, |a| *a += T::Index::one()); } /// Return the storage key for an account. - fn hashed_key_for(who: T::AccountId) -> Vec { + fn hashed_key_for(who: &T::AccountId) -> Vec { Account::::hashed_key_for(who) } } diff --git a/frame/system/src/offchain.rs b/frame/system/src/offchain.rs index b91bb6e889f81..abf57d30727fd 100644 --- a/frame/system/src/offchain.rs +++ b/frame/system/src/offchain.rs @@ -549,7 +549,7 @@ pub trait SendSignedTransaction< account: &Account, call: LocalCall, ) -> Option> { - let account_nonce = T::AccountStorage::account_nonce(account.id.clone()); + let account_nonce = T::AccountStorage::account_nonce(&account.id); log::debug!( target: "runtime::offchain", "Creating signed transaction from account: {:?} (nonce: {:?})", @@ -568,7 +568,7 @@ pub trait SendSignedTransaction< if res.is_ok() { // increment the nonce. This is fine, since the code should always // be running in off-chain context, so we NEVER persists data. - T::AccountStorage::inc_account_nonce(account.id.clone()); + T::AccountStorage::inc_account_nonce(&account.id); } Some(res) diff --git a/frame/system/src/tests.rs b/frame/system/src/tests.rs index 9f500e5a3b050..fb936a4e72a09 100644 --- a/frame/system/src/tests.rs +++ b/frame/system/src/tests.rs @@ -28,91 +28,6 @@ fn origin_works() { assert_eq!(x.unwrap(), RawOrigin::::Signed(1u64)); } -#[test] -fn stored_map_works() { - new_test_ext().execute_with(|| { - assert!(System::insert(&0, 42).is_ok()); - assert!(!System::is_provider_required(&0)); - - assert_eq!(Account::::get(0), AccountInfo { - nonce: 0, - providers: 1, - consumers: 0, - sufficients: 0, - data: 42, - }); - - assert!(System::inc_consumers(&0).is_ok()); - assert!(System::is_provider_required(&0)); - - assert!(System::insert(&0, 69).is_ok()); - assert!(System::is_provider_required(&0)); - - System::dec_consumers(&0); - assert!(!System::is_provider_required(&0)); - - assert!(KILLED.with(|r| r.borrow().is_empty())); - assert!(System::remove(&0).is_ok()); - assert_eq!(KILLED.with(|r| r.borrow().clone()), vec![0u64]); - }); -} - -#[test] -fn provider_ref_handover_to_self_sufficient_ref_works() { - new_test_ext().execute_with(|| { - assert_eq!(System::inc_providers(&0), IncRefStatus::Created); - System::inc_account_nonce(&0); - assert_eq!(System::account_nonce(&0), 1); - - // a second reference coming and going doesn't change anything. - assert_eq!(System::inc_sufficients(&0), IncRefStatus::Existed); - assert_eq!(System::dec_sufficients(&0), DecRefStatus::Exists); - assert_eq!(System::account_nonce(&0), 1); - - // a provider reference coming and going doesn't change anything. - assert_eq!(System::inc_providers(&0), IncRefStatus::Existed); - assert_eq!(System::dec_providers(&0).unwrap(), DecRefStatus::Exists); - assert_eq!(System::account_nonce(&0), 1); - - // decreasing the providers with a self-sufficient present should not delete the account - assert_eq!(System::inc_sufficients(&0), IncRefStatus::Existed); - assert_eq!(System::dec_providers(&0).unwrap(), DecRefStatus::Exists); - assert_eq!(System::account_nonce(&0), 1); - - // decreasing the sufficients should delete the account - assert_eq!(System::dec_sufficients(&0), DecRefStatus::Reaped); - assert_eq!(System::account_nonce(&0), 0); - }); -} - -#[test] -fn self_sufficient_ref_handover_to_provider_ref_works() { - new_test_ext().execute_with(|| { - assert_eq!(System::inc_sufficients(&0), IncRefStatus::Created); - System::inc_account_nonce(&0); - assert_eq!(System::account_nonce(&0), 1); - - // a second reference coming and going doesn't change anything. - assert_eq!(System::inc_providers(&0), IncRefStatus::Existed); - assert_eq!(System::dec_providers(&0).unwrap(), DecRefStatus::Exists); - assert_eq!(System::account_nonce(&0), 1); - - // a sufficient reference coming and going doesn't change anything. - assert_eq!(System::inc_sufficients(&0), IncRefStatus::Existed); - assert_eq!(System::dec_sufficients(&0), DecRefStatus::Exists); - assert_eq!(System::account_nonce(&0), 1); - - // decreasing the sufficients with a provider present should not delete the account - assert_eq!(System::inc_providers(&0), IncRefStatus::Existed); - assert_eq!(System::dec_sufficients(&0), DecRefStatus::Exists); - assert_eq!(System::account_nonce(&0), 1); - - // decreasing the providers should delete the account - assert_eq!(System::dec_providers(&0).unwrap(), DecRefStatus::Reaped); - assert_eq!(System::account_nonce(&0), 0); - }); -} - #[test] fn sufficient_cannot_support_consumer() { new_test_ext().execute_with(|| { From bf6f72959828ff2efd390a298f54c111498c2284 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Thu, 4 Mar 2021 00:47:49 -0400 Subject: [PATCH 12/31] more tests moved --- frame/accounts/src/tests.rs | 41 +++++++++++++++++++++++++++++++++---- frame/system/src/tests.rs | 36 -------------------------------- 2 files changed, 37 insertions(+), 40 deletions(-) diff --git a/frame/accounts/src/tests.rs b/frame/accounts/src/tests.rs index cb624b28d2cb1..2a14cb836d3ef 100644 --- a/frame/accounts/src/tests.rs +++ b/frame/accounts/src/tests.rs @@ -17,10 +17,7 @@ use crate::*; use mock::*; -use frame_support::{ - assert_noop, weights::WithPostDispatchInfo, dispatch::PostDispatchInfo, - sp_runtime::{DispatchError, DispatchErrorWithPostInfo, traits::{Header, BlakeTwo256}}, -}; +use frame_support::assert_noop; #[test] fn stored_map_works() { @@ -106,3 +103,39 @@ fn self_sufficient_ref_handover_to_provider_ref_works() { assert_eq!(Accounts::account_nonce(&0), 0); }); } + +#[test] +fn sufficient_cannot_support_consumer() { + new_test_ext().execute_with(|| { + assert_eq!(Accounts::inc_sufficients(&0), IncRefStatus::Created); + Accounts::inc_account_nonce(&0); + assert_eq!(Accounts::account_nonce(&0), 1); + assert_noop!(Accounts::inc_consumers(&0), IncRefError::NoProviders); + + assert_eq!(Accounts::inc_providers(&0), IncRefStatus::Existed); + assert!(Accounts::inc_consumers(&0).is_ok()); + assert_noop!(Accounts::dec_providers(&0), DecRefError::ConsumerRemaining); + }); +} + +#[test] +fn provider_required_to_support_consumer() { + new_test_ext().execute_with(|| { + assert_noop!(Accounts::inc_consumers(&0), IncRefError::NoProviders); + + assert_eq!(Accounts::inc_providers(&0), IncRefStatus::Created); + Accounts::inc_account_nonce(&0); + assert_eq!(Accounts::account_nonce(&0), 1); + + assert_eq!(Accounts::inc_providers(&0), IncRefStatus::Existed); + assert_eq!(Accounts::dec_providers(&0).unwrap(), DecRefStatus::Exists); + assert_eq!(Accounts::account_nonce(&0), 1); + + assert!(Accounts::inc_consumers(&0).is_ok()); + assert_noop!(Accounts::dec_providers(&0), DecRefError::ConsumerRemaining); + + Accounts::dec_consumers(&0); + assert_eq!(Accounts::dec_providers(&0).unwrap(), DecRefStatus::Reaped); + assert_eq!(Accounts::account_nonce(&0), 0); + }); +} diff --git a/frame/system/src/tests.rs b/frame/system/src/tests.rs index fb936a4e72a09..72cd22fe44294 100644 --- a/frame/system/src/tests.rs +++ b/frame/system/src/tests.rs @@ -28,42 +28,6 @@ fn origin_works() { assert_eq!(x.unwrap(), RawOrigin::::Signed(1u64)); } -#[test] -fn sufficient_cannot_support_consumer() { - new_test_ext().execute_with(|| { - assert_eq!(System::inc_sufficients(&0), IncRefStatus::Created); - System::inc_account_nonce(&0); - assert_eq!(System::account_nonce(&0), 1); - assert_noop!(System::inc_consumers(&0), IncRefError::NoProviders); - - assert_eq!(System::inc_providers(&0), IncRefStatus::Existed); - assert!(System::inc_consumers(&0).is_ok()); - assert_noop!(System::dec_providers(&0), DecRefError::ConsumerRemaining); - }); -} - -#[test] -fn provider_required_to_support_consumer() { - new_test_ext().execute_with(|| { - assert_noop!(System::inc_consumers(&0), IncRefError::NoProviders); - - assert_eq!(System::inc_providers(&0), IncRefStatus::Created); - System::inc_account_nonce(&0); - assert_eq!(System::account_nonce(&0), 1); - - assert_eq!(System::inc_providers(&0), IncRefStatus::Existed); - assert_eq!(System::dec_providers(&0).unwrap(), DecRefStatus::Exists); - assert_eq!(System::account_nonce(&0), 1); - - assert!(System::inc_consumers(&0).is_ok()); - assert_noop!(System::dec_providers(&0), DecRefError::ConsumerRemaining); - - System::dec_consumers(&0); - assert_eq!(System::dec_providers(&0).unwrap(), DecRefStatus::Reaped); - assert_eq!(System::account_nonce(&0), 0); - }); -} - #[test] fn deposit_event_should_work() { new_test_ext().execute_with(|| { From 049e718105cb30a1b73e879efa27928edfbab5fe Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Thu, 4 Mar 2021 01:00:41 -0400 Subject: [PATCH 13/31] fix system tests --- frame/system/src/extensions/check_nonce.rs | 8 +------- frame/system/src/mock.rs | 14 +------------- frame/system/src/tests.rs | 7 +++---- 3 files changed, 5 insertions(+), 24 deletions(-) diff --git a/frame/system/src/extensions/check_nonce.rs b/frame/system/src/extensions/check_nonce.rs index 07a51a1d3ae34..299c5eca7b243 100644 --- a/frame/system/src/extensions/check_nonce.rs +++ b/frame/system/src/extensions/check_nonce.rs @@ -126,13 +126,7 @@ mod tests { #[test] fn signed_ext_check_nonce_works() { new_test_ext().execute_with(|| { - crate::Account::::insert(1, crate::AccountInfo { - nonce: 1, - consumers: 0, - providers: 0, - sufficients: 0, - data: 0, - }); + crate::Account::::insert(1, 1); let info = DispatchInfo::default(); let len = 0_usize; // stale diff --git a/frame/system/src/mock.rs b/frame/system/src/mock.rs index 2b31929b5da81..00201c4ce599a 100644 --- a/frame/system/src/mock.rs +++ b/frame/system/src/mock.rs @@ -16,7 +16,6 @@ // limitations under the License. use crate::{self as frame_system, *}; -use sp_std::cell::RefCell; use sp_core::H256; use sp_runtime::{ traits::{BlakeTwo256, IdentityLookup}, @@ -75,15 +74,6 @@ parameter_types! { limits::BlockLength::max_with_normal_ratio(1024, NORMAL_DISPATCH_RATIO); } -thread_local!{ - pub static KILLED: RefCell> = RefCell::new(vec![]); -} - -pub struct RecordKilled; -impl OnKilledAccount for RecordKilled { - fn on_killed_account(who: &u64) { KILLED.with(|r| r.borrow_mut().push(*who)) } -} - impl Config for Test { type BaseCallFilter = (); type BlockWeights = RuntimeBlockWeights; @@ -102,11 +92,9 @@ impl Config for Test { type DbWeight = DbWeight; type Version = Version; type PalletInfo = PalletInfo; - type AccountData = u32; - type OnNewAccount = (); - type OnKilledAccount = RecordKilled; type SystemWeightInfo = (); type SS58Prefix = (); + type AccountStorage = System; } pub type SysEvent = frame_system::Event; diff --git a/frame/system/src/tests.rs b/frame/system/src/tests.rs index 72cd22fe44294..190dd080ca688 100644 --- a/frame/system/src/tests.rs +++ b/frame/system/src/tests.rs @@ -19,7 +19,7 @@ use crate::*; use mock::{*, Origin}; use sp_core::H256; use sp_runtime::{DispatchError, DispatchErrorWithPostInfo, traits::{Header, BlakeTwo256}}; -use frame_support::{assert_noop, weights::WithPostDispatchInfo, dispatch::PostDispatchInfo}; +use frame_support::{assert_ok, weights::WithPostDispatchInfo, dispatch::PostDispatchInfo}; #[test] fn origin_works() { @@ -377,12 +377,11 @@ fn events_not_emitted_during_genesis() { new_test_ext().execute_with(|| { // Block Number is zero at genesis assert!(System::block_number().is_zero()); - let mut account_data = AccountInfo::default(); - System::on_created_account(Default::default(), &mut account_data); + assert_ok!(System::remark_with_event(Origin::signed(1), vec![1,2,3])); assert!(System::events().is_empty()); // Events will be emitted starting on block 1 System::set_block_number(1); - System::on_created_account(Default::default(), &mut account_data); + assert_ok!(System::remark_with_event(Origin::signed(1), vec![1,2,3])); assert!(System::events().len() == 1); }); } From 73431117065935c1ce01561c7e9260b353e464b2 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Thu, 4 Mar 2021 01:20:41 -0400 Subject: [PATCH 14/31] downgrade from frame to pallet --- Cargo.lock | 70 +++++++++---------- Cargo.toml | 2 +- bin/node-template/runtime/Cargo.toml | 6 +- bin/node-template/runtime/src/lib.rs | 8 +-- bin/node/runtime/Cargo.toml | 6 +- bin/node/runtime/src/lib.rs | 8 +-- frame/accounts/Cargo.toml | 2 +- frame/accounts/src/mock.rs | 6 +- frame/assets/Cargo.toml | 2 +- frame/balances/Cargo.toml | 4 +- frame/benchmarking/src/lib.rs | 2 +- frame/benchmarking/src/utils.rs | 2 +- frame/recovery/Cargo.toml | 4 +- frame/recovery/src/lib.rs | 6 +- frame/staking/Cargo.toml | 4 +- frame/staking/fuzzer/Cargo.toml | 2 +- frame/staking/fuzzer/src/mock.rs | 7 +- frame/staking/src/lib.rs | 2 +- frame/system/Cargo.toml | 2 +- .../rpc/runtime-api/Cargo.toml | 2 +- .../rpc/runtime-api/README.md | 0 .../rpc/runtime-api/src/lib.rs | 0 test-utils/runtime/Cargo.toml | 8 +-- test-utils/runtime/src/lib.rs | 12 ++-- utils/frame/rpc/system/Cargo.toml | 2 +- utils/frame/rpc/system/src/lib.rs | 2 +- 26 files changed, 87 insertions(+), 84 deletions(-) rename frame/{accounts => system}/rpc/runtime-api/Cargo.toml (94%) rename frame/{accounts => system}/rpc/runtime-api/README.md (100%) rename frame/{accounts => system}/rpc/runtime-api/src/lib.rs (100%) diff --git a/Cargo.lock b/Cargo.lock index cccfdaf6051f6..d8439d9448754 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1681,28 +1681,6 @@ dependencies = [ "percent-encoding 2.1.0", ] -[[package]] -name = "frame-accounts" -version = "3.0.0" -dependencies = [ - "frame-support", - "frame-system", - "log", - "parity-scale-codec", - "serde", - "sp-core", - "sp-io", - "sp-std", -] - -[[package]] -name = "frame-accounts-rpc-runtime-api" -version = "3.0.0" -dependencies = [ - "parity-scale-codec", - "sp-api", -] - [[package]] name = "frame-benchmarking" version = "3.1.0" @@ -1859,10 +1837,10 @@ name = "frame-system" version = "3.0.0" dependencies = [ "criterion", - "frame-accounts", "frame-support", "impl-trait-for-tuples", "log", + "pallet-accounts", "parity-scale-codec", "serde", "sp-core", @@ -1889,6 +1867,14 @@ dependencies = [ "sp-std", ] +[[package]] +name = "frame-system-rpc-runtime-api" +version = "3.0.0" +dependencies = [ + "parity-scale-codec", + "sp-api", +] + [[package]] name = "frame-try-runtime" version = "0.9.0" @@ -4106,17 +4092,17 @@ dependencies = [ name = "node-runtime" version = "2.0.1" dependencies = [ - "frame-accounts", - "frame-accounts-rpc-runtime-api", "frame-benchmarking", "frame-executive", "frame-support", "frame-system", "frame-system-benchmarking", + "frame-system-rpc-runtime-api", "frame-try-runtime", "hex-literal", "log", "node-primitives", + "pallet-accounts", "pallet-assets", "pallet-authority-discovery", "pallet-authorship", @@ -4220,14 +4206,14 @@ dependencies = [ name = "node-template-runtime" version = "2.0.0" dependencies = [ - "frame-accounts", - "frame-accounts-rpc-runtime-api", "frame-benchmarking", "frame-executive", "frame-support", "frame-system", "frame-system-benchmarking", + "frame-system-rpc-runtime-api", "hex-literal", + "pallet-accounts", "pallet-aura", "pallet-balances", "pallet-grandpa", @@ -4449,14 +4435,28 @@ dependencies = [ "stable_deref_trait", ] +[[package]] +name = "pallet-accounts" +version = "3.0.0" +dependencies = [ + "frame-support", + "frame-system", + "log", + "parity-scale-codec", + "serde", + "sp-core", + "sp-io", + "sp-std", +] + [[package]] name = "pallet-assets" version = "3.0.0" dependencies = [ - "frame-accounts", "frame-benchmarking", "frame-support", "frame-system", + "pallet-accounts", "pallet-balances", "parity-scale-codec", "serde", @@ -4571,11 +4571,11 @@ dependencies = [ name = "pallet-balances" version = "3.0.0" dependencies = [ - "frame-accounts", "frame-benchmarking", "frame-support", "frame-system", "log", + "pallet-accounts", "pallet-transaction-payment", "parity-scale-codec", "serde", @@ -5125,9 +5125,9 @@ name = "pallet-recovery" version = "3.0.0" dependencies = [ "enumflags2", - "frame-accounts", "frame-support", "frame-system", + "pallet-accounts", "pallet-balances", "parity-scale-codec", "serde", @@ -5233,12 +5233,12 @@ dependencies = [ name = "pallet-staking" version = "3.0.0" dependencies = [ - "frame-accounts", "frame-benchmarking", "frame-support", "frame-system", "hex", "log", + "pallet-accounts", "pallet-authorship", "pallet-balances", "pallet-session", @@ -5266,10 +5266,10 @@ dependencies = [ name = "pallet-staking-fuzz" version = "0.0.0" dependencies = [ - "frame-accounts", "frame-support", "frame-system", "honggfuzz", + "pallet-accounts", "pallet-balances", "pallet-indices", "pallet-session", @@ -9240,7 +9240,7 @@ dependencies = [ name = "substrate-frame-rpc-system" version = "3.0.0" dependencies = [ - "frame-accounts-rpc-runtime-api", + "frame-system-rpc-runtime-api", "futures 0.3.12", "jsonrpc-core", "jsonrpc-core-client", @@ -9305,12 +9305,12 @@ name = "substrate-test-runtime" version = "2.0.0" dependencies = [ "cfg-if 1.0.0", - "frame-accounts", - "frame-accounts-rpc-runtime-api", "frame-support", "frame-system", + "frame-system-rpc-runtime-api", "log", "memory-db", + "pallet-accounts", "pallet-babe", "pallet-timestamp", "parity-scale-codec", diff --git a/Cargo.toml b/Cargo.toml index e6b6be19602bd..c0833412325cb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -60,7 +60,7 @@ members = [ "client/transaction-pool", "client/transaction-pool/graph", "frame/accounts", - "frame/accounts/rpc/runtime-api", + "frame/system/rpc/runtime-api", "frame/assets", "frame/atomic-swap", "frame/aura", diff --git a/bin/node-template/runtime/Cargo.toml b/bin/node-template/runtime/Cargo.toml index a4ac5f6ac8be8..eb461fdb0fbc1 100644 --- a/bin/node-template/runtime/Cargo.toml +++ b/bin/node-template/runtime/Cargo.toml @@ -15,7 +15,7 @@ codec = { package = "parity-scale-codec", version = "2.0.0", default-features = pallet-aura = { version = "3.0.0", default-features = false, path = "../../../frame/aura" } pallet-balances = { version = "3.0.0", default-features = false, path = "../../../frame/balances" } -frame-accounts = { version = "3.0.0", default-features = false, path = "../../../frame/accounts" } +pallet-accounts = { version = "3.0.0", default-features = false, path = "../../../frame/accounts" } frame-support = { version = "3.0.0", default-features = false, path = "../../../frame/support" } pallet-grandpa = { version = "3.0.0", default-features = false, path = "../../../frame/grandpa" } pallet-randomness-collective-flip = { version = "3.0.0", default-features = false, path = "../../../frame/randomness-collective-flip" } @@ -38,7 +38,7 @@ sp-transaction-pool = { version = "3.0.0", default-features = false, path = "../ sp-version = { version = "3.0.0", default-features = false, path = "../../../primitives/version" } # Used for the node template's RPCs -frame-accounts-rpc-runtime-api = { version = "3.0.0", default-features = false, path = "../../../frame/accounts/rpc/runtime-api/" } +frame-system-rpc-runtime-api = { version = "3.0.0", default-features = false, path = "../../../frame/system/rpc/runtime-api/" } pallet-transaction-payment-rpc-runtime-api = { version = "3.0.0", default-features = false, path = "../../../frame/transaction-payment/rpc/runtime-api/" } # Used for runtime benchmarking @@ -78,7 +78,7 @@ std = [ "sp-transaction-pool/std", "sp-version/std", "frame-system/std", - "frame-accounts-rpc-runtime-api/std", + "frame-system-rpc-runtime-api/std", "template/std", ] runtime-benchmarks = [ diff --git a/bin/node-template/runtime/src/lib.rs b/bin/node-template/runtime/src/lib.rs index 788c4bcbf1fec..e26ffc76c1320 100644 --- a/bin/node-template/runtime/src/lib.rs +++ b/bin/node-template/runtime/src/lib.rs @@ -185,7 +185,7 @@ impl frame_system::Config for Runtime { type AccountStorage = Accounts; } -impl frame_accounts::Config for Runtime { +impl pallet_accounts::Config for Runtime { type Event = Event; /// What to do if a new account is created. type OnNewAccount = (); @@ -277,7 +277,7 @@ construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic { System: frame_system::{Module, Call, Config, Storage, Event}, - Accounts: frame_accounts::{Module, Call, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, RandomnessCollectiveFlip: pallet_randomness_collective_flip::{Module, Call, Storage}, Timestamp: pallet_timestamp::{Module, Call, Storage, Inherent}, Aura: pallet_aura::{Module, Config}, @@ -432,10 +432,10 @@ impl_runtime_apis! { } } - impl frame_accounts_rpc_runtime_api::AccountNonceApi for Runtime { + impl frame_system_rpc_runtime_api::AccountNonceApi for Runtime { fn account_nonce(account: AccountId) -> Index { use frame_support::traits::BasicAccount; - Accounts::account_nonce(account) + Accounts::account_nonce(&account) } } diff --git a/bin/node/runtime/Cargo.toml b/bin/node/runtime/Cargo.toml index ad68e19b3416e..01d8b3b9aff39 100644 --- a/bin/node/runtime/Cargo.toml +++ b/bin/node/runtime/Cargo.toml @@ -38,13 +38,13 @@ sp-transaction-pool = { version = "3.0.0", default-features = false, path = "../ sp-version = { version = "3.0.0", default-features = false, path = "../../../primitives/version" } # frame dependencies -frame-accounts = { version = "3.0.0", default-features = false, path = "../../../frame/accounts" } +pallet-accounts = { version = "3.0.0", default-features = false, path = "../../../frame/accounts" } frame-executive = { version = "3.0.0", default-features = false, path = "../../../frame/executive" } frame-benchmarking = { version = "3.1.0", default-features = false, path = "../../../frame/benchmarking", optional = true } frame-support = { version = "3.0.0", default-features = false, path = "../../../frame/support" } frame-system = { version = "3.0.0", default-features = false, path = "../../../frame/system" } frame-system-benchmarking = { version = "3.0.0", default-features = false, path = "../../../frame/system/benchmarking", optional = true } -frame-accounts-rpc-runtime-api = { version = "3.0.0", default-features = false, path = "../../../frame/accounts/rpc/runtime-api/" } +frame-system-rpc-runtime-api = { version = "3.0.0", default-features = false, path = "../../../frame/system/rpc/runtime-api/" } frame-try-runtime = { version = "0.9.0", default-features = false, path = "../../../frame/try-runtime", optional = true } pallet-assets = { version = "3.0.0", default-features = false, path = "../../../frame/assets" } pallet-authority-discovery = { version = "3.0.0", default-features = false, path = "../../../frame/authority-discovery" } @@ -144,7 +144,7 @@ std = [ "pallet-sudo/std", "frame-support/std", "frame-benchmarking/std", - "frame-accounts-rpc-runtime-api/std", + "frame-system-rpc-runtime-api/std", "frame-system/std", "pallet-election-provider-multi-phase/std", "pallet-timestamp/std", diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index bf56db5509e74..3ebef80dd74a8 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -206,7 +206,7 @@ impl frame_system::Config for Runtime { type AccountStorage = Accounts; } -impl frame_accounts::Config for Runtime { +impl pallet_accounts::Config for Runtime { type Event = Event; type AccountData = pallet_balances::AccountData; type OnNewAccount = (); @@ -1088,7 +1088,7 @@ construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic { System: frame_system::{Module, Call, Config, Storage, Event}, - Accounts: frame_accounts::{Module, Call, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Utility: pallet_utility::{Module, Call, Event}, Babe: pallet_babe::{Module, Call, Storage, Config, ValidateUnsigned}, Timestamp: pallet_timestamp::{Module, Call, Storage, Inherent}, @@ -1332,10 +1332,10 @@ impl_runtime_apis! { } } - impl frame_accounts_rpc_runtime_api::AccountNonceApi for Runtime { + impl frame_system_rpc_runtime_api::AccountNonceApi for Runtime { fn account_nonce(account: AccountId) -> Index { use frame_support::traits::BasicAccount; - Accounts::account_nonce(account) + Accounts::account_nonce(&account) } } diff --git a/frame/accounts/Cargo.toml b/frame/accounts/Cargo.toml index 4b050014c8625..582b1b28e72cd 100644 --- a/frame/accounts/Cargo.toml +++ b/frame/accounts/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "frame-accounts" +name = "pallet-accounts" version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" diff --git a/frame/accounts/src/mock.rs b/frame/accounts/src/mock.rs index de7dffc77d815..b0e36bad1ff6d 100644 --- a/frame/accounts/src/mock.rs +++ b/frame/accounts/src/mock.rs @@ -15,7 +15,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use crate::{self as frame_accounts, *}; +use crate::{self as pallet_accounts, *}; use sp_std::cell::RefCell; use sp_core::H256; use frame_support::{ @@ -35,7 +35,7 @@ frame_support::construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, - Accounts: frame_accounts::{Module, Call, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, } ); @@ -71,7 +71,7 @@ impl frame_system::Config for Test { type AccountStorage = Accounts; } -impl frame_accounts::Config for Test { +impl pallet_accounts::Config for Test { type Event = Event; type AccountData = u32; type OnNewAccount = (); diff --git a/frame/assets/Cargo.toml b/frame/assets/Cargo.toml index 32c1f9a089e96..48646d3ae553d 100644 --- a/frame/assets/Cargo.toml +++ b/frame/assets/Cargo.toml @@ -22,7 +22,7 @@ sp-runtime = { version = "3.0.0", default-features = false, path = "../../primit frame-support = { version = "3.0.0", default-features = false, path = "../support" } # `system` module provides us with all sorts of useful stuff and macros depend on it being around. frame-system = { version = "3.0.0", default-features = false, path = "../system" } -frame-accounts = { version = "3.0.0", default-features = false, path = "../accounts" } +pallet-accounts = { version = "3.0.0", default-features = false, path = "../accounts" } frame-benchmarking = { version = "3.1.0", default-features = false, path = "../benchmarking", optional = true } [dev-dependencies] diff --git a/frame/balances/Cargo.toml b/frame/balances/Cargo.toml index 9e5cfe4259391..89d5e8a3f0b68 100644 --- a/frame/balances/Cargo.toml +++ b/frame/balances/Cargo.toml @@ -20,7 +20,7 @@ sp-runtime = { version = "3.0.0", default-features = false, path = "../../primit frame-benchmarking = { version = "3.1.0", default-features = false, path = "../benchmarking", optional = true } frame-support = { version = "3.0.0", default-features = false, path = "../support" } frame-system = { version = "3.0.0", default-features = false, path = "../system" } -frame-accounts = { version = "3.0.0", default-features = false, path = "../accounts" } +pallet-accounts = { version = "3.0.0", default-features = false, path = "../accounts" } log = { version = "0.4.14", default-features = false } [dev-dependencies] @@ -38,7 +38,7 @@ std = [ "frame-benchmarking/std", "frame-support/std", "frame-system/std", - "frame-accounts/std", + "pallet-accounts/std", "log/std", ] runtime-benchmarks = ["frame-benchmarking"] diff --git a/frame/benchmarking/src/lib.rs b/frame/benchmarking/src/lib.rs index b99437f2a6e33..ff52b461cab38 100644 --- a/frame/benchmarking/src/lib.rs +++ b/frame/benchmarking/src/lib.rs @@ -716,7 +716,7 @@ macro_rules! impl_benchmark { ::AccountStorage as $crate::frame_support::traits::BasicAccount >::hashed_key_for( - $crate::whitelisted_caller::() + &$crate::whitelisted_caller::() ); whitelist.push(whitelisted_caller_key.into()); $crate::benchmarking::set_whitelist(whitelist); diff --git a/frame/benchmarking/src/utils.rs b/frame/benchmarking/src/utils.rs index f0c294a33670a..100886dc339ae 100644 --- a/frame/benchmarking/src/utils.rs +++ b/frame/benchmarking/src/utils.rs @@ -229,7 +229,7 @@ macro_rules! whitelist_account { ::AccountStorage as $crate::frame_support::traits::BasicAccount >::hashed_key_for( - $crate::whitelisted_caller::() + &$crate::whitelisted_caller::() ).into() ); } diff --git a/frame/recovery/Cargo.toml b/frame/recovery/Cargo.toml index ced2a03210bc9..0efe8c6f5c3cc 100644 --- a/frame/recovery/Cargo.toml +++ b/frame/recovery/Cargo.toml @@ -21,7 +21,7 @@ sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/ sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } frame-support = { version = "3.0.0", default-features = false, path = "../support" } frame-system = { version = "3.0.0", default-features = false, path = "../system" } -frame-accounts = { version = "3.0.0", default-features = false, path = "../accounts" } +pallet-accounts = { version = "3.0.0", default-features = false, path = "../accounts" } [dev-dependencies] sp-core = { version = "3.0.0", path = "../../primitives/core" } @@ -37,6 +37,6 @@ std = [ "sp-runtime/std", "frame-support/std", "frame-system/std", - "frame-accounts/std", + "pallet-accounts/std", ] try-runtime = ["frame-support/try-runtime"] diff --git a/frame/recovery/src/lib.rs b/frame/recovery/src/lib.rs index 0f2182d1a1c95..3fbf18bcdd0aa 100644 --- a/frame/recovery/src/lib.rs +++ b/frame/recovery/src/lib.rs @@ -175,7 +175,7 @@ type BalanceOf = <::Currency as Currency<::AccountId>>::Balance; /// Configuration trait. -pub trait Config: frame_system::Config + frame_accounts::Config { +pub trait Config: frame_system::Config + pallet_accounts::Config { /// The overarching event type. type Event: From> + Into<::Event>; @@ -591,7 +591,7 @@ decl_module! { recovery_config.threshold as usize <= active_recovery.friends.len(), Error::::Threshold ); - frame_accounts::Module::::inc_consumers(&who).map_err(|_| Error::::BadState)?; + pallet_accounts::Module::::inc_consumers(&who).map_err(|_| Error::::BadState)?; // Create the recovery storage item Proxy::::insert(&who, &account); Self::deposit_event(RawEvent::AccountRecovered(account, who)); @@ -680,7 +680,7 @@ decl_module! { // Check `who` is allowed to make a call on behalf of `account` ensure!(Self::proxy(&who) == Some(account), Error::::NotAllowed); Proxy::::remove(&who); - frame_accounts::Module::::dec_consumers(&who); + pallet_accounts::Module::::dec_consumers(&who); } } } diff --git a/frame/staking/Cargo.toml b/frame/staking/Cargo.toml index 1d76b6f3cd58d..b85b1171c4666 100644 --- a/frame/staking/Cargo.toml +++ b/frame/staking/Cargo.toml @@ -24,7 +24,7 @@ sp-runtime = { version = "3.0.0", default-features = false, path = "../../primit sp-staking = { version = "3.0.0", default-features = false, path = "../../primitives/staking" } frame-support = { version = "3.0.0", default-features = false, path = "../support" } frame-system = { version = "3.0.0", default-features = false, path = "../system" } -frame-accounts = { version = "3.0.0", default-features = false, path = "../accounts" } +pallet-accounts = { version = "3.0.0", default-features = false, path = "../accounts" } pallet-session = { version = "3.0.0", default-features = false, features = ["historical"], path = "../session" } pallet-authorship = { version = "3.0.0", default-features = false, path = "../authorship" } sp-application-crypto = { version = "3.0.0", default-features = false, path = "../../primitives/application-crypto" } @@ -62,7 +62,7 @@ std = [ "sp-staking/std", "pallet-session/std", "frame-system/std", - "frame-accounts/std", + "pallet-accounts/std", "pallet-authorship/std", "sp-application-crypto/std", "log/std", diff --git a/frame/staking/fuzzer/Cargo.toml b/frame/staking/fuzzer/Cargo.toml index 7212c41f983a2..014e042deed05 100644 --- a/frame/staking/fuzzer/Cargo.toml +++ b/frame/staking/fuzzer/Cargo.toml @@ -22,7 +22,7 @@ pallet-indices = { version = "3.0.0", path = "../../indices" } pallet-balances = { version = "3.0.0", path = "../../balances" } pallet-timestamp = { version = "3.0.0", path = "../../timestamp" } frame-system = { version = "3.0.0", path = "../../system" } -frame-accounts = { version = "3.0.0", path = "../../accounts" } +pallet-accounts = { version = "3.0.0", path = "../../accounts" } frame-support = { version = "3.0.0", path = "../../support" } sp-std = { version = "3.0.0", path = "../../../primitives/std" } sp-io ={ version = "3.0.0", path = "../../../primitives/io" } diff --git a/frame/staking/fuzzer/src/mock.rs b/frame/staking/fuzzer/src/mock.rs index 073c28ef30400..83463b96febd4 100644 --- a/frame/staking/fuzzer/src/mock.rs +++ b/frame/staking/fuzzer/src/mock.rs @@ -34,7 +34,7 @@ frame_support::construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, - Accounts: frame_accounts::{Module, Call, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, Staking: pallet_staking::{Module, Call, Config, Storage, Event, ValidateUnsigned}, Indices: pallet_indices::{Module, Call, Storage, Config, Event}, @@ -65,7 +65,7 @@ impl frame_system::Config for Test { type AccountStorage = Accounts; } -impl frame_accounts::Config for Test { +impl pallet_accounts::Config for Test { type Event = Event; type AccountData = pallet_balances::AccountData; type OnNewAccount = (); @@ -82,6 +82,7 @@ impl pallet_balances::Config for Test { type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; type AccountStore = Accounts; + type ReferencedAccount = Accounts; type WeightInfo = (); } impl pallet_indices::Config for Test { @@ -136,6 +137,7 @@ impl pallet_session::Config for Test { type ValidatorId = AccountId; type ValidatorIdOf = pallet_staking::StashOf; type DisabledValidatorsThreshold = (); + type ReferencedAccount = Accounts; type WeightInfo = (); } pallet_staking_reward_curve::build! { @@ -198,4 +200,5 @@ impl pallet_staking::Config for Test { type OffchainSolutionWeightLimit = (); type WeightInfo = (); type ElectionProvider = MockElectionProvider; + type ReferencedAccount = Accounts; } diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index 0c4e038dca43a..6b2962c6d4556 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -780,7 +780,7 @@ impl SessionInterface<::AccountId> for T w } } -pub trait Config: frame_system::Config + frame_accounts::Config + SendTransactionTypes> { +pub trait Config: frame_system::Config + pallet_accounts::Config + SendTransactionTypes> { /// The staking balance. type Currency: LockableCurrency; diff --git a/frame/system/Cargo.toml b/frame/system/Cargo.toml index d5e6456f21960..8c256a38ce6ef 100644 --- a/frame/system/Cargo.toml +++ b/frame/system/Cargo.toml @@ -28,7 +28,7 @@ log = { version = "0.4.14", default-features = false } criterion = "0.3.3" sp-externalities = { version = "0.9.0", path = "../../primitives/externalities" } substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/runtime/client" } -frame-accounts = { version = "3.0.0", path = "../accounts" } +pallet-accounts = { version = "3.0.0", path = "../accounts" } [features] default = ["std"] diff --git a/frame/accounts/rpc/runtime-api/Cargo.toml b/frame/system/rpc/runtime-api/Cargo.toml similarity index 94% rename from frame/accounts/rpc/runtime-api/Cargo.toml rename to frame/system/rpc/runtime-api/Cargo.toml index d7313db74729f..6c2a8565a627d 100644 --- a/frame/accounts/rpc/runtime-api/Cargo.toml +++ b/frame/system/rpc/runtime-api/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "frame-accounts-rpc-runtime-api" +name = "frame-system-rpc-runtime-api" version = "3.0.0" authors = ["Parity Technologies "] edition = "2018" diff --git a/frame/accounts/rpc/runtime-api/README.md b/frame/system/rpc/runtime-api/README.md similarity index 100% rename from frame/accounts/rpc/runtime-api/README.md rename to frame/system/rpc/runtime-api/README.md diff --git a/frame/accounts/rpc/runtime-api/src/lib.rs b/frame/system/rpc/runtime-api/src/lib.rs similarity index 100% rename from frame/accounts/rpc/runtime-api/src/lib.rs rename to frame/system/rpc/runtime-api/src/lib.rs diff --git a/test-utils/runtime/Cargo.toml b/test-utils/runtime/Cargo.toml index 28dd886e8fddb..56cec2156990e 100644 --- a/test-utils/runtime/Cargo.toml +++ b/test-utils/runtime/Cargo.toml @@ -32,9 +32,9 @@ sp-session = { version = "3.0.0", default-features = false, path = "../../primit sp-api = { version = "3.0.0", default-features = false, path = "../../primitives/api" } sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } pallet-babe = { version = "3.0.0", default-features = false, path = "../../frame/babe" } -frame-accounts = { version = "3.0.0", default-features = false, path = "../../frame/accounts" } +pallet-accounts = { version = "3.0.0", default-features = false, path = "../../frame/accounts" } frame-system = { version = "3.0.0", default-features = false, path = "../../frame/system" } -frame-accounts-rpc-runtime-api = { version = "3.0.0", default-features = false, path = "../../frame/accounts/rpc/runtime-api" } +frame-system-rpc-runtime-api = { version = "3.0.0", default-features = false, path = "../../frame/system/rpc/runtime-api" } pallet-timestamp = { version = "3.0.0", default-features = false, path = "../../frame/timestamp" } sp-finality-grandpa = { version = "3.0.0", default-features = false, path = "../../primitives/finality-grandpa" } sp-trie = { version = "3.0.0", default-features = false, path = "../../primitives/trie" } @@ -87,9 +87,9 @@ std = [ "sp-externalities/std", "sp-state-machine/std", "pallet-babe/std", - "frame-accounts-rpc-runtime-api/std", + "frame-system-rpc-runtime-api/std", "frame-system/std", - "frame-accounts/std", + "pallet-accounts/std", "pallet-timestamp/std", "sc-service", "sp-finality-grandpa/std", diff --git a/test-utils/runtime/src/lib.rs b/test-utils/runtime/src/lib.rs index dcc74ea669e34..d93fde961cbb1 100644 --- a/test-utils/runtime/src/lib.rs +++ b/test-utils/runtime/src/lib.rs @@ -448,8 +448,8 @@ impl From> for Event { } } -impl From> for Event { - fn from(_evt: frame_accounts::Event) -> Self { +impl From> for Event { + fn from(_evt: pallet_accounts::Event) -> Self { unimplemented!("Not required in tests!") } } @@ -498,7 +498,7 @@ parameter_types! { BlockWeights::with_sensible_defaults(4 * 1024 * 1024, Perbill::from_percent(75)); } -impl frame_accounts::Config for Runtime { +impl pallet_accounts::Config for Runtime { type Event = Event; type AccountData = (); type OnNewAccount = (); @@ -525,7 +525,7 @@ impl frame_system::Config for Runtime { type PalletInfo = Self; type SystemWeightInfo = (); type SS58Prefix = (); - type AccountStorage = frame_accounts::Pallet::; + type AccountStorage = pallet_accounts::Pallet::; } impl pallet_timestamp::Config for Runtime { @@ -866,7 +866,7 @@ cfg_if! { } } - impl frame_accounts_rpc_runtime_api::AccountNonceApi for Runtime { + impl frame_system_rpc_runtime_api::AccountNonceApi for Runtime { fn account_nonce(_account: AccountId) -> Index { 0 } @@ -1101,7 +1101,7 @@ cfg_if! { } } - impl frame_accounts_rpc_runtime_api::AccountNonceApi for Runtime { + impl frame_system_rpc_runtime_api::AccountNonceApi for Runtime { fn account_nonce(_account: AccountId) -> Index { 0 } diff --git a/utils/frame/rpc/system/Cargo.toml b/utils/frame/rpc/system/Cargo.toml index 2e2ce63ece4f2..ea8d97a82ad34 100644 --- a/utils/frame/rpc/system/Cargo.toml +++ b/utils/frame/rpc/system/Cargo.toml @@ -23,7 +23,7 @@ log = "0.4.8" serde = { version = "1.0.101", features = ["derive"] } sp-runtime = { version = "3.0.0", path = "../../../../primitives/runtime" } sp-api = { version = "3.0.0", path = "../../../../primitives/api" } -frame-accounts-rpc-runtime-api = { version = "3.0.0", path = "../../../../frame/accounts/rpc/runtime-api" } +frame-system-rpc-runtime-api = { version = "3.0.0", path = "../../../../frame/system/rpc/runtime-api" } sp-core = { version = "3.0.0", path = "../../../../primitives/core" } sp-blockchain = { version = "3.0.0", path = "../../../../primitives/blockchain" } sp-transaction-pool = { version = "3.0.0", path = "../../../../primitives/transaction-pool" } diff --git a/utils/frame/rpc/system/src/lib.rs b/utils/frame/rpc/system/src/lib.rs index 8442db03e593d..57c0cda9cca3a 100644 --- a/utils/frame/rpc/system/src/lib.rs +++ b/utils/frame/rpc/system/src/lib.rs @@ -40,7 +40,7 @@ use sp_transaction_pool::{TransactionPool, InPoolTransaction}; use sp_block_builder::BlockBuilder; use sc_rpc_api::DenyUnsafe; -pub use frame_accounts_rpc_runtime_api::AccountNonceApi; +pub use frame_system_rpc_runtime_api::AccountNonceApi; pub use self::gen_client::Client as SystemClient; /// Future that resolves to account nonce. From 22446755ec19bb7d8a8b0999d3e30635b8476247 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Thu, 4 Mar 2021 18:42:58 -0400 Subject: [PATCH 15/31] fixing up tests --- Cargo.lock | 1 + bin/node-template/pallets/template/src/mock.rs | 6 ++---- frame/assets/Cargo.toml | 1 + frame/assets/src/lib.rs | 16 ++++++++++++---- frame/atomic-swap/src/tests.rs | 6 ++---- frame/aura/src/mock.rs | 4 +--- frame/authority-discovery/src/lib.rs | 4 +--- frame/authorship/src/lib.rs | 4 +--- frame/balances/src/tests_local.rs | 4 +--- frame/balances/src/tests_reentrancy.rs | 4 +--- frame/benchmarking/src/tests.rs | 4 +--- frame/collective/src/lib.rs | 4 +--- frame/example-offchain-worker/src/tests.rs | 4 +--- frame/example-parallel/src/tests.rs | 4 +--- frame/example/src/lib.rs | 6 ++---- frame/executive/src/lib.rs | 6 ++---- frame/im-online/src/mock.rs | 4 +--- frame/lottery/Cargo.toml | 1 + frame/lottery/src/mock.rs | 15 +++++++++++---- frame/membership/src/lib.rs | 4 +--- frame/merkle-mountain-range/src/mock.rs | 4 +--- frame/nicks/src/lib.rs | 6 ++---- frame/node-authorization/src/lib.rs | 4 +--- frame/offences/src/mock.rs | 4 +--- frame/randomness-collective-flip/src/lib.rs | 4 +--- frame/scheduler/src/lib.rs | 4 +--- frame/session/src/mock.rs | 4 +--- frame/sudo/src/mock.rs | 4 +--- frame/support/test/tests/pallet.rs | 4 +--- frame/support/test/tests/pallet_compatibility.rs | 4 +--- .../test/tests/pallet_compatibility_instance.rs | 4 +--- frame/support/test/tests/pallet_instance.rs | 4 +--- frame/support/test/tests/pallet_version.rs | 4 +--- .../tests/pallet_with_name_trait_is_valid.rs | 4 +--- frame/system/benches/bench.rs | 4 +--- frame/system/benchmarking/src/mock.rs | 4 +--- frame/timestamp/src/lib.rs | 4 +--- test-utils/runtime/src/lib.rs | 4 +--- 38 files changed, 64 insertions(+), 112 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d8439d9448754..c02302e80d5d8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4941,6 +4941,7 @@ dependencies = [ "frame-benchmarking", "frame-support", "frame-system", + "pallet-accounts", "pallet-balances", "parity-scale-codec", "serde", diff --git a/bin/node-template/pallets/template/src/mock.rs b/bin/node-template/pallets/template/src/mock.rs index d33670f2e9cb0..f2ab9e3492853 100644 --- a/bin/node-template/pallets/template/src/mock.rs +++ b/bin/node-template/pallets/template/src/mock.rs @@ -7,7 +7,7 @@ use sp_runtime::{ use frame_system as system; type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; -type Block = frame_system::mocking::MockBlock; +type Block = frame_system::mocking::MockBlock; // Configure a mock runtime to test the pallet. frame_support::construct_runtime!( @@ -44,9 +44,7 @@ impl system::Config for Test { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = (); - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = System; type SystemWeightInfo = (); type SS58Prefix = SS58Prefix; } diff --git a/frame/assets/Cargo.toml b/frame/assets/Cargo.toml index 48646d3ae553d..055976198f884 100644 --- a/frame/assets/Cargo.toml +++ b/frame/assets/Cargo.toml @@ -30,6 +30,7 @@ sp-core = { version = "3.0.0", path = "../../primitives/core" } sp-std = { version = "3.0.0", path = "../../primitives/std" } sp-io = { version = "3.0.0", path = "../../primitives/io" } pallet-balances = { version = "3.0.0", path = "../balances" } +pallet-accounts = { version = "3.0.0", path = "../accounts" } [features] default = ["std"] diff --git a/frame/assets/src/lib.rs b/frame/assets/src/lib.rs index aba9e062b8c44..b5ae740b80db9 100644 --- a/frame/assets/src/lib.rs +++ b/frame/assets/src/lib.rs @@ -1162,6 +1162,7 @@ mod tests { UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, Assets: pallet_assets::{Module, Call, Storage, Event}, } @@ -1188,13 +1189,18 @@ mod tests { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = Accounts; type SystemWeightInfo = (); type SS58Prefix = (); } + impl pallet_accounts::Config for Test { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = pallet_balances::AccountData; + } + parameter_types! { pub const ExistentialDeposit: u64 = 1; } @@ -1205,7 +1211,8 @@ mod tests { type DustRemoval = (); type Event = Event; type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; + type AccountStore = Accounts; + type ReferencedAccount = Accounts; type WeightInfo = (); } @@ -1228,6 +1235,7 @@ mod tests { type StringLimit = StringLimit; type MetadataDepositBase = MetadataDepositBase; type MetadataDepositPerByte = MetadataDepositPerByte; + type ReferencedAccount = Accounts; type WeightInfo = (); } diff --git a/frame/atomic-swap/src/tests.rs b/frame/atomic-swap/src/tests.rs index 977b17f8710e3..5694af6672f2f 100644 --- a/frame/atomic-swap/src/tests.rs +++ b/frame/atomic-swap/src/tests.rs @@ -48,9 +48,7 @@ impl frame_system::Config for Test { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = System; type SystemWeightInfo = (); type SS58Prefix = (); } @@ -63,7 +61,7 @@ impl pallet_balances::Config for Test { type DustRemoval = (); type Event = Event; type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; + type AccountStore = Balances; type WeightInfo = (); } parameter_types! { diff --git a/frame/aura/src/mock.rs b/frame/aura/src/mock.rs index a5ef12f5935f1..3e15844a18fdb 100644 --- a/frame/aura/src/mock.rs +++ b/frame/aura/src/mock.rs @@ -65,9 +65,7 @@ impl frame_system::Config for Test { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = (); - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = System; type SystemWeightInfo = (); type SS58Prefix = (); } diff --git a/frame/authority-discovery/src/lib.rs b/frame/authority-discovery/src/lib.rs index cc3f41f59ed89..4150acd0a0983 100644 --- a/frame/authority-discovery/src/lib.rs +++ b/frame/authority-discovery/src/lib.rs @@ -195,9 +195,7 @@ mod tests { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = (); - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = System; type SystemWeightInfo = (); type SS58Prefix = (); } diff --git a/frame/authorship/src/lib.rs b/frame/authorship/src/lib.rs index 3d89ab24d01cf..cabe5475ed162 100644 --- a/frame/authorship/src/lib.rs +++ b/frame/authorship/src/lib.rs @@ -442,9 +442,7 @@ mod tests { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = (); - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = System; type SystemWeightInfo = (); type SS58Prefix = (); } diff --git a/frame/balances/src/tests_local.rs b/frame/balances/src/tests_local.rs index 02088e88b98ec..e0f0e257a05da 100644 --- a/frame/balances/src/tests_local.rs +++ b/frame/balances/src/tests_local.rs @@ -72,9 +72,7 @@ impl frame_system::Config for Test { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = (); - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = System; type SystemWeightInfo = (); type SS58Prefix = (); } diff --git a/frame/balances/src/tests_reentrancy.rs b/frame/balances/src/tests_reentrancy.rs index 020c514b6317c..eb6e0603cac25 100644 --- a/frame/balances/src/tests_reentrancy.rs +++ b/frame/balances/src/tests_reentrancy.rs @@ -85,9 +85,7 @@ impl frame_system::Config for Test { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = (); - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = System; type SystemWeightInfo = (); type SS58Prefix = (); } diff --git a/frame/benchmarking/src/tests.rs b/frame/benchmarking/src/tests.rs index 8431f3e46c277..e0497e40a75b1 100644 --- a/frame/benchmarking/src/tests.rs +++ b/frame/benchmarking/src/tests.rs @@ -99,9 +99,7 @@ impl frame_system::Config for Test { type BlockHashCount = (); type Version = (); type PalletInfo = PalletInfo; - type AccountData = (); - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = System; type SystemWeightInfo = (); type SS58Prefix = (); } diff --git a/frame/collective/src/lib.rs b/frame/collective/src/lib.rs index a8184b8dd5283..88917674cf6a9 100644 --- a/frame/collective/src/lib.rs +++ b/frame/collective/src/lib.rs @@ -999,9 +999,7 @@ mod tests { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = (); - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = System; type SystemWeightInfo = (); type SS58Prefix = (); } diff --git a/frame/example-offchain-worker/src/tests.rs b/frame/example-offchain-worker/src/tests.rs index 6f73ffcb9e151..ee3f421f0a2d8 100644 --- a/frame/example-offchain-worker/src/tests.rs +++ b/frame/example-offchain-worker/src/tests.rs @@ -77,9 +77,7 @@ impl frame_system::Config for Test { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = (); - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = System; type SystemWeightInfo = (); type SS58Prefix = (); } diff --git a/frame/example-parallel/src/tests.rs b/frame/example-parallel/src/tests.rs index da2892c67d42a..08fa6bb7883ba 100644 --- a/frame/example-parallel/src/tests.rs +++ b/frame/example-parallel/src/tests.rs @@ -61,9 +61,7 @@ impl frame_system::Config for Test { type BlockWeights = (); type BlockLength = (); type Version = (); - type AccountData = (); - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = System; type SystemWeightInfo = (); type SS58Prefix = (); } diff --git a/frame/example/src/lib.rs b/frame/example/src/lib.rs index b4ae35c5508a9..77368fa70e886 100644 --- a/frame/example/src/lib.rs +++ b/frame/example/src/lib.rs @@ -787,9 +787,7 @@ mod tests { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = System; type SystemWeightInfo = (); type SS58Prefix = (); } @@ -802,7 +800,7 @@ mod tests { type DustRemoval = (); type Event = Event; type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; + type AccountStore = Balances; type WeightInfo = (); } impl Config for Test { diff --git a/frame/executive/src/lib.rs b/frame/executive/src/lib.rs index 9485e75bbdecf..2664539b4e080 100644 --- a/frame/executive/src/lib.rs +++ b/frame/executive/src/lib.rs @@ -634,9 +634,7 @@ mod tests { type BlockHashCount = BlockHashCount; type Version = RuntimeVersion; type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = System; type SystemWeightInfo = (); type SS58Prefix = (); } @@ -650,7 +648,7 @@ mod tests { type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; + type AccountStore = Balances; type MaxLocks = (); type WeightInfo = (); } diff --git a/frame/im-online/src/mock.rs b/frame/im-online/src/mock.rs index 1b80f5b12dedb..dcafdb37dcbb6 100644 --- a/frame/im-online/src/mock.rs +++ b/frame/im-online/src/mock.rs @@ -131,9 +131,7 @@ impl frame_system::Config for Runtime { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = (); - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = System; type SystemWeightInfo = (); type SS58Prefix = (); } diff --git a/frame/lottery/Cargo.toml b/frame/lottery/Cargo.toml index 0d60b0aaca35d..d9fb9e4338ea2 100644 --- a/frame/lottery/Cargo.toml +++ b/frame/lottery/Cargo.toml @@ -22,6 +22,7 @@ frame-system = { version = "3.0.0", default-features = false, path = "../system" frame-benchmarking = { version = "3.1.0", default-features = false, path = "../benchmarking", optional = true } [dev-dependencies] +pallet-accounts = { version = "3.0.0", path = "../accounts" } pallet-balances = { version = "3.0.0", path = "../balances" } sp-core = { version = "3.0.0", path = "../../primitives/core" } sp-io = { version = "3.0.0", path = "../../primitives/io" } diff --git a/frame/lottery/src/mock.rs b/frame/lottery/src/mock.rs index ea73ee190e6dd..dcedf676dc3f4 100644 --- a/frame/lottery/src/mock.rs +++ b/frame/lottery/src/mock.rs @@ -42,6 +42,7 @@ frame_support::construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, Lottery: pallet_lottery::{Module, Call, Storage, Event}, } @@ -72,13 +73,18 @@ impl frame_system::Config for Test { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = System; type SystemWeightInfo = (); type SS58Prefix = (); } +impl pallet_accounts::Config for Test { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = pallet_balances::AccountData; +} + parameter_types! { pub const ExistentialDeposit: u64 = 1; } @@ -89,7 +95,8 @@ impl pallet_balances::Config for Test { type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; + type AccountStore = Accounts; + type ReferencedAccount = Accounts; type WeightInfo = (); } diff --git a/frame/membership/src/lib.rs b/frame/membership/src/lib.rs index f080938095445..043c2a1e7739a 100644 --- a/frame/membership/src/lib.rs +++ b/frame/membership/src/lib.rs @@ -323,9 +323,7 @@ mod tests { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = (); - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = System; type SystemWeightInfo = (); type SS58Prefix = (); } diff --git a/frame/merkle-mountain-range/src/mock.rs b/frame/merkle-mountain-range/src/mock.rs index 0adb0294d5080..1ff4e122a74c9 100644 --- a/frame/merkle-mountain-range/src/mock.rs +++ b/frame/merkle-mountain-range/src/mock.rs @@ -66,9 +66,7 @@ impl frame_system::Config for Test { type BlockLength = (); type Version = (); type PalletInfo = PalletInfo; - type AccountData = (); - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = System; type SystemWeightInfo = (); type SS58Prefix = (); } diff --git a/frame/nicks/src/lib.rs b/frame/nicks/src/lib.rs index 681a45626fbca..7c03225fa1446 100644 --- a/frame/nicks/src/lib.rs +++ b/frame/nicks/src/lib.rs @@ -286,9 +286,7 @@ mod tests { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = System; type SystemWeightInfo = (); type SS58Prefix = (); } @@ -301,7 +299,7 @@ mod tests { type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; + type AccountStore = Balances; type WeightInfo = (); } parameter_types! { diff --git a/frame/node-authorization/src/lib.rs b/frame/node-authorization/src/lib.rs index 090be28492630..4f4bd5c69eef5 100644 --- a/frame/node-authorization/src/lib.rs +++ b/frame/node-authorization/src/lib.rs @@ -481,9 +481,7 @@ mod tests { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = (); - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = System; type SystemWeightInfo = (); type SS58Prefix = (); } diff --git a/frame/offences/src/mock.rs b/frame/offences/src/mock.rs index c47a9cf943c18..fd96e4f05bb45 100644 --- a/frame/offences/src/mock.rs +++ b/frame/offences/src/mock.rs @@ -119,9 +119,7 @@ impl frame_system::Config for Runtime { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = (); - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = System; type SystemWeightInfo = (); type SS58Prefix = (); } diff --git a/frame/randomness-collective-flip/src/lib.rs b/frame/randomness-collective-flip/src/lib.rs index 0dba6727da60a..396d483785551 100644 --- a/frame/randomness-collective-flip/src/lib.rs +++ b/frame/randomness-collective-flip/src/lib.rs @@ -182,9 +182,7 @@ mod tests { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = (); - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = System; type SystemWeightInfo = (); type SS58Prefix = (); } diff --git a/frame/scheduler/src/lib.rs b/frame/scheduler/src/lib.rs index 5cab10b0aff38..d1e0f464b8874 100644 --- a/frame/scheduler/src/lib.rs +++ b/frame/scheduler/src/lib.rs @@ -830,9 +830,7 @@ mod tests { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = (); - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = System; type SystemWeightInfo = (); type SS58Prefix = (); } diff --git a/frame/session/src/mock.rs b/frame/session/src/mock.rs index 73499bf739b87..0e426dfb6d37e 100644 --- a/frame/session/src/mock.rs +++ b/frame/session/src/mock.rs @@ -245,9 +245,7 @@ impl frame_system::Config for Test { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = (); - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = System; type SystemWeightInfo = (); type SS58Prefix = (); } diff --git a/frame/sudo/src/mock.rs b/frame/sudo/src/mock.rs index 91cd03ac4756a..ce3d425b32286 100644 --- a/frame/sudo/src/mock.rs +++ b/frame/sudo/src/mock.rs @@ -118,9 +118,7 @@ impl frame_system::Config for Test { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = (); - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = System; type SystemWeightInfo = (); type SS58Prefix = (); } diff --git a/frame/support/test/tests/pallet.rs b/frame/support/test/tests/pallet.rs index a31ce9d91ae2d..bf5fc1a46f57e 100644 --- a/frame/support/test/tests/pallet.rs +++ b/frame/support/test/tests/pallet.rs @@ -368,9 +368,7 @@ impl frame_system::Config for Runtime { type DbWeight = (); type Version = (); type PalletInfo = PalletInfo; - type AccountData = (); - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = System; type SystemWeightInfo = (); type SS58Prefix = (); } diff --git a/frame/support/test/tests/pallet_compatibility.rs b/frame/support/test/tests/pallet_compatibility.rs index 5b9001e0475fe..c3b6aa0759750 100644 --- a/frame/support/test/tests/pallet_compatibility.rs +++ b/frame/support/test/tests/pallet_compatibility.rs @@ -220,9 +220,7 @@ impl frame_system::Config for Runtime { type DbWeight = (); type Version = (); type PalletInfo = PalletInfo; - type AccountData = (); - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = System; type SystemWeightInfo = (); type SS58Prefix = (); } diff --git a/frame/support/test/tests/pallet_compatibility_instance.rs b/frame/support/test/tests/pallet_compatibility_instance.rs index d7de03ea46cfd..2c1400eccf718 100644 --- a/frame/support/test/tests/pallet_compatibility_instance.rs +++ b/frame/support/test/tests/pallet_compatibility_instance.rs @@ -212,9 +212,7 @@ impl frame_system::Config for Runtime { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = (); - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = System; type SystemWeightInfo = (); type SS58Prefix = (); } diff --git a/frame/support/test/tests/pallet_instance.rs b/frame/support/test/tests/pallet_instance.rs index 62654d53e19d7..b1f6577c0cc86 100644 --- a/frame/support/test/tests/pallet_instance.rs +++ b/frame/support/test/tests/pallet_instance.rs @@ -255,9 +255,7 @@ impl frame_system::Config for Runtime { type DbWeight = (); type Version = (); type PalletInfo = PalletInfo; - type AccountData = (); - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = System; type SystemWeightInfo = (); type SS58Prefix = (); } diff --git a/frame/support/test/tests/pallet_version.rs b/frame/support/test/tests/pallet_version.rs index 4cc93d395db2a..b28b2ed948d86 100644 --- a/frame/support/test/tests/pallet_version.rs +++ b/frame/support/test/tests/pallet_version.rs @@ -161,9 +161,7 @@ impl frame_system::Config for Runtime { type DbWeight = (); type Version = (); type PalletInfo = PalletInfo; - type AccountData = (); - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = System; type SystemWeightInfo = (); type SS58Prefix = (); } diff --git a/frame/support/test/tests/pallet_with_name_trait_is_valid.rs b/frame/support/test/tests/pallet_with_name_trait_is_valid.rs index b09beb04cd17c..2f8ad42358bbf 100644 --- a/frame/support/test/tests/pallet_with_name_trait_is_valid.rs +++ b/frame/support/test/tests/pallet_with_name_trait_is_valid.rs @@ -136,9 +136,7 @@ mod tests { type BlockLength = (); type Version = (); type PalletInfo = PalletInfo; - type AccountData = (); - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = System; type SystemWeightInfo = (); type SS58Prefix = (); } diff --git a/frame/system/benches/bench.rs b/frame/system/benches/bench.rs index 6ed3d456826c2..d8de464f33aaa 100644 --- a/frame/system/benches/bench.rs +++ b/frame/system/benches/bench.rs @@ -84,9 +84,7 @@ impl system::Config for Runtime { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = (); - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = System; type SystemWeightInfo = (); type SS58Prefix = (); } diff --git a/frame/system/benchmarking/src/mock.rs b/frame/system/benchmarking/src/mock.rs index edc5dfebbd106..447dde72d165f 100644 --- a/frame/system/benchmarking/src/mock.rs +++ b/frame/system/benchmarking/src/mock.rs @@ -56,9 +56,7 @@ impl frame_system::Config for Test { type BlockHashCount = (); type Version = (); type PalletInfo = PalletInfo; - type AccountData = (); - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = System; type SystemWeightInfo = (); type SS58Prefix = (); } diff --git a/frame/timestamp/src/lib.rs b/frame/timestamp/src/lib.rs index 0deef258ed5bd..568d4298eeebf 100644 --- a/frame/timestamp/src/lib.rs +++ b/frame/timestamp/src/lib.rs @@ -347,9 +347,7 @@ mod tests { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = (); - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = System; type SystemWeightInfo = (); type SS58Prefix = (); } diff --git a/test-utils/runtime/src/lib.rs b/test-utils/runtime/src/lib.rs index d93fde961cbb1..7095c56b49e74 100644 --- a/test-utils/runtime/src/lib.rs +++ b/test-utils/runtime/src/lib.rs @@ -500,9 +500,7 @@ parameter_types! { impl pallet_accounts::Config for Runtime { type Event = Event; - type AccountData = (); - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = System; } impl frame_system::Config for Runtime { From c96b5e5578427546e89ee1d021433c69c9e88098 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Thu, 4 Mar 2021 19:26:15 -0400 Subject: [PATCH 16/31] fix more tests --- Cargo.lock | 3 ++ frame/atomic-swap/Cargo.toml | 1 + frame/atomic-swap/src/tests.rs | 14 ++++++- frame/authority-discovery/Cargo.toml | 1 + frame/authority-discovery/src/lib.rs | 9 ++++ frame/babe/Cargo.toml | 1 + frame/babe/src/mock.rs | 17 ++++++-- frame/balances/Cargo.toml | 1 + frame/balances/src/tests.rs | 57 +++++++++++++------------- frame/balances/src/tests_composite.rs | 14 +++++-- frame/balances/src/tests_local.rs | 14 +++++-- frame/balances/src/tests_reentrancy.rs | 12 +++++- 12 files changed, 101 insertions(+), 43 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c02302e80d5d8..eb6fa16bb5a7e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4472,6 +4472,7 @@ version = "3.0.0" dependencies = [ "frame-support", "frame-system", + "pallet-accounts", "pallet-balances", "parity-scale-codec", "serde", @@ -4508,6 +4509,7 @@ version = "3.0.0" dependencies = [ "frame-support", "frame-system", + "pallet-accounts", "pallet-session", "parity-scale-codec", "serde", @@ -4545,6 +4547,7 @@ dependencies = [ "frame-support", "frame-system", "log", + "pallet-accounts", "pallet-authorship", "pallet-balances", "pallet-offences", diff --git a/frame/atomic-swap/Cargo.toml b/frame/atomic-swap/Cargo.toml index a3b62d65e56a3..bb9921b2956e2 100644 --- a/frame/atomic-swap/Cargo.toml +++ b/frame/atomic-swap/Cargo.toml @@ -24,6 +24,7 @@ sp-core = { version = "3.0.0", default-features = false, path = "../../primitive [dev-dependencies] pallet-balances = { version = "3.0.0", path = "../balances" } +pallet-accounts = { version = "3.0.0", path = "../accounts" } [features] default = ["std"] diff --git a/frame/atomic-swap/src/tests.rs b/frame/atomic-swap/src/tests.rs index 5694af6672f2f..0a3fcb4c87860 100644 --- a/frame/atomic-swap/src/tests.rs +++ b/frame/atomic-swap/src/tests.rs @@ -20,6 +20,7 @@ frame_support::construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, AtomicSwap: pallet_atomic_swap::{Module, Call, Event}, } @@ -48,10 +49,18 @@ impl frame_system::Config for Test { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountStorage = System; + type AccountStorage = Accounts; type SystemWeightInfo = (); type SS58Prefix = (); } + +impl pallet_accounts::Config for Test { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = pallet_balances::AccountData; +} + parameter_types! { pub const ExistentialDeposit: u64 = 1; } @@ -61,7 +70,8 @@ impl pallet_balances::Config for Test { type DustRemoval = (); type Event = Event; type ExistentialDeposit = ExistentialDeposit; - type AccountStore = Balances; + type AccountStore = Accounts; + type ReferencedAccount = Accounts; type WeightInfo = (); } parameter_types! { diff --git a/frame/authority-discovery/Cargo.toml b/frame/authority-discovery/Cargo.toml index 85844cf716f03..0d0b24c43b838 100644 --- a/frame/authority-discovery/Cargo.toml +++ b/frame/authority-discovery/Cargo.toml @@ -24,6 +24,7 @@ frame-support = { version = "3.0.0", default-features = false, path = "../suppor frame-system = { version = "3.0.0", default-features = false, path = "../system" } [dev-dependencies] +pallet-accounts = { version = "3.0.0", path = "../accounts" } sp-core = { version = "3.0.0", path = "../../primitives/core" } sp-io = { version = "3.0.0", path = "../../primitives/io" } sp-staking = { version = "3.0.0", default-features = false, path = "../../primitives/staking" } diff --git a/frame/authority-discovery/src/lib.rs b/frame/authority-discovery/src/lib.rs index 4150acd0a0983..99300bc57c374 100644 --- a/frame/authority-discovery/src/lib.rs +++ b/frame/authority-discovery/src/lib.rs @@ -137,6 +137,7 @@ mod tests { UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Session: pallet_session::{Module, Call, Storage, Event, Config}, AuthorityDiscovery: pallet_authority_discovery::{Module, Call, Config}, } @@ -144,6 +145,13 @@ mod tests { impl Config for Test {} + impl pallet_accounts::Config for Test { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = (); + } + parameter_types! { pub const DisabledValidatorsThreshold: Perbill = Perbill::from_percent(33); } @@ -158,6 +166,7 @@ mod tests { type ValidatorIdOf = ConvertInto; type DisabledValidatorsThreshold = DisabledValidatorsThreshold; type NextSessionRotation = pallet_session::PeriodicSessions; + type ReferencedAccount = Accounts; type WeightInfo = (); } diff --git a/frame/babe/Cargo.toml b/frame/babe/Cargo.toml index 2d7467d82e5b7..73780663a6b04 100644 --- a/frame/babe/Cargo.toml +++ b/frame/babe/Cargo.toml @@ -33,6 +33,7 @@ sp-timestamp = { version = "3.0.0", default-features = false, path = "../../prim log = { version = "0.4.14", default-features = false } [dev-dependencies] +pallet-accounts = { version = "3.0.0", path = "../accounts" } pallet-balances = { version = "3.0.0", path = "../balances" } pallet-offences = { version = "3.0.0", path = "../offences" } pallet-staking = { version = "3.0.0", path = "../staking" } diff --git a/frame/babe/src/mock.rs b/frame/babe/src/mock.rs index 412f13f6a2df8..76b483bea5d87 100644 --- a/frame/babe/src/mock.rs +++ b/frame/babe/src/mock.rs @@ -52,6 +52,7 @@ frame_support::construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, Historical: pallet_session_historical::{Module}, Offences: pallet_offences::{Module, Call, Storage, Event}, @@ -87,13 +88,18 @@ impl frame_system::Config for Test { type Event = Event; type BlockHashCount = BlockHashCount; type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = Accounts; type SystemWeightInfo = (); type SS58Prefix = (); } +impl pallet_accounts::Config for Test { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = pallet_balances::AccountData; +} + impl frame_system::offchain::SendTransactionTypes for Test where Call: From, @@ -118,6 +124,7 @@ impl pallet_session::Config for Test { type SessionHandler = ::KeyTypeIdProviders; type Keys = MockSessionKeys; type DisabledValidatorsThreshold = DisabledValidatorsThreshold; + type ReferencedAccount = Accounts; type WeightInfo = (); } @@ -158,7 +165,8 @@ impl pallet_balances::Config for Test { type DustRemoval = (); type Event = Event; type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; + type AccountStore = Accounts; + type ReferencedAccount = Accounts; type WeightInfo = (); } @@ -214,6 +222,7 @@ impl pallet_staking::Config for Test { type MinSolutionScoreBump = (); type OffchainSolutionWeightLimit = (); type ElectionProvider = onchain::OnChainSequentialPhragmen; + type ReferencedAccount = Accounts; type WeightInfo = (); } diff --git a/frame/balances/Cargo.toml b/frame/balances/Cargo.toml index 89d5e8a3f0b68..4cabf26123fd4 100644 --- a/frame/balances/Cargo.toml +++ b/frame/balances/Cargo.toml @@ -27,6 +27,7 @@ log = { version = "0.4.14", default-features = false } sp-io = { version = "3.0.0", path = "../../primitives/io" } sp-core = { version = "3.0.0", path = "../../primitives/core" } pallet-transaction-payment = { version = "3.0.0", path = "../transaction-payment" } +pallet-accounts = { version = "3.0.0", path = "../accounts" } [features] default = ["std"] diff --git a/frame/balances/src/tests.rs b/frame/balances/src/tests.rs index 776cda140efb8..c6803233edca4 100644 --- a/frame/balances/src/tests.rs +++ b/frame/balances/src/tests.rs @@ -29,7 +29,8 @@ macro_rules! decl_tests { assert_noop, assert_storage_noop, assert_ok, assert_err, StorageValue, traits::{ LockableCurrency, LockIdentifier, WithdrawReasons, - Currency, ReservableCurrency, ExistenceRequirement::AllowDeath + Currency, ReservableCurrency, ExistenceRequirement::AllowDeath, + BasicAccount, ReferencedAccount, } }; use pallet_transaction_payment::{ChargeTransactionPayment, Multiplier}; @@ -55,7 +56,7 @@ macro_rules! decl_tests { } fn last_event() -> Event { - system::Module::::events().pop().expect("Event expected").event + frame_system::Module::::events().pop().expect("Event expected").event } #[test] @@ -264,13 +265,13 @@ macro_rules! decl_tests { .monied(true) .build() .execute_with(|| { - System::inc_account_nonce(&2); + Accounts::inc_account_nonce(&2); assert_eq!(Balances::total_balance(&2), 256 * 20); assert_ok!(Balances::reserve(&2, 256 * 19 + 1)); // account 2 becomes mostly reserved assert_eq!(Balances::free_balance(2), 255); // "free" account deleted." assert_eq!(Balances::total_balance(&2), 256 * 20); // reserve still exists. - assert_eq!(System::account_nonce(&2), 1); + assert_eq!(Accounts::account_nonce(&2), 1); // account 4 tries to take index 1 for account 5. assert_ok!(Balances::transfer(Some(4).into(), 5, 256 * 1 + 0x69)); @@ -279,7 +280,7 @@ macro_rules! decl_tests { assert!(Balances::slash(&2, 256 * 19 + 2).1.is_zero()); // account 2 gets slashed // "reserve" account reduced to 255 (below ED) so account deleted assert_eq!(Balances::total_balance(&2), 0); - assert_eq!(System::account_nonce(&2), 0); // nonce zero + assert_eq!(Accounts::account_nonce(&2), 0); // nonce zero // account 4 tries to take index 1 again for account 6. assert_ok!(Balances::transfer(Some(4).into(), 6, 256 * 1 + 0x69)); @@ -304,14 +305,14 @@ macro_rules! decl_tests { .monied(true) .build() .execute_with(|| { - System::inc_account_nonce(&2); - assert_eq!(System::account_nonce(&2), 1); + Accounts::inc_account_nonce(&2); + assert_eq!(Accounts::account_nonce(&2), 1); assert_eq!(Balances::total_balance(&2), 2000); // index 1 (account 2) becomes zombie assert_ok!(Balances::transfer(Some(2).into(), 5, 1901)); assert_eq!(Balances::total_balance(&2), 0); assert_eq!(Balances::total_balance(&5), 1901); - assert_eq!(System::account_nonce(&2), 0); + assert_eq!(Accounts::account_nonce(&2), 0); }); } @@ -721,7 +722,7 @@ macro_rules! decl_tests { assert_eq!( events(), [ - Event::frame_system(system::Event::NewAccount(1)), + Event::pallet_accounts(pallet_accounts::Event::NewAccount(1)), Event::pallet_balances(crate::Event::Endowed(1, 100)), Event::pallet_balances(crate::Event::BalanceSet(1, 100, 0)), ] @@ -732,7 +733,7 @@ macro_rules! decl_tests { assert_eq!( events(), [ - Event::frame_system(system::Event::KilledAccount(1)), + Event::pallet_accounts(pallet_accounts::Event::KilledAccount(1)), Event::pallet_balances(crate::Event::DustLost(1, 99)), ] ); @@ -750,7 +751,7 @@ macro_rules! decl_tests { assert_eq!( events(), [ - Event::frame_system(system::Event::NewAccount(1)), + Event::pallet_accounts(pallet_accounts::Event::NewAccount(1)), Event::pallet_balances(crate::Event::Endowed(1, 100)), Event::pallet_balances(crate::Event::BalanceSet(1, 100, 0)), ] @@ -761,7 +762,7 @@ macro_rules! decl_tests { assert_eq!( events(), [ - Event::frame_system(system::Event::KilledAccount(1)) + Event::pallet_accounts(pallet_accounts::Event::KilledAccount(1)) ] ); }); @@ -780,7 +781,7 @@ macro_rules! decl_tests { // Slashed completed in full assert_eq!(Balances::slash(&1, 900), (NegativeImbalance::new(900), 0)); // Account is still alive - assert!(System::account_exists(&1)); + assert!(Accounts::account_exists(&1)); // SCENARIO: Slash will kill account because not enough balance left. assert_ok!(Balances::set_balance(Origin::root(), 1, 1_000, 0)); @@ -801,7 +802,7 @@ macro_rules! decl_tests { // Slashed full free_balance and 300 of reserved balance assert_eq!(Balances::slash(&1, 1_300), (NegativeImbalance::new(1300), 0)); // Account is still alive - assert!(System::account_exists(&1)); + assert!(Accounts::account_exists(&1)); // SCENARIO: Over-slash can take from reserved, and kill. assert_ok!(Balances::set_balance(Origin::root(), 1, 1_000, 350)); @@ -821,46 +822,46 @@ macro_rules! decl_tests { // SCENARIO: Slash would not kill account. assert_ok!(Balances::set_balance(Origin::root(), 1, 1_000, 0)); - assert_ok!(System::inc_consumers(&1)); // <-- Reference counter added here is enough for all tests + assert_ok!(Accounts::inc_consumers(&1)); // <-- Reference counter added here is enough for all tests // Slashed completed in full assert_eq!(Balances::slash(&1, 900), (NegativeImbalance::new(900), 0)); // Account is still alive - assert!(System::account_exists(&1)); + assert!(Accounts::account_exists(&1)); // SCENARIO: Slash will take as much as possible without killing account. assert_ok!(Balances::set_balance(Origin::root(), 1, 1_000, 0)); // Slashed completed in full assert_eq!(Balances::slash(&1, 950), (NegativeImbalance::new(900), 50)); // Account is still alive - assert!(System::account_exists(&1)); + assert!(Accounts::account_exists(&1)); // SCENARIO: Over-slash will not kill account, and report missing slash amount. assert_ok!(Balances::set_balance(Origin::root(), 1, 1_000, 0)); // Slashed full free_balance minus ED, and reports 400 not slashed assert_eq!(Balances::slash(&1, 1_300), (NegativeImbalance::new(900), 400)); // Account is still alive - assert!(System::account_exists(&1)); + assert!(Accounts::account_exists(&1)); // SCENARIO: Over-slash can take from reserved, but keep alive. assert_ok!(Balances::set_balance(Origin::root(), 1, 1_000, 400)); // Slashed full free_balance and 300 of reserved balance assert_eq!(Balances::slash(&1, 1_300), (NegativeImbalance::new(1300), 0)); // Account is still alive - assert!(System::account_exists(&1)); + assert!(Accounts::account_exists(&1)); // SCENARIO: Over-slash can take from reserved, but keep alive. assert_ok!(Balances::set_balance(Origin::root(), 1, 1_000, 350)); // Slashed full free_balance and 250 of reserved balance to leave ED assert_eq!(Balances::slash(&1, 1_300), (NegativeImbalance::new(1250), 50)); // Account is still alive - assert!(System::account_exists(&1)); + assert!(Accounts::account_exists(&1)); // SCENARIO: Over-slash can take as much as possible from reserved and report missing amount. assert_ok!(Balances::set_balance(Origin::root(), 1, 1_000, 250)); // Slashed full free_balance and 300 of reserved balance assert_eq!(Balances::slash(&1, 1_300), (NegativeImbalance::new(1150), 150)); // Account is still alive - assert!(System::account_exists(&1)); + assert!(Accounts::account_exists(&1)); // Slash on non-existent account is okay. assert_eq!(Balances::slash(&12345, 1_300), (NegativeImbalance::new(0), 1300)); @@ -880,7 +881,7 @@ macro_rules! decl_tests { // Slashed completed in full assert_eq!(Balances::slash_reserved(&1, 900), (NegativeImbalance::new(900), 0)); // Account is still alive - assert!(System::account_exists(&1)); + assert!(Accounts::account_exists(&1)); // SCENARIO: Slash would kill account. assert_ok!(Balances::set_balance(Origin::root(), 1, 50, 1_000)); @@ -901,38 +902,38 @@ macro_rules! decl_tests { // Slashed completed in full assert_eq!(Balances::slash_reserved(&1, 1_300), (NegativeImbalance::new(1_000), 300)); // Account is alive because of free balance - assert!(System::account_exists(&1)); + assert!(Accounts::account_exists(&1)); /* User has a reference counter, so they cannot die */ // SCENARIO: Slash would not kill account. assert_ok!(Balances::set_balance(Origin::root(), 1, 50, 1_000)); - assert_ok!(System::inc_consumers(&1)); // <-- Reference counter added here is enough for all tests + assert_ok!(Accounts::inc_consumers(&1)); // <-- Reference counter added here is enough for all tests // Slashed completed in full assert_eq!(Balances::slash_reserved(&1, 900), (NegativeImbalance::new(900), 0)); // Account is still alive - assert!(System::account_exists(&1)); + assert!(Accounts::account_exists(&1)); // SCENARIO: Slash as much as possible without killing. assert_ok!(Balances::set_balance(Origin::root(), 1, 50, 1_000)); // Slashed as much as possible assert_eq!(Balances::slash_reserved(&1, 1_000), (NegativeImbalance::new(950), 50)); // Account is still alive - assert!(System::account_exists(&1)); + assert!(Accounts::account_exists(&1)); // SCENARIO: Over-slash reports correctly, where reserved is needed to keep alive. assert_ok!(Balances::set_balance(Origin::root(), 1, 50, 1_000)); // Slashed as much as possible assert_eq!(Balances::slash_reserved(&1, 1_300), (NegativeImbalance::new(950), 350)); // Account is still alive - assert!(System::account_exists(&1)); + assert!(Accounts::account_exists(&1)); // SCENARIO: Over-slash reports correctly, where full reserved is removed. assert_ok!(Balances::set_balance(Origin::root(), 1, 200, 1_000)); // Slashed as much as possible assert_eq!(Balances::slash_reserved(&1, 1_300), (NegativeImbalance::new(1_000), 300)); // Account is still alive - assert!(System::account_exists(&1)); + assert!(Accounts::account_exists(&1)); // Slash on non-existent account is okay. assert_eq!(Balances::slash_reserved(&12345, 1_300), (NegativeImbalance::new(0), 1300)); diff --git a/frame/balances/src/tests_composite.rs b/frame/balances/src/tests_composite.rs index 14dfd0c4b33d6..fbec4ae126ef9 100644 --- a/frame/balances/src/tests_composite.rs +++ b/frame/balances/src/tests_composite.rs @@ -42,6 +42,7 @@ frame_support::construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, } ); @@ -70,11 +71,15 @@ impl frame_system::Config for Test { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = super::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); type SystemWeightInfo = (); type SS58Prefix = (); + type AccountStorage = Accounts; +} +impl pallet_accounts::Config for Test { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = super::AccountData; } parameter_types! { pub const TransactionByteFee: u64 = 1; @@ -91,7 +96,8 @@ impl Config for Test { type DustRemoval = (); type Event = Event; type ExistentialDeposit = ExistentialDeposit; - type AccountStore = frame_system::Pallet; + type AccountStore = Accounts; + type ReferencedAccount = Accounts; type MaxLocks = (); type WeightInfo = (); } diff --git a/frame/balances/src/tests_local.rs b/frame/balances/src/tests_local.rs index e0f0e257a05da..2367482b65917 100644 --- a/frame/balances/src/tests_local.rs +++ b/frame/balances/src/tests_local.rs @@ -44,6 +44,7 @@ frame_support::construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, } ); @@ -76,6 +77,12 @@ impl frame_system::Config for Test { type SystemWeightInfo = (); type SS58Prefix = (); } +impl pallet_accounts::Config for Test { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = (); +} parameter_types! { pub const TransactionByteFee: u64 = 1; } @@ -95,11 +102,12 @@ impl Config for Test { type ExistentialDeposit = ExistentialDeposit; type AccountStore = StorageMapShim< super::Account, - system::Provider, + pallet_accounts::Provider, u64, super::AccountData, >; type MaxLocks = MaxLocks; + type ReferencedAccount = Accounts; type WeightInfo = (); } @@ -166,7 +174,7 @@ fn emit_events_with_no_existential_deposit_suicide_with_dust() { assert_eq!( events(), [ - Event::frame_system(system::Event::NewAccount(1)), + Event::pallet_accounts(pallet_accounts::Event::NewAccount(1)), Event::pallet_balances(crate::Event::Endowed(1, 100)), Event::pallet_balances(crate::Event::BalanceSet(1, 100, 0)), ] @@ -182,7 +190,7 @@ fn emit_events_with_no_existential_deposit_suicide_with_dust() { assert_eq!( events(), [ - Event::frame_system(system::Event::KilledAccount(1)), + Event::pallet_accounts(pallet_accounts::Event::KilledAccount(1)), Event::pallet_balances(crate::Event::DustLost(1, 1)), ] ); diff --git a/frame/balances/src/tests_reentrancy.rs b/frame/balances/src/tests_reentrancy.rs index eb6e0603cac25..5044447d8edc0 100644 --- a/frame/balances/src/tests_reentrancy.rs +++ b/frame/balances/src/tests_reentrancy.rs @@ -47,7 +47,7 @@ type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; type Block = frame_system::mocking::MockBlock; fn last_event() -> Event { - system::Module::::events().pop().expect("Event expected").event + frame_system::Module::::events().pop().expect("Event expected").event } frame_support::construct_runtime!( @@ -57,6 +57,7 @@ frame_support::construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, } ); @@ -89,6 +90,12 @@ impl frame_system::Config for Test { type SystemWeightInfo = (); type SS58Prefix = (); } +impl pallet_accounts::Config for Test { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = pallet_balances::AccountData; +} parameter_types! { pub const TransactionByteFee: u64 = 1; } @@ -115,12 +122,13 @@ impl Config for Test { type ExistentialDeposit = ExistentialDeposit; type AccountStore = StorageMapShim< super::Account, - system::Provider, + pallet_accounts::Provider, u64, super::AccountData, >; type MaxLocks = MaxLocks; type WeightInfo = (); + type ReferencedAccount = Accounts; } pub struct ExtBuilder { From 27d97cc28d39a2a22c2356d3b52e9441ebe9a9eb Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Thu, 4 Mar 2021 20:07:42 -0400 Subject: [PATCH 17/31] fix more tests --- Cargo.lock | 4 ++++ frame/bounties/Cargo.toml | 1 + frame/bounties/src/tests.rs | 14 ++++++++++---- frame/contracts/Cargo.toml | 1 + frame/contracts/src/tests.rs | 14 ++++++++++---- frame/democracy/Cargo.toml | 1 + frame/democracy/src/tests.rs | 14 ++++++++++---- frame/election-provider-multi-phase/Cargo.toml | 1 + frame/election-provider-multi-phase/src/mock.rs | 15 ++++++++++----- 9 files changed, 48 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index eb6fa16bb5a7e..e351c021141fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4595,6 +4595,7 @@ dependencies = [ "frame-benchmarking", "frame-support", "frame-system", + "pallet-accounts", "pallet-balances", "pallet-treasury", "parity-scale-codec", @@ -4634,6 +4635,7 @@ dependencies = [ "frame-system", "hex-literal", "log", + "pallet-accounts", "pallet-balances", "pallet-contracts-primitives", "pallet-contracts-proc-macro", @@ -4713,6 +4715,7 @@ dependencies = [ "frame-support", "frame-system", "hex-literal", + "pallet-accounts", "pallet-balances", "pallet-scheduler", "parity-scale-codec", @@ -4734,6 +4737,7 @@ dependencies = [ "frame-system", "hex-literal", "log", + "pallet-accounts", "pallet-balances", "parity-scale-codec", "parking_lot 0.11.1", diff --git a/frame/bounties/Cargo.toml b/frame/bounties/Cargo.toml index ff1a3a6807098..f96e30db2658c 100644 --- a/frame/bounties/Cargo.toml +++ b/frame/bounties/Cargo.toml @@ -28,6 +28,7 @@ sp-io ={ version = "3.0.0", path = "../../primitives/io" } sp-core = { version = "3.0.0", path = "../../primitives/core" } sp-storage = { version = "3.0.0", path = "../../primitives/storage" } pallet-balances = { version = "3.0.0", path = "../balances" } +pallet-accounts = { version = "3.0.0", path = "../accounts" } [features] default = ["std"] diff --git a/frame/bounties/src/tests.rs b/frame/bounties/src/tests.rs index cbff502daa65e..a3cfb8cd0e55c 100644 --- a/frame/bounties/src/tests.rs +++ b/frame/bounties/src/tests.rs @@ -44,6 +44,7 @@ frame_support::construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, Bounties: pallet_bounties::{Module, Call, Storage, Event}, Treasury: pallet_treasury::{Module, Call, Storage, Config, Event}, @@ -75,11 +76,15 @@ impl frame_system::Config for Test { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); type SystemWeightInfo = (); type SS58Prefix = (); + type AccountStorage = Accounts; +} +impl pallet_accounts::Config for Test { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = pallet_balances::AccountData; } parameter_types! { pub const ExistentialDeposit: u64 = 1; @@ -90,7 +95,8 @@ impl pallet_balances::Config for Test { type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; + type AccountStore = Accounts; + type ReferencedAccount = Accounts; type WeightInfo = (); } thread_local! { diff --git a/frame/contracts/Cargo.toml b/frame/contracts/Cargo.toml index 018a8a5df672e..f21ec2d4c512c 100644 --- a/frame/contracts/Cargo.toml +++ b/frame/contracts/Cargo.toml @@ -37,6 +37,7 @@ rand_pcg = { version = "0.2.1", optional = true } [dev-dependencies] assert_matches = "1.3.0" hex-literal = "0.3.1" +pallet-accounts = { version = "3.0.0", path = "../accounts" } pallet-balances = { version = "3.0.0", path = "../balances" } pallet-timestamp = { version = "3.0.0", path = "../timestamp" } pallet-randomness-collective-flip = { version = "3.0.0", path = "../randomness-collective-flip" } diff --git a/frame/contracts/src/tests.rs b/frame/contracts/src/tests.rs index c17434300d454..c46346dc0da1e 100644 --- a/frame/contracts/src/tests.rs +++ b/frame/contracts/src/tests.rs @@ -58,6 +58,7 @@ frame_support::construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, Timestamp: pallet_timestamp::{Module, Call, Storage, Inherent}, Randomness: pallet_randomness_collective_flip::{Module, Call, Storage}, @@ -216,11 +217,15 @@ impl frame_system::Config for Test { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); type SystemWeightInfo = (); type SS58Prefix = (); + type AccountStorage = Accounts; +} +impl pallet_accounts::Config for Test { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = pallet_balances::AccountData; } impl pallet_balances::Config for Test { type MaxLocks = (); @@ -228,7 +233,8 @@ impl pallet_balances::Config for Test { type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; + type AccountStore = Accounts; + type ReferencedAccount = Accounts; type WeightInfo = (); } parameter_types! { diff --git a/frame/democracy/Cargo.toml b/frame/democracy/Cargo.toml index f9b0d035b089d..ceb6b695301cc 100644 --- a/frame/democracy/Cargo.toml +++ b/frame/democracy/Cargo.toml @@ -24,6 +24,7 @@ frame-system = { version = "3.0.0", default-features = false, path = "../system" [dev-dependencies] sp-core = { version = "3.0.0", path = "../../primitives/core" } +pallet-accounts = { version = "3.0.0", path = "../accounts" } pallet-balances = { version = "3.0.0", path = "../balances" } pallet-scheduler = { version = "3.0.0", path = "../scheduler" } sp-storage = { version = "3.0.0", path = "../../primitives/storage" } diff --git a/frame/democracy/src/tests.rs b/frame/democracy/src/tests.rs index 99f413b389284..88467747a218b 100644 --- a/frame/democracy/src/tests.rs +++ b/frame/democracy/src/tests.rs @@ -61,6 +61,7 @@ frame_support::construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, Scheduler: pallet_scheduler::{Module, Call, Storage, Config, Event}, Democracy: pallet_democracy::{Module, Call, Storage, Config, Event}, @@ -98,11 +99,15 @@ impl frame_system::Config for Test { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); type SystemWeightInfo = (); type SS58Prefix = (); + type AccountStorage = Accounts; +} +impl pallet_accounts::Config for Test { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = pallet_balances::AccountData; } parameter_types! { pub MaximumSchedulerWeight: Weight = Perbill::from_percent(80) * BlockWeights::get().max_block; @@ -126,7 +131,8 @@ impl pallet_balances::Config for Test { type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; + type AccountStore = Accounts; + type ReferencedAccount = Accounts; type WeightInfo = (); } parameter_types! { diff --git a/frame/election-provider-multi-phase/Cargo.toml b/frame/election-provider-multi-phase/Cargo.toml index 1d63f9df40a25..4e9b3a0efc903 100644 --- a/frame/election-provider-multi-phase/Cargo.toml +++ b/frame/election-provider-multi-phase/Cargo.toml @@ -44,6 +44,7 @@ sp-tracing = { version = "3.0.0", path = "../../primitives/tracing" } sp-election-providers = { version = "3.0.0", features = ["runtime-benchmarks"], path = "../../primitives/election-providers" } pallet-balances = { version = "3.0.0", path = "../balances" } frame-benchmarking = { path = "../benchmarking" , version = "3.1.0"} +pallet-accounts = { path = "../accounts" , version = "3.0.0"} [features] default = ["std"] diff --git a/frame/election-provider-multi-phase/src/mock.rs b/frame/election-provider-multi-phase/src/mock.rs index eb38a4cd52e95..27cc9c375dfbd 100644 --- a/frame/election-provider-multi-phase/src/mock.rs +++ b/frame/election-provider-multi-phase/src/mock.rs @@ -55,6 +55,7 @@ frame_support::construct_runtime!( System: frame_system::{Module, Call, Event, Config}, Balances: pallet_balances::{Module, Call, Event, Config}, MultiPhase: multi_phase::{Module, Call, Event}, + Accounts: pallet_accounts::{Module, Call, Event,}, } ); @@ -155,12 +156,15 @@ impl frame_system::Config for Runtime { type BlockWeights = BlockWeights; type Version = (); type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); type SystemWeightInfo = (); + type AccountStorage = Accounts; +} +impl pallet_accounts::Config for Runtime { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = pallet_balances::AccountData; } - const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75); parameter_types! { pub const ExistentialDeposit: u64 = 1; @@ -173,7 +177,8 @@ impl pallet_balances::Config for Runtime { type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; + type AccountStore = Accounts; + type ReferencedAccount = Accounts; type MaxLocks = (); type WeightInfo = (); } From a3d14fefd179c15ab937f151d24f0b128d8e98a5 Mon Sep 17 00:00:00 2001 From: Gavin Wood Date: Fri, 5 Mar 2021 16:35:34 +0100 Subject: [PATCH 18/31] Update frame/accounts/src/lib.rs --- frame/accounts/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/accounts/src/lib.rs b/frame/accounts/src/lib.rs index da1546733448f..31ee4cbd1c9c6 100644 --- a/frame/accounts/src/lib.rs +++ b/frame/accounts/src/lib.rs @@ -260,7 +260,7 @@ pub mod pallet { }) } - /// Decrement the sufficients reference counter on an account. + /// Decrement the self-sufficient reference counter on an account. /// /// This *MUST* only be done once for every time you called `inc_sufficients` on `who`. fn dec_sufficients(who: &T::AccountId) -> DecRefStatus { From 284fc25fa324d783b75fe8e910c98c14dcea286a Mon Sep 17 00:00:00 2001 From: Gavin Wood Date: Fri, 5 Mar 2021 16:36:38 +0100 Subject: [PATCH 19/31] Update frame/accounts/src/lib.rs --- frame/accounts/src/lib.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/frame/accounts/src/lib.rs b/frame/accounts/src/lib.rs index 31ee4cbd1c9c6..73a61688b71d8 100644 --- a/frame/accounts/src/lib.rs +++ b/frame/accounts/src/lib.rs @@ -5,13 +5,8 @@ use sp_std::{ marker::PhantomData, }; use frame_support::{ - sp_runtime::{ - RuntimeDebug, - traits::{StoredMapError, One}, - }, - traits::{ - HandleLifetime, StoredMap, OnNewAccount, OnKilledAccount, ReferencedAccount, BasicAccount, - } + sp_runtime::{RuntimeDebug, traits::{StoredMapError, One}}, + traits::{HandleLifetime, StoredMap, OnNewAccount, OnKilledAccount, ReferencedAccount, BasicAccount}, }; use codec::{Encode, Decode, FullCodec}; From 3ff3748bfce0b4e84c728f1877b6bb95d778b74c Mon Sep 17 00:00:00 2001 From: Gavin Wood Date: Fri, 5 Mar 2021 16:36:58 +0100 Subject: [PATCH 20/31] Update frame/accounts/src/lib.rs --- frame/accounts/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/frame/accounts/src/lib.rs b/frame/accounts/src/lib.rs index 73a61688b71d8..2c19de07903ab 100644 --- a/frame/accounts/src/lib.rs +++ b/frame/accounts/src/lib.rs @@ -186,6 +186,7 @@ pub mod pallet { type DecRefStatus = DecRefStatus; type IncRefError = IncRefError; type DecRefError = DecRefError; + /// Increment the provider reference counter on an account. fn inc_providers(who: &T::AccountId) -> IncRefStatus { Account::::mutate(who, |a| if a.providers == 0 && a.sufficients == 0 { From f7ed2ba9237aaee4301725e7864a337fde3bd01d Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Mon, 8 Mar 2021 19:12:37 -0400 Subject: [PATCH 21/31] fix more tests --- Cargo.lock | 3 +++ frame/elections-phragmen/Cargo.toml | 1 + frame/elections-phragmen/src/lib.rs | 17 ++++++++++++----- frame/elections/Cargo.toml | 1 + frame/elections/src/mock.rs | 15 +++++++++++---- frame/example/Cargo.toml | 3 ++- frame/example/src/lib.rs | 14 ++++++++++++-- 7 files changed, 42 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4d84df1212e9a..19f7904bd47c5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4824,6 +4824,7 @@ dependencies = [ "frame-support", "frame-system", "hex-literal", + "pallet-accounts", "pallet-balances", "parity-scale-codec", "serde", @@ -4842,6 +4843,7 @@ dependencies = [ "frame-system", "hex-literal", "log", + "pallet-accounts", "pallet-balances", "parity-scale-codec", "serde", @@ -4860,6 +4862,7 @@ dependencies = [ "frame-benchmarking", "frame-support", "frame-system", + "pallet-accounts", "pallet-balances", "parity-scale-codec", "serde", diff --git a/frame/elections-phragmen/Cargo.toml b/frame/elections-phragmen/Cargo.toml index 89723cb85fbe1..1a28850b16a7d 100644 --- a/frame/elections-phragmen/Cargo.toml +++ b/frame/elections-phragmen/Cargo.toml @@ -26,6 +26,7 @@ log = { version = "0.4.14", default-features = false } [dev-dependencies] sp-io = { version = "3.0.0", path = "../../primitives/io" } hex-literal = "0.3.1" +pallet-accounts = { version = "3.0.0", path = "../accounts" } pallet-balances = { version = "3.0.0", path = "../balances" } sp-core = { version = "3.0.0", path = "../../primitives/core" } substrate-test-utils = { version = "3.0.0", path = "../../test-utils" } diff --git a/frame/elections-phragmen/src/lib.rs b/frame/elections-phragmen/src/lib.rs index 779570ca633ee..69a46dd3bb76a 100644 --- a/frame/elections-phragmen/src/lib.rs +++ b/frame/elections-phragmen/src/lib.rs @@ -1080,11 +1080,16 @@ mod tests { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); type SystemWeightInfo = (); - type SS58Prefix = (); + type SS58Prefix = (); + type AccountStorage = Accounts; + } + + impl pallet_accounts::Config for Test { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = pallet_balances::AccountData; } parameter_types! { @@ -1096,7 +1101,8 @@ mod tests { type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type AccountStore = frame_system::Module; + type AccountStore = Accounts; + type ReferencedAccount = Accounts; type MaxLocks = (); type WeightInfo = (); } @@ -1188,6 +1194,7 @@ mod tests { UncheckedExtrinsic = UncheckedExtrinsic { System: frame_system::{Module, Call, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Balances: pallet_balances::{Module, Call, Event, Config}, Elections: elections_phragmen::{Module, Call, Event, Config}, } diff --git a/frame/elections/Cargo.toml b/frame/elections/Cargo.toml index ac3c709300f51..596ef730811be 100644 --- a/frame/elections/Cargo.toml +++ b/frame/elections/Cargo.toml @@ -24,6 +24,7 @@ frame-system = { version = "3.0.0", default-features = false, path = "../system" [dev-dependencies] hex-literal = "0.3.1" +pallet-accounts = { version = "3.0.0", path = "../accounts" } pallet-balances = { version = "3.0.0", path = "../balances" } [features] diff --git a/frame/elections/src/mock.rs b/frame/elections/src/mock.rs index 31d3f5a1c28a5..2a30bbee9f1bd 100644 --- a/frame/elections/src/mock.rs +++ b/frame/elections/src/mock.rs @@ -53,11 +53,16 @@ impl frame_system::Config for Test { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); type SystemWeightInfo = (); type SS58Prefix = (); + type AccountStorage = Accounts; +} + +impl pallet_accounts::Config for Test { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = pallet_balances::AccountData; } parameter_types! { @@ -69,7 +74,8 @@ impl pallet_balances::Config for Test { type DustRemoval = (); type Event = Event; type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; + type AccountStore = Accounts; + type ReferencedAccount = Accounts; type WeightInfo = (); } @@ -136,6 +142,7 @@ frame_support::construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic { System: system::{Module, Call, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Balances: pallet_balances::{Module, Call, Event, Config}, Elections: elections::{Module, Call, Event, Config}, } diff --git a/frame/example/Cargo.toml b/frame/example/Cargo.toml index de741294b9c16..d2af9f42b86bf 100644 --- a/frame/example/Cargo.toml +++ b/frame/example/Cargo.toml @@ -25,7 +25,8 @@ sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/ frame-benchmarking = { version = "3.1.0", default-features = false, path = "../benchmarking", optional = true } [dev-dependencies] -sp-core = { version = "3.0.0", path = "../../primitives/core", default-features = false } +sp-core = { version = "3.0.0", path = "../../primitives/core" } +pallet-accounts = { version = "3.0.0", path = "../accounts" } [features] default = ["std"] diff --git a/frame/example/src/lib.rs b/frame/example/src/lib.rs index 12f2567e43de9..1b12d028023ce 100644 --- a/frame/example/src/lib.rs +++ b/frame/example/src/lib.rs @@ -759,6 +759,7 @@ mod tests { UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, Example: pallet_example::{Module, Call, Storage, Config, Event}, } @@ -787,10 +788,18 @@ mod tests { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountStorage = System; + type AccountStorage = Accounts; type SystemWeightInfo = (); type SS58Prefix = (); } + + impl pallet_accounts::Config for Test { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = pallet_balances::AccountData; + } + parameter_types! { pub const ExistentialDeposit: u64 = 1; } @@ -800,7 +809,8 @@ mod tests { type DustRemoval = (); type Event = Event; type ExistentialDeposit = ExistentialDeposit; - type AccountStore = Balances; + type AccountStore = Accounts; + type ReferencedAccount = Accounts; type WeightInfo = (); } impl Config for Test { From 586bf73d5f7258e6ecdff3563051794d536087a9 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Mon, 8 Mar 2021 19:43:44 -0400 Subject: [PATCH 22/31] fix more tests --- Cargo.lock | 5 +++++ frame/executive/Cargo.toml | 3 ++- frame/executive/src/lib.rs | 15 ++++++++++++--- frame/gilt/Cargo.toml | 1 + frame/gilt/src/mock.rs | 15 +++++++++++---- frame/grandpa/Cargo.toml | 1 + frame/grandpa/src/mock.rs | 17 +++++++++++++---- frame/identity/Cargo.toml | 1 + frame/identity/src/tests.rs | 14 ++++++++++---- frame/im-online/Cargo.toml | 1 + frame/im-online/src/mock.rs | 11 ++++++++++- 11 files changed, 67 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 19f7904bd47c5..22b669a84a8fd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1730,6 +1730,7 @@ dependencies = [ "frame-support", "frame-system", "hex-literal", + "pallet-accounts", "pallet-balances", "pallet-indices", "pallet-transaction-payment", @@ -4911,6 +4912,7 @@ dependencies = [ "frame-benchmarking", "frame-support", "frame-system", + "pallet-accounts", "pallet-balances", "parity-scale-codec", "serde", @@ -4930,6 +4932,7 @@ dependencies = [ "frame-support", "frame-system", "log", + "pallet-accounts", "pallet-authorship", "pallet-balances", "pallet-offences", @@ -4959,6 +4962,7 @@ dependencies = [ "frame-benchmarking", "frame-support", "frame-system", + "pallet-accounts", "pallet-balances", "parity-scale-codec", "serde", @@ -4976,6 +4980,7 @@ dependencies = [ "frame-support", "frame-system", "log", + "pallet-accounts", "pallet-authorship", "pallet-session", "parity-scale-codec", diff --git a/frame/executive/Cargo.toml b/frame/executive/Cargo.toml index 6a0042308736e..737c8ebcc907f 100644 --- a/frame/executive/Cargo.toml +++ b/frame/executive/Cargo.toml @@ -27,8 +27,9 @@ sp-core = { version = "3.0.0", default-features = false, path = "../../primitive hex-literal = "0.3.1" sp-core = { version = "3.0.0", path = "../../primitives/core" } sp-io ={ version = "3.0.0", path = "../../primitives/io" } -pallet-indices = { version = "3.0.0", path = "../indices" } +pallet-accounts = { version = "3.0.0", path = "../accounts" } pallet-balances = { version = "3.0.0", path = "../balances" } +pallet-indices = { version = "3.0.0", path = "../indices" } pallet-transaction-payment = { version = "3.0.0", path = "../transaction-payment" } sp-version = { version = "3.0.0", path = "../../primitives/version" } diff --git a/frame/executive/src/lib.rs b/frame/executive/src/lib.rs index 2664539b4e080..68b2d324556f6 100644 --- a/frame/executive/src/lib.rs +++ b/frame/executive/src/lib.rs @@ -598,6 +598,7 @@ mod tests { UncheckedExtrinsic = TestUncheckedExtrinsic { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, Custom: custom::{Module, Call, ValidateUnsigned}, } @@ -634,11 +635,18 @@ mod tests { type BlockHashCount = BlockHashCount; type Version = RuntimeVersion; type PalletInfo = PalletInfo; - type AccountStorage = System; + type AccountStorage = Accounts; type SystemWeightInfo = (); type SS58Prefix = (); } + impl pallet_accounts::Config for Runtime { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = pallet_balances::AccountData; + } + type Balance = u64; parameter_types! { pub const ExistentialDeposit: Balance = 1; @@ -648,7 +656,8 @@ mod tests { type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type AccountStore = Balances; + type AccountStore = Accounts; + type ReferencedAccount = Accounts; type MaxLocks = (); type WeightInfo = (); } @@ -767,7 +776,7 @@ mod tests { header: Header { parent_hash: [69u8; 32].into(), number: 1, - state_root: hex!("2c01e6f33d595793119823478b45b36978a8f65a731b5ae3fdfb6330b4cd4b11").into(), + state_root: hex!("b7d31cf177f0cc54bbc5c437d63123829643b2ac63895df9edc151b6ef446874").into(), extrinsics_root: hex!("03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314").into(), digest: Digest { logs: vec![], }, }, diff --git a/frame/gilt/Cargo.toml b/frame/gilt/Cargo.toml index 4df0dc49aaf93..42f5b1c12fbbb 100644 --- a/frame/gilt/Cargo.toml +++ b/frame/gilt/Cargo.toml @@ -25,6 +25,7 @@ frame-system = { version = "3.0.0", default-features = false, path = "../system" [dev-dependencies] sp-io = { version = "3.0.0", path = "../../primitives/io" } sp-core = { version = "3.0.0", path = "../../primitives/core" } +pallet-accounts = { version = "3.0.0", path = "../accounts" } pallet-balances = { version = "3.0.0", path = "../balances" } [features] diff --git a/frame/gilt/src/mock.rs b/frame/gilt/src/mock.rs index 701c5c2f6d73b..793aa269f5f26 100644 --- a/frame/gilt/src/mock.rs +++ b/frame/gilt/src/mock.rs @@ -36,6 +36,7 @@ frame_support::construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Balances: pallet_balances::{Module, Call, Config, Storage, Event}, Gilt: pallet_gilt::{Module, Call, Config, Storage, Event}, } @@ -64,11 +65,16 @@ impl frame_system::Config for Test { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); type SystemWeightInfo = (); type SS58Prefix = SS58Prefix; + type AccountStorage = Accounts; +} + +impl pallet_accounts::Config for Test { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = pallet_balances::AccountData; } parameter_types! { @@ -81,7 +87,8 @@ impl pallet_balances::Config for Test { type DustRemoval = (); type Event = Event; type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; + type AccountStore = Accounts; + type ReferencedAccount = Accounts; type WeightInfo = (); } diff --git a/frame/grandpa/Cargo.toml b/frame/grandpa/Cargo.toml index 2bf7306f58e15..d3c593d49f062 100644 --- a/frame/grandpa/Cargo.toml +++ b/frame/grandpa/Cargo.toml @@ -34,6 +34,7 @@ frame-benchmarking = { version = "3.1.0", path = "../benchmarking" } grandpa = { package = "finality-grandpa", version = "0.14.0", features = ["derive-codec"] } sp-io = { version = "3.0.0", path = "../../primitives/io" } sp-keyring = { version = "3.0.0", path = "../../primitives/keyring" } +pallet-accounts = { version = "3.0.0", path = "../accounts" } pallet-balances = { version = "3.0.0", path = "../balances" } pallet-offences = { version = "3.0.0", path = "../offences" } pallet-staking = { version = "3.0.0", path = "../staking" } diff --git a/frame/grandpa/src/mock.rs b/frame/grandpa/src/mock.rs index 0a24a2344547e..1f74e9efb7a95 100644 --- a/frame/grandpa/src/mock.rs +++ b/frame/grandpa/src/mock.rs @@ -52,6 +52,7 @@ frame_support::construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Timestamp: pallet_timestamp::{Module, Call, Storage, Inherent}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, Staking: pallet_staking::{Module, Call, Config, Storage, Event, ValidateUnsigned}, @@ -92,11 +93,16 @@ impl frame_system::Config for Test { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); type SystemWeightInfo = (); type SS58Prefix = (); + type AccountStorage = Accounts; +} + +impl pallet_accounts::Config for Test { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = pallet_balances::AccountData; } impl frame_system::offchain::SendTransactionTypes for Test @@ -125,6 +131,7 @@ impl pallet_session::Config for Test { type Keys = TestSessionKeys; type DisabledValidatorsThreshold = DisabledValidatorsThreshold; type WeightInfo = (); + type ReferencedAccount = Accounts; } impl pallet_session::historical::Config for Test { @@ -153,7 +160,8 @@ impl pallet_balances::Config for Test { type DustRemoval = (); type Event = Event; type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; + type AccountStore = Accounts; + type ReferencedAccount = Accounts; type WeightInfo = (); } @@ -221,6 +229,7 @@ impl pallet_staking::Config for Test { type OffchainSolutionWeightLimit = (); type ElectionProvider = onchain::OnChainSequentialPhragmen; type WeightInfo = (); + type ReferencedAccount = Accounts; } parameter_types! { diff --git a/frame/identity/Cargo.toml b/frame/identity/Cargo.toml index 08109fda2584c..a3a215db5830f 100644 --- a/frame/identity/Cargo.toml +++ b/frame/identity/Cargo.toml @@ -25,6 +25,7 @@ frame-system = { version = "3.0.0", default-features = false, path = "../system" [dev-dependencies] sp-core = { version = "3.0.0", path = "../../primitives/core" } +pallet-accounts = { version = "3.0.0", path = "../accounts" } pallet-balances = { version = "3.0.0", path = "../balances" } [features] diff --git a/frame/identity/src/tests.rs b/frame/identity/src/tests.rs index 230079a21ea0d..5f0afc18781e7 100644 --- a/frame/identity/src/tests.rs +++ b/frame/identity/src/tests.rs @@ -38,6 +38,7 @@ frame_support::construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, Identity: pallet_identity::{Module, Call, Storage, Event}, } @@ -66,11 +67,15 @@ impl frame_system::Config for Test { type DbWeight = (); type Version = (); type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); type SystemWeightInfo = (); type SS58Prefix = (); + type AccountStorage = Accounts; +} +impl pallet_accounts::Config for Test { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = pallet_balances::AccountData; } parameter_types! { pub const ExistentialDeposit: u64 = 1; @@ -80,7 +85,8 @@ impl pallet_balances::Config for Test { type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; + type AccountStore = Accounts; + type ReferencedAccount = Accounts; type MaxLocks = (); type WeightInfo = (); } diff --git a/frame/im-online/Cargo.toml b/frame/im-online/Cargo.toml index 4c5b4a8863bcd..5a5bf27aba966 100644 --- a/frame/im-online/Cargo.toml +++ b/frame/im-online/Cargo.toml @@ -29,6 +29,7 @@ log = { version = "0.4.14", default-features = false } frame-benchmarking = { version = "3.1.0", default-features = false, path = "../benchmarking", optional = true } [dev-dependencies] +pallet-accounts = { version = "3.0.0", path = "../accounts" } pallet-session = { version = "3.0.0", path = "../session" } [features] diff --git a/frame/im-online/src/mock.rs b/frame/im-online/src/mock.rs index dcafdb37dcbb6..07b06352094dc 100644 --- a/frame/im-online/src/mock.rs +++ b/frame/im-online/src/mock.rs @@ -41,6 +41,7 @@ frame_support::construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Session: pallet_session::{Module, Call, Storage, Event, Config}, ImOnline: imonline::{Module, Call, Storage, Config, Event}, Historical: pallet_session_historical::{Module}, @@ -131,11 +132,18 @@ impl frame_system::Config for Runtime { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountStorage = System; + type AccountStorage = Accounts; type SystemWeightInfo = (); type SS58Prefix = (); } +impl pallet_accounts::Config for Runtime { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = (); +} + parameter_types! { pub const Period: u64 = 1; pub const Offset: u64 = 0; @@ -156,6 +164,7 @@ impl pallet_session::Config for Runtime { type DisabledValidatorsThreshold = DisabledValidatorsThreshold; type NextSessionRotation = pallet_session::PeriodicSessions; type WeightInfo = (); + type ReferencedAccount = Accounts; } impl pallet_session::historical::Config for Runtime { From 02fa345529a72ea49acb016bebece8de601fd3d3 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Mon, 8 Mar 2021 22:31:10 -0400 Subject: [PATCH 23/31] fix more tests --- Cargo.lock | 4 ++++ frame/indices/Cargo.toml | 1 + frame/indices/src/mock.rs | 15 +++++++++++---- frame/multisig/Cargo.toml | 1 + frame/multisig/src/tests.rs | 14 ++++++++++---- frame/nicks/Cargo.toml | 1 + frame/nicks/src/lib.rs | 12 ++++++++++-- frame/offences/benchmarking/Cargo.toml | 1 + frame/offences/benchmarking/src/lib.rs | 19 +++++++++++++------ frame/offences/benchmarking/src/mock.rs | 16 ++++++++++++---- 10 files changed, 64 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 22b669a84a8fd..426743a5f9658 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5000,6 +5000,7 @@ dependencies = [ "frame-benchmarking", "frame-support", "frame-system", + "pallet-accounts", "pallet-balances", "parity-scale-codec", "serde", @@ -5083,6 +5084,7 @@ dependencies = [ "frame-benchmarking", "frame-support", "frame-system", + "pallet-accounts", "pallet-balances", "parity-scale-codec", "serde", @@ -5098,6 +5100,7 @@ version = "3.0.0" dependencies = [ "frame-support", "frame-system", + "pallet-accounts", "pallet-balances", "parity-scale-codec", "serde", @@ -5146,6 +5149,7 @@ dependencies = [ "frame-benchmarking", "frame-support", "frame-system", + "pallet-accounts", "pallet-babe", "pallet-balances", "pallet-grandpa", diff --git a/frame/indices/Cargo.toml b/frame/indices/Cargo.toml index ce9b2053ff184..b82f4c3b5f169 100644 --- a/frame/indices/Cargo.toml +++ b/frame/indices/Cargo.toml @@ -26,6 +26,7 @@ frame-system = { version = "3.0.0", default-features = false, path = "../system" frame-benchmarking = { version = "3.1.0", default-features = false, path = "../benchmarking", optional = true } [dev-dependencies] +pallet-accounts = { version = "3.0.0", path = "../accounts" } pallet-balances = { version = "3.0.0", path = "../balances" } [features] diff --git a/frame/indices/src/mock.rs b/frame/indices/src/mock.rs index 06c73b1a9bc27..4cd742f3d1041 100644 --- a/frame/indices/src/mock.rs +++ b/frame/indices/src/mock.rs @@ -34,6 +34,7 @@ frame_support::construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, Indices: pallet_indices::{Module, Call, Storage, Config, Event}, } @@ -63,11 +64,16 @@ impl frame_system::Config for Test { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); type SystemWeightInfo = (); type SS58Prefix = (); + type AccountStorage = Accounts; +} + +impl pallet_accounts::Config for Test { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = pallet_balances::AccountData; } parameter_types! { @@ -80,7 +86,8 @@ impl pallet_balances::Config for Test { type DustRemoval = (); type Event = Event; type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; + type AccountStore = Accounts; + type ReferencedAccount = Accounts; type WeightInfo = (); } diff --git a/frame/multisig/Cargo.toml b/frame/multisig/Cargo.toml index e48f80567f67a..2d5a4f643719d 100644 --- a/frame/multisig/Cargo.toml +++ b/frame/multisig/Cargo.toml @@ -26,6 +26,7 @@ frame-benchmarking = { version = "3.1.0", default-features = false, path = "../b [dev-dependencies] sp-core = { version = "3.0.0", path = "../../primitives/core" } +pallet-accounts = { version = "3.0.0", path = "../accounts" } pallet-balances = { version = "3.0.0", path = "../balances" } [features] diff --git a/frame/multisig/src/tests.rs b/frame/multisig/src/tests.rs index a3f47a26e6422..ea7eb4180aab3 100644 --- a/frame/multisig/src/tests.rs +++ b/frame/multisig/src/tests.rs @@ -38,6 +38,7 @@ frame_support::construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, Multisig: pallet_multisig::{Module, Call, Storage, Event}, } @@ -66,11 +67,15 @@ impl frame_system::Config for Test { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); type SystemWeightInfo = (); type SS58Prefix = (); + type AccountStorage = Accounts; +} +impl pallet_accounts::Config for Test { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = pallet_balances::AccountData; } parameter_types! { pub const ExistentialDeposit: u64 = 1; @@ -81,7 +86,8 @@ impl pallet_balances::Config for Test { type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; + type AccountStore = Accounts; + type ReferencedAccount = Accounts; type WeightInfo = (); } parameter_types! { diff --git a/frame/nicks/Cargo.toml b/frame/nicks/Cargo.toml index 6c8b609b401ca..88c99119f0781 100644 --- a/frame/nicks/Cargo.toml +++ b/frame/nicks/Cargo.toml @@ -23,6 +23,7 @@ frame-system = { version = "3.0.0", default-features = false, path = "../system" [dev-dependencies] sp-core = { version = "3.0.0", path = "../../primitives/core" } +pallet-accounts = { version = "3.0.0", path = "../accounts" } pallet-balances = { version = "3.0.0", path = "../balances" } [features] diff --git a/frame/nicks/src/lib.rs b/frame/nicks/src/lib.rs index 7c03225fa1446..63334a8f13760 100644 --- a/frame/nicks/src/lib.rs +++ b/frame/nicks/src/lib.rs @@ -258,6 +258,7 @@ mod tests { UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, Nicks: pallet_nicks::{Module, Call, Storage, Event}, } @@ -286,10 +287,16 @@ mod tests { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountStorage = System; + type AccountStorage = Accounts; type SystemWeightInfo = (); type SS58Prefix = (); } + impl pallet_accounts::Config for Test { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = pallet_balances::AccountData; + } parameter_types! { pub const ExistentialDeposit: u64 = 1; } @@ -299,7 +306,8 @@ mod tests { type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type AccountStore = Balances; + type AccountStore = Accounts; + type ReferencedAccount = Accounts; type WeightInfo = (); } parameter_types! { diff --git a/frame/offences/benchmarking/Cargo.toml b/frame/offences/benchmarking/Cargo.toml index 6be2787734a4f..2e8a00f533492 100644 --- a/frame/offences/benchmarking/Cargo.toml +++ b/frame/offences/benchmarking/Cargo.toml @@ -18,6 +18,7 @@ frame-benchmarking = { version = "3.1.0", default-features = false, path = "../. frame-support = { version = "3.0.0", default-features = false, path = "../../support" } frame-system = { version = "3.0.0", default-features = false, path = "../../system" } pallet-babe = { version = "3.0.0", default-features = false, path = "../../babe" } +pallet-accounts = { version = "3.0.0", default-features = false, path = "../../accounts" } pallet-balances = { version = "3.0.0", default-features = false, path = "../../balances" } pallet-grandpa = { version = "3.0.0", default-features = false, path = "../../grandpa" } pallet-im-online = { version = "3.0.0", default-features = false, path = "../../im-online" } diff --git a/frame/offences/benchmarking/src/lib.rs b/frame/offences/benchmarking/src/lib.rs index 0ceebaecd91ae..99d81c5c4161d 100644 --- a/frame/offences/benchmarking/src/lib.rs +++ b/frame/offences/benchmarking/src/lib.rs @@ -281,13 +281,20 @@ benchmarks! { StakingEvent::::Slash(stash, BalanceOf::::from(slash_amount)) )) .collect::>(); + let reward_events = reporters.into_iter() - .flat_map(|reporter| vec![ - frame_system::Event::::NewAccount(reporter.clone()).into(), - ::Event::from( - pallet_balances::Event::::Endowed(reporter, (reward_amount / r).into()) - ).into() - ]); + .flat_map(|reporter| { + let accounts_event: ::Event + = pallet_accounts::Event::NewAccount(reporter.clone()).into(); + let new_account_event: ::Event = accounts_event.into(); + + vec![ + new_account_event, + ::Event::from( + pallet_balances::Event::::Endowed(reporter, (reward_amount / r).into()) + ).into() + ] + }); // rewards are applied after first offender and it's nominators let slash_rest = slash_events.split_off(1 + n as usize); diff --git a/frame/offences/benchmarking/src/mock.rs b/frame/offences/benchmarking/src/mock.rs index 124e6b13b77ac..17cb1c45c13a1 100644 --- a/frame/offences/benchmarking/src/mock.rs +++ b/frame/offences/benchmarking/src/mock.rs @@ -60,11 +60,15 @@ impl frame_system::Config for Test { type BlockHashCount = (); type Version = (); type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); type SystemWeightInfo = (); type SS58Prefix = (); + type AccountStorage = Accounts; +} +impl pallet_accounts::Config for Test { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = pallet_balances::AccountData; } parameter_types! { pub const ExistentialDeposit: Balance = 10; @@ -75,7 +79,8 @@ impl pallet_balances::Config for Test { type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; + type AccountStore = Accounts; + type ReferencedAccount = Accounts; type WeightInfo = (); } @@ -130,6 +135,7 @@ impl pallet_session::Config for Test { type ValidatorIdOf = pallet_staking::StashOf; type DisabledValidatorsThreshold = (); type WeightInfo = (); + type ReferencedAccount = Accounts; } pallet_staking_reward_curve::build! { @@ -180,6 +186,7 @@ impl pallet_staking::Config for Test { type OffchainSolutionWeightLimit = (); type ElectionProvider = onchain::OnChainSequentialPhragmen; type WeightInfo = (); + type ReferencedAccount = Accounts; } impl pallet_im_online::Config for Test { @@ -220,6 +227,7 @@ frame_support::construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic { System: system::{Module, Call, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, Staking: pallet_staking::{Module, Call, Config, Storage, Event, ValidateUnsigned}, Session: pallet_session::{Module, Call, Storage, Event, Config}, From 8e889f6a0b4245fa015d13914d78100ee46c3646 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Mon, 8 Mar 2021 23:25:10 -0400 Subject: [PATCH 24/31] fix more tests --- Cargo.lock | 10 ++++++++++ frame/proxy/Cargo.toml | 1 + frame/proxy/src/tests.rs | 14 ++++++++++---- frame/recovery/Cargo.toml | 3 +-- frame/recovery/src/lib.rs | 6 +++--- frame/recovery/src/mock.rs | 16 ++++++++++++---- frame/scored-pool/Cargo.toml | 1 + frame/scored-pool/src/mock.rs | 14 ++++++++++---- frame/session/Cargo.toml | 1 + frame/session/benchmarking/Cargo.toml | 1 + frame/session/benchmarking/src/mock.rs | 16 ++++++++++++---- frame/session/src/historical/mod.rs | 6 +++--- frame/session/src/historical/offchain.rs | 6 +++--- frame/session/src/mock.rs | 16 +++++++++++++--- frame/session/src/tests.rs | 6 +++--- frame/society/Cargo.toml | 1 + frame/society/src/mock.rs | 15 +++++++++++---- frame/staking/src/mock.rs | 16 ++++++++++++---- frame/tips/Cargo.toml | 1 + frame/tips/src/tests.rs | 14 ++++++++++---- frame/transaction-payment/Cargo.toml | 1 + frame/transaction-payment/src/lib.rs | 19 +++++++++++++------ frame/treasury/Cargo.toml | 4 ++-- frame/treasury/src/tests.rs | 14 ++++++++++---- frame/utility/Cargo.toml | 1 + frame/utility/src/tests.rs | 14 ++++++++++---- frame/vesting/Cargo.toml | 1 + frame/vesting/src/lib.rs | 16 +++++++++++----- test-utils/runtime/src/lib.rs | 4 +++- 29 files changed, 171 insertions(+), 67 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 426743a5f9658..8c0a41135d301 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5176,6 +5176,7 @@ dependencies = [ "frame-benchmarking", "frame-support", "frame-system", + "pallet-accounts", "pallet-balances", "pallet-utility", "parity-scale-codec", @@ -5241,6 +5242,7 @@ version = "3.0.0" dependencies = [ "frame-support", "frame-system", + "pallet-accounts", "pallet-balances", "parity-scale-codec", "serde", @@ -5258,6 +5260,7 @@ dependencies = [ "frame-system", "impl-trait-for-tuples", "lazy_static", + "pallet-accounts", "pallet-timestamp", "parity-scale-codec", "serde", @@ -5278,6 +5281,7 @@ dependencies = [ "frame-benchmarking", "frame-support", "frame-system", + "pallet-accounts", "pallet-balances", "pallet-session", "pallet-staking", @@ -5300,6 +5304,7 @@ version = "3.0.0" dependencies = [ "frame-support", "frame-system", + "pallet-accounts", "pallet-balances", "parity-scale-codec", "rand_chacha 0.2.2", @@ -5432,6 +5437,7 @@ dependencies = [ "frame-benchmarking", "frame-support", "frame-system", + "pallet-accounts", "pallet-balances", "pallet-treasury", "parity-scale-codec", @@ -5449,6 +5455,7 @@ version = "3.0.0" dependencies = [ "frame-support", "frame-system", + "pallet-accounts", "pallet-balances", "parity-scale-codec", "serde", @@ -5495,6 +5502,7 @@ dependencies = [ "frame-support", "frame-system", "impl-trait-for-tuples", + "pallet-accounts", "pallet-balances", "parity-scale-codec", "serde", @@ -5512,6 +5520,7 @@ dependencies = [ "frame-benchmarking", "frame-support", "frame-system", + "pallet-accounts", "pallet-balances", "parity-scale-codec", "serde", @@ -5530,6 +5539,7 @@ dependencies = [ "frame-support", "frame-system", "hex-literal", + "pallet-accounts", "pallet-balances", "parity-scale-codec", "serde", diff --git a/frame/proxy/Cargo.toml b/frame/proxy/Cargo.toml index 2934b9953b316..108febdc5cb54 100644 --- a/frame/proxy/Cargo.toml +++ b/frame/proxy/Cargo.toml @@ -26,6 +26,7 @@ frame-benchmarking = { version = "3.1.0", default-features = false, path = "../b [dev-dependencies] sp-core = { version = "3.0.0", path = "../../primitives/core" } +pallet-accounts = { version = "3.0.0", path = "../accounts" } pallet-balances = { version = "3.0.0", path = "../balances" } pallet-utility = { version = "3.0.0", path = "../utility" } diff --git a/frame/proxy/src/tests.rs b/frame/proxy/src/tests.rs index b31ef1dfdb2fe..18ea17c368fb4 100644 --- a/frame/proxy/src/tests.rs +++ b/frame/proxy/src/tests.rs @@ -39,6 +39,7 @@ frame_support::construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, Proxy: proxy::{Module, Call, Storage, Event}, Utility: pallet_utility::{Module, Call, Event}, @@ -68,11 +69,15 @@ impl frame_system::Config for Test { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); type SystemWeightInfo = (); type SS58Prefix = (); + type AccountStorage = Accounts; +} +impl pallet_accounts::Config for Test { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = pallet_balances::AccountData; } parameter_types! { pub const ExistentialDeposit: u64 = 1; @@ -83,7 +88,8 @@ impl pallet_balances::Config for Test { type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; + type AccountStore = Accounts; + type ReferencedAccount = Accounts; type WeightInfo = (); } impl pallet_utility::Config for Test { diff --git a/frame/recovery/Cargo.toml b/frame/recovery/Cargo.toml index 0efe8c6f5c3cc..0cb7c4c708e91 100644 --- a/frame/recovery/Cargo.toml +++ b/frame/recovery/Cargo.toml @@ -21,10 +21,10 @@ sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/ sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } frame-support = { version = "3.0.0", default-features = false, path = "../support" } frame-system = { version = "3.0.0", default-features = false, path = "../system" } -pallet-accounts = { version = "3.0.0", default-features = false, path = "../accounts" } [dev-dependencies] sp-core = { version = "3.0.0", path = "../../primitives/core" } +pallet-accounts = { version = "3.0.0", path = "../accounts" } pallet-balances = { version = "3.0.0", path = "../balances" } [features] @@ -37,6 +37,5 @@ std = [ "sp-runtime/std", "frame-support/std", "frame-system/std", - "pallet-accounts/std", ] try-runtime = ["frame-support/try-runtime"] diff --git a/frame/recovery/src/lib.rs b/frame/recovery/src/lib.rs index 3fbf18bcdd0aa..04563cbe21695 100644 --- a/frame/recovery/src/lib.rs +++ b/frame/recovery/src/lib.rs @@ -175,7 +175,7 @@ type BalanceOf = <::Currency as Currency<::AccountId>>::Balance; /// Configuration trait. -pub trait Config: frame_system::Config + pallet_accounts::Config { +pub trait Config: frame_system::Config { /// The overarching event type. type Event: From> + Into<::Event>; @@ -591,7 +591,7 @@ decl_module! { recovery_config.threshold as usize <= active_recovery.friends.len(), Error::::Threshold ); - pallet_accounts::Module::::inc_consumers(&who).map_err(|_| Error::::BadState)?; + T::ReferencedAccount::inc_consumers(&who).map_err(|_| Error::::BadState)?; // Create the recovery storage item Proxy::::insert(&who, &account); Self::deposit_event(RawEvent::AccountRecovered(account, who)); @@ -680,7 +680,7 @@ decl_module! { // Check `who` is allowed to make a call on behalf of `account` ensure!(Self::proxy(&who) == Some(account), Error::::NotAllowed); Proxy::::remove(&who); - pallet_accounts::Module::::dec_consumers(&who); + T::ReferencedAccount::dec_consumers(&who); } } } diff --git a/frame/recovery/src/mock.rs b/frame/recovery/src/mock.rs index ee38b0e24cc60..9630c765159d7 100644 --- a/frame/recovery/src/mock.rs +++ b/frame/recovery/src/mock.rs @@ -36,6 +36,7 @@ frame_support::construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, Recovery: recovery::{Module, Call, Storage, Event}, } @@ -65,11 +66,16 @@ impl frame_system::Config for Test { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); type SystemWeightInfo = (); type SS58Prefix = (); + type AccountStorage = Accounts; +} + +impl pallet_accounts::Config for Test { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = pallet_balances::AccountData; } parameter_types! { @@ -82,7 +88,8 @@ impl pallet_balances::Config for Test { type DustRemoval = (); type Event = Event; type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; + type AccountStore = Accounts; + type ReferencedAccount = Accounts; type WeightInfo = (); } @@ -101,6 +108,7 @@ impl Config for Test { type FriendDepositFactor = FriendDepositFactor; type MaxFriends = MaxFriends; type RecoveryDeposit = RecoveryDeposit; + type ReferencedAccount = Accounts; } pub type BalancesCall = pallet_balances::Call; diff --git a/frame/scored-pool/Cargo.toml b/frame/scored-pool/Cargo.toml index 97e3a954d7e25..c60d2a821acf6 100644 --- a/frame/scored-pool/Cargo.toml +++ b/frame/scored-pool/Cargo.toml @@ -22,6 +22,7 @@ frame-support = { version = "3.0.0", default-features = false, path = "../suppor frame-system = { version = "3.0.0", default-features = false, path = "../system" } [dev-dependencies] +pallet-accounts = { version = "3.0.0", path = "../accounts" } pallet-balances = { version = "3.0.0", path = "../balances" } sp-core = { version = "3.0.0", path = "../../primitives/core" } diff --git a/frame/scored-pool/src/mock.rs b/frame/scored-pool/src/mock.rs index 3c4263b813e41..8ce5387ffad27 100644 --- a/frame/scored-pool/src/mock.rs +++ b/frame/scored-pool/src/mock.rs @@ -38,6 +38,7 @@ frame_support::construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, ScoredPool: pallet_scored_pool::{Module, Call, Storage, Config, Event}, } @@ -74,11 +75,15 @@ impl frame_system::Config for Test { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); type SystemWeightInfo = (); type SS58Prefix = (); + type AccountStorage = Accounts; +} +impl pallet_accounts::Config for Test { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = pallet_balances::AccountData; } impl pallet_balances::Config for Test { @@ -87,7 +92,8 @@ impl pallet_balances::Config for Test { type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; + type AccountStore = Accounts; + type ReferencedAccount = Accounts; type WeightInfo = (); } diff --git a/frame/session/Cargo.toml b/frame/session/Cargo.toml index 91c5993438b12..8eaf64374bf8c 100644 --- a/frame/session/Cargo.toml +++ b/frame/session/Cargo.toml @@ -29,6 +29,7 @@ impl-trait-for-tuples = "0.2.1" [dev-dependencies] sp-application-crypto = { version = "3.0.0", path = "../../primitives/application-crypto" } +pallet-accounts = { version = "3.0.0", path = "../accounts" } lazy_static = "1.4.0" [features] diff --git a/frame/session/benchmarking/Cargo.toml b/frame/session/benchmarking/Cargo.toml index 47265ed5ef7a8..508dc87ceb98b 100644 --- a/frame/session/benchmarking/Cargo.toml +++ b/frame/session/benchmarking/Cargo.toml @@ -30,6 +30,7 @@ sp-core = { version = "3.0.0", path = "../../../primitives/core" } pallet-staking-reward-curve = { version = "3.0.0", path = "../../staking/reward-curve" } sp-io ={ version = "3.0.0", path = "../../../primitives/io" } pallet-timestamp = { version = "3.0.0", path = "../../timestamp" } +pallet-accounts = { version = "3.0.0", path = "../../accounts" } pallet-balances = { version = "3.0.0", path = "../../balances" } sp-election-providers = { version = "3.0.0", path = "../../../primitives/election-providers" } diff --git a/frame/session/benchmarking/src/mock.rs b/frame/session/benchmarking/src/mock.rs index 0eba5452b28d0..1c45c6f6194a5 100644 --- a/frame/session/benchmarking/src/mock.rs +++ b/frame/session/benchmarking/src/mock.rs @@ -38,6 +38,7 @@ frame_support::construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, Staking: pallet_staking::{Module, Call, Config, Storage, Event, ValidateUnsigned}, Session: pallet_session::{Module, Call, Storage, Event, Config}, @@ -62,11 +63,15 @@ impl frame_system::Config for Test { type BlockHashCount = (); type Version = (); type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); type SystemWeightInfo = (); type SS58Prefix = (); + type AccountStorage = Accounts; +} +impl pallet_accounts::Config for Test { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = pallet_balances::AccountData; } parameter_types! { pub const ExistentialDeposit: Balance = 10; @@ -77,7 +82,8 @@ impl pallet_balances::Config for Test { type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; + type AccountStore = Accounts; + type ReferencedAccount = Accounts; type WeightInfo = (); } @@ -126,6 +132,7 @@ impl pallet_session::Config for Test { type ValidatorId = AccountId; type ValidatorIdOf = pallet_staking::StashOf; type DisabledValidatorsThreshold = (); + type ReferencedAccount = Accounts; type WeightInfo = (); } pallet_staking_reward_curve::build! { @@ -184,6 +191,7 @@ impl pallet_staking::Config for Test { type MinSolutionScoreBump = (); type OffchainSolutionWeightLimit = (); type ElectionProvider = onchain::OnChainSequentialPhragmen; + type ReferencedAccount = Accounts; type WeightInfo = (); } diff --git a/frame/session/src/historical/mod.rs b/frame/session/src/historical/mod.rs index 9b4d2704cf456..8a9ff8dc2b5cc 100644 --- a/frame/session/src/historical/mod.rs +++ b/frame/session/src/historical/mod.rs @@ -344,9 +344,9 @@ pub(crate) mod tests { use sp_runtime::testing::UintAuthorityId; use crate::mock::{ NEXT_VALIDATORS, force_new_session, - set_next_validators, Test, System, Session, + set_next_validators, Test, System, Session, Accounts, }; - use frame_support::traits::{KeyOwnerProofSystem, OnInitialize}; + use frame_support::traits::{KeyOwnerProofSystem, OnInitialize, ReferencedAccount}; use frame_support::BasicExternalities; type Historical = Module; @@ -358,7 +358,7 @@ pub(crate) mod tests { ); BasicExternalities::execute_with_storage(&mut t, || { for (ref k, ..) in &keys { - frame_system::Module::::inc_providers(k); + Accounts::inc_providers(k); } }); crate::GenesisConfig:: { keys }.assimilate_storage(&mut t).unwrap(); diff --git a/frame/session/src/historical/offchain.rs b/frame/session/src/historical/offchain.rs index 38cf09124ccf6..50d29b2a8dc19 100644 --- a/frame/session/src/historical/offchain.rs +++ b/frame/session/src/historical/offchain.rs @@ -140,10 +140,10 @@ mod tests { use super::super::{onchain, Module}; use super::*; use crate::mock::{ - force_new_session, set_next_validators, Session, System, Test, NEXT_VALIDATORS, + force_new_session, set_next_validators, Session, System, Test, NEXT_VALIDATORS, Accounts, }; use codec::Encode; - use frame_support::traits::{KeyOwnerProofSystem, OnInitialize}; + use frame_support::traits::{KeyOwnerProofSystem, OnInitialize, ReferencedAccount}; use sp_core::crypto::key_types::DUMMY; use sp_core::offchain::{ testing::TestOffchainExt, @@ -166,7 +166,7 @@ mod tests { ); BasicExternalities::execute_with_storage(&mut t, || { for (ref k, ..) in &keys { - frame_system::Module::::inc_providers(k); + Accounts::inc_providers(k); } }); diff --git a/frame/session/src/mock.rs b/frame/session/src/mock.rs index 0e426dfb6d37e..86b13a4c45ea8 100644 --- a/frame/session/src/mock.rs +++ b/frame/session/src/mock.rs @@ -79,6 +79,7 @@ frame_support::construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Session: pallet_session::{Module, Call, Storage, Event, Config}, Historical: pallet_session_historical::{Module}, } @@ -92,6 +93,7 @@ frame_support::construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Session: pallet_session::{Module, Call, Storage, Event, Config}, } ); @@ -210,11 +212,11 @@ pub fn new_test_ext() -> sp_io::TestExternalities { ); BasicExternalities::execute_with_storage(&mut t, || { for (ref k, ..) in &keys { - frame_system::Module::::inc_providers(k); + Accounts::inc_providers(k); } - frame_system::Module::::inc_providers(&4); + Accounts::inc_providers(&4); // An additional identity that we use. - frame_system::Module::::inc_providers(&69); + Accounts::inc_providers(&69); }); pallet_session::GenesisConfig:: { keys }.assimilate_storage(&mut t).unwrap(); sp_io::TestExternalities::new(t) @@ -250,6 +252,13 @@ impl frame_system::Config for Test { type SS58Prefix = (); } +impl pallet_accounts::Config for Test { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = (); +} + impl pallet_timestamp::Config for Test { type Moment = u64; type OnTimestampSet = (); @@ -275,6 +284,7 @@ impl Config for Test { type DisabledValidatorsThreshold = DisabledValidatorsThreshold; type NextSessionRotation = (); type WeightInfo = (); + type ReferencedAccount = Accounts; } #[cfg(feature = "historical")] diff --git a/frame/session/src/tests.rs b/frame/session/src/tests.rs index b2e086aed90c5..6f209f45e9bc0 100644 --- a/frame/session/src/tests.rs +++ b/frame/session/src/tests.rs @@ -26,7 +26,7 @@ use mock::{ SESSION_CHANGED, TEST_SESSION_CHANGED, authorities, force_new_session, set_next_validators, set_session_length, session_changed, Origin, System, Session, reset_before_session_end_called, before_session_end_called, new_test_ext, - PreUpgradeMockSessionKeys, + PreUpgradeMockSessionKeys, Accounts, }; fn initialize_block(block: u64) { @@ -61,9 +61,9 @@ fn keys_cleared_on_kill() { let id = DUMMY; assert_eq!(Session::key_owner(id, UintAuthorityId(1).get_raw(id)), Some(1)); - assert!(System::is_provider_required(&1)); + assert!(Accounts::is_provider_required(&1)); assert_ok!(Session::purge_keys(Origin::signed(1))); - assert!(!System::is_provider_required(&1)); + assert!(!Accounts::is_provider_required(&1)); assert_eq!(Session::load_keys(&1), None); assert_eq!(Session::key_owner(id, UintAuthorityId(1).get_raw(id)), None); diff --git a/frame/society/Cargo.toml b/frame/society/Cargo.toml index 913e40e0301d6..8fae2b4e8533a 100644 --- a/frame/society/Cargo.toml +++ b/frame/society/Cargo.toml @@ -24,6 +24,7 @@ rand_chacha = { version = "0.2", default-features = false } [dev-dependencies] sp-core = { version = "3.0.0", path = "../../primitives/core" } sp-io ={ version = "3.0.0", path = "../../primitives/io" } +pallet-accounts = { version = "3.0.0", path = "../accounts" } pallet-balances = { version = "3.0.0", path = "../balances" } [features] diff --git a/frame/society/src/mock.rs b/frame/society/src/mock.rs index 4b1bb21dd18db..5576f49c6363d 100644 --- a/frame/society/src/mock.rs +++ b/frame/society/src/mock.rs @@ -41,6 +41,7 @@ frame_support::construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, Society: pallet_society::{Module, Call, Storage, Event, Config}, } @@ -84,20 +85,26 @@ impl frame_system::Config for Test { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type OnNewAccount = (); - type OnKilledAccount = (); - type AccountData = pallet_balances::AccountData; + type AccountStorage = Accounts; type SystemWeightInfo = (); type SS58Prefix = (); } +impl pallet_accounts::Config for Test { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = pallet_balances::AccountData; +} + impl pallet_balances::Config for Test { type MaxLocks = (); type Balance = u64; type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; + type AccountStore = Accounts; + type ReferencedAccount = Accounts; type WeightInfo = (); } diff --git a/frame/staking/src/mock.rs b/frame/staking/src/mock.rs index 0d6701c48b894..142d673b3a12f 100644 --- a/frame/staking/src/mock.rs +++ b/frame/staking/src/mock.rs @@ -99,6 +99,7 @@ frame_support::construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Timestamp: pallet_timestamp::{Module, Call, Storage, Inherent}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, Staking: staking::{Module, Call, Config, Storage, Event, ValidateUnsigned}, @@ -150,19 +151,24 @@ impl frame_system::Config for Test { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = Accounts; type SystemWeightInfo = (); type SS58Prefix = (); } +impl pallet_accounts::Config for Test { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = pallet_balances::AccountData; +} impl pallet_balances::Config for Test { type MaxLocks = MaxLocks; type Balance = Balance; type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; + type AccountStore = Accounts; + type ReferencedAccount = Accounts; type WeightInfo = (); } parameter_types! { @@ -184,6 +190,7 @@ impl pallet_session::Config for Test { type ValidatorIdOf = crate::StashOf; type DisabledValidatorsThreshold = DisabledValidatorsThreshold; type NextSessionRotation = pallet_session::PeriodicSessions; + type ReferencedAccount = Accounts; type WeightInfo = (); } @@ -269,6 +276,7 @@ impl Config for Test { type UnsignedPriority = UnsignedPriority; type OffchainSolutionWeightLimit = OffchainSolutionWeightLimit; type ElectionProvider = onchain::OnChainSequentialPhragmen; + type ReferencedAccount = Accounts; type WeightInfo = (); } diff --git a/frame/tips/Cargo.toml b/frame/tips/Cargo.toml index a16c9b91327ec..942b949412000 100644 --- a/frame/tips/Cargo.toml +++ b/frame/tips/Cargo.toml @@ -27,6 +27,7 @@ frame-benchmarking = { version = "3.1.0", default-features = false, path = "../b sp-io ={ version = "3.0.0", path = "../../primitives/io" } sp-core = { version = "3.0.0", path = "../../primitives/core" } sp-storage = { version = "3.0.0", path = "../../primitives/storage" } +pallet-accounts = { version = "3.0.0", path = "../accounts" } pallet-balances = { version = "3.0.0", path = "../balances" } [features] diff --git a/frame/tips/src/tests.rs b/frame/tips/src/tests.rs index 413e2dd9437e2..930f39ac3f817 100644 --- a/frame/tips/src/tests.rs +++ b/frame/tips/src/tests.rs @@ -41,6 +41,7 @@ frame_support::construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, Treasury: pallet_treasury::{Module, Call, Storage, Config, Event}, TipsModTestInst: tips::{Module, Call, Storage, Event}, @@ -71,11 +72,15 @@ impl frame_system::Config for Test { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); type SystemWeightInfo = (); type SS58Prefix = (); + type AccountStorage = Accounts; +} +impl pallet_accounts::Config for Test { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = pallet_balances::AccountData; } parameter_types! { pub const ExistentialDeposit: u64 = 1; @@ -86,7 +91,8 @@ impl pallet_balances::Config for Test { type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; + type AccountStore = Accounts; + type ReferencedAccount = Accounts; type WeightInfo = (); } thread_local! { diff --git a/frame/transaction-payment/Cargo.toml b/frame/transaction-payment/Cargo.toml index 2a7fbe503efaf..e2b9b9f11e444 100644 --- a/frame/transaction-payment/Cargo.toml +++ b/frame/transaction-payment/Cargo.toml @@ -25,6 +25,7 @@ sp-core = { version = "3.0.0", path = "../../primitives/core", default-features [dev-dependencies] serde_json = "1.0.41" +pallet-accounts = { version = "3.0.0", path = "../accounts" } pallet-balances = { version = "3.0.0", path = "../balances" } sp-storage = { version = "3.0.0", path = "../../primitives/storage" } diff --git a/frame/transaction-payment/src/lib.rs b/frame/transaction-payment/src/lib.rs index 709a8f69a487d..f04b28cfdc056 100644 --- a/frame/transaction-payment/src/lib.rs +++ b/frame/transaction-payment/src/lib.rs @@ -631,6 +631,7 @@ mod tests { UncheckedExtrinsic = UncheckedExtrinsic, { System: system::{Module, Call, Config, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, TransactionPayment: pallet_transaction_payment::{Module, Storage}, } @@ -682,24 +683,30 @@ mod tests { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = Accounts; type SystemWeightInfo = (); type SS58Prefix = (); } + impl pallet_accounts::Config for Runtime { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = pallet_balances::AccountData; + } + parameter_types! { pub const ExistentialDeposit: u64 = 1; } impl pallet_balances::Config for Runtime { + type MaxLocks = (); type Balance = u64; type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; - type MaxLocks = (); + type AccountStore = Accounts; + type ReferencedAccount = Accounts; type WeightInfo = (); } @@ -1160,7 +1167,7 @@ mod tests { })); // Killed Event assert!(System::events().iter().any(|event| { - event.event == Event::system(system::Event::KilledAccount(2)) + event.event == Event::pallet_accounts(pallet_accounts::Event::KilledAccount(2)) })); }); } diff --git a/frame/treasury/Cargo.toml b/frame/treasury/Cargo.toml index da0ffcb725c9b..7bacb771da266 100644 --- a/frame/treasury/Cargo.toml +++ b/frame/treasury/Cargo.toml @@ -19,7 +19,6 @@ sp-std = { version = "3.0.0", default-features = false, path = "../../primitives sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" } frame-support = { version = "3.0.0", default-features = false, path = "../support" } frame-system = { version = "3.0.0", default-features = false, path = "../system" } -pallet-balances = { version = "3.0.0", default-features = false, path = "../balances" } impl-trait-for-tuples = "0.2.1" frame-benchmarking = { version = "3.1.0", default-features = false, path = "../benchmarking", optional = true } @@ -28,6 +27,8 @@ frame-benchmarking = { version = "3.1.0", default-features = false, path = "../b sp-io ={ version = "3.0.0", path = "../../primitives/io" } sp-core = { version = "3.0.0", path = "../../primitives/core" } sp-storage = { version = "3.0.0", path = "../../primitives/storage" } +pallet-accounts = { version = "3.0.0", path = "../accounts" } +pallet-balances = { version = "3.0.0", path = "../balances" } [features] default = ["std"] @@ -38,7 +39,6 @@ std = [ "sp-runtime/std", "frame-support/std", "frame-system/std", - "pallet-balances/std", ] runtime-benchmarks = [ "frame-benchmarking", diff --git a/frame/treasury/src/tests.rs b/frame/treasury/src/tests.rs index 3c70099843ea8..956cd2a56517b 100644 --- a/frame/treasury/src/tests.rs +++ b/frame/treasury/src/tests.rs @@ -44,6 +44,7 @@ frame_support::construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, Treasury: treasury::{Module, Call, Storage, Config, Event}, } @@ -72,11 +73,15 @@ impl frame_system::Config for Test { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); type SystemWeightInfo = (); type SS58Prefix = (); + type AccountStorage = Accounts; +} +impl pallet_accounts::Config for Test { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = pallet_balances::AccountData; } parameter_types! { pub const ExistentialDeposit: u64 = 1; @@ -87,7 +92,8 @@ impl pallet_balances::Config for Test { type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; + type AccountStore = Accounts; + type ReferencedAccount = Accounts; type WeightInfo = (); } thread_local! { diff --git a/frame/utility/Cargo.toml b/frame/utility/Cargo.toml index f55cff4d653c5..20875f8437de1 100644 --- a/frame/utility/Cargo.toml +++ b/frame/utility/Cargo.toml @@ -26,6 +26,7 @@ frame-benchmarking = { version = "3.1.0", default-features = false, path = "../b [dev-dependencies] sp-core = { version = "3.0.0", path = "../../primitives/core" } +pallet-accounts = { version = "3.0.0", path = "../accounts" } pallet-balances = { version = "3.0.0", path = "../balances" } [features] diff --git a/frame/utility/src/tests.rs b/frame/utility/src/tests.rs index af31bbe96cbc4..451fca7e0d6f6 100644 --- a/frame/utility/src/tests.rs +++ b/frame/utility/src/tests.rs @@ -76,6 +76,7 @@ frame_support::construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, Utility: utility::{Module, Call, Event}, Example: example::{Module, Call}, @@ -105,11 +106,15 @@ impl frame_system::Config for Test { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); type SystemWeightInfo = (); type SS58Prefix = (); + type AccountStorage = Accounts; +} +impl pallet_accounts::Config for Test { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = pallet_balances::AccountData; } parameter_types! { pub const ExistentialDeposit: u64 = 1; @@ -120,7 +125,8 @@ impl pallet_balances::Config for Test { type DustRemoval = (); type Event = Event; type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; + type AccountStore = Accounts; + type ReferencedAccount = Accounts; type WeightInfo = (); } parameter_types! { diff --git a/frame/vesting/Cargo.toml b/frame/vesting/Cargo.toml index e1335237eb508..2d4d968ac5433 100644 --- a/frame/vesting/Cargo.toml +++ b/frame/vesting/Cargo.toml @@ -25,6 +25,7 @@ frame-benchmarking = { version = "3.1.0", default-features = false, path = "../b [dev-dependencies] sp-io = { version = "3.0.0", path = "../../primitives/io" } sp-core = { version = "3.0.0", path = "../../primitives/core" } +pallet-accounts = { version = "3.0.0", path = "../accounts" } pallet-balances = { version = "3.0.0", path = "../balances" } sp-storage = { version = "3.0.0", path = "../../primitives/storage" } hex-literal = "0.3.1" diff --git a/frame/vesting/src/lib.rs b/frame/vesting/src/lib.rs index 9cf9166b37c0c..6c49ac8fb800a 100644 --- a/frame/vesting/src/lib.rs +++ b/frame/vesting/src/lib.rs @@ -409,6 +409,7 @@ mod tests { UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, Vesting: pallet_vesting::{Module, Call, Storage, Event, Config}, } @@ -437,22 +438,27 @@ mod tests { type BlockHashCount = BlockHashCount; type Version = (); type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); + type AccountStorage = Accounts; type SystemWeightInfo = (); type SS58Prefix = (); } + impl pallet_accounts::Config for Test { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = pallet_balances::AccountData; + } parameter_types! { pub const MaxLocks: u32 = 10; } impl pallet_balances::Config for Test { + type MaxLocks = MaxLocks; type Balance = u64; type DustRemoval = (); type Event = Event; type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; - type MaxLocks = MaxLocks; + type AccountStore = Accounts; + type ReferencedAccount = Accounts; type WeightInfo = (); } parameter_types! { diff --git a/test-utils/runtime/src/lib.rs b/test-utils/runtime/src/lib.rs index 7095c56b49e74..0aad99b3d0275 100644 --- a/test-utils/runtime/src/lib.rs +++ b/test-utils/runtime/src/lib.rs @@ -500,7 +500,9 @@ parameter_types! { impl pallet_accounts::Config for Runtime { type Event = Event; - type AccountStorage = System; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = (); } impl frame_system::Config for Runtime { From 3956a232e4c7e41fbdd3b8b5cc539aa8d1bcab2c Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Tue, 9 Mar 2021 00:00:09 -0400 Subject: [PATCH 25/31] executor test fixes --- Cargo.lock | 1 + bin/node/executor/Cargo.toml | 1 + bin/node/executor/tests/basic.rs | 3 ++- bin/node/executor/tests/fees.rs | 2 +- bin/node/executor/tests/submit_transaction.rs | 4 ++-- 5 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8c0a41135d301..e49cd247b3f5e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4052,6 +4052,7 @@ dependencies = [ "node-primitives", "node-runtime", "node-testing", + "pallet-accounts", "pallet-balances", "pallet-contracts", "pallet-grandpa", diff --git a/bin/node/executor/Cargo.toml b/bin/node/executor/Cargo.toml index fb7fc9191141c..3dc784b13114e 100644 --- a/bin/node/executor/Cargo.toml +++ b/bin/node/executor/Cargo.toml @@ -29,6 +29,7 @@ criterion = "0.3.0" frame-support = { version = "3.0.0", path = "../../../frame/support" } frame-system = { version = "3.0.0", path = "../../../frame/system" } node-testing = { version = "2.0.0", path = "../testing" } +pallet-accounts = { version = "3.0.0", path = "../../../frame/accounts" } pallet-balances = { version = "3.0.0", path = "../../../frame/balances" } pallet-contracts = { version = "3.0.0", path = "../../../frame/contracts" } pallet-grandpa = { version = "3.0.0", path = "../../../frame/grandpa" } diff --git a/bin/node/executor/tests/basic.rs b/bin/node/executor/tests/basic.rs index 279b6a776031a..a416d6f7dcc3d 100644 --- a/bin/node/executor/tests/basic.rs +++ b/bin/node/executor/tests/basic.rs @@ -26,7 +26,8 @@ use sp_runtime::{ traits::Hash as HashT, transaction_validity::InvalidTransaction, }; -use frame_system::{self, EventRecord, Phase, AccountInfo}; +use frame_system::{self, EventRecord, Phase}; +use pallet_accounts::AccountInfo; use node_runtime::{ Header, Block, UncheckedExtrinsic, CheckedExtrinsic, Call, Runtime, Balances, diff --git a/bin/node/executor/tests/fees.rs b/bin/node/executor/tests/fees.rs index ad24db03f9832..74bfc1fe459b5 100644 --- a/bin/node/executor/tests/fees.rs +++ b/bin/node/executor/tests/fees.rs @@ -125,7 +125,7 @@ fn fee_multiplier_increases_and_decreases_on_big_weight() { } fn new_account_info(free_dollars: u128) -> Vec { - frame_system::AccountInfo { + pallet_accounts::AccountInfo { nonce: 0u32, consumers: 0, providers: 0, diff --git a/bin/node/executor/tests/submit_transaction.rs b/bin/node/executor/tests/submit_transaction.rs index 3de0758d81462..fd3e5ef9be342 100644 --- a/bin/node/executor/tests/submit_transaction.rs +++ b/bin/node/executor/tests/submit_transaction.rs @@ -252,8 +252,8 @@ fn submitted_transaction_should_be_valid() { let author = extrinsic.signature.clone().unwrap().0; let address = Indices::lookup(author).unwrap(); let data = pallet_balances::AccountData { free: 5_000_000_000_000, ..Default::default() }; - let account = frame_system::AccountInfo { data, .. Default::default() }; - >::insert(&address, account); + let account = pallet_accounts::AccountInfo { data, .. Default::default() }; + >::insert(&address, account); // check validity let res = Executive::validate_transaction(source, extrinsic).unwrap(); From 7c84785748c855fc52602feb26217c359dc325f8 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Tue, 9 Mar 2021 00:48:22 -0400 Subject: [PATCH 26/31] use proper storage item for executor tests --- bin/node/executor/tests/basic.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/bin/node/executor/tests/basic.rs b/bin/node/executor/tests/basic.rs index a416d6f7dcc3d..7a116501ab369 100644 --- a/bin/node/executor/tests/basic.rs +++ b/bin/node/executor/tests/basic.rs @@ -169,7 +169,7 @@ fn block_with_size(time: u64, nonce: u32, size: usize) -> (Vec, Hash) { fn panic_execution_with_foreign_code_gives_error() { let mut t = new_test_ext(bloaty_code_unwrap(), false); t.insert( - >::hashed_key_for(alice()), + >::hashed_key_for(alice()), (69u128, 0u32, 0u128, 0u128, 0u128).encode() ); t.insert(>::hashed_key().to_vec(), 69_u128.encode()); @@ -198,7 +198,7 @@ fn panic_execution_with_foreign_code_gives_error() { fn bad_extrinsic_with_native_equivalent_code_gives_error() { let mut t = new_test_ext(compact_code_unwrap(), false); t.insert( - >::hashed_key_for(alice()), + >::hashed_key_for(alice()), (0u32, 0u32, 0u32, 69u128, 0u128, 0u128, 0u128).encode() ); t.insert(>::hashed_key().to_vec(), 69_u128.encode()); @@ -227,14 +227,14 @@ fn bad_extrinsic_with_native_equivalent_code_gives_error() { fn successful_execution_with_native_equivalent_code_gives_ok() { let mut t = new_test_ext(compact_code_unwrap(), false); t.insert( - >::hashed_key_for(alice()), + >::hashed_key_for(alice()), AccountInfo::<::Index, _> { data: (111 * DOLLARS, 0u128, 0u128, 0u128), .. Default::default() }.encode(), ); t.insert( - >::hashed_key_for(bob()), + >::hashed_key_for(bob()), AccountInfo::<::Index, _> { data: (0 * DOLLARS, 0u128, 0u128, 0u128), .. Default::default() @@ -276,14 +276,14 @@ fn successful_execution_with_native_equivalent_code_gives_ok() { fn successful_execution_with_foreign_code_gives_ok() { let mut t = new_test_ext(bloaty_code_unwrap(), false); t.insert( - >::hashed_key_for(alice()), + >::hashed_key_for(alice()), AccountInfo::<::Index, _> { data: (111 * DOLLARS, 0u128, 0u128, 0u128), .. Default::default() }.encode(), ); t.insert( - >::hashed_key_for(bob()), + >::hashed_key_for(bob()), AccountInfo::<::Index, _> { data: (0 * DOLLARS, 0u128, 0u128, 0u128), .. Default::default() @@ -716,7 +716,7 @@ fn native_big_block_import_fails_on_fallback() { fn panic_execution_gives_error() { let mut t = new_test_ext(bloaty_code_unwrap(), false); t.insert( - >::hashed_key_for(alice()), + >::hashed_key_for(alice()), AccountInfo::<::Index, _> { data: (0 * DOLLARS, 0u128, 0u128, 0u128), .. Default::default() @@ -748,14 +748,14 @@ fn panic_execution_gives_error() { fn successful_execution_gives_ok() { let mut t = new_test_ext(compact_code_unwrap(), false); t.insert( - >::hashed_key_for(alice()), + >::hashed_key_for(alice()), AccountInfo::<::Index, _> { data: (111 * DOLLARS, 0u128, 0u128, 0u128), .. Default::default() }.encode(), ); t.insert( - >::hashed_key_for(bob()), + >::hashed_key_for(bob()), AccountInfo::<::Index, _> { data: (0 * DOLLARS, 0u128, 0u128, 0u128), .. Default::default() From 1e2185ad50803515caed6676671647743a95b0ae Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Tue, 9 Mar 2021 01:14:29 -0400 Subject: [PATCH 27/31] more test fixes --- Cargo.lock | 2 ++ bin/node/executor/tests/fees.rs | 4 ++-- frame/example/Cargo.toml | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e49cd247b3f5e..4ee2692122806 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,7 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + [[package]] name = "Inflector" version = "0.11.4" diff --git a/bin/node/executor/tests/fees.rs b/bin/node/executor/tests/fees.rs index 74bfc1fe459b5..e034539ec8451 100644 --- a/bin/node/executor/tests/fees.rs +++ b/bin/node/executor/tests/fees.rs @@ -144,8 +144,8 @@ fn transaction_fee_is_correct() { // - 1 milli-dot based on current polkadot runtime. // (this baed on assigning 0.1 CENT to the cheapest tx with `weight = 100`) let mut t = new_test_ext(compact_code_unwrap(), false); - t.insert(>::hashed_key_for(alice()), new_account_info(100)); - t.insert(>::hashed_key_for(bob()), new_account_info(10)); + t.insert(>::hashed_key_for(alice()), new_account_info(100)); + t.insert(>::hashed_key_for(bob()), new_account_info(10)); t.insert( >::hashed_key().to_vec(), (110 * DOLLARS).encode() diff --git a/frame/example/Cargo.toml b/frame/example/Cargo.toml index d2af9f42b86bf..298ac3a967ef6 100644 --- a/frame/example/Cargo.toml +++ b/frame/example/Cargo.toml @@ -25,8 +25,8 @@ sp-io = { version = "3.0.0", default-features = false, path = "../../primitives/ frame-benchmarking = { version = "3.1.0", default-features = false, path = "../benchmarking", optional = true } [dev-dependencies] -sp-core = { version = "3.0.0", path = "../../primitives/core" } -pallet-accounts = { version = "3.0.0", path = "../accounts" } +sp-core = { version = "3.0.0", default-features = false, path = "../../primitives/core" } +pallet-accounts = { version = "3.0.0", default-features = false, path = "../accounts" } [features] default = ["std"] From b9fdb7e4f7229d7193e67b9e9b0730a95ab7cf7d Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Tue, 9 Mar 2021 01:26:54 -0400 Subject: [PATCH 28/31] move trait impls out of mod pallet --- frame/accounts/src/lib.rs | 388 +++++++++++++++++++------------------- 1 file changed, 193 insertions(+), 195 deletions(-) diff --git a/frame/accounts/src/lib.rs b/frame/accounts/src/lib.rs index 2c19de07903ab..c155b878a4394 100644 --- a/frame/accounts/src/lib.rs +++ b/frame/accounts/src/lib.rs @@ -137,214 +137,21 @@ pub mod pallet { #[pallet::call] impl Pallet {} - impl BasicAccount for Pallet { - type AccountInfo = AccountInfo::AccountData>; - - /// Return whether an account exists in storage. - fn account_exists(who: &T::AccountId) -> bool { - Account::::contains_key(who) - } - - /// Return the data for an account - fn get(who: &T::AccountId) -> Self::AccountInfo { - Account::::get(who) - } - - /// Retrieve the account transaction counter from storage. - fn account_nonce(who: &T::AccountId) -> T::Index { - Account::::get(who).nonce - } - - /// Increment a particular account's nonce by 1. - fn inc_account_nonce(who: &T::AccountId) { - Account::::mutate(who, |a| a.nonce += T::Index::one()); - } - - /// Return the storage key for an account. - fn hashed_key_for(who: &T::AccountId) -> Vec { - Account::::hashed_key_for(who) - } - } - impl Pallet { /// An account is being created. - pub fn on_created_account(who: T::AccountId, _a: &mut AccountInfo) { + pub(crate) fn on_created_account(who: T::AccountId, _a: &mut AccountInfo) { T::OnNewAccount::on_new_account(&who); Self::deposit_event(Event::NewAccount(who)); } /// Do anything that needs to be done after an account has been killed. - fn on_killed_account(who: T::AccountId) { + pub(crate) fn on_killed_account(who: T::AccountId) { T::OnKilledAccount::on_killed_account(&who); Self::deposit_event(Event::KilledAccount(who)); } } - - impl ReferencedAccount for Pallet { - type RefCount = RefCount; - type IncRefStatus = IncRefStatus; - type DecRefStatus = DecRefStatus; - type IncRefError = IncRefError; - type DecRefError = DecRefError; - - /// Increment the provider reference counter on an account. - fn inc_providers(who: &T::AccountId) -> IncRefStatus { - Account::::mutate(who, |a| if a.providers == 0 && a.sufficients == 0 { - // Account is being created. - a.providers = 1; - Self::on_created_account(who.clone(), a); - IncRefStatus::Created - } else { - a.providers = a.providers.saturating_add(1); - IncRefStatus::Existed - }) - } - - /// Decrement the provider reference counter on an account. - /// - /// This *MUST* only be done once for every time you called `inc_providers` on `who`. - fn dec_providers(who: &T::AccountId) -> Result { - Account::::try_mutate_exists(who, |maybe_account| { - if let Some(mut account) = maybe_account.take() { - if account.providers == 0 { - // Logic error - cannot decrement beyond zero. - log::error!( - target: "runtime::system", - "Logic error: Unexpected underflow in reducing provider", - ); - account.providers = 1; - } - match (account.providers, account.consumers, account.sufficients) { - (1, 0, 0) => { - // No providers left (and no consumers) and no sufficients. Account dead. - - Module::::on_killed_account(who.clone()); - Ok(DecRefStatus::Reaped) - } - (1, c, _) if c > 0 => { - // Cannot remove last provider if there are consumers. - Err(DecRefError::ConsumerRemaining) - } - (x, _, _) => { - // Account will continue to exist as there is either > 1 provider or - // > 0 sufficients. - account.providers = x - 1; - *maybe_account = Some(account); - Ok(DecRefStatus::Exists) - } - } - } else { - log::error!( - target: "runtime::system", - "Logic error: Account already dead when reducing provider", - ); - Ok(DecRefStatus::Reaped) - } - }) - } - - /// Increment the self-sufficient reference counter on an account. - fn inc_sufficients(who: &T::AccountId) -> IncRefStatus { - Account::::mutate(who, |a| if a.providers + a.sufficients == 0 { - // Account is being created. - a.sufficients = 1; - Self::on_created_account(who.clone(), a); - IncRefStatus::Created - } else { - a.sufficients = a.sufficients.saturating_add(1); - IncRefStatus::Existed - }) - } - - /// Decrement the self-sufficient reference counter on an account. - /// - /// This *MUST* only be done once for every time you called `inc_sufficients` on `who`. - fn dec_sufficients(who: &T::AccountId) -> DecRefStatus { - Account::::mutate_exists(who, |maybe_account| { - if let Some(mut account) = maybe_account.take() { - if account.sufficients == 0 { - // Logic error - cannot decrement beyond zero. - log::error!( - target: "runtime::system", - "Logic error: Unexpected underflow in reducing sufficients", - ); - } - match (account.sufficients, account.providers) { - (0, 0) | (1, 0) => { - Module::::on_killed_account(who.clone()); - DecRefStatus::Reaped - } - (x, _) => { - account.sufficients = x - 1; - *maybe_account = Some(account); - DecRefStatus::Exists - } - } - } else { - log::error!( - target: "runtime::system", - "Logic error: Account already dead when reducing provider", - ); - DecRefStatus::Reaped - } - }) - } - - /// The number of outstanding provider references for the account `who`. - fn providers(who: &T::AccountId) -> RefCount { - Account::::get(who).providers - } - - /// The number of outstanding sufficient references for the account `who`. - fn sufficients(who: &T::AccountId) -> RefCount { - Account::::get(who).sufficients - } - - /// The number of outstanding provider and sufficient references for the account `who`. - fn reference_count(who: &T::AccountId) -> RefCount { - let a = Account::::get(who); - a.providers + a.sufficients - } - - /// Increment the reference counter on an account. - /// - /// The account `who`'s `providers` must be non-zero or this will return an error. - fn inc_consumers(who: &T::AccountId) -> Result<(), IncRefError> { - Account::::try_mutate(who, |a| if a.providers > 0 { - a.consumers = a.consumers.saturating_add(1); - Ok(()) - } else { - Err(IncRefError::NoProviders) - }) - } - - /// Decrement the reference counter on an account. This *MUST* only be done once for every time - /// you called `inc_consumers` on `who`. - fn dec_consumers(who: &T::AccountId) { - Account::::mutate(who, |a| if a.consumers > 0 { - a.consumers -= 1; - } else { - log::error!( - target: "runtime::system", - "Logic error: Unexpected underflow in reducing consumer", - ); - }) - } - - /// The number of outstanding references for the account `who`. - fn consumers(who: &T::AccountId) -> RefCount { - Account::::get(who).consumers - } - - /// True if the account has some outstanding references. - fn is_provider_required(who: &T::AccountId) -> bool { - Account::::get(who).consumers != 0 - } - - } } - /// Event handler which registers a provider when created. pub struct Provider(PhantomData); impl HandleLifetime for Provider { @@ -429,3 +236,194 @@ impl StoredMap::AccountData> for Pallet(d: &T) -> bool { d != &T::default() } + +impl BasicAccount for Pallet { + type AccountInfo = AccountInfo::AccountData>; + + /// Return whether an account exists in storage. + fn account_exists(who: &T::AccountId) -> bool { + Account::::contains_key(who) + } + + /// Return the data for an account + fn get(who: &T::AccountId) -> Self::AccountInfo { + Account::::get(who) + } + + /// Retrieve the account transaction counter from storage. + fn account_nonce(who: &T::AccountId) -> T::Index { + Account::::get(who).nonce + } + + /// Increment a particular account's nonce by 1. + fn inc_account_nonce(who: &T::AccountId) { + Account::::mutate(who, |a| a.nonce += T::Index::one()); + } + + /// Return the storage key for an account. + fn hashed_key_for(who: &T::AccountId) -> Vec { + Account::::hashed_key_for(who) + } +} + +impl ReferencedAccount for Pallet { + type RefCount = RefCount; + type IncRefStatus = IncRefStatus; + type DecRefStatus = DecRefStatus; + type IncRefError = IncRefError; + type DecRefError = DecRefError; + + /// Increment the provider reference counter on an account. + fn inc_providers(who: &T::AccountId) -> IncRefStatus { + Account::::mutate(who, |a| if a.providers == 0 && a.sufficients == 0 { + // Account is being created. + a.providers = 1; + Self::on_created_account(who.clone(), a); + IncRefStatus::Created + } else { + a.providers = a.providers.saturating_add(1); + IncRefStatus::Existed + }) + } + + /// Decrement the provider reference counter on an account. + /// + /// This *MUST* only be done once for every time you called `inc_providers` on `who`. + fn dec_providers(who: &T::AccountId) -> Result { + Account::::try_mutate_exists(who, |maybe_account| { + if let Some(mut account) = maybe_account.take() { + if account.providers == 0 { + // Logic error - cannot decrement beyond zero. + log::error!( + target: "runtime::system", + "Logic error: Unexpected underflow in reducing provider", + ); + account.providers = 1; + } + match (account.providers, account.consumers, account.sufficients) { + (1, 0, 0) => { + // No providers left (and no consumers) and no sufficients. Account dead. + + Module::::on_killed_account(who.clone()); + Ok(DecRefStatus::Reaped) + } + (1, c, _) if c > 0 => { + // Cannot remove last provider if there are consumers. + Err(DecRefError::ConsumerRemaining) + } + (x, _, _) => { + // Account will continue to exist as there is either > 1 provider or + // > 0 sufficients. + account.providers = x - 1; + *maybe_account = Some(account); + Ok(DecRefStatus::Exists) + } + } + } else { + log::error!( + target: "runtime::system", + "Logic error: Account already dead when reducing provider", + ); + Ok(DecRefStatus::Reaped) + } + }) + } + + /// Increment the self-sufficient reference counter on an account. + fn inc_sufficients(who: &T::AccountId) -> IncRefStatus { + Account::::mutate(who, |a| if a.providers + a.sufficients == 0 { + // Account is being created. + a.sufficients = 1; + Self::on_created_account(who.clone(), a); + IncRefStatus::Created + } else { + a.sufficients = a.sufficients.saturating_add(1); + IncRefStatus::Existed + }) + } + + /// Decrement the self-sufficient reference counter on an account. + /// + /// This *MUST* only be done once for every time you called `inc_sufficients` on `who`. + fn dec_sufficients(who: &T::AccountId) -> DecRefStatus { + Account::::mutate_exists(who, |maybe_account| { + if let Some(mut account) = maybe_account.take() { + if account.sufficients == 0 { + // Logic error - cannot decrement beyond zero. + log::error!( + target: "runtime::system", + "Logic error: Unexpected underflow in reducing sufficients", + ); + } + match (account.sufficients, account.providers) { + (0, 0) | (1, 0) => { + Module::::on_killed_account(who.clone()); + DecRefStatus::Reaped + } + (x, _) => { + account.sufficients = x - 1; + *maybe_account = Some(account); + DecRefStatus::Exists + } + } + } else { + log::error!( + target: "runtime::system", + "Logic error: Account already dead when reducing provider", + ); + DecRefStatus::Reaped + } + }) + } + + /// The number of outstanding provider references for the account `who`. + fn providers(who: &T::AccountId) -> RefCount { + Account::::get(who).providers + } + + /// The number of outstanding sufficient references for the account `who`. + fn sufficients(who: &T::AccountId) -> RefCount { + Account::::get(who).sufficients + } + + /// The number of outstanding provider and sufficient references for the account `who`. + fn reference_count(who: &T::AccountId) -> RefCount { + let a = Account::::get(who); + a.providers + a.sufficients + } + + /// Increment the reference counter on an account. + /// + /// The account `who`'s `providers` must be non-zero or this will return an error. + fn inc_consumers(who: &T::AccountId) -> Result<(), IncRefError> { + Account::::try_mutate(who, |a| if a.providers > 0 { + a.consumers = a.consumers.saturating_add(1); + Ok(()) + } else { + Err(IncRefError::NoProviders) + }) + } + + /// Decrement the reference counter on an account. This *MUST* only be done once for every time + /// you called `inc_consumers` on `who`. + fn dec_consumers(who: &T::AccountId) { + Account::::mutate(who, |a| if a.consumers > 0 { + a.consumers -= 1; + } else { + log::error!( + target: "runtime::system", + "Logic error: Unexpected underflow in reducing consumer", + ); + }) + } + + /// The number of outstanding references for the account `who`. + fn consumers(who: &T::AccountId) -> RefCount { + Account::::get(who).consumers + } + + /// True if the account has some outstanding references. + fn is_provider_required(who: &T::AccountId) -> bool { + Account::::get(who).consumers != 0 + } +} From 89303fd84769a1feb7996bc58de30fde3980105d Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Tue, 9 Mar 2021 13:39:41 -0400 Subject: [PATCH 29/31] merge patches --- frame/assets/src/mock.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/frame/assets/src/mock.rs b/frame/assets/src/mock.rs index 434a7ccce0757..9f4f4edab3884 100644 --- a/frame/assets/src/mock.rs +++ b/frame/assets/src/mock.rs @@ -34,6 +34,7 @@ construct_runtime!( UncheckedExtrinsic = UncheckedExtrinsic, { System: frame_system::{Module, Call, Config, Storage, Event}, + Accounts: pallet_accounts::{Module, Call, Storage, Event}, Balances: pallet_balances::{Module, Call, Storage, Config, Event}, Assets: pallet_assets::{Module, Call, Storage, Event}, } @@ -60,11 +61,16 @@ impl frame_system::Config for Test { type DbWeight = (); type Version = (); type PalletInfo = PalletInfo; - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); type SystemWeightInfo = (); type SS58Prefix = (); + type AccountStorage = Accounts; +} + +impl pallet_accounts::Config for Test { + type Event = Event; + type OnKilledAccount = (); + type OnNewAccount = (); + type AccountData = pallet_balances::AccountData; } parameter_types! { @@ -76,7 +82,8 @@ impl pallet_balances::Config for Test { type DustRemoval = (); type Event = Event; type ExistentialDeposit = ExistentialDeposit; - type AccountStore = System; + type AccountStore = Accounts; + type ReferencedAccount = Accounts; type WeightInfo = (); type MaxLocks = (); } @@ -100,6 +107,7 @@ impl Config for Test { type MetadataDepositPerByte = MetadataDepositPerByte; type ApprovalDeposit = ApprovalDeposit; type StringLimit = StringLimit; + type ReferencedAccount = Accounts; type WeightInfo = (); } From add54ec1d596bde6daca3d60032f0f167bf79af0 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Tue, 9 Mar 2021 13:47:29 -0400 Subject: [PATCH 30/31] fix contract tests --- frame/contracts/src/tests.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/frame/contracts/src/tests.rs b/frame/contracts/src/tests.rs index c46346dc0da1e..37df766c636f1 100644 --- a/frame/contracts/src/tests.rs +++ b/frame/contracts/src/tests.rs @@ -479,7 +479,7 @@ fn instantiate_and_call_and_deposit_event() { assert_eq!(System::events(), vec![ EventRecord { phase: Phase::Initialization, - event: Event::frame_system(frame_system::Event::NewAccount(ALICE.clone())), + event: Event::pallet_accounts(pallet_accounts::Event::NewAccount(ALICE.clone())), topics: vec![], }, EventRecord { @@ -491,7 +491,7 @@ fn instantiate_and_call_and_deposit_event() { }, EventRecord { phase: Phase::Initialization, - event: Event::frame_system(frame_system::Event::NewAccount(addr.clone())), + event: Event::pallet_accounts(pallet_accounts::Event::NewAccount(addr.clone())), topics: vec![], }, EventRecord { @@ -1208,7 +1208,7 @@ fn restoration( let mut events = vec![ EventRecord { phase: Phase::Initialization, - event: Event::frame_system(frame_system::Event::NewAccount(ALICE)), + event: Event::pallet_accounts(pallet_accounts::Event::NewAccount(ALICE)), topics: vec![], }, EventRecord { @@ -1220,7 +1220,7 @@ fn restoration( }, EventRecord { phase: Phase::Initialization, - event: Event::frame_system(frame_system::Event::NewAccount(addr_bob.clone())), + event: Event::pallet_accounts(pallet_accounts::Event::NewAccount(addr_bob.clone())), topics: vec![], }, EventRecord { @@ -1269,7 +1269,7 @@ fn restoration( events.extend([ EventRecord { phase: Phase::Initialization, - event: Event::frame_system(frame_system::Event::NewAccount(addr_dummy.clone())), + event: Event::pallet_accounts(pallet_accounts::Event::NewAccount(addr_dummy.clone())), topics: vec![], }, EventRecord { @@ -1421,7 +1421,7 @@ fn restoration( }, EventRecord { phase: Phase::Initialization, - event: Event::frame_system(frame_system::Event::NewAccount(CHARLIE)), + event: Event::pallet_accounts(pallet_accounts::Event::NewAccount(CHARLIE)), topics: vec![], }, EventRecord { @@ -1431,7 +1431,7 @@ fn restoration( }, EventRecord { phase: Phase::Initialization, - event: Event::frame_system(frame_system::Event::NewAccount(addr_django.clone())), + event: Event::pallet_accounts(pallet_accounts::Event::NewAccount(addr_django.clone())), topics: vec![], }, EventRecord { @@ -1494,7 +1494,7 @@ fn restoration( }, EventRecord { phase: Phase::Initialization, - event: Event::frame_system(system::Event::KilledAccount(addr_django.clone())), + event: Event::pallet_accounts(pallet_accounts::Event::KilledAccount(addr_django.clone())), topics: vec![], }, EventRecord { @@ -1727,8 +1727,8 @@ fn self_destruct_works() { pretty_assertions::assert_eq!(System::events(), vec![ EventRecord { phase: Phase::Initialization, - event: Event::frame_system( - frame_system::Event::KilledAccount(addr.clone()) + event: Event::pallet_accounts( + pallet_accounts::Event::KilledAccount(addr.clone()) ), topics: vec![], }, From 031a2b7e50ce2cd47952a7558017ab61d1513748 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Wed, 10 Mar 2021 23:37:11 -0400 Subject: [PATCH 31/31] more fixes --- frame/assets/src/benchmarking.rs | 10 +++++++--- frame/balances/src/tests.rs | 12 ++++++------ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/frame/assets/src/benchmarking.rs b/frame/assets/src/benchmarking.rs index 42f876ff7f3de..13bd185813e76 100644 --- a/frame/assets/src/benchmarking.rs +++ b/frame/assets/src/benchmarking.rs @@ -26,8 +26,12 @@ use frame_system::RawOrigin as SystemOrigin; use frame_benchmarking::{ benchmarks, account, whitelisted_caller, whitelist_account, impl_benchmark_test_suite }; -use frame_support::traits::Get; -use frame_support::{traits::EnsureOrigin, dispatch::UnfilteredDispatchable}; +use frame_support::{ + traits::{ + Get, EnsureOrigin, BasicAccount, + }, + dispatch::UnfilteredDispatchable, +}; use crate::Module as Assets; @@ -193,7 +197,7 @@ benchmarks! { let target_lookup = T::Lookup::unlookup(target.clone()); }: _(SystemOrigin::Signed(caller.clone()), Default::default(), target_lookup, amount) verify { - assert!(frame_system::Module::::account_exists(&caller)); + assert!(T::ReferencedAccount::account_exists(&caller)); assert_last_event::(Event::Transferred(Default::default(), caller, target, amount).into()); } diff --git a/frame/balances/src/tests.rs b/frame/balances/src/tests.rs index c6803233edca4..ab19e71e5e3bc 100644 --- a/frame/balances/src/tests.rs +++ b/frame/balances/src/tests.rs @@ -788,14 +788,14 @@ macro_rules! decl_tests { // Slashed completed in full assert_eq!(Balances::slash(&1, 950), (NegativeImbalance::new(950), 0)); // Account is killed - assert!(!System::account_exists(&1)); + assert!(!Accounts::account_exists(&1)); // SCENARIO: Over-slash will kill account, and report missing slash amount. assert_ok!(Balances::set_balance(Origin::root(), 1, 1_000, 0)); // Slashed full free_balance, and reports 300 not slashed assert_eq!(Balances::slash(&1, 1_300), (NegativeImbalance::new(1000), 300)); // Account is dead - assert!(!System::account_exists(&1)); + assert!(!Accounts::account_exists(&1)); // SCENARIO: Over-slash can take from reserved, but keep alive. assert_ok!(Balances::set_balance(Origin::root(), 1, 1_000, 400)); @@ -809,14 +809,14 @@ macro_rules! decl_tests { // Slashed full free_balance and 300 of reserved balance assert_eq!(Balances::slash(&1, 1_300), (NegativeImbalance::new(1300), 0)); // Account is dead because 50 reserved balance is not enough to keep alive - assert!(!System::account_exists(&1)); + assert!(!Accounts::account_exists(&1)); // SCENARIO: Over-slash can take as much as possible from reserved, kill, and report missing amount. assert_ok!(Balances::set_balance(Origin::root(), 1, 1_000, 250)); // Slashed full free_balance and 300 of reserved balance assert_eq!(Balances::slash(&1, 1_300), (NegativeImbalance::new(1250), 50)); // Account is super dead - assert!(!System::account_exists(&1)); + assert!(!Accounts::account_exists(&1)); /* User will now have a reference counter on them, keeping them alive in these scenarios */ @@ -888,14 +888,14 @@ macro_rules! decl_tests { // Slashed completed in full assert_eq!(Balances::slash_reserved(&1, 1_000), (NegativeImbalance::new(1_000), 0)); // Account is dead - assert!(!System::account_exists(&1)); + assert!(!Accounts::account_exists(&1)); // SCENARIO: Over-slash would kill account, and reports left over slash. assert_ok!(Balances::set_balance(Origin::root(), 1, 50, 1_000)); // Slashed completed in full assert_eq!(Balances::slash_reserved(&1, 1_300), (NegativeImbalance::new(1_000), 300)); // Account is dead - assert!(!System::account_exists(&1)); + assert!(!Accounts::account_exists(&1)); // SCENARIO: Over-slash does not take from free balance. assert_ok!(Balances::set_balance(Origin::root(), 1, 300, 1_000));