mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 06:31:09 +00:00
Latest substrate updates (#70)
* Fix compilation errors with latest substrate * System::Account replaces FreeBalance and Nonce * System::Account replaces FreeBalance and Nonce * Remove Balances FreeBalance, replaced with System Account * Update system event, fix instantiate * reorder deps
This commit is contained in:
+3
-3
@@ -50,8 +50,8 @@ use crate::{
|
||||
|
||||
/// Top level Event that can be produced by a substrate runtime
|
||||
#[derive(Debug)]
|
||||
pub enum RuntimeEvent {
|
||||
System(SystemEvent),
|
||||
pub enum RuntimeEvent<T: System> {
|
||||
System(SystemEvent<T>),
|
||||
Raw(RawEvent),
|
||||
}
|
||||
|
||||
@@ -197,7 +197,7 @@ impl<T: System + Balances + 'static> EventsDecoder<T> {
|
||||
pub fn decode_events(
|
||||
&self,
|
||||
input: &mut &[u8],
|
||||
) -> Result<Vec<(Phase, RuntimeEvent)>, EventsError> {
|
||||
) -> Result<Vec<(Phase, RuntimeEvent<T>)>, EventsError> {
|
||||
let compact_len = <Compact<u32>>::decode(input)?;
|
||||
let len = compact_len.0 as usize;
|
||||
|
||||
|
||||
+23
-71
@@ -18,28 +18,19 @@
|
||||
|
||||
use std::{
|
||||
fmt::Debug,
|
||||
pin::Pin,
|
||||
};
|
||||
|
||||
use futures::future::{
|
||||
self,
|
||||
Future,
|
||||
};
|
||||
|
||||
use codec::{Encode, Decode};
|
||||
use frame_support::Parameter;
|
||||
use sp_runtime::traits::{
|
||||
MaybeSerialize,
|
||||
Member,
|
||||
SimpleArithmetic,
|
||||
AtLeast32Bit,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
error::Error,
|
||||
frame::{
|
||||
system::System,
|
||||
Call,
|
||||
},
|
||||
Client,
|
||||
};
|
||||
|
||||
/// The subset of the `pallet_balances::Trait` that a client must implement.
|
||||
@@ -47,7 +38,7 @@ pub trait Balances: System {
|
||||
/// The balance of an account.
|
||||
type Balance: Parameter
|
||||
+ Member
|
||||
+ SimpleArithmetic
|
||||
+ AtLeast32Bit
|
||||
+ codec::Codec
|
||||
+ Default
|
||||
+ Copy
|
||||
@@ -56,67 +47,28 @@ pub trait Balances: System {
|
||||
+ From<<Self as System>::BlockNumber>;
|
||||
}
|
||||
|
||||
/// The Balances extension trait for the Client.
|
||||
pub trait BalancesStore {
|
||||
/// Balances type.
|
||||
type Balances: Balances;
|
||||
|
||||
/// The 'free' balance of a given account.
|
||||
/// All balance information for an account.
|
||||
#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq, Default)]
|
||||
pub struct AccountData<Balance> {
|
||||
/// Non-reserved part of the balance. There may still be restrictions on this, but it is the
|
||||
/// total pool what may in principle be transferred, reserved and used for tipping.
|
||||
///
|
||||
/// This is the only balance that matters in terms of most operations on
|
||||
/// tokens. It alone is used to determine the balance when in the contract
|
||||
/// execution environment. When this balance falls below the value of
|
||||
/// `ExistentialDeposit`, then the 'current account' is deleted:
|
||||
/// specifically `FreeBalance`. Further, the `OnFreeBalanceZero` callback
|
||||
/// is invoked, giving a chance to external modules to clean up data
|
||||
/// associated with the deleted account.
|
||||
/// This is the only balance that matters in terms of most operations on tokens. It
|
||||
/// alone is used to determine the balance when in the contract execution environment.
|
||||
pub free: Balance,
|
||||
/// Balance which is reserved and may not be used at all.
|
||||
///
|
||||
/// `system::AccountNonce` is also deleted if `ReservedBalance` is also
|
||||
/// zero. It also gets collapsed to zero if it ever becomes less than
|
||||
/// `ExistentialDeposit`.
|
||||
fn free_balance(
|
||||
&self,
|
||||
account_id: <Self::Balances as System>::AccountId,
|
||||
) -> Pin<
|
||||
Box<
|
||||
dyn Future<Output = Result<<Self::Balances as Balances>::Balance, Error>>
|
||||
+ Send,
|
||||
>,
|
||||
>;
|
||||
}
|
||||
|
||||
impl<T: Balances + Sync + Send + 'static, S: 'static> BalancesStore for Client<T, S> {
|
||||
type Balances = T;
|
||||
|
||||
fn free_balance(
|
||||
&self,
|
||||
account_id: <Self::Balances as System>::AccountId,
|
||||
) -> Pin<
|
||||
Box<
|
||||
dyn Future<Output = Result<<Self::Balances as Balances>::Balance, Error>>
|
||||
+ Send,
|
||||
>,
|
||||
> {
|
||||
let free_balance_map = || {
|
||||
Ok(self
|
||||
.metadata()
|
||||
.module("Balances")?
|
||||
.storage("FreeBalance")?
|
||||
.get_map::<
|
||||
<Self::Balances as System>::AccountId,
|
||||
<Self::Balances as Balances>::Balance>()?)
|
||||
};
|
||||
let map = match free_balance_map() {
|
||||
Ok(map) => map,
|
||||
Err(err) => return Box::pin(future::err(err)),
|
||||
};
|
||||
let client = self.clone();
|
||||
Box::pin(async move {
|
||||
client
|
||||
.fetch_or(map.key(account_id), None, map.default())
|
||||
.await
|
||||
})
|
||||
}
|
||||
/// This can still get slashed, but gets slashed last of all.
|
||||
///
|
||||
/// This balance is a 'reserve' balance that other subsystems use in order to set aside tokens
|
||||
/// that are still 'owned' by the account holder, but which are suspendable.
|
||||
pub reserved: Balance,
|
||||
/// The amount that `free` may not drop below when withdrawing for *anything except transaction
|
||||
/// fee payment*.
|
||||
pub misc_frozen: Balance,
|
||||
/// The amount that `free` may not drop below when withdrawing specifically for transaction
|
||||
/// fee payment.
|
||||
pub fee_frozen: Balance,
|
||||
}
|
||||
|
||||
const MODULE: &str = "Balances";
|
||||
|
||||
@@ -212,12 +212,12 @@ mod tests {
|
||||
fn tx_instantiate() {
|
||||
env_logger::try_init().ok();
|
||||
let result: Result<_, Error> = async_std::task::block_on(async move {
|
||||
let signer = AccountKeyring::Alice.pair();
|
||||
let signer = AccountKeyring::Bob.pair();
|
||||
let client = test_client().await;
|
||||
|
||||
let code_hash = put_code(&client, signer.clone()).await?;
|
||||
|
||||
println!("{:?}", code_hash);
|
||||
log::info!("Code hash: {:?}", code_hash);
|
||||
|
||||
let xt = client.xt(signer, None).await?;
|
||||
let result = xt
|
||||
@@ -235,9 +235,11 @@ mod tests {
|
||||
Ok(event)
|
||||
});
|
||||
|
||||
log::info!("Instantiate result: {:?}", result);
|
||||
|
||||
assert!(
|
||||
result.is_ok(),
|
||||
"Contracts CodeStored event should be received and decoded"
|
||||
"Contract should be instantiated successfully"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
+25
-12
@@ -30,10 +30,11 @@ use sp_runtime::traits::{
|
||||
Hash,
|
||||
Header,
|
||||
MaybeDisplay,
|
||||
MaybeMallocSizeOf,
|
||||
MaybeSerialize,
|
||||
MaybeSerializeDeserialize,
|
||||
Member,
|
||||
SimpleArithmetic,
|
||||
AtLeast32Bit,
|
||||
SimpleBitOps,
|
||||
};
|
||||
use std::{
|
||||
@@ -60,16 +61,17 @@ pub trait System: 'static + Eq + Clone + Debug {
|
||||
+ Debug
|
||||
+ Default
|
||||
+ MaybeDisplay
|
||||
+ SimpleArithmetic
|
||||
+ AtLeast32Bit
|
||||
+ Copy;
|
||||
|
||||
/// The block number type used by the runtime.
|
||||
type BlockNumber: Parameter
|
||||
+ Member
|
||||
+ MaybeMallocSizeOf
|
||||
+ MaybeSerializeDeserialize
|
||||
+ Debug
|
||||
+ MaybeDisplay
|
||||
+ SimpleArithmetic
|
||||
+ AtLeast32Bit
|
||||
+ Default
|
||||
+ Bounded
|
||||
+ Copy
|
||||
@@ -79,6 +81,7 @@ pub trait System: 'static + Eq + Clone + Debug {
|
||||
/// The output of the `Hashing` function.
|
||||
type Hash: Parameter
|
||||
+ Member
|
||||
+ MaybeMallocSizeOf
|
||||
+ MaybeSerializeDeserialize
|
||||
+ Debug
|
||||
+ MaybeDisplay
|
||||
@@ -113,6 +116,10 @@ pub trait System: 'static + Eq + Clone + Debug {
|
||||
|
||||
/// Extrinsic type within blocks.
|
||||
type Extrinsic: Parameter + Member + Extrinsic + Debug + MaybeSerializeDeserialize;
|
||||
|
||||
/// Data to be associated with an account (other than nonce/transaction counter, which this
|
||||
/// module does regardless).
|
||||
type AccountData: Member + Codec + Clone + Default;
|
||||
}
|
||||
|
||||
/// The System extension trait for the Client.
|
||||
@@ -120,12 +127,12 @@ pub trait SystemStore {
|
||||
/// System type.
|
||||
type System: System;
|
||||
|
||||
/// Returns the account nonce for an account_id.
|
||||
fn account_nonce(
|
||||
/// Returns the nonce and account data for an account_id.
|
||||
fn account(
|
||||
&self,
|
||||
account_id: <Self::System as System>::AccountId,
|
||||
) -> Pin<
|
||||
Box<dyn Future<Output = Result<<Self::System as System>::Index, Error>> + Send>,
|
||||
Box<dyn Future<Output = Result<(<Self::System as System>::Index, <Self::System as System>::AccountData), Error>> + Send>,
|
||||
>;
|
||||
}
|
||||
|
||||
@@ -134,20 +141,20 @@ impl<T: System + Balances + Sync + Send + 'static, S: 'static> SystemStore
|
||||
{
|
||||
type System = T;
|
||||
|
||||
fn account_nonce(
|
||||
fn account(
|
||||
&self,
|
||||
account_id: <Self::System as System>::AccountId,
|
||||
) -> Pin<
|
||||
Box<dyn Future<Output = Result<<Self::System as System>::Index, Error>> + Send>,
|
||||
Box<dyn Future<Output = Result<(<Self::System as System>::Index, <Self::System as System>::AccountData), Error>> + Send>,
|
||||
> {
|
||||
let account_nonce_map = || {
|
||||
let account_map = || {
|
||||
Ok(self
|
||||
.metadata
|
||||
.module("System")?
|
||||
.storage("AccountNonce")?
|
||||
.storage("Account")?
|
||||
.get_map()?)
|
||||
};
|
||||
let map = match account_nonce_map() {
|
||||
let map = match account_map() {
|
||||
Ok(map) => map,
|
||||
Err(err) => return Box::pin(future::err(err)),
|
||||
};
|
||||
@@ -175,11 +182,17 @@ use frame_support::weights::DispatchInfo;
|
||||
|
||||
/// Event for the System module.
|
||||
#[derive(Clone, Debug, codec::Decode)]
|
||||
pub enum SystemEvent {
|
||||
pub enum SystemEvent<T: System> {
|
||||
/// An extrinsic completed successfully.
|
||||
ExtrinsicSuccess(DispatchInfo),
|
||||
/// An extrinsic failed.
|
||||
ExtrinsicFailed(sp_runtime::DispatchError, DispatchInfo),
|
||||
/// `:code` was updated.
|
||||
CodeUpdated,
|
||||
/// A new account was created.
|
||||
NewAccount(T::AccountId),
|
||||
/// An account was reaped.
|
||||
ReapedAccount(T::AccountId),
|
||||
}
|
||||
|
||||
/// A phase of a block's execution.
|
||||
|
||||
+11
-29
@@ -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_nonce(account_id).await?,
|
||||
None => self.account(account_id).await?.0,
|
||||
};
|
||||
|
||||
let genesis_hash = self.genesis_hash;
|
||||
@@ -491,15 +491,11 @@ mod tests {
|
||||
|
||||
use super::*;
|
||||
use crate::{
|
||||
frame::balances::{
|
||||
Balances,
|
||||
BalancesStore,
|
||||
},
|
||||
frame::balances::Balances,
|
||||
DefaultNodeRuntime as Runtime,
|
||||
Error,
|
||||
};
|
||||
|
||||
type Index = <Runtime as System>::Index;
|
||||
type AccountId = <Runtime as System>::AccountId;
|
||||
type Address = <Runtime as System>::Address;
|
||||
type Balance = <Runtime as Balances>::Balance;
|
||||
@@ -565,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.free_balance(account.into()).await?;
|
||||
let balance = client.account(account.into()).await?.1.free;
|
||||
Ok(balance)
|
||||
});
|
||||
|
||||
@@ -614,32 +610,18 @@ mod tests {
|
||||
let call2 = balances.call("transfer", subxt_transfer.args).unwrap();
|
||||
assert_eq!(call.encode().to_vec(), call2.0);
|
||||
|
||||
let free_balance =
|
||||
<pallet_balances::FreeBalance<node_runtime::Runtime>>::hashed_key_for(&dest);
|
||||
let free_balance_key = StorageKey(free_balance);
|
||||
let free_balance_key2 = client
|
||||
.metadata()
|
||||
.module("Balances")
|
||||
.unwrap()
|
||||
.storage("FreeBalance")
|
||||
.unwrap()
|
||||
.get_map::<AccountId, Balance>()
|
||||
.unwrap()
|
||||
.key(dest.clone());
|
||||
assert_eq!(free_balance_key, free_balance_key2);
|
||||
|
||||
let account_nonce =
|
||||
<frame_system::AccountNonce<node_runtime::Runtime>>::hashed_key_for(&dest);
|
||||
let account_nonce_key = StorageKey(account_nonce);
|
||||
let account_nonce_key2 = client
|
||||
let account_key =
|
||||
<frame_system::Account<node_runtime::Runtime>>::hashed_key_for(&dest);
|
||||
let account_key_substrate = StorageKey(account_key);
|
||||
let account_key_from_meta = client
|
||||
.metadata()
|
||||
.module("System")
|
||||
.unwrap()
|
||||
.storage("AccountNonce")
|
||||
.storage("Account")
|
||||
.unwrap()
|
||||
.get_map::<AccountId, Index>()
|
||||
.get_map::<AccountId, pallet_balances::AccountData<Balance>>()
|
||||
.unwrap()
|
||||
.key(dest);
|
||||
assert_eq!(account_nonce_key, account_nonce_key2);
|
||||
.key(dest.clone());
|
||||
assert_eq!(account_key_substrate, account_key_from_meta);
|
||||
}
|
||||
}
|
||||
|
||||
+7
-3
@@ -340,9 +340,13 @@ 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::Invalid => return Err("Extrinsic Invalid".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()),
|
||||
}
|
||||
}
|
||||
unreachable!()
|
||||
@@ -357,7 +361,7 @@ pub struct ExtrinsicSuccess<T: System> {
|
||||
/// Extrinsic hash.
|
||||
pub extrinsic: T::Hash,
|
||||
/// Raw runtime events, can be decoded by the caller.
|
||||
pub events: Vec<RuntimeEvent>,
|
||||
pub events: Vec<RuntimeEvent<T>>,
|
||||
}
|
||||
|
||||
impl<T: System> ExtrinsicSuccess<T> {
|
||||
@@ -377,7 +381,7 @@ impl<T: System> ExtrinsicSuccess<T> {
|
||||
}
|
||||
|
||||
/// Returns all System Events
|
||||
pub fn system_events(&self) -> Vec<&SystemEvent> {
|
||||
pub fn system_events(&self) -> Vec<&SystemEvent<T>> {
|
||||
self.events
|
||||
.iter()
|
||||
.filter_map(|evt| {
|
||||
|
||||
+2
-1
@@ -26,7 +26,7 @@ use sp_runtime::{
|
||||
};
|
||||
|
||||
use crate::frame::{
|
||||
balances::Balances,
|
||||
balances::{Balances, AccountData},
|
||||
contracts::Contracts,
|
||||
system::System,
|
||||
};
|
||||
@@ -49,6 +49,7 @@ impl System for DefaultNodeRuntime {
|
||||
type Address = pallet_indices::address::Address<Self::AccountId, u32>;
|
||||
type Header = Header<Self::BlockNumber, BlakeTwo256>;
|
||||
type Extrinsic = OpaqueExtrinsic;
|
||||
type AccountData = AccountData<<Self as Balances>::Balance>;
|
||||
}
|
||||
|
||||
impl Balances for DefaultNodeRuntime {
|
||||
|
||||
Reference in New Issue
Block a user