From dcc6b91d106125507c3d696185bd6f398a4c0bfb Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 25 Feb 2020 17:18:10 +0000 Subject: [PATCH] Use new AccountInfo struct for "System Account" storage data (#71) * Add AccountInfo struct for "System Account" data * Fmt * Simplify AccountInfo struct constraints --- src/frame/balances.rs | 19 ++++++------- src/frame/contracts.rs | 5 +--- src/frame/system.rs | 62 ++++++++++++++++++++++++++++-------------- src/lib.rs | 4 +-- src/rpc.rs | 23 ++++++++++++---- src/runtimes.rs | 5 +++- 6 files changed, 75 insertions(+), 43 deletions(-) diff --git a/src/frame/balances.rs b/src/frame/balances.rs index c4a6538cc0963..1b1bb0a7e5011 100644 --- a/src/frame/balances.rs +++ b/src/frame/balances.rs @@ -16,22 +16,21 @@ //! Implements support for the pallet_balances module. -use std::{ - fmt::Debug, +use crate::frame::{ + system::System, + Call, +}; +use codec::{ + Decode, + Encode, }; -use codec::{Encode, Decode}; use frame_support::Parameter; use sp_runtime::traits::{ + AtLeast32Bit, MaybeSerialize, Member, - AtLeast32Bit, -}; -use crate::{ - frame::{ - system::System, - Call, - }, }; +use std::fmt::Debug; /// The subset of the `pallet_balances::Trait` that a client must implement. pub trait Balances: System { diff --git a/src/frame/contracts.rs b/src/frame/contracts.rs index 52d702b14699d..11bedf12abc87 100644 --- a/src/frame/contracts.rs +++ b/src/frame/contracts.rs @@ -160,10 +160,7 @@ mod tests { type AccountId = ::AccountId; - async fn put_code( - client: &Client, - signer: P, - ) -> Result + async fn put_code(client: &Client, signer: P) -> Result where T: System + Balances + Send + Sync, T::Address: From, diff --git a/src/frame/system.rs b/src/frame/system.rs index c1805b62b1750..81486c59491da 100644 --- a/src/frame/system.rs +++ b/src/frame/system.rs @@ -16,26 +16,33 @@ //! Implements support for the frame_system module. -use codec::Codec; +use codec::{ + Codec, + Decode, + Encode, +}; use frame_support::Parameter; use futures::future::{ self, Future, }; use serde::de::DeserializeOwned; -use sp_runtime::traits::{ - Bounded, - CheckEqual, - Extrinsic, - Hash, - Header, - MaybeDisplay, - MaybeMallocSizeOf, - MaybeSerialize, - MaybeSerializeDeserialize, - Member, - AtLeast32Bit, - SimpleBitOps, +use sp_runtime::{ + traits::{ + AtLeast32Bit, + Bounded, + CheckEqual, + Extrinsic, + Hash, + Header, + MaybeDisplay, + MaybeMallocSizeOf, + MaybeSerialize, + MaybeSerializeDeserialize, + Member, + SimpleBitOps, + }, + RuntimeDebug, }; use std::{ fmt::Debug, @@ -118,10 +125,26 @@ pub trait System: 'static + Eq + Clone + Debug { type Extrinsic: Parameter + Member + Extrinsic + Debug + MaybeSerializeDeserialize; /// Data to be associated with an account (other than nonce/transaction counter, which this - /// module does regardless). + /// module does regardless). type AccountData: Member + Codec + Clone + Default; } +/// Type used to encode the number of references an account has. +pub type RefCount = u8; + +/// 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: T::Index, + /// The number of other modules that currently depend on this account's existence. The account + /// cannot be reaped until this is zero. + pub refcount: RefCount, + /// The additional data that belongs to this account. Used to store the balance(s) in a lot of + /// chains. + pub data: T::AccountData, +} + /// The System extension trait for the Client. pub trait SystemStore { /// System type. @@ -131,9 +154,7 @@ pub trait SystemStore { fn account( &self, account_id: ::AccountId, - ) -> Pin< - Box::Index, ::AccountData), Error>> + Send>, - >; + ) -> Pin, Error>> + Send>>; } impl SystemStore @@ -144,9 +165,8 @@ impl SystemStore fn account( &self, account_id: ::AccountId, - ) -> Pin< - Box::Index, ::AccountData), Error>> + Send>, - > { + ) -> Pin, Error>> + Send>> + { let account_map = || { Ok(self .metadata diff --git a/src/lib.rs b/src/lib.rs index 850003fa0fcd1..016ad0c517ead 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -311,7 +311,7 @@ impl Client { let account_id = S::Signer::from(signer.public()).into_account(); let nonce = match nonce { Some(nonce) => nonce, - None => self.account(account_id).await?.0, + None => self.account(account_id).await?.nonce, }; let genesis_hash = self.genesis_hash; @@ -561,7 +561,7 @@ mod tests { let result: Result<_, Error> = async_std::task::block_on(async move { let account = AccountKeyring::Alice.to_account_id(); let client = test_client().await; - let balance = client.account(account.into()).await?.1.free; + let balance = client.account(account.into()).await?.data.free; Ok(balance) }); diff --git a/src/rpc.rs b/src/rpc.rs index cd15587e67dcb..0414177236e22 100644 --- a/src/rpc.rs +++ b/src/rpc.rs @@ -130,8 +130,15 @@ where from: T::Hash, to: Option, ) -> Result::Hash>>, Error> { - let params = Params::Array(vec![to_json_value(keys)?, to_json_value(from)?, to_json_value(to)?]); - self.client.request("state_queryStorage", params).await.map_err(Into::into) + let params = Params::Array(vec![ + to_json_value(keys)?, + to_json_value(from)?, + to_json_value(to)?, + ]); + self.client + .request("state_queryStorage", params) + .await + .map_err(Into::into) } /// Fetch the genesis hash @@ -343,10 +350,16 @@ impl Rpc { TransactionStatus::Invalid => return Err("Extrinsic Invalid".into()), TransactionStatus::Usurped(_) => return Err("Extrinsic Usurped".into()), TransactionStatus::Dropped => return Err("Extrinsic Dropped".into()), - TransactionStatus::Retracted(_) => return Err("Extrinsic Retracted".into()), + TransactionStatus::Retracted(_) => { + return Err("Extrinsic Retracted".into()) + } // should have made it `InBlock` before either of these - TransactionStatus::Finalized(_) => return Err("Extrinsic Finalized".into()), - TransactionStatus::FinalityTimeout(_) => return Err("Extrinsic FinalityTimeout".into()), + TransactionStatus::Finalized(_) => { + return Err("Extrinsic Finalized".into()) + } + TransactionStatus::FinalityTimeout(_) => { + return Err("Extrinsic FinalityTimeout".into()) + } } } unreachable!() diff --git a/src/runtimes.rs b/src/runtimes.rs index 0d4cc45276be9..5b3fb9b339973 100644 --- a/src/runtimes.rs +++ b/src/runtimes.rs @@ -26,7 +26,10 @@ use sp_runtime::{ }; use crate::frame::{ - balances::{Balances, AccountData}, + balances::{ + AccountData, + Balances, + }, contracts::Contracts, system::System, };