tp_runtime/generic/
mod.rs

1// This file is part of Tetcore.
2
3// Copyright (C) 2017-2021 Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18// tag::description[]
19//! Generic implementations of Extrinsic/Header/Block.
20// end::description[]
21
22mod unchecked_extrinsic;
23mod era;
24mod checked_extrinsic;
25mod header;
26mod block;
27mod digest;
28#[cfg(test)]
29mod tests;
30
31pub use self::unchecked_extrinsic::{UncheckedExtrinsic, SignedPayload};
32pub use self::era::{Era, Phase};
33pub use self::checked_extrinsic::CheckedExtrinsic;
34pub use self::header::Header;
35pub use self::block::{Block, SignedBlock, BlockId};
36pub use self::digest::{
37	Digest, DigestItem, DigestItemRef, OpaqueDigestItemId, ChangesTrieSignal,
38};
39
40use crate::codec::Encode;
41use tetcore_std::prelude::*;
42
43fn encode_with_vec_prefix<T: Encode, F: Fn(&mut Vec<u8>)>(encoder: F) -> Vec<u8> {
44	let size = ::tetcore_std::mem::size_of::<T>();
45	let reserve = match size {
46		0..=0b00111111 => 1,
47		0..=0b00111111_11111111 => 2,
48		_ => 4,
49	};
50	let mut v = Vec::with_capacity(reserve + size);
51	v.resize(reserve, 0);
52	encoder(&mut v);
53
54	// need to prefix with the total length to ensure it's binary compatible with
55	// Vec<u8>.
56	let mut length: Vec<()> = Vec::new();
57	length.resize(v.len() - reserve, ());
58	length.using_encoded(|s| {
59		v.splice(0..reserve, s.iter().cloned());
60	});
61
62	v
63}