From 828955768d47eaab93cee9fd0d4f66c1aacb1dea Mon Sep 17 00:00:00 2001 From: James Wilson Date: Thu, 25 May 2023 17:06:46 +0100 Subject: [PATCH 1/3] Reduce some repetition when obtaining metadata pallets/runtime_traits --- subxt/src/blocks/extrinsic_types.rs | 5 +---- subxt/src/constants/constants_client.rs | 9 +++------ subxt/src/error/dispatch_error.rs | 6 +----- subxt/src/events/events_type.rs | 4 +--- subxt/src/metadata/metadata_type.rs | 25 ++++++++++++++++++++++++ subxt/src/runtime_api/runtime_payload.rs | 6 ++---- subxt/src/runtime_api/runtime_types.rs | 6 +----- subxt/src/storage/storage_address.rs | 4 +--- subxt/src/storage/storage_client.rs | 6 ++---- subxt/src/storage/storage_type.rs | 4 +--- subxt/src/tx/tx_client.rs | 3 +-- subxt/src/tx/tx_payload.rs | 4 +--- 12 files changed, 40 insertions(+), 42 deletions(-) diff --git a/subxt/src/blocks/extrinsic_types.rs b/subxt/src/blocks/extrinsic_types.rs index 6dd9e3f2b7..953272b4fb 100644 --- a/subxt/src/blocks/extrinsic_types.rs +++ b/subxt/src/blocks/extrinsic_types.rs @@ -366,10 +366,7 @@ where /// Fetch the metadata for this extrinsic. pub fn extrinsic_metadata(&self) -> Result { - 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()))?; diff --git a/subxt/src/constants/constants_client.rs b/subxt/src/constants/constants_client.rs index 6db520c4fa..de688bba6b 100644 --- a/subxt/src/constants/constants_client.rs +++ b/subxt/src/constants/constants_client.rs @@ -39,8 +39,7 @@ impl> ConstantsClient { 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()) @@ -65,10 +64,8 @@ impl> ConstantsClient { 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()) diff --git a/subxt/src/error/dispatch_error.rs b/subxt/src/error/dispatch_error.rs index 64a4378b39..eb5856620c 100644 --- a/subxt/src/error/dispatch_error.rs +++ b/subxt/src/error/dispatch_error.rs @@ -154,11 +154,7 @@ impl std::fmt::Display for ModuleError { impl ModuleError { /// Return more details about this error. pub fn details(&self) -> Result { - 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]))?; diff --git a/subxt/src/events/events_type.rs b/subxt/src/events/events_type.rs index 177f5da440..af196f9984 100644 --- a/subxt/src/events/events_type.rs +++ b/subxt/src/events/events_type.rs @@ -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))?; diff --git a/subxt/src/metadata/metadata_type.rs b/subxt/src/metadata/metadata_type.rs index 30c73d1f72..79fc1ad08d 100644 --- a/subxt/src/metadata/metadata_type.rs +++ b/subxt/src/metadata/metadata_type.rs @@ -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. @@ -23,6 +24,30 @@ impl Metadata { inner: Arc::new(md), } } + + pub(crate) fn pallet_by_name_err( + &self, + name: &str, + ) -> Result { + self.pallet_by_name(name) + .ok_or_else(|| MetadataError::PalletNameNotFound(name.to_owned())) + } + + pub(crate) fn pallet_by_index_err( + &self, + index: u8, + ) -> Result { + self.pallet_by_index(index) + .ok_or_else(|| MetadataError::PalletIndexNotFound(index)) + } + + pub(crate) fn runtime_api_trait_by_name_err( + &self, + name: &str, + ) -> Result { + self.runtime_api_trait_by_name(name) + .ok_or_else(|| MetadataError::RuntimeTraitNotFound(name.to_owned())) + } } impl From for Metadata { diff --git a/subxt/src/runtime_api/runtime_payload.rs b/subxt/src/runtime_api/runtime_payload.rs index e129a75d24..fe21df1fec 100644 --- a/subxt/src/runtime_api/runtime_payload.rs +++ b/subxt/src/runtime_api/runtime_payload.rs @@ -88,10 +88,8 @@ impl RuntimeApiPayload } fn encode_args_to(&self, metadata: &Metadata, out: &mut Vec) -> 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()))?; diff --git a/subxt/src/runtime_api/runtime_types.rs b/subxt/src/runtime_api/runtime_types.rs index 9594c0859c..3df2d2957d 100644 --- a/subxt/src/runtime_api/runtime_types.rs +++ b/subxt/src/runtime_api/runtime_types.rs @@ -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(|| { diff --git a/subxt/src/storage/storage_address.rs b/subxt/src/storage/storage_address.rs index 9f97b0bf82..8ba61d9f09 100644 --- a/subxt/src/storage/storage_address.rs +++ b/subxt/src/storage/storage_address.rs @@ -138,9 +138,7 @@ where } fn append_entry_bytes(&self, metadata: &Metadata, bytes: &mut Vec) -> 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()))?; diff --git a/subxt/src/storage/storage_client.rs b/subxt/src/storage/storage_client.rs index a85bbeb5d2..2055c80ace 100644 --- a/subxt/src/storage/storage_client.rs +++ b/subxt/src/storage/storage_client.rs @@ -9,7 +9,7 @@ use super::{ use crate::{ client::{OfflineClientT, OnlineClientT}, - error::{Error, MetadataError}, + error::Error, Config, }; use derivative::Derivative; @@ -44,9 +44,7 @@ where /// the pallet or storage entry in question do not exist at all). pub fn validate(&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) } diff --git a/subxt/src/storage/storage_type.rs b/subxt/src/storage/storage_type.rs index 30d61edd01..7beb923873 100644 --- a/subxt/src/storage/storage_type.rs +++ b/subxt/src/storage/storage_type.rs @@ -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()))?; diff --git a/subxt/src/tx/tx_client.rs b/subxt/src/tx/tx_client.rs index ac9a59d495..a47f891142 100644 --- a/subxt/src/tx/tx_client.rs +++ b/subxt/src/tx/tx_client.rs @@ -50,8 +50,7 @@ impl> TxClient { 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()))?; diff --git a/subxt/src/tx/tx_payload.rs b/subxt/src/tx/tx_payload.rs index a92b7f56f4..7a2ceaf189 100644 --- a/subxt/src/tx/tx_payload.rs +++ b/subxt/src/tx/tx_payload.rs @@ -141,9 +141,7 @@ impl Payload> { impl TxPayload for Payload { fn encode_call_data_to(&self, metadata: &Metadata, out: &mut Vec) -> 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()))?; From c2c61bfbc75a4ae94aca81a50d60578d832366c1 Mon Sep 17 00:00:00 2001 From: James Wilson Date: Thu, 25 May 2023 17:09:34 +0100 Subject: [PATCH 2/3] make them pub --- subxt/src/metadata/metadata_type.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/subxt/src/metadata/metadata_type.rs b/subxt/src/metadata/metadata_type.rs index 79fc1ad08d..8e3cbe7fdd 100644 --- a/subxt/src/metadata/metadata_type.rs +++ b/subxt/src/metadata/metadata_type.rs @@ -25,7 +25,8 @@ impl Metadata { } } - pub(crate) fn pallet_by_name_err( + /// 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 { @@ -33,7 +34,8 @@ impl Metadata { .ok_or_else(|| MetadataError::PalletNameNotFound(name.to_owned())) } - pub(crate) fn pallet_by_index_err( + /// 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 { @@ -41,7 +43,8 @@ impl Metadata { .ok_or_else(|| MetadataError::PalletIndexNotFound(index)) } - pub(crate) fn runtime_api_trait_by_name_err( + /// 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 { From 9f6da768432c6503a4b72d1fdfbbf9180071b0d8 Mon Sep 17 00:00:00 2001 From: James Wilson Date: Thu, 25 May 2023 17:14:43 +0100 Subject: [PATCH 3/3] fix docs and clippy --- subxt/src/metadata/metadata_type.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/subxt/src/metadata/metadata_type.rs b/subxt/src/metadata/metadata_type.rs index 8e3cbe7fdd..30a3cf62e5 100644 --- a/subxt/src/metadata/metadata_type.rs +++ b/subxt/src/metadata/metadata_type.rs @@ -25,7 +25,7 @@ impl Metadata { } } - /// Identical to [`Metadata::pallet_by_name`], but returns an error if the pallet is not found. + /// 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, @@ -34,16 +34,16 @@ impl Metadata { .ok_or_else(|| MetadataError::PalletNameNotFound(name.to_owned())) } - /// Identical to [`Metadata::pallet_by_index`], but returns an error if the pallet is not found. + /// 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 { self.pallet_by_index(index) - .ok_or_else(|| MetadataError::PalletIndexNotFound(index)) + .ok_or(MetadataError::PalletIndexNotFound(index)) } - /// Identical to [`Metadata::runtime_api_trait_by_name`], but returns an error if the trait is not found. + /// 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,