Skip to content

Commit

Permalink
refactor and doc
Browse files Browse the repository at this point in the history
  • Loading branch information
gui1117 committed Jan 8, 2025
1 parent d1bdbd9 commit a59da7b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 25 deletions.
37 changes: 18 additions & 19 deletions substrate/primitives/runtime/src/generic/unchecked_extrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
}
}

Expand Down Expand Up @@ -388,8 +390,6 @@ impl<Address, Call, Signature, ExtensionV0, ExtensionOtherVersions>
}

/// 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 }
}
Expand Down Expand Up @@ -561,14 +561,14 @@ where
}

#[cfg(feature = "serde")]
impl<
Address: Encode,
Signature: Encode,
Call: Encode,
ExtensionV0: Encode,
ExtensionOtherVersions: Encode + VersTxExtLineVersion,
> serde::Serialize
impl<Address, Signature, Call, ExtensionV0, ExtensionOtherVersions> serde::Serialize
for UncheckedExtrinsic<Address, Call, Signature, ExtensionV0, ExtensionOtherVersions>
where
Address: Encode,
Signature: Encode,
Call: Encode,
ExtensionV0: Encode,
ExtensionOtherVersions: Encode + VersTxExtLineVersion,
{
fn serialize<S>(&self, seq: S) -> Result<S::Ok, S::Error>
where
Expand All @@ -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<Address, Call, Signature, ExtensionV0, ExtensionOtherVersions>
where
Address: Decode,
Signature: Decode,
Call: Decode,
ExtensionV0: Decode,
ExtensionOtherVersions: DecodeWithVersion,
{
fn deserialize<D>(de: D) -> Result<Self, D::Error>
where
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,10 @@ impl<const VERSION: u8, Extension> VersTxExtLineVersion for TxExtLineAtVers<VERS
}
}

impl<
const VERSION: u8,
Call: Dispatchable<RuntimeOrigin: AsTransactionAuthorizedOrigin> + Encode,
Extension: TransactionExtension<Call>,
> VersTxExtLine<Call> for TxExtLineAtVers<VERSION, Extension>
impl<const VERSION: u8, Call, Extension> VersTxExtLine<Call> for TxExtLineAtVers<VERSION, Extension>
where
Call: Dispatchable<RuntimeOrigin: AsTransactionAuthorizedOrigin> + Encode,
Extension: TransactionExtension<Call>,
{
fn build_metadata(builder: &mut VersTxExtLineMetadataBuilder) {
builder.push_versioned_extension(VERSION, Extension::metadata());
Expand Down
2 changes: 1 addition & 1 deletion substrate/primitives/runtime/src/traits/vers_tx_ext/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8, Vec<u32>>,
/// The list of all transaction extension item used.
pub in_versions: Vec<TransactionExtensionMetadata>,
Expand Down
6 changes: 6 additions & 0 deletions substrate/primitives/runtime/src/traits/vers_tx_ext/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>;
}

Expand Down Expand Up @@ -145,6 +147,10 @@ macro_rules! declare_multi_version_enum {
input: &mut CodecInput,
) -> Result<Self, codec::Error> {
$(
// 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)?));
}
Expand Down

0 comments on commit a59da7b

Please sign in to comment.