Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new AccountInfo struct for "System Account" storage data #71

Merged
merged 3 commits into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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