Rename traits to remove T suffix (#1535)

* Rename traits to renmove T suffix

* Fix doc links

* Fix straggler doc links
This commit is contained in:
James Wilson
2024-04-16 16:35:14 +01:00
committed by GitHub
parent 1e111ea9db
commit ac606cf625
32 changed files with 2426 additions and 2229 deletions
+9 -7
View File
@@ -12,7 +12,7 @@ use derive_where::derive_where;
/// This represents a constant address. Anything implementing this trait
/// can be used to fetch constants.
pub trait AddressT {
pub trait Address {
/// The target type of the value that lives at this address.
type Target: DecodeWithMetadata;
@@ -32,18 +32,20 @@ pub trait AddressT {
/// This represents the address of a constant.
#[derive_where(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
pub struct Address<ReturnTy> {
pub struct DefaultAddress<ReturnTy> {
pallet_name: Cow<'static, str>,
constant_name: Cow<'static, str>,
constant_hash: Option<[u8; 32]>,
_marker: core::marker::PhantomData<ReturnTy>,
}
/// The type of address used by our static codegen.
pub type StaticAddress<ReturnTy> = DefaultAddress<ReturnTy>;
/// The type of address typically used to return dynamic constant values.
pub type DynamicAddress = Address<DecodedValueThunk>;
pub type DynamicAddress = DefaultAddress<DecodedValueThunk>;
impl<ReturnTy> Address<ReturnTy> {
/// Create a new [`Address`] to use to look up a constant.
impl<ReturnTy> DefaultAddress<ReturnTy> {
/// Create a new [`DefaultAddress`] to use to look up a constant.
pub fn new(pallet_name: impl Into<String>, constant_name: impl Into<String>) -> Self {
Self {
pallet_name: Cow::Owned(pallet_name.into()),
@@ -53,7 +55,7 @@ impl<ReturnTy> Address<ReturnTy> {
}
}
/// Create a new [`Address`] that will be validated
/// Create a new [`DefaultAddress`] that will be validated
/// against node metadata using the hash given.
#[doc(hidden)]
pub fn new_static(
@@ -80,7 +82,7 @@ impl<ReturnTy> Address<ReturnTy> {
}
}
impl<ReturnTy: DecodeWithMetadata> AddressT for Address<ReturnTy> {
impl<ReturnTy: DecodeWithMetadata> Address for DefaultAddress<ReturnTy> {
type Target = ReturnTy;
fn pallet_name(&self) -> &str {
+4 -7
View File
@@ -40,7 +40,7 @@
pub mod address;
use address::AddressT;
use address::Address;
use alloc::borrow::ToOwned;
use crate::{error::MetadataError, metadata::DecodeWithMetadata, Error, Metadata};
@@ -50,7 +50,7 @@ use crate::{error::MetadataError, metadata::DecodeWithMetadata, Error, Metadata}
///
/// When the provided `address` is dynamic (and thus does not come with any expectation of the
/// shape of the constant value), this just returns `Ok(())`
pub fn validate<Address: AddressT>(address: &Address, metadata: &Metadata) -> Result<(), Error> {
pub fn validate<Addr: Address>(address: &Addr, metadata: &Metadata) -> Result<(), Error> {
if let Some(actual_hash) = address.validation_hash() {
let expected_hash = metadata
.pallet_by_name_err(address.pallet_name())?
@@ -67,10 +67,7 @@ pub fn validate<Address: AddressT>(address: &Address, metadata: &Metadata) -> Re
/// Fetch a constant out of the metadata given a constant address. If the `address` has been
/// statically generated, this will validate that the constant shape is as expected, too.
pub fn get<Address: AddressT>(
address: &Address,
metadata: &Metadata,
) -> Result<Address::Target, Error> {
pub fn get<Addr: Address>(address: &Addr, metadata: &Metadata) -> Result<Addr::Target, Error> {
// 1. Validate constant shape if hash given:
validate(address, metadata)?;
@@ -79,7 +76,7 @@ pub fn get<Address: AddressT>(
.pallet_by_name_err(address.pallet_name())?
.constant_by_name(address.constant_name())
.ok_or_else(|| MetadataError::ConstantNameNotFound(address.constant_name().to_owned()))?;
let value = <Address::Target as DecodeWithMetadata>::decode_with_metadata(
let value = <Addr::Target as DecodeWithMetadata>::decode_with_metadata(
&mut constant.value(),
constant.ty(),
metadata,
+4 -4
View File
@@ -8,13 +8,13 @@ use crate::dynamic::DecodedValueThunk;
use crate::metadata::DecodeWithMetadata;
use derive_where::derive_where;
/// Use this with [`AddressT::IsDecodable`].
/// Use this with [`Address::IsDecodable`].
pub use crate::utils::Yes;
/// This represents the address of a custom value in in the metadata.
/// Anything that implements it can be used to fetch custom values from the metadata.
/// The trait is implemented by [`str`] for dynamic lookup and [`StaticAddress`] for static queries.
pub trait AddressT {
pub trait Address {
/// The type of the custom value.
type Target: DecodeWithMetadata;
/// Should be set to `Yes` for Dynamic values and static values that have a valid type.
@@ -30,7 +30,7 @@ pub trait AddressT {
}
}
impl AddressT for str {
impl Address for str {
type Target = DecodedValueThunk;
type IsDecodable = Yes;
@@ -68,7 +68,7 @@ impl<ReturnTy, IsDecodable> StaticAddress<ReturnTy, IsDecodable> {
}
}
impl<R: DecodeWithMetadata, Y> AddressT for StaticAddress<R, Y> {
impl<R: DecodeWithMetadata, Y> Address for StaticAddress<R, Y> {
type Target = R;
type IsDecodable = Y;
+8 -11
View File
@@ -34,16 +34,13 @@ pub mod address;
use crate::utils::Yes;
use crate::{error::MetadataError, metadata::DecodeWithMetadata, Error, Metadata};
use address::AddressT;
use address::Address;
use alloc::vec::Vec;
/// Run the validation logic against some custom value address you'd like to access. Returns `Ok(())`
/// if the address is valid (or if it's not possible to check since the address has no validation hash).
/// Returns an error if the address was not valid (wrong name, type or raw bytes)
pub fn validate<Address: AddressT + ?Sized>(
address: &Address,
metadata: &Metadata,
) -> Result<(), Error> {
pub fn validate<Addr: Address + ?Sized>(address: &Addr, metadata: &Metadata) -> Result<(), Error> {
if let Some(actual_hash) = address.validation_hash() {
let custom = metadata.custom();
let custom_value = custom
@@ -62,16 +59,16 @@ pub fn validate<Address: AddressT + ?Sized>(
/// Access a custom value by the address it is registered under. This can be just a [str] to get back a dynamic value,
/// or a static address from the generated static interface to get a value of a static type returned.
pub fn get<Address: AddressT<IsDecodable = Yes> + ?Sized>(
address: &Address,
pub fn get<Addr: Address<IsDecodable = Yes> + ?Sized>(
address: &Addr,
metadata: &Metadata,
) -> Result<Address::Target, Error> {
) -> Result<Addr::Target, Error> {
// 1. Validate custom value shape if hash given:
validate(address, metadata)?;
// 2. Attempt to decode custom value:
let custom_value = metadata.custom_value_by_name_err(address.name())?;
let value = <Address::Target as DecodeWithMetadata>::decode_with_metadata(
let value = <Addr::Target as DecodeWithMetadata>::decode_with_metadata(
&mut custom_value.bytes(),
custom_value.type_id(),
metadata,
@@ -80,8 +77,8 @@ pub fn get<Address: AddressT<IsDecodable = Yes> + ?Sized>(
}
/// Access the bytes of a custom value by the address it is registered under.
pub fn get_bytes<Address: AddressT + ?Sized>(
address: &Address,
pub fn get_bytes<Addr: Address + ?Sized>(
address: &Addr,
metadata: &Metadata,
) -> Result<Vec<u8>, Error> {
// 1. Validate custom value shape if hash given:
+8 -11
View File
@@ -49,13 +49,13 @@ use alloc::borrow::ToOwned;
use alloc::format;
use alloc::string::String;
use alloc::vec::Vec;
use payload::PayloadT;
use payload::Payload;
/// Run the validation logic against some runtime API payload you'd like to use. Returns `Ok(())`
/// if the payload is valid (or if it's not possible to check since the payload has no validation hash).
/// Return an error if the payload was not valid or something went wrong trying to validate it (ie
/// the runtime API in question do not exist at all)
pub fn validate<Payload: PayloadT>(payload: &Payload, metadata: &Metadata) -> Result<(), Error> {
pub fn validate<P: Payload>(payload: &P, metadata: &Metadata) -> Result<(), Error> {
let Some(static_hash) = payload.validation_hash() else {
return Ok(());
};
@@ -72,30 +72,27 @@ pub fn validate<Payload: PayloadT>(payload: &Payload, metadata: &Metadata) -> Re
}
/// Return the name of the runtime API call from the payload.
pub fn call_name<Payload: PayloadT>(payload: &Payload) -> String {
pub fn call_name<P: Payload>(payload: &P) -> String {
format!("{}_{}", payload.trait_name(), payload.method_name())
}
/// Return the encoded call args given a runtime API payload.
pub fn call_args<Payload: PayloadT>(
payload: &Payload,
metadata: &Metadata,
) -> Result<Vec<u8>, Error> {
pub fn call_args<P: Payload>(payload: &P, metadata: &Metadata) -> Result<Vec<u8>, Error> {
payload.encode_args(metadata)
}
/// Decode the value bytes at the location given by the provided runtime API payload.
pub fn decode_value<Payload: PayloadT>(
pub fn decode_value<P: Payload>(
bytes: &mut &[u8],
payload: &Payload,
payload: &P,
metadata: &Metadata,
) -> Result<Payload::ReturnType, Error> {
) -> Result<P::ReturnType, Error> {
let api_method = metadata
.runtime_api_trait_by_name_err(payload.trait_name())?
.method_by_name(payload.method_name())
.ok_or_else(|| MetadataError::RuntimeMethodNotFound(payload.method_name().to_owned()))?;
let val = <Payload::ReturnType as DecodeWithMetadata>::decode_with_metadata(
let val = <P::ReturnType as DecodeWithMetadata>::decode_with_metadata(
&mut &bytes[..],
api_method.output_ty(),
metadata,
+17 -15
View File
@@ -40,7 +40,7 @@ use crate::metadata::{DecodeWithMetadata, Metadata};
/// - encoded arguments
///
/// Each argument of the runtime function must be scale-encoded.
pub trait PayloadT {
pub trait Payload {
/// The return type of the function call.
// Note: `DecodeWithMetadata` is needed to decode the function call result
// with the `subxt::Metadata.
@@ -56,7 +56,7 @@ pub trait PayloadT {
fn encode_args_to(&self, metadata: &Metadata, out: &mut Vec<u8>) -> Result<(), Error>;
/// Encode arguments data and return the output. This is a convenience
/// wrapper around [`PayloadT::encode_args_to`].
/// wrapper around [`Payload::encode_args_to`].
fn encode_args(&self, metadata: &Metadata) -> Result<Vec<u8>, Error> {
let mut v = Vec::new();
self.encode_args_to(metadata, &mut v)?;
@@ -75,7 +75,7 @@ pub trait PayloadT {
/// This can be created from static values (ie those generated
/// via the `subxt` macro) or dynamic values via [`dynamic`].
#[derive_where(Clone, Debug, Eq, Ord, PartialEq, PartialOrd; ArgsData)]
pub struct Payload<ArgsData, ReturnTy> {
pub struct DefaultPayload<ArgsData, ReturnTy> {
trait_name: Cow<'static, str>,
method_name: Cow<'static, str>,
args_data: ArgsData,
@@ -83,8 +83,13 @@ pub struct Payload<ArgsData, ReturnTy> {
_marker: PhantomData<ReturnTy>,
}
impl<ArgsData: EncodeAsFields, ReturnTy: DecodeWithMetadata> PayloadT
for Payload<ArgsData, ReturnTy>
/// A statically generated runtime API payload.
pub type StaticPayload<ArgsData, ReturnTy> = DefaultPayload<ArgsData, ReturnTy>;
/// A dynamic runtime API payload.
pub type DynamicPayload = DefaultPayload<Composite<()>, DecodedValueThunk>;
impl<ArgsData: EncodeAsFields, ReturnTy: DecodeWithMetadata> Payload
for DefaultPayload<ArgsData, ReturnTy>
{
type ReturnType = ReturnTy;
@@ -115,17 +120,14 @@ impl<ArgsData: EncodeAsFields, ReturnTy: DecodeWithMetadata> PayloadT
}
}
/// A dynamic runtime API payload.
pub type DynamicPayload = Payload<Composite<()>, DecodedValueThunk>;
impl<ReturnTy, ArgsData> Payload<ArgsData, ReturnTy> {
/// Create a new [`Payload`].
impl<ReturnTy, ArgsData> DefaultPayload<ArgsData, ReturnTy> {
/// Create a new [`DefaultPayload`].
pub fn new(
trait_name: impl Into<String>,
method_name: impl Into<String>,
args_data: ArgsData,
) -> Self {
Payload {
DefaultPayload {
trait_name: Cow::Owned(trait_name.into()),
method_name: Cow::Owned(method_name.into()),
args_data,
@@ -134,7 +136,7 @@ impl<ReturnTy, ArgsData> Payload<ArgsData, ReturnTy> {
}
}
/// Create a new static [`Payload`] using static function name
/// Create a new static [`DefaultPayload`] using static function name
/// and scale-encoded argument data.
///
/// This is only expected to be used from codegen.
@@ -144,8 +146,8 @@ impl<ReturnTy, ArgsData> Payload<ArgsData, ReturnTy> {
method_name: &'static str,
args_data: ArgsData,
hash: [u8; 32],
) -> Payload<ArgsData, ReturnTy> {
Payload {
) -> DefaultPayload<ArgsData, ReturnTy> {
DefaultPayload {
trait_name: Cow::Borrowed(trait_name),
method_name: Cow::Borrowed(method_name),
args_data,
@@ -184,5 +186,5 @@ pub fn dynamic(
method_name: impl Into<String>,
args_data: impl Into<Composite<()>>,
) -> DynamicPayload {
Payload::new(trait_name, method_name, args_data.into())
DefaultPayload::new(trait_name, method_name, args_data.into())
}
+10 -7
View File
@@ -21,7 +21,7 @@ pub use super::storage_key::{StaticStorageKey, StorageHashers, StorageHashersIte
/// This represents a storage address. Anything implementing this trait
/// can be used to fetch and iterate over storage entries.
pub trait AddressT {
pub trait Address {
/// The target type of the value that lives at this address.
type Target: DecodeWithMetadata;
/// The keys type used to construct this address.
@@ -57,7 +57,7 @@ pub trait AddressT {
/// A concrete storage address. This can be created from static values (ie those generated
/// via the `subxt` macro) or dynamic values via [`dynamic`].
#[derive_where(Clone, Debug, Eq, Ord, PartialEq, PartialOrd; Keys)]
pub struct Address<Keys: StorageKey, ReturnTy, Fetchable, Defaultable, Iterable> {
pub struct DefaultAddress<Keys: StorageKey, ReturnTy, Fetchable, Defaultable, Iterable> {
pallet_name: Cow<'static, str>,
entry_name: Cow<'static, str>,
keys: Keys,
@@ -65,9 +65,12 @@ pub struct Address<Keys: StorageKey, ReturnTy, Fetchable, Defaultable, Iterable>
_marker: core::marker::PhantomData<(ReturnTy, Fetchable, Defaultable, Iterable)>,
}
/// A storage address constructed by the static codegen.
pub type StaticAddress<Keys, ReturnTy, Fetchable, Defaultable, Iterable> =
DefaultAddress<Keys, 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<Keys> = Address<Keys, DecodedValueThunk, Yes, Yes, Yes>;
pub type DynamicAddress<Keys> = DefaultAddress<Keys, DecodedValueThunk, Yes, Yes, Yes>;
impl<Keys: StorageKey> DynamicAddress<Keys> {
/// Creates a new dynamic address. As `Keys` you can use a `Vec<scale_value::Value>`
@@ -83,7 +86,7 @@ impl<Keys: StorageKey> DynamicAddress<Keys> {
}
impl<Keys, ReturnTy, Fetchable, Defaultable, Iterable>
Address<Keys, ReturnTy, Fetchable, Defaultable, Iterable>
DefaultAddress<Keys, ReturnTy, Fetchable, Defaultable, Iterable>
where
Keys: StorageKey,
ReturnTy: DecodeWithMetadata,
@@ -108,7 +111,7 @@ where
}
impl<Keys, ReturnTy, Fetchable, Defaultable, Iterable>
Address<Keys, ReturnTy, Fetchable, Defaultable, Iterable>
DefaultAddress<Keys, ReturnTy, Fetchable, Defaultable, Iterable>
where
Keys: StorageKey,
ReturnTy: DecodeWithMetadata,
@@ -127,8 +130,8 @@ where
}
}
impl<Keys, ReturnTy, Fetchable, Defaultable, Iterable> AddressT
for Address<Keys, ReturnTy, Fetchable, Defaultable, Iterable>
impl<Keys, ReturnTy, Fetchable, Defaultable, Iterable> Address
for DefaultAddress<Keys, ReturnTy, Fetchable, Defaultable, Iterable>
where
Keys: StorageKey,
ReturnTy: DecodeWithMetadata,
+13 -13
View File
@@ -47,7 +47,7 @@ mod utils;
pub mod address;
use crate::{error::MetadataError, metadata::DecodeWithMetadata, Error, Metadata};
use address::AddressT;
use address::Address;
use alloc::vec::Vec;
// This isn't a part of the public API, but expose here because it's useful in Subxt.
@@ -59,7 +59,7 @@ pub use utils::lookup_storage_entry_details;
///
/// When the provided `address` is dynamic (and thus does not come with any expectation of the
/// shape of the constant value), this just returns `Ok(())`
pub fn validate<Address: AddressT>(address: &Address, metadata: &Metadata) -> Result<(), Error> {
pub fn validate<Addr: Address>(address: &Addr, metadata: &Metadata) -> Result<(), Error> {
let Some(hash) = address.validation_hash() else {
return Ok(());
};
@@ -80,8 +80,8 @@ pub fn validate<Address: AddressT>(address: &Address, metadata: &Metadata) -> Re
/// Given a storage address and some metadata, this encodes the address into bytes which can be
/// handed to a node to retrieve the corresponding value.
pub fn get_address_bytes<Address: AddressT>(
address: &Address,
pub fn get_address_bytes<Addr: Address>(
address: &Addr,
metadata: &Metadata,
) -> Result<Vec<u8>, Error> {
let mut bytes = Vec::new();
@@ -93,7 +93,7 @@ pub fn get_address_bytes<Address: AddressT>(
/// Given a storage address and some metadata, this encodes the root of the address (ie the pallet
/// and storage entry part) into bytes. If the entry being addressed is inside a map, this returns
/// the bytes needed to iterate over all of the entries within it.
pub fn get_address_root_bytes<Address: AddressT>(address: &Address) -> Vec<u8> {
pub fn get_address_root_bytes<Addr: Address>(address: &Addr) -> Vec<u8> {
let mut bytes = Vec::new();
utils::write_storage_address_root_bytes(address, &mut bytes);
bytes
@@ -102,11 +102,11 @@ pub fn get_address_root_bytes<Address: AddressT>(address: &Address) -> Vec<u8> {
/// Given some storage value that we've retrieved from a node, the address used to retrieve it, and
/// metadata from the node, this function attempts to decode the bytes into the target value specified
/// by the address.
pub fn decode_value<Address: AddressT>(
pub fn decode_value<Addr: Address>(
bytes: &mut &[u8],
address: &Address,
address: &Addr,
metadata: &Metadata,
) -> Result<Address::Target, Error> {
) -> Result<Addr::Target, Error> {
let pallet_name = address.pallet_name();
let entry_name = address.entry_name();
@@ -117,15 +117,15 @@ pub fn decode_value<Address: AddressT>(
subxt_metadata::StorageEntryType::Map { value_ty, .. } => *value_ty,
};
let val = Address::Target::decode_with_metadata(bytes, value_ty_id, metadata)?;
let val = Addr::Target::decode_with_metadata(bytes, value_ty_id, metadata)?;
Ok(val)
}
/// Return the default value at a given storage address if one is available, or an error otherwise.
pub fn default_value<Address: AddressT>(
address: &Address,
pub fn default_value<Addr: Address>(
address: &Addr,
metadata: &Metadata,
) -> Result<Address::Target, Error> {
) -> Result<Addr::Target, Error> {
let pallet_name = address.pallet_name();
let entry_name = address.entry_name();
@@ -137,6 +137,6 @@ pub fn default_value<Address: AddressT>(
};
let default_bytes = entry_metadata.default_bytes();
let val = Address::Target::decode_with_metadata(&mut &*default_bytes, value_ty_id, metadata)?;
let val = Addr::Target::decode_with_metadata(&mut &*default_bytes, value_ty_id, metadata)?;
Ok(val)
}
+4 -4
View File
@@ -2,20 +2,20 @@
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.
//! these utility methods complement the [`AddressT`] trait, but
//! these utility methods complement the [`Address`] trait, but
//! aren't things that should ever be overridden, and so don't exist on
//! the trait itself.
use super::address::AddressT;
use super::address::Address;
use crate::error::{Error, MetadataError};
use crate::metadata::Metadata;
use alloc::borrow::ToOwned;
use alloc::vec::Vec;
use subxt_metadata::{PalletMetadata, StorageEntryMetadata, StorageHasher};
/// Return the root of a given [`AddressT`]: hash the pallet name and entry name
/// Return the root of a given [`Address`]: hash the pallet name and entry name
/// and append those bytes to the output.
pub fn write_storage_address_root_bytes<Address: AddressT>(addr: &Address, out: &mut Vec<u8>) {
pub fn write_storage_address_root_bytes<Addr: Address>(addr: &Addr, out: &mut Vec<u8>) {
out.extend(sp_crypto_hashing::twox_128(addr.pallet_name().as_bytes()));
out.extend(sp_crypto_hashing::twox_128(addr.entry_name().as_bytes()));
}
+6 -6
View File
@@ -64,7 +64,7 @@ use crate::utils::Encoded;
use alloc::borrow::{Cow, ToOwned};
use alloc::vec::Vec;
use codec::{Compact, Encode};
use payload::PayloadT;
use payload::Payload;
use signer::Signer as SignerT;
use sp_crypto_hashing::blake2_256;
@@ -75,7 +75,7 @@ pub use crate::client::{ClientState, RuntimeVersion};
/// if the call is valid (or if it's not possible to check since the call has no validation hash).
/// Return an error if the call was not valid or something went wrong trying to validate it (ie
/// the pallet or call in question do not exist at all).
pub fn validate<Call: PayloadT>(call: &Call, metadata: &Metadata) -> Result<(), Error> {
pub fn validate<Call: Payload>(call: &Call, metadata: &Metadata) -> Result<(), Error> {
if let Some(details) = call.validation_details() {
let expected_hash = metadata
.pallet_by_name_err(details.pallet_name)?
@@ -90,14 +90,14 @@ pub fn validate<Call: PayloadT>(call: &Call, metadata: &Metadata) -> Result<(),
}
/// Return the SCALE encoded bytes representing the call data of the transaction.
pub fn call_data<Call: PayloadT>(call: &Call, metadata: &Metadata) -> Result<Vec<u8>, Error> {
pub fn call_data<Call: Payload>(call: &Call, metadata: &Metadata) -> Result<Vec<u8>, Error> {
let mut bytes = Vec::new();
call.encode_call_data_to(metadata, &mut bytes)?;
Ok(bytes)
}
/// Creates an unsigned extrinsic without submitting it.
pub fn create_unsigned<T: Config, Call: PayloadT>(
pub fn create_unsigned<T: Config, Call: Payload>(
call: &Call,
metadata: &Metadata,
) -> Result<Transaction<T>, Error> {
@@ -130,7 +130,7 @@ pub fn create_unsigned<T: Config, Call: PayloadT>(
///
/// Note: if not provided, the default account nonce will be set to 0 and the default mortality will be _immortal_.
/// This is because this method runs offline, and so is unable to fetch the data needed for more appropriate values.
pub fn create_partial_signed<T: Config, Call: PayloadT>(
pub fn create_partial_signed<T: Config, Call: Payload>(
call: &Call,
client_state: &ClientState<T>,
params: <T::ExtrinsicParams as ExtrinsicParams<T>>::Params,
@@ -165,7 +165,7 @@ pub fn create_signed<T, Call, Signer>(
) -> Result<Transaction<T>, Error>
where
T: Config,
Call: PayloadT,
Call: Payload,
Signer: SignerT<T>,
{
// 1. Validate this call against the current node metadata if the call comes
+15 -13
View File
@@ -18,12 +18,12 @@ use scale_value::{Composite, Value, ValueDef, Variant};
/// This represents a transaction payload that can be submitted
/// to a node.
pub trait PayloadT {
pub trait Payload {
/// Encode call data to the provided output.
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 [`PayloadT::encode_call_data_to`].
/// wrapper around [`Payload::encode_call_data_to`].
fn encode_call_data(&self, metadata: &Metadata) -> Result<Vec<u8>, Error> {
let mut v = Vec::new();
self.encode_call_data_to(metadata, &mut v)?;
@@ -51,24 +51,26 @@ pub struct ValidationDetails<'a> {
/// A transaction payload containing some generic `CallData`.
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct Payload<CallData> {
pub struct DefaultPayload<CallData> {
pallet_name: Cow<'static, str>,
call_name: Cow<'static, str>,
call_data: CallData,
validation_hash: Option<[u8; 32]>,
}
/// The payload type used by static codegen.
pub type StaticPayload<Calldata> = DefaultPayload<Calldata>;
/// The type of a payload typically used for dynamic transaction payloads.
pub type DynamicPayload = Payload<Composite<()>>;
pub type DynamicPayload = DefaultPayload<Composite<()>>;
impl<CallData> Payload<CallData> {
/// Create a new [`Payload`].
impl<CallData> DefaultPayload<CallData> {
/// Create a new [`DefaultPayload`].
pub fn new(
pallet_name: impl Into<String>,
call_name: impl Into<String>,
call_data: CallData,
) -> Self {
Payload {
DefaultPayload {
pallet_name: Cow::Owned(pallet_name.into()),
call_name: Cow::Owned(call_name.into()),
call_data,
@@ -76,7 +78,7 @@ impl<CallData> Payload<CallData> {
}
}
/// Create a new [`Payload`] using static strings for the pallet and call name.
/// Create a new [`DefaultPayload`] using static strings for the pallet and call name.
/// This is only expected to be used from codegen.
#[doc(hidden)]
pub fn new_static(
@@ -85,7 +87,7 @@ impl<CallData> Payload<CallData> {
call_data: CallData,
validation_hash: [u8; 32],
) -> Self {
Payload {
DefaultPayload {
pallet_name: Cow::Borrowed(pallet_name),
call_name: Cow::Borrowed(call_name),
call_data,
@@ -117,7 +119,7 @@ impl<CallData> Payload<CallData> {
}
}
impl Payload<Composite<()>> {
impl DefaultPayload<Composite<()>> {
/// Convert the dynamic `Composite` payload into a [`Value`].
/// This is useful if you want to use this as an argument for a
/// larger dynamic call that wants to use this as a nested call.
@@ -134,7 +136,7 @@ impl Payload<Composite<()>> {
}
}
impl<CallData: EncodeAsFields> PayloadT for Payload<CallData> {
impl<CallData: EncodeAsFields> Payload for DefaultPayload<CallData> {
fn encode_call_data_to(&self, metadata: &Metadata, out: &mut Vec<u8>) -> Result<(), Error> {
let pallet = metadata.pallet_by_name_err(&self.pallet_name)?;
let call = pallet
@@ -167,12 +169,12 @@ impl<CallData: EncodeAsFields> PayloadT for Payload<CallData> {
}
}
/// Construct a transaction at runtime; essentially an alias to [`Payload::new()`]
/// Construct a transaction at runtime; essentially an alias to [`DefaultPayload::new()`]
/// which provides a [`Composite`] value for the call data.
pub fn dynamic(
pallet_name: impl Into<String>,
call_name: impl Into<String>,
call_data: impl Into<Composite<()>>,
) -> DynamicPayload {
Payload::new(pallet_name, call_name, call_data.into())
DefaultPayload::new(pallet_name, call_name, call_data.into())
}