Skip to content

Commit

Permalink
Use new AccountInfo struct for "System Account" storage data (parityt…
Browse files Browse the repository at this point in the history
…ech#71)

* Add AccountInfo struct for "System Account" data

* Fmt

* Simplify AccountInfo struct constraints
  • Loading branch information
ascjones authored Feb 25, 2020
1 parent b7565ff commit dcc6b91
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 43 deletions.
19 changes: 9 additions & 10 deletions src/frame/balances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 1 addition & 4 deletions src/frame/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,7 @@ mod tests {

type AccountId = <Runtime as System>::AccountId;

async fn put_code<T, P, S>(
client: &Client<T, S>,
signer: P,
) -> Result<T::Hash, Error>
async fn put_code<T, P, S>(client: &Client<T, S>, signer: P) -> Result<T::Hash, Error>
where
T: System + Balances + Send + Sync,
T::Address: From<T::AccountId>,
Expand Down
62 changes: 41 additions & 21 deletions src/frame/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<T: System> {
/// 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.
Expand All @@ -131,9 +154,7 @@ pub trait SystemStore {
fn account(
&self,
account_id: <Self::System as System>::AccountId,
) -> Pin<
Box<dyn Future<Output = Result<(<Self::System as System>::Index, <Self::System as System>::AccountData), Error>> + Send>,
>;
) -> Pin<Box<dyn Future<Output = Result<AccountInfo<Self::System>, Error>> + Send>>;
}

impl<T: System + Balances + Sync + Send + 'static, S: 'static> SystemStore
Expand All @@ -144,9 +165,8 @@ impl<T: System + Balances + Sync + Send + 'static, S: 'static> SystemStore
fn account(
&self,
account_id: <Self::System as System>::AccountId,
) -> Pin<
Box<dyn Future<Output = Result<(<Self::System as System>::Index, <Self::System as System>::AccountData), Error>> + Send>,
> {
) -> Pin<Box<dyn Future<Output = Result<AccountInfo<Self::System>, Error>> + Send>>
{
let account_map = || {
Ok(self
.metadata
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ impl<T: System + Balances + Sync + Send + 'static, S: 'static> Client<T, S> {
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;
Expand Down Expand Up @@ -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)
});

Expand Down
23 changes: 18 additions & 5 deletions src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,15 @@ where
from: T::Hash,
to: Option<T::Hash>,
) -> Result<Vec<StorageChangeSet<<T as System>::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
Expand Down Expand Up @@ -343,10 +350,16 @@ impl<T: System + Balances + 'static> Rpc<T> {
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!()
Expand Down
5 changes: 4 additions & 1 deletion src/runtimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ use sp_runtime::{
};

use crate::frame::{
balances::{Balances, AccountData},
balances::{
AccountData,
Balances,
},
contracts::Contracts,
system::System,
};
Expand Down

0 comments on commit dcc6b91

Please sign in to comment.