From abd5d6850bb253ecd5c5e6dd77bf61777319fddb Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 25 Feb 2020 16:28:28 +0000 Subject: [PATCH 1/3] Add AccountInfo struct for "System Account" data --- src/frame/system.rs | 51 +++++++++++++++++++++++++++++++-------------- src/lib.rs | 4 ++-- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/src/frame/system.rs b/src/frame/system.rs index c1805b62b1..a6e1d39ce6 100644 --- a/src/frame/system.rs +++ b/src/frame/system.rs @@ -16,26 +16,29 @@ //! 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::{ + RuntimeDebug, + traits::{ + Bounded, + CheckEqual, + Extrinsic, + Hash, + Header, + MaybeDisplay, + MaybeMallocSizeOf, + MaybeSerialize, + MaybeSerializeDeserialize, + Member, + AtLeast32Bit, + SimpleBitOps, + } }; use std::{ fmt::Debug, @@ -122,6 +125,22 @@ pub trait System: 'static + Eq + Clone + Debug { 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: 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: AccountData, +} + /// The System extension trait for the Client. pub trait SystemStore { /// System type. @@ -132,7 +151,7 @@ pub trait SystemStore { &self, account_id: ::AccountId, ) -> Pin< - Box::Index, ::AccountData), Error>> + Send>, + Box::Index, ::AccountData>, Error>> + Send>, >; } @@ -145,7 +164,7 @@ impl SystemStore &self, account_id: ::AccountId, ) -> Pin< - Box::Index, ::AccountData), Error>> + Send>, + Box::Index, ::AccountData>, Error>> + Send>, > { let account_map = || { Ok(self diff --git a/src/lib.rs b/src/lib.rs index 850003fa0f..016ad0c517 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) }); From 56e044c71a760a3468b140955ab29f9cf93bce5b Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 25 Feb 2020 16:33:52 +0000 Subject: [PATCH 2/3] Fmt --- src/frame/balances.rs | 19 +++++++++---------- src/frame/contracts.rs | 5 +---- src/frame/system.rs | 38 +++++++++++++++++++++++++++++++------- src/rpc.rs | 23 ++++++++++++++++++----- src/runtimes.rs | 5 ++++- 5 files changed, 63 insertions(+), 27 deletions(-) diff --git a/src/frame/balances.rs b/src/frame/balances.rs index c4a6538cc0..1b1bb0a7e5 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 52d702b146..11bedf12ab 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 a6e1d39ce6..018122d232 100644 --- a/src/frame/system.rs +++ b/src/frame/system.rs @@ -16,7 +16,11 @@ //! Implements support for the frame_system module. -use codec::{Codec, Decode, Encode}; +use codec::{ + Codec, + Decode, + Encode, +}; use frame_support::Parameter; use futures::future::{ self, @@ -24,8 +28,8 @@ use futures::future::{ }; use serde::de::DeserializeOwned; use sp_runtime::{ - RuntimeDebug, traits::{ + AtLeast32Bit, Bounded, CheckEqual, Extrinsic, @@ -36,9 +40,9 @@ use sp_runtime::{ MaybeSerialize, MaybeSerializeDeserialize, Member, - AtLeast32Bit, SimpleBitOps, - } + }, + RuntimeDebug, }; use std::{ fmt::Debug, @@ -121,7 +125,7 @@ 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; } @@ -151,7 +155,17 @@ pub trait SystemStore { &self, account_id: ::AccountId, ) -> Pin< - Box::Index, ::AccountData>, Error>> + Send>, + Box< + dyn Future< + Output = Result< + AccountInfo< + ::Index, + ::AccountData, + >, + Error, + >, + > + Send, + >, >; } @@ -164,7 +178,17 @@ impl SystemStore &self, account_id: ::AccountId, ) -> Pin< - Box::Index, ::AccountData>, Error>> + Send>, + Box< + dyn Future< + Output = Result< + AccountInfo< + ::Index, + ::AccountData, + >, + Error, + >, + > + Send, + >, > { let account_map = || { Ok(self diff --git a/src/rpc.rs b/src/rpc.rs index cd15587e67..0414177236 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 0d4cc45276..5b3fb9b339 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, }; From 04c5ca9b3519038e5c03e61c4c29332bf20a986e Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 25 Feb 2020 16:54:34 +0000 Subject: [PATCH 3/3] Simplify AccountInfo struct constraints --- src/frame/system.rs | 35 ++++++----------------------------- 1 file changed, 6 insertions(+), 29 deletions(-) diff --git a/src/frame/system.rs b/src/frame/system.rs index 018122d232..81486c5949 100644 --- a/src/frame/system.rs +++ b/src/frame/system.rs @@ -134,15 +134,15 @@ pub type RefCount = u8; /// Information of an account. #[derive(Clone, Eq, PartialEq, Default, RuntimeDebug, Encode, Decode)] -pub struct AccountInfo { +pub struct AccountInfo { /// The number of transactions this account has sent. - pub nonce: Index, + 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: AccountData, + pub data: T::AccountData, } /// The System extension trait for the Client. @@ -154,19 +154,7 @@ pub trait SystemStore { fn account( &self, account_id: ::AccountId, - ) -> Pin< - Box< - dyn Future< - Output = Result< - AccountInfo< - ::Index, - ::AccountData, - >, - Error, - >, - > + Send, - >, - >; + ) -> Pin, Error>> + Send>>; } impl SystemStore @@ -177,19 +165,8 @@ impl SystemStore fn account( &self, account_id: ::AccountId, - ) -> Pin< - Box< - dyn Future< - Output = Result< - AccountInfo< - ::Index, - ::AccountData, - >, - Error, - >, - > + Send, - >, - > { + ) -> Pin, Error>> + Send>> + { let account_map = || { Ok(self .metadata