Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 0 additions & 25 deletions primitives/runtime/src/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,3 @@ pub use self::{
header::Header,
unchecked_extrinsic::{SignedPayload, UncheckedExtrinsic},
};

use crate::codec::Encode;
use sp_std::prelude::*;

fn encode_with_vec_prefix<T: Encode, F: Fn(&mut Vec<u8>)>(encoder: F) -> Vec<u8> {
let size = ::sp_std::mem::size_of::<T>();
let reserve = match size {
0..=0b00111111 => 1,
0b01000000..=0b00111111_11111111 => 2,
_ => 4,
};
let mut v = Vec::with_capacity(reserve + size);
v.resize(reserve, 0);
encoder(&mut v);

// need to prefix with the total length to ensure it's binary compatible with
// Vec<u8>.
let mut length: Vec<()> = Vec::new();
length.resize(v.len() - reserve, ());
length.using_encoded(|s| {
v.splice(0..reserve, s.iter().cloned());
});

v
}
36 changes: 23 additions & 13 deletions primitives/runtime/src/generic/unchecked_extrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,19 +228,29 @@ where
Extra: SignedExtension,
{
fn encode(&self) -> Vec<u8> {
super::encode_with_vec_prefix::<Self, _>(|v| {
// 1 byte version id.
match self.signature.as_ref() {
Some(s) => {
v.push(EXTRINSIC_VERSION | 0b1000_0000);
s.encode_to(v);
},
None => {
v.push(EXTRINSIC_VERSION & 0b0111_1111);
},
}
self.function.encode_to(v);
})
let mut tmp = Vec::with_capacity(sp_std::mem::size_of::<Self>());

// 1 byte version id.
match self.signature.as_ref() {
Some(s) => {
tmp.push(EXTRINSIC_VERSION | 0b1000_0000);
s.encode_to(&mut tmp);
},
None => {
tmp.push(EXTRINSIC_VERSION & 0b0111_1111);
},
}
self.function.encode_to(&mut tmp);

let compact_len = codec::Compact::<u32>(tmp.len() as u32);

// Allocate the output buffer with the correct length
let mut output = Vec::with_capacity(compact_len.size_hint() + tmp.len());

compact_len.encode_to(&mut output);
output.extend(tmp);

output
}
}

Expand Down