Skip to content

Commit

Permalink
config: Add SkipCheckIfFeeless signed extension (#1264)
Browse files Browse the repository at this point in the history
* config: Add `SkipCheckIfFeeless` signed extension

Signed-off-by: Alexandru Vasile <[email protected]>

* config: Add extra extension to the default params

Signed-off-by: Alexandru Vasile <[email protected]>

* examples: Adjust signed extension example

Signed-off-by: Alexandru Vasile <[email protected]>

* config: Extend SkipCheckIfFeeless with inner signed extension

Signed-off-by: Alexandru Vasile <[email protected]>

* config: Configure SkipCheck with inner signed extension params

Signed-off-by: Alexandru Vasile <[email protected]>

* config: Implement Deafult for SkipCheckIfFeelessParams with Option

Signed-off-by: Alexandru Vasile <[email protected]>

* examples: Fix example with proper extension

Signed-off-by: Alexandru Vasile <[email protected]>

* config: Extend <T as Config>::AssetId with EncodeAsType and Clone

Signed-off-by: Alexandru Vasile <[email protected]>

* config: Add SkipCheck with AssetTx

Signed-off-by: Alexandru Vasile <[email protected]>

* config: Encode as type from metadata the inner signed extensions

Signed-off-by: Alexandru Vasile <[email protected]>

* Adjust examples

Signed-off-by: Alexandru Vasile <[email protected]>

* blocks: Use `SkipCheckIfFeeless` for decoding the tip of extensions

Signed-off-by: Alexandru Vasile <[email protected]>

* config: Decode `SkipCheckIfFeeless` with `Self`

Signed-off-by: Alexandru Vasile <[email protected]>

* tests: Adjust testing

Signed-off-by: Alexandru Vasile <[email protected]>

* config: Descriptive errors for building `SkipCheckIfFeeless`

Signed-off-by: Alexandru Vasile <[email protected]>

* config: Add docs for extra error types

Signed-off-by: Alexandru Vasile <[email protected]>

* subxt: Add extra derives to signed extensions

Signed-off-by: Alexandru Vasile <[email protected]>

* config: Use `Default::default` to simplify type init

Signed-off-by: Alexandru Vasile <[email protected]>

---------

Signed-off-by: Alexandru Vasile <[email protected]>
  • Loading branch information
lexnv authored Nov 16, 2023
1 parent eeacf4b commit 25e107c
Show file tree
Hide file tree
Showing 9 changed files with 301 additions and 19 deletions.
14 changes: 13 additions & 1 deletion subxt/examples/setup_config_custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,19 @@ use subxt::client::OfflineClientT;
use subxt::config::{Config, ExtrinsicParams, ExtrinsicParamsEncoder};
use subxt_signer::sr25519::dev;

#[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata_full.scale")]
#[subxt::subxt(
runtime_metadata_path = "../artifacts/polkadot_metadata_full.scale",
derive_for_type(path = "xcm::v2::multilocation::MultiLocation", derive = "Clone"),
derive_for_type(path = "xcm::v2::multilocation::Junctions", derive = "Clone"),
derive_for_type(path = "xcm::v2::junction::Junction", derive = "Clone"),
derive_for_type(path = "xcm::v2::NetworkId", derive = "Clone"),
derive_for_type(path = "xcm::v2::BodyId", derive = "Clone"),
derive_for_type(path = "xcm::v2::BodyPart", derive = "Clone"),
derive_for_type(
path = "bounded_collections::weak_bounded_vec::WeakBoundedVec",
derive = "Clone"
)
)]
pub mod runtime {}
use runtime::runtime_types::xcm::v2::multilocation::MultiLocation;

Expand Down
10 changes: 8 additions & 2 deletions subxt/examples/setup_config_signed_extension.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use codec::Encode;
use scale_encode::EncodeAsType;
use subxt::client::OfflineClientT;
use subxt::config::signed_extensions;
use subxt::config::{
Expand All @@ -11,6 +12,7 @@ pub mod runtime {}

// We don't need to construct this at runtime,
// so an empty enum is appropriate:
#[derive(EncodeAsType)]
pub enum CustomConfig {}

impl Config for CustomConfig {
Expand All @@ -32,6 +34,10 @@ impl Config for CustomConfig {
signed_extensions::CheckMortality<Self>,
signed_extensions::ChargeAssetTxPayment<Self>,
signed_extensions::ChargeTransactionPayment,
signed_extensions::SkipCheckIfFeeless<
Self,
signed_extensions::ChargeAssetTxPayment<Self>,
>,
// And add a new one of our own:
CustomSignedExtension,
),
Expand Down Expand Up @@ -81,8 +87,8 @@ impl ExtrinsicParamsEncoder for CustomSignedExtension {
pub fn custom(
params: DefaultExtrinsicParamsBuilder<CustomConfig>,
) -> <<CustomConfig as Config>::ExtrinsicParams as ExtrinsicParams<CustomConfig>>::OtherParams {
let (a, b, c, d, e, f, g) = params.build();
(a, b, c, d, e, f, g, ())
let (a, b, c, d, e, f, g, h) = params.build();
(a, b, c, d, e, f, g, h, ())
}

#[tokio::main]
Expand Down
10 changes: 8 additions & 2 deletions subxt/src/blocks/extrinsic_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
};

use crate::config::signed_extensions::{
ChargeAssetTxPayment, ChargeTransactionPayment, CheckNonce,
ChargeAssetTxPayment, ChargeTransactionPayment, CheckNonce, SkipCheckIfFeeless,
};
use crate::config::SignedExtension;
use crate::dynamic::DecodedValue;
Expand Down Expand Up @@ -685,7 +685,7 @@ impl<'a, T: Config> ExtrinsicSignedExtensions<'a, T> {
///
/// Returns `None` if `tip` was not found or decoding failed.
pub fn tip(&self) -> Option<u128> {
// Note: the overhead of iterating twice should be negligible.
// Note: the overhead of iterating multiple time should be negligible.
self.find::<ChargeTransactionPayment>()
.ok()
.flatten()
Expand All @@ -696,6 +696,12 @@ impl<'a, T: Config> ExtrinsicSignedExtensions<'a, T> {
.flatten()
.map(|e| e.tip())
})
.or_else(|| {
self.find::<SkipCheckIfFeeless<T, ChargeAssetTxPayment<T>>>()
.ok()
.flatten()
.map(|skip_check| skip_check.inner_signed_extension().tip())
})
}

/// The nonce of the account that submitted the extrinsic, extracted from the CheckNonce signed extension.
Expand Down
5 changes: 5 additions & 0 deletions subxt/src/config/default_extrinsic_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub type DefaultExtrinsicParams<T> = signed_extensions::AnyOf<
signed_extensions::CheckMortality<T>,
signed_extensions::ChargeAssetTxPayment<T>,
signed_extensions::ChargeTransactionPayment,
signed_extensions::SkipCheckIfFeeless<T, signed_extensions::ChargeAssetTxPayment<T>>,
),
>;

Expand Down Expand Up @@ -131,6 +132,9 @@ impl<T: Config> DefaultExtrinsicParamsBuilder<T> {
let charge_transaction_params =
signed_extensions::ChargeTransactionPaymentParams::tip(self.tip);

let skip_check_params =
signed_extensions::SkipCheckIfFeelessParams::from(charge_asset_tx_params.clone());

(
(),
(),
Expand All @@ -139,6 +143,7 @@ impl<T: Config> DefaultExtrinsicParamsBuilder<T> {
check_mortality_params,
charge_asset_tx_params,
charge_transaction_params,
skip_check_params,
)
}
}
14 changes: 13 additions & 1 deletion subxt/src/config/extrinsic_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,19 @@ pub enum ExtrinsicParamsError {
/// A signed extension was encountered that we don't know about.
#[error("Error constructing extrinsic parameters: Unknown signed extension '{0}'")]
UnknownSignedExtension(String),
/// Some custom error.
/// Cannot find the type id of a signed extension in the metadata.
#[error("Cannot find extension's '{0}' type id '{1} in the metadata")]
MissingTypeId(String, u32),
/// User provided a different signed extension than the one expected.
#[error("Provided a different signed extension for '{0}', the metadata expect '{1}'")]
ExpectedAnotherExtension(String, String),
/// The inner type of a signed extension is not present in the metadata.
#[error("The inner type of the signed extension '{0}' is not present in the metadata")]
MissingInnerSignedExtension(String),
/// The inner type of the signed extension is not named.
#[error("The signed extension's '{0}' type id '{1}' does not have a name in the metadata")]
ExpectedNamedTypeId(String, u32),
/// Some custom error.s
#[error("Error constructing extrinsic parameters: {0}")]
Custom(CustomError),
}
Expand Down
3 changes: 2 additions & 1 deletion subxt/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub mod substrate;
use codec::{Decode, Encode};
use core::fmt::Debug;
use scale_decode::DecodeAsType;
use scale_encode::EncodeAsType;
use serde::{de::DeserializeOwned, Serialize};

pub use default_extrinsic_params::{DefaultExtrinsicParams, DefaultExtrinsicParamsBuilder};
Expand Down Expand Up @@ -54,7 +55,7 @@ pub trait Config: Sized + Send + Sync + 'static {
type ExtrinsicParams: ExtrinsicParams<Self>;

/// This is used to identify an asset in the `ChargeAssetTxPayment` signed extension.
type AssetId: Debug + Encode + DecodeAsType;
type AssetId: Debug + Clone + Encode + DecodeAsType + EncodeAsType;
}

/// given some [`Config`], this return the other params needed for its `ExtrinsicParams`.
Expand Down
Loading

0 comments on commit 25e107c

Please sign in to comment.