mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-11 22:21:07 +00:00
Static Decoding of Signed Extensions: Simple Approach (#1235)
* skeleton commit * signed extension decoding * fix some minor things * make api more similar to Extrinsics * defer decoding of signed extensions * fix byte slices * add test for nonce signed extension * adjust test and extend for tip * clippy * support both ChargeTransactionPayment and ChargeAssetTxPayment * address PR comments * Extend lifetimes, expose pub structs, remove as_type * add signed extensions to block subscribing example * add Decoded type * fix merging bug and tests * add decoded type in CustomSignedExtension * fix minor issues, extend test * cargo fmt differences * remove the `decoded` function * new as_signed_extra fn, do not expose as_type anymore * fix Result-Option order, simplify obtaining Nonce * tx: Remove `wait_for_in_block` helper method (#1237) Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * Update smoldot to 0.12 (#1212) * Update lightclient Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * testing: Fix typo Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * testing: Update cargo.toml Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * lightclient: Add tracing logs to improve debugging Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * lightclient: Add socket buffers module for `PlatformRef` Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * lightclient: Update `SubxtPlatform` Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * cargo: Add lightclient dependencies Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * Update cargo.lock of wasm tests Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * lightclient: Add constant for with-buffer module Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * lightclient: Replace rand crate with getrandom Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * example: Update cargo lock file Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * examples: Update deps Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> --------- Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> Co-authored-by: Tadeo Hepperle <62739623+tadeohepperle@users.noreply.github.com> * ChargeAssetTxPayment: support providing u32 or MultiLocation in default impl (#1227) * Asset Id in Config trait * add example configuring the config * fmt * fix Default trait bound * merge examples, fix default again * adjust config in examples * Update subxt/src/config/mod.rs Co-authored-by: James Wilson <james@jsdw.me> --------- Co-authored-by: James Wilson <james@jsdw.me> * generic AssetId * fix generics * fmt --------- Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> Co-authored-by: James Wilson <james@jsdw.me> Co-authored-by: Alexandru Vasile <60601340+lexnv@users.noreply.github.com>
This commit is contained in:
@@ -12,9 +12,13 @@ use crate::{
|
||||
Metadata,
|
||||
};
|
||||
|
||||
use crate::config::signed_extensions::{
|
||||
ChargeAssetTxPayment, ChargeTransactionPayment, CheckNonce,
|
||||
};
|
||||
use crate::config::SignedExtension;
|
||||
use crate::dynamic::DecodedValue;
|
||||
use crate::utils::strip_compact_prefix;
|
||||
use codec::{Compact, Decode};
|
||||
use codec::Decode;
|
||||
use derivative::Derivative;
|
||||
use scale_decode::{DecodeAsFields, DecodeAsType};
|
||||
|
||||
@@ -366,12 +370,13 @@ where
|
||||
}
|
||||
|
||||
/// Returns `None` if the extrinsic is not signed.
|
||||
pub fn signed_extensions(&self) -> Option<ExtrinsicSignedExtensions<'_>> {
|
||||
pub fn signed_extensions(&self) -> Option<ExtrinsicSignedExtensions<'_, T>> {
|
||||
let signed = self.signed_details.as_ref()?;
|
||||
let extra_bytes = &self.bytes[signed.signature_end_idx..signed.extra_end_idx];
|
||||
Some(ExtrinsicSignedExtensions {
|
||||
bytes: extra_bytes,
|
||||
metadata: &self.metadata,
|
||||
_marker: std::marker::PhantomData,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -605,21 +610,22 @@ impl<T: Config> ExtrinsicEvents<T> {
|
||||
|
||||
/// The signed extensions of an extrinsic.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ExtrinsicSignedExtensions<'a> {
|
||||
pub struct ExtrinsicSignedExtensions<'a, T: Config> {
|
||||
bytes: &'a [u8],
|
||||
metadata: &'a Metadata,
|
||||
_marker: std::marker::PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<'a> ExtrinsicSignedExtensions<'a> {
|
||||
impl<'a, T: Config> ExtrinsicSignedExtensions<'a, T> {
|
||||
/// Returns an iterator over each of the signed extension details of the extrinsic.
|
||||
/// If the decoding of any signed extension fails, an error item is yielded and the iterator stops.
|
||||
pub fn iter(&self) -> impl Iterator<Item = Result<ExtrinsicSignedExtension<'a>, Error>> {
|
||||
pub fn iter(&self) -> impl Iterator<Item = Result<ExtrinsicSignedExtension<T>, Error>> {
|
||||
let signed_extension_types = self.metadata.extrinsic().signed_extensions();
|
||||
let num_signed_extensions = signed_extension_types.len();
|
||||
let bytes = self.bytes;
|
||||
let metadata = self.metadata;
|
||||
let mut index = 0;
|
||||
let mut byte_start_idx = 0;
|
||||
let metadata = &self.metadata;
|
||||
|
||||
std::iter::from_fn(move || {
|
||||
if index == num_signed_extensions {
|
||||
@@ -649,49 +655,69 @@ impl<'a> ExtrinsicSignedExtensions<'a> {
|
||||
ty_id,
|
||||
identifier: extension.identifier(),
|
||||
metadata,
|
||||
_marker: std::marker::PhantomData,
|
||||
}))
|
||||
})
|
||||
}
|
||||
|
||||
fn find_by_name(&self, name: &str) -> Option<ExtrinsicSignedExtension<'_, T>> {
|
||||
let signed_extension = self
|
||||
.iter()
|
||||
.find_map(|e| e.ok().filter(|e| e.name() == name))?;
|
||||
Some(signed_extension)
|
||||
}
|
||||
|
||||
/// Searches through all signed extensions to find a specific one.
|
||||
/// If the Signed Extension is not found `Ok(None)` is returned.
|
||||
/// If the Signed Extension is found but decoding failed `Err(_)` is returned.
|
||||
pub fn find<S: SignedExtension<T>>(&self) -> Result<Option<S::Decoded>, Error> {
|
||||
self.find_by_name(S::NAME)
|
||||
.map(|s| {
|
||||
s.as_signed_extra::<S>().map(|e| {
|
||||
e.expect("signed extra name is correct, because it was found before; qed.")
|
||||
})
|
||||
})
|
||||
.transpose()
|
||||
}
|
||||
|
||||
/// The tip of an extrinsic, extracted from the ChargeTransactionPayment or ChargeAssetTxPayment
|
||||
/// signed extension, depending on which is present.
|
||||
///
|
||||
/// Returns `None` if `tip` was not found or decoding failed.
|
||||
pub fn tip(&self) -> Option<u128> {
|
||||
let tip = self.iter().find_map(|e| {
|
||||
e.ok().filter(|e| {
|
||||
e.name() == "ChargeTransactionPayment" || e.name() == "ChargeAssetTxPayment"
|
||||
// Note: the overhead of iterating twice should be negligible.
|
||||
self.find::<ChargeTransactionPayment>()
|
||||
.ok()
|
||||
.flatten()
|
||||
.map(|e| e.tip())
|
||||
.or_else(|| {
|
||||
self.find::<ChargeAssetTxPayment<T>>()
|
||||
.ok()
|
||||
.flatten()
|
||||
.map(|e| e.tip())
|
||||
})
|
||||
})?;
|
||||
|
||||
// Note: ChargeAssetTxPayment might have addition information in it (asset_id).
|
||||
// But both should start with a compact encoded u128, so this decoding is fine.
|
||||
let tip = Compact::<u128>::decode(&mut tip.bytes()).ok()?.0;
|
||||
Some(tip)
|
||||
}
|
||||
|
||||
/// The nonce of the account that submitted the extrinsic, extracted from the CheckNonce signed extension.
|
||||
///
|
||||
/// Returns `None` if `nonce` was not found or decoding failed.
|
||||
pub fn nonce(&self) -> Option<u64> {
|
||||
let nonce = self
|
||||
.iter()
|
||||
.find_map(|e| e.ok().filter(|e| e.name() == "CheckNonce"))?;
|
||||
let nonce = Compact::<u64>::decode(&mut nonce.bytes()).ok()?.0;
|
||||
let nonce = self.find::<CheckNonce>().ok()??.0;
|
||||
Some(nonce)
|
||||
}
|
||||
}
|
||||
|
||||
/// A single signed extension
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ExtrinsicSignedExtension<'a> {
|
||||
pub struct ExtrinsicSignedExtension<'a, T: Config> {
|
||||
bytes: &'a [u8],
|
||||
ty_id: u32,
|
||||
identifier: &'a str,
|
||||
metadata: &'a Metadata,
|
||||
_marker: std::marker::PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<'a> ExtrinsicSignedExtension<'a> {
|
||||
impl<'a, T: Config> ExtrinsicSignedExtension<'a, T> {
|
||||
/// The bytes representing this signed extension.
|
||||
pub fn bytes(&self) -> &'a [u8] {
|
||||
self.bytes
|
||||
@@ -709,11 +735,23 @@ impl<'a> ExtrinsicSignedExtension<'a> {
|
||||
|
||||
/// Signed Extension as a [`scale_value::Value`]
|
||||
pub fn value(&self) -> Result<DecodedValue, Error> {
|
||||
let value =
|
||||
DecodedValue::decode_as_type(&mut &self.bytes[..], self.ty_id, self.metadata.types())?;
|
||||
self.as_type()
|
||||
}
|
||||
|
||||
/// Decodes the `extra` bytes of this Signed Extension into a static type.
|
||||
fn as_type<E: DecodeAsType>(&self) -> Result<E, Error> {
|
||||
let value = E::decode_as_type(&mut &self.bytes[..], self.ty_id, self.metadata.types())?;
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
/// Decodes the `extra` bytes of this Signed Extension into its associated `Decoded` type.
|
||||
/// Returns `Ok(None)` if the identitfier of this Signed Extension object does not line up with the `NAME` constant of the provided Signed Extension type.
|
||||
pub fn as_signed_extra<S: SignedExtension<T>>(&self) -> Result<Option<S::Decoded>, Error> {
|
||||
if self.identifier != S::NAME {
|
||||
return Ok(None);
|
||||
}
|
||||
self.as_type::<S::Decoded>().map(Some)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user