Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Rename traits to remove T suffix #1535

Merged
merged 3 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions codegen/src/api/calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use scale_typegen::typegen::ir::ToTokensWithSettings;
use scale_typegen::{typegen::ir::type_ir::CompositeIRKind, TypeGenerator};
use subxt_metadata::PalletMetadata;

/// Generate calls from the provided pallet's metadata. Each call returns a `StaticTxPayload`
/// Generate calls from the provided pallet's metadata. Each call returns a `StaticPayload`
/// that can be passed to the subxt client to submit/sign/encode.
///
/// # Arguments
Expand Down Expand Up @@ -92,8 +92,8 @@ pub fn generate_calls(
pub fn #fn_name(
&self,
#( #call_fn_args, )*
) -> #crate_path::tx::payload::Payload<types::#struct_name> {
#crate_path::tx::payload::Payload::new_static(
) -> #crate_path::tx::payload::StaticPayload<types::#struct_name> {
#crate_path::tx::payload::StaticPayload::new_static(
#pallet_name,
#call_name,
types::#struct_name { #( #call_args, )* },
Expand Down
4 changes: 2 additions & 2 deletions codegen/src/api/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ pub fn generate_constants(

Ok(quote! {
#docs
pub fn #fn_name(&self) -> #crate_path::constants::address::Address<#return_ty> {
#crate_path::constants::address::Address::new_static(
pub fn #fn_name(&self) -> #crate_path::constants::address::StaticAddress<#return_ty> {
#crate_path::constants::address::StaticAddress::new_static(
#pallet_name,
#constant_name,
[#(#constant_hash,)*]
Expand Down
4 changes: 2 additions & 2 deletions codegen/src/api/runtime_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ fn generate_runtime_api(

let method = quote!(
#docs
pub fn #method_name(&self, #( #fn_params, )* ) -> #crate_path::runtime_api::payload::Payload<types::#struct_name, types::#method_name::output::Output> {
#crate_path::runtime_api::payload::Payload::new_static(
pub fn #method_name(&self, #( #fn_params, )* ) -> #crate_path::runtime_api::payload::StaticPayload<types::#struct_name, types::#method_name::output::Output> {
#crate_path::runtime_api::payload::StaticPayload::new_static(
#trait_name_str,
#method_name_str,
types::#struct_name { #( #param_names, )* },
Expand Down
4 changes: 2 additions & 2 deletions codegen/src/api/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,14 @@ fn generate_storage_entry_fns(
pub fn #fn_name(
&self,
#(#key_args,)*
) -> #crate_path::storage::address::Address::<
) -> #crate_path::storage::address::StaticAddress::<
#keys_type,
#alias_storage_path,
#is_fetchable_type,
#is_defaultable_type,
#is_iterable_type
> {
#crate_path::storage::address::Address::new_static(
#crate_path::storage::address::StaticAddress::new_static(
#pallet_name,
#storage_name,
#keys,
Expand Down
16 changes: 9 additions & 7 deletions core/src/constants/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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()),
Expand All @@ -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(
Expand All @@ -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 {
Expand Down
11 changes: 4 additions & 7 deletions core/src/constants/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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())?
Expand All @@ -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)?;

Expand All @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions core/src/custom_values/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -30,7 +30,7 @@ pub trait AddressT {
}
}

impl AddressT for str {
impl Address for str {
type Target = DecodedValueThunk;
type IsDecodable = Yes;

Expand Down Expand Up @@ -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;

Expand Down
19 changes: 8 additions & 11 deletions core/src/custom_values/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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:
Expand Down
19 changes: 8 additions & 11 deletions core/src/runtime_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(());
};
Expand All @@ -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,
Expand Down
Loading
Loading