Make UncheckedExtrinsic encode more readable (#9531)

Actually this will cost us another allocation, but before this wasn't
really safe. Assuming that we only need `size_of` bytes for the encoding
of the tx could have ended with an invalid encoding.
This commit is contained in:
Bastian Köcher
2021-08-10 15:33:21 +02:00
committed by GitHub
parent 394c2817d5
commit 17b774a21d
2 changed files with 23 additions and 38 deletions
@@ -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
}