cargo fmt with stable defaults (#876)

This commit is contained in:
James Wilson
2023-03-21 16:53:47 +00:00
committed by GitHub
parent c63ff6ec6d
commit 7c252fccf7
110 changed files with 663 additions and 1949 deletions
+12 -27
View File
@@ -3,19 +3,9 @@
// see LICENSE for license details.
use crate::{
client::{
OfflineClientT,
OnlineClientT,
},
config::{
Config,
Hasher,
Header,
},
error::{
BlockError,
Error,
},
client::{OfflineClientT, OnlineClientT},
config::{Config, Hasher, Header},
error::{BlockError, Error},
events,
rpc::types::ChainBlockResponse,
runtime_api::RuntimeApi,
@@ -135,15 +125,13 @@ where
.extrinsics
.iter()
.enumerate()
.map(|(idx, e)| {
Extrinsic {
index: idx as u32,
bytes: &e.0,
client: self.client.clone(),
block_hash: self.details.block.header.hash(),
cached_events: self.cached_events.clone(),
_marker: std::marker::PhantomData,
}
.map(|(idx, e)| Extrinsic {
index: idx as u32,
bytes: &e.0,
client: self.client.clone(),
block_hash: self.details.block.header.hash(),
cached_events: self.cached_events.clone(),
_marker: std::marker::PhantomData,
})
}
}
@@ -181,8 +169,7 @@ where
{
/// The events associated with the extrinsic.
pub async fn events(&self) -> Result<ExtrinsicEvents<T>, Error> {
let events =
get_events(&self.client, self.block_hash, &self.cached_events).await?;
let events = get_events(&self.client, self.block_hash, &self.cached_events).await?;
let ext_hash = T::Hasher::hash_of(&self.bytes);
Ok(ExtrinsicEvents::new(ext_hash, self.index, events))
}
@@ -248,9 +235,7 @@ impl<T: Config> ExtrinsicEvents<T> {
///
/// This works in the same way that [`events::Events::find()`] does, with the
/// exception that it filters out events not related to the submitted extrinsic.
pub fn find<Ev: events::StaticEvent>(
&self,
) -> impl Iterator<Item = Result<Ev, Error>> + '_ {
pub fn find<Ev: events::StaticEvent>(&self) -> impl Iterator<Item = Result<Ev, Error>> + '_ {
self.iter().filter_map(|ev| {
ev.and_then(|ev| ev.as_event::<Ev>().map_err(Into::into))
.transpose()
+11 -31
View File
@@ -5,27 +5,13 @@
use super::Block;
use crate::{
client::OnlineClientT,
config::{
Config,
Header,
},
error::{
BlockError,
Error,
},
config::{Config, Header},
error::{BlockError, Error},
utils::PhantomDataSendSync,
};
use derivative::Derivative;
use futures::{
future::Either,
stream,
Stream,
StreamExt,
};
use std::{
future::Future,
pin::Pin,
};
use futures::{future::Either, stream, Stream, StreamExt};
use std::{future::Future, pin::Pin};
type BlockStream<T> = Pin<Box<dyn Stream<Item = Result<T, Error>> + Send>>;
type BlockStreamRes<T> = Result<BlockStream<T>, Error>;
@@ -71,13 +57,11 @@ where
// for the latest block and use that.
let block_hash = match block_hash {
Some(hash) => hash,
None => {
client
.rpc()
.block_hash(None)
.await?
.expect("didn't pass a block number; qed")
}
None => client
.rpc()
.block_hash(None)
.await?
.expect("didn't pass a block number; qed"),
};
let block_header = match client.rpc().header(Some(block_hash)).await? {
@@ -145,12 +129,8 @@ where
// Adjust the subscription stream to fill in any missing blocks.
BlockStreamRes::Ok(
subscribe_to_block_headers_filling_in_gaps(
client,
last_finalized_block_num,
sub,
)
.boxed(),
subscribe_to_block_headers_filling_in_gaps(client, last_finalized_block_num, sub)
.boxed(),
)
})
}
+2 -9
View File
@@ -7,12 +7,5 @@
mod block_types;
mod blocks_client;
pub use block_types::{
Block,
Extrinsic,
ExtrinsicEvents,
};
pub use blocks_client::{
subscribe_to_block_headers_filling_in_gaps,
BlocksClient,
};
pub use block_types::{Block, Extrinsic, ExtrinsicEvents};
pub use blocks_client::{subscribe_to_block_headers_filling_in_gaps, BlocksClient};
+2 -10
View File
@@ -11,17 +11,9 @@
mod offline_client;
mod online_client;
pub use offline_client::{
OfflineClient,
OfflineClientT,
};
pub use offline_client::{OfflineClient, OfflineClientT};
pub use online_client::{
ClientRuntimeUpdater,
OnlineClient,
OnlineClientT,
RuntimeUpdaterStream,
Update,
UpgradeError,
ClientRuntimeUpdater, OnlineClient, OnlineClientT, RuntimeUpdaterStream, Update, UpgradeError,
};
#[cfg(any(
+3 -9
View File
@@ -3,15 +3,9 @@
// see LICENSE for license details.
use crate::{
blocks::BlocksClient,
constants::ConstantsClient,
events::EventsClient,
rpc::types::RuntimeVersion,
runtime_api::RuntimeApiClient,
storage::StorageClient,
tx::TxClient,
Config,
Metadata,
blocks::BlocksClient, constants::ConstantsClient, events::EventsClient,
rpc::types::RuntimeVersion, runtime_api::RuntimeApiClient, storage::StorageClient,
tx::TxClient, Config, Metadata,
};
use derivative::Derivative;
use std::sync::Arc;
+10 -38
View File
@@ -2,33 +2,22 @@
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.
use super::{
OfflineClient,
OfflineClientT,
};
use super::{OfflineClient, OfflineClientT};
use crate::{
blocks::BlocksClient,
constants::ConstantsClient,
error::Error,
events::EventsClient,
rpc::{
types::{
RuntimeVersion,
Subscription,
},
Rpc,
RpcClientT,
types::{RuntimeVersion, Subscription},
Rpc, RpcClientT,
},
runtime_api::RuntimeApiClient,
storage::StorageClient,
tx::TxClient,
Config,
Metadata,
};
use codec::{
Compact,
Decode,
Config, Metadata,
};
use codec::{Compact, Decode};
use derivative::Derivative;
use frame_metadata::RuntimeMetadataPrefixed;
use futures::future;
@@ -114,12 +103,7 @@ impl<T: Config> OnlineClient<T> {
)
.await;
OnlineClient::from_rpc_client_with(
genesis_hash?,
runtime_version?,
metadata?,
rpc_client,
)
OnlineClient::from_rpc_client_with(genesis_hash?, runtime_version?, metadata?, rpc_client)
}
/// Construct a new [`OnlineClient`] by providing all of the underlying details needed
@@ -341,7 +325,7 @@ impl<T: Config> ClientRuntimeUpdater<T> {
/// Tries to apply a new update.
pub fn apply_update(&self, update: Update) -> Result<(), UpgradeError> {
if !self.is_runtime_version_different(&update.runtime_version) {
return Err(UpgradeError::SameVersion)
return Err(UpgradeError::SameVersion);
}
self.do_update(update);
@@ -442,18 +426,9 @@ impl Update {
#[cfg(feature = "jsonrpsee-ws")]
mod jsonrpsee_helpers {
pub use jsonrpsee::{
client_transport::ws::{
InvalidUri,
Receiver,
Sender,
Uri,
WsTransportClientBuilder,
},
client_transport::ws::{InvalidUri, Receiver, Sender, Uri, WsTransportClientBuilder},
core::{
client::{
Client,
ClientBuilder,
},
client::{Client, ClientBuilder},
Error,
},
};
@@ -483,10 +458,7 @@ mod jsonrpsee_helpers {
pub use jsonrpsee::{
client_transport::web,
core::{
client::{
Client,
ClientBuilder,
},
client::{Client, ClientBuilder},
Error,
},
};
+4 -16
View File
@@ -7,21 +7,11 @@
//! implementation of the trait is provided ([`BaseExtrinsicParams`]) which is
//! used by the provided Substrate and Polkadot configuration.
use crate::{
utils::Encoded,
Config,
};
use codec::{
Compact,
Decode,
Encode,
};
use crate::{utils::Encoded, Config};
use codec::{Compact, Decode, Encode};
use core::fmt::Debug;
use derivative::Derivative;
use serde::{
Deserialize,
Serialize,
};
use serde::{Deserialize, Serialize};
/// This trait allows you to configure the "signed extra" and
/// "additional" parameters that are signed and used in transactions.
@@ -148,9 +138,7 @@ impl<T: Config, Tip: Debug + Encode + 'static> ExtrinsicParams<T::Index, T::Hash
) -> Self {
BaseExtrinsicParams {
era: other_params.era,
mortality_checkpoint: other_params
.mortality_checkpoint
.unwrap_or(genesis_hash),
mortality_checkpoint: other_params.mortality_checkpoint.unwrap_or(genesis_hash),
tip: other_params.tip,
nonce,
spec_version,
+3 -12
View File
@@ -12,15 +12,9 @@ pub mod extrinsic_params;
pub mod polkadot;
pub mod substrate;
use codec::{
Decode,
Encode,
};
use codec::{Decode, Encode};
use core::fmt::Debug;
use serde::{
de::DeserializeOwned,
Serialize,
};
use serde::{de::DeserializeOwned, Serialize};
pub use extrinsic_params::ExtrinsicParams;
pub use polkadot::PolkadotConfig;
@@ -110,10 +104,7 @@ pub trait Header: Sized + Encode {
/// // This is how PolkadotConfig is implemented:
/// type PolkadotConfig = WithExtrinsicParams<SubstrateConfig, PolkadotExtrinsicParams<SubstrateConfig>>;
/// ```
pub struct WithExtrinsicParams<
T: Config,
E: extrinsic_params::ExtrinsicParams<T::Index, T::Hash>,
> {
pub struct WithExtrinsicParams<T: Config, E: extrinsic_params::ExtrinsicParams<T::Index, T::Hash>> {
_marker: std::marker::PhantomData<(T, E)>,
}
+1 -4
View File
@@ -6,10 +6,7 @@
use codec::Encode;
use super::extrinsic_params::{
BaseExtrinsicParams,
BaseExtrinsicParamsBuilder,
};
use super::extrinsic_params::{BaseExtrinsicParams, BaseExtrinsicParamsBuilder};
/// Default set of commonly used types by Polkadot nodes.
pub type PolkadotConfig = super::WithExtrinsicParams<
+8 -30
View File
@@ -5,32 +5,14 @@
//! Substrate specific configuration
use super::{
extrinsic_params::{
BaseExtrinsicParams,
BaseExtrinsicParamsBuilder,
},
Config,
Hasher,
Header,
};
use codec::{
Decode,
Encode,
};
use serde::{
Deserialize,
Serialize,
extrinsic_params::{BaseExtrinsicParams, BaseExtrinsicParamsBuilder},
Config, Hasher, Header,
};
use codec::{Decode, Encode};
use serde::{Deserialize, Serialize};
pub use crate::utils::{
AccountId32,
MultiAddress,
MultiSignature,
};
pub use primitive_types::{
H256,
U256,
};
pub use crate::utils::{AccountId32, MultiAddress, MultiSignature};
pub use primitive_types::{H256, U256};
/// Default set of commonly used types by Substrate runtimes.
// Note: We only use this at the type level, so it should be impossible to
@@ -137,9 +119,7 @@ where
}
/// Generic header digest. From `sp_runtime::generic::digest`.
#[derive(
Encode, Decode, Debug, PartialEq, Eq, Clone, Serialize, Deserialize, Default,
)]
#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, Serialize, Deserialize, Default)]
pub struct Digest {
/// A list of digest items.
pub logs: Vec<DigestItem>,
@@ -240,9 +220,7 @@ impl Decode for DigestItem {
Ok(Self::Seal(vals.0, vals.1))
}
DigestItemType::Other => Ok(Self::Other(Decode::decode(input)?)),
DigestItemType::RuntimeEnvironmentUpdated => {
Ok(Self::RuntimeEnvironmentUpdated)
}
DigestItemType::RuntimeEnvironmentUpdated => Ok(Self::RuntimeEnvironmentUpdated),
}
}
}
+2 -8
View File
@@ -2,10 +2,7 @@
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.
use crate::{
dynamic::DecodedValueThunk,
metadata::DecodeWithMetadata,
};
use crate::{dynamic::DecodedValueThunk, metadata::DecodeWithMetadata};
use std::borrow::Cow;
/// This represents a constant address. Anything implementing this trait
@@ -94,9 +91,6 @@ impl<ReturnTy: DecodeWithMetadata> ConstantAddress for Address<ReturnTy> {
}
/// Construct a new dynamic constant lookup.
pub fn dynamic(
pallet_name: impl Into<String>,
constant_name: impl Into<String>,
) -> DynamicAddress {
pub fn dynamic(pallet_name: impl Into<String>, constant_name: impl Into<String>) -> DynamicAddress {
DynamicAddress::new(pallet_name, constant_name)
}
+3 -9
View File
@@ -6,10 +6,7 @@ use super::ConstantAddress;
use crate::{
client::OfflineClientT,
error::Error,
metadata::{
DecodeWithMetadata,
MetadataError,
},
metadata::{DecodeWithMetadata, MetadataError},
Config,
};
use derivative::Derivative;
@@ -37,10 +34,7 @@ impl<T: Config, Client: OfflineClientT<T>> ConstantsClient<T, Client> {
/// if the address is valid (or if it's not possible to check since the address has no validation hash).
/// Return an error if the address was not valid or something went wrong trying to validate it (ie
/// the pallet or constant in question do not exist at all).
pub fn validate<Address: ConstantAddress>(
&self,
address: &Address,
) -> Result<(), Error> {
pub fn validate<Address: ConstantAddress>(&self, address: &Address) -> Result<(), Error> {
if let Some(actual_hash) = address.validation_hash() {
let expected_hash = self
.client
@@ -51,7 +45,7 @@ impl<T: Config, Client: OfflineClientT<T>> ConstantsClient<T, Client> {
address.pallet_name().into(),
address.constant_name().into(),
)
.into())
.into());
}
}
Ok(())
+1 -6
View File
@@ -7,10 +7,5 @@
mod constant_address;
mod constants_client;
pub use constant_address::{
dynamic,
Address,
ConstantAddress,
DynamicAddress,
};
pub use constant_address::{dynamic, Address, ConstantAddress, DynamicAddress};
pub use constants_client::ConstantsClient;
+2 -8
View File
@@ -7,10 +7,7 @@
use crate::{
error::Error,
metadata::{
DecodeWithMetadata,
Metadata,
},
metadata::{DecodeWithMetadata, Metadata},
};
use scale_decode::DecodeAsType;
@@ -29,10 +26,7 @@ pub use crate::tx::dynamic as tx;
pub use crate::constants::dynamic as constant;
// Lookup storage values dynamically.
pub use crate::storage::{
dynamic as storage,
dynamic_root as storage_root,
};
pub use crate::storage::{dynamic as storage, dynamic_root as storage_root};
/// This is the result of making a dynamic request to a node. From this,
/// we can return the raw SCALE bytes that we were handed back, or we can
+11 -20
View File
@@ -11,10 +11,7 @@ use scale_info::TypeDef;
use std::borrow::Cow;
// Re-expose the errors we use from other crates here:
pub use crate::metadata::{
InvalidMetadataError,
MetadataError,
};
pub use crate::metadata::{InvalidMetadataError, MetadataError};
pub use scale_decode::Error as DecodeError;
pub use scale_encode::Error as EncodeError;
@@ -123,7 +120,7 @@ impl DispatchError {
tracing::warn!(
"Can't decode error: sp_runtime::DispatchError was not found in Metadata"
);
return DispatchError::Other(bytes.into_owned())
return DispatchError::Other(bytes.into_owned());
}
};
@@ -131,7 +128,7 @@ impl DispatchError {
Some(ty) => ty,
None => {
tracing::warn!("Can't decode error: sp_runtime::DispatchError type ID doesn't resolve to a known type");
return DispatchError::Other(bytes.into_owned())
return DispatchError::Other(bytes.into_owned());
}
};
@@ -141,7 +138,7 @@ impl DispatchError {
tracing::warn!(
"Can't decode error: sp_runtime::DispatchError type is not a Variant"
);
return DispatchError::Other(bytes.into_owned())
return DispatchError::Other(bytes.into_owned());
}
};
@@ -154,14 +151,14 @@ impl DispatchError {
Some(idx) => idx,
None => {
tracing::warn!("Can't decode error: sp_runtime::DispatchError does not have a 'Module' variant");
return DispatchError::Other(bytes.into_owned())
return DispatchError::Other(bytes.into_owned());
}
};
// If the error bytes don't correspond to a ModuleError, just return the bytes.
// This is perfectly reasonable and expected, so no logging.
if bytes[0] != module_variant_idx {
return DispatchError::Other(bytes.into_owned())
return DispatchError::Other(bytes.into_owned());
}
// The remaining bytes are the module error, all being well:
@@ -189,7 +186,7 @@ impl DispatchError {
Ok(err) => err,
Err(_) => {
tracing::warn!("Can't decode error: sp_runtime::DispatchError does not match known formats");
return DispatchError::Other(bytes.to_vec())
return DispatchError::Other(bytes.to_vec());
}
};
CurrentModuleError {
@@ -203,7 +200,7 @@ impl DispatchError {
Ok(details) => details,
Err(_) => {
tracing::warn!("Can't decode error: sp_runtime::DispatchError::Module details do not match known information");
return DispatchError::Other(bytes.to_vec())
return DispatchError::Other(bytes.to_vec());
}
};
@@ -223,9 +220,7 @@ impl DispatchError {
#[derive(Clone, Debug, Eq, thiserror::Error, PartialEq)]
pub enum BlockError {
/// The block
#[error(
"Could not find a block with hash {0} (perhaps it was on a non-finalized fork?)"
)]
#[error("Could not find a block with hash {0} (perhaps it was on a non-finalized fork?)")]
BlockHashNotFound(String),
}
@@ -299,14 +294,10 @@ pub enum StorageAddressError {
expected: usize,
},
/// Storage lookup requires a type that wasn't found in the metadata.
#[error(
"Storage lookup requires type {0} to exist in the metadata, but it was not found"
)]
#[error("Storage lookup requires type {0} to exist in the metadata, but it was not found")]
TypeNotFound(u32),
/// This storage entry in the metadata does not have the correct number of hashers to fields.
#[error(
"Storage entry in metadata does not have the correct number of hashers to fields"
)]
#[error("Storage entry in metadata does not have the correct number of hashers to fields")]
WrongNumberOfHashers {
/// The number of hashers in the metadata for this storage entry.
hashers: usize,
+6 -14
View File
@@ -2,13 +2,7 @@
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.
use crate::{
client::OnlineClientT,
error::Error,
events::Events,
rpc::types::StorageKey,
Config,
};
use crate::{client::OnlineClientT, error::Error, events::Events, rpc::types::StorageKey, Config};
use derivative::Derivative;
use std::future::Future;
@@ -54,13 +48,11 @@ where
// for the latest block and use that.
let block_hash = match block_hash {
Some(hash) => hash,
None => {
client
.rpc()
.block_hash(None)
.await?
.expect("didn't pass a block number; qed")
}
None => client
.rpc()
.block_hash(None)
.await?
.expect("didn't pass a block number; qed"),
};
let event_bytes = get_event_bytes(&client, Some(block_hash)).await?;
+21 -63
View File
@@ -4,25 +4,15 @@
//! A representation of a block of events.
use super::{
Phase,
StaticEvent,
};
use super::{Phase, StaticEvent};
use crate::{
client::OnlineClientT,
error::Error,
events::events_client::get_event_bytes,
metadata::{
DecodeWithMetadata,
EventMetadata,
},
Config,
Metadata,
};
use codec::{
Compact,
Decode,
metadata::{DecodeWithMetadata, EventMetadata},
Config, Metadata,
};
use codec::{Compact, Decode};
use derivative::Derivative;
use std::sync::Arc;
@@ -42,11 +32,7 @@ pub struct Events<T: Config> {
}
impl<T: Config> Events<T> {
pub(crate) fn new(
metadata: Metadata,
block_hash: T::Hash,
event_bytes: Vec<u8>,
) -> Self {
pub(crate) fn new(metadata: Metadata, block_hash: T::Hash, event_bytes: Vec<u8>) -> Self {
// event_bytes is a SCALE encoded vector of events. So, pluck the
// compact encoded length from the front, leaving the remaining bytes
// for our iterating to decode.
@@ -348,12 +334,11 @@ impl EventDetails {
let event_metadata = self.event_metadata();
use scale_decode::DecodeAsFields;
let decoded =
<scale_value::Composite<scale_value::scale::TypeId>>::decode_as_fields(
bytes,
event_metadata.fields(),
&self.metadata.runtime_metadata().types,
)?;
let decoded = <scale_value::Composite<scale_value::scale::TypeId>>::decode_as_fields(
bytes,
event_metadata.fields(),
&self.metadata.runtime_metadata().types,
)?;
Ok(decoded)
}
@@ -403,8 +388,7 @@ impl EventDetails {
/// the pallet and event enum variants as well as the event fields). A compatible
/// type for this is exposed via static codegen as a root level `Event` type.
pub fn as_root_event<E: RootEvent>(&self) -> Result<E, Error> {
let pallet_bytes =
&self.all_bytes[self.event_start_idx + 1..self.event_fields_end_idx];
let pallet_bytes = &self.all_bytes[self.event_start_idx + 1..self.event_fields_end_idx];
let pallet = self.metadata.pallet(self.pallet_name())?;
let pallet_event_ty = pallet.event_ty_id().ok_or_else(|| {
Error::Metadata(crate::metadata::MetadataError::EventNotFound(
@@ -443,24 +427,13 @@ pub trait RootEvent: Sized {
#[cfg(test)]
pub(crate) mod test_utils {
use super::*;
use crate::{
Config,
SubstrateConfig,
};
use crate::{Config, SubstrateConfig};
use codec::Encode;
use frame_metadata::{
v14::{
ExtrinsicMetadata,
PalletEventMetadata,
PalletMetadata,
RuntimeMetadataV14,
},
v14::{ExtrinsicMetadata, PalletEventMetadata, PalletMetadata, RuntimeMetadataV14},
RuntimeMetadataPrefixed,
};
use scale_info::{
meta_type,
TypeInfo,
};
use scale_info::{meta_type, TypeInfo};
use std::convert::TryFrom;
/// An "outer" events enum containing exactly one event.
@@ -492,7 +465,7 @@ pub(crate) mod test_utils {
&mut bytes,
pallet_event_ty,
metadata,
)?))
)?));
}
panic!("Asked for pallet name '{pallet_name}', which isn't in our test AllEvents type")
}
@@ -579,12 +552,7 @@ pub(crate) mod test_utils {
#[cfg(test)]
mod tests {
use super::{
test_utils::{
event_record,
events,
events_raw,
AllEvents,
},
test_utils::{event_record, events, events_raw, AllEvents},
*,
};
use codec::Encode;
@@ -627,13 +595,8 @@ mod tests {
let mut actual_bytes = vec![];
for field in actual_fields.into_values() {
scale_value::scale::encode_as_type(
&field,
field.context,
types,
&mut actual_bytes,
)
.expect("should be able to encode properly");
scale_value::scale::encode_as_type(&field, field.context, types, &mut actual_bytes)
.expect("should be able to encode properly");
}
assert_eq!(actual_bytes, actual.field_bytes());
@@ -656,9 +619,7 @@ mod tests {
#[test]
fn statically_decode_single_root_event() {
#[derive(
Clone, Debug, PartialEq, Decode, Encode, TypeInfo, scale_decode::DecodeAsType,
)]
#[derive(Clone, Debug, PartialEq, Decode, Encode, TypeInfo, scale_decode::DecodeAsType)]
enum Event {
A(u8, bool, Vec<String>),
}
@@ -691,9 +652,7 @@ mod tests {
#[test]
fn statically_decode_single_pallet_event() {
#[derive(
Clone, Debug, PartialEq, Decode, Encode, TypeInfo, scale_decode::DecodeAsType,
)]
#[derive(Clone, Debug, PartialEq, Decode, Encode, TypeInfo, scale_decode::DecodeAsType)]
enum Event {
A(u8, bool, Vec<String>),
}
@@ -847,8 +806,7 @@ mod tests {
// Encode 2 events:
let mut event_bytes = vec![];
event_record(Phase::Initialization, Event::A(1)).encode_to(&mut event_bytes);
event_record(Phase::ApplyExtrinsic(123), Event::B(true))
.encode_to(&mut event_bytes);
event_record(Phase::ApplyExtrinsic(123), Event::B(true)).encode_to(&mut event_bytes);
// Push a few naff bytes to the end (a broken third event):
event_bytes.extend_from_slice(&[3, 127, 45, 0, 2]);
+1 -4
View File
@@ -9,10 +9,7 @@
mod events_client;
mod events_type;
use codec::{
Decode,
Encode,
};
use codec::{Decode, Encode};
pub use events_client::EventsClient;
pub use events_type::{
EventDetails,
+5 -10
View File
@@ -144,7 +144,9 @@ pub use subxt_macro::subxt;
pub use getrandom as _;
#[cfg(all(feature = "jsonrpsee-ws", feature = "jsonrpsee-web"))]
std::compile_error!("Both the features `jsonrpsee-ws` and `jsonrpsee-web` are enabled which are mutually exclusive");
std::compile_error!(
"Both the features `jsonrpsee-ws` and `jsonrpsee-web` are enabled which are mutually exclusive"
);
pub mod blocks;
pub mod client;
@@ -163,15 +165,8 @@ pub mod utils;
// Expose a few of the most common types at root,
// but leave most types behind their respective modules.
pub use crate::{
client::{
OfflineClient,
OnlineClient,
},
config::{
Config,
PolkadotConfig,
SubstrateConfig,
},
client::{OfflineClient, OnlineClient},
config::{Config, PolkadotConfig, SubstrateConfig},
error::Error,
metadata::Metadata,
};
+3 -11
View File
@@ -3,10 +3,7 @@
// see LICENSE for license details.
use parking_lot::RwLock;
use std::{
borrow::Cow,
collections::HashMap,
};
use std::{borrow::Cow, collections::HashMap};
/// A cache with the simple goal of storing 32 byte hashes against pallet+item keys
#[derive(Default, Debug)]
@@ -17,12 +14,7 @@ pub struct HashCache {
impl HashCache {
/// get a hash out of the cache by its pallet and item key. If the item doesn't exist,
/// run the function provided to obtain a hash to insert (or bail with some error on failure).
pub fn get_or_insert<F, E>(
&self,
pallet: &str,
item: &str,
f: F,
) -> Result<[u8; 32], E>
pub fn get_or_insert<F, E>(&self, pallet: &str, item: &str, f: F) -> Result<[u8; 32], E>
where
F: FnOnce() -> Result<[u8; 32], E>,
{
@@ -33,7 +25,7 @@ impl HashCache {
.copied();
if let Some(hash) = maybe_hash {
return Ok(hash)
return Ok(hash);
}
let hash = f()?;
+48 -98
View File
@@ -5,24 +5,12 @@
use super::hash_cache::HashCache;
use codec::Error as CodecError;
use frame_metadata::{
PalletConstantMetadata,
RuntimeMetadata,
RuntimeMetadataPrefixed,
RuntimeMetadataV14,
StorageEntryMetadata,
META_RESERVED,
PalletConstantMetadata, RuntimeMetadata, RuntimeMetadataPrefixed, RuntimeMetadataV14,
StorageEntryMetadata, META_RESERVED,
};
use parking_lot::RwLock;
use scale_info::{
form::PortableForm,
PortableRegistry,
Type,
};
use std::{
collections::HashMap,
convert::TryFrom,
sync::Arc,
};
use scale_info::{form::PortableForm, PortableRegistry, Type};
use std::{collections::HashMap, convert::TryFrom, sync::Arc};
/// Metadata error originated from inspecting the internal representation of the runtime metadata.
#[derive(Debug, thiserror::Error, PartialEq, Eq)]
@@ -164,82 +152,54 @@ impl Metadata {
}
/// Obtain the unique hash for a specific storage entry.
pub fn storage_hash(
&self,
pallet: &str,
storage: &str,
) -> Result<[u8; 32], MetadataError> {
pub fn storage_hash(&self, pallet: &str, storage: &str) -> Result<[u8; 32], MetadataError> {
self.inner
.cached_storage_hashes
.get_or_insert(pallet, storage, || {
subxt_metadata::get_storage_hash(&self.inner.metadata, pallet, storage)
.map_err(|e| {
match e {
subxt_metadata::NotFound::Pallet => {
MetadataError::PalletNotFound
}
subxt_metadata::NotFound::Item => {
MetadataError::StorageNotFound
}
}
})
subxt_metadata::get_storage_hash(&self.inner.metadata, pallet, storage).map_err(
|e| match e {
subxt_metadata::NotFound::Pallet => MetadataError::PalletNotFound,
subxt_metadata::NotFound::Item => MetadataError::StorageNotFound,
},
)
})
}
/// Obtain the unique hash for a constant.
pub fn constant_hash(
&self,
pallet: &str,
constant: &str,
) -> Result<[u8; 32], MetadataError> {
pub fn constant_hash(&self, pallet: &str, constant: &str) -> Result<[u8; 32], MetadataError> {
self.inner
.cached_constant_hashes
.get_or_insert(pallet, constant, || {
subxt_metadata::get_constant_hash(&self.inner.metadata, pallet, constant)
.map_err(|e| {
match e {
subxt_metadata::NotFound::Pallet => {
MetadataError::PalletNotFound
}
subxt_metadata::NotFound::Item => {
MetadataError::ConstantNotFound
}
}
})
subxt_metadata::get_constant_hash(&self.inner.metadata, pallet, constant).map_err(
|e| match e {
subxt_metadata::NotFound::Pallet => MetadataError::PalletNotFound,
subxt_metadata::NotFound::Item => MetadataError::ConstantNotFound,
},
)
})
}
/// Obtain the unique hash for a call.
pub fn call_hash(
&self,
pallet: &str,
function: &str,
) -> Result<[u8; 32], MetadataError> {
pub fn call_hash(&self, pallet: &str, function: &str) -> Result<[u8; 32], MetadataError> {
self.inner
.cached_call_hashes
.get_or_insert(pallet, function, || {
subxt_metadata::get_call_hash(&self.inner.metadata, pallet, function)
.map_err(|e| {
match e {
subxt_metadata::NotFound::Pallet => {
MetadataError::PalletNotFound
}
subxt_metadata::NotFound::Item => MetadataError::CallNotFound,
}
})
subxt_metadata::get_call_hash(&self.inner.metadata, pallet, function).map_err(|e| {
match e {
subxt_metadata::NotFound::Pallet => MetadataError::PalletNotFound,
subxt_metadata::NotFound::Item => MetadataError::CallNotFound,
}
})
})
}
/// Obtain the unique hash for this metadata.
pub fn metadata_hash<T: AsRef<str>>(&self, pallets: &[T]) -> [u8; 32] {
if let Some(hash) = *self.inner.cached_metadata_hash.read() {
return hash
return hash;
}
let hash = subxt_metadata::get_metadata_per_pallet_hash(
self.runtime_metadata(),
pallets,
);
let hash = subxt_metadata::get_metadata_per_pallet_hash(self.runtime_metadata(), pallets);
*self.inner.cached_metadata_hash.write() = Some(hash);
hash
@@ -292,10 +252,7 @@ impl PalletMetadata {
}
/// Return [`StorageEntryMetadata`] given some storage key.
pub fn storage(
&self,
key: &str,
) -> Result<&StorageEntryMetadata<PortableForm>, MetadataError> {
pub fn storage(&self, key: &str) -> Result<&StorageEntryMetadata<PortableForm>, MetadataError> {
self.storage.get(key).ok_or(MetadataError::StorageNotFound)
}
@@ -411,7 +368,7 @@ impl TryFrom<RuntimeMetadataPrefixed> for Metadata {
fn try_from(metadata: RuntimeMetadataPrefixed) -> Result<Self, Self::Error> {
if metadata.0 != META_RESERVED {
return Err(InvalidMetadataError::InvalidPrefix)
return Err(InvalidMetadataError::InvalidPrefix);
}
let metadata = match metadata.1 {
RuntimeMetadata::V14(meta) => meta,
@@ -436,24 +393,23 @@ impl TryFrom<RuntimeMetadataPrefixed> for Metadata {
let call_ty_id = pallet.calls.as_ref().map(|c| c.ty.id());
let event_ty_id = pallet.event.as_ref().map(|e| e.ty.id());
let call_metadata =
pallet.calls.as_ref().map_or(Ok(HashMap::new()), |call| {
let type_def_variant = get_type_def_variant(call.ty.id())?;
let call_indexes = type_def_variant
.variants()
.iter()
.map(|v| {
(
v.name().clone(),
CallMetadata {
call_index: v.index(),
fields: v.fields().to_vec(),
},
)
})
.collect();
Ok(call_indexes)
})?;
let call_metadata = pallet.calls.as_ref().map_or(Ok(HashMap::new()), |call| {
let type_def_variant = get_type_def_variant(call.ty.id())?;
let call_indexes = type_def_variant
.variants()
.iter()
.map(|v| {
(
v.name().clone(),
CallMetadata {
call_index: v.index(),
fields: v.fields().to_vec(),
},
)
})
.collect();
Ok(call_indexes)
})?;
let storage = pallet.storage.as_ref().map_or(HashMap::new(), |storage| {
storage
@@ -548,15 +504,9 @@ impl TryFrom<RuntimeMetadataPrefixed> for Metadata {
mod tests {
use super::*;
use frame_metadata::{
ExtrinsicMetadata,
PalletStorageMetadata,
StorageEntryModifier,
StorageEntryType,
};
use scale_info::{
meta_type,
TypeInfo,
ExtrinsicMetadata, PalletStorageMetadata, StorageEntryModifier, StorageEntryType,
};
use scale_info::{meta_type, TypeInfo};
fn load_metadata() -> Metadata {
#[allow(dead_code)]
+2 -10
View File
@@ -12,15 +12,7 @@ mod metadata_type;
pub use metadata_location::MetadataLocation;
pub use metadata_type::{
ErrorMetadata,
EventMetadata,
InvalidMetadataError,
Metadata,
MetadataError,
PalletMetadata,
ErrorMetadata, EventMetadata, InvalidMetadataError, Metadata, MetadataError, PalletMetadata,
};
pub use decode_encode_traits::{
DecodeWithMetadata,
EncodeWithMetadata,
};
pub use decode_encode_traits::{DecodeWithMetadata, EncodeWithMetadata};
+3 -15
View File
@@ -2,24 +2,12 @@
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.
use super::{
RpcClientT,
RpcFuture,
RpcSubscription,
};
use super::{RpcClientT, RpcFuture, RpcSubscription};
use crate::error::RpcError;
use futures::stream::{
StreamExt,
TryStreamExt,
};
use futures::stream::{StreamExt, TryStreamExt};
use jsonrpsee::{
core::{
client::{
Client,
ClientT,
SubscriptionClientT,
SubscriptionKind,
},
client::{Client, ClientT, SubscriptionClientT, SubscriptionKind},
traits::ToRpcParams,
Error as JsonRpseeError,
},
+2 -12
View File
@@ -58,17 +58,7 @@ pub mod types;
pub use rpc::*;
pub use rpc_client_t::{
RawValue,
RpcClientT,
RpcFuture,
RpcSubscription,
RpcSubscriptionId,
RpcSubscriptionStream,
RawValue, RpcClientT, RpcFuture, RpcSubscription, RpcSubscriptionId, RpcSubscriptionStream,
};
pub use rpc_client::{
rpc_params,
RpcClient,
RpcParams,
Subscription,
};
pub use rpc_client::{rpc_params, RpcClient, RpcParams, Subscription};
+11 -43
View File
@@ -33,25 +33,11 @@
use super::{
rpc_params,
types::{
self,
ChainHeadEvent,
FollowEvent,
},
RpcClient,
RpcClientT,
Subscription,
};
use crate::{
error::Error,
utils::PhantomDataSendSync,
Config,
Metadata,
};
use codec::{
Decode,
Encode,
types::{self, ChainHeadEvent, FollowEvent},
RpcClient, RpcClientT, Subscription,
};
use crate::{error::Error, utils::PhantomDataSendSync, Config, Metadata};
use codec::{Decode, Encode};
use frame_metadata::RuntimeMetadataPrefixed;
use serde::Serialize;
use std::sync::Arc;
@@ -222,10 +208,7 @@ impl<T: Config> Rpc<T> {
}
/// Get a header
pub async fn header(
&self,
hash: Option<T::Hash>,
) -> Result<Option<T::Header>, Error> {
pub async fn header(&self, hash: Option<T::Hash>) -> Result<Option<T::Header>, Error> {
let params = rpc_params![hash];
let header = self.client.request("chain_getHeader", params).await?;
Ok(header)
@@ -300,9 +283,7 @@ impl<T: Config> Rpc<T> {
}
/// Subscribe to all new best block headers.
pub async fn subscribe_best_block_headers(
&self,
) -> Result<Subscription<T::Header>, Error> {
pub async fn subscribe_best_block_headers(&self) -> Result<Subscription<T::Header>, Error> {
let subscription = self
.client
.subscribe(
@@ -319,9 +300,7 @@ impl<T: Config> Rpc<T> {
}
/// Subscribe to all new block headers.
pub async fn subscribe_all_block_headers(
&self,
) -> Result<Subscription<T::Header>, Error> {
pub async fn subscribe_all_block_headers(&self) -> Result<Subscription<T::Header>, Error> {
let subscription = self
.client
.subscribe(
@@ -374,10 +353,7 @@ impl<T: Config> Rpc<T> {
}
/// Create and submit an extrinsic and return corresponding Hash if successful
pub async fn submit_extrinsic<X: Encode>(
&self,
extrinsic: X,
) -> Result<T::Hash, Error> {
pub async fn submit_extrinsic<X: Encode>(&self, extrinsic: X) -> Result<T::Hash, Error> {
let bytes: types::Bytes = extrinsic.encode().into();
let params = rpc_params![bytes];
let xt_hash = self
@@ -448,10 +424,7 @@ impl<T: Config> Rpc<T> {
/// `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: types::Bytes,
) -> Result<bool, Error> {
pub async fn has_session_keys(&self, session_keys: types::Bytes) -> Result<bool, Error> {
let params = rpc_params![session_keys];
self.client.request("author_hasSessionKeys", params).await
}
@@ -459,11 +432,7 @@ impl<T: Config> Rpc<T> {
/// 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: types::Bytes,
key_type: String,
) -> Result<bool, Error> {
pub async fn has_key(&self, public_key: types::Bytes, key_type: String) -> Result<bool, Error> {
let params = rpc_params![public_key, key_type];
self.client.request("author_hasKey", params).await
}
@@ -477,8 +446,7 @@ impl<T: Config> Rpc<T> {
at: Option<T::Hash>,
) -> Result<types::DryRunResult, Error> {
let params = rpc_params![to_hex(encoded_signed), at];
let result_bytes: types::Bytes =
self.client.request("system_dryRun", params).await?;
let result_bytes: types::Bytes = self.client.request("system_dryRun", params).await?;
Ok(types::decode_dry_run_result(&mut &*result_bytes.0)?)
}
+6 -21
View File
@@ -2,26 +2,12 @@
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.
use super::{
RpcClientT,
RpcSubscription,
RpcSubscriptionId,
};
use super::{RpcClientT, RpcSubscription, RpcSubscriptionId};
use crate::error::Error;
use futures::{
Stream,
StreamExt,
};
use serde::{
de::DeserializeOwned,
Serialize,
};
use futures::{Stream, StreamExt};
use serde::{de::DeserializeOwned, Serialize};
use serde_json::value::RawValue;
use std::{
pin::Pin,
sync::Arc,
task::Poll,
};
use std::{pin::Pin, sync::Arc, task::Poll};
/// A concrete wrapper around an [`RpcClientT`] which exposes the udnerlying interface via some
/// higher level methods that make it a little easier to work with.
@@ -214,9 +200,8 @@ impl<Res: DeserializeOwned> Stream for Subscription<Res> {
// Decode the inner RawValue to the type we're expecting and map
// any errors to the right shape:
let res = res.map(|r| {
r.map_err(|e| e.into()).and_then(|raw_val| {
serde_json::from_str(raw_val.get()).map_err(|e| e.into())
})
r.map_err(|e| e.into())
.and_then(|raw_val| serde_json::from_str(raw_val.get()).map_err(|e| e.into()))
});
Poll::Ready(res)
+2 -6
View File
@@ -4,10 +4,7 @@
use crate::error::RpcError;
use futures::Stream;
use std::{
future::Future,
pin::Pin,
};
use std::{future::Future, pin::Pin};
// Re-exporting for simplicity since it's used a bunch in the trait definition.
pub use serde_json::value::RawValue;
@@ -56,8 +53,7 @@ pub trait RpcClientT: Send + Sync + 'static {
}
/// A boxed future that is returned from the [`RpcClientT`] methods.
pub type RpcFuture<'a, T> =
Pin<Box<dyn Future<Output = Result<T, RpcError>> + Send + 'a>>;
pub type RpcFuture<'a, T> = Pin<Box<dyn Future<Output = Result<T, RpcError>> + Send + 'a>>;
/// The RPC subscription returned from [`RpcClientT`]'s `subscription` method.
pub struct RpcSubscription {
+25 -76
View File
@@ -5,15 +5,9 @@
//! Types sent to/from the Substrate RPC interface.
use crate::Config;
use codec::{
Decode,
Encode,
};
use codec::{Decode, Encode};
use primitive_types::U256;
use serde::{
Deserialize,
Serialize,
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
// Subscription types are returned from some calls, so expose it with the rest of the returned types.
@@ -334,17 +328,7 @@ pub struct BlockStats {
/// Storage key.
#[derive(
Serialize,
Deserialize,
Hash,
PartialOrd,
Ord,
PartialEq,
Eq,
Clone,
Encode,
Decode,
Debug,
Serialize, Deserialize, Hash, PartialOrd, Ord, PartialEq, Eq, Clone, Encode, Decode, Debug,
)]
pub struct StorageKey(#[serde(with = "impl_serde::serialize")] pub Vec<u8>);
impl AsRef<[u8]> for StorageKey {
@@ -355,17 +339,7 @@ impl AsRef<[u8]> for StorageKey {
/// Storage data.
#[derive(
Serialize,
Deserialize,
Hash,
PartialOrd,
Ord,
PartialEq,
Eq,
Clone,
Encode,
Decode,
Debug,
Serialize, Deserialize, Hash, PartialOrd, Ord, PartialEq, Eq, Clone, Encode, Decode, Debug,
)]
pub struct StorageData(#[serde(with = "impl_serde::serialize")] pub Vec<u8>);
impl AsRef<[u8]> for StorageData {
@@ -709,14 +683,10 @@ impl<Hash> From<TransactionEvent<Hash>> for TransactionEventIR<Hash> {
TransactionEventIR::NonBlock(TransactionEventNonBlockIR::Validated)
}
TransactionEvent::Broadcasted(event) => {
TransactionEventIR::NonBlock(TransactionEventNonBlockIR::Broadcasted(
event,
))
TransactionEventIR::NonBlock(TransactionEventNonBlockIR::Broadcasted(event))
}
TransactionEvent::BestChainBlockIncluded(event) => {
TransactionEventIR::Block(
TransactionEventBlockIR::BestChainBlockIncluded(event),
)
TransactionEventIR::Block(TransactionEventBlockIR::BestChainBlockIncluded(event))
}
TransactionEvent::Finalized(event) => {
TransactionEventIR::Block(TransactionEventBlockIR::Finalized(event))
@@ -737,33 +707,21 @@ impl<Hash> From<TransactionEvent<Hash>> for TransactionEventIR<Hash> {
impl<Hash> From<TransactionEventIR<Hash>> for TransactionEvent<Hash> {
fn from(value: TransactionEventIR<Hash>) -> Self {
match value {
TransactionEventIR::NonBlock(status) => {
match status {
TransactionEventNonBlockIR::Validated => TransactionEvent::Validated,
TransactionEventNonBlockIR::Broadcasted(event) => {
TransactionEvent::Broadcasted(event)
}
TransactionEventNonBlockIR::Error(event) => {
TransactionEvent::Error(event)
}
TransactionEventNonBlockIR::Invalid(event) => {
TransactionEvent::Invalid(event)
}
TransactionEventNonBlockIR::Dropped(event) => {
TransactionEvent::Dropped(event)
}
TransactionEventIR::NonBlock(status) => match status {
TransactionEventNonBlockIR::Validated => TransactionEvent::Validated,
TransactionEventNonBlockIR::Broadcasted(event) => {
TransactionEvent::Broadcasted(event)
}
}
TransactionEventIR::Block(block) => {
match block {
TransactionEventBlockIR::Finalized(event) => {
TransactionEvent::Finalized(event)
}
TransactionEventBlockIR::BestChainBlockIncluded(event) => {
TransactionEvent::BestChainBlockIncluded(event)
}
TransactionEventNonBlockIR::Error(event) => TransactionEvent::Error(event),
TransactionEventNonBlockIR::Invalid(event) => TransactionEvent::Invalid(event),
TransactionEventNonBlockIR::Dropped(event) => TransactionEvent::Dropped(event),
},
TransactionEventIR::Block(block) => match block {
TransactionEventBlockIR::Finalized(event) => TransactionEvent::Finalized(event),
TransactionEventBlockIR::BestChainBlockIncluded(event) => {
TransactionEvent::BestChainBlockIncluded(event)
}
}
},
}
}
}
@@ -773,9 +731,7 @@ mod as_string {
use super::*;
use serde::Deserializer;
pub fn deserialize<'de, D: Deserializer<'de>>(
deserializer: D,
) -> Result<usize, D::Error> {
pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<usize, D::Error> {
String::deserialize(deserializer)?
.parse()
.map_err(|e| serde::de::Error::custom(format!("Parsing failed: {e}")))
@@ -789,10 +745,7 @@ mod test {
/// A util function to assert the result of serialization and deserialization is the same.
pub fn assert_deser<T>(s: &str, expected: T)
where
T: std::fmt::Debug
+ serde::ser::Serialize
+ serde::de::DeserializeOwned
+ PartialEq,
T: std::fmt::Debug + serde::ser::Serialize + serde::de::DeserializeOwned + PartialEq,
{
assert_eq!(serde_json::from_str::<T>(s).unwrap(), expected);
assert_eq!(serde_json::to_string(&expected).unwrap(), s);
@@ -820,10 +773,8 @@ mod test {
..Default::default()
};
let json = serde_json::to_string(&substrate_runtime_version)
.expect("serializing failed");
let val: RuntimeVersion =
serde_json::from_str(&json).expect("deserializing failed");
let json = serde_json::to_string(&substrate_runtime_version).expect("serializing failed");
let val: RuntimeVersion = serde_json::from_str(&json).expect("deserializing failed");
// We ignore any other properties.
assert_eq!(val.spec_version, 123);
@@ -873,8 +824,7 @@ mod test {
InvalidTransaction as SpInvalidTransaction,
TransactionValidityError as SpTransactionValidityError,
},
ApplyExtrinsicResult as SpApplyExtrinsicResult,
DispatchError as SpDispatchError,
ApplyExtrinsicResult as SpApplyExtrinsicResult, DispatchError as SpDispatchError,
};
let pairs = vec![
@@ -915,8 +865,7 @@ mod test {
#[test]
fn storage_types_are_substrate_compatible() {
use sp_core::storage::{
StorageChangeSet as SpStorageChangeSet,
StorageData as SpStorageData,
StorageChangeSet as SpStorageChangeSet, StorageData as SpStorageData,
StorageKey as SpStorageKey,
};
+5 -16
View File
@@ -4,16 +4,9 @@
use super::runtime_types::RuntimeApi;
use crate::{
client::OnlineClientT,
error::Error,
Config,
};
use crate::{client::OnlineClientT, error::Error, Config};
use derivative::Derivative;
use std::{
future::Future,
marker::PhantomData,
};
use std::{future::Future, marker::PhantomData};
/// Execute runtime API calls.
#[derive(Derivative)]
@@ -51,13 +44,9 @@ where
// for the latest block and use that.
let block_hash = match block_hash {
Some(hash) => hash,
None => {
client
.rpc()
.block_hash(None)
.await?
.expect("substrate RPC returns the best block when no block number is provided; qed")
}
None => client.rpc().block_hash(None).await?.expect(
"substrate RPC returns the best block when no block number is provided; qed",
),
};
Ok(RuntimeApi::new(client, block_hash))
+2 -9
View File
@@ -2,16 +2,9 @@
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.
use crate::{
client::OnlineClientT,
error::Error,
Config,
};
use crate::{client::OnlineClientT, error::Error, Config};
use derivative::Derivative;
use std::{
future::Future,
marker::PhantomData,
};
use std::{future::Future, marker::PhantomData};
/// Execute runtime API calls.
#[derive(Derivative)]
+3 -18
View File
@@ -12,10 +12,7 @@ pub mod utils;
pub use storage_client::StorageClient;
pub use storage_type::{
KeyIter,
Storage,
};
pub use storage_type::{KeyIter, Storage};
// Re-export as this is used in the public API in this module:
pub use crate::rpc::types::StorageKey;
@@ -24,22 +21,10 @@ pub use crate::rpc::types::StorageKey;
/// entry lives and how to properly decode it.
pub mod address {
pub use super::storage_address::{
dynamic,
dynamic_root,
Address,
DynamicAddress,
StaticStorageMapKey,
StorageAddress,
Yes,
dynamic, dynamic_root, Address, DynamicAddress, StaticStorageMapKey, StorageAddress, Yes,
};
}
// For consistency with other modules, also expose
// the basic address stuff at the root of the module.
pub use storage_address::{
dynamic,
dynamic_root,
Address,
DynamicAddress,
StorageAddress,
};
pub use storage_address::{dynamic, dynamic_root, Address, DynamicAddress, StorageAddress};
+8 -30
View File
@@ -3,24 +3,11 @@
// see LICENSE for license details.
use crate::{
dynamic::{
DecodedValueThunk,
Value,
},
error::{
Error,
StorageAddressError,
},
metadata::{
DecodeWithMetadata,
EncodeWithMetadata,
Metadata,
},
};
use frame_metadata::{
StorageEntryType,
StorageHasher,
dynamic::{DecodedValueThunk, Value},
error::{Error, StorageAddressError},
metadata::{DecodeWithMetadata, EncodeWithMetadata, Metadata},
};
use frame_metadata::{StorageEntryType, StorageHasher};
use scale_info::TypeDef;
use std::borrow::Cow;
@@ -47,11 +34,7 @@ pub trait StorageAddress {
/// Output the non-prefix bytes; that is, any additional bytes that need
/// to be appended to the key to dig into maps.
fn append_entry_bytes(
&self,
metadata: &Metadata,
bytes: &mut Vec<u8>,
) -> Result<(), Error>;
fn append_entry_bytes(&self, metadata: &Metadata, bytes: &mut Vec<u8>) -> Result<(), Error>;
/// An optional hash which, if present, will be checked against
/// the node metadata to confirm that the return type matches what
@@ -77,8 +60,7 @@ pub struct Address<StorageKey, ReturnTy, Fetchable, Defaultable, Iterable> {
/// A typical storage address constructed at runtime rather than via the `subxt` macro; this
/// has no restriction on what it can be used for (since we don't statically know).
pub type DynamicAddress<StorageKey> =
Address<StorageKey, DecodedValueThunk, Yes, Yes, Yes>;
pub type DynamicAddress<StorageKey> = Address<StorageKey, DecodedValueThunk, Yes, Yes, Yes>;
impl<StorageKey, ReturnTy, Fetchable, Defaultable, Iterable>
Address<StorageKey, ReturnTy, Fetchable, Defaultable, Iterable>
@@ -154,11 +136,7 @@ where
&self.entry_name
}
fn append_entry_bytes(
&self,
metadata: &Metadata,
bytes: &mut Vec<u8>,
) -> Result<(), Error> {
fn append_entry_bytes(&self, metadata: &Metadata, bytes: &mut Vec<u8>) -> Result<(), Error> {
let pallet = metadata.pallet(&self.pallet_name)?;
let storage = pallet.storage(&self.entry_name)?;
@@ -193,7 +171,7 @@ where
expected: type_ids.len(),
actual: self.storage_entry_keys.len(),
}
.into())
.into());
}
if hashers.len() == 1 {
+11 -29
View File
@@ -3,27 +3,17 @@
// see LICENSE for license details.
use super::{
storage_type::{
validate_storage_address,
Storage,
},
utils,
StorageAddress,
storage_type::{validate_storage_address, Storage},
utils, StorageAddress,
};
use crate::{
client::{
OfflineClientT,
OnlineClientT,
},
client::{OfflineClientT, OnlineClientT},
error::Error,
Config,
};
use derivative::Derivative;
use std::{
future::Future,
marker::PhantomData,
};
use std::{future::Future, marker::PhantomData};
/// Query the runtime storage.
#[derive(Derivative)]
@@ -52,19 +42,13 @@ where
/// if the address is valid (or if it's not possible to check since the address has no validation hash).
/// Return an error if the address was not valid or something went wrong trying to validate it (ie
/// the pallet or storage entry in question do not exist at all).
pub fn validate<Address: StorageAddress>(
&self,
address: &Address,
) -> Result<(), Error> {
pub fn validate<Address: StorageAddress>(&self, address: &Address) -> Result<(), Error> {
validate_storage_address(address, &self.client.metadata())
}
/// Convert some storage address into the raw bytes that would be submitted to the node in order
/// to retrieve the entries at the root of the associated address.
pub fn address_root_bytes<Address: StorageAddress>(
&self,
address: &Address,
) -> Vec<u8> {
pub fn address_root_bytes<Address: StorageAddress>(&self, address: &Address) -> Vec<u8> {
utils::storage_address_root_bytes(address)
}
@@ -99,13 +83,11 @@ where
// for the latest block and use that.
let block_hash = match block_hash {
Some(hash) => hash,
None => {
client
.rpc()
.block_hash(None)
.await?
.expect("didn't pass a block number; qed")
}
None => client
.rpc()
.block_hash(None)
.await?
.expect("didn't pass a block number; qed"),
};
Ok(Storage::new(client, block_hash))
+15 -37
View File
@@ -2,30 +2,18 @@
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.
use super::storage_address::{
StorageAddress,
Yes,
};
use super::storage_address::{StorageAddress, Yes};
use crate::{
client::OnlineClientT,
error::Error,
metadata::{
DecodeWithMetadata,
Metadata,
},
rpc::types::{
StorageData,
StorageKey,
},
metadata::{DecodeWithMetadata, Metadata},
rpc::types::{StorageData, StorageKey},
Config,
};
use derivative::Derivative;
use frame_metadata::StorageEntryType;
use scale_info::form::PortableForm;
use std::{
future::Future,
marker::PhantomData,
};
use std::{future::Future, marker::PhantomData};
/// Query the runtime storage.
#[derive(Derivative)]
@@ -150,15 +138,10 @@ where
// We have to dig into metadata already, so no point using the optimised `decode_storage_with_metadata` call.
let pallet_metadata = metadata.pallet(pallet_name)?;
let storage_metadata = pallet_metadata.storage(storage_name)?;
let return_ty_id =
return_type_from_storage_entry_type(&storage_metadata.ty);
let return_ty_id = return_type_from_storage_entry_type(&storage_metadata.ty);
let bytes = &mut &storage_metadata.default[..];
let val = Address::Target::decode_with_metadata(
bytes,
return_ty_id,
&metadata,
)?;
let val = Address::Target::decode_with_metadata(bytes, return_ty_id, &metadata)?;
Ok(val)
}
}
@@ -237,11 +220,8 @@ where
// Look up the return type for flexible decoding. Do this once here to avoid
// potentially doing it every iteration if we used `decode_storage_with_metadata`
// in the iterator.
let return_type_id = lookup_storage_return_type(
&metadata,
address.pallet_name(),
address.entry_name(),
)?;
let return_type_id =
lookup_storage_return_type(&metadata, address.pallet_name(), address.entry_name())?;
// The root pallet/entry bytes for this storage entry:
let address_root_bytes = super::utils::storage_address_root_bytes(&address);
@@ -289,7 +269,7 @@ where
self.return_type_id,
&self.metadata,
)?;
return Ok(Some((k, val)))
return Ok(Some((k, val)));
} else {
let start_key = self.start_key.take();
let keys = self
@@ -302,7 +282,7 @@ where
.await?;
if keys.is_empty() {
return Ok(None)
return Ok(None);
}
self.start_key = keys.last().cloned();
@@ -350,13 +330,11 @@ fn validate_storage(
};
match expected_hash == hash {
true => Ok(()),
false => {
Err(crate::error::MetadataError::IncompatibleStorageMetadata(
pallet_name.into(),
storage_name.into(),
)
.into())
}
false => Err(crate::error::MetadataError::IncompatibleStorageMetadata(
pallet_name.into(),
storage_name.into(),
)
.into()),
}
}
+2 -7
View File
@@ -7,10 +7,7 @@
//! the trait itself.
use super::StorageAddress;
use crate::{
error::Error,
metadata::Metadata,
};
use crate::{error::Error, metadata::Metadata};
/// Return the root of a given [`StorageAddress`]: hash the pallet name and entry name
/// and append those bytes to the output.
@@ -35,9 +32,7 @@ pub(crate) fn storage_address_bytes<Address: StorageAddress>(
}
/// Outputs a vector containing the bytes written by [`write_storage_address_root_bytes`].
pub(crate) fn storage_address_root_bytes<Address: StorageAddress>(
addr: &Address,
) -> Vec<u8> {
pub(crate) fn storage_address_root_bytes<Address: StorageAddress>(addr: &Address) -> Vec<u8> {
let mut bytes = Vec::new();
write_storage_address_root_bytes(addr, &mut bytes);
bytes
+3 -16
View File
@@ -21,20 +21,7 @@ pub use self::signer::PairSigner;
pub use self::{
signer::Signer,
tx_client::{
SubmittableExtrinsic,
TxClient,
},
tx_payload::{
dynamic,
BoxedPayload,
DynamicPayload,
Payload,
TxPayload,
},
tx_progress::{
TxInBlock,
TxProgress,
TxStatus,
},
tx_client::{SubmittableExtrinsic, TxClient},
tx_payload::{dynamic, BoxedPayload, DynamicPayload, Payload, TxPayload},
tx_progress::{TxInBlock, TxProgress, TxStatus},
};
+4 -8
View File
@@ -35,12 +35,8 @@ mod pair_signer {
use crate::Config;
use sp_core::Pair as PairT;
use sp_runtime::{
traits::{
IdentifyAccount,
Verify,
},
AccountId32 as SpAccountId32,
MultiSignature as SpMultiSignature,
traits::{IdentifyAccount, Verify},
AccountId32 as SpAccountId32, MultiSignature as SpMultiSignature,
};
/// A [`Signer`] implementation that can be constructed from an [`sp_core::Pair`].
@@ -62,8 +58,8 @@ mod pair_signer {
{
/// Creates a new [`Signer`] from an [`sp_core::Pair`].
pub fn new(signer: Pair) -> Self {
let account_id = <SpMultiSignature as Verify>::Signer::from(signer.public())
.into_account();
let account_id =
<SpMultiSignature as Verify>::Signer::from(signer.public()).into_account();
Self {
account_id: account_id.into(),
signer,
+11 -36
View File
@@ -4,29 +4,13 @@
use super::TxPayload;
use crate::{
client::{
OfflineClientT,
OnlineClientT,
},
config::{
Config,
ExtrinsicParams,
Hasher,
},
client::{OfflineClientT, OnlineClientT},
config::{Config, ExtrinsicParams, Hasher},
error::Error,
tx::{
Signer as SignerT,
TxProgress,
},
utils::{
Encoded,
PhantomDataSendSync,
},
};
use codec::{
Compact,
Encode,
tx::{Signer as SignerT, TxProgress},
utils::{Encoded, PhantomDataSendSync},
};
use codec::{Compact, Encode};
use derivative::Derivative;
use std::borrow::Cow;
@@ -62,14 +46,13 @@ impl<T: Config, C: OfflineClientT<T>> TxClient<T, C> {
{
if let Some(details) = call.validation_details() {
let metadata = self.client.metadata();
let expected_hash =
metadata.call_hash(details.pallet_name, details.call_name)?;
let expected_hash = metadata.call_hash(details.pallet_name, details.call_name)?;
if details.hash != expected_hash {
return Err(crate::metadata::MetadataError::IncompatibleCallMetadata(
details.pallet_name.into(),
details.call_name.into(),
)
.into())
.into());
}
}
Ok(())
@@ -87,10 +70,7 @@ impl<T: Config, C: OfflineClientT<T>> TxClient<T, C> {
}
/// Creates an unsigned extrinsic without submitting it.
pub fn create_unsigned<Call>(
&self,
call: &Call,
) -> Result<SubmittableExtrinsic<T, C>, Error>
pub fn create_unsigned<Call>(&self, call: &Call) -> Result<SubmittableExtrinsic<T, C>, Error>
where
Call: TxPayload,
{
@@ -107,8 +87,7 @@ impl<T: Config, C: OfflineClientT<T>> TxClient<T, C> {
call.encode_call_data_to(&self.client.metadata(), &mut encoded_inner)?;
// now, prefix byte length:
let len = Compact(
u32::try_from(encoded_inner.len())
.expect("extrinsic size expected to be <4GB"),
u32::try_from(encoded_inner.len()).expect("extrinsic size expected to be <4GB"),
);
let mut encoded = Vec::new();
len.encode_to(&mut encoded);
@@ -193,10 +172,7 @@ where
C: OnlineClientT<T>,
{
// Get the next account nonce to use.
async fn next_account_nonce(
&self,
account_id: &T::AccountId,
) -> Result<T::Index, Error> {
async fn next_account_nonce(&self, account_id: &T::AccountId) -> Result<T::Index, Error> {
self.client
.rpc()
.system_account_next_index(account_id)
@@ -399,8 +375,7 @@ where
encoded_inner.extend(&self.call_data);
// now, prefix byte length:
let len = Compact(
u32::try_from(encoded_inner.len())
.expect("extrinsic size expected to be <4GB"),
u32::try_from(encoded_inner.len()).expect("extrinsic size expected to be <4GB"),
);
let mut encoded = Vec::new();
len.encode_to(&mut encoded);
+9 -30
View File
@@ -5,32 +5,17 @@
//! This module contains the trait and types used to represent
//! transactions that can be submitted.
use crate::{
dynamic::Value,
error::Error,
metadata::Metadata,
};
use crate::{dynamic::Value, error::Error, metadata::Metadata};
use codec::Encode;
use scale_encode::EncodeAsFields;
use scale_value::{
Composite,
ValueDef,
Variant,
};
use std::{
borrow::Cow,
sync::Arc,
};
use scale_value::{Composite, ValueDef, Variant};
use std::{borrow::Cow, sync::Arc};
/// This represents a transaction payload that can be submitted
/// to a node.
pub trait TxPayload {
/// Encode call data to the provided output.
fn encode_call_data_to(
&self,
metadata: &Metadata,
out: &mut Vec<u8>,
) -> Result<(), Error>;
fn encode_call_data_to(&self, metadata: &Metadata, out: &mut Vec<u8>) -> Result<(), Error>;
/// Encode call data and return the output. This is a convenience
/// wrapper around [`TxPayload::encode_call_data_to`].
@@ -151,11 +136,7 @@ impl Payload<Composite<()>> {
}
impl<CallData: EncodeAsFields> TxPayload for Payload<CallData> {
fn encode_call_data_to(
&self,
metadata: &Metadata,
out: &mut Vec<u8>,
) -> Result<(), Error> {
fn encode_call_data_to(&self, metadata: &Metadata, out: &mut Vec<u8>) -> Result<(), Error> {
let pallet = metadata.pallet(&self.pallet_name)?;
let call = pallet.call(&self.call_name)?;
@@ -171,12 +152,10 @@ impl<CallData: EncodeAsFields> TxPayload for Payload<CallData> {
}
fn validation_details(&self) -> Option<ValidationDetails<'_>> {
self.validation_hash.map(|hash| {
ValidationDetails {
pallet_name: &self.pallet_name,
call_name: &self.call_name,
hash,
}
self.validation_hash.map(|hash| ValidationDetails {
pallet_name: &self.pallet_name,
call_name: &self.call_name,
hash,
})
}
}
+10 -29
View File
@@ -8,24 +8,13 @@ use std::task::Poll;
use crate::{
client::OnlineClientT,
error::{
DispatchError,
Error,
RpcError,
TransactionError,
},
error::{DispatchError, Error, RpcError, TransactionError},
events::EventsClient,
rpc::types::{
Subscription,
SubstrateTxStatus,
},
rpc::types::{Subscription, SubstrateTxStatus},
Config,
};
use derivative::Derivative;
use futures::{
Stream,
StreamExt,
};
use futures::{Stream, StreamExt};
/// This struct represents a subscription to the progress of some transaction.
#[derive(Derivative)]
@@ -163,11 +152,7 @@ impl<T: Config, C: OnlineClientT<T>> Stream for TxProgress<T, C> {
SubstrateTxStatus::Ready => TxStatus::Ready,
SubstrateTxStatus::Broadcast(peers) => TxStatus::Broadcast(peers),
SubstrateTxStatus::InBlock(hash) => {
TxStatus::InBlock(TxInBlock::new(
hash,
self.ext_hash,
self.client.clone(),
))
TxStatus::InBlock(TxInBlock::new(hash, self.ext_hash, self.client.clone()))
}
SubstrateTxStatus::Retracted(hash) => TxStatus::Retracted(hash),
SubstrateTxStatus::Usurped(hash) => TxStatus::Usurped(hash),
@@ -188,11 +173,7 @@ impl<T: Config, C: OnlineClientT<T>> Stream for TxProgress<T, C> {
}
SubstrateTxStatus::Finalized(hash) => {
self.sub = None;
TxStatus::Finalized(TxInBlock::new(
hash,
self.ext_hash,
self.client.clone(),
))
TxStatus::Finalized(TxInBlock::new(hash, self.ext_hash, self.client.clone()))
}
}
})
@@ -335,9 +316,7 @@ impl<T: Config, C: OnlineClientT<T>> TxInBlock<T, C> {
///
/// **Note:** This has to download block details from the node and decode events
/// from them.
pub async fn wait_for_success(
&self,
) -> Result<crate::blocks::ExtrinsicEvents<T>, Error> {
pub async fn wait_for_success(&self) -> Result<crate::blocks::ExtrinsicEvents<T>, Error> {
let events = self.fetch_events().await?;
// Try to find any errors; return the first one we encounter.
@@ -346,7 +325,7 @@ impl<T: Config, C: OnlineClientT<T>> TxInBlock<T, C> {
if ev.pallet_name() == "System" && ev.variant_name() == "ExtrinsicFailed" {
let dispatch_error =
DispatchError::decode_from(ev.field_bytes(), &self.client.metadata());
return Err(dispatch_error.into())
return Err(dispatch_error.into());
}
}
@@ -367,7 +346,9 @@ impl<T: Config, C: OnlineClientT<T>> TxInBlock<T, C> {
.await?
.ok_or(Error::Transaction(TransactionError::BlockHashNotFound))?;
let extrinsic_idx = block.block.extrinsics
let extrinsic_idx = block
.block
.extrinsics
.iter()
.position(|ext| {
use crate::config::Hasher;
+7 -17
View File
@@ -6,14 +6,8 @@
//! This doesn't contain much functionality itself, but is easy to convert to/from an `sp_core::AccountId32`
//! for instance, to gain functionality without forcing a dependency on Substrate crates here.
use codec::{
Decode,
Encode,
};
use serde::{
Deserialize,
Serialize,
};
use codec::{Decode, Encode};
use serde::{Deserialize, Serialize};
/// A 32-byte cryptographic identifier. This is a simplified version of Substrate's
/// `sp_core::crypto::AccountId32`. To obtain more functionality, convert this into
@@ -80,7 +74,7 @@ impl AccountId32 {
use base58::FromBase58;
let data = s.from_base58().map_err(|_| FromSs58Error::BadBase58)?;
if data.len() < 2 {
return Err(FromSs58Error::BadLength)
return Err(FromSs58Error::BadLength);
}
let prefix_len = match data[0] {
0..=63 => 1,
@@ -88,14 +82,13 @@ impl AccountId32 {
_ => return Err(FromSs58Error::InvalidPrefix),
};
if data.len() != prefix_len + body_len + CHECKSUM_LEN {
return Err(FromSs58Error::BadLength)
return Err(FromSs58Error::BadLength);
}
let hash = ss58hash(&data[0..body_len + prefix_len]);
let checksum = &hash[0..CHECKSUM_LEN];
if data[body_len + prefix_len..body_len + prefix_len + CHECKSUM_LEN] != *checksum
{
if data[body_len + prefix_len..body_len + prefix_len + CHECKSUM_LEN] != *checksum {
// Invalid checksum.
return Err(FromSs58Error::InvalidChecksum)
return Err(FromSs58Error::InvalidChecksum);
}
let result = data[prefix_len..body_len + prefix_len]
@@ -121,10 +114,7 @@ pub enum FromSs58Error {
// We do this just to get a checksum to help verify the validity of the address in to_ss58check
fn ss58hash(data: &[u8]) -> Vec<u8> {
use blake2::{
Blake2b512,
Digest,
};
use blake2::{Blake2b512, Digest};
const PREFIX: &[u8] = b"SS58PRE";
let mut ctx = Blake2b512::new();
ctx.update(PREFIX);
+7 -17
View File
@@ -4,16 +4,9 @@
//! Generic `scale_bits` over `bitvec`-like `BitOrder` and `BitFormat` types.
use codec::{
Compact,
Input,
};
use codec::{Compact, Input};
use scale_bits::{
scale::format::{
Format,
OrderFormat,
StoreFormat,
},
scale::format::{Format, OrderFormat, StoreFormat},
Bits,
};
use scale_decode::IntoVisitor;
@@ -109,7 +102,7 @@ impl<Store: BitStore, Order: BitOrder> codec::Decode for DecodedBits<Store, Orde
let Compact(bits) = <Compact<u32>>::decode(input)?;
// Otherwise it is impossible to store it on 32bit machine.
if bits > ARCH32BIT_BITSLICE_MAX_BITS {
return Err("Attempt to decode a BitVec with too many bits".into())
return Err("Attempt to decode a BitVec with too many bits".into());
}
// NOTE: Replace with `bits.div_ceil(Store::BITS)` if `int_roundings` is stabilised
let elements = (bits / Store::BITS) + u32::from(bits % Store::BITS != 0);
@@ -125,8 +118,7 @@ impl<Store: BitStore, Order: BitOrder> codec::Decode for DecodedBits<Store, Orde
storage.extend(vec![0; bytes_needed]);
input.read(&mut storage[prefix_len..])?;
let decoder =
scale_bits::decode_using_format_from(&storage, bit_format::<Store, Order>())?;
let decoder = scale_bits::decode_using_format_from(&storage, bit_format::<Store, Order>())?;
let bits = decoder.collect::<Result<Vec<_>, _>>()?;
let bits = Bits::from_iter(bits);
@@ -172,11 +164,9 @@ impl<Store, Order> scale_decode::Visitor for DecodedBitsVisitor<Store, Order> {
types,
Bits::into_visitor(),
)
.map(|bits| {
DecodedBits {
bits,
_marker: PhantomData,
}
.map(|bits| DecodedBits {
bits,
_marker: PhantomData,
});
scale_decode::visitor::DecodeAsTypeResult::Decoded(res)
}
+2 -9
View File
@@ -10,10 +10,7 @@ mod multi_address;
mod multi_signature;
mod wrapper_opaque;
use codec::{
Decode,
Encode,
};
use codec::{Decode, Encode};
use derivative::Derivative;
pub use account_id::AccountId32;
@@ -23,11 +20,7 @@ pub use wrapper_opaque::WrapperKeepOpaque;
// Used in codegen
#[doc(hidden)]
pub use primitive_types::{
H160,
H256,
H512,
};
pub use primitive_types::{H160, H256, H512};
/// Wraps an already encoded byte vector, prevents being encoded as a raw byte vector as part of
/// the transaction payload
+2 -8
View File
@@ -6,10 +6,7 @@
//! This doesn't contain much functionality itself, but is easy to convert to/from an `sp_runtime::MultiAddress`
//! for instance, to gain functionality without forcing a dependency on Substrate crates here.
use codec::{
Decode,
Encode,
};
use codec::{Decode, Encode};
/// A multi-format address wrapper for on-chain accounts. This is a simplified version of Substrate's
/// `sp_runtime::MultiAddress`. To obtain more functionality, convert this into that type (this conversion
@@ -48,10 +45,7 @@ impl<AccountId, AccountIndex> From<AccountId> for MultiAddress<AccountId, Accoun
// Improve compat with the substrate version if we're using those crates:
#[cfg(feature = "substrate-compat")]
mod substrate_impls {
use super::{
super::AccountId32,
*,
};
use super::{super::AccountId32, *};
impl<N> From<sp_runtime::AccountId32> for MultiAddress<AccountId32, N> {
fn from(value: sp_runtime::AccountId32) -> Self {
+1 -4
View File
@@ -6,10 +6,7 @@
//! This doesn't contain much functionality itself, but is easy to convert to/from an `sp_runtime::MultiSignature`
//! for instance, to gain functionality without forcing a dependency on Substrate crates here.
use codec::{
Decode,
Encode,
};
use codec::{Decode, Encode};
/// Signature container that can store known signature types. This is a simplified version of
/// `sp_runtime::MultiSignature`. To obtain more functionality, convert this into that type.
+10 -33
View File
@@ -3,17 +3,9 @@
// see LICENSE for license details.
use super::PhantomDataSendSync;
use codec::{
Compact,
Decode,
DecodeAll,
Encode,
};
use codec::{Compact, Decode, DecodeAll, Encode};
use derivative::Derivative;
use scale_decode::{
IntoVisitor,
Visitor,
};
use scale_decode::{IntoVisitor, Visitor};
use scale_encode::EncodeAsType;
/// A wrapper for any type `T` which implement encode/decode in a way compatible with `Vec<u8>`.
@@ -88,11 +80,7 @@ impl<T> EncodeAsType for WrapperKeepOpaque<T> {
types: &scale_info::PortableRegistry,
out: &mut Vec<u8>,
) -> Result<(), scale_encode::Error> {
use scale_encode::error::{
Error,
ErrorKind,
Kind,
};
use scale_encode::error::{Error, ErrorKind, Kind};
let Some(ty) = types.resolve(type_id) else {
return Err(Error::new(ErrorKind::TypeNotFound(type_id)))
@@ -111,7 +99,7 @@ impl<T> EncodeAsType for WrapperKeepOpaque<T> {
return Err(Error::new(ErrorKind::WrongShape {
actual: Kind::Struct,
expected: type_id,
}))
}));
}
// Just blat the bytes out.
@@ -130,21 +118,18 @@ impl<T> Visitor for WrapperKeepOpaqueVisitor<T> {
value: &mut scale_decode::visitor::types::Composite<'scale, 'info>,
_type_id: scale_decode::visitor::TypeId,
) -> Result<Self::Value<'scale, 'info>, Self::Error> {
use scale_decode::error::{
Error,
ErrorKind,
};
use scale_decode::error::{Error, ErrorKind};
if value.path().ident().as_deref() != Some("WrapperKeepOpaque") {
return Err(Error::new(ErrorKind::Custom(
"Type to decode is not 'WrapperTypeKeepOpaque'".into(),
)))
)));
}
if value.remaining() != 2 {
return Err(Error::new(ErrorKind::WrongLength {
actual_len: value.remaining(),
expected_len: 2,
}))
}));
}
// The field to decode is a compact len followed by bytes. Decode the length, then grab the bytes.
@@ -184,13 +169,7 @@ mod test {
impl<T: scale_info::TypeInfo + 'static> scale_info::TypeInfo for WrapperKeepOpaque<T> {
type Identity = Self;
fn type_info() -> scale_info::Type {
use scale_info::{
build::Fields,
meta_type,
Path,
Type,
TypeParameter,
};
use scale_info::{build::Fields, meta_type, Path, Type, TypeParameter};
Type::builder()
.path(Path::new("WrapperKeepOpaque", module_path!()))
@@ -204,8 +183,7 @@ mod test {
}
/// Given a type definition, return type ID and registry representing it.
fn make_type<T: scale_info::TypeInfo + 'static>(
) -> (u32, scale_info::PortableRegistry) {
fn make_type<T: scale_info::TypeInfo + 'static>() -> (u32, scale_info::PortableRegistry) {
let m = scale_info::MetaType::new::<T>();
let mut types = scale_info::Registry::new();
let id = types.register_type(&m);
@@ -239,8 +217,7 @@ mod test {
.expect("decode-as-type decodes");
let decode_scale_codec_bytes = &mut &*scale_codec_encoded;
let decoded_scale_codec =
T::decode(decode_scale_codec_bytes).expect("scale-codec decodes");
let decoded_scale_codec = T::decode(decode_scale_codec_bytes).expect("scale-codec decodes");
assert!(
decode_as_type_bytes.is_empty(),