diff --git a/substrate/primitives/runtime/src/generic/mod.rs b/substrate/primitives/runtime/src/generic/mod.rs index deaecd65e4..71127e88ec 100644 --- a/substrate/primitives/runtime/src/generic/mod.rs +++ b/substrate/primitives/runtime/src/generic/mod.rs @@ -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)>(encoder: F) -> Vec { - let size = ::sp_std::mem::size_of::(); - 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. - let mut length: Vec<()> = Vec::new(); - length.resize(v.len() - reserve, ()); - length.using_encoded(|s| { - v.splice(0..reserve, s.iter().cloned()); - }); - - v -} diff --git a/substrate/primitives/runtime/src/generic/unchecked_extrinsic.rs b/substrate/primitives/runtime/src/generic/unchecked_extrinsic.rs index 68ab8447cf..9f50ab35b3 100644 --- a/substrate/primitives/runtime/src/generic/unchecked_extrinsic.rs +++ b/substrate/primitives/runtime/src/generic/unchecked_extrinsic.rs @@ -228,19 +228,29 @@ where Extra: SignedExtension, { fn encode(&self) -> Vec { - super::encode_with_vec_prefix::(|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::()); + + // 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::(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 } }