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

Tidy up some metadata accessing #978

Merged
merged 3 commits into from
May 26, 2023
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
5 changes: 1 addition & 4 deletions subxt/src/blocks/extrinsic_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,7 @@ where

/// Fetch the metadata for this extrinsic.
pub fn extrinsic_metadata(&self) -> Result<ExtrinsicMetadataDetails, Error> {
let pallet = self
.metadata
.pallet_by_index(self.pallet_index())
.ok_or_else(|| MetadataError::PalletIndexNotFound(self.pallet_index()))?;
let pallet = self.metadata.pallet_by_index_err(self.pallet_index())?;
let variant = pallet
.call_variant_by_index(self.variant_index())
.ok_or_else(|| MetadataError::VariantIndexNotFound(self.variant_index()))?;
Expand Down
9 changes: 3 additions & 6 deletions subxt/src/constants/constants_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ impl<T: Config, Client: OfflineClientT<T>> ConstantsClient<T, Client> {
let expected_hash = self
.client
.metadata()
.pallet_by_name(address.pallet_name())
.ok_or_else(|| MetadataError::PalletNameNotFound(address.pallet_name().to_owned()))?
.pallet_by_name_err(address.pallet_name())?
.constant_hash(address.constant_name())
.ok_or_else(|| {
MetadataError::ConstantNameNotFound(address.constant_name().to_owned())
Expand All @@ -65,10 +64,8 @@ impl<T: Config, Client: OfflineClientT<T>> ConstantsClient<T, Client> {
self.validate(address)?;

// 2. Attempt to decode the constant into the type given:
let pallet = metadata
.pallet_by_name(address.pallet_name())
.ok_or_else(|| MetadataError::PalletNameNotFound(address.pallet_name().to_owned()))?;
let constant = pallet
let constant = metadata
.pallet_by_name_err(address.pallet_name())?
.constant_by_name(address.constant_name())
.ok_or_else(|| {
MetadataError::ConstantNameNotFound(address.constant_name().to_owned())
Expand Down
6 changes: 1 addition & 5 deletions subxt/src/error/dispatch_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,7 @@ impl std::fmt::Display for ModuleError {
impl ModuleError {
/// Return more details about this error.
pub fn details(&self) -> Result<ModuleErrorDetails, MetadataError> {
let pallet = self
.metadata
.pallet_by_index(self.raw.pallet_index)
.ok_or(MetadataError::PalletIndexNotFound(self.raw.pallet_index))?;

let pallet = self.metadata.pallet_by_index_err(self.raw.pallet_index)?;
let variant = pallet
.error_variant_by_index(self.raw.error[0])
.ok_or_else(|| MetadataError::VariantIndexNotFound(self.raw.error[0]))?;
Expand Down
4 changes: 1 addition & 3 deletions subxt/src/events/events_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,7 @@ impl EventDetails {
let event_fields_start_idx = all_bytes.len() - input.len();

// Get metadata for the event:
let event_pallet = metadata
.pallet_by_index(pallet_index)
.ok_or(MetadataError::PalletIndexNotFound(pallet_index))?;
let event_pallet = metadata.pallet_by_index_err(pallet_index)?;
let event_variant = event_pallet
.event_variant_by_index(variant_index)
.ok_or(MetadataError::VariantIndexNotFound(variant_index))?;
Expand Down
28 changes: 28 additions & 0 deletions subxt/src/metadata/metadata_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.

use crate::error::MetadataError;
use std::sync::Arc;

/// A cheaply clone-able representation of the runtime metadata received from a node.
Expand All @@ -23,6 +24,33 @@ impl Metadata {
inner: Arc::new(md),
}
}

/// Identical to `metadata.pallet_by_name()`, but returns an error if the pallet is not found.
pub fn pallet_by_name_err(
&self,
name: &str,
) -> Result<subxt_metadata::PalletMetadata, MetadataError> {
self.pallet_by_name(name)
.ok_or_else(|| MetadataError::PalletNameNotFound(name.to_owned()))
}

/// Identical to `metadata.pallet_by_index()`, but returns an error if the pallet is not found.
pub fn pallet_by_index_err(
&self,
index: u8,
) -> Result<subxt_metadata::PalletMetadata, MetadataError> {
self.pallet_by_index(index)
.ok_or(MetadataError::PalletIndexNotFound(index))
}

/// Identical to `metadata.runtime_api_trait_by_name()`, but returns an error if the trait is not found.
pub fn runtime_api_trait_by_name_err(
&self,
name: &str,
) -> Result<subxt_metadata::RuntimeApiMetadata, MetadataError> {
self.runtime_api_trait_by_name(name)
.ok_or_else(|| MetadataError::RuntimeTraitNotFound(name.to_owned()))
}
}

impl From<subxt_metadata::Metadata> for Metadata {
Expand Down
6 changes: 2 additions & 4 deletions subxt/src/runtime_api/runtime_payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,8 @@ impl<ArgsData: EncodeAsFields, ReturnTy: DecodeWithMetadata> RuntimeApiPayload
}

fn encode_args_to(&self, metadata: &Metadata, out: &mut Vec<u8>) -> Result<(), Error> {
let api_trait = metadata
.runtime_api_trait_by_name(&self.trait_name)
.ok_or_else(|| MetadataError::RuntimeTraitNotFound((*self.trait_name).to_owned()))?;
let api_method = api_trait
let api_method = metadata
.runtime_api_trait_by_name_err(&self.trait_name)?
.method_by_name(&self.method_name)
.ok_or_else(|| MetadataError::RuntimeMethodNotFound((*self.method_name).to_owned()))?;

Expand Down
6 changes: 1 addition & 5 deletions subxt/src/runtime_api/runtime_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,7 @@ where
async move {
let metadata = client.metadata();

let api_trait = metadata
.runtime_api_trait_by_name(payload.trait_name())
.ok_or_else(|| {
MetadataError::RuntimeTraitNotFound(payload.trait_name().to_owned())
})?;
let api_trait = metadata.runtime_api_trait_by_name_err(payload.trait_name())?;
let api_method = api_trait
.method_by_name(payload.method_name())
.ok_or_else(|| {
Expand Down
4 changes: 1 addition & 3 deletions subxt/src/storage/storage_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,7 @@ where
}

fn append_entry_bytes(&self, metadata: &Metadata, bytes: &mut Vec<u8>) -> Result<(), Error> {
let pallet = metadata
.pallet_by_name(self.pallet_name())
.ok_or_else(|| MetadataError::PalletNameNotFound(self.pallet_name().to_owned()))?;
let pallet = metadata.pallet_by_name_err(self.pallet_name())?;
let storage = pallet
.storage()
.ok_or_else(|| MetadataError::StorageNotFoundInPallet(self.pallet_name().to_owned()))?;
Expand Down
6 changes: 2 additions & 4 deletions subxt/src/storage/storage_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::{

use crate::{
client::{OfflineClientT, OnlineClientT},
error::{Error, MetadataError},
error::Error,
Config,
};
use derivative::Derivative;
Expand Down Expand Up @@ -44,9 +44,7 @@ where
/// the pallet or storage entry in question do not exist at all).
pub fn validate<Address: StorageAddress>(&self, address: &Address) -> Result<(), Error> {
let metadata = self.client.metadata();
let pallet_metadata = metadata
.pallet_by_name(address.pallet_name())
.ok_or_else(|| MetadataError::PalletNameNotFound(address.pallet_name().to_owned()))?;
let pallet_metadata = metadata.pallet_by_name_err(address.pallet_name())?;
validate_storage_address(address, pallet_metadata)
}

Expand Down
4 changes: 1 addition & 3 deletions subxt/src/storage/storage_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,7 @@ fn lookup_entry_details<'a>(
entry_name: &str,
metadata: &'a Metadata,
) -> Result<(PalletMetadata<'a>, &'a StorageEntryMetadata), Error> {
let pallet_metadata = metadata
.pallet_by_name(pallet_name)
.ok_or_else(|| MetadataError::PalletNameNotFound(pallet_name.to_owned()))?;
let pallet_metadata = metadata.pallet_by_name_err(pallet_name)?;
let storage_metadata = pallet_metadata
.storage()
.ok_or_else(|| MetadataError::StorageNotFoundInPallet(pallet_name.to_owned()))?;
Expand Down
3 changes: 1 addition & 2 deletions subxt/src/tx/tx_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ impl<T: Config, C: OfflineClientT<T>> TxClient<T, C> {
let expected_hash = self
.client
.metadata()
.pallet_by_name(details.pallet_name)
.ok_or_else(|| MetadataError::PalletNameNotFound(details.pallet_name.to_owned()))?
.pallet_by_name_err(details.pallet_name)?
.call_hash(details.call_name)
.ok_or_else(|| MetadataError::CallNameNotFound(details.call_name.to_owned()))?;

Expand Down
4 changes: 1 addition & 3 deletions subxt/src/tx/tx_payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,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> {
let pallet = metadata
.pallet_by_name(&self.pallet_name)
.ok_or_else(|| MetadataError::PalletNameNotFound((*self.pallet_name).to_owned()))?;
let pallet = metadata.pallet_by_name_err(&self.pallet_name)?;
let call = pallet
.call_variant_by_name(&self.call_name)
.ok_or_else(|| MetadataError::CallNameNotFound((*self.call_name).to_owned()))?;
Expand Down