mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 19:21:13 +00:00
Generate runtime API from metadata (#294)
* Remove test macro * Remove client crate * Create tests crate and move pallet specific tests there * Extract client, remove metadata and extra, more demolition * Update substrate dependencies to git dependencies * Remove Store stuff for now * Comment out some Call usages * Add back Runtime trait coped from original System trait * Make subxt lib compile * Delete old proc macros and copy over type generation from chameleon * WIP make transfer balance test pass * Change to subxt attribute macro * WIP provide user defined type substitutes * User defined type substitutes compile * WIP submitting transactions * WIP transfer balance test * Fix macro * Cargo fmt * WIP generating storage hashers * WIP add AccountData trait for fetching the nonce * Support single type storage map keys * WIP impl AccountInfo retrieval * Fix up storage struct generation * Implement AccountData triait directly on storage entry * Borrow storage map key and convert account id * Implement storage fetch client methods * Remove legacy metadata storage key construction * Rename CheckEra to CheckMortality * Substitute perthings types for compact impls * Fmt * Downgrade dyn-clone for cargo-contract compat * Scale-fo 1.0 * scale-info 1.0 * Remove special range handling * Restore wildcard type params * Frame metadata 14.0 * WIP decoding events * WIP more dynamically decoding events * Fmt * Decode events, handle errors * Uncomment some tests * Remove unused get_mod function * Fix some warnings * Fix some more warnings * Fix some more warnings * Add tests mod * Rename node-runtime tests mod to frame * Fix some warnings * Fmt * WIP generate storage client with getters * Storage client compiling * Generate storage client api * Fix up system account query account ids * WIP generating tx api fns * Only generate tx api fields when calls available * Fix tx api call fns * Fmt * WIP generate event structs * call functions not async * Derive Eq for comparison on generated types * Generate event structs * Fix call name * Fmt * Update node runtime metadata to substrate c000780db * Download latest substrate release for integration testing * Fix event decoding * Remove unused imports * Fix plain storage access, total_issuance pass * Fmt * Restore contracts tests * Backoff connecting to substrate node * Add required TypeInfo impls for local SignedExtension impls * Remove unnecessary assert formatting * Fix handling of DispatchError * Refactor contracts tests * Troubleshooting contract not found * Remove more client feature stuff * Fix dynamic event variant decoding, write consumed index to output * Fmt * Use substrate branch with heavy dependency removed * Remove sp-rcp dependency, define types locally * Ignore cargo timeing files * Use my branch for substrate test deps * Fix storage key type gen * Comment out fetching contract info * Add key iteration, extract storage client from main client * Debugging key generation * Use substrate master branch * Fix call test * Remove TypeSegmenter and dynclone dependency * Publicly expose Rpc mod * Unused import warnings * Add getter for runtime metadata * Add pallet and event indices for raw events * Add is_call and is_event convenience trait functions * Add missing docs * Refactor tests crate * Restore remaining client tests * Fmt * Fix warnings * Restore get_mod as test helper and fmt * Use client references for api calls * Fix api usages with methods * Use Bytes for RawEvent debug * Update metadata * Restoring some Balances tests * Populate runtime storage metadata * Restore balances lock test * Restore Balances error test * Fmt * Restore transfer subscription API * Staking test * Restore another staking test * Restore another staking test * Restore another staking test * Partially restore chill_works_for_controller_only staking test * Fix fetching Optional storage entries * Restore staking bond test * Restore remaining staking tests * Fmt * Restore sudo tests * Add some system tests * Fmt * Resolve some todos * Remove pass through rpc methods on Client, expose via rpc() getter * Remove more rpc pass through methods * Remove submit tx pass through rpc methods * Add some comments to SubmittableExtrinsic methods * Construct the runtime api from the client * Fmt * Use From trait instead of new for AccountData query * Rename subxt_proc_macro crate to subxt_macro * Fix AccountData From impl * Extract codegen crate from macro crate * Fmt * Replace chameleon hidden field name * Extract StructDef for generating structs * More refactoring of StructDef, moving towards sharing with typegen * Replace explicit tests crate with single implicit integration tests crate * Rename from substrate-subxt to subxt * Fix runtime path relative to root Cargo.toml * Move RpcClient creation to RpcClient * WIP get examples to compile * Rename Runtime to Config trait * WIP implementing default Config * WIP implementing default extrinsic extras * fix metadata constants (#299) * Move DefaultConfig definition and impl to macro * Extract type substitute parsing to ir mod * Extract calls, events and storage from api generation * Add some hardcoded type substitute defaults * Fmt * Add utility pallet tests (#300) * add batch call test example * add pallet utility tests * add utility module * fix warnings * Add polkadot runtime metadata for example * Fix system errors and fmt * Add subxt-cli crate * Add metadata and codegen subcommands * Make subxt-cli codegen command work * Fmt * Add polkadot codegen test * Comment about how to run codegen * Derive AsCompact for structs with single concrete unsigned int field * Fix bitvec codegen, adds as non optional dependency * Regenerate polkadot api with bitvec fix * Edition 2021 * Fix polkadot codegen with bitvec * Polkadot balance transfer is working * Fix fetch remote * Fix transfer_subscribe example * Fix submit_and_watch example * Fmt * Generate storage iter method for iterating over keys * Fmt * Fix existential deposit test * Fix staking tests * Add option for custom generated type derives * Add generated type derives for test runtime api * Fmt * Copy WrapperTypeOpaque from substrate, add Encode/Decode * Fmt * Extract type generator to module, separate & fix tests * Fully qualified primitive and prelude types * Fix up remaining type gen tests * Skip formatting of generated polkadot example code * Remove empty utility test file. * Newline * Update cli/src/main.rs Co-authored-by: David <dvdplm@gmail.com> * Rename subxt-cli executable to subxt * Update src/client.rs Co-authored-by: David <dvdplm@gmail.com> * Add some code docs to TypeGenerator. * Extract TypePath to own file * Extract type def generation to separate file * Renamed ModuleType to TypeDefGen * Fmt * Factor out type parameter from final_key * Fix some type paths * Resolve some todos * Resolve some panic todos in events * Add EventsDecodingError * Decode compact composite types with a single primitive field * Decode compact composite types with a single primitive field * Update src/metadata.rs Co-authored-by: Andrew Plaza <aplaza@liquidthink.net> * Remove Perbill compact substitute types * Remove todos regarding maintaining Rust code items, promoted to follow up issue. * Remove todo regarding overridding default config impl * Remove todo regarding overridding default Extra * Remove todo regarding AccountData storage type defintion * Remove todo regarding borrowing storage key arguments * Remove type substitution tests todo * Remove `Box` field name type hack todo * Remove Compact todo * Remove sudo todos * Remove BitVec implementation todo * Fmt * Add health warning to README * Fix up health warning Co-authored-by: Paulo Martins <paulormart@users.noreply.github.com> Co-authored-by: David <dvdplm@gmail.com> Co-authored-by: Andrew Plaza <aplaza@liquidthink.net>
This commit is contained in:
+280
@@ -0,0 +1,280 @@
|
||||
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
|
||||
// This file is part of subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// subxt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use futures::future;
|
||||
pub use sp_runtime::traits::SignedExtension;
|
||||
pub use sp_version::RuntimeVersion;
|
||||
|
||||
use crate::{
|
||||
events::EventsDecoder,
|
||||
extrinsic::{
|
||||
self,
|
||||
SignedExtra,
|
||||
Signer,
|
||||
UncheckedExtrinsic,
|
||||
},
|
||||
rpc::{
|
||||
ExtrinsicSuccess,
|
||||
Rpc,
|
||||
RpcClient,
|
||||
SystemProperties,
|
||||
},
|
||||
storage::StorageClient,
|
||||
AccountData,
|
||||
Call,
|
||||
Config,
|
||||
Error,
|
||||
ExtrinsicExtraData,
|
||||
Metadata,
|
||||
};
|
||||
|
||||
/// ClientBuilder for constructing a Client.
|
||||
#[derive(Default)]
|
||||
pub struct ClientBuilder {
|
||||
url: Option<String>,
|
||||
client: Option<RpcClient>,
|
||||
page_size: Option<u32>,
|
||||
accept_weak_inclusion: bool,
|
||||
}
|
||||
|
||||
impl ClientBuilder {
|
||||
/// Creates a new ClientBuilder.
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
url: None,
|
||||
client: None,
|
||||
page_size: None,
|
||||
accept_weak_inclusion: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets the jsonrpsee client.
|
||||
pub fn set_client<C: Into<RpcClient>>(mut self, client: C) -> Self {
|
||||
self.client = Some(client.into());
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the substrate rpc address.
|
||||
pub fn set_url<P: Into<String>>(mut self, url: P) -> Self {
|
||||
self.url = Some(url.into());
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the page size.
|
||||
pub fn set_page_size(mut self, size: u32) -> Self {
|
||||
self.page_size = Some(size);
|
||||
self
|
||||
}
|
||||
|
||||
/// Only check that transactions are InBlock on submit.
|
||||
pub fn accept_weak_inclusion(mut self) -> Self {
|
||||
self.accept_weak_inclusion = true;
|
||||
self
|
||||
}
|
||||
|
||||
/// Creates a new Client.
|
||||
pub async fn build<T: Config>(self) -> Result<Client<T>, Error> {
|
||||
let client = if let Some(client) = self.client {
|
||||
client
|
||||
} else {
|
||||
let url = self.url.as_deref().unwrap_or("ws://127.0.0.1:9944");
|
||||
RpcClient::try_from_url(url).await?
|
||||
};
|
||||
let mut rpc = Rpc::new(client);
|
||||
if self.accept_weak_inclusion {
|
||||
rpc.accept_weak_inclusion();
|
||||
}
|
||||
let (metadata, genesis_hash, runtime_version, properties) = future::join4(
|
||||
rpc.metadata(),
|
||||
rpc.genesis_hash(),
|
||||
rpc.runtime_version(None),
|
||||
rpc.system_properties(),
|
||||
)
|
||||
.await;
|
||||
let metadata = metadata?;
|
||||
|
||||
let events_decoder = EventsDecoder::new(metadata.clone());
|
||||
|
||||
Ok(Client {
|
||||
rpc,
|
||||
genesis_hash: genesis_hash?,
|
||||
metadata,
|
||||
events_decoder,
|
||||
properties: properties.unwrap_or_else(|_| Default::default()),
|
||||
runtime_version: runtime_version?,
|
||||
iter_page_size: self.page_size.unwrap_or(10),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Client to interface with a substrate node.
|
||||
pub struct Client<T: Config> {
|
||||
rpc: Rpc<T>,
|
||||
genesis_hash: T::Hash,
|
||||
metadata: Metadata,
|
||||
events_decoder: EventsDecoder<T>,
|
||||
properties: SystemProperties,
|
||||
runtime_version: RuntimeVersion,
|
||||
// _marker: PhantomData<(fn() -> T::Signature, T::Extra)>,
|
||||
iter_page_size: u32,
|
||||
}
|
||||
|
||||
impl<T: Config> Clone for Client<T> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
rpc: self.rpc.clone(),
|
||||
genesis_hash: self.genesis_hash,
|
||||
metadata: self.metadata.clone(),
|
||||
events_decoder: self.events_decoder.clone(),
|
||||
properties: self.properties.clone(),
|
||||
runtime_version: self.runtime_version.clone(),
|
||||
iter_page_size: self.iter_page_size,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Config> Client<T> {
|
||||
/// Returns the genesis hash.
|
||||
pub fn genesis(&self) -> &T::Hash {
|
||||
&self.genesis_hash
|
||||
}
|
||||
|
||||
/// Returns the chain metadata.
|
||||
pub fn metadata(&self) -> &Metadata {
|
||||
&self.metadata
|
||||
}
|
||||
|
||||
/// Returns the system properties
|
||||
pub fn properties(&self) -> &SystemProperties {
|
||||
&self.properties
|
||||
}
|
||||
|
||||
/// Returns the rpc client.
|
||||
pub fn rpc(&self) -> &Rpc<T> {
|
||||
&self.rpc
|
||||
}
|
||||
|
||||
/// Create a client for accessing runtime storage
|
||||
pub fn storage(&self) -> StorageClient<T> {
|
||||
StorageClient::new(&self.rpc, &self.metadata, self.iter_page_size)
|
||||
}
|
||||
|
||||
/// Convert the client to a runtime api wrapper for custom runtime access.
|
||||
///
|
||||
/// The `subxt` proc macro will provide methods to submit extrinsics and read storage specific
|
||||
/// to the target runtime.
|
||||
pub fn to_runtime_api<R: From<Self>>(self) -> R {
|
||||
self.into()
|
||||
}
|
||||
|
||||
/// Returns the events decoder.
|
||||
pub fn events_decoder(&self) -> &EventsDecoder<T> {
|
||||
&self.events_decoder
|
||||
}
|
||||
}
|
||||
|
||||
/// A constructed call ready to be signed and submitted.
|
||||
pub struct SubmittableExtrinsic<'a, T: Config, C> {
|
||||
client: &'a Client<T>,
|
||||
call: C,
|
||||
}
|
||||
|
||||
impl<'a, T, C> SubmittableExtrinsic<'a, T, C>
|
||||
where
|
||||
T: Config + ExtrinsicExtraData<T>,
|
||||
C: Call + Send + Sync,
|
||||
{
|
||||
/// Create a new [`SubmittableExtrinsic`].
|
||||
pub fn new(client: &'a Client<T>, call: C) -> Self {
|
||||
Self { client, call }
|
||||
}
|
||||
|
||||
/// Creates and signs an extrinsic and submits it to the chain.
|
||||
///
|
||||
/// Returns when the extrinsic has successfully been included in the block, together with any
|
||||
/// events which were triggered by the extrinsic.
|
||||
pub async fn sign_and_submit_then_watch(
|
||||
self,
|
||||
signer: &(dyn Signer<T> + Send + Sync),
|
||||
) -> Result<ExtrinsicSuccess<T>, Error>
|
||||
where
|
||||
<<<T as ExtrinsicExtraData<T>>::Extra as SignedExtra<T>>::Extra as SignedExtension>::AdditionalSigned: Send + Sync + 'static
|
||||
{
|
||||
let extrinsic = self.create_signed(signer).await?;
|
||||
self.client
|
||||
.rpc()
|
||||
.submit_and_watch_extrinsic(extrinsic, self.client.events_decoder())
|
||||
.await
|
||||
}
|
||||
|
||||
/// Creates and signs an extrinsic and submits to the chain for block inclusion.
|
||||
///
|
||||
/// Returns `Ok` with the extrinsic hash if it is valid extrinsic.
|
||||
///
|
||||
/// # Note
|
||||
///
|
||||
/// Success does not mean the extrinsic has been included in the block, just that it is valid
|
||||
/// and has been included in the transaction pool.
|
||||
pub async fn sign_and_submit(
|
||||
self,
|
||||
signer: &(dyn Signer<T> + Send + Sync),
|
||||
) -> Result<T::Hash, Error>
|
||||
where
|
||||
<<<T as ExtrinsicExtraData<T>>::Extra as SignedExtra<T>>::Extra as SignedExtension>::AdditionalSigned: Send + Sync + 'static
|
||||
{
|
||||
let extrinsic = self.create_signed(signer).await?;
|
||||
self.client.rpc().submit_extrinsic(extrinsic).await
|
||||
}
|
||||
|
||||
/// Creates a signed extrinsic.
|
||||
pub async fn create_signed(
|
||||
&self,
|
||||
signer: &(dyn Signer<T> + Send + Sync),
|
||||
) -> Result<UncheckedExtrinsic<T>, Error>
|
||||
where
|
||||
<<<T as ExtrinsicExtraData<T>>::Extra as SignedExtra<T>>::Extra as SignedExtension>::AdditionalSigned: Send + Sync + 'static
|
||||
{
|
||||
let account_nonce = if let Some(nonce) = signer.nonce() {
|
||||
nonce
|
||||
} else {
|
||||
let account_storage_entry =
|
||||
<<T as ExtrinsicExtraData<T>>::AccountData as AccountData<T>>::storage_entry(signer.account_id().clone());
|
||||
let account_data = self
|
||||
.client
|
||||
.storage()
|
||||
.fetch_or_default(&account_storage_entry, None)
|
||||
.await?;
|
||||
<<T as ExtrinsicExtraData<T>>::AccountData as AccountData<T>>::nonce(
|
||||
&account_data,
|
||||
)
|
||||
};
|
||||
let call = self
|
||||
.client
|
||||
.metadata()
|
||||
.pallet(C::PALLET)
|
||||
.and_then(|pallet| pallet.encode_call(&self.call))?;
|
||||
|
||||
let signed = extrinsic::create_signed(
|
||||
&self.client.runtime_version,
|
||||
self.client.genesis_hash,
|
||||
account_nonce,
|
||||
call,
|
||||
signer,
|
||||
)
|
||||
.await?;
|
||||
Ok(signed)
|
||||
}
|
||||
}
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
|
||||
// This file is part of subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// subxt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{
|
||||
SignedExtra,
|
||||
StorageEntry,
|
||||
};
|
||||
use codec::{
|
||||
Codec,
|
||||
Encode,
|
||||
EncodeLike,
|
||||
};
|
||||
use core::fmt::Debug;
|
||||
use sp_runtime::traits::{
|
||||
AtLeast32Bit,
|
||||
Extrinsic,
|
||||
Hash,
|
||||
Header,
|
||||
MaybeSerializeDeserialize,
|
||||
Member,
|
||||
Verify,
|
||||
};
|
||||
|
||||
/// Runtime types.
|
||||
pub trait Config: Clone + Sized + Send + Sync + 'static {
|
||||
/// Account index (aka nonce) type. This stores the number of previous
|
||||
/// transactions associated with a sender account.
|
||||
type Index: Parameter + Member + Default + AtLeast32Bit + Copy + scale_info::TypeInfo;
|
||||
|
||||
/// The block number type used by the runtime.
|
||||
type BlockNumber: Parameter
|
||||
+ Member
|
||||
+ Default
|
||||
+ Copy
|
||||
+ core::hash::Hash
|
||||
+ core::str::FromStr;
|
||||
|
||||
/// The output of the `Hashing` function.
|
||||
type Hash: Parameter
|
||||
+ Member
|
||||
+ MaybeSerializeDeserialize
|
||||
+ Ord
|
||||
+ Default
|
||||
+ Copy
|
||||
+ std::hash::Hash
|
||||
+ AsRef<[u8]>
|
||||
+ AsMut<[u8]>
|
||||
+ scale_info::TypeInfo;
|
||||
|
||||
/// The hashing system (algorithm) being used in the runtime (e.g. Blake2).
|
||||
type Hashing: Hash<Output = Self::Hash>;
|
||||
|
||||
/// The user account identifier type for the runtime.
|
||||
type AccountId: Parameter + Member;
|
||||
|
||||
/// The address type. This instead of `<frame_system::Trait::Lookup as StaticLookup>::Source`.
|
||||
type Address: Codec + Clone + PartialEq;
|
||||
|
||||
/// The block header.
|
||||
type Header: Parameter
|
||||
+ Header<Number = Self::BlockNumber, Hash = Self::Hash>
|
||||
+ serde::de::DeserializeOwned;
|
||||
|
||||
/// Signature type.
|
||||
type Signature: Verify + Encode + Send + Sync + 'static;
|
||||
|
||||
/// Extrinsic type within blocks.
|
||||
type Extrinsic: Parameter + Extrinsic + Debug + MaybeSerializeDeserialize;
|
||||
}
|
||||
|
||||
/// Parameter trait copied from `substrate::frame_support`
|
||||
pub trait Parameter: Codec + EncodeLike + Clone + Eq + Debug {}
|
||||
impl<T> Parameter for T where T: Codec + EncodeLike + Clone + Eq + Debug {}
|
||||
|
||||
/// Trait to fetch data about an account.
|
||||
///
|
||||
/// Should be implemented on a type implementing `StorageEntry`,
|
||||
/// usually generated by the `subxt` macro.
|
||||
pub trait AccountData<T: Config>: StorageEntry {
|
||||
/// Create a new storage entry key from the account id.
|
||||
fn storage_entry(account_id: T::AccountId) -> Self;
|
||||
/// Get the nonce from the storage entry value.
|
||||
fn nonce(result: &<Self as StorageEntry>::Value) -> T::Index;
|
||||
}
|
||||
|
||||
/// Trait to configure the extra data for an extrinsic.
|
||||
pub trait ExtrinsicExtraData<T: Config> {
|
||||
/// The type of the [`StorageEntry`] which can be used to retrieve an account nonce.
|
||||
type AccountData: AccountData<T>;
|
||||
/// The type of extra data and additional signed data to be included in a transaction.
|
||||
type Extra: SignedExtra<T> + Send + Sync + 'static;
|
||||
}
|
||||
+29
-28
@@ -1,5 +1,5 @@
|
||||
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
|
||||
// This file is part of substrate-subxt.
|
||||
// This file is part of subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
@@ -12,8 +12,16 @@
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with substrate-subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{
|
||||
events::EventsDecodingError,
|
||||
metadata::{
|
||||
InvalidMetadataError,
|
||||
MetadataError,
|
||||
},
|
||||
Metadata,
|
||||
};
|
||||
use jsonrpsee_types::Error as RequestError;
|
||||
use sp_core::crypto::SecretStringError;
|
||||
use sp_runtime::{
|
||||
@@ -22,11 +30,6 @@ use sp_runtime::{
|
||||
};
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::metadata::{
|
||||
Metadata,
|
||||
MetadataError,
|
||||
};
|
||||
|
||||
/// Error enum.
|
||||
#[derive(Debug, Error)]
|
||||
pub enum Error {
|
||||
@@ -48,22 +51,18 @@ pub enum Error {
|
||||
/// Extrinsic validity error
|
||||
#[error("Transaction Validity Error: {0:?}")]
|
||||
Invalid(TransactionValidityError),
|
||||
/// Metadata error.
|
||||
#[error("Metadata error: {0}")]
|
||||
/// Invalid metadata error
|
||||
#[error("Invalid Metadata: {0}")]
|
||||
InvalidMetadata(#[from] InvalidMetadataError),
|
||||
/// Invalid metadata error
|
||||
#[error("Metadata: {0}")]
|
||||
Metadata(#[from] MetadataError),
|
||||
/// Unregistered type sizes.
|
||||
#[error(
|
||||
"The following types do not have a type size registered: \
|
||||
{0:?} \
|
||||
Use `ClientBuilder::register_type_size` to register missing type sizes."
|
||||
)]
|
||||
MissingTypeSizes(Vec<String>),
|
||||
/// Type size unavailable.
|
||||
#[error("Type size unavailable while decoding event: {0:?}")]
|
||||
TypeSizeUnavailable(String),
|
||||
/// Runtime error.
|
||||
#[error("Runtime error: {0}")]
|
||||
Runtime(#[from] RuntimeError),
|
||||
/// Events decoding error.
|
||||
#[error("Events decoding error: {0}")]
|
||||
EventsDecoding(#[from] EventsDecodingError),
|
||||
/// Other error.
|
||||
#[error("Other error: {0}")]
|
||||
Other(String),
|
||||
@@ -98,7 +97,7 @@ impl From<String> for Error {
|
||||
pub enum RuntimeError {
|
||||
/// Module error.
|
||||
#[error("Runtime module error: {0}")]
|
||||
Module(ModuleError),
|
||||
Module(PalletError),
|
||||
/// At least one consumer is remaining so the account cannot be destroyed.
|
||||
#[error("At least one consumer is remaining so the account cannot be destroyed.")]
|
||||
ConsumerRemaining,
|
||||
@@ -128,11 +127,11 @@ impl RuntimeError {
|
||||
error,
|
||||
message: _,
|
||||
} => {
|
||||
let module = metadata.module_with_errors(index)?;
|
||||
let error = module.error(error)?;
|
||||
Ok(Self::Module(ModuleError {
|
||||
module: module.name().to_string(),
|
||||
error: error.to_string(),
|
||||
let error = metadata.error(index, error)?;
|
||||
Ok(Self::Module(PalletError {
|
||||
pallet: error.pallet().to_string(),
|
||||
error: error.error().to_string(),
|
||||
description: error.description().to_vec(),
|
||||
}))
|
||||
}
|
||||
DispatchError::BadOrigin => Ok(Self::BadOrigin),
|
||||
@@ -150,10 +149,12 @@ impl RuntimeError {
|
||||
|
||||
/// Module error.
|
||||
#[derive(Clone, Debug, Eq, Error, PartialEq)]
|
||||
#[error("{error} from {module}")]
|
||||
pub struct ModuleError {
|
||||
#[error("{error} from {pallet}")]
|
||||
pub struct PalletError {
|
||||
/// The module where the error originated.
|
||||
pub module: String,
|
||||
pub pallet: String,
|
||||
/// The actual error code.
|
||||
pub error: String,
|
||||
/// The error description.
|
||||
pub description: Vec<String>,
|
||||
}
|
||||
|
||||
+273
-356
@@ -1,5 +1,5 @@
|
||||
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
|
||||
// This file is part of substrate-subxt.
|
||||
// This file is part of subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
@@ -12,7 +12,7 @@
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with substrate-subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use codec::{
|
||||
Codec,
|
||||
@@ -20,115 +20,57 @@ use codec::{
|
||||
Decode,
|
||||
Encode,
|
||||
Input,
|
||||
Output,
|
||||
};
|
||||
use dyn_clone::DynClone;
|
||||
use sp_runtime::{
|
||||
DispatchError,
|
||||
DispatchResult,
|
||||
};
|
||||
use std::{
|
||||
collections::{
|
||||
hash_map::{
|
||||
Entry,
|
||||
HashMap,
|
||||
},
|
||||
HashSet,
|
||||
},
|
||||
fmt,
|
||||
marker::{
|
||||
PhantomData,
|
||||
Send,
|
||||
},
|
||||
};
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use crate::{
|
||||
error::{
|
||||
Error,
|
||||
RuntimeError,
|
||||
},
|
||||
metadata::{
|
||||
EventArg,
|
||||
Metadata,
|
||||
EventMetadata,
|
||||
MetadataError,
|
||||
},
|
||||
Config,
|
||||
Error,
|
||||
Metadata,
|
||||
Phase,
|
||||
Runtime,
|
||||
System,
|
||||
RuntimeError,
|
||||
};
|
||||
use scale_info::{
|
||||
TypeDef,
|
||||
TypeDefPrimitive,
|
||||
};
|
||||
use sp_core::Bytes;
|
||||
|
||||
/// Raw bytes for an Event
|
||||
#[derive(Debug)]
|
||||
pub struct RawEvent {
|
||||
/// The name of the module from whence the Event originated
|
||||
pub module: String,
|
||||
/// The name of the Event
|
||||
/// The name of the pallet from whence the Event originated.
|
||||
pub pallet: String,
|
||||
/// The index of the pallet from whence the Event originated.
|
||||
pub pallet_index: u8,
|
||||
/// The name of the pallet Event variant.
|
||||
pub variant: String,
|
||||
/// The index of the pallet Event variant.
|
||||
pub variant_index: u8,
|
||||
/// The raw Event data
|
||||
pub data: Vec<u8>,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for RawEvent {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
f.debug_struct("RawEvent")
|
||||
.field("module", &self.module)
|
||||
.field("variant", &self.variant)
|
||||
.field("data", &hex::encode(&self.data))
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
pub trait TypeSegmenter: DynClone + Send + Sync {
|
||||
/// Consumes an object from an input stream, and output the serialized bytes.
|
||||
fn segment(&self, input: &mut &[u8], output: &mut Vec<u8>) -> Result<(), Error>;
|
||||
}
|
||||
|
||||
// derive object safe Clone impl for `Box<dyn TypeSegmenter>`
|
||||
dyn_clone::clone_trait_object!(TypeSegmenter);
|
||||
|
||||
struct TypeMarker<T>(PhantomData<T>);
|
||||
impl<T> TypeSegmenter for TypeMarker<T>
|
||||
where
|
||||
T: Codec + Send + Sync,
|
||||
{
|
||||
fn segment(&self, input: &mut &[u8], output: &mut Vec<u8>) -> Result<(), Error> {
|
||||
T::decode(input).map_err(Error::from)?.encode_to(output);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Clone for TypeMarker<T> {
|
||||
fn clone(&self) -> Self {
|
||||
Self(Default::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Default for TypeMarker<T> {
|
||||
fn default() -> Self {
|
||||
Self(Default::default())
|
||||
}
|
||||
pub data: Bytes,
|
||||
}
|
||||
|
||||
/// Events decoder.
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct EventsDecoder<T> {
|
||||
metadata: Metadata,
|
||||
event_type_registry: EventTypeRegistry<T>,
|
||||
marker: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<T> Clone for EventsDecoder<T> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
metadata: self.metadata.clone(),
|
||||
event_type_registry: self.event_type_registry.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Runtime + System> EventsDecoder<T> {
|
||||
impl<T> EventsDecoder<T>
|
||||
where
|
||||
T: Config,
|
||||
{
|
||||
/// Creates a new `EventsDecoder`.
|
||||
pub fn new(metadata: Metadata, event_type_registry: EventTypeRegistry<T>) -> Self {
|
||||
pub fn new(metadata: Metadata) -> Self {
|
||||
Self {
|
||||
metadata,
|
||||
event_type_registry,
|
||||
marker: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,28 +78,28 @@ impl<T: Runtime + System> EventsDecoder<T> {
|
||||
pub fn decode_events(&self, input: &mut &[u8]) -> Result<Vec<(Phase, Raw)>, Error> {
|
||||
let compact_len = <Compact<u32>>::decode(input)?;
|
||||
let len = compact_len.0 as usize;
|
||||
log::debug!("decoding {} events", len);
|
||||
|
||||
let mut r = Vec::new();
|
||||
for _ in 0..len {
|
||||
// decode EventRecord
|
||||
let phase = Phase::decode(input)?;
|
||||
let module_variant = input.read_byte()?;
|
||||
|
||||
let module = self.metadata.module_with_events(module_variant)?;
|
||||
let event_variant = input.read_byte()?;
|
||||
let event_metadata = module.event(event_variant)?;
|
||||
|
||||
let pallet_index = input.read_byte()?;
|
||||
let variant_index = input.read_byte()?;
|
||||
log::debug!(
|
||||
"received event '{}::{}' ({:?})",
|
||||
module.name(),
|
||||
event_metadata.name,
|
||||
event_metadata.arguments()
|
||||
"phase {:?}, pallet_index {}, event_variant: {}",
|
||||
phase,
|
||||
pallet_index,
|
||||
variant_index
|
||||
);
|
||||
log::debug!("remaining input: {}", hex::encode(&input));
|
||||
|
||||
let event_metadata = self.metadata.event(pallet_index, variant_index)?;
|
||||
|
||||
let mut event_data = Vec::<u8>::new();
|
||||
let mut event_errors = Vec::<RuntimeError>::new();
|
||||
let result = self.decode_raw_bytes(
|
||||
&event_metadata.arguments(),
|
||||
let result = self.decode_raw_event(
|
||||
&event_metadata,
|
||||
input,
|
||||
&mut event_data,
|
||||
&mut event_errors,
|
||||
@@ -167,13 +109,17 @@ impl<T: Runtime + System> EventsDecoder<T> {
|
||||
log::debug!("raw bytes: {}", hex::encode(&event_data),);
|
||||
|
||||
let event = RawEvent {
|
||||
module: module.name().to_string(),
|
||||
variant: event_metadata.name.clone(),
|
||||
data: event_data,
|
||||
pallet: event_metadata.pallet().to_string(),
|
||||
pallet_index,
|
||||
variant: event_metadata.event().to_string(),
|
||||
variant_index,
|
||||
data: event_data.into(),
|
||||
};
|
||||
|
||||
// topics come after the event data in EventRecord
|
||||
let _topics = Vec::<T::Hash>::decode(input)?;
|
||||
let topics = Vec::<T::Hash>::decode(input)?;
|
||||
log::debug!("topics: {:?}", topics);
|
||||
|
||||
Raw::Event(event)
|
||||
}
|
||||
Err(err) => return Err(err),
|
||||
@@ -190,154 +136,200 @@ impl<T: Runtime + System> EventsDecoder<T> {
|
||||
Ok(r)
|
||||
}
|
||||
|
||||
fn decode_raw_bytes<W: Output>(
|
||||
fn decode_raw_event(
|
||||
&self,
|
||||
args: &[EventArg],
|
||||
event_metadata: &EventMetadata,
|
||||
input: &mut &[u8],
|
||||
output: &mut W,
|
||||
output: &mut Vec<u8>,
|
||||
errors: &mut Vec<RuntimeError>,
|
||||
) -> Result<(), Error> {
|
||||
for arg in args {
|
||||
match arg {
|
||||
EventArg::Vec(arg) => {
|
||||
let len = <Compact<u32>>::decode(input)?;
|
||||
len.encode_to(output);
|
||||
for _ in 0..len.0 {
|
||||
self.decode_raw_bytes(&[*arg.clone()], input, output, errors)?
|
||||
}
|
||||
}
|
||||
EventArg::Option(arg) => {
|
||||
match input.read_byte()? {
|
||||
0 => output.push_byte(0),
|
||||
1 => {
|
||||
output.push_byte(1);
|
||||
self.decode_raw_bytes(&[*arg.clone()], input, output, errors)?
|
||||
}
|
||||
_ => {
|
||||
return Err(Error::Other(
|
||||
"unexpected first byte decoding Option".into(),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
EventArg::Tuple(args) => {
|
||||
self.decode_raw_bytes(args, input, output, errors)?
|
||||
}
|
||||
EventArg::Primitive(name) => {
|
||||
let result = match name.as_str() {
|
||||
"DispatchResult" => DispatchResult::decode(input)?,
|
||||
"DispatchError" => Err(DispatchError::decode(input)?),
|
||||
_ => {
|
||||
if let Some(seg) = self.event_type_registry.resolve(name) {
|
||||
let mut buf = Vec::<u8>::new();
|
||||
seg.segment(input, &mut buf)?;
|
||||
output.write(&buf);
|
||||
Ok(())
|
||||
} else {
|
||||
return Err(Error::TypeSizeUnavailable(name.to_owned()))
|
||||
}
|
||||
}
|
||||
};
|
||||
if let Err(error) = result {
|
||||
// since the input may contain any number of args we propagate
|
||||
// runtime errors to the caller for handling
|
||||
errors.push(RuntimeError::from_dispatch(&self.metadata, error)?);
|
||||
}
|
||||
log::debug!(
|
||||
"Decoding Event '{}::{}'",
|
||||
event_metadata.pallet(),
|
||||
event_metadata.event()
|
||||
);
|
||||
for arg in event_metadata.variant().fields() {
|
||||
let type_id = arg.ty().id();
|
||||
if event_metadata.pallet() == "System"
|
||||
&& event_metadata.event() == "ExtrinsicFailed"
|
||||
{
|
||||
let ty = self
|
||||
.metadata
|
||||
.resolve_type(type_id)
|
||||
.ok_or(MetadataError::TypeNotFound(type_id))?;
|
||||
|
||||
if ty.path().ident() == Some("DispatchError".to_string()) {
|
||||
let dispatch_error = sp_runtime::DispatchError::decode(input)?;
|
||||
log::info!("Dispatch Error {:?}", dispatch_error);
|
||||
dispatch_error.encode_to(output);
|
||||
let runtime_error =
|
||||
RuntimeError::from_dispatch(&self.metadata, dispatch_error)?;
|
||||
errors.push(runtime_error);
|
||||
continue
|
||||
}
|
||||
}
|
||||
self.decode_type(type_id, input, output)?
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Registry for event types which cannot be directly inferred from the metadata.
|
||||
#[derive(Default)]
|
||||
pub struct EventTypeRegistry<T> {
|
||||
segmenters: HashMap<String, Box<dyn TypeSegmenter>>,
|
||||
marker: PhantomData<fn() -> T>,
|
||||
}
|
||||
|
||||
impl<T> Clone for EventTypeRegistry<T> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
segmenters: self.segmenters.clone(),
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> fmt::Debug for EventTypeRegistry<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("EventTypeRegistry")
|
||||
.field(
|
||||
"segmenters",
|
||||
&self.segmenters.keys().cloned().collect::<String>(),
|
||||
)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Runtime> EventTypeRegistry<T> {
|
||||
/// Create a new [`EventTypeRegistry`].
|
||||
pub fn new() -> Self {
|
||||
let mut registry = Self {
|
||||
segmenters: HashMap::new(),
|
||||
marker: PhantomData,
|
||||
};
|
||||
T::register_type_sizes(&mut registry);
|
||||
registry
|
||||
}
|
||||
|
||||
/// Register a type.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// If there is already a type size registered with this name.
|
||||
pub fn register_type_size<U>(&mut self, name: &str)
|
||||
where
|
||||
U: Codec + Send + Sync + 'static,
|
||||
{
|
||||
// A segmenter decodes a type from an input stream (&mut &[u8]) and returns te serialized
|
||||
// type to the output stream (&mut Vec<u8>).
|
||||
match self.segmenters.entry(name.to_string()) {
|
||||
Entry::Occupied(_) => panic!("Already a type registered with key {}", name),
|
||||
Entry::Vacant(entry) => entry.insert(Box::new(TypeMarker::<U>::default())),
|
||||
};
|
||||
}
|
||||
|
||||
/// Check missing type sizes.
|
||||
pub fn check_missing_type_sizes(
|
||||
fn decode_type(
|
||||
&self,
|
||||
metadata: &Metadata,
|
||||
) -> Result<(), HashSet<String>> {
|
||||
let mut missing = HashSet::new();
|
||||
for module in metadata.modules_with_events() {
|
||||
for event in module.events() {
|
||||
for arg in event.arguments() {
|
||||
for primitive in arg.primitives() {
|
||||
if !self.segmenters.contains_key(&primitive) {
|
||||
missing.insert(format!(
|
||||
"{}::{}::{}",
|
||||
module.name(),
|
||||
event.name,
|
||||
primitive
|
||||
));
|
||||
}
|
||||
type_id: u32,
|
||||
input: &mut &[u8],
|
||||
output: &mut Vec<u8>,
|
||||
) -> Result<(), Error> {
|
||||
let ty = self
|
||||
.metadata
|
||||
.resolve_type(type_id)
|
||||
.ok_or(MetadataError::TypeNotFound(type_id))?;
|
||||
|
||||
fn decode_raw<T: Codec>(
|
||||
input: &mut &[u8],
|
||||
output: &mut Vec<u8>,
|
||||
) -> Result<(), Error> {
|
||||
let decoded = T::decode(input)?;
|
||||
decoded.encode_to(output);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
match ty.type_def() {
|
||||
TypeDef::Composite(composite) => {
|
||||
for field in composite.fields() {
|
||||
self.decode_type(field.ty().id(), input, output)?
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
TypeDef::Variant(variant) => {
|
||||
let variant_index = u8::decode(input)?;
|
||||
variant_index.encode_to(output);
|
||||
let variant = variant.variants().get(variant_index as usize).ok_or(
|
||||
Error::Other(format!("Variant {} not found", variant_index)),
|
||||
)?;
|
||||
for field in variant.fields() {
|
||||
self.decode_type(field.ty().id(), input, output)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
TypeDef::Sequence(seq) => {
|
||||
let len = <Compact<u32>>::decode(input)?;
|
||||
len.encode_to(output);
|
||||
for _ in 0..len.0 {
|
||||
self.decode_type(seq.type_param().id(), input, output)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
TypeDef::Array(arr) => {
|
||||
for _ in 0..arr.len() {
|
||||
self.decode_type(arr.type_param().id(), input, output)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
TypeDef::Tuple(tuple) => {
|
||||
for field in tuple.fields() {
|
||||
self.decode_type(field.id(), input, output)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
TypeDef::Primitive(primitive) => {
|
||||
match primitive {
|
||||
TypeDefPrimitive::Bool => decode_raw::<bool>(input, output),
|
||||
TypeDefPrimitive::Char => {
|
||||
Err(EventsDecodingError::UnsupportedPrimitive(
|
||||
TypeDefPrimitive::Char,
|
||||
)
|
||||
.into())
|
||||
}
|
||||
TypeDefPrimitive::Str => decode_raw::<String>(input, output),
|
||||
TypeDefPrimitive::U8 => decode_raw::<u8>(input, output),
|
||||
TypeDefPrimitive::U16 => decode_raw::<u16>(input, output),
|
||||
TypeDefPrimitive::U32 => decode_raw::<u32>(input, output),
|
||||
TypeDefPrimitive::U64 => decode_raw::<u64>(input, output),
|
||||
TypeDefPrimitive::U128 => decode_raw::<u128>(input, output),
|
||||
TypeDefPrimitive::U256 => {
|
||||
Err(EventsDecodingError::UnsupportedPrimitive(
|
||||
TypeDefPrimitive::U256,
|
||||
)
|
||||
.into())
|
||||
}
|
||||
TypeDefPrimitive::I8 => decode_raw::<i8>(input, output),
|
||||
TypeDefPrimitive::I16 => decode_raw::<i16>(input, output),
|
||||
TypeDefPrimitive::I32 => decode_raw::<i32>(input, output),
|
||||
TypeDefPrimitive::I64 => decode_raw::<i64>(input, output),
|
||||
TypeDefPrimitive::I128 => decode_raw::<i128>(input, output),
|
||||
TypeDefPrimitive::I256 => {
|
||||
Err(EventsDecodingError::UnsupportedPrimitive(
|
||||
TypeDefPrimitive::I256,
|
||||
)
|
||||
.into())
|
||||
}
|
||||
}
|
||||
}
|
||||
TypeDef::Compact(_compact) => {
|
||||
let inner = self
|
||||
.metadata
|
||||
.resolve_type(type_id)
|
||||
.ok_or(MetadataError::TypeNotFound(type_id))?;
|
||||
let mut decode_compact_primitive = |primitive: &TypeDefPrimitive| {
|
||||
match primitive {
|
||||
TypeDefPrimitive::U8 => decode_raw::<Compact<u8>>(input, output),
|
||||
TypeDefPrimitive::U16 => {
|
||||
decode_raw::<Compact<u16>>(input, output)
|
||||
}
|
||||
TypeDefPrimitive::U32 => {
|
||||
decode_raw::<Compact<u32>>(input, output)
|
||||
}
|
||||
TypeDefPrimitive::U64 => {
|
||||
decode_raw::<Compact<u64>>(input, output)
|
||||
}
|
||||
TypeDefPrimitive::U128 => {
|
||||
decode_raw::<Compact<u128>>(input, output)
|
||||
}
|
||||
prim => {
|
||||
Err(EventsDecodingError::InvalidCompactPrimitive(
|
||||
prim.clone(),
|
||||
)
|
||||
.into())
|
||||
}
|
||||
}
|
||||
};
|
||||
match inner.type_def() {
|
||||
TypeDef::Primitive(primitive) => decode_compact_primitive(primitive),
|
||||
TypeDef::Composite(composite) => {
|
||||
match composite.fields() {
|
||||
[field] => {
|
||||
let field_ty =
|
||||
self.metadata.resolve_type(field.ty().id()).ok_or(
|
||||
MetadataError::TypeNotFound(field.ty().id()),
|
||||
)?;
|
||||
if let TypeDef::Primitive(primitive) = field_ty.type_def()
|
||||
{
|
||||
decode_compact_primitive(primitive)
|
||||
} else {
|
||||
Err(EventsDecodingError::InvalidCompactType("Composite type must have a single primitive field".into()).into())
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
Err(EventsDecodingError::InvalidCompactType(
|
||||
"Composite type must have a single field".into(),
|
||||
)
|
||||
.into())
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
Err(EventsDecodingError::InvalidCompactType(
|
||||
"Compact type must be a primitive or a composite type".into(),
|
||||
)
|
||||
.into())
|
||||
}
|
||||
}
|
||||
}
|
||||
TypeDef::BitSequence(_bitseq) => {
|
||||
// decode_raw::<bitvec::BitVec>
|
||||
unimplemented!("BitVec decoding for events not implemented yet")
|
||||
}
|
||||
}
|
||||
|
||||
if !missing.is_empty() {
|
||||
Err(missing)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Resolve a segmenter for a type by its name.
|
||||
pub fn resolve(&self, name: &str) -> Option<&Box<dyn TypeSegmenter>> {
|
||||
self.segmenters.get(name)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -350,122 +342,47 @@ pub enum Raw {
|
||||
Error(RuntimeError),
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use frame_metadata::{
|
||||
DecodeDifferent,
|
||||
ErrorMetadata,
|
||||
EventMetadata,
|
||||
ExtrinsicMetadata,
|
||||
ModuleMetadata,
|
||||
RuntimeMetadata,
|
||||
RuntimeMetadataPrefixed,
|
||||
RuntimeMetadataV13,
|
||||
META_RESERVED,
|
||||
};
|
||||
use std::convert::TryFrom;
|
||||
|
||||
type TestRuntime = crate::NodeTemplateRuntime;
|
||||
|
||||
#[test]
|
||||
fn test_decode_option() {
|
||||
let decoder = EventsDecoder::<TestRuntime>::new(
|
||||
Metadata::default(),
|
||||
EventTypeRegistry::new(),
|
||||
);
|
||||
|
||||
let value = Some(0u8);
|
||||
let input = value.encode();
|
||||
let mut output = Vec::<u8>::new();
|
||||
let mut errors = Vec::<RuntimeError>::new();
|
||||
|
||||
decoder
|
||||
.decode_raw_bytes(
|
||||
&[EventArg::Option(Box::new(EventArg::Primitive(
|
||||
"u8".to_string(),
|
||||
)))],
|
||||
&mut &input[..],
|
||||
&mut output,
|
||||
&mut errors,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(output, vec![1, 0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_decode_system_events_and_error() {
|
||||
let decoder = EventsDecoder::<TestRuntime>::new(
|
||||
Metadata::try_from(RuntimeMetadataPrefixed(
|
||||
META_RESERVED,
|
||||
RuntimeMetadata::V13(RuntimeMetadataV13 {
|
||||
modules: DecodeDifferent::Decoded(vec![ModuleMetadata {
|
||||
name: DecodeDifferent::Decoded("System".to_string()),
|
||||
storage: None,
|
||||
calls: None,
|
||||
event: Some(DecodeDifferent::Decoded(vec![
|
||||
EventMetadata {
|
||||
name: DecodeDifferent::Decoded(
|
||||
"ExtrinsicSuccess".to_string(),
|
||||
),
|
||||
arguments: DecodeDifferent::Decoded(vec![
|
||||
"DispatchInfo".to_string()
|
||||
]),
|
||||
documentation: DecodeDifferent::Decoded(vec![]),
|
||||
},
|
||||
EventMetadata {
|
||||
name: DecodeDifferent::Decoded(
|
||||
"ExtrinsicFailed".to_string(),
|
||||
),
|
||||
arguments: DecodeDifferent::Decoded(vec![
|
||||
"DispatchError".to_string(),
|
||||
"DispatchInfo".to_string(),
|
||||
]),
|
||||
documentation: DecodeDifferent::Decoded(vec![]),
|
||||
},
|
||||
])),
|
||||
constants: DecodeDifferent::Decoded(vec![]),
|
||||
errors: DecodeDifferent::Decoded(vec![
|
||||
ErrorMetadata {
|
||||
name: DecodeDifferent::Decoded(
|
||||
"InvalidSpecName".to_string(),
|
||||
),
|
||||
documentation: DecodeDifferent::Decoded(vec![]),
|
||||
},
|
||||
ErrorMetadata {
|
||||
name: DecodeDifferent::Decoded(
|
||||
"SpecVersionNeedsToIncrease".to_string(),
|
||||
),
|
||||
documentation: DecodeDifferent::Decoded(vec![]),
|
||||
},
|
||||
ErrorMetadata {
|
||||
name: DecodeDifferent::Decoded(
|
||||
"FailedToExtractRuntimeVersion".to_string(),
|
||||
),
|
||||
documentation: DecodeDifferent::Decoded(vec![]),
|
||||
},
|
||||
ErrorMetadata {
|
||||
name: DecodeDifferent::Decoded(
|
||||
"NonDefaultComposite".to_string(),
|
||||
),
|
||||
documentation: DecodeDifferent::Decoded(vec![]),
|
||||
},
|
||||
]),
|
||||
index: 0,
|
||||
}]),
|
||||
extrinsic: ExtrinsicMetadata {
|
||||
version: 0,
|
||||
signed_extensions: vec![],
|
||||
},
|
||||
}),
|
||||
))
|
||||
.unwrap(),
|
||||
EventTypeRegistry::new(),
|
||||
);
|
||||
|
||||
// [(ApplyExtrinsic(0), Event(RawEvent { module: "System", variant: "ExtrinsicSuccess", data: "482d7c09000000000200" })), (ApplyExtrinsic(1), Error(Module(ModuleError { module: "System", error: "NonDefaultComposite" }))), (ApplyExtrinsic(2), Error(Module(ModuleError { module: "System", error: "NonDefaultComposite" })))]
|
||||
let input = hex::decode("0c00000000000000482d7c0900000000020000000100000000010300035884723300000000000000000200000000010300035884723300000000000000").unwrap();
|
||||
decoder.decode_events(&mut &input[..]).unwrap();
|
||||
}
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum EventsDecodingError {
|
||||
/// Unsupported primitive type
|
||||
#[error("Unsupported primitive type {0:?}")]
|
||||
UnsupportedPrimitive(TypeDefPrimitive),
|
||||
/// Invalid compact type, must be an unsigned int.
|
||||
#[error("Invalid compact primitive {0:?}")]
|
||||
InvalidCompactPrimitive(TypeDefPrimitive),
|
||||
#[error("Invalid compact composite type {0}")]
|
||||
InvalidCompactType(String),
|
||||
}
|
||||
|
||||
// #[cfg(test)]
|
||||
// mod tests {
|
||||
// use super::*;
|
||||
// use std::convert::TryFrom;
|
||||
//
|
||||
// type DefaultConfig = crate::NodeTemplateRuntime;
|
||||
//
|
||||
// #[test]
|
||||
// fn test_decode_option() {
|
||||
// let decoder = EventsDecoder::<DefaultConfig>::new(
|
||||
// Metadata::default(),
|
||||
// );
|
||||
//
|
||||
// let value = Some(0u8);
|
||||
// let input = value.encode();
|
||||
// let mut output = Vec::<u8>::new();
|
||||
// let mut errors = Vec::<RuntimeError>::new();
|
||||
//
|
||||
// decoder
|
||||
// .decode_raw_bytes(
|
||||
// &[EventArg::Option(Box::new(EventArg::Primitive(
|
||||
// "u8".to_string(),
|
||||
// )))],
|
||||
// &mut &input[..],
|
||||
// &mut output,
|
||||
// &mut errors,
|
||||
// )
|
||||
// .unwrap();
|
||||
//
|
||||
// assert_eq!(output, vec![1, 0]);
|
||||
// }
|
||||
// }
|
||||
|
||||
+45
-49
@@ -1,5 +1,5 @@
|
||||
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
|
||||
// This file is part of substrate-subxt.
|
||||
// This file is part of subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
@@ -12,7 +12,7 @@
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with substrate-subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use codec::{
|
||||
Decode,
|
||||
@@ -22,22 +22,17 @@ use core::{
|
||||
fmt::Debug,
|
||||
marker::PhantomData,
|
||||
};
|
||||
use scale_info::TypeInfo;
|
||||
use sp_runtime::{
|
||||
generic::Era,
|
||||
traits::SignedExtension,
|
||||
transaction_validity::TransactionValidityError,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
frame::{
|
||||
balances::Balances,
|
||||
system::System,
|
||||
},
|
||||
runtimes::Runtime,
|
||||
};
|
||||
use crate::Config;
|
||||
|
||||
/// Extra type.
|
||||
pub type Extra<T> = <<T as Runtime>::Extra as SignedExtra<T>>::Extra;
|
||||
// pub type Extra<T> = <<T as Config>::Extra as SignedExtra<T>>::Extra;
|
||||
|
||||
/// SignedExtra checks copied from substrate, in order to remove requirement to implement
|
||||
/// substrate's `frame_system::Trait`
|
||||
@@ -50,8 +45,9 @@ pub type Extra<T> = <<T as Runtime>::Extra as SignedExtra<T>>::Extra;
|
||||
/// returned via `additional_signed()`.
|
||||
|
||||
/// Ensure the runtime version registered in the transaction is the same as at present.
|
||||
#[derive(Encode, Decode, Clone, Eq, PartialEq, Debug)]
|
||||
pub struct CheckSpecVersion<T: System>(
|
||||
#[derive(Encode, Decode, Clone, Eq, PartialEq, Debug, TypeInfo)]
|
||||
#[scale_info(skip_type_params(T))]
|
||||
pub struct CheckSpecVersion<T: Config>(
|
||||
pub PhantomData<T>,
|
||||
/// Local version to be used for `AdditionalSigned`
|
||||
#[codec(skip)]
|
||||
@@ -60,7 +56,7 @@ pub struct CheckSpecVersion<T: System>(
|
||||
|
||||
impl<T> SignedExtension for CheckSpecVersion<T>
|
||||
where
|
||||
T: System + Clone + Debug + Eq + Send + Sync,
|
||||
T: Config + Clone + Debug + Eq + Send + Sync,
|
||||
{
|
||||
const IDENTIFIER: &'static str = "CheckSpecVersion";
|
||||
type AccountId = u64;
|
||||
@@ -80,8 +76,9 @@ where
|
||||
///
|
||||
/// This is modified from the substrate version to allow passing in of the version, which is
|
||||
/// returned via `additional_signed()`.
|
||||
#[derive(Encode, Decode, Clone, Eq, PartialEq, Debug)]
|
||||
pub struct CheckTxVersion<T: System>(
|
||||
#[derive(Encode, Decode, Clone, Eq, PartialEq, Debug, TypeInfo)]
|
||||
#[scale_info(skip_type_params(T))]
|
||||
pub struct CheckTxVersion<T: Config>(
|
||||
pub PhantomData<T>,
|
||||
/// Local version to be used for `AdditionalSigned`
|
||||
#[codec(skip)]
|
||||
@@ -90,7 +87,7 @@ pub struct CheckTxVersion<T: System>(
|
||||
|
||||
impl<T> SignedExtension for CheckTxVersion<T>
|
||||
where
|
||||
T: System + Clone + Debug + Eq + Send + Sync,
|
||||
T: Config + Clone + Debug + Eq + Send + Sync,
|
||||
{
|
||||
const IDENTIFIER: &'static str = "CheckTxVersion";
|
||||
type AccountId = u64;
|
||||
@@ -110,8 +107,9 @@ where
|
||||
///
|
||||
/// This is modified from the substrate version to allow passing in of the genesis hash, which is
|
||||
/// returned via `additional_signed()`.
|
||||
#[derive(Encode, Decode, Clone, Eq, PartialEq, Debug)]
|
||||
pub struct CheckGenesis<T: System>(
|
||||
#[derive(Encode, Decode, Clone, Eq, PartialEq, Debug, TypeInfo)]
|
||||
#[scale_info(skip_type_params(T))]
|
||||
pub struct CheckGenesis<T: Config>(
|
||||
pub PhantomData<T>,
|
||||
/// Local genesis hash to be used for `AdditionalSigned`
|
||||
#[codec(skip)]
|
||||
@@ -120,7 +118,7 @@ pub struct CheckGenesis<T: System>(
|
||||
|
||||
impl<T> SignedExtension for CheckGenesis<T>
|
||||
where
|
||||
T: System + Clone + Debug + Eq + Send + Sync,
|
||||
T: Config + Clone + Debug + Eq + Send + Sync,
|
||||
{
|
||||
const IDENTIFIER: &'static str = "CheckGenesis";
|
||||
type AccountId = u64;
|
||||
@@ -141,8 +139,9 @@ where
|
||||
/// This is modified from the substrate version to allow passing in of the genesis hash, which is
|
||||
/// returned via `additional_signed()`. It assumes therefore `Era::Immortal` (The transaction is
|
||||
/// valid forever)
|
||||
#[derive(Encode, Decode, Clone, Eq, PartialEq, Debug)]
|
||||
pub struct CheckEra<T: System>(
|
||||
#[derive(Encode, Decode, Clone, Eq, PartialEq, Debug, TypeInfo)]
|
||||
#[scale_info(skip_type_params(T))]
|
||||
pub struct CheckMortality<T: Config>(
|
||||
/// The default structure for the Extra encoding
|
||||
pub (Era, PhantomData<T>),
|
||||
/// Local genesis hash to be used for `AdditionalSigned`
|
||||
@@ -150,11 +149,11 @@ pub struct CheckEra<T: System>(
|
||||
pub T::Hash,
|
||||
);
|
||||
|
||||
impl<T> SignedExtension for CheckEra<T>
|
||||
impl<T> SignedExtension for CheckMortality<T>
|
||||
where
|
||||
T: System + Clone + Debug + Eq + Send + Sync,
|
||||
T: Config + Clone + Debug + Eq + Send + Sync,
|
||||
{
|
||||
const IDENTIFIER: &'static str = "CheckEra";
|
||||
const IDENTIFIER: &'static str = "CheckMortality";
|
||||
type AccountId = u64;
|
||||
type Call = ();
|
||||
type AdditionalSigned = T::Hash;
|
||||
@@ -167,12 +166,13 @@ where
|
||||
}
|
||||
|
||||
/// Nonce check and increment to give replay protection for transactions.
|
||||
#[derive(Encode, Decode, Clone, Eq, PartialEq, Debug)]
|
||||
pub struct CheckNonce<T: System>(#[codec(compact)] pub T::Index);
|
||||
#[derive(Encode, Decode, Clone, Eq, PartialEq, Debug, TypeInfo)]
|
||||
#[scale_info(skip_type_params(T))]
|
||||
pub struct CheckNonce<T: Config>(#[codec(compact)] pub T::Index);
|
||||
|
||||
impl<T> SignedExtension for CheckNonce<T>
|
||||
where
|
||||
T: System + Clone + Debug + Eq + Send + Sync,
|
||||
T: Config + Clone + Debug + Eq + Send + Sync,
|
||||
{
|
||||
const IDENTIFIER: &'static str = "CheckNonce";
|
||||
type AccountId = u64;
|
||||
@@ -187,12 +187,13 @@ where
|
||||
}
|
||||
|
||||
/// Resource limit check.
|
||||
#[derive(Encode, Decode, Clone, Eq, PartialEq, Debug)]
|
||||
pub struct CheckWeight<T: System>(pub PhantomData<T>);
|
||||
#[derive(Encode, Decode, Clone, Eq, PartialEq, Debug, TypeInfo)]
|
||||
#[scale_info(skip_type_params(T))]
|
||||
pub struct CheckWeight<T: Config>(pub PhantomData<T>);
|
||||
|
||||
impl<T> SignedExtension for CheckWeight<T>
|
||||
where
|
||||
T: System + Clone + Debug + Eq + Send + Sync,
|
||||
T: Config + Clone + Debug + Eq + Send + Sync,
|
||||
{
|
||||
const IDENTIFIER: &'static str = "CheckWeight";
|
||||
type AccountId = u64;
|
||||
@@ -208,13 +209,11 @@ where
|
||||
|
||||
/// Require the transactor pay for themselves and maybe include a tip to gain additional priority
|
||||
/// in the queue.
|
||||
#[derive(Encode, Decode, Clone, Eq, PartialEq, Debug)]
|
||||
pub struct ChargeTransactionPayment<T: Balances>(#[codec(compact)] pub T::Balance);
|
||||
#[derive(Encode, Decode, Clone, Eq, PartialEq, Debug, TypeInfo)]
|
||||
#[scale_info(skip_type_params(T))]
|
||||
pub struct ChargeTransactionPayment(#[codec(compact)] pub u128);
|
||||
|
||||
impl<T> SignedExtension for ChargeTransactionPayment<T>
|
||||
where
|
||||
T: Balances + Clone + Debug + Eq + Send + Sync,
|
||||
{
|
||||
impl SignedExtension for ChargeTransactionPayment {
|
||||
const IDENTIFIER: &'static str = "ChargeTransactionPayment";
|
||||
type AccountId = u64;
|
||||
type Call = ();
|
||||
@@ -228,7 +227,7 @@ where
|
||||
}
|
||||
|
||||
/// Trait for implementing transaction extras for a runtime.
|
||||
pub trait SignedExtra<T: System>: SignedExtension {
|
||||
pub trait SignedExtra<T: Config>: SignedExtension {
|
||||
/// The type the extras.
|
||||
type Extra: SignedExtension + Send + Sync;
|
||||
|
||||
@@ -245,25 +244,24 @@ pub trait SignedExtra<T: System>: SignedExtension {
|
||||
}
|
||||
|
||||
/// Default `SignedExtra` for substrate runtimes.
|
||||
#[derive(Encode, Decode, Clone, Eq, PartialEq, Debug)]
|
||||
pub struct DefaultExtra<T: System> {
|
||||
#[derive(Encode, Decode, Clone, Eq, PartialEq, Debug, TypeInfo)]
|
||||
#[scale_info(skip_type_params(T))]
|
||||
pub struct DefaultExtra<T: Config> {
|
||||
spec_version: u32,
|
||||
tx_version: u32,
|
||||
nonce: T::Index,
|
||||
genesis_hash: T::Hash,
|
||||
}
|
||||
|
||||
impl<T: System + Balances + Clone + Debug + Eq + Send + Sync> SignedExtra<T>
|
||||
for DefaultExtra<T>
|
||||
{
|
||||
impl<T: Config + Clone + Debug + Eq + Send + Sync> SignedExtra<T> for DefaultExtra<T> {
|
||||
type Extra = (
|
||||
CheckSpecVersion<T>,
|
||||
CheckTxVersion<T>,
|
||||
CheckGenesis<T>,
|
||||
CheckEra<T>,
|
||||
CheckMortality<T>,
|
||||
CheckNonce<T>,
|
||||
CheckWeight<T>,
|
||||
ChargeTransactionPayment<T>,
|
||||
ChargeTransactionPayment,
|
||||
);
|
||||
|
||||
fn new(
|
||||
@@ -285,17 +283,15 @@ impl<T: System + Balances + Clone + Debug + Eq + Send + Sync> SignedExtra<T>
|
||||
CheckSpecVersion(PhantomData, self.spec_version),
|
||||
CheckTxVersion(PhantomData, self.tx_version),
|
||||
CheckGenesis(PhantomData, self.genesis_hash),
|
||||
CheckEra((Era::Immortal, PhantomData), self.genesis_hash),
|
||||
CheckMortality((Era::Immortal, PhantomData), self.genesis_hash),
|
||||
CheckNonce(self.nonce),
|
||||
CheckWeight(PhantomData),
|
||||
ChargeTransactionPayment(<T as Balances>::Balance::default()),
|
||||
ChargeTransactionPayment(u128::default()),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: System + Balances + Clone + Debug + Eq + Send + Sync> SignedExtension
|
||||
for DefaultExtra<T>
|
||||
{
|
||||
impl<T: Config + Clone + Debug + Eq + Send + Sync> SignedExtension for DefaultExtra<T> {
|
||||
const IDENTIFIER: &'static str = "DefaultExtra";
|
||||
type AccountId = T::AccountId;
|
||||
type Call = ();
|
||||
|
||||
+20
-21
@@ -1,5 +1,5 @@
|
||||
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
|
||||
// This file is part of substrate-subxt.
|
||||
// This file is part of subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
@@ -12,7 +12,7 @@
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with substrate-subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Create signed or unsigned extrinsics.
|
||||
|
||||
@@ -22,14 +22,13 @@ mod signer;
|
||||
pub use self::{
|
||||
extra::{
|
||||
ChargeTransactionPayment,
|
||||
CheckEra,
|
||||
CheckGenesis,
|
||||
CheckMortality,
|
||||
CheckNonce,
|
||||
CheckSpecVersion,
|
||||
CheckTxVersion,
|
||||
CheckWeight,
|
||||
DefaultExtra,
|
||||
Extra,
|
||||
SignedExtra,
|
||||
},
|
||||
signer::{
|
||||
@@ -42,22 +41,25 @@ use sp_runtime::traits::SignedExtension;
|
||||
use sp_version::RuntimeVersion;
|
||||
|
||||
use crate::{
|
||||
frame::system::System,
|
||||
runtimes::Runtime,
|
||||
Config,
|
||||
Encoded,
|
||||
Error,
|
||||
ExtrinsicExtraData,
|
||||
};
|
||||
|
||||
/// UncheckedExtrinsic type.
|
||||
pub type UncheckedExtrinsic<T> = sp_runtime::generic::UncheckedExtrinsic<
|
||||
<T as System>::Address,
|
||||
<T as Config>::Address,
|
||||
Encoded,
|
||||
<T as Runtime>::Signature,
|
||||
Extra<T>,
|
||||
<T as Config>::Signature,
|
||||
<<T as ExtrinsicExtraData<T>>::Extra as SignedExtra<T>>::Extra,
|
||||
>;
|
||||
|
||||
/// SignedPayload type.
|
||||
pub type SignedPayload<T> = sp_runtime::generic::SignedPayload<Encoded, Extra<T>>;
|
||||
pub type SignedPayload<T> = sp_runtime::generic::SignedPayload<
|
||||
Encoded,
|
||||
<<T as ExtrinsicExtraData<T>>::Extra as SignedExtra<T>>::Extra,
|
||||
>;
|
||||
|
||||
/// Creates a signed extrinsic
|
||||
pub async fn create_signed<T>(
|
||||
@@ -68,22 +70,19 @@ pub async fn create_signed<T>(
|
||||
signer: &(dyn Signer<T> + Send + Sync),
|
||||
) -> Result<UncheckedExtrinsic<T>, Error>
|
||||
where
|
||||
T: Runtime,
|
||||
<<T::Extra as SignedExtra<T>>::Extra as SignedExtension>::AdditionalSigned:
|
||||
T: Config + ExtrinsicExtraData<T>,
|
||||
<<<T as ExtrinsicExtraData<T>>::Extra as SignedExtra<T>>::Extra as SignedExtension>::AdditionalSigned:
|
||||
Send + Sync,
|
||||
{
|
||||
let spec_version = runtime_version.spec_version;
|
||||
let tx_version = runtime_version.transaction_version;
|
||||
let extra: T::Extra = T::Extra::new(spec_version, tx_version, nonce, genesis_hash);
|
||||
let extra = <T as ExtrinsicExtraData<T>>::Extra::new(
|
||||
spec_version,
|
||||
tx_version,
|
||||
nonce,
|
||||
genesis_hash,
|
||||
);
|
||||
let payload = SignedPayload::<T>::new(call, extra.extra())?;
|
||||
let signed = signer.sign(payload).await?;
|
||||
Ok(signed)
|
||||
}
|
||||
|
||||
/// Creates an unsigned extrinsic
|
||||
pub fn create_unsigned<T>(call: Encoded) -> UncheckedExtrinsic<T>
|
||||
where
|
||||
T: Runtime,
|
||||
{
|
||||
UncheckedExtrinsic::<T>::new_unsigned(call)
|
||||
}
|
||||
|
||||
+12
-9
@@ -1,5 +1,5 @@
|
||||
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
|
||||
// This file is part of substrate-subxt.
|
||||
// This file is part of subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
@@ -12,17 +12,20 @@
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with substrate-subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! A library to **sub**mit e**xt**rinsics to a
|
||||
//! [substrate](https://github.com/paritytech/substrate) node via RPC.
|
||||
|
||||
use super::{
|
||||
SignedExtra,
|
||||
SignedPayload,
|
||||
UncheckedExtrinsic,
|
||||
};
|
||||
use crate::runtimes::Runtime;
|
||||
use crate::{
|
||||
Config,
|
||||
ExtrinsicExtraData,
|
||||
SignedExtra,
|
||||
};
|
||||
use codec::Encode;
|
||||
use sp_core::Pair;
|
||||
use sp_runtime::traits::{
|
||||
@@ -33,7 +36,7 @@ use sp_runtime::traits::{
|
||||
|
||||
/// Extrinsic signer.
|
||||
#[async_trait::async_trait]
|
||||
pub trait Signer<T: Runtime> {
|
||||
pub trait Signer<T: Config + ExtrinsicExtraData<T>> {
|
||||
/// Returns the account id.
|
||||
fn account_id(&self) -> &T::AccountId;
|
||||
|
||||
@@ -52,7 +55,7 @@ pub trait Signer<T: Runtime> {
|
||||
|
||||
/// Extrinsic signer using a private key.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct PairSigner<T: Runtime, P: Pair> {
|
||||
pub struct PairSigner<T: Config, P: Pair> {
|
||||
account_id: T::AccountId,
|
||||
nonce: Option<T::Index>,
|
||||
signer: P,
|
||||
@@ -60,7 +63,7 @@ pub struct PairSigner<T: Runtime, P: Pair> {
|
||||
|
||||
impl<T, P> PairSigner<T, P>
|
||||
where
|
||||
T: Runtime,
|
||||
T: Config + ExtrinsicExtraData<T>,
|
||||
T::Signature: From<P::Signature>,
|
||||
<T::Signature as Verify>::Signer:
|
||||
From<P::Public> + IdentifyAccount<AccountId = T::AccountId>,
|
||||
@@ -96,9 +99,9 @@ where
|
||||
#[async_trait::async_trait]
|
||||
impl<T, P> Signer<T> for PairSigner<T, P>
|
||||
where
|
||||
T: Runtime,
|
||||
T: Config + ExtrinsicExtraData<T>,
|
||||
T::AccountId: Into<T::Address> + 'static,
|
||||
<<T::Extra as SignedExtra<T>>::Extra as SignedExtension>::AdditionalSigned: Send,
|
||||
<<<T as ExtrinsicExtraData<T>>::Extra as SignedExtra<T>>::Extra as SignedExtension>::AdditionalSigned: Send + Sync + 'static,
|
||||
P: Pair + 'static,
|
||||
P::Signature: Into<T::Signature> + 'static,
|
||||
{
|
||||
|
||||
@@ -1,317 +0,0 @@
|
||||
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
|
||||
// This file is part of substrate-subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// subxt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with substrate-subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Implements support for the pallet_balances module.
|
||||
|
||||
use crate::frame::system::System;
|
||||
use codec::{
|
||||
Decode,
|
||||
Encode,
|
||||
};
|
||||
use core::marker::PhantomData;
|
||||
use frame_support::{
|
||||
traits::LockIdentifier,
|
||||
Parameter,
|
||||
};
|
||||
use sp_runtime::traits::{
|
||||
AtLeast32Bit,
|
||||
MaybeSerialize,
|
||||
Member,
|
||||
};
|
||||
use std::fmt::Debug;
|
||||
|
||||
/// The subset of the `pallet_balances::Trait` that a client must implement.
|
||||
#[module]
|
||||
pub trait Balances: System {
|
||||
/// The balance of an account.
|
||||
type Balance: Parameter
|
||||
+ Member
|
||||
+ AtLeast32Bit
|
||||
+ codec::Codec
|
||||
+ Default
|
||||
+ Copy
|
||||
+ MaybeSerialize
|
||||
+ Debug
|
||||
+ From<<Self as System>::BlockNumber>;
|
||||
}
|
||||
|
||||
/// All balance information for an account.
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Default, Decode, Encode)]
|
||||
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.
|
||||
pub free: Balance,
|
||||
/// Balance which is reserved and may not be used at all.
|
||||
///
|
||||
/// 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,
|
||||
}
|
||||
|
||||
/// The total issuance of the balances module.
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Store, Encode)]
|
||||
pub struct TotalIssuanceStore<T: Balances> {
|
||||
#[store(returns = T::Balance)]
|
||||
/// Runtime marker.
|
||||
pub _runtime: PhantomData<T>,
|
||||
}
|
||||
|
||||
/// The locks of the balances module.
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Store, Encode, Decode)]
|
||||
pub struct LocksStore<'a, T: Balances> {
|
||||
#[store(returns = Vec<BalanceLock<T::Balance>>)]
|
||||
/// Account to retrieve the balance locks for.
|
||||
pub account_id: &'a T::AccountId,
|
||||
}
|
||||
|
||||
/// A single lock on a balance. There can be many of these on an account and they "overlap", so the
|
||||
/// same balance is frozen by multiple locks.
|
||||
#[derive(Clone, PartialEq, Eq, Encode, Decode)]
|
||||
pub struct BalanceLock<Balance> {
|
||||
/// An identifier for this lock. Only one lock may be in existence for each identifier.
|
||||
pub id: LockIdentifier,
|
||||
/// The amount which the free balance may not drop below when this lock is in effect.
|
||||
pub amount: Balance,
|
||||
/// If true, then the lock remains in effect even for payment of transaction fees.
|
||||
pub reasons: Reasons,
|
||||
}
|
||||
|
||||
impl<Balance: Debug> Debug for BalanceLock<Balance> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
f.debug_struct("BalanceLock")
|
||||
.field("id", &String::from_utf8_lossy(&self.id))
|
||||
.field("amount", &self.amount)
|
||||
.field("reasons", &self.reasons)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
/// Simplified reasons for withdrawing balance.
|
||||
#[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, Debug)]
|
||||
pub enum Reasons {
|
||||
/// Paying system transaction fees.
|
||||
Fee,
|
||||
/// Any reason other than paying system transaction fees.
|
||||
Misc,
|
||||
/// Any reason at all.
|
||||
All,
|
||||
}
|
||||
|
||||
/// Transfer some liquid free balance to another account.
|
||||
///
|
||||
/// `transfer` will set the `FreeBalance` of the sender and receiver.
|
||||
/// It will decrease the total issuance of the system by the `TransferFee`.
|
||||
/// If the sender's account is below the existential deposit as a result
|
||||
/// of the transfer, the account will be reaped.
|
||||
#[derive(Clone, Debug, PartialEq, Call, Encode)]
|
||||
pub struct TransferCall<'a, T: Balances> {
|
||||
/// Destination of the transfer.
|
||||
pub to: &'a <T as System>::Address,
|
||||
/// Amount to transfer.
|
||||
#[codec(compact)]
|
||||
pub amount: T::Balance,
|
||||
}
|
||||
|
||||
/// Transfer event.
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Event, Decode)]
|
||||
pub struct TransferEvent<T: Balances> {
|
||||
/// Account balance was transfered from.
|
||||
pub from: <T as System>::AccountId,
|
||||
/// Account balance was transfered to.
|
||||
pub to: <T as System>::AccountId,
|
||||
/// Amount of balance that was transfered.
|
||||
pub amount: T::Balance,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{
|
||||
error::{
|
||||
Error,
|
||||
ModuleError,
|
||||
RuntimeError,
|
||||
},
|
||||
extrinsic::{
|
||||
PairSigner,
|
||||
Signer,
|
||||
},
|
||||
subscription::EventSubscription,
|
||||
system::AccountStoreExt,
|
||||
tests::{
|
||||
test_node_process,
|
||||
TestRuntime,
|
||||
},
|
||||
};
|
||||
use sp_core::{
|
||||
sr25519::Pair,
|
||||
Pair as _,
|
||||
};
|
||||
use sp_keyring::AccountKeyring;
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_basic_transfer() {
|
||||
env_logger::try_init().ok();
|
||||
let alice = PairSigner::<TestRuntime, _>::new(AccountKeyring::Alice.pair());
|
||||
let bob = PairSigner::<TestRuntime, _>::new(AccountKeyring::Bob.pair());
|
||||
let bob_address = bob.account_id().clone().into();
|
||||
let test_node_proc = test_node_process().await;
|
||||
let client = test_node_proc.client();
|
||||
|
||||
let alice_pre = client.account(alice.account_id(), None).await.unwrap();
|
||||
let bob_pre = client.account(bob.account_id(), None).await.unwrap();
|
||||
|
||||
let event = client
|
||||
.transfer_and_watch(&alice, &bob_address, 10_000)
|
||||
.await
|
||||
.expect("sending an xt works")
|
||||
.transfer()
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let expected_event = TransferEvent {
|
||||
from: alice.account_id().clone(),
|
||||
to: bob.account_id().clone(),
|
||||
amount: 10_000,
|
||||
};
|
||||
assert_eq!(event, expected_event);
|
||||
|
||||
let alice_post = client.account(alice.account_id(), None).await.unwrap();
|
||||
let bob_post = client.account(bob.account_id(), None).await.unwrap();
|
||||
|
||||
assert!(alice_pre.data.free - 10_000 >= alice_post.data.free);
|
||||
assert_eq!(bob_pre.data.free + 10_000, bob_post.data.free);
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_state_total_issuance() {
|
||||
env_logger::try_init().ok();
|
||||
let test_node_proc = test_node_process().await;
|
||||
let client = test_node_proc.client();
|
||||
let total_issuance = client.total_issuance(None).await.unwrap();
|
||||
assert_ne!(total_issuance, 0);
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_state_read_free_balance() {
|
||||
env_logger::try_init().ok();
|
||||
let test_node_proc = test_node_process().await;
|
||||
let client = test_node_proc.client();
|
||||
let account = AccountKeyring::Alice.to_account_id();
|
||||
let info = client.account(&account, None).await.unwrap();
|
||||
assert_ne!(info.data.free, 0);
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_state_balance_lock() -> Result<(), crate::Error> {
|
||||
use crate::frame::staking::{
|
||||
BondCallExt,
|
||||
RewardDestination,
|
||||
};
|
||||
|
||||
env_logger::try_init().ok();
|
||||
let bob = PairSigner::<TestRuntime, _>::new(AccountKeyring::Bob.pair());
|
||||
let test_node_proc = test_node_process().await;
|
||||
let client = test_node_proc.client();
|
||||
|
||||
client
|
||||
.bond_and_watch(
|
||||
&bob,
|
||||
&AccountKeyring::Charlie.to_account_id().into(),
|
||||
100_000_000_000_000,
|
||||
RewardDestination::Stash,
|
||||
)
|
||||
.await?;
|
||||
|
||||
let locks = client
|
||||
.locks(&AccountKeyring::Bob.to_account_id(), None)
|
||||
.await?;
|
||||
|
||||
assert_eq!(
|
||||
locks,
|
||||
vec![BalanceLock {
|
||||
id: *b"staking ",
|
||||
amount: 100_000_000_000_000,
|
||||
reasons: Reasons::All,
|
||||
}]
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_transfer_error() {
|
||||
env_logger::try_init().ok();
|
||||
let alice = PairSigner::<TestRuntime, _>::new(AccountKeyring::Alice.pair());
|
||||
let alice_addr = alice.account_id().clone().into();
|
||||
let hans = PairSigner::<TestRuntime, _>::new(Pair::generate().0);
|
||||
let hans_address = hans.account_id().clone().into();
|
||||
let test_node_proc = test_node_process().await;
|
||||
let client = test_node_proc.client();
|
||||
client
|
||||
.transfer_and_watch(&alice, &hans_address, 100_000_000_000_000_000)
|
||||
.await
|
||||
.unwrap();
|
||||
let res = client
|
||||
.transfer_and_watch(&hans, &alice_addr, 100_000_000_000_000_000)
|
||||
.await;
|
||||
|
||||
if let Err(Error::Runtime(RuntimeError::Module(error))) = res {
|
||||
let error2 = ModuleError {
|
||||
module: "Balances".into(),
|
||||
error: "InsufficientBalance".into(),
|
||||
};
|
||||
assert_eq!(error, error2);
|
||||
} else {
|
||||
panic!("expected an error");
|
||||
}
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_transfer_subscription() {
|
||||
env_logger::try_init().ok();
|
||||
let alice = PairSigner::<TestRuntime, _>::new(AccountKeyring::Alice.pair());
|
||||
let bob = AccountKeyring::Bob.to_account_id();
|
||||
let bob_addr = bob.clone().into();
|
||||
let test_node_proc = test_node_process().await;
|
||||
let client = test_node_proc.client();
|
||||
let sub = client.subscribe_events().await.unwrap();
|
||||
let decoder = client.events_decoder();
|
||||
let mut sub = EventSubscription::<TestRuntime>::new(sub, &decoder);
|
||||
sub.filter_event::<TransferEvent<_>>();
|
||||
client.transfer(&alice, &bob_addr, 10_000).await.unwrap();
|
||||
let raw = sub.next().await.unwrap().unwrap();
|
||||
let event = TransferEvent::<TestRuntime>::decode(&mut &raw.data[..]).unwrap();
|
||||
assert_eq!(
|
||||
event,
|
||||
TransferEvent {
|
||||
from: alice.account_id().clone(),
|
||||
to: bob.clone(),
|
||||
amount: 10_000,
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,293 +0,0 @@
|
||||
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
|
||||
// This file is part of substrate-subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// subxt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with substrate-subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Implements support for the pallet_contracts module.
|
||||
|
||||
use crate::frame::{
|
||||
balances::Balances,
|
||||
system::System,
|
||||
};
|
||||
use codec::{
|
||||
Decode,
|
||||
Encode,
|
||||
};
|
||||
|
||||
/// Gas units are chosen to be represented by u64 so that gas metering
|
||||
/// instructions can operate on them efficiently.
|
||||
pub type Gas = u64;
|
||||
|
||||
/// The subset of the `pallet_contracts::Trait` that a client must implement.
|
||||
#[module]
|
||||
pub trait Contracts: System + Balances {}
|
||||
|
||||
/// Instantiates a new contract from the supplied `code` optionally transferring
|
||||
/// some balance.
|
||||
///
|
||||
/// This is the only function that can deploy new code to the chain.
|
||||
///
|
||||
/// Instantiation is executed as follows:
|
||||
///
|
||||
/// - The supplied `code` is instrumented, deployed, and a `code_hash` is created for that code.
|
||||
/// - If the `code_hash` already exists on the chain the underlying `code` will be shared.
|
||||
/// - The destination address is computed based on the sender, code_hash and the salt.
|
||||
/// - The smart-contract account is created at the computed address.
|
||||
/// - The `endowment` is transferred to the new account.
|
||||
/// - The `deploy` function is executed in the context of the newly-created account.
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Call, Encode)]
|
||||
pub struct InstantiateWithCodeCall<'a, T: Contracts> {
|
||||
/// The balance to transfer from the `origin` to the newly created contract.
|
||||
#[codec(compact)]
|
||||
pub endowment: <T as Balances>::Balance,
|
||||
/// The gas limit enforced when executing the constructor.
|
||||
#[codec(compact)]
|
||||
pub gas_limit: Gas,
|
||||
/// The contract code to deploy in raw bytes.
|
||||
pub code: &'a [u8],
|
||||
/// The input data to pass to the contract constructor.
|
||||
pub data: &'a [u8],
|
||||
/// Used for the address derivation.
|
||||
pub salt: &'a [u8],
|
||||
}
|
||||
|
||||
/// Instantiates a contract from a previously deployed wasm binary.
|
||||
///
|
||||
/// This function is identical to [`InstantiateWithCodeCall`] but without the
|
||||
/// code deployment step. Instead, the `code_hash` of an on-chain deployed wasm binary
|
||||
/// must be supplied.
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Call, Encode)]
|
||||
pub struct InstantiateCall<'a, T: Contracts> {
|
||||
/// The balance to transfer from the `origin` to the newly created contract.
|
||||
#[codec(compact)]
|
||||
pub endowment: <T as Balances>::Balance,
|
||||
/// The gas limit enforced when executing the constructor.
|
||||
#[codec(compact)]
|
||||
pub gas_limit: Gas,
|
||||
/// Code hash of the already deployed on-chain deployed wasm binary.
|
||||
pub code_hash: &'a <T as System>::Hash,
|
||||
/// Data to initialize the contract with.
|
||||
pub data: &'a [u8],
|
||||
/// Used for the address derivation.
|
||||
pub salt: &'a [u8],
|
||||
}
|
||||
|
||||
/// Makes a call to an account, optionally transferring some balance.
|
||||
///
|
||||
/// * If the account is a smart-contract account, the associated code will be
|
||||
/// executed and any value will be transferred.
|
||||
/// * If the account is a regular account, any value will be transferred.
|
||||
/// * If no account exists and the call value is not less than `existential_deposit`,
|
||||
/// a regular account will be created and any value will be transferred.
|
||||
#[derive(Clone, Debug, PartialEq, Call, Encode)]
|
||||
pub struct CallCall<'a, T: Contracts> {
|
||||
/// Address of the contract.
|
||||
pub dest: &'a <T as System>::Address,
|
||||
/// Value to transfer to the contract.
|
||||
#[codec(compact)]
|
||||
pub value: <T as Balances>::Balance,
|
||||
/// Gas limit.
|
||||
#[codec(compact)]
|
||||
pub gas_limit: Gas,
|
||||
/// Data to send to the contract.
|
||||
pub data: &'a [u8],
|
||||
}
|
||||
|
||||
/// Code stored event.
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Event, Decode)]
|
||||
pub struct CodeStoredEvent<T: Contracts> {
|
||||
/// Code hash of the contract.
|
||||
pub code_hash: T::Hash,
|
||||
}
|
||||
|
||||
/// Instantiated event.
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Event, Decode)]
|
||||
pub struct InstantiatedEvent<T: Contracts> {
|
||||
/// Caller that instantiated the contract.
|
||||
pub caller: <T as System>::AccountId,
|
||||
/// The address of the contract.
|
||||
pub contract: <T as System>::AccountId,
|
||||
}
|
||||
|
||||
/// Contract execution event.
|
||||
///
|
||||
/// Emitted upon successful execution of a contract, if any contract events were produced.
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Event, Decode)]
|
||||
pub struct ContractExecutionEvent<T: Contracts> {
|
||||
/// Caller of the contract.
|
||||
pub caller: <T as System>::AccountId,
|
||||
/// SCALE encoded contract event data.
|
||||
pub data: Vec<u8>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use sp_keyring::AccountKeyring;
|
||||
|
||||
use super::*;
|
||||
use crate::{
|
||||
tests::{
|
||||
test_node_process,
|
||||
TestNodeProcess,
|
||||
TestRuntime,
|
||||
},
|
||||
Client,
|
||||
Error,
|
||||
ExtrinsicSuccess,
|
||||
PairSigner,
|
||||
};
|
||||
use sp_core::sr25519::Pair;
|
||||
|
||||
struct TestContext {
|
||||
node_process: TestNodeProcess<TestRuntime>,
|
||||
signer: PairSigner<TestRuntime, Pair>,
|
||||
}
|
||||
|
||||
impl TestContext {
|
||||
async fn init() -> Self {
|
||||
env_logger::try_init().ok();
|
||||
|
||||
let node_process = test_node_process().await;
|
||||
let signer = PairSigner::new(AccountKeyring::Alice.pair());
|
||||
|
||||
TestContext {
|
||||
node_process,
|
||||
signer,
|
||||
}
|
||||
}
|
||||
|
||||
async fn instantiate_with_code(
|
||||
&self,
|
||||
) -> Result<CodeStoredEvent<TestRuntime>, Error> {
|
||||
const CONTRACT: &str = r#"
|
||||
(module
|
||||
(func (export "call"))
|
||||
(func (export "deploy"))
|
||||
)
|
||||
"#;
|
||||
let code = wabt::wat2wasm(CONTRACT).expect("invalid wabt");
|
||||
|
||||
let result = self
|
||||
.client()
|
||||
.instantiate_with_code_and_watch(
|
||||
&self.signer,
|
||||
100_000_000_000_000_000, // endowment
|
||||
500_000_000_000, // gas_limit
|
||||
&code,
|
||||
&[], // data
|
||||
&[], // salt
|
||||
)
|
||||
.await?;
|
||||
let event = result.code_stored()?.ok_or_else(|| {
|
||||
Error::Other("Failed to find a CodeStored event".into())
|
||||
})?;
|
||||
log::info!("Code hash: {:?}", event.code_hash);
|
||||
Ok(event)
|
||||
}
|
||||
|
||||
async fn instantiate(
|
||||
&self,
|
||||
code_hash: &<TestRuntime as System>::Hash,
|
||||
data: &[u8],
|
||||
salt: &[u8],
|
||||
) -> Result<InstantiatedEvent<TestRuntime>, Error> {
|
||||
// call instantiate extrinsic
|
||||
let result = self
|
||||
.client()
|
||||
.instantiate_and_watch(
|
||||
&self.signer,
|
||||
100_000_000_000_000_000, // endowment
|
||||
500_000_000_000, // gas_limit
|
||||
code_hash,
|
||||
data,
|
||||
salt,
|
||||
)
|
||||
.await?;
|
||||
|
||||
log::info!("Instantiate result: {:?}", result);
|
||||
let instantiated = result.instantiated()?.ok_or_else(|| {
|
||||
Error::Other("Failed to find a Instantiated event".into())
|
||||
})?;
|
||||
|
||||
Ok(instantiated)
|
||||
}
|
||||
|
||||
async fn call(
|
||||
&self,
|
||||
contract: &<TestRuntime as System>::Address,
|
||||
input_data: &[u8],
|
||||
) -> Result<ExtrinsicSuccess<TestRuntime>, Error> {
|
||||
let result = self
|
||||
.client()
|
||||
.call_and_watch(
|
||||
&self.signer,
|
||||
contract,
|
||||
0, // value
|
||||
500_000_000, // gas_limit
|
||||
input_data,
|
||||
)
|
||||
.await?;
|
||||
log::info!("Call result: {:?}", result);
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
fn client(&self) -> &Client<TestRuntime> {
|
||||
self.node_process.client()
|
||||
}
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn tx_instantiate_with_code() {
|
||||
let ctx = TestContext::init().await;
|
||||
let code_stored = ctx.instantiate_with_code().await;
|
||||
|
||||
assert!(
|
||||
code_stored.is_ok(),
|
||||
format!(
|
||||
"Error calling instantiate_with_code and receiving CodeStored Event: {:?}",
|
||||
code_stored
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn tx_instantiate() {
|
||||
let ctx = TestContext::init().await;
|
||||
let code_stored = ctx.instantiate_with_code().await.unwrap();
|
||||
|
||||
let instantiated = ctx.instantiate(&code_stored.code_hash, &[], &[1u8]).await;
|
||||
|
||||
assert!(
|
||||
instantiated.is_ok(),
|
||||
format!("Error instantiating contract: {:?}", instantiated)
|
||||
);
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn tx_call() {
|
||||
let ctx = TestContext::init().await;
|
||||
let code_stored = ctx.instantiate_with_code().await.unwrap();
|
||||
|
||||
let instantiated = ctx
|
||||
.instantiate(&code_stored.code_hash.into(), &[], &[1u8])
|
||||
.await
|
||||
.unwrap();
|
||||
let executed = ctx.call(&instantiated.contract.into(), &[]).await;
|
||||
|
||||
assert!(
|
||||
executed.is_ok(),
|
||||
format!("Error calling contract: {:?}", executed)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
|
||||
// This file is part of substrate-subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// subxt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with substrate-subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Implements support for built-in runtime modules.
|
||||
|
||||
use crate::metadata::{
|
||||
Metadata,
|
||||
MetadataError,
|
||||
};
|
||||
use codec::{
|
||||
Decode,
|
||||
Encode,
|
||||
};
|
||||
use sp_core::storage::StorageKey;
|
||||
|
||||
pub mod balances;
|
||||
pub mod contracts;
|
||||
pub mod session;
|
||||
pub mod staking;
|
||||
pub mod sudo;
|
||||
pub mod system;
|
||||
|
||||
/// Store trait.
|
||||
pub trait Store<T>: Encode {
|
||||
/// Module name.
|
||||
const MODULE: &'static str;
|
||||
/// Field name.
|
||||
const FIELD: &'static str;
|
||||
/// Return type.
|
||||
type Returns: Decode;
|
||||
/// Returns the key prefix for storage maps
|
||||
fn prefix(metadata: &Metadata) -> Result<StorageKey, MetadataError>;
|
||||
/// Returns the `StorageKey`.
|
||||
fn key(&self, metadata: &Metadata) -> Result<StorageKey, MetadataError>;
|
||||
/// Returns the default value.
|
||||
fn default(&self, metadata: &Metadata) -> Result<Self::Returns, MetadataError> {
|
||||
Ok(metadata
|
||||
.module(Self::MODULE)?
|
||||
.storage(Self::FIELD)?
|
||||
.default()?)
|
||||
}
|
||||
}
|
||||
|
||||
/// Call trait.
|
||||
pub trait Call<T>: Encode {
|
||||
/// Module name.
|
||||
const MODULE: &'static str;
|
||||
/// Function name.
|
||||
const FUNCTION: &'static str;
|
||||
}
|
||||
|
||||
/// Event trait.
|
||||
pub trait Event<T>: Decode {
|
||||
/// Module name.
|
||||
const MODULE: &'static str;
|
||||
/// Event name.
|
||||
const EVENT: &'static str;
|
||||
}
|
||||
@@ -1,100 +0,0 @@
|
||||
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
|
||||
// This file is part of substrate-subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// subxt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with substrate-subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Session support
|
||||
use crate::frame::{
|
||||
balances::Balances,
|
||||
system::System,
|
||||
};
|
||||
use codec::Encode;
|
||||
use frame_support::Parameter;
|
||||
use sp_runtime::traits::{
|
||||
Member,
|
||||
OpaqueKeys,
|
||||
};
|
||||
use std::{
|
||||
fmt::Debug,
|
||||
marker::PhantomData,
|
||||
};
|
||||
use substrate_subxt_proc_macro::Store;
|
||||
|
||||
/// Impls `Default::default` for some types that have a `_runtime` field of type
|
||||
/// `PhantomData` as their only field.
|
||||
macro_rules! default_impl {
|
||||
($name:ident) => {
|
||||
impl<T: Session> Default for $name<T> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
_runtime: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
type IdentificationTuple<T> = (
|
||||
<T as Session>::ValidatorId,
|
||||
pallet_staking::Exposure<<T as System>::AccountId, <T as Balances>::Balance>,
|
||||
);
|
||||
|
||||
/// The trait needed for this module.
|
||||
#[module]
|
||||
pub trait Session: System + Balances {
|
||||
#![event_alias(IdentificationTuple = IdentificationTuple<T>)]
|
||||
#![event_alias(OpaqueTimeSlot = Vec<u8>)]
|
||||
#![event_alias(SessionIndex = u32)]
|
||||
|
||||
/// The validator account identifier type for the runtime.
|
||||
type ValidatorId: Parameter + Debug + Ord + Default + Send + Sync + 'static;
|
||||
|
||||
/// The keys.
|
||||
type Keys: OpaqueKeys + Member + Parameter + Default;
|
||||
}
|
||||
|
||||
/// The current set of validators.
|
||||
#[derive(Encode, Store, Debug)]
|
||||
pub struct ValidatorsStore<T: Session> {
|
||||
#[store(returns = Vec<<T as Session>::ValidatorId>)]
|
||||
/// Marker for the runtime
|
||||
pub _runtime: PhantomData<T>,
|
||||
}
|
||||
|
||||
/// The queued keys for the next session.
|
||||
#[derive(Encode, Store, Debug)]
|
||||
pub struct QueuedKeysStore<T: Session> {
|
||||
#[store(returns = Vec<(<T as Session>::ValidatorId, T::Keys)>)]
|
||||
/// Marker for the runtime
|
||||
pub _runtime: PhantomData<T>,
|
||||
}
|
||||
|
||||
/// The next session keys for a validator.
|
||||
#[derive(Encode, Store, Debug)]
|
||||
pub struct NextKeysStore<'a, T: Session> {
|
||||
#[store(returns = Option<<T as Session>::Keys>)]
|
||||
/// The validator account.
|
||||
pub validator_id: &'a <T as Session>::ValidatorId,
|
||||
}
|
||||
|
||||
default_impl!(ValidatorsStore);
|
||||
|
||||
/// Set the session keys for a validator.
|
||||
#[derive(Encode, Call, Debug)]
|
||||
pub struct SetKeysCall<T: Session> {
|
||||
/// The keys
|
||||
pub keys: T::Keys,
|
||||
/// The proof. This is not currently used and can be set to an empty vector.
|
||||
pub proof: Vec<u8>,
|
||||
}
|
||||
@@ -1,435 +0,0 @@
|
||||
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
|
||||
// This file is part of substrate-subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// subxt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with substrate-subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Implements support for the pallet_staking module.
|
||||
|
||||
use super::balances::Balances;
|
||||
use codec::{
|
||||
Decode,
|
||||
Encode,
|
||||
};
|
||||
|
||||
use std::{
|
||||
collections::BTreeMap,
|
||||
fmt::Debug,
|
||||
marker::PhantomData,
|
||||
};
|
||||
|
||||
pub use pallet_staking::{
|
||||
ActiveEraInfo,
|
||||
EraIndex,
|
||||
Exposure,
|
||||
Nominations,
|
||||
RewardDestination,
|
||||
RewardPoint,
|
||||
StakingLedger,
|
||||
ValidatorPrefs,
|
||||
};
|
||||
|
||||
/// Rewards for the last `HISTORY_DEPTH` eras.
|
||||
/// If reward hasn't been set or has been removed then 0 reward is returned.
|
||||
#[derive(Clone, Encode, Decode, Debug, Store)]
|
||||
pub struct ErasRewardPointsStore<T: Staking> {
|
||||
#[store(returns = EraRewardPoints<T::AccountId>)]
|
||||
/// Era index
|
||||
pub index: EraIndex,
|
||||
/// Marker for the runtime
|
||||
pub _phantom: PhantomData<T>,
|
||||
}
|
||||
|
||||
/// Preference of what happens regarding validation.
|
||||
#[derive(Clone, Encode, Decode, Debug, Call)]
|
||||
pub struct SetPayeeCall<T: Staking> {
|
||||
/// The payee
|
||||
pub payee: RewardDestination<T::AccountId>,
|
||||
/// Marker for the runtime
|
||||
pub _runtime: PhantomData<T>,
|
||||
}
|
||||
|
||||
/// The subset of the `frame::Trait` that a client must implement.
|
||||
#[module]
|
||||
#[rustfmt::skip]
|
||||
pub trait Staking: Balances {
|
||||
#![event_alias(ElectionCompute = u8)]
|
||||
#![event_type(EraIndex)]
|
||||
}
|
||||
|
||||
/// Number of eras to keep in history.
|
||||
///
|
||||
/// Information is kept for eras in `[current_era - history_depth; current_era]`.
|
||||
///
|
||||
/// Must be more than the number of eras delayed by session otherwise.
|
||||
/// I.e. active era must always be in history.
|
||||
/// I.e. `active_era > current_era - history_depth` must be guaranteed.
|
||||
#[derive(Encode, Decode, Copy, Clone, Debug, Default, Store)]
|
||||
pub struct HistoryDepthStore<T: Staking> {
|
||||
#[store(returns = u32)]
|
||||
/// Marker for the runtime
|
||||
pub _runtime: PhantomData<T>,
|
||||
}
|
||||
|
||||
/// Map from all locked "stash" accounts to the controller account.
|
||||
#[derive(Encode, Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd, Store)]
|
||||
pub struct BondedStore<T: Staking> {
|
||||
#[store(returns = Option<T::AccountId>)]
|
||||
/// Tٗhe stash account
|
||||
pub stash: T::AccountId,
|
||||
}
|
||||
|
||||
/// Map from all (unlocked) "controller" accounts to the info regarding the staking.
|
||||
#[derive(Encode, Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd, Store)]
|
||||
pub struct LedgerStore<T: Staking> {
|
||||
#[store(returns = Option<StakingLedger<T::AccountId, T::Balance>>)]
|
||||
/// The controller account
|
||||
pub controller: T::AccountId,
|
||||
}
|
||||
|
||||
/// Where the reward payment should be made. Keyed by stash.
|
||||
#[derive(Encode, Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd, Store)]
|
||||
pub struct PayeeStore<T: Staking> {
|
||||
#[store(returns = RewardDestination<T::AccountId>)]
|
||||
/// Tٗhe stash account
|
||||
pub stash: T::AccountId,
|
||||
}
|
||||
|
||||
/// The map from (wannabe) validator stash key to the preferences of that validator.
|
||||
#[derive(Encode, Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd, Store)]
|
||||
pub struct ValidatorsStore<T: Staking> {
|
||||
#[store(returns = ValidatorPrefs)]
|
||||
/// Tٗhe stash account
|
||||
pub stash: T::AccountId,
|
||||
}
|
||||
|
||||
/// The map from nominator stash key to the set of stash keys of all validators to nominate.
|
||||
#[derive(Encode, Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Store)]
|
||||
pub struct NominatorsStore<T: Staking> {
|
||||
#[store(returns = Option<Nominations<T::AccountId>>)]
|
||||
/// Tٗhe stash account
|
||||
pub stash: T::AccountId,
|
||||
}
|
||||
|
||||
/// The current era index.
|
||||
///
|
||||
/// This is the latest planned era, depending on how the Session pallet queues the validator
|
||||
/// set, it might be active or not.
|
||||
#[derive(Encode, Copy, Clone, Debug, Store)]
|
||||
pub struct CurrentEraStore<T: Staking> {
|
||||
#[store(returns = Option<EraIndex>)]
|
||||
/// Marker for the runtime
|
||||
pub _runtime: PhantomData<T>,
|
||||
}
|
||||
|
||||
/// Reward points of an era. Used to split era total payout between validators.
|
||||
///
|
||||
/// This points will be used to reward validators and their respective nominators.
|
||||
#[derive(PartialEq, Encode, Decode, Default, Debug)]
|
||||
pub struct EraRewardPoints<AccountId: Ord> {
|
||||
/// Total number of points. Equals the sum of reward points for each validator.
|
||||
pub total: RewardPoint,
|
||||
/// The reward points earned by a given validator.
|
||||
pub individual: BTreeMap<AccountId, RewardPoint>,
|
||||
}
|
||||
|
||||
/// Declare no desire to either validate or nominate.
|
||||
///
|
||||
/// Effective at the beginning of the next era.
|
||||
///
|
||||
/// The dispatch origin for this call must be _Signed_ by the controller, not the stash.
|
||||
/// Can only be called when [`EraElectionStatus`] is `Closed`.
|
||||
#[derive(Debug, Call, Encode)]
|
||||
pub struct ChillCall<T: Staking> {
|
||||
/// Runtime marker
|
||||
pub _runtime: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<T: Staking> Default for ChillCall<T> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
_runtime: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<T: Staking> Clone for ChillCall<T> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
_runtime: self._runtime,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<T: Staking> Copy for ChillCall<T> {}
|
||||
|
||||
/// Declare the desire to validate for the origin controller.
|
||||
///
|
||||
/// Effective at the beginning of the next era.
|
||||
///
|
||||
/// The dispatch origin for this call must be _Signed_ by the controller, not the stash.
|
||||
/// Can only be called when [`EraElectionStatus`] is `Closed`.
|
||||
#[derive(Clone, Debug, PartialEq, Call, Encode)]
|
||||
pub struct ValidateCall<T: Staking> {
|
||||
/// Runtime marker
|
||||
pub _runtime: PhantomData<T>,
|
||||
/// Validation preferences
|
||||
pub prefs: ValidatorPrefs,
|
||||
}
|
||||
|
||||
/// Declare the desire to nominate `targets` for the origin controller.
|
||||
///
|
||||
/// Effective at the beginning of the next era.
|
||||
///
|
||||
/// The dispatch origin for this call must be _Signed_ by the controller, not the stash.
|
||||
/// Can only be called when [`EraElectionStatus`] is `Closed`.
|
||||
#[derive(Call, Encode, Debug)]
|
||||
pub struct NominateCall<T: Staking> {
|
||||
/// The targets that are being nominated
|
||||
pub targets: Vec<T::Address>,
|
||||
}
|
||||
|
||||
/// Take the origin account as a stash and lock up `value` of its balance.
|
||||
/// `controller` will be the account that controls it.
|
||||
#[derive(Call, Encode, Debug)]
|
||||
pub struct BondCall<'a, T: Staking> {
|
||||
/// Tٗhe controller account
|
||||
pub controller: &'a T::Address,
|
||||
/// Lock up `value` of its balance.
|
||||
#[codec(compact)]
|
||||
pub value: T::Balance,
|
||||
/// Destination of Staking reward.
|
||||
pub payee: RewardDestination<T::AccountId>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{
|
||||
error::RuntimeError,
|
||||
extrinsic::{
|
||||
PairSigner,
|
||||
Signer,
|
||||
},
|
||||
frame::balances::*,
|
||||
tests::{
|
||||
test_node_process,
|
||||
TestRuntime,
|
||||
},
|
||||
Error,
|
||||
ExtrinsicSuccess,
|
||||
};
|
||||
use assert_matches::assert_matches;
|
||||
use sp_core::{
|
||||
sr25519,
|
||||
Pair,
|
||||
};
|
||||
use sp_keyring::AccountKeyring;
|
||||
|
||||
/// Helper function to generate a crypto pair from seed
|
||||
fn get_from_seed(seed: &str) -> sr25519::Pair {
|
||||
sr25519::Pair::from_string(&format!("//{}", seed), None)
|
||||
.expect("static values are valid; qed")
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_validate_with_controller_account() -> Result<(), Error> {
|
||||
env_logger::try_init().ok();
|
||||
let alice = PairSigner::<TestRuntime, _>::new(AccountKeyring::Alice.pair());
|
||||
let test_node_proc = test_node_process().await;
|
||||
let client = test_node_proc.client();
|
||||
let announce_validator = client
|
||||
.validate_and_watch(&alice, ValidatorPrefs::default())
|
||||
.await;
|
||||
assert_matches!(announce_validator, Ok(ExtrinsicSuccess {block: _, extrinsic: _, events}) => {
|
||||
// TOOD: this is unsatisfying – can we do better?
|
||||
assert_eq!(events.len(), 2);
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_validate_not_possible_for_stash_account() -> Result<(), Error> {
|
||||
env_logger::try_init().ok();
|
||||
let alice_stash =
|
||||
PairSigner::<TestRuntime, _>::new(get_from_seed("Alice//stash"));
|
||||
let test_node_proc = test_node_process().await;
|
||||
let client = test_node_proc.client();
|
||||
let announce_validator = client
|
||||
.validate_and_watch(&alice_stash, ValidatorPrefs::default())
|
||||
.await;
|
||||
assert_matches!(announce_validator, Err(Error::Runtime(RuntimeError::Module(module_err))) => {
|
||||
assert_eq!(module_err.module, "Staking");
|
||||
assert_eq!(module_err.error, "NotController");
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_nominate_with_controller_account() -> Result<(), Error> {
|
||||
env_logger::try_init().ok();
|
||||
let alice = PairSigner::<TestRuntime, _>::new(AccountKeyring::Alice.pair());
|
||||
let bob = PairSigner::<TestRuntime, _>::new(AccountKeyring::Bob.pair());
|
||||
let test_node_proc = test_node_process().await;
|
||||
let client = test_node_proc.client();
|
||||
|
||||
let nomination = client
|
||||
.nominate_and_watch(&alice, vec![bob.account_id().clone().into()])
|
||||
.await;
|
||||
assert_matches!(nomination, Ok(ExtrinsicSuccess {block: _, extrinsic: _, events}) => {
|
||||
// TOOD: this is unsatisfying – can we do better?
|
||||
assert_eq!(events.len(), 2);
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_nominate_not_possible_for_stash_account() -> Result<(), Error> {
|
||||
env_logger::try_init().ok();
|
||||
let alice_stash =
|
||||
PairSigner::<TestRuntime, sr25519::Pair>::new(get_from_seed("Alice//stash"));
|
||||
let bob = PairSigner::<TestRuntime, _>::new(AccountKeyring::Bob.pair());
|
||||
let test_node_proc = test_node_process().await;
|
||||
let client = test_node_proc.client();
|
||||
|
||||
let nomination = client
|
||||
.nominate_and_watch(&alice_stash, vec![bob.account_id().clone().into()])
|
||||
.await;
|
||||
assert_matches!(nomination, Err(Error::Runtime(RuntimeError::Module(module_err))) => {
|
||||
assert_eq!(module_err.module, "Staking");
|
||||
assert_eq!(module_err.error, "NotController");
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_chill_works_for_controller_only() -> Result<(), Error> {
|
||||
env_logger::try_init().ok();
|
||||
let alice_stash =
|
||||
PairSigner::<TestRuntime, sr25519::Pair>::new(get_from_seed("Alice//stash"));
|
||||
let bob_stash =
|
||||
PairSigner::<TestRuntime, sr25519::Pair>::new(get_from_seed("Bob//stash"));
|
||||
let alice = PairSigner::<TestRuntime, _>::new(AccountKeyring::Alice.pair());
|
||||
let test_node_proc = test_node_process().await;
|
||||
let client = test_node_proc.client();
|
||||
|
||||
// this will fail the second time, which is why this is one test, not two
|
||||
client
|
||||
.nominate_and_watch(&alice, vec![bob_stash.account_id().clone().into()])
|
||||
.await?;
|
||||
let store = LedgerStore {
|
||||
controller: alice.account_id().clone(),
|
||||
};
|
||||
let StakingLedger { stash, .. } = client.fetch(&store, None).await?.unwrap();
|
||||
assert_eq!(alice_stash.account_id(), &stash);
|
||||
let chill = client.chill_and_watch(&alice_stash).await;
|
||||
|
||||
assert_matches!(chill, Err(Error::Runtime(RuntimeError::Module(module_err))) => {
|
||||
assert_eq!(module_err.module, "Staking");
|
||||
assert_eq!(module_err.error, "NotController");
|
||||
});
|
||||
|
||||
let chill = client.chill_and_watch(&alice).await;
|
||||
assert_matches!(chill, Ok(ExtrinsicSuccess {block: _, extrinsic: _, events}) => {
|
||||
// TOOD: this is unsatisfying – can we do better?
|
||||
assert_eq!(events.len(), 2);
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_bond() -> Result<(), Error> {
|
||||
env_logger::try_init().ok();
|
||||
let alice = PairSigner::<TestRuntime, _>::new(AccountKeyring::Alice.pair());
|
||||
let test_node_proc = test_node_process().await;
|
||||
let client = test_node_proc.client();
|
||||
|
||||
let bond = client
|
||||
.bond_and_watch(
|
||||
&alice,
|
||||
&AccountKeyring::Bob.to_account_id().into(),
|
||||
100_000_000_000_000,
|
||||
RewardDestination::Stash,
|
||||
)
|
||||
.await;
|
||||
|
||||
assert_matches!(bond, Ok(ExtrinsicSuccess {block: _, extrinsic: _, events}) => {
|
||||
// TOOD: this is unsatisfying – can we do better?
|
||||
assert_eq!(events.len(), 3);
|
||||
});
|
||||
|
||||
let bond_again = client
|
||||
.bond_and_watch(
|
||||
&alice,
|
||||
&AccountKeyring::Bob.to_account_id().into(),
|
||||
100_000_000_000,
|
||||
RewardDestination::Stash,
|
||||
)
|
||||
.await;
|
||||
|
||||
assert_matches!(bond_again, Err(Error::Runtime(RuntimeError::Module(module_err))) => {
|
||||
assert_eq!(module_err.module, "Staking");
|
||||
assert_eq!(module_err.error, "AlreadyBonded");
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_total_issuance_is_okay() -> Result<(), Error> {
|
||||
env_logger::try_init().ok();
|
||||
let test_node_proc = test_node_process().await;
|
||||
let client = test_node_proc.client();
|
||||
let total_issuance = client.total_issuance(None).await?;
|
||||
assert!(total_issuance > 1u128 << 32);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_history_depth_is_okay() -> Result<(), Error> {
|
||||
env_logger::try_init().ok();
|
||||
let test_node_proc = test_node_process().await;
|
||||
let client = test_node_proc.client();
|
||||
let history_depth = client.history_depth(None).await?;
|
||||
assert_eq!(history_depth, 84);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_current_era_is_okay() -> Result<(), Error> {
|
||||
env_logger::try_init().ok();
|
||||
let test_node_proc = test_node_process().await;
|
||||
let client = test_node_proc.client();
|
||||
let _current_era = client
|
||||
.current_era(None)
|
||||
.await?
|
||||
.expect("current era always exists");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_era_reward_points_is_okay() -> Result<(), Error> {
|
||||
env_logger::try_init().ok();
|
||||
let test_node_proc = test_node_process().await;
|
||||
let client = test_node_proc.client();
|
||||
let store = ErasRewardPointsStore {
|
||||
_phantom: PhantomData,
|
||||
index: 0,
|
||||
};
|
||||
|
||||
let current_era_result = client.fetch(&store, None).await?;
|
||||
|
||||
assert_matches!(current_era_result, Some(_));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1,122 +0,0 @@
|
||||
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
|
||||
// This file is part of substrate-subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// subxt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with substrate-subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Implements support for the frame_sudo module.
|
||||
|
||||
use crate::{
|
||||
frame::system::System,
|
||||
Encoded,
|
||||
};
|
||||
use codec::Encode;
|
||||
use core::marker::PhantomData;
|
||||
use frame_support::weights::Weight;
|
||||
|
||||
/// The subset of the `frame_sudo::Trait` that a client must implement.
|
||||
#[module]
|
||||
pub trait Sudo: System {}
|
||||
|
||||
/// Execute a transaction with sudo permissions.
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Call, Encode)]
|
||||
pub struct SudoCall<'a, T: Sudo> {
|
||||
/// Runtime marker.
|
||||
pub _runtime: PhantomData<T>,
|
||||
/// Encoded transaction.
|
||||
pub call: &'a Encoded,
|
||||
}
|
||||
|
||||
/// Execute a transaction with sudo permissions without checking the call weight.
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Call, Encode)]
|
||||
pub struct SudoUncheckedWeightCall<'a, T: Sudo> {
|
||||
/// Runtime marker.
|
||||
pub _runtime: PhantomData<T>,
|
||||
/// Encoded transaction.
|
||||
pub call: &'a Encoded,
|
||||
/// Call weight.
|
||||
///
|
||||
/// This argument is actually unused in runtime, you can pass any value of
|
||||
/// `Weight` type when using this call.
|
||||
pub weight: Weight,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{
|
||||
error::{
|
||||
Error,
|
||||
RuntimeError,
|
||||
},
|
||||
extrinsic::PairSigner,
|
||||
frame::balances::TransferCall,
|
||||
tests::{
|
||||
test_node_process,
|
||||
TestRuntime,
|
||||
},
|
||||
};
|
||||
use sp_keyring::AccountKeyring;
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_sudo() {
|
||||
env_logger::try_init().ok();
|
||||
let alice = PairSigner::<TestRuntime, _>::new(AccountKeyring::Alice.pair());
|
||||
let bob = AccountKeyring::Bob.to_account_id().clone().into();
|
||||
let test_node_proc = test_node_process().await;
|
||||
let client = test_node_proc.client();
|
||||
|
||||
let call = client
|
||||
.encode(TransferCall {
|
||||
to: &bob,
|
||||
amount: 10_000,
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
let res = client.sudo_and_watch(&alice, &call).await;
|
||||
assert!(
|
||||
if let Err(Error::Runtime(RuntimeError::BadOrigin)) = res {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_sudo_unchecked_weight() {
|
||||
env_logger::try_init().ok();
|
||||
let alice = PairSigner::<TestRuntime, _>::new(AccountKeyring::Alice.pair());
|
||||
let bob = AccountKeyring::Bob.to_account_id().into();
|
||||
let test_node_proc = test_node_process().await;
|
||||
let client = test_node_proc.client();
|
||||
|
||||
let call = client
|
||||
.encode(TransferCall {
|
||||
to: &bob,
|
||||
amount: 10_000,
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
let res = client
|
||||
.sudo_unchecked_weight_and_watch(&alice, &call, 0u64)
|
||||
.await;
|
||||
assert!(
|
||||
if let Err(Error::Runtime(RuntimeError::BadOrigin)) = res {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,215 +0,0 @@
|
||||
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
|
||||
// This file is part of substrate-subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// subxt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with substrate-subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Implements support for the frame_system module.
|
||||
|
||||
use codec::{
|
||||
Codec,
|
||||
Decode,
|
||||
Encode,
|
||||
};
|
||||
use core::marker::PhantomData;
|
||||
use frame_support::{
|
||||
weights::DispatchInfo,
|
||||
Parameter,
|
||||
};
|
||||
use serde::de::DeserializeOwned;
|
||||
use sp_runtime::{
|
||||
traits::{
|
||||
AtLeast32Bit,
|
||||
AtLeast32BitUnsigned,
|
||||
Bounded,
|
||||
CheckEqual,
|
||||
Extrinsic,
|
||||
Hash,
|
||||
Header,
|
||||
MaybeDisplay,
|
||||
MaybeMallocSizeOf,
|
||||
MaybeSerialize,
|
||||
MaybeSerializeDeserialize,
|
||||
Member,
|
||||
SimpleBitOps,
|
||||
},
|
||||
DispatchError,
|
||||
};
|
||||
use std::fmt::Debug;
|
||||
|
||||
/// The subset of the `frame::Trait` that a client must implement.
|
||||
#[module]
|
||||
pub trait System {
|
||||
/// Account index (aka nonce) type. This stores the number of previous
|
||||
/// transactions associated with a sender account.
|
||||
type Index: Parameter
|
||||
+ Member
|
||||
+ MaybeSerialize
|
||||
+ Debug
|
||||
+ Default
|
||||
+ MaybeDisplay
|
||||
+ AtLeast32Bit
|
||||
+ Copy;
|
||||
|
||||
/// The block number type used by the runtime.
|
||||
type BlockNumber: Parameter
|
||||
+ Member
|
||||
+ MaybeMallocSizeOf
|
||||
+ MaybeSerializeDeserialize
|
||||
+ Debug
|
||||
+ MaybeDisplay
|
||||
+ AtLeast32BitUnsigned
|
||||
+ Default
|
||||
+ Bounded
|
||||
+ Copy
|
||||
+ std::hash::Hash
|
||||
+ std::str::FromStr;
|
||||
|
||||
/// The output of the `Hashing` function.
|
||||
type Hash: Parameter
|
||||
+ Member
|
||||
+ MaybeMallocSizeOf
|
||||
+ MaybeSerializeDeserialize
|
||||
+ Debug
|
||||
+ MaybeDisplay
|
||||
+ Ord
|
||||
+ SimpleBitOps
|
||||
+ Default
|
||||
+ Copy
|
||||
+ CheckEqual
|
||||
+ std::hash::Hash
|
||||
+ AsRef<[u8]>
|
||||
+ AsMut<[u8]>;
|
||||
|
||||
/// The hashing system (algorithm) being used in the runtime (e.g. Blake2).
|
||||
#[module(ignore)]
|
||||
type Hashing: Hash<Output = Self::Hash>;
|
||||
|
||||
/// The user account identifier type for the runtime.
|
||||
type AccountId: Parameter + Member + MaybeSerialize + MaybeDisplay + Ord + Default;
|
||||
|
||||
/// The address type. This instead of `<frame_system::Trait::Lookup as StaticLookup>::Source`.
|
||||
#[module(ignore)]
|
||||
type Address: Codec + Clone + PartialEq + Debug + Send + Sync;
|
||||
|
||||
/// The block header.
|
||||
#[module(ignore)]
|
||||
type Header: Parameter
|
||||
+ Header<Number = Self::BlockNumber, Hash = Self::Hash>
|
||||
+ DeserializeOwned;
|
||||
|
||||
/// Extrinsic type within blocks.
|
||||
#[module(ignore)]
|
||||
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;
|
||||
}
|
||||
|
||||
/// Type used to encode the number of references an account has.
|
||||
pub type RefCount = u32;
|
||||
|
||||
/// Information of an account.
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Default, Decode, Encode)]
|
||||
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 consumers: RefCount,
|
||||
/// The number of other modules that allow this account to exist. The account may not be reaped
|
||||
/// until this is zero.
|
||||
pub providers: RefCount,
|
||||
/// The additional data that belongs to this account. Used to store the balance(s) in a lot of
|
||||
/// chains.
|
||||
pub data: T::AccountData,
|
||||
}
|
||||
|
||||
/// Account field of the `System` module.
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Store, Encode)]
|
||||
pub struct AccountStore<'a, T: System> {
|
||||
#[store(returns = AccountInfo<T>)]
|
||||
/// Account to retrieve the `AccountInfo<T>` for.
|
||||
pub account_id: &'a T::AccountId,
|
||||
}
|
||||
|
||||
/// Arguments for updating the runtime code
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Call, Encode)]
|
||||
pub struct SetCodeCall<'a, T: System> {
|
||||
/// Runtime marker.
|
||||
pub _runtime: PhantomData<T>,
|
||||
/// Runtime wasm blob.
|
||||
pub code: &'a [u8],
|
||||
}
|
||||
|
||||
/// Arguments for updating the runtime code without checks
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Call, Encode)]
|
||||
pub struct SetCodeWithoutChecksCall<'a, T: System> {
|
||||
/// Runtime marker.
|
||||
pub _runtime: PhantomData<T>,
|
||||
/// Runtime wasm blob.
|
||||
pub code: &'a [u8],
|
||||
}
|
||||
|
||||
/// A phase of a block's execution.
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Decode)]
|
||||
pub enum Phase {
|
||||
/// Applying an extrinsic.
|
||||
ApplyExtrinsic(u32),
|
||||
/// Finalizing the block.
|
||||
Finalization,
|
||||
/// Initializing the block.
|
||||
Initialization,
|
||||
}
|
||||
|
||||
/// An extrinsic completed successfully.
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Event, Decode)]
|
||||
pub struct ExtrinsicSuccessEvent<T: System> {
|
||||
/// Runtime marker.
|
||||
pub _runtime: PhantomData<T>,
|
||||
/// The dispatch info.
|
||||
pub info: DispatchInfo,
|
||||
}
|
||||
|
||||
/// An extrinsic failed.
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Event, Decode)]
|
||||
pub struct ExtrinsicFailedEvent<T: System> {
|
||||
/// Runtime marker.
|
||||
pub _runtime: PhantomData<T>,
|
||||
/// The dispatch error.
|
||||
pub error: DispatchError,
|
||||
/// The dispatch info.
|
||||
pub info: DispatchInfo,
|
||||
}
|
||||
|
||||
/// `:code` was updated.
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Event, Decode)]
|
||||
pub struct CodeUpdatedEvent<T: System> {
|
||||
/// Runtime marker.
|
||||
pub _runtime: PhantomData<T>,
|
||||
}
|
||||
|
||||
/// A new account was created.
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Event, Decode)]
|
||||
pub struct NewAccountEvent<T: System> {
|
||||
/// Created account id.
|
||||
pub account: T::AccountId,
|
||||
}
|
||||
|
||||
/// An account was reaped.
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Event, Decode)]
|
||||
pub struct KilledAccountEvent<T: System> {
|
||||
/// Killed account id.
|
||||
pub account: T::AccountId,
|
||||
}
|
||||
+102
-558
@@ -1,5 +1,5 @@
|
||||
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
|
||||
// This file is part of substrate-subxt.
|
||||
// This file is part of subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
@@ -12,7 +12,7 @@
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with substrate-subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! A library to **sub**mit e**xt**rinsics to a
|
||||
//! [substrate](https://github.com/paritytech/substrate) node via RPC.
|
||||
@@ -40,68 +40,63 @@
|
||||
)]
|
||||
#![allow(clippy::type_complexity)]
|
||||
|
||||
#[macro_use]
|
||||
extern crate substrate_subxt_proc_macro;
|
||||
pub use frame_metadata::StorageHasher;
|
||||
pub use subxt_macro::subxt;
|
||||
|
||||
pub use bitvec;
|
||||
pub use codec;
|
||||
pub use sp_arithmetic;
|
||||
pub use sp_core;
|
||||
pub use sp_runtime;
|
||||
|
||||
use codec::{
|
||||
Codec,
|
||||
Decode,
|
||||
DecodeAll,
|
||||
Encode,
|
||||
};
|
||||
use futures::future;
|
||||
use jsonrpsee_http_client::HttpClientBuilder;
|
||||
use jsonrpsee_types::Subscription;
|
||||
use jsonrpsee_ws_client::WsClientBuilder;
|
||||
use sp_core::{
|
||||
storage::{
|
||||
StorageChangeSet,
|
||||
StorageData,
|
||||
StorageKey,
|
||||
},
|
||||
Bytes,
|
||||
};
|
||||
pub use sp_runtime::traits::SignedExtension;
|
||||
pub use sp_version::RuntimeVersion;
|
||||
use std::{
|
||||
use core::{
|
||||
fmt::Debug,
|
||||
marker::PhantomData,
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
mod client;
|
||||
mod config;
|
||||
mod error;
|
||||
mod events;
|
||||
pub mod extrinsic;
|
||||
mod frame;
|
||||
mod metadata;
|
||||
mod rpc;
|
||||
mod runtimes;
|
||||
pub mod rpc;
|
||||
pub mod storage;
|
||||
mod subscription;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
pub use crate::{
|
||||
client::{
|
||||
Client,
|
||||
ClientBuilder,
|
||||
SubmittableExtrinsic,
|
||||
},
|
||||
config::{
|
||||
AccountData,
|
||||
Config,
|
||||
ExtrinsicExtraData,
|
||||
},
|
||||
error::{
|
||||
Error,
|
||||
ModuleError,
|
||||
PalletError,
|
||||
RuntimeError,
|
||||
},
|
||||
events::{
|
||||
EventTypeRegistry,
|
||||
EventsDecoder,
|
||||
RawEvent,
|
||||
},
|
||||
extrinsic::{
|
||||
DefaultExtra,
|
||||
PairSigner,
|
||||
SignedExtra,
|
||||
Signer,
|
||||
UncheckedExtrinsic,
|
||||
},
|
||||
frame::*,
|
||||
metadata::{
|
||||
Metadata,
|
||||
MetadataError,
|
||||
},
|
||||
metadata::Metadata,
|
||||
rpc::{
|
||||
BlockNumber,
|
||||
ExtrinsicSuccess,
|
||||
@@ -109,543 +104,42 @@ pub use crate::{
|
||||
RpcClient,
|
||||
SystemProperties,
|
||||
},
|
||||
runtimes::*,
|
||||
storage::{
|
||||
KeyIter,
|
||||
StorageEntry,
|
||||
StorageEntryKey,
|
||||
StorageMapKey,
|
||||
},
|
||||
subscription::{
|
||||
EventStorageSubscription,
|
||||
EventSubscription,
|
||||
FinalizedEventStorageSubscription,
|
||||
},
|
||||
substrate_subxt_proc_macro::*,
|
||||
};
|
||||
use crate::{
|
||||
frame::system::{
|
||||
AccountStoreExt,
|
||||
Phase,
|
||||
System,
|
||||
},
|
||||
rpc::{
|
||||
ChainBlock,
|
||||
Rpc,
|
||||
},
|
||||
};
|
||||
|
||||
/// ClientBuilder for constructing a Client.
|
||||
#[derive(Default)]
|
||||
pub struct ClientBuilder<T: Runtime> {
|
||||
url: Option<String>,
|
||||
client: Option<RpcClient>,
|
||||
page_size: Option<u32>,
|
||||
event_type_registry: EventTypeRegistry<T>,
|
||||
skip_type_sizes_check: bool,
|
||||
accept_weak_inclusion: bool,
|
||||
}
|
||||
/// Call trait.
|
||||
pub trait Call: Encode {
|
||||
/// Pallet name.
|
||||
const PALLET: &'static str;
|
||||
/// Function name.
|
||||
const FUNCTION: &'static str;
|
||||
|
||||
impl<T: Runtime> ClientBuilder<T> {
|
||||
/// Creates a new ClientBuilder.
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
url: None,
|
||||
client: None,
|
||||
page_size: None,
|
||||
event_type_registry: EventTypeRegistry::new(),
|
||||
skip_type_sizes_check: false,
|
||||
accept_weak_inclusion: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets the jsonrpsee client.
|
||||
pub fn set_client<C: Into<RpcClient>>(mut self, client: C) -> Self {
|
||||
self.client = Some(client.into());
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the substrate rpc address.
|
||||
pub fn set_url<P: Into<String>>(mut self, url: P) -> Self {
|
||||
self.url = Some(url.into());
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the page size.
|
||||
pub fn set_page_size(mut self, size: u32) -> Self {
|
||||
self.page_size = Some(size);
|
||||
self
|
||||
}
|
||||
|
||||
/// Register a custom type segmenter, for consuming types in events where the size cannot
|
||||
/// be inferred from the metadata.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// If there is already a type size registered with this name.
|
||||
pub fn register_type_size<U>(mut self, name: &str) -> Self
|
||||
where
|
||||
U: Codec + Send + Sync + 'static,
|
||||
{
|
||||
self.event_type_registry.register_type_size::<U>(name);
|
||||
self
|
||||
}
|
||||
|
||||
/// Disable the check for missing type sizes on `build`.
|
||||
///
|
||||
/// *WARNING* can lead to runtime errors if receiving events with unknown types.
|
||||
pub fn skip_type_sizes_check(mut self) -> Self {
|
||||
self.skip_type_sizes_check = true;
|
||||
self
|
||||
}
|
||||
|
||||
/// Only check that transactions are InBlock on submit.
|
||||
pub fn accept_weak_inclusion(mut self) -> Self {
|
||||
self.accept_weak_inclusion = true;
|
||||
self
|
||||
}
|
||||
|
||||
/// Creates a new Client.
|
||||
pub async fn build<'a>(self) -> Result<Client<T>, Error> {
|
||||
let client = if let Some(client) = self.client {
|
||||
client
|
||||
} else {
|
||||
let url = self.url.as_deref().unwrap_or("ws://127.0.0.1:9944");
|
||||
if url.starts_with("ws://") || url.starts_with("wss://") {
|
||||
let client = WsClientBuilder::default()
|
||||
.max_notifs_per_subscription(4096)
|
||||
.build(url)
|
||||
.await?;
|
||||
RpcClient::WebSocket(Arc::new(client))
|
||||
} else {
|
||||
let client = HttpClientBuilder::default().build(&url)?;
|
||||
RpcClient::Http(Arc::new(client))
|
||||
}
|
||||
};
|
||||
let mut rpc = Rpc::new(client);
|
||||
if self.accept_weak_inclusion {
|
||||
rpc.accept_weak_inclusion();
|
||||
}
|
||||
let (metadata, genesis_hash, runtime_version, properties) = future::join4(
|
||||
rpc.metadata(),
|
||||
rpc.genesis_hash(),
|
||||
rpc.runtime_version(None),
|
||||
rpc.system_properties(),
|
||||
)
|
||||
.await;
|
||||
let metadata = metadata?;
|
||||
|
||||
if let Err(missing) = self.event_type_registry.check_missing_type_sizes(&metadata)
|
||||
{
|
||||
if self.skip_type_sizes_check {
|
||||
log::warn!(
|
||||
"The following types do not have registered type segmenters: {:?} \
|
||||
If any events containing these types are received, this can cause a \
|
||||
`TypeSizeUnavailable` error and prevent decoding the actual event \
|
||||
being listened for.\
|
||||
\
|
||||
Use `ClientBuilder::register_type_size` to register missing type sizes.",
|
||||
missing
|
||||
);
|
||||
} else {
|
||||
return Err(Error::MissingTypeSizes(missing.into_iter().collect()))
|
||||
}
|
||||
}
|
||||
|
||||
let events_decoder =
|
||||
EventsDecoder::new(metadata.clone(), self.event_type_registry);
|
||||
|
||||
Ok(Client {
|
||||
rpc,
|
||||
genesis_hash: genesis_hash?,
|
||||
metadata,
|
||||
events_decoder,
|
||||
properties: properties.unwrap_or_else(|_| Default::default()),
|
||||
runtime_version: runtime_version?,
|
||||
_marker: PhantomData,
|
||||
page_size: self.page_size.unwrap_or(10),
|
||||
})
|
||||
/// Returns true if the given pallet and function names match this call.
|
||||
fn is_call(pallet: &str, function: &str) -> bool {
|
||||
Self::PALLET == pallet && Self::FUNCTION == function
|
||||
}
|
||||
}
|
||||
|
||||
/// Client to interface with a substrate node.
|
||||
pub struct Client<T: Runtime> {
|
||||
rpc: Rpc<T>,
|
||||
genesis_hash: T::Hash,
|
||||
metadata: Metadata,
|
||||
events_decoder: EventsDecoder<T>,
|
||||
properties: SystemProperties,
|
||||
runtime_version: RuntimeVersion,
|
||||
_marker: PhantomData<(fn() -> T::Signature, T::Extra)>,
|
||||
page_size: u32,
|
||||
}
|
||||
/// Event trait.
|
||||
pub trait Event: Decode {
|
||||
/// Pallet name.
|
||||
const PALLET: &'static str;
|
||||
/// Event name.
|
||||
const EVENT: &'static str;
|
||||
|
||||
impl<T: Runtime> Clone for Client<T> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
rpc: self.rpc.clone(),
|
||||
genesis_hash: self.genesis_hash,
|
||||
metadata: self.metadata.clone(),
|
||||
events_decoder: self.events_decoder.clone(),
|
||||
properties: self.properties.clone(),
|
||||
runtime_version: self.runtime_version.clone(),
|
||||
_marker: PhantomData,
|
||||
page_size: self.page_size,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Iterates over key value pairs in a map.
|
||||
pub struct KeyIter<T: Runtime, F: Store<T>> {
|
||||
client: Client<T>,
|
||||
_marker: PhantomData<F>,
|
||||
count: u32,
|
||||
hash: T::Hash,
|
||||
start_key: Option<StorageKey>,
|
||||
buffer: Vec<(StorageKey, StorageData)>,
|
||||
}
|
||||
|
||||
impl<T: Runtime, F: Store<T>> KeyIter<T, F> {
|
||||
/// Returns the next key value pair from a map.
|
||||
pub async fn next(&mut self) -> Result<Option<(StorageKey, F::Returns)>, Error> {
|
||||
loop {
|
||||
if let Some((k, v)) = self.buffer.pop() {
|
||||
return Ok(Some((k, Decode::decode(&mut &v.0[..])?)))
|
||||
} else {
|
||||
let keys = self
|
||||
.client
|
||||
.fetch_keys::<F>(self.count, self.start_key.take(), Some(self.hash))
|
||||
.await?;
|
||||
|
||||
if keys.is_empty() {
|
||||
return Ok(None)
|
||||
}
|
||||
|
||||
self.start_key = keys.last().cloned();
|
||||
|
||||
let change_sets = self
|
||||
.client
|
||||
.rpc
|
||||
.query_storage_at(&keys, Some(self.hash))
|
||||
.await?;
|
||||
for change_set in change_sets {
|
||||
for (k, v) in change_set.changes {
|
||||
if let Some(v) = v {
|
||||
self.buffer.push((k, v));
|
||||
}
|
||||
}
|
||||
}
|
||||
debug_assert_eq!(self.buffer.len(), keys.len());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Runtime> Client<T> {
|
||||
/// Returns the genesis hash.
|
||||
pub fn genesis(&self) -> &T::Hash {
|
||||
&self.genesis_hash
|
||||
}
|
||||
|
||||
/// Returns the chain metadata.
|
||||
pub fn metadata(&self) -> &Metadata {
|
||||
&self.metadata
|
||||
}
|
||||
|
||||
/// Returns the system properties
|
||||
pub fn properties(&self) -> &SystemProperties {
|
||||
&self.properties
|
||||
}
|
||||
|
||||
/// Returns the rpc client.
|
||||
pub fn rpc_client(&self) -> &RpcClient {
|
||||
&self.rpc.client
|
||||
}
|
||||
|
||||
/// Fetch the value under an unhashed storage key
|
||||
pub async fn fetch_unhashed<V: Decode>(
|
||||
&self,
|
||||
key: StorageKey,
|
||||
hash: Option<T::Hash>,
|
||||
) -> Result<Option<V>, Error> {
|
||||
if let Some(data) = self.rpc.storage(&key, hash).await? {
|
||||
Ok(Some(Decode::decode(&mut &data.0[..])?))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
/// Fetch a StorageKey with an optional block hash.
|
||||
pub async fn fetch<F: Store<T>>(
|
||||
&self,
|
||||
store: &F,
|
||||
hash: Option<T::Hash>,
|
||||
) -> Result<Option<F::Returns>, Error> {
|
||||
let key = store.key(&self.metadata)?;
|
||||
self.fetch_unhashed::<F::Returns>(key, hash).await
|
||||
}
|
||||
|
||||
/// Fetch a StorageKey that has a default value with an optional block hash.
|
||||
pub async fn fetch_or_default<F: Store<T>>(
|
||||
&self,
|
||||
store: &F,
|
||||
hash: Option<T::Hash>,
|
||||
) -> Result<F::Returns, Error> {
|
||||
if let Some(data) = self.fetch(store, hash).await? {
|
||||
Ok(data)
|
||||
} else {
|
||||
Ok(store.default(&self.metadata)?)
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns an iterator of key value pairs.
|
||||
pub async fn iter<F: Store<T>>(
|
||||
&self,
|
||||
hash: Option<T::Hash>,
|
||||
) -> Result<KeyIter<T, F>, Error> {
|
||||
let hash = if let Some(hash) = hash {
|
||||
hash
|
||||
} else {
|
||||
self.block_hash(None)
|
||||
.await?
|
||||
.expect("didn't pass a block number; qed")
|
||||
};
|
||||
Ok(KeyIter {
|
||||
client: self.clone(),
|
||||
hash,
|
||||
count: self.page_size,
|
||||
start_key: None,
|
||||
buffer: Default::default(),
|
||||
_marker: PhantomData,
|
||||
})
|
||||
}
|
||||
|
||||
/// Fetch up to `count` keys for a storage map in lexicographic order.
|
||||
///
|
||||
/// Supports pagination by passing a value to `start_key`.
|
||||
pub async fn fetch_keys<F: Store<T>>(
|
||||
&self,
|
||||
count: u32,
|
||||
start_key: Option<StorageKey>,
|
||||
hash: Option<T::Hash>,
|
||||
) -> Result<Vec<StorageKey>, Error> {
|
||||
let prefix = <F as Store<T>>::prefix(&self.metadata)?;
|
||||
let keys = self
|
||||
.rpc
|
||||
.storage_keys_paged(Some(prefix), count, start_key, hash)
|
||||
.await?;
|
||||
Ok(keys)
|
||||
}
|
||||
|
||||
/// Query historical storage entries
|
||||
pub async fn query_storage(
|
||||
&self,
|
||||
keys: Vec<StorageKey>,
|
||||
from: T::Hash,
|
||||
to: Option<T::Hash>,
|
||||
) -> Result<Vec<StorageChangeSet<<T as System>::Hash>>, Error> {
|
||||
self.rpc.query_storage(keys, from, to).await
|
||||
}
|
||||
|
||||
/// Get a header
|
||||
pub async fn header<H>(&self, hash: Option<H>) -> Result<Option<T::Header>, Error>
|
||||
where
|
||||
H: Into<T::Hash> + 'static,
|
||||
{
|
||||
let header = self.rpc.header(hash.map(|h| h.into())).await?;
|
||||
Ok(header)
|
||||
}
|
||||
|
||||
/// Get a block hash. By default returns the latest block hash
|
||||
pub async fn block_hash(
|
||||
&self,
|
||||
block_number: Option<BlockNumber>,
|
||||
) -> Result<Option<T::Hash>, Error> {
|
||||
let hash = self.rpc.block_hash(block_number).await?;
|
||||
Ok(hash)
|
||||
}
|
||||
|
||||
/// Get a block hash of the latest finalized block
|
||||
pub async fn finalized_head(&self) -> Result<T::Hash, Error> {
|
||||
let head = self.rpc.finalized_head().await?;
|
||||
Ok(head)
|
||||
}
|
||||
|
||||
/// Get a block
|
||||
pub async fn block<H>(&self, hash: Option<H>) -> Result<Option<ChainBlock<T>>, Error>
|
||||
where
|
||||
H: Into<T::Hash> + 'static,
|
||||
{
|
||||
let block = self.rpc.block(hash.map(|h| h.into())).await?;
|
||||
Ok(block)
|
||||
}
|
||||
|
||||
/// Get proof of storage entries at a specific block's state.
|
||||
pub async fn read_proof<H>(
|
||||
&self,
|
||||
keys: Vec<StorageKey>,
|
||||
hash: Option<H>,
|
||||
) -> Result<ReadProof<T::Hash>, Error>
|
||||
where
|
||||
H: Into<T::Hash> + 'static,
|
||||
{
|
||||
let proof = self.rpc.read_proof(keys, hash.map(|h| h.into())).await?;
|
||||
Ok(proof)
|
||||
}
|
||||
|
||||
/// Subscribe to events.
|
||||
///
|
||||
/// *WARNING* these may not be included in the finalized chain, use
|
||||
/// `subscribe_finalized_events` to ensure events are finalized.
|
||||
pub async fn subscribe_events(&self) -> Result<EventStorageSubscription<T>, Error> {
|
||||
let events = self.rpc.subscribe_events().await?;
|
||||
Ok(events)
|
||||
}
|
||||
|
||||
/// Subscribe to finalized events.
|
||||
pub async fn subscribe_finalized_events(
|
||||
&self,
|
||||
) -> Result<EventStorageSubscription<T>, Error> {
|
||||
let events = self.rpc.subscribe_finalized_events().await?;
|
||||
Ok(events)
|
||||
}
|
||||
|
||||
/// Subscribe to new blocks.
|
||||
pub async fn subscribe_blocks(&self) -> Result<Subscription<T::Header>, Error> {
|
||||
let headers = self.rpc.subscribe_blocks().await?;
|
||||
Ok(headers)
|
||||
}
|
||||
|
||||
/// Subscribe to finalized blocks.
|
||||
pub async fn subscribe_finalized_blocks(
|
||||
&self,
|
||||
) -> Result<Subscription<T::Header>, Error> {
|
||||
let headers = self.rpc.subscribe_finalized_blocks().await?;
|
||||
Ok(headers)
|
||||
}
|
||||
|
||||
/// Encodes a call.
|
||||
pub fn encode<C: Call<T>>(&self, call: C) -> Result<Encoded, Error> {
|
||||
Ok(self
|
||||
.metadata()
|
||||
.module_with_calls(C::MODULE)
|
||||
.and_then(|module| module.call(C::FUNCTION, call))?)
|
||||
}
|
||||
|
||||
/// Creates an unsigned extrinsic.
|
||||
pub fn create_unsigned<C: Call<T> + Send + Sync>(
|
||||
&self,
|
||||
call: C,
|
||||
) -> Result<UncheckedExtrinsic<T>, Error> {
|
||||
let call = self.encode(call)?;
|
||||
Ok(extrinsic::create_unsigned::<T>(call))
|
||||
}
|
||||
|
||||
/// Creates a signed extrinsic.
|
||||
pub async fn create_signed<C: Call<T> + Send + Sync>(
|
||||
&self,
|
||||
call: C,
|
||||
signer: &(dyn Signer<T> + Send + Sync),
|
||||
) -> Result<UncheckedExtrinsic<T>, Error>
|
||||
where
|
||||
<<T::Extra as SignedExtra<T>>::Extra as SignedExtension>::AdditionalSigned:
|
||||
Send + Sync,
|
||||
{
|
||||
let account_nonce = if let Some(nonce) = signer.nonce() {
|
||||
nonce
|
||||
} else {
|
||||
self.account(signer.account_id(), None).await?.nonce
|
||||
};
|
||||
let call = self.encode(call)?;
|
||||
let signed = extrinsic::create_signed(
|
||||
&self.runtime_version,
|
||||
self.genesis_hash,
|
||||
account_nonce,
|
||||
call,
|
||||
signer,
|
||||
)
|
||||
.await?;
|
||||
Ok(signed)
|
||||
}
|
||||
|
||||
/// Returns the events decoder.
|
||||
pub fn events_decoder(&self) -> &EventsDecoder<T> {
|
||||
&self.events_decoder
|
||||
}
|
||||
|
||||
/// Create and submit an extrinsic and return corresponding Hash if successful
|
||||
pub async fn submit_extrinsic(
|
||||
&self,
|
||||
extrinsic: UncheckedExtrinsic<T>,
|
||||
) -> Result<T::Hash, Error> {
|
||||
self.rpc.submit_extrinsic(extrinsic).await
|
||||
}
|
||||
|
||||
/// Create and submit an extrinsic and return corresponding Event if successful
|
||||
pub async fn submit_and_watch_extrinsic(
|
||||
&self,
|
||||
extrinsic: UncheckedExtrinsic<T>,
|
||||
) -> Result<ExtrinsicSuccess<T>, Error> {
|
||||
self.rpc
|
||||
.submit_and_watch_extrinsic(extrinsic, &self.events_decoder)
|
||||
.await
|
||||
}
|
||||
|
||||
/// Submits a transaction to the chain.
|
||||
pub async fn submit<C: Call<T> + Send + Sync>(
|
||||
&self,
|
||||
call: C,
|
||||
signer: &(dyn Signer<T> + Send + Sync),
|
||||
) -> Result<T::Hash, Error>
|
||||
where
|
||||
<<T::Extra as SignedExtra<T>>::Extra as SignedExtension>::AdditionalSigned:
|
||||
Send + Sync,
|
||||
{
|
||||
let extrinsic = self.create_signed(call, signer).await?;
|
||||
self.submit_extrinsic(extrinsic).await
|
||||
}
|
||||
|
||||
/// Submits transaction to the chain and watch for events.
|
||||
pub async fn watch<C: Call<T> + Send + Sync>(
|
||||
&self,
|
||||
call: C,
|
||||
signer: &(dyn Signer<T> + Send + Sync),
|
||||
) -> Result<ExtrinsicSuccess<T>, Error>
|
||||
where
|
||||
<<T::Extra as SignedExtra<T>>::Extra as SignedExtension>::AdditionalSigned:
|
||||
Send + Sync,
|
||||
{
|
||||
let extrinsic = self.create_signed(call, signer).await?;
|
||||
self.submit_and_watch_extrinsic(extrinsic).await
|
||||
}
|
||||
|
||||
/// Insert a key into the keystore.
|
||||
pub async fn insert_key(
|
||||
&self,
|
||||
key_type: String,
|
||||
suri: String,
|
||||
public: Bytes,
|
||||
) -> Result<(), Error> {
|
||||
self.rpc.insert_key(key_type, suri, public).await
|
||||
}
|
||||
|
||||
/// Generate new session keys and returns the corresponding public keys.
|
||||
pub async fn rotate_keys(&self) -> Result<Bytes, Error> {
|
||||
self.rpc.rotate_keys().await
|
||||
}
|
||||
|
||||
/// Checks if the keystore has private keys for the given session public keys.
|
||||
///
|
||||
/// `session_keys` is the SCALE encoded session keys object from the runtime.
|
||||
///
|
||||
/// Returns `true` iff all private keys could be found.
|
||||
pub async fn has_session_keys(&self, session_keys: Bytes) -> Result<bool, Error> {
|
||||
self.rpc.has_session_keys(session_keys).await
|
||||
}
|
||||
|
||||
/// Checks if the keystore has private keys for the given public key and key type.
|
||||
///
|
||||
/// Returns `true` if a private key could be found.
|
||||
pub async fn has_key(
|
||||
&self,
|
||||
public_key: Bytes,
|
||||
key_type: String,
|
||||
) -> Result<bool, Error> {
|
||||
self.rpc.has_key(public_key, key_type).await
|
||||
/// Returns true if the given pallet and event names match this event.
|
||||
fn is_event(pallet: &str, event: &str) -> bool {
|
||||
Self::PALLET == pallet && Self::EVENT == event
|
||||
}
|
||||
}
|
||||
|
||||
@@ -659,3 +153,53 @@ impl codec::Encode for Encoded {
|
||||
self.0.to_owned()
|
||||
}
|
||||
}
|
||||
|
||||
/// A phase of a block's execution.
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Decode)]
|
||||
pub enum Phase {
|
||||
/// Applying an extrinsic.
|
||||
ApplyExtrinsic(u32),
|
||||
/// Finalizing the block.
|
||||
Finalization,
|
||||
/// Initializing the block.
|
||||
Initialization,
|
||||
}
|
||||
|
||||
/// A wrapper for any type `T` which implement encode/decode in a way compatible with `Vec<u8>`.
|
||||
///
|
||||
/// This type is similar to [`WrapperOpaque`], but it differs in the way it stores the type `T`.
|
||||
/// While [`WrapperOpaque`] stores the decoded type, the [`WrapperKeepOpaque`] stores the type only
|
||||
/// in its opaque format, aka as a `Vec<u8>`. To access the real type `T` [`Self::try_decode`] needs
|
||||
/// to be used.
|
||||
#[derive(Debug, Eq, PartialEq, Default, Clone, Decode, Encode)]
|
||||
pub struct WrapperKeepOpaque<T> {
|
||||
data: Vec<u8>,
|
||||
_phantom: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<T: Decode> WrapperKeepOpaque<T> {
|
||||
/// Try to decode the wrapped type from the inner `data`.
|
||||
///
|
||||
/// Returns `None` if the decoding failed.
|
||||
pub fn try_decode(&self) -> Option<T> {
|
||||
T::decode_all(&mut &self.data[..]).ok()
|
||||
}
|
||||
|
||||
/// Returns the length of the encoded `T`.
|
||||
pub fn encoded_len(&self) -> usize {
|
||||
self.data.len()
|
||||
}
|
||||
|
||||
/// Returns the encoded data.
|
||||
pub fn encoded(&self) -> &[u8] {
|
||||
&self.data
|
||||
}
|
||||
|
||||
/// Create from the given encoded `data`.
|
||||
pub fn from_encoded(data: Vec<u8>) -> Self {
|
||||
Self {
|
||||
data,
|
||||
_phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+232
-552
@@ -1,5 +1,5 @@
|
||||
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
|
||||
// This file is part of substrate-subxt.
|
||||
// This file is part of subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
@@ -12,55 +12,52 @@
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with substrate-subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
convert::TryFrom,
|
||||
marker::PhantomData,
|
||||
str::FromStr,
|
||||
};
|
||||
|
||||
use codec::{
|
||||
Decode,
|
||||
Encode,
|
||||
Error as CodecError,
|
||||
};
|
||||
use codec::Error as CodecError;
|
||||
|
||||
use frame_metadata::{
|
||||
DecodeDifferent,
|
||||
PalletConstantMetadata,
|
||||
RuntimeMetadata,
|
||||
RuntimeMetadataLastVersion,
|
||||
RuntimeMetadataPrefixed,
|
||||
StorageEntryModifier,
|
||||
StorageEntryType,
|
||||
StorageHasher,
|
||||
StorageEntryMetadata,
|
||||
META_RESERVED,
|
||||
};
|
||||
use sp_core::storage::StorageKey;
|
||||
|
||||
use crate::Encoded;
|
||||
use crate::{
|
||||
Call,
|
||||
Encoded,
|
||||
};
|
||||
use scale_info::{
|
||||
form::PortableForm,
|
||||
Type,
|
||||
Variant,
|
||||
};
|
||||
|
||||
/// Metadata error.
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum MetadataError {
|
||||
/// Failed to parse metadata.
|
||||
#[error("Error converting substrate metadata: {0}")]
|
||||
Conversion(#[from] ConversionError),
|
||||
/// Module is not in metadata.
|
||||
#[error("Module {0} not found")]
|
||||
ModuleNotFound(String),
|
||||
/// Module is not in metadata.
|
||||
#[error("Module index {0} not found")]
|
||||
ModuleIndexNotFound(u8),
|
||||
#[error("Pallet {0} not found")]
|
||||
PalletNotFound(String),
|
||||
/// Pallet is not in metadata.
|
||||
#[error("Pallet index {0} not found")]
|
||||
PalletIndexNotFound(u8),
|
||||
/// Call is not in metadata.
|
||||
#[error("Call {0} not found")]
|
||||
CallNotFound(&'static str),
|
||||
/// Event is not in metadata.
|
||||
#[error("Event {0} not found")]
|
||||
EventNotFound(u8),
|
||||
#[error("Pallet {0}, Event {0} not found")]
|
||||
EventNotFound(u8, u8),
|
||||
/// Event is not in metadata.
|
||||
#[error("Error {0} not found")]
|
||||
ErrorNotFound(u8),
|
||||
#[error("Pallet {0}, Error {0} not found")]
|
||||
ErrorNotFound(u8, u8),
|
||||
/// Storage is not in metadata.
|
||||
#[error("Storage {0} not found")]
|
||||
StorageNotFound(&'static str),
|
||||
@@ -76,107 +73,91 @@ pub enum MetadataError {
|
||||
/// Constant is not in metadata.
|
||||
#[error("Constant {0} not found")]
|
||||
ConstantNotFound(&'static str),
|
||||
#[error("Type {0} missing from type registry")]
|
||||
TypeNotFound(u32),
|
||||
}
|
||||
|
||||
/// Runtime metadata.
|
||||
#[derive(Clone, Debug, Default)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Metadata {
|
||||
modules: HashMap<String, ModuleMetadata>,
|
||||
modules_with_calls: HashMap<String, ModuleWithCalls>,
|
||||
modules_with_events: HashMap<String, ModuleWithEvents>,
|
||||
modules_with_errors: HashMap<String, ModuleWithErrors>,
|
||||
metadata: RuntimeMetadataLastVersion,
|
||||
pallets: HashMap<String, PalletMetadata>,
|
||||
events: HashMap<(u8, u8), EventMetadata>,
|
||||
errors: HashMap<(u8, u8), ErrorMetadata>,
|
||||
}
|
||||
|
||||
impl Metadata {
|
||||
/// Returns `ModuleMetadata`.
|
||||
pub fn module<S>(&self, name: S) -> Result<&ModuleMetadata, MetadataError>
|
||||
where
|
||||
S: ToString,
|
||||
{
|
||||
let name = name.to_string();
|
||||
self.modules
|
||||
.get(&name)
|
||||
.ok_or(MetadataError::ModuleNotFound(name))
|
||||
/// Returns a reference to [`PalletMetadata`].
|
||||
pub fn pallet(&self, name: &'static str) -> Result<&PalletMetadata, MetadataError> {
|
||||
self.pallets
|
||||
.get(name)
|
||||
.ok_or(MetadataError::PalletNotFound(name.to_string()))
|
||||
}
|
||||
|
||||
/// Returns `ModuleWithCalls`.
|
||||
pub fn module_with_calls<S>(&self, name: S) -> Result<&ModuleWithCalls, MetadataError>
|
||||
where
|
||||
S: ToString,
|
||||
{
|
||||
let name = name.to_string();
|
||||
self.modules_with_calls
|
||||
.get(&name)
|
||||
.ok_or(MetadataError::ModuleNotFound(name))
|
||||
}
|
||||
|
||||
/// Returns Iterator of `ModuleWithEvents`.
|
||||
pub fn modules_with_events(&self) -> impl Iterator<Item = &ModuleWithEvents> {
|
||||
self.modules_with_events.values()
|
||||
}
|
||||
|
||||
/// Returns `ModuleWithEvents`.
|
||||
pub fn module_with_events(
|
||||
/// Returns the metadata for the event at the given pallet and event indices.
|
||||
pub fn event(
|
||||
&self,
|
||||
module_index: u8,
|
||||
) -> Result<&ModuleWithEvents, MetadataError> {
|
||||
self.modules_with_events
|
||||
.values()
|
||||
.find(|&module| module.index == module_index)
|
||||
.ok_or(MetadataError::ModuleIndexNotFound(module_index))
|
||||
pallet_index: u8,
|
||||
event_index: u8,
|
||||
) -> Result<&EventMetadata, MetadataError> {
|
||||
let event = self
|
||||
.events
|
||||
.get(&(pallet_index, event_index))
|
||||
.ok_or(MetadataError::EventNotFound(pallet_index, event_index))?;
|
||||
Ok(event)
|
||||
}
|
||||
|
||||
/// Returns `ModuleWithErrors`.
|
||||
pub fn module_with_errors(
|
||||
/// Returns the metadata for the error at the given pallet and error indices.
|
||||
pub fn error(
|
||||
&self,
|
||||
module_index: u8,
|
||||
) -> Result<&ModuleWithErrors, MetadataError> {
|
||||
self.modules_with_errors
|
||||
.values()
|
||||
.find(|&module| module.index == module_index)
|
||||
.ok_or(MetadataError::ModuleIndexNotFound(module_index))
|
||||
pallet_index: u8,
|
||||
error_index: u8,
|
||||
) -> Result<&ErrorMetadata, MetadataError> {
|
||||
let error = self
|
||||
.errors
|
||||
.get(&(pallet_index, error_index))
|
||||
.ok_or(MetadataError::ErrorNotFound(pallet_index, error_index))?;
|
||||
Ok(error)
|
||||
}
|
||||
|
||||
/// Pretty print metadata.
|
||||
pub fn pretty(&self) -> String {
|
||||
let mut string = String::new();
|
||||
for (name, module) in &self.modules {
|
||||
string.push_str(name.as_str());
|
||||
string.push('\n');
|
||||
for storage in module.storage.keys() {
|
||||
string.push_str(" s ");
|
||||
string.push_str(storage.as_str());
|
||||
string.push('\n');
|
||||
}
|
||||
if let Some(module) = self.modules_with_calls.get(name) {
|
||||
for call in module.calls.keys() {
|
||||
string.push_str(" c ");
|
||||
string.push_str(call.as_str());
|
||||
string.push('\n');
|
||||
}
|
||||
}
|
||||
if let Some(module) = self.modules_with_events.get(name) {
|
||||
for event in module.events.values() {
|
||||
string.push_str(" e ");
|
||||
string.push_str(event.name.as_str());
|
||||
string.push('\n');
|
||||
}
|
||||
}
|
||||
}
|
||||
string
|
||||
/// Resolve a type definition.
|
||||
pub fn resolve_type(&self, id: u32) -> Option<&Type<PortableForm>> {
|
||||
self.metadata.types.resolve(id)
|
||||
}
|
||||
|
||||
/// Return the runtime metadata.
|
||||
pub fn runtime_metadata(&self) -> &RuntimeMetadataLastVersion {
|
||||
&self.metadata
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ModuleMetadata {
|
||||
pub struct PalletMetadata {
|
||||
index: u8,
|
||||
name: String,
|
||||
storage: HashMap<String, StorageMetadata>,
|
||||
constants: HashMap<String, ModuleConstantMetadata>,
|
||||
calls: HashMap<String, u8>,
|
||||
storage: HashMap<String, StorageEntryMetadata<PortableForm>>,
|
||||
constants: HashMap<String, PalletConstantMetadata<PortableForm>>,
|
||||
}
|
||||
|
||||
impl ModuleMetadata {
|
||||
pub fn storage(&self, key: &'static str) -> Result<&StorageMetadata, MetadataError> {
|
||||
impl PalletMetadata {
|
||||
pub fn encode_call<C>(&self, call: &C) -> Result<Encoded, MetadataError>
|
||||
where
|
||||
C: Call,
|
||||
{
|
||||
let fn_index = self
|
||||
.calls
|
||||
.get(C::FUNCTION)
|
||||
.ok_or(MetadataError::CallNotFound(C::FUNCTION))?;
|
||||
let mut bytes = vec![self.index, *fn_index];
|
||||
bytes.extend(call.encode());
|
||||
Ok(Encoded(bytes))
|
||||
}
|
||||
|
||||
pub fn storage(
|
||||
&self,
|
||||
key: &'static str,
|
||||
) -> Result<&StorageEntryMetadata<PortableForm>, MetadataError> {
|
||||
self.storage
|
||||
.get(key)
|
||||
.ok_or(MetadataError::StorageNotFound(key))
|
||||
@@ -186,7 +167,7 @@ impl ModuleMetadata {
|
||||
pub fn constant(
|
||||
&self,
|
||||
key: &'static str,
|
||||
) -> Result<&ModuleConstantMetadata, MetadataError> {
|
||||
) -> Result<&PalletConstantMetadata<PortableForm>, MetadataError> {
|
||||
self.constants
|
||||
.get(key)
|
||||
.ok_or(MetadataError::ConstantNotFound(key))
|
||||
@@ -194,484 +175,183 @@ impl ModuleMetadata {
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ModuleWithCalls {
|
||||
index: u8,
|
||||
calls: HashMap<String, u8>,
|
||||
pub struct EventMetadata {
|
||||
pallet: String,
|
||||
event: String,
|
||||
variant: Variant<PortableForm>,
|
||||
}
|
||||
|
||||
impl ModuleWithCalls {
|
||||
pub fn call<T: Encode>(
|
||||
&self,
|
||||
function: &'static str,
|
||||
params: T,
|
||||
) -> Result<Encoded, MetadataError> {
|
||||
let fn_index = self
|
||||
.calls
|
||||
.get(function)
|
||||
.ok_or(MetadataError::CallNotFound(function))?;
|
||||
let mut bytes = vec![self.index, *fn_index];
|
||||
bytes.extend(params.encode());
|
||||
Ok(Encoded(bytes))
|
||||
impl EventMetadata {
|
||||
/// Get the name of the pallet from which the event was emitted.
|
||||
pub fn pallet(&self) -> &str {
|
||||
&self.pallet
|
||||
}
|
||||
|
||||
/// Get the name of the pallet event which was emitted.
|
||||
pub fn event(&self) -> &str {
|
||||
&self.event
|
||||
}
|
||||
|
||||
/// Get the type def variant for the pallet event.
|
||||
pub fn variant(&self) -> &Variant<PortableForm> {
|
||||
&self.variant
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ModuleWithEvents {
|
||||
index: u8,
|
||||
name: String,
|
||||
events: HashMap<u8, ModuleEventMetadata>,
|
||||
pub struct ErrorMetadata {
|
||||
pallet: String,
|
||||
error: String,
|
||||
variant: Variant<PortableForm>,
|
||||
}
|
||||
|
||||
impl ModuleWithEvents {
|
||||
pub fn name(&self) -> &str {
|
||||
&self.name
|
||||
impl ErrorMetadata {
|
||||
/// Get the name of the pallet from which the error originates.
|
||||
pub fn pallet(&self) -> &str {
|
||||
&self.pallet
|
||||
}
|
||||
|
||||
pub fn events(&self) -> impl Iterator<Item = &ModuleEventMetadata> {
|
||||
self.events.values()
|
||||
/// Get the name of the specific pallet error.
|
||||
pub fn error(&self) -> &str {
|
||||
&self.error
|
||||
}
|
||||
|
||||
pub fn event(&self, index: u8) -> Result<&ModuleEventMetadata, MetadataError> {
|
||||
self.events
|
||||
.get(&index)
|
||||
.ok_or(MetadataError::EventNotFound(index))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ModuleWithErrors {
|
||||
index: u8,
|
||||
name: String,
|
||||
errors: HashMap<u8, String>,
|
||||
}
|
||||
|
||||
impl ModuleWithErrors {
|
||||
pub fn name(&self) -> &str {
|
||||
&self.name
|
||||
}
|
||||
|
||||
pub fn error(&self, index: u8) -> Result<&String, MetadataError> {
|
||||
self.errors
|
||||
.get(&index)
|
||||
.ok_or(MetadataError::ErrorNotFound(index))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct StorageMetadata {
|
||||
module_prefix: String,
|
||||
storage_prefix: String,
|
||||
modifier: StorageEntryModifier,
|
||||
ty: StorageEntryType,
|
||||
default: Vec<u8>,
|
||||
}
|
||||
|
||||
impl StorageMetadata {
|
||||
pub fn prefix(&self) -> StorageKey {
|
||||
let mut bytes = sp_core::twox_128(self.module_prefix.as_bytes()).to_vec();
|
||||
bytes.extend(&sp_core::twox_128(self.storage_prefix.as_bytes())[..]);
|
||||
StorageKey(bytes)
|
||||
}
|
||||
|
||||
pub fn default<V: Decode>(&self) -> Result<V, MetadataError> {
|
||||
Decode::decode(&mut &self.default[..]).map_err(MetadataError::DefaultError)
|
||||
}
|
||||
|
||||
pub fn hash(hasher: &StorageHasher, bytes: &[u8]) -> Vec<u8> {
|
||||
match hasher {
|
||||
StorageHasher::Identity => bytes.to_vec(),
|
||||
StorageHasher::Blake2_128 => sp_core::blake2_128(bytes).to_vec(),
|
||||
StorageHasher::Blake2_128Concat => {
|
||||
// copied from substrate Blake2_128Concat::hash since StorageHasher is not public
|
||||
sp_core::blake2_128(bytes)
|
||||
.iter()
|
||||
.chain(bytes)
|
||||
.cloned()
|
||||
.collect()
|
||||
}
|
||||
StorageHasher::Blake2_256 => sp_core::blake2_256(bytes).to_vec(),
|
||||
StorageHasher::Twox128 => sp_core::twox_128(bytes).to_vec(),
|
||||
StorageHasher::Twox256 => sp_core::twox_256(bytes).to_vec(),
|
||||
StorageHasher::Twox64Concat => {
|
||||
sp_core::twox_64(bytes)
|
||||
.iter()
|
||||
.chain(bytes)
|
||||
.cloned()
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn hash_key<K: Encode>(hasher: &StorageHasher, key: &K) -> Vec<u8> {
|
||||
Self::hash(hasher, &key.encode())
|
||||
}
|
||||
|
||||
pub fn plain(&self) -> Result<StoragePlain, MetadataError> {
|
||||
match &self.ty {
|
||||
StorageEntryType::Plain(_) => {
|
||||
Ok(StoragePlain {
|
||||
prefix: self.prefix().0,
|
||||
})
|
||||
}
|
||||
_ => Err(MetadataError::StorageTypeError),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn map<K: Encode>(&self) -> Result<StorageMap<K>, MetadataError> {
|
||||
match &self.ty {
|
||||
StorageEntryType::Map { hasher, .. } => {
|
||||
Ok(StorageMap {
|
||||
_marker: PhantomData,
|
||||
prefix: self.prefix().0,
|
||||
hasher: hasher.clone(),
|
||||
})
|
||||
}
|
||||
_ => Err(MetadataError::StorageTypeError),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn double_map<K1: Encode, K2: Encode>(
|
||||
&self,
|
||||
) -> Result<StorageDoubleMap<K1, K2>, MetadataError> {
|
||||
match &self.ty {
|
||||
StorageEntryType::DoubleMap {
|
||||
hasher,
|
||||
key2_hasher,
|
||||
..
|
||||
} => {
|
||||
Ok(StorageDoubleMap {
|
||||
_marker: PhantomData,
|
||||
prefix: self.prefix().0,
|
||||
hasher1: hasher.clone(),
|
||||
hasher2: key2_hasher.clone(),
|
||||
})
|
||||
}
|
||||
_ => Err(MetadataError::StorageTypeError),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct StoragePlain {
|
||||
prefix: Vec<u8>,
|
||||
}
|
||||
|
||||
impl StoragePlain {
|
||||
pub fn key(&self) -> StorageKey {
|
||||
StorageKey(self.prefix.clone())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct StorageMap<K> {
|
||||
_marker: PhantomData<K>,
|
||||
prefix: Vec<u8>,
|
||||
hasher: StorageHasher,
|
||||
}
|
||||
|
||||
impl<K: Encode> StorageMap<K> {
|
||||
pub fn key(&self, key: &K) -> StorageKey {
|
||||
let mut bytes = self.prefix.clone();
|
||||
bytes.extend(StorageMetadata::hash_key(&self.hasher, key));
|
||||
StorageKey(bytes)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct StorageDoubleMap<K1, K2> {
|
||||
_marker: PhantomData<(K1, K2)>,
|
||||
prefix: Vec<u8>,
|
||||
hasher1: StorageHasher,
|
||||
hasher2: StorageHasher,
|
||||
}
|
||||
|
||||
impl<K1: Encode, K2: Encode> StorageDoubleMap<K1, K2> {
|
||||
pub fn key(&self, key1: &K1, key2: &K2) -> StorageKey {
|
||||
let mut bytes = self.prefix.clone();
|
||||
bytes.extend(StorageMetadata::hash_key(&self.hasher1, key1));
|
||||
bytes.extend(StorageMetadata::hash_key(&self.hasher2, key2));
|
||||
StorageKey(bytes)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ModuleEventMetadata {
|
||||
pub name: String,
|
||||
arguments: Vec<EventArg>,
|
||||
}
|
||||
|
||||
impl ModuleEventMetadata {
|
||||
pub fn arguments(&self) -> Vec<EventArg> {
|
||||
self.arguments.to_vec()
|
||||
}
|
||||
}
|
||||
|
||||
/// Naive representation of event argument types, supports current set of substrate EventArg types.
|
||||
/// If and when Substrate uses `type-metadata`, this can be replaced.
|
||||
///
|
||||
/// Used to calculate the size of a instance of an event variant without having the concrete type,
|
||||
/// so the raw bytes can be extracted from the encoded `Vec<EventRecord<E>>` (without `E` defined).
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
|
||||
pub enum EventArg {
|
||||
Primitive(String),
|
||||
Vec(Box<EventArg>),
|
||||
Tuple(Vec<EventArg>),
|
||||
Option(Box<EventArg>),
|
||||
}
|
||||
|
||||
impl FromStr for EventArg {
|
||||
type Err = ConversionError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
if s.starts_with("Vec<") {
|
||||
if s.ends_with('>') {
|
||||
Ok(EventArg::Vec(Box::new(s[4..s.len() - 1].parse()?)))
|
||||
} else {
|
||||
Err(ConversionError::InvalidEventArg(
|
||||
s.to_string(),
|
||||
"Expected closing `>` for `Vec`",
|
||||
))
|
||||
}
|
||||
} else if s.starts_with("Option<") {
|
||||
if s.ends_with('>') {
|
||||
Ok(EventArg::Option(Box::new(s[7..s.len() - 1].parse()?)))
|
||||
} else {
|
||||
Err(ConversionError::InvalidEventArg(
|
||||
s.to_string(),
|
||||
"Expected closing `>` for `Option`",
|
||||
))
|
||||
}
|
||||
} else if s.starts_with('(') {
|
||||
if s.ends_with(')') {
|
||||
let mut args = Vec::new();
|
||||
for arg in s[1..s.len() - 1].split(',') {
|
||||
let arg = arg.trim().parse()?;
|
||||
args.push(arg)
|
||||
}
|
||||
Ok(EventArg::Tuple(args))
|
||||
} else {
|
||||
Err(ConversionError::InvalidEventArg(
|
||||
s.to_string(),
|
||||
"Expecting closing `)` for tuple",
|
||||
))
|
||||
}
|
||||
} else {
|
||||
Ok(EventArg::Primitive(s.to_string()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl EventArg {
|
||||
/// Returns all primitive types for this EventArg
|
||||
pub fn primitives(&self) -> Vec<String> {
|
||||
match self {
|
||||
EventArg::Primitive(p) => vec![p.clone()],
|
||||
EventArg::Vec(arg) => arg.primitives(),
|
||||
EventArg::Option(arg) => arg.primitives(),
|
||||
EventArg::Tuple(args) => {
|
||||
let mut primitives = Vec::new();
|
||||
for arg in args {
|
||||
primitives.extend(arg.primitives())
|
||||
}
|
||||
primitives
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ModuleConstantMetadata {
|
||||
name: String,
|
||||
ty: String,
|
||||
value: Vec<u8>,
|
||||
documentation: Vec<String>,
|
||||
}
|
||||
|
||||
impl ModuleConstantMetadata {
|
||||
/// Name
|
||||
pub fn name(&self) -> &String {
|
||||
&self.name
|
||||
}
|
||||
|
||||
/// Constant value (decoded)
|
||||
pub fn value<V: Decode>(&self) -> Result<V, MetadataError> {
|
||||
Decode::decode(&mut &self.value[..]).map_err(MetadataError::ConstantValueError)
|
||||
}
|
||||
|
||||
/// Type (as defined in the runtime)
|
||||
pub fn ty(&self) -> &String {
|
||||
&self.ty
|
||||
}
|
||||
|
||||
/// Documentation
|
||||
pub fn documentation(&self) -> &Vec<String> {
|
||||
&self.documentation
|
||||
/// Get the description of the specific pallet error.
|
||||
pub fn description(&self) -> &[String] {
|
||||
self.variant.docs()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum ConversionError {
|
||||
pub enum InvalidMetadataError {
|
||||
#[error("Invalid prefix")]
|
||||
InvalidPrefix,
|
||||
#[error("Invalid version")]
|
||||
InvalidVersion,
|
||||
#[error("Expected DecodeDifferent::Decoded")]
|
||||
ExpectedDecoded,
|
||||
#[error("Invalid event arg {0}")]
|
||||
InvalidEventArg(String, &'static str),
|
||||
#[error("Type {0} missing from type registry")]
|
||||
MissingType(u32),
|
||||
#[error("Type {0} was not a variant/enum type")]
|
||||
TypeDefNotVariant(u32),
|
||||
}
|
||||
|
||||
impl TryFrom<RuntimeMetadataPrefixed> for Metadata {
|
||||
type Error = MetadataError;
|
||||
type Error = InvalidMetadataError;
|
||||
|
||||
fn try_from(metadata: RuntimeMetadataPrefixed) -> Result<Self, Self::Error> {
|
||||
if metadata.0 != META_RESERVED {
|
||||
return Err(ConversionError::InvalidPrefix.into())
|
||||
return Err(InvalidMetadataError::InvalidPrefix.into())
|
||||
}
|
||||
let meta = match metadata.1 {
|
||||
RuntimeMetadata::V13(meta) => meta,
|
||||
_ => return Err(ConversionError::InvalidVersion.into()),
|
||||
let metadata = match metadata.1 {
|
||||
RuntimeMetadata::V14(meta) => meta,
|
||||
_ => return Err(InvalidMetadataError::InvalidVersion.into()),
|
||||
};
|
||||
let mut modules = HashMap::new();
|
||||
let mut modules_with_calls = HashMap::new();
|
||||
let mut modules_with_events = HashMap::new();
|
||||
let mut modules_with_errors = HashMap::new();
|
||||
for module in convert(meta.modules)?.into_iter() {
|
||||
let module_name = convert(module.name.clone())?;
|
||||
|
||||
let mut constant_map = HashMap::new();
|
||||
for constant in convert(module.constants)?.into_iter() {
|
||||
let constant_meta = convert_constant(constant)?;
|
||||
constant_map.insert(constant_meta.name.clone(), constant_meta);
|
||||
let get_type_def_variant = |type_id: u32| {
|
||||
let ty = metadata
|
||||
.types
|
||||
.resolve(type_id)
|
||||
.ok_or(InvalidMetadataError::MissingType(type_id))?;
|
||||
if let scale_info::TypeDef::Variant(var) = ty.type_def() {
|
||||
Ok(var)
|
||||
} else {
|
||||
Err(InvalidMetadataError::TypeDefNotVariant(type_id))
|
||||
}
|
||||
};
|
||||
let pallets = metadata
|
||||
.pallets
|
||||
.iter()
|
||||
.map(|pallet| {
|
||||
let calls = pallet.calls.as_ref().map_or(Ok(HashMap::new()), |call| {
|
||||
let type_def_variant = get_type_def_variant(call.ty.id())?;
|
||||
let calls = type_def_variant
|
||||
.variants()
|
||||
.iter()
|
||||
.map(|v| (v.name().clone(), v.index()))
|
||||
.collect();
|
||||
Ok(calls)
|
||||
})?;
|
||||
|
||||
let mut storage_map = HashMap::new();
|
||||
if let Some(storage) = module.storage {
|
||||
let storage = convert(storage)?;
|
||||
let module_prefix = convert(storage.prefix)?;
|
||||
for entry in convert(storage.entries)?.into_iter() {
|
||||
let storage_prefix = convert(entry.name.clone())?;
|
||||
let entry = convert_entry(
|
||||
module_prefix.clone(),
|
||||
storage_prefix.clone(),
|
||||
entry,
|
||||
)?;
|
||||
storage_map.insert(storage_prefix, entry);
|
||||
}
|
||||
}
|
||||
modules.insert(
|
||||
module_name.clone(),
|
||||
ModuleMetadata {
|
||||
index: module.index,
|
||||
name: module_name.clone(),
|
||||
storage: storage_map,
|
||||
constants: constant_map,
|
||||
},
|
||||
);
|
||||
let storage = pallet.storage.as_ref().map_or(HashMap::new(), |storage| {
|
||||
storage
|
||||
.entries
|
||||
.iter()
|
||||
.map(|entry| (entry.name.clone(), entry.clone()))
|
||||
.collect()
|
||||
});
|
||||
|
||||
if let Some(calls) = module.calls {
|
||||
let mut call_map = HashMap::new();
|
||||
for (index, call) in convert(calls)?.into_iter().enumerate() {
|
||||
let name = convert(call.name)?;
|
||||
call_map.insert(name, index as u8);
|
||||
}
|
||||
modules_with_calls.insert(
|
||||
module_name.clone(),
|
||||
ModuleWithCalls {
|
||||
index: module.index,
|
||||
calls: call_map,
|
||||
},
|
||||
);
|
||||
}
|
||||
if let Some(events) = module.event {
|
||||
let mut event_map = HashMap::new();
|
||||
for (index, event) in convert(events)?.into_iter().enumerate() {
|
||||
event_map.insert(index as u8, convert_event(event)?);
|
||||
}
|
||||
modules_with_events.insert(
|
||||
module_name.clone(),
|
||||
ModuleWithEvents {
|
||||
index: module.index,
|
||||
name: module_name.clone(),
|
||||
events: event_map,
|
||||
},
|
||||
);
|
||||
}
|
||||
let mut error_map = HashMap::new();
|
||||
for (index, error) in convert(module.errors)?.into_iter().enumerate() {
|
||||
error_map.insert(index as u8, convert_error(error)?);
|
||||
}
|
||||
modules_with_errors.insert(
|
||||
module_name.clone(),
|
||||
ModuleWithErrors {
|
||||
index: module.index,
|
||||
name: module_name.clone(),
|
||||
errors: error_map,
|
||||
},
|
||||
);
|
||||
}
|
||||
Ok(Metadata {
|
||||
modules,
|
||||
modules_with_calls,
|
||||
modules_with_events,
|
||||
modules_with_errors,
|
||||
let constants = pallet
|
||||
.constants
|
||||
.iter()
|
||||
.map(|constant| (constant.name.clone(), constant.clone()))
|
||||
.collect();
|
||||
|
||||
let pallet_metadata = PalletMetadata {
|
||||
index: pallet.index,
|
||||
name: pallet.name.to_string(),
|
||||
calls,
|
||||
storage,
|
||||
constants,
|
||||
};
|
||||
|
||||
Ok((pallet.name.to_string(), pallet_metadata))
|
||||
})
|
||||
.collect::<Result<_, _>>()?;
|
||||
|
||||
let pallet_events = metadata
|
||||
.pallets
|
||||
.iter()
|
||||
.filter_map(|pallet| {
|
||||
pallet.event.as_ref().map(|event| {
|
||||
let type_def_variant = get_type_def_variant(event.ty.id())?;
|
||||
Ok((pallet, type_def_variant))
|
||||
})
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
let events = pallet_events
|
||||
.iter()
|
||||
.flat_map(|(pallet, type_def_variant)| {
|
||||
type_def_variant.variants().iter().map(move |var| {
|
||||
let key = (pallet.index, var.index());
|
||||
let value = EventMetadata {
|
||||
pallet: pallet.name.clone(),
|
||||
event: var.name().clone(),
|
||||
variant: var.clone(),
|
||||
};
|
||||
(key, value)
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
|
||||
let pallet_errors = metadata
|
||||
.pallets
|
||||
.iter()
|
||||
.filter_map(|pallet| {
|
||||
pallet.error.as_ref().map(|error| {
|
||||
let type_def_variant = get_type_def_variant(error.ty.id())?;
|
||||
Ok((pallet, type_def_variant))
|
||||
})
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
let errors = pallet_errors
|
||||
.iter()
|
||||
.flat_map(|(pallet, type_def_variant)| {
|
||||
type_def_variant.variants().iter().map(move |var| {
|
||||
let key = (pallet.index, var.index());
|
||||
let value = ErrorMetadata {
|
||||
pallet: pallet.name.clone(),
|
||||
error: var.name().clone(),
|
||||
variant: var.clone(),
|
||||
};
|
||||
(key, value)
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok(Self {
|
||||
metadata,
|
||||
pallets,
|
||||
events,
|
||||
errors,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn convert<B: 'static, O: 'static>(
|
||||
dd: DecodeDifferent<B, O>,
|
||||
) -> Result<O, ConversionError> {
|
||||
match dd {
|
||||
DecodeDifferent::Decoded(value) => Ok(value),
|
||||
_ => Err(ConversionError::ExpectedDecoded),
|
||||
}
|
||||
}
|
||||
|
||||
fn convert_event(
|
||||
event: frame_metadata::EventMetadata,
|
||||
) -> Result<ModuleEventMetadata, ConversionError> {
|
||||
let name = convert(event.name)?;
|
||||
let mut arguments = Vec::new();
|
||||
for arg in convert(event.arguments)? {
|
||||
let arg = arg.parse::<EventArg>()?;
|
||||
arguments.push(arg);
|
||||
}
|
||||
Ok(ModuleEventMetadata { name, arguments })
|
||||
}
|
||||
|
||||
fn convert_entry(
|
||||
module_prefix: String,
|
||||
storage_prefix: String,
|
||||
entry: frame_metadata::StorageEntryMetadata,
|
||||
) -> Result<StorageMetadata, ConversionError> {
|
||||
let default = convert(entry.default)?;
|
||||
Ok(StorageMetadata {
|
||||
module_prefix,
|
||||
storage_prefix,
|
||||
modifier: entry.modifier,
|
||||
ty: entry.ty,
|
||||
default,
|
||||
})
|
||||
}
|
||||
|
||||
fn convert_error(
|
||||
error: frame_metadata::ErrorMetadata,
|
||||
) -> Result<String, ConversionError> {
|
||||
convert(error.name)
|
||||
}
|
||||
|
||||
fn convert_constant(
|
||||
constant: frame_metadata::ModuleConstantMetadata,
|
||||
) -> Result<ModuleConstantMetadata, ConversionError> {
|
||||
let name = convert(constant.name)?;
|
||||
let ty = convert(constant.ty)?;
|
||||
let value = convert(constant.value)?;
|
||||
let documentation = convert(constant.documentation)?;
|
||||
Ok(ModuleConstantMetadata {
|
||||
name,
|
||||
ty,
|
||||
value,
|
||||
documentation,
|
||||
})
|
||||
}
|
||||
|
||||
+80
-50
@@ -1,5 +1,5 @@
|
||||
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
|
||||
// This file is part of substrate-subxt.
|
||||
// This file is part of subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
@@ -12,11 +12,13 @@
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with substrate-subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! RPC types and client for interacting with a substrate node.
|
||||
|
||||
// jsonrpsee subscriptions are interminable.
|
||||
// Allows `while let status = subscription.next().await {}`
|
||||
// Related: https://github.com/paritytech/substrate-subxt/issues/66
|
||||
// Related: https://github.com/paritytech/subxt/issues/66
|
||||
#![allow(irrefutable_let_patterns)]
|
||||
|
||||
use std::sync::Arc;
|
||||
@@ -31,7 +33,10 @@ use core::{
|
||||
marker::PhantomData,
|
||||
};
|
||||
use frame_metadata::RuntimeMetadataPrefixed;
|
||||
use jsonrpsee_http_client::HttpClient;
|
||||
use jsonrpsee_http_client::{
|
||||
HttpClient,
|
||||
HttpClientBuilder,
|
||||
};
|
||||
use jsonrpsee_types::{
|
||||
to_json_value,
|
||||
traits::{
|
||||
@@ -43,7 +48,10 @@ use jsonrpsee_types::{
|
||||
JsonValue,
|
||||
Subscription,
|
||||
};
|
||||
use jsonrpsee_ws_client::WsClient;
|
||||
use jsonrpsee_ws_client::{
|
||||
WsClient,
|
||||
WsClientBuilder,
|
||||
};
|
||||
use serde::{
|
||||
Deserialize,
|
||||
Serialize,
|
||||
@@ -55,10 +63,7 @@ use sp_core::{
|
||||
StorageKey,
|
||||
},
|
||||
Bytes,
|
||||
};
|
||||
use sp_rpc::{
|
||||
list::ListOrValue,
|
||||
number::NumberOrHex,
|
||||
U256,
|
||||
};
|
||||
use sp_runtime::{
|
||||
generic::{
|
||||
@@ -75,22 +80,48 @@ use crate::{
|
||||
EventsDecoder,
|
||||
RawEvent,
|
||||
},
|
||||
frame::{
|
||||
system::System,
|
||||
Event,
|
||||
},
|
||||
metadata::Metadata,
|
||||
runtimes::Runtime,
|
||||
storage::StorageKeyPrefix,
|
||||
subscription::{
|
||||
EventStorageSubscription,
|
||||
EventSubscription,
|
||||
FinalizedEventStorageSubscription,
|
||||
SystemEvents,
|
||||
},
|
||||
Config,
|
||||
Event,
|
||||
Metadata,
|
||||
};
|
||||
|
||||
/// A number type that can be serialized both as a number or a string that encodes a number in a
|
||||
/// string.
|
||||
///
|
||||
/// We allow two representations of the block number as input. Either we deserialize to the type
|
||||
/// that is specified in the block type or we attempt to parse given hex value.
|
||||
///
|
||||
/// The primary motivation for having this type is to avoid overflows when using big integers in
|
||||
/// JavaScript (which we consider as an important RPC API consumer).
|
||||
#[derive(Copy, Clone, Serialize, Deserialize, Debug, PartialEq)]
|
||||
#[serde(untagged)]
|
||||
pub enum NumberOrHex {
|
||||
/// The number represented directly.
|
||||
Number(u64),
|
||||
/// Hex representation of the number.
|
||||
Hex(U256),
|
||||
}
|
||||
|
||||
/// RPC list or value wrapper.
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq)]
|
||||
#[serde(untagged)]
|
||||
pub enum ListOrValue<T> {
|
||||
/// A list of values of given type.
|
||||
List(Vec<T>),
|
||||
/// A single value of given type.
|
||||
Value(T),
|
||||
}
|
||||
|
||||
/// Alias for the type of a block returned by `chain_getBlock`
|
||||
pub type ChainBlock<T> =
|
||||
SignedBlock<Block<<T as System>::Header, <T as System>::Extrinsic>>;
|
||||
SignedBlock<Block<<T as Config>::Header, <T as Config>::Extrinsic>>;
|
||||
|
||||
/// Wrapper for NumberOrHex to allow custom From impls
|
||||
#[derive(Serialize)]
|
||||
@@ -153,9 +184,6 @@ pub enum TransactionStatus<Hash, BlockHash> {
|
||||
Invalid,
|
||||
}
|
||||
|
||||
#[cfg(feature = "client")]
|
||||
use substrate_subxt_client::SubxtClient;
|
||||
|
||||
/// Rpc client wrapper.
|
||||
/// This is workaround because adding generic types causes the macros to fail.
|
||||
#[derive(Clone)]
|
||||
@@ -165,12 +193,27 @@ pub enum RpcClient {
|
||||
/// JSONRPC client HTTP transport.
|
||||
// NOTE: Arc because `HttpClient` is not clone.
|
||||
Http(Arc<HttpClient>),
|
||||
#[cfg(feature = "client")]
|
||||
/// Embedded substrate node.
|
||||
Subxt(SubxtClient),
|
||||
}
|
||||
|
||||
impl RpcClient {
|
||||
/// Create a new [`RpcClient`] from the given URL.
|
||||
///
|
||||
/// Infers the protocol from the URL, supports:
|
||||
/// - Websockets (`ws://`, `wss://`)
|
||||
/// - Http (`http://`, `https://`)
|
||||
pub async fn try_from_url(url: &str) -> Result<Self, Error> {
|
||||
if url.starts_with("ws://") || url.starts_with("wss://") {
|
||||
let client = WsClientBuilder::default()
|
||||
.max_notifs_per_subscription(4096)
|
||||
.build(url)
|
||||
.await?;
|
||||
Ok(RpcClient::WebSocket(Arc::new(client)))
|
||||
} else {
|
||||
let client = HttpClientBuilder::default().build(&url)?;
|
||||
Ok(RpcClient::Http(Arc::new(client)))
|
||||
}
|
||||
}
|
||||
|
||||
/// Start a JSON-RPC request.
|
||||
pub async fn request<'a, T: DeserializeOwned + std::fmt::Debug>(
|
||||
&self,
|
||||
@@ -178,15 +221,13 @@ impl RpcClient {
|
||||
params: &[JsonValue],
|
||||
) -> Result<T, Error> {
|
||||
let params = params.into();
|
||||
log::debug!("request {}: {:?}", method, params);
|
||||
let data = match self {
|
||||
Self::WebSocket(inner) => {
|
||||
inner.request(method, params).await.map_err(Into::into)
|
||||
}
|
||||
Self::Http(inner) => inner.request(method, params).await.map_err(Into::into),
|
||||
#[cfg(feature = "client")]
|
||||
Self::Subxt(inner) => inner.request(method, params).await.map_err(Into::into),
|
||||
};
|
||||
log::debug!("{}: {:?}", method, data);
|
||||
data
|
||||
}
|
||||
|
||||
@@ -211,13 +252,6 @@ impl RpcClient {
|
||||
)
|
||||
.into())
|
||||
}
|
||||
#[cfg(feature = "client")]
|
||||
Self::Subxt(inner) => {
|
||||
inner
|
||||
.subscribe(subscribe_method, params, unsubscribe_method)
|
||||
.await
|
||||
.map_err(Into::into)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -246,13 +280,6 @@ impl From<Arc<HttpClient>> for RpcClient {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "client")]
|
||||
impl From<SubxtClient> for RpcClient {
|
||||
fn from(client: SubxtClient) -> Self {
|
||||
RpcClient::Subxt(client)
|
||||
}
|
||||
}
|
||||
|
||||
/// ReadProof struct returned by the RPC
|
||||
///
|
||||
/// # Note
|
||||
@@ -269,14 +296,14 @@ pub struct ReadProof<Hash> {
|
||||
}
|
||||
|
||||
/// Client for substrate rpc interfaces
|
||||
pub struct Rpc<T: Runtime> {
|
||||
pub struct Rpc<T: Config> {
|
||||
/// Rpc client for sending requests.
|
||||
pub client: RpcClient,
|
||||
marker: PhantomData<T>,
|
||||
accept_weak_inclusion: bool,
|
||||
}
|
||||
|
||||
impl<T: Runtime> Clone for Rpc<T> {
|
||||
impl<T: Config> Clone for Rpc<T> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
client: self.client.clone(),
|
||||
@@ -286,7 +313,8 @@ impl<T: Runtime> Clone for Rpc<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Runtime> Rpc<T> {
|
||||
impl<T: Config> Rpc<T> {
|
||||
/// Create a new [`Rpc`]
|
||||
pub fn new(client: RpcClient) -> Self {
|
||||
Self {
|
||||
client,
|
||||
@@ -317,11 +345,12 @@ impl<T: Runtime> Rpc<T> {
|
||||
/// If `start_key` is passed, return next keys in storage in lexicographic order.
|
||||
pub async fn storage_keys_paged(
|
||||
&self,
|
||||
prefix: Option<StorageKey>,
|
||||
prefix: Option<StorageKeyPrefix>,
|
||||
count: u32,
|
||||
start_key: Option<StorageKey>,
|
||||
hash: Option<T::Hash>,
|
||||
) -> Result<Vec<StorageKey>, Error> {
|
||||
let prefix = prefix.map(|p| p.to_storage_key());
|
||||
let params = &[
|
||||
to_json_value(prefix)?,
|
||||
to_json_value(count)?,
|
||||
@@ -338,7 +367,7 @@ impl<T: Runtime> Rpc<T> {
|
||||
keys: Vec<StorageKey>,
|
||||
from: T::Hash,
|
||||
to: Option<T::Hash>,
|
||||
) -> Result<Vec<StorageChangeSet<<T as System>::Hash>>, Error> {
|
||||
) -> Result<Vec<StorageChangeSet<T::Hash>>, Error> {
|
||||
let params = &[
|
||||
to_json_value(keys)?,
|
||||
to_json_value(from)?,
|
||||
@@ -355,7 +384,7 @@ impl<T: Runtime> Rpc<T> {
|
||||
&self,
|
||||
keys: &[StorageKey],
|
||||
at: Option<T::Hash>,
|
||||
) -> Result<Vec<StorageChangeSet<<T as System>::Hash>>, Error> {
|
||||
) -> Result<Vec<StorageChangeSet<T::Hash>>, Error> {
|
||||
let params = &[to_json_value(keys)?, to_json_value(at)?];
|
||||
self.client
|
||||
.request("state_queryStorageAt", params)
|
||||
@@ -520,6 +549,7 @@ impl<T: Runtime> Rpc<T> {
|
||||
Ok(xt_hash)
|
||||
}
|
||||
|
||||
/// Create and submit an extrinsic and return a subscription to the events triggered.
|
||||
pub async fn watch_extrinsic<E: Encode>(
|
||||
&self,
|
||||
extrinsic: E,
|
||||
@@ -678,7 +708,7 @@ impl<T: Runtime> Rpc<T> {
|
||||
|
||||
/// Captures data for when an extrinsic is successfully included in a block
|
||||
#[derive(Debug)]
|
||||
pub struct ExtrinsicSuccess<T: System> {
|
||||
pub struct ExtrinsicSuccess<T: Config> {
|
||||
/// Block hash.
|
||||
pub block: T::Hash,
|
||||
/// Extrinsic hash.
|
||||
@@ -687,20 +717,20 @@ pub struct ExtrinsicSuccess<T: System> {
|
||||
pub events: Vec<RawEvent>,
|
||||
}
|
||||
|
||||
impl<T: System> ExtrinsicSuccess<T> {
|
||||
impl<T: Config> ExtrinsicSuccess<T> {
|
||||
/// Find the Event for the given module/variant, with raw encoded event data.
|
||||
/// Returns `None` if the Event is not found.
|
||||
pub fn find_event_raw(&self, module: &str, variant: &str) -> Option<&RawEvent> {
|
||||
self.events
|
||||
.iter()
|
||||
.find(|raw| raw.module == module && raw.variant == variant)
|
||||
.find(|raw| raw.pallet == module && raw.variant == variant)
|
||||
}
|
||||
|
||||
/// Find the Event for the given module/variant, attempting to decode the event data.
|
||||
/// Returns `None` if the Event is not found.
|
||||
/// Returns `Err` if the data fails to decode into the supplied type.
|
||||
pub fn find_event<E: Event<T>>(&self) -> Result<Option<E>, CodecError> {
|
||||
if let Some(event) = self.find_event_raw(E::MODULE, E::EVENT) {
|
||||
pub fn find_event<E: Event>(&self) -> Result<Option<E>, CodecError> {
|
||||
if let Some(event) = self.find_event_raw(E::PALLET, E::EVENT) {
|
||||
Ok(Some(E::decode(&mut &event.data[..])?))
|
||||
} else {
|
||||
Ok(None)
|
||||
|
||||
-456
@@ -1,456 +0,0 @@
|
||||
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
|
||||
// This file is part of substrate-subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// subxt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with substrate-subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use codec::Encode;
|
||||
use sp_runtime::{
|
||||
generic::Header,
|
||||
impl_opaque_keys,
|
||||
traits::{
|
||||
BlakeTwo256,
|
||||
IdentifyAccount,
|
||||
Verify,
|
||||
},
|
||||
MultiSignature,
|
||||
OpaqueExtrinsic,
|
||||
};
|
||||
use sp_std::prelude::*;
|
||||
|
||||
/// BABE marker struct
|
||||
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
||||
pub struct Babe;
|
||||
|
||||
/// Application specific crypto types
|
||||
///
|
||||
/// # Note
|
||||
///
|
||||
/// These are redefined here to avoid dependencies on the substrate creates where they are defined.
|
||||
/// They must be identical to the definitions in the target substrate version.
|
||||
pub mod app {
|
||||
use sp_application_crypto::{
|
||||
app_crypto,
|
||||
ed25519,
|
||||
key_types,
|
||||
sr25519,
|
||||
};
|
||||
|
||||
/// Authority discovery app crypto types
|
||||
pub mod authority_discovery {
|
||||
use super::*;
|
||||
app_crypto!(sr25519, key_types::AUTHORITY_DISCOVERY);
|
||||
}
|
||||
/// Babe app crypto types
|
||||
pub mod babe {
|
||||
use super::*;
|
||||
app_crypto!(sr25519, key_types::BABE);
|
||||
}
|
||||
/// Im online discovery app crypto types
|
||||
pub mod im_online {
|
||||
use super::*;
|
||||
app_crypto!(ed25519, key_types::IM_ONLINE);
|
||||
}
|
||||
/// Grandpa app crypto types
|
||||
pub mod grandpa {
|
||||
use super::*;
|
||||
app_crypto!(ed25519, key_types::GRANDPA);
|
||||
}
|
||||
/// Validator app crypto types
|
||||
pub mod validator {
|
||||
use super::*;
|
||||
app_crypto!(ed25519, sp_core::crypto::KeyTypeId(*b"para"));
|
||||
}
|
||||
}
|
||||
|
||||
impl sp_runtime::BoundToRuntimeAppPublic for Babe {
|
||||
type Public = app::babe::Public;
|
||||
}
|
||||
|
||||
/// ImOnline marker struct
|
||||
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
||||
pub struct ImOnline;
|
||||
impl sp_runtime::BoundToRuntimeAppPublic for ImOnline {
|
||||
type Public = app::im_online::Public;
|
||||
}
|
||||
|
||||
/// GRANDPA marker struct
|
||||
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
||||
pub struct Grandpa;
|
||||
impl sp_runtime::BoundToRuntimeAppPublic for Grandpa {
|
||||
type Public = app::grandpa::Public;
|
||||
}
|
||||
|
||||
/// Parachain marker struct
|
||||
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
||||
pub struct Parachains;
|
||||
|
||||
impl sp_runtime::BoundToRuntimeAppPublic for Parachains {
|
||||
type Public = app::validator::Public;
|
||||
}
|
||||
|
||||
/// Authority discovery marker struct
|
||||
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
||||
pub struct AuthorityDiscovery;
|
||||
impl sp_runtime::BoundToRuntimeAppPublic for AuthorityDiscovery {
|
||||
type Public = app::authority_discovery::Public;
|
||||
}
|
||||
|
||||
impl_opaque_keys! {
|
||||
/// Substrate base runtime keys
|
||||
pub struct BasicSessionKeys {
|
||||
/// GRANDPA session key
|
||||
pub grandpa: Grandpa,
|
||||
/// BABE session key
|
||||
pub babe: Babe,
|
||||
/// ImOnline session key
|
||||
pub im_online: ImOnline,
|
||||
/// Parachain validation session key
|
||||
pub parachains: Parachains,
|
||||
/// AuthorityDiscovery session key
|
||||
pub authority_discovery: AuthorityDiscovery,
|
||||
}
|
||||
}
|
||||
|
||||
impl_opaque_keys! {
|
||||
/// Polkadot/Kusama runtime keys
|
||||
pub struct SessionKeys {
|
||||
/// GRANDPA session key
|
||||
pub grandpa: Grandpa,
|
||||
/// BABE session key
|
||||
pub babe: Babe,
|
||||
/// ImOnline session key
|
||||
pub im_online: ImOnline,
|
||||
/// ParachainValidator session key
|
||||
pub parachain_validator: Parachains,
|
||||
/// AuthorityDiscovery session key
|
||||
pub authority_discovery: AuthorityDiscovery,
|
||||
}
|
||||
}
|
||||
|
||||
use crate::{
|
||||
extrinsic::{
|
||||
DefaultExtra,
|
||||
SignedExtra,
|
||||
},
|
||||
frame::{
|
||||
balances::{
|
||||
AccountData,
|
||||
Balances,
|
||||
BalancesEventTypeRegistry,
|
||||
},
|
||||
contracts::{
|
||||
Contracts,
|
||||
ContractsEventTypeRegistry,
|
||||
},
|
||||
session::{
|
||||
Session,
|
||||
SessionEventTypeRegistry,
|
||||
},
|
||||
staking::{
|
||||
Staking,
|
||||
StakingEventTypeRegistry,
|
||||
},
|
||||
sudo::{
|
||||
Sudo,
|
||||
SudoEventTypeRegistry,
|
||||
},
|
||||
system::{
|
||||
System,
|
||||
SystemEventTypeRegistry,
|
||||
},
|
||||
},
|
||||
EventTypeRegistry,
|
||||
};
|
||||
|
||||
/// Runtime trait.
|
||||
pub trait Runtime: System + Sized + Send + Sync + 'static {
|
||||
/// Signature type.
|
||||
type Signature: Verify + Encode + Send + Sync + 'static;
|
||||
/// Transaction extras.
|
||||
type Extra: SignedExtra<Self> + Send + Sync + 'static;
|
||||
|
||||
/// Register type sizes for this runtime
|
||||
fn register_type_sizes(event_type_registry: &mut EventTypeRegistry<Self>);
|
||||
}
|
||||
|
||||
/// Concrete type definitions compatible with those in the default substrate `node_runtime`
|
||||
///
|
||||
/// # Note
|
||||
///
|
||||
/// If the concrete types in the target substrate runtime differ from these, a custom Runtime
|
||||
/// definition MUST be used to ensure type compatibility.
|
||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||
pub struct DefaultNodeRuntime;
|
||||
|
||||
impl Staking for DefaultNodeRuntime {}
|
||||
|
||||
impl Runtime for DefaultNodeRuntime {
|
||||
type Signature = MultiSignature;
|
||||
type Extra = DefaultExtra<Self>;
|
||||
|
||||
fn register_type_sizes(event_type_registry: &mut EventTypeRegistry<Self>) {
|
||||
event_type_registry.with_system();
|
||||
event_type_registry.with_balances();
|
||||
event_type_registry.with_session();
|
||||
event_type_registry.with_staking();
|
||||
event_type_registry.with_contracts();
|
||||
event_type_registry.with_sudo();
|
||||
register_default_type_sizes(event_type_registry);
|
||||
}
|
||||
}
|
||||
|
||||
impl System for DefaultNodeRuntime {
|
||||
type Index = u32;
|
||||
type BlockNumber = u32;
|
||||
type Hash = sp_core::H256;
|
||||
type Hashing = BlakeTwo256;
|
||||
type AccountId = <<MultiSignature as Verify>::Signer as IdentifyAccount>::AccountId;
|
||||
type Address = sp_runtime::MultiAddress<Self::AccountId, u32>;
|
||||
type Header = Header<Self::BlockNumber, BlakeTwo256>;
|
||||
type Extrinsic = OpaqueExtrinsic;
|
||||
type AccountData = AccountData<<Self as Balances>::Balance>;
|
||||
}
|
||||
|
||||
impl Balances for DefaultNodeRuntime {
|
||||
type Balance = u128;
|
||||
}
|
||||
|
||||
impl Session for DefaultNodeRuntime {
|
||||
type ValidatorId = <Self as System>::AccountId;
|
||||
type Keys = BasicSessionKeys;
|
||||
}
|
||||
|
||||
impl Contracts for DefaultNodeRuntime {}
|
||||
|
||||
impl Sudo for DefaultNodeRuntime {}
|
||||
|
||||
/// Concrete type definitions compatible with the node template.
|
||||
///
|
||||
/// # Note
|
||||
///
|
||||
/// Main difference is `type Address = AccountId`.
|
||||
/// Also the contracts module is not part of the node template runtime.
|
||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||
pub struct NodeTemplateRuntime;
|
||||
|
||||
impl Runtime for NodeTemplateRuntime {
|
||||
type Signature = MultiSignature;
|
||||
type Extra = DefaultExtra<Self>;
|
||||
|
||||
fn register_type_sizes(event_type_registry: &mut EventTypeRegistry<Self>) {
|
||||
event_type_registry.with_system();
|
||||
event_type_registry.with_balances();
|
||||
event_type_registry.with_session();
|
||||
event_type_registry.with_sudo();
|
||||
register_default_type_sizes(event_type_registry);
|
||||
}
|
||||
}
|
||||
|
||||
impl System for NodeTemplateRuntime {
|
||||
type Index = u32;
|
||||
type BlockNumber = u32;
|
||||
type Hash = sp_core::H256;
|
||||
type Hashing = BlakeTwo256;
|
||||
type AccountId = <<MultiSignature as Verify>::Signer as IdentifyAccount>::AccountId;
|
||||
type Address = sp_runtime::MultiAddress<Self::AccountId, u32>;
|
||||
type Header = Header<Self::BlockNumber, BlakeTwo256>;
|
||||
type Extrinsic = OpaqueExtrinsic;
|
||||
type AccountData = AccountData<<Self as Balances>::Balance>;
|
||||
}
|
||||
|
||||
impl Balances for NodeTemplateRuntime {
|
||||
type Balance = u128;
|
||||
}
|
||||
|
||||
impl Session for NodeTemplateRuntime {
|
||||
type ValidatorId = <Self as System>::AccountId;
|
||||
type Keys = BasicSessionKeys;
|
||||
}
|
||||
|
||||
impl Sudo for NodeTemplateRuntime {}
|
||||
|
||||
/// Concrete type definitions compatible with the node template, with the
|
||||
/// contracts pallet enabled.
|
||||
///
|
||||
/// Inherits types from [`NodeTemplateRuntime`], but adds an implementation for
|
||||
/// the contracts pallet trait.
|
||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||
pub struct ContractsTemplateRuntime;
|
||||
|
||||
impl Runtime for ContractsTemplateRuntime {
|
||||
type Signature = <NodeTemplateRuntime as Runtime>::Signature;
|
||||
type Extra = DefaultExtra<Self>;
|
||||
|
||||
fn register_type_sizes(event_type_registry: &mut EventTypeRegistry<Self>) {
|
||||
event_type_registry.with_system();
|
||||
event_type_registry.with_balances();
|
||||
event_type_registry.with_contracts();
|
||||
event_type_registry.with_sudo();
|
||||
register_default_type_sizes(event_type_registry);
|
||||
}
|
||||
}
|
||||
|
||||
impl System for ContractsTemplateRuntime {
|
||||
type Index = <NodeTemplateRuntime as System>::Index;
|
||||
type BlockNumber = <NodeTemplateRuntime as System>::BlockNumber;
|
||||
type Hash = <NodeTemplateRuntime as System>::Hash;
|
||||
type Hashing = <NodeTemplateRuntime as System>::Hashing;
|
||||
type AccountId = <NodeTemplateRuntime as System>::AccountId;
|
||||
type Address = <NodeTemplateRuntime as System>::Address;
|
||||
type Header = <NodeTemplateRuntime as System>::Header;
|
||||
type Extrinsic = <NodeTemplateRuntime as System>::Extrinsic;
|
||||
type AccountData = <NodeTemplateRuntime as System>::AccountData;
|
||||
}
|
||||
|
||||
impl Balances for ContractsTemplateRuntime {
|
||||
type Balance = <NodeTemplateRuntime as Balances>::Balance;
|
||||
}
|
||||
|
||||
impl Contracts for ContractsTemplateRuntime {}
|
||||
|
||||
impl Sudo for ContractsTemplateRuntime {}
|
||||
|
||||
/// Concrete type definitions compatible with those for kusama, v0.7
|
||||
///
|
||||
/// # Note
|
||||
///
|
||||
/// Main difference is `type Address = AccountId`.
|
||||
/// Also the contracts module is not part of the kusama runtime.
|
||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||
pub struct KusamaRuntime;
|
||||
|
||||
impl Runtime for KusamaRuntime {
|
||||
type Signature = MultiSignature;
|
||||
type Extra = DefaultExtra<Self>;
|
||||
|
||||
fn register_type_sizes(event_type_registry: &mut EventTypeRegistry<Self>) {
|
||||
event_type_registry.with_system();
|
||||
event_type_registry.with_balances();
|
||||
event_type_registry.with_session();
|
||||
event_type_registry.with_staking();
|
||||
register_default_type_sizes(event_type_registry);
|
||||
}
|
||||
}
|
||||
|
||||
impl System for KusamaRuntime {
|
||||
type Index = u32;
|
||||
type BlockNumber = u32;
|
||||
type Hash = sp_core::H256;
|
||||
type Hashing = BlakeTwo256;
|
||||
type AccountId = <<MultiSignature as Verify>::Signer as IdentifyAccount>::AccountId;
|
||||
type Address = Self::AccountId;
|
||||
type Header = Header<Self::BlockNumber, BlakeTwo256>;
|
||||
type Extrinsic = OpaqueExtrinsic;
|
||||
type AccountData = AccountData<<Self as Balances>::Balance>;
|
||||
}
|
||||
|
||||
impl Session for KusamaRuntime {
|
||||
type ValidatorId = <Self as System>::AccountId;
|
||||
type Keys = SessionKeys;
|
||||
}
|
||||
|
||||
impl Staking for KusamaRuntime {}
|
||||
|
||||
impl Balances for KusamaRuntime {
|
||||
type Balance = u128;
|
||||
}
|
||||
|
||||
/// Identity of a Grandpa authority.
|
||||
pub type AuthorityId = crate::runtimes::app::grandpa::Public;
|
||||
/// The weight of an authority.
|
||||
pub type AuthorityWeight = u64;
|
||||
/// A list of Grandpa authorities with associated weights.
|
||||
pub type AuthorityList = Vec<(AuthorityId, AuthorityWeight)>;
|
||||
|
||||
/// Register default common runtime type sizes
|
||||
pub fn register_default_type_sizes<T: Runtime>(
|
||||
event_type_registry: &mut EventTypeRegistry<T>,
|
||||
) {
|
||||
// for types which have all variants with no data, the size is just the index byte.
|
||||
type CLikeEnum = u8;
|
||||
|
||||
// primitives
|
||||
event_type_registry.register_type_size::<bool>("bool");
|
||||
event_type_registry.register_type_size::<u8>("u8");
|
||||
event_type_registry.register_type_size::<u16>("u16");
|
||||
event_type_registry.register_type_size::<u32>("u32");
|
||||
event_type_registry.register_type_size::<u64>("u64");
|
||||
event_type_registry.register_type_size::<u128>("u128");
|
||||
|
||||
event_type_registry.register_type_size::<()>("PhantomData");
|
||||
event_type_registry
|
||||
.register_type_size::<()>("sp_std::marker::PhantomData<(AccountId, Event)>");
|
||||
|
||||
// frame_support types
|
||||
event_type_registry
|
||||
.register_type_size::<frame_support::dispatch::DispatchInfo>("DispatchInfo");
|
||||
event_type_registry
|
||||
.register_type_size::<frame_support::dispatch::DispatchResult>("DispatchResult");
|
||||
event_type_registry
|
||||
.register_type_size::<frame_support::dispatch::DispatchError>("DispatchError");
|
||||
event_type_registry
|
||||
.register_type_size::<frame_support::traits::BalanceStatus>("Status");
|
||||
|
||||
// aliases etc.
|
||||
event_type_registry.register_type_size::<u32>("ReferendumIndex");
|
||||
event_type_registry.register_type_size::<[u8; 16]>("Kind");
|
||||
|
||||
event_type_registry.register_type_size::<u32>("AccountIndex");
|
||||
event_type_registry.register_type_size::<u32>("AssetId");
|
||||
event_type_registry.register_type_size::<u32>("BountyIndex");
|
||||
event_type_registry.register_type_size::<(u8, u8)>("CallIndex");
|
||||
event_type_registry.register_type_size::<[u8; 32]>("CallHash");
|
||||
event_type_registry.register_type_size::<u32>("PropIndex");
|
||||
event_type_registry.register_type_size::<u32>("ProposalIndex");
|
||||
event_type_registry.register_type_size::<CLikeEnum>("ProxyType");
|
||||
event_type_registry.register_type_size::<u32>("AuthorityIndex");
|
||||
event_type_registry.register_type_size::<u32>("MemberCount");
|
||||
event_type_registry.register_type_size::<u32>("RegistrarIndex");
|
||||
|
||||
event_type_registry.register_type_size::<u8>("VoteThreshold");
|
||||
event_type_registry
|
||||
.register_type_size::<(T::BlockNumber, u32)>("TaskAddress<BlockNumber>");
|
||||
event_type_registry
|
||||
.register_type_size::<(T::BlockNumber, u32)>("Timepoint<BlockNumber>");
|
||||
|
||||
event_type_registry.register_type_size::<AuthorityId>("AuthorityId");
|
||||
event_type_registry.register_type_size::<AuthorityWeight>("AuthorityWeight");
|
||||
event_type_registry
|
||||
.register_type_size::<Vec<(AuthorityId, AuthorityWeight)>>("AuthorityList");
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn can_register_default_runtime_type_sizes() {
|
||||
EventTypeRegistry::<DefaultNodeRuntime>::new();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_register_node_template_runtime_type_sizes() {
|
||||
EventTypeRegistry::<NodeTemplateRuntime>::new();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_register_contracts_template_runtime_type_sizes() {
|
||||
EventTypeRegistry::<ContractsTemplateRuntime>::new();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_register_kusama_runtime_type_sizes() {
|
||||
EventTypeRegistry::<KusamaRuntime>::new();
|
||||
}
|
||||
}
|
||||
+297
@@ -0,0 +1,297 @@
|
||||
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
|
||||
// This file is part of subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// subxt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! For querying runtime storage.
|
||||
|
||||
use codec::{
|
||||
Decode,
|
||||
Encode,
|
||||
};
|
||||
use sp_core::storage::{
|
||||
StorageChangeSet,
|
||||
StorageData,
|
||||
StorageKey,
|
||||
};
|
||||
pub use sp_runtime::traits::SignedExtension;
|
||||
pub use sp_version::RuntimeVersion;
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use crate::{
|
||||
metadata::{
|
||||
Metadata,
|
||||
MetadataError,
|
||||
},
|
||||
rpc::Rpc,
|
||||
Config,
|
||||
Error,
|
||||
StorageHasher,
|
||||
};
|
||||
|
||||
/// Storage entry trait.
|
||||
pub trait StorageEntry {
|
||||
/// Pallet name.
|
||||
const PALLET: &'static str;
|
||||
/// Storage name.
|
||||
const STORAGE: &'static str;
|
||||
/// Type of the storage entry value.
|
||||
type Value: Decode;
|
||||
/// Get the key data for the storage.
|
||||
fn key(&self) -> StorageEntryKey;
|
||||
}
|
||||
|
||||
/// The prefix of the key to a [`StorageEntry`]
|
||||
pub struct StorageKeyPrefix(Vec<u8>);
|
||||
|
||||
impl StorageKeyPrefix {
|
||||
/// Create the storage key prefix for a [`StorageEntry`]
|
||||
pub fn new<T: StorageEntry>() -> Self {
|
||||
let mut bytes = sp_core::twox_128(T::PALLET.as_bytes()).to_vec();
|
||||
bytes.extend(&sp_core::twox_128(T::STORAGE.as_bytes())[..]);
|
||||
Self(bytes)
|
||||
}
|
||||
|
||||
/// Convert the prefix into a [`StorageKey`]
|
||||
pub fn to_storage_key(self) -> StorageKey {
|
||||
StorageKey(self.0)
|
||||
}
|
||||
}
|
||||
|
||||
/// Storage key.
|
||||
pub enum StorageEntryKey {
|
||||
/// Plain key.
|
||||
Plain,
|
||||
/// Map key(s).
|
||||
Map(Vec<StorageMapKey>),
|
||||
}
|
||||
|
||||
impl StorageEntryKey {
|
||||
/// Construct the final [`sp_core::storage::StorageKey`] for the storage entry.
|
||||
pub fn final_key(&self, prefix: StorageKeyPrefix) -> sp_core::storage::StorageKey {
|
||||
let mut bytes = prefix.0;
|
||||
if let Self::Map(map_keys) = self {
|
||||
for map_key in map_keys {
|
||||
bytes.extend(Self::hash(&map_key.hasher, &map_key.value))
|
||||
}
|
||||
}
|
||||
sp_core::storage::StorageKey(bytes)
|
||||
}
|
||||
|
||||
fn hash(hasher: &StorageHasher, bytes: &[u8]) -> Vec<u8> {
|
||||
match hasher {
|
||||
StorageHasher::Identity => bytes.to_vec(),
|
||||
StorageHasher::Blake2_128 => sp_core::blake2_128(bytes).to_vec(),
|
||||
StorageHasher::Blake2_128Concat => {
|
||||
// copied from substrate Blake2_128Concat::hash since StorageHasher is not public
|
||||
sp_core::blake2_128(bytes)
|
||||
.iter()
|
||||
.chain(bytes)
|
||||
.cloned()
|
||||
.collect()
|
||||
}
|
||||
StorageHasher::Blake2_256 => sp_core::blake2_256(bytes).to_vec(),
|
||||
StorageHasher::Twox128 => sp_core::twox_128(bytes).to_vec(),
|
||||
StorageHasher::Twox256 => sp_core::twox_256(bytes).to_vec(),
|
||||
StorageHasher::Twox64Concat => {
|
||||
sp_core::twox_64(bytes)
|
||||
.iter()
|
||||
.chain(bytes)
|
||||
.cloned()
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Storage key for a Map.
|
||||
pub struct StorageMapKey {
|
||||
value: Vec<u8>,
|
||||
hasher: StorageHasher,
|
||||
}
|
||||
|
||||
impl StorageMapKey {
|
||||
/// Create a new [`StorageMapKey`] with the encoded data and the hasher.
|
||||
pub fn new<T: Encode>(value: &T, hasher: StorageHasher) -> Self {
|
||||
Self {
|
||||
value: value.encode(),
|
||||
hasher,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Client for querying runtime storage.
|
||||
#[derive(Clone)]
|
||||
pub struct StorageClient<'a, T: Config> {
|
||||
rpc: &'a Rpc<T>,
|
||||
metadata: &'a Metadata,
|
||||
iter_page_size: u32,
|
||||
}
|
||||
|
||||
impl<'a, T: Config> StorageClient<'a, T> {
|
||||
/// Create a new [`StorageClient`]
|
||||
pub fn new(rpc: &'a Rpc<T>, metadata: &'a Metadata, iter_page_size: u32) -> Self {
|
||||
Self {
|
||||
rpc,
|
||||
metadata,
|
||||
iter_page_size,
|
||||
}
|
||||
}
|
||||
|
||||
/// Fetch the value under an unhashed storage key
|
||||
pub async fn fetch_unhashed<V: Decode>(
|
||||
&self,
|
||||
key: StorageKey,
|
||||
hash: Option<T::Hash>,
|
||||
) -> Result<Option<V>, Error> {
|
||||
if let Some(data) = self.rpc.storage(&key, hash).await? {
|
||||
Ok(Some(Decode::decode(&mut &data.0[..])?))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
/// Fetch the raw encoded value under the raw storage key.
|
||||
pub async fn fetch_raw(
|
||||
&self,
|
||||
key: StorageKey,
|
||||
hash: Option<T::Hash>,
|
||||
) -> Result<Option<StorageData>, Error> {
|
||||
self.rpc.storage(&key, hash).await
|
||||
}
|
||||
|
||||
/// Fetch a StorageKey with an optional block hash.
|
||||
pub async fn fetch<F: StorageEntry>(
|
||||
&self,
|
||||
store: &F,
|
||||
hash: Option<T::Hash>,
|
||||
) -> Result<Option<F::Value>, Error> {
|
||||
let prefix = StorageKeyPrefix::new::<F>();
|
||||
let key = store.key().final_key(prefix);
|
||||
self.fetch_unhashed::<F::Value>(key, hash).await
|
||||
}
|
||||
|
||||
/// Fetch a StorageKey that has a default value with an optional block hash.
|
||||
pub async fn fetch_or_default<F: StorageEntry>(
|
||||
&self,
|
||||
store: &F,
|
||||
hash: Option<T::Hash>,
|
||||
) -> Result<F::Value, Error> {
|
||||
if let Some(data) = self.fetch(store, hash).await? {
|
||||
Ok(data)
|
||||
} else {
|
||||
let pallet_metadata = self.metadata.pallet(F::PALLET)?;
|
||||
let storage_metadata = pallet_metadata.storage(F::STORAGE)?;
|
||||
let default = Decode::decode(&mut &storage_metadata.default[..])
|
||||
.map_err(MetadataError::DefaultError)?;
|
||||
Ok(default)
|
||||
}
|
||||
}
|
||||
|
||||
/// Query historical storage entries
|
||||
pub async fn query_storage(
|
||||
&self,
|
||||
keys: Vec<StorageKey>,
|
||||
from: T::Hash,
|
||||
to: Option<T::Hash>,
|
||||
) -> Result<Vec<StorageChangeSet<T::Hash>>, Error> {
|
||||
self.rpc.query_storage(keys, from, to).await
|
||||
}
|
||||
|
||||
/// Fetch up to `count` keys for a storage map in lexicographic order.
|
||||
///
|
||||
/// Supports pagination by passing a value to `start_key`.
|
||||
pub async fn fetch_keys<F: StorageEntry>(
|
||||
&self,
|
||||
count: u32,
|
||||
start_key: Option<StorageKey>,
|
||||
hash: Option<T::Hash>,
|
||||
) -> Result<Vec<StorageKey>, Error> {
|
||||
let prefix = StorageKeyPrefix::new::<F>();
|
||||
let keys = self
|
||||
.rpc
|
||||
.storage_keys_paged(Some(prefix), count, start_key, hash)
|
||||
.await?;
|
||||
Ok(keys)
|
||||
}
|
||||
|
||||
/// Returns an iterator of key value pairs.
|
||||
pub async fn iter<F: StorageEntry>(
|
||||
&self,
|
||||
hash: Option<T::Hash>,
|
||||
) -> Result<KeyIter<'a, T, F>, Error> {
|
||||
let hash = if let Some(hash) = hash {
|
||||
hash
|
||||
} else {
|
||||
self.rpc
|
||||
.block_hash(None)
|
||||
.await?
|
||||
.expect("didn't pass a block number; qed")
|
||||
};
|
||||
Ok(KeyIter {
|
||||
client: self.clone(),
|
||||
hash,
|
||||
count: self.iter_page_size,
|
||||
start_key: None,
|
||||
buffer: Default::default(),
|
||||
_marker: PhantomData,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Iterates over key value pairs in a map.
|
||||
pub struct KeyIter<'a, T: Config, F: StorageEntry> {
|
||||
client: StorageClient<'a, T>,
|
||||
_marker: PhantomData<F>,
|
||||
count: u32,
|
||||
hash: T::Hash,
|
||||
start_key: Option<StorageKey>,
|
||||
buffer: Vec<(StorageKey, StorageData)>,
|
||||
}
|
||||
|
||||
impl<'a, T: Config, F: StorageEntry> KeyIter<'a, T, F> {
|
||||
/// Returns the next key value pair from a map.
|
||||
pub async fn next(&mut self) -> Result<Option<(StorageKey, F::Value)>, Error> {
|
||||
loop {
|
||||
if let Some((k, v)) = self.buffer.pop() {
|
||||
return Ok(Some((k, Decode::decode(&mut &v.0[..])?)))
|
||||
} else {
|
||||
let keys = self
|
||||
.client
|
||||
.fetch_keys::<F>(self.count, self.start_key.take(), Some(self.hash))
|
||||
.await?;
|
||||
|
||||
if keys.is_empty() {
|
||||
return Ok(None)
|
||||
}
|
||||
|
||||
self.start_key = keys.last().cloned();
|
||||
|
||||
let change_sets = self
|
||||
.client
|
||||
.rpc
|
||||
.query_storage_at(&keys, Some(self.hash))
|
||||
.await?;
|
||||
for change_set in change_sets {
|
||||
for (k, v) in change_set.changes {
|
||||
if let Some(v) = v {
|
||||
self.buffer.push((k, v));
|
||||
}
|
||||
}
|
||||
}
|
||||
debug_assert_eq!(self.buffer.len(), keys.len());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+14
-16
@@ -1,5 +1,5 @@
|
||||
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
|
||||
// This file is part of substrate-subxt.
|
||||
// This file is part of subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
@@ -12,7 +12,7 @@
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with substrate-subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use jsonrpsee_types::{
|
||||
DeserializeOwned,
|
||||
@@ -35,17 +35,15 @@ use crate::{
|
||||
Raw,
|
||||
RawEvent,
|
||||
},
|
||||
frame::{
|
||||
system::Phase,
|
||||
Event,
|
||||
},
|
||||
rpc::Rpc,
|
||||
runtimes::Runtime,
|
||||
Config,
|
||||
Event,
|
||||
Phase,
|
||||
};
|
||||
|
||||
/// Event subscription simplifies filtering a storage change set stream for
|
||||
/// events of interest.
|
||||
pub struct EventSubscription<'a, T: Runtime> {
|
||||
pub struct EventSubscription<'a, T: Config> {
|
||||
subscription: EventStorageSubscription<T>,
|
||||
decoder: &'a EventsDecoder<T>,
|
||||
block: Option<T::Hash>,
|
||||
@@ -55,7 +53,7 @@ pub struct EventSubscription<'a, T: Runtime> {
|
||||
finished: bool,
|
||||
}
|
||||
|
||||
impl<'a, T: Runtime> EventSubscription<'a, T> {
|
||||
impl<'a, T: Config> EventSubscription<'a, T> {
|
||||
/// Creates a new event subscription.
|
||||
pub fn new(
|
||||
subscription: EventStorageSubscription<T>,
|
||||
@@ -84,8 +82,8 @@ impl<'a, T: Runtime> EventSubscription<'a, T> {
|
||||
}
|
||||
|
||||
/// Filters events by type.
|
||||
pub fn filter_event<E: Event<T>>(&mut self) {
|
||||
self.event = Some((E::MODULE, E::EVENT));
|
||||
pub fn filter_event<E: Event>(&mut self) {
|
||||
self.event = Some((E::PALLET, E::EVENT));
|
||||
}
|
||||
|
||||
/// Gets the next event.
|
||||
@@ -124,7 +122,7 @@ impl<'a, T: Runtime> EventSubscription<'a, T> {
|
||||
Raw::Error(err) => return Some(Err(err.into())),
|
||||
};
|
||||
if let Some((module, variant)) = self.event {
|
||||
if event.module != module || event.variant != variant {
|
||||
if event.pallet != module || event.variant != variant {
|
||||
continue
|
||||
}
|
||||
}
|
||||
@@ -155,14 +153,14 @@ impl From<SystemEvents> for StorageKey {
|
||||
}
|
||||
|
||||
/// Event subscription to only fetch finalized storage changes.
|
||||
pub struct FinalizedEventStorageSubscription<T: Runtime> {
|
||||
pub struct FinalizedEventStorageSubscription<T: Config> {
|
||||
rpc: Rpc<T>,
|
||||
subscription: Subscription<T::Header>,
|
||||
storage_changes: VecDeque<StorageChangeSet<T::Hash>>,
|
||||
storage_key: StorageKey,
|
||||
}
|
||||
|
||||
impl<T: Runtime> FinalizedEventStorageSubscription<T> {
|
||||
impl<T: Config> FinalizedEventStorageSubscription<T> {
|
||||
/// Creates a new finalized event storage subscription.
|
||||
pub fn new(rpc: Rpc<T>, subscription: Subscription<T::Header>) -> Self {
|
||||
Self {
|
||||
@@ -193,14 +191,14 @@ impl<T: Runtime> FinalizedEventStorageSubscription<T> {
|
||||
}
|
||||
|
||||
/// Wrapper over imported and finalized event subscriptions.
|
||||
pub enum EventStorageSubscription<T: Runtime> {
|
||||
pub enum EventStorageSubscription<T: Config> {
|
||||
/// Events that are InBlock
|
||||
Imported(Subscription<StorageChangeSet<T::Hash>>),
|
||||
/// Events that are Finalized
|
||||
Finalized(FinalizedEventStorageSubscription<T>),
|
||||
}
|
||||
|
||||
impl<T: Runtime> EventStorageSubscription<T> {
|
||||
impl<T: Config> EventStorageSubscription<T> {
|
||||
/// Gets the next change_set from the subscription.
|
||||
pub async fn next(&mut self) -> Option<StorageChangeSet<T::Hash>> {
|
||||
match self {
|
||||
|
||||
@@ -1,176 +0,0 @@
|
||||
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
|
||||
// This file is part of substrate-subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// subxt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with substrate-subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
mod node_proc;
|
||||
|
||||
use super::*;
|
||||
pub use node_proc::TestNodeProcess;
|
||||
use sp_core::storage::{
|
||||
well_known_keys,
|
||||
StorageKey,
|
||||
};
|
||||
use sp_keyring::AccountKeyring;
|
||||
|
||||
/// substrate node should be installed on the $PATH
|
||||
const SUBSTRATE_NODE_PATH: &str = "substrate";
|
||||
|
||||
pub(crate) type TestRuntime = crate::DefaultNodeRuntime;
|
||||
|
||||
pub(crate) async fn test_node_process_with(
|
||||
key: AccountKeyring,
|
||||
) -> TestNodeProcess<TestRuntime> {
|
||||
if which::which(SUBSTRATE_NODE_PATH).is_err() {
|
||||
panic!("A substrate binary should be installed on your path for integration tests. See https://github.com/paritytech/substrate-subxt/tree/master#integration-testing")
|
||||
}
|
||||
|
||||
let proc = TestNodeProcess::<TestRuntime>::build(SUBSTRATE_NODE_PATH)
|
||||
.with_authority(key)
|
||||
.scan_for_open_ports()
|
||||
.spawn::<TestRuntime>()
|
||||
.await;
|
||||
proc.unwrap()
|
||||
}
|
||||
|
||||
pub(crate) async fn test_node_process() -> TestNodeProcess<TestRuntime> {
|
||||
test_node_process_with(AccountKeyring::Alice).await
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_insert_key() {
|
||||
let test_node_process = test_node_process_with(AccountKeyring::Bob).await;
|
||||
let client = test_node_process.client();
|
||||
let public = AccountKeyring::Alice.public().as_array_ref().to_vec();
|
||||
client
|
||||
.insert_key(
|
||||
"aura".to_string(),
|
||||
"//Alice".to_string(),
|
||||
public.clone().into(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(client
|
||||
.has_key(public.clone().into(), "aura".to_string())
|
||||
.await
|
||||
.unwrap());
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_tx_transfer_balance() {
|
||||
let mut signer = PairSigner::new(AccountKeyring::Alice.pair());
|
||||
let dest = AccountKeyring::Bob.to_account_id().into();
|
||||
|
||||
let node_process = test_node_process().await;
|
||||
let client = node_process.client();
|
||||
let nonce = client
|
||||
.account(&AccountKeyring::Alice.to_account_id(), None)
|
||||
.await
|
||||
.unwrap()
|
||||
.nonce;
|
||||
signer.set_nonce(nonce);
|
||||
client
|
||||
.submit(
|
||||
balances::TransferCall {
|
||||
to: &dest,
|
||||
amount: 10_000,
|
||||
},
|
||||
&signer,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
// check that nonce is handled correctly
|
||||
signer.increment_nonce();
|
||||
client
|
||||
.submit(
|
||||
balances::TransferCall {
|
||||
to: &dest,
|
||||
amount: 10_000,
|
||||
},
|
||||
&signer,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_getting_hash() {
|
||||
let node_process = test_node_process().await;
|
||||
node_process.client().block_hash(None).await.unwrap();
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_getting_block() {
|
||||
let node_process = test_node_process().await;
|
||||
let client = node_process.client();
|
||||
let block_hash = client.block_hash(None).await.unwrap();
|
||||
client.block(block_hash).await.unwrap();
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_getting_read_proof() {
|
||||
let node_process = test_node_process().await;
|
||||
let client = node_process.client();
|
||||
let block_hash = client.block_hash(None).await.unwrap();
|
||||
client
|
||||
.read_proof(
|
||||
vec![
|
||||
StorageKey(well_known_keys::HEAP_PAGES.to_vec()),
|
||||
StorageKey(well_known_keys::EXTRINSIC_INDEX.to_vec()),
|
||||
],
|
||||
block_hash,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_chain_subscribe_blocks() {
|
||||
let node_process = test_node_process().await;
|
||||
let client = node_process.client();
|
||||
let mut blocks = client.subscribe_blocks().await.unwrap();
|
||||
blocks.next().await.unwrap();
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_chain_subscribe_finalized_blocks() {
|
||||
let node_process = test_node_process().await;
|
||||
let client = node_process.client();
|
||||
let mut blocks = client.subscribe_finalized_blocks().await.unwrap();
|
||||
blocks.next().await.unwrap();
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_fetch_keys() {
|
||||
let node_process = test_node_process().await;
|
||||
let client = node_process.client();
|
||||
let keys = client
|
||||
.fetch_keys::<system::AccountStore<_>>(4, None, None)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(keys.len(), 4)
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_iter() {
|
||||
let node_process = test_node_process().await;
|
||||
let client = node_process.client();
|
||||
let mut iter = client.iter::<system::AccountStore<_>>(None).await.unwrap();
|
||||
let mut i = 0;
|
||||
while let Some(_) = iter.next().await.unwrap() {
|
||||
i += 1;
|
||||
}
|
||||
assert_eq!(i, 13);
|
||||
}
|
||||
@@ -1,237 +0,0 @@
|
||||
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
|
||||
// This file is part of substrate-subxt.
|
||||
//
|
||||
// subxt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// subxt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with substrate-subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{
|
||||
Client,
|
||||
ClientBuilder,
|
||||
Runtime,
|
||||
};
|
||||
use sp_keyring::AccountKeyring;
|
||||
use std::{
|
||||
ffi::{
|
||||
OsStr,
|
||||
OsString,
|
||||
},
|
||||
net::TcpListener,
|
||||
process,
|
||||
sync::atomic::{
|
||||
AtomicU16,
|
||||
Ordering,
|
||||
},
|
||||
thread,
|
||||
time,
|
||||
};
|
||||
|
||||
/// Spawn a local substrate node for testing subxt.
|
||||
pub struct TestNodeProcess<R: Runtime> {
|
||||
proc: process::Child,
|
||||
client: Client<R>,
|
||||
}
|
||||
|
||||
impl<R> Drop for TestNodeProcess<R>
|
||||
where
|
||||
R: Runtime,
|
||||
{
|
||||
fn drop(&mut self) {
|
||||
let _ = self.kill();
|
||||
}
|
||||
}
|
||||
|
||||
impl<R> TestNodeProcess<R>
|
||||
where
|
||||
R: Runtime,
|
||||
{
|
||||
/// Construct a builder for spawning a test node process.
|
||||
pub fn build<S>(program: S) -> TestNodeProcessBuilder
|
||||
where
|
||||
S: AsRef<OsStr> + Clone,
|
||||
{
|
||||
TestNodeProcessBuilder::new(program)
|
||||
}
|
||||
|
||||
/// Attempt to kill the running substrate process.
|
||||
pub fn kill(&mut self) -> Result<(), String> {
|
||||
log::info!("Killing contracts node process {}", self.proc.id());
|
||||
if let Err(err) = self.proc.kill() {
|
||||
let err = format!(
|
||||
"Error killing contracts node process {}: {}",
|
||||
self.proc.id(),
|
||||
err
|
||||
);
|
||||
log::error!("{}", err);
|
||||
return Err(err.into())
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns the subxt client connected to the running node.
|
||||
pub fn client(&self) -> &Client<R> {
|
||||
&self.client
|
||||
}
|
||||
}
|
||||
|
||||
/// Construct a test node process.
|
||||
pub struct TestNodeProcessBuilder {
|
||||
node_path: OsString,
|
||||
authority: Option<AccountKeyring>,
|
||||
scan_port_range: bool,
|
||||
}
|
||||
|
||||
impl TestNodeProcessBuilder {
|
||||
pub fn new<P>(node_path: P) -> TestNodeProcessBuilder
|
||||
where
|
||||
P: AsRef<OsStr>,
|
||||
{
|
||||
Self {
|
||||
node_path: node_path.as_ref().into(),
|
||||
authority: None,
|
||||
scan_port_range: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the authority dev account for a node in validator mode e.g. --alice.
|
||||
pub fn with_authority(&mut self, account: AccountKeyring) -> &mut Self {
|
||||
self.authority = Some(account);
|
||||
self
|
||||
}
|
||||
|
||||
/// Enable port scanning to scan for open ports.
|
||||
///
|
||||
/// Allows spawning multiple node instances for tests to run in parallel.
|
||||
pub fn scan_for_open_ports(&mut self) -> &mut Self {
|
||||
self.scan_port_range = true;
|
||||
self
|
||||
}
|
||||
|
||||
/// Spawn the substrate node at the given path, and wait for rpc to be initialized.
|
||||
pub async fn spawn<R>(&self) -> Result<TestNodeProcess<R>, String>
|
||||
where
|
||||
R: Runtime,
|
||||
{
|
||||
let mut cmd = process::Command::new(&self.node_path);
|
||||
cmd.env("RUST_LOG", "error").arg("--dev").arg("--tmp");
|
||||
|
||||
if let Some(authority) = self.authority {
|
||||
let authority = format!("{:?}", authority);
|
||||
let arg = format!("--{}", authority.as_str().to_lowercase());
|
||||
cmd.arg(arg);
|
||||
}
|
||||
|
||||
let ws_port = if self.scan_port_range {
|
||||
let (p2p_port, http_port, ws_port) = next_open_port()
|
||||
.ok_or("No available ports in the given port range".to_owned())?;
|
||||
|
||||
cmd.arg(format!("--port={}", p2p_port));
|
||||
cmd.arg(format!("--rpc-port={}", http_port));
|
||||
cmd.arg(format!("--ws-port={}", ws_port));
|
||||
ws_port
|
||||
} else {
|
||||
// the default Websockets port
|
||||
9944
|
||||
};
|
||||
|
||||
let ws_url = format!("ws://127.0.0.1:{}", ws_port);
|
||||
|
||||
let mut proc = cmd.spawn().map_err(|e| {
|
||||
format!(
|
||||
"Error spawning substrate node '{}': {}",
|
||||
self.node_path.to_string_lossy(),
|
||||
e
|
||||
)
|
||||
})?;
|
||||
// wait for rpc to be initialized
|
||||
const MAX_ATTEMPTS: u32 = 10;
|
||||
let mut attempts = 1;
|
||||
let client = loop {
|
||||
thread::sleep(time::Duration::from_secs(1));
|
||||
log::info!(
|
||||
"Connecting to contracts enabled node, attempt {}/{}",
|
||||
attempts,
|
||||
MAX_ATTEMPTS
|
||||
);
|
||||
let result = ClientBuilder::<R>::new()
|
||||
.set_url(ws_url.clone())
|
||||
.build()
|
||||
.await;
|
||||
match result {
|
||||
Ok(client) => break Ok(client),
|
||||
Err(crate::Error::MissingTypeSizes(e)) => {
|
||||
break Err(crate::Error::MissingTypeSizes(e))
|
||||
}
|
||||
Err(err) => {
|
||||
if attempts < MAX_ATTEMPTS {
|
||||
attempts += 1;
|
||||
continue
|
||||
}
|
||||
break Err(err)
|
||||
}
|
||||
}
|
||||
};
|
||||
match client {
|
||||
Ok(client) => Ok(TestNodeProcess { proc, client }),
|
||||
Err(err) => {
|
||||
let err = format!(
|
||||
"Failed to connect to node rpc at {} after {} attempts: {}",
|
||||
ws_url, attempts, err
|
||||
);
|
||||
log::error!("{}", err);
|
||||
proc.kill().map_err(|e| {
|
||||
format!("Error killing substrate process '{}': {}", proc.id(), e)
|
||||
})?;
|
||||
Err(err.into())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The start of the port range to scan.
|
||||
const START_PORT: u16 = 9900;
|
||||
/// The end of the port range to scan.
|
||||
const END_PORT: u16 = 10000;
|
||||
/// The maximum number of ports to scan before giving up.
|
||||
const MAX_PORTS: u16 = 1000;
|
||||
/// Next available unclaimed port for test node endpoints.
|
||||
static PORT: AtomicU16 = AtomicU16::new(START_PORT);
|
||||
|
||||
/// Returns the next set of 3 open ports.
|
||||
///
|
||||
/// Returns None if there are not 3 open ports available.
|
||||
fn next_open_port() -> Option<(u16, u16, u16)> {
|
||||
let mut ports = Vec::new();
|
||||
let mut ports_scanned = 0u16;
|
||||
loop {
|
||||
let _ = PORT.compare_exchange(
|
||||
END_PORT,
|
||||
START_PORT,
|
||||
Ordering::SeqCst,
|
||||
Ordering::SeqCst,
|
||||
);
|
||||
let next = PORT.fetch_add(1, Ordering::SeqCst);
|
||||
match TcpListener::bind(("0.0.0.0", next)) {
|
||||
Ok(_) => {
|
||||
ports.push(next);
|
||||
if ports.len() == 3 {
|
||||
return Some((ports[0], ports[1], ports[2]))
|
||||
}
|
||||
}
|
||||
Err(_) => (),
|
||||
}
|
||||
ports_scanned += 1;
|
||||
if ports_scanned == MAX_PORTS {
|
||||
return None
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user