Fixed extrinsic encoding (#794)

* Fixed extrinsic encoding

* Reserve heuristic

* Fixed no-std build
This commit is contained in:
Arkadiy Paronyan
2018-09-25 18:01:10 +02:00
committed by Gav Wood
parent 68e3e3ee11
commit a613c62dc1
4 changed files with 74 additions and 40 deletions
@@ -35,3 +35,28 @@ pub use self::checked_extrinsic::CheckedExtrinsic;
pub use self::header::Header;
pub use self::block::{Block, SignedBlock, BlockId};
pub use self::digest::{Digest, DigestItem, DigestItemRef};
use codec::Encode;
use rstd::prelude::*;
fn encode_with_vec_prefix<T: Encode, F: Fn(&mut Vec<u8>)>(encoder: F) -> Vec<u8> {
let size = ::rstd::mem::size_of::<T>();
let reserve = match size {
0...0b00111111 => 1,
0...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 comptible 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
}