tp_runtime/generic/
header.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//! Generic implementation of a block header.
19
20#[cfg(feature = "std")]
21use serde::{Deserialize, Serialize};
22use crate::codec::{Decode, Encode, Codec, Input, Output, HasCompact, EncodeAsRef, Error};
23use crate::traits::{
24	self, Member, AtLeast32BitUnsigned, SimpleBitOps, Hash as HashT,
25	MaybeSerializeDeserialize, MaybeSerialize, MaybeDisplay,
26	MaybeMallocSizeOf,
27};
28use crate::generic::Digest;
29use tet_core::U256;
30use tetcore_std::{
31	convert::TryFrom,
32	fmt::Debug,
33};
34
35/// Abstraction over a block header for a tetcore chain.
36#[derive(PartialEq, Eq, Clone, tet_core::RuntimeDebug)]
37#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
38#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
39#[cfg_attr(feature = "std", serde(deny_unknown_fields))]
40pub struct Header<Number: Copy + Into<U256> + TryFrom<U256>, Hash: HashT> {
41	/// The parent hash.
42	pub parent_hash: Hash::Output,
43	/// The block number.
44	#[cfg_attr(feature = "std", serde(
45		serialize_with = "serialize_number",
46		deserialize_with = "deserialize_number"))]
47	pub number: Number,
48	/// The state trie merkle root
49	pub state_root: Hash::Output,
50	/// The merkle root of the extrinsics.
51	pub extrinsics_root: Hash::Output,
52	/// A chain-specific digest of data useful for light clients or referencing auxiliary data.
53	pub digest: Digest<Hash::Output>,
54}
55
56#[cfg(feature = "std")]
57impl<Number, Hash> tetsy_util_mem::MallocSizeOf for Header<Number, Hash>
58where
59	Number: Copy + Into<U256> + TryFrom<U256> + tetsy_util_mem::MallocSizeOf,
60	Hash: HashT,
61	Hash::Output: tetsy_util_mem::MallocSizeOf,
62{
63	fn size_of(&self, ops: &mut tetsy_util_mem::MallocSizeOfOps) -> usize {
64		self.parent_hash.size_of(ops) +
65			self.number.size_of(ops) +
66			self.state_root.size_of(ops) +
67			self.extrinsics_root.size_of(ops) +
68			self.digest.size_of(ops)
69	}
70}
71
72#[cfg(feature = "std")]
73pub fn serialize_number<S, T: Copy + Into<U256> + TryFrom<U256>>(
74	val: &T, s: S,
75) -> Result<S::Ok, S::Error> where S: serde::Serializer {
76	let u256: U256 = (*val).into();
77	serde::Serialize::serialize(&u256, s)
78}
79
80#[cfg(feature = "std")]
81pub fn deserialize_number<'a, D, T: Copy + Into<U256> + TryFrom<U256>>(
82	d: D,
83) -> Result<T, D::Error> where D: serde::Deserializer<'a> {
84	let u256: U256 = serde::Deserialize::deserialize(d)?;
85	TryFrom::try_from(u256).map_err(|_| serde::de::Error::custom("Try from failed"))
86}
87
88impl<Number, Hash> Decode for Header<Number, Hash> where
89	Number: HasCompact + Copy + Into<U256> + TryFrom<U256>,
90	Hash: HashT,
91	Hash::Output: Decode,
92{
93	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
94		Ok(Header {
95			parent_hash: Decode::decode(input)?,
96			number: <<Number as HasCompact>::Type>::decode(input)?.into(),
97			state_root: Decode::decode(input)?,
98			extrinsics_root: Decode::decode(input)?,
99			digest: Decode::decode(input)?,
100		})
101	}
102}
103
104impl<Number, Hash> Encode for Header<Number, Hash> where
105	Number: HasCompact + Copy + Into<U256> + TryFrom<U256>,
106	Hash: HashT,
107	Hash::Output: Encode,
108{
109	fn encode_to<T: Output + ?Sized>(&self, dest: &mut T) {
110		self.parent_hash.encode_to(dest);
111		<<<Number as HasCompact>::Type as EncodeAsRef<_>>::RefType>::from(&self.number).encode_to(dest);
112		self.state_root.encode_to(dest);
113		self.extrinsics_root.encode_to(dest);
114		self.digest.encode_to(dest);
115	}
116}
117
118impl<Number, Hash> codec::EncodeLike for Header<Number, Hash> where
119	Number: HasCompact + Copy + Into<U256> + TryFrom<U256>,
120	Hash: HashT,
121	Hash::Output: Encode,
122{}
123
124impl<Number, Hash> traits::Header for Header<Number, Hash> where
125	Number: Member + MaybeSerializeDeserialize + Debug + tetcore_std::hash::Hash + MaybeDisplay +
126		AtLeast32BitUnsigned + Codec + Copy + Into<U256> + TryFrom<U256> + tetcore_std::str::FromStr +
127		MaybeMallocSizeOf,
128	Hash: HashT,
129	Hash::Output: Default + tetcore_std::hash::Hash + Copy + Member + Ord +
130		MaybeSerialize + Debug + MaybeDisplay + SimpleBitOps + Codec + MaybeMallocSizeOf,
131{
132	type Number = Number;
133	type Hash = <Hash as HashT>::Output;
134	type Hashing = Hash;
135
136	fn number(&self) -> &Self::Number { &self.number }
137	fn set_number(&mut self, num: Self::Number) { self.number = num }
138
139	fn extrinsics_root(&self) -> &Self::Hash { &self.extrinsics_root }
140	fn set_extrinsics_root(&mut self, root: Self::Hash) { self.extrinsics_root = root }
141
142	fn state_root(&self) -> &Self::Hash { &self.state_root }
143	fn set_state_root(&mut self, root: Self::Hash) { self.state_root = root }
144
145	fn parent_hash(&self) -> &Self::Hash { &self.parent_hash }
146	fn set_parent_hash(&mut self, hash: Self::Hash) { self.parent_hash = hash }
147
148	fn digest(&self) -> &Digest<Self::Hash> { &self.digest }
149
150	fn digest_mut(&mut self) -> &mut Digest<Self::Hash> {
151		#[cfg(feature = "std")]
152		log::debug!(target: "header", "Retrieving mutable reference to digest");
153		&mut self.digest
154	}
155
156	fn new(
157		number: Self::Number,
158		extrinsics_root: Self::Hash,
159		state_root: Self::Hash,
160		parent_hash: Self::Hash,
161		digest: Digest<Self::Hash>,
162	) -> Self {
163		Header {
164			number,
165			extrinsics_root,
166			state_root,
167			parent_hash,
168			digest,
169		}
170	}
171}
172
173impl<Number, Hash> Header<Number, Hash> where
174	Number: Member + tetcore_std::hash::Hash + Copy + MaybeDisplay + AtLeast32BitUnsigned + Codec +
175		Into<U256> + TryFrom<U256>,
176	Hash: HashT,
177	Hash::Output: Default + tetcore_std::hash::Hash + Copy + Member + MaybeDisplay + SimpleBitOps + Codec,
178 {
179	/// Convenience helper for computing the hash of the header without having
180	/// to import the trait.
181	pub fn hash(&self) -> Hash::Output {
182		Hash::hash_of(self)
183	}
184}
185
186#[cfg(all(test, feature = "std"))]
187mod tests {
188	use super::*;
189
190	#[test]
191	fn should_serialize_numbers() {
192		fn serialize(num: u128) -> String {
193			let mut v = vec![];
194			{
195				let mut ser = serde_json::Serializer::new(std::io::Cursor::new(&mut v));
196				serialize_number(&num, &mut ser).unwrap();
197			}
198			String::from_utf8(v).unwrap()
199		}
200
201		assert_eq!(serialize(0), "\"0x0\"".to_owned());
202		assert_eq!(serialize(1), "\"0x1\"".to_owned());
203		assert_eq!(serialize(u64::max_value() as u128), "\"0xffffffffffffffff\"".to_owned());
204		assert_eq!(serialize(u64::max_value() as u128 + 1), "\"0x10000000000000000\"".to_owned());
205	}
206
207	#[test]
208	fn should_deserialize_number() {
209		fn deserialize(num: &str) -> u128 {
210			let mut der = serde_json::Deserializer::new(serde_json::de::StrRead::new(num));
211			deserialize_number(&mut der).unwrap()
212		}
213
214		assert_eq!(deserialize("\"0x0\""), 0);
215		assert_eq!(deserialize("\"0x1\""), 1);
216		assert_eq!(deserialize("\"0xffffffffffffffff\""), u64::max_value() as u128);
217		assert_eq!(deserialize("\"0x10000000000000000\""), u64::max_value() as u128 + 1);
218	}
219}