mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-28 13:17:56 +00:00
Fixed extrinsic encoding (#794)
* Fixed extrinsic encoding * Reserve heuristic * Fixed no-std build
This commit is contained in:
committed by
Gav Wood
parent
68e3e3ee11
commit
a613c62dc1
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user