Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions primitives/runtime/src/generic/unchecked_extrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use crate::{
},
generic::CheckedExtrinsic,
transaction_validity::{TransactionValidityError, InvalidTransaction},
OpaqueExtrinsic,
};

const TRANSACTION_VERSION: u8 = 4;
Expand Down Expand Up @@ -316,6 +317,20 @@ where
}
}

impl<Address, Call, Signature, Extra> From<UncheckedExtrinsic<Address, Call, Signature, Extra>>
for OpaqueExtrinsic
where
Address: Encode,
Signature: Encode,
Call: Encode,
Extra: SignedExtension,
{
fn from(extrinsic: UncheckedExtrinsic<Address, Call, Signature, Extra>) -> Self {
OpaqueExtrinsic::decode(&mut extrinsic.encode().as_slice())
.expect("decoding from something we have encoded cannot fail; qed")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The proof is not really correct. The reason it works is not because we just encoded it, but rather because both OpaqueExtrinsic and UncheckedExtrinsic have encoding that is compatible with raw Vec<u8> encoding.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -424,4 +439,13 @@ mod tests {
let as_vec: Vec<u8> = Decode::decode(&mut encoded.as_slice()).unwrap();
assert_eq!(as_vec.encode(), encoded);
}

#[test]
fn conversion_to_opaque() {
let ux = Ex::new_unsigned(vec![0u8; 0]);
let encoded = ux.encode();
let opaque: OpaqueExtrinsic = ux.into();
let opaque_encoded = opaque.encode();
assert_eq!(opaque_encoded, encoded);
}
}