From a59da7b4f25bdb2d27575feb0f7c661a706e6fef Mon Sep 17 00:00:00 2001 From: gui Date: Wed, 8 Jan 2025 20:49:05 +0900 Subject: [PATCH] refactor and doc --- .../src/generic/unchecked_extrinsic.rs | 37 +++++++++---------- .../runtime/src/traits/vers_tx_ext/at_vers.rs | 9 ++--- .../runtime/src/traits/vers_tx_ext/mod.rs | 2 +- .../runtime/src/traits/vers_tx_ext/multi.rs | 6 +++ 4 files changed, 29 insertions(+), 25 deletions(-) diff --git a/substrate/primitives/runtime/src/generic/unchecked_extrinsic.rs b/substrate/primitives/runtime/src/generic/unchecked_extrinsic.rs index f9d6c993ea18..dc93129f737d 100644 --- a/substrate/primitives/runtime/src/generic/unchecked_extrinsic.rs +++ b/substrate/primitives/runtime/src/generic/unchecked_extrinsic.rs @@ -156,8 +156,10 @@ where .saturating_add(address.size_hint()) .saturating_add(signature.size_hint()) .saturating_add(ext.size_hint()), - Preamble::General(ext) => - EXTRINSIC_FORMAT_VERSION.size_hint().saturating_add(ext.size_hint()), + Preamble::General(ext) => EXTRINSIC_FORMAT_VERSION + .size_hint() + .saturating_add(0u8.size_hint()) // version + .saturating_add(ext.size_hint()), } } @@ -388,8 +390,6 @@ impl } /// New instance of an new-school unsigned transaction. - /// - /// This function is only available for `UncheckedExtrinsic` without multi version extension. pub fn new_transaction(function: Call, tx_ext: ExtensionV0) -> Self { Self { preamble: Preamble::General(ExtensionVariant::V0(tx_ext)), function } } @@ -561,14 +561,14 @@ where } #[cfg(feature = "serde")] -impl< - Address: Encode, - Signature: Encode, - Call: Encode, - ExtensionV0: Encode, - ExtensionOtherVersions: Encode + VersTxExtLineVersion, - > serde::Serialize +impl serde::Serialize for UncheckedExtrinsic +where + Address: Encode, + Signature: Encode, + Call: Encode, + ExtensionV0: Encode, + ExtensionOtherVersions: Encode + VersTxExtLineVersion, { fn serialize(&self, seq: S) -> Result where @@ -579,15 +579,14 @@ impl< } #[cfg(feature = "serde")] -impl< - 'a, - Address: Decode, - Signature: Decode, - Call: Decode, - ExtensionV0: Decode, - ExtensionOtherVersions: DecodeWithVersion, - > serde::Deserialize<'a> +impl<'a, Address, Signature, Call, ExtensionV0, ExtensionOtherVersions> serde::Deserialize<'a> for UncheckedExtrinsic +where + Address: Decode, + Signature: Decode, + Call: Decode, + ExtensionV0: Decode, + ExtensionOtherVersions: DecodeWithVersion, { fn deserialize(de: D) -> Result where diff --git a/substrate/primitives/runtime/src/traits/vers_tx_ext/at_vers.rs b/substrate/primitives/runtime/src/traits/vers_tx_ext/at_vers.rs index 51031e5a0025..5a404a2a9c2d 100644 --- a/substrate/primitives/runtime/src/traits/vers_tx_ext/at_vers.rs +++ b/substrate/primitives/runtime/src/traits/vers_tx_ext/at_vers.rs @@ -65,11 +65,10 @@ impl VersTxExtLineVersion for TxExtLineAtVers + Encode, - Extension: TransactionExtension, - > VersTxExtLine for TxExtLineAtVers +impl VersTxExtLine for TxExtLineAtVers +where + Call: Dispatchable + Encode, + Extension: TransactionExtension, { fn build_metadata(builder: &mut VersTxExtLineMetadataBuilder) { builder.push_versioned_extension(VERSION, Extension::metadata()); diff --git a/substrate/primitives/runtime/src/traits/vers_tx_ext/mod.rs b/substrate/primitives/runtime/src/traits/vers_tx_ext/mod.rs index 800acf454e17..275dfa1446cc 100644 --- a/substrate/primitives/runtime/src/traits/vers_tx_ext/mod.rs +++ b/substrate/primitives/runtime/src/traits/vers_tx_ext/mod.rs @@ -109,7 +109,7 @@ pub trait DecodeWithVersion: Sized { /// A type to build the metadata for the versioned transaction extension pipeline. pub struct VersTxExtLineMetadataBuilder { /// The transaction extension pipeline by version and its list of items as vec of index into - /// other field `in_versions`. + /// the other field `in_versions`. pub by_version: BTreeMap>, /// The list of all transaction extension item used. pub in_versions: Vec, diff --git a/substrate/primitives/runtime/src/traits/vers_tx_ext/multi.rs b/substrate/primitives/runtime/src/traits/vers_tx_ext/multi.rs index 102040a3e12a..7e6fedcd972a 100644 --- a/substrate/primitives/runtime/src/traits/vers_tx_ext/multi.rs +++ b/substrate/primitives/runtime/src/traits/vers_tx_ext/multi.rs @@ -35,6 +35,8 @@ use sp_weights::Weight; /// single version. pub trait MultiVersionItem { /// The version of the transaction extension pipeline. + /// + /// `None` means that the item has no version and can't be decoded. const VERSION: Option; } @@ -145,6 +147,10 @@ macro_rules! declare_multi_version_enum { input: &mut CodecInput, ) -> Result { $( + // NOTE: Here we could try all variants without checking for the version, + // but the error would be less informative. + // Otherwise we could change the trait `DecodeWithVersion` to return an enum of + // 3 variants: ok, error and invalid_version. if $variant::VERSION == Some(extension_version) { return Ok(MultiVersion::$variant($variant::decode_with_version(extension_version, input)?)); }