tp_runtime/generic/
block.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 and associated items.
19
20#[cfg(feature = "std")]
21use std::fmt;
22
23#[cfg(feature = "std")]
24use serde::{Deserialize, Serialize};
25
26use tetcore_std::prelude::*;
27use tet_core::RuntimeDebug;
28use crate::codec::{Codec, Encode, Decode};
29use crate::traits::{
30	self, Member, Block as BlockT, Header as HeaderT, MaybeSerialize, MaybeMallocSizeOf,
31	NumberFor,
32};
33use crate::Justification;
34
35/// Something to identify a block.
36#[derive(PartialEq, Eq, Clone, RuntimeDebug)]
37#[cfg_attr(feature = "std", derive(Serialize))]
38#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
39#[cfg_attr(feature = "std", serde(deny_unknown_fields))]
40pub enum BlockId<Block: BlockT> {
41	/// Identify by block header hash.
42	Hash(Block::Hash),
43	/// Identify by block number.
44	Number(NumberFor<Block>),
45}
46
47impl<Block: BlockT> BlockId<Block> {
48	/// Create a block ID from a hash.
49	pub fn hash(hash: Block::Hash) -> Self {
50		BlockId::Hash(hash)
51	}
52
53	/// Create a block ID from a number.
54	pub fn number(number: NumberFor<Block>) -> Self {
55		BlockId::Number(number)
56	}
57}
58
59impl<Block: BlockT> Copy for BlockId<Block> {}
60
61#[cfg(feature = "std")]
62impl<Block: BlockT> fmt::Display for BlockId<Block> {
63	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
64		write!(f, "{:?}", self)
65	}
66}
67
68/// Abstraction over a tetcore block.
69#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
70#[cfg_attr(feature = "std", derive(Serialize, Deserialize, tetsy_util_mem::MallocSizeOf))]
71#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
72#[cfg_attr(feature = "std", serde(deny_unknown_fields))]
73pub struct Block<Header, Extrinsic: MaybeSerialize> {
74	/// The block header.
75	pub header: Header,
76	/// The accompanying extrinsics.
77	pub extrinsics: Vec<Extrinsic>,
78}
79
80impl<Header, Extrinsic: MaybeSerialize> traits::Block for Block<Header, Extrinsic>
81where
82	Header: HeaderT,
83	Extrinsic: Member + Codec + traits::Extrinsic + MaybeMallocSizeOf,
84{
85	type Extrinsic = Extrinsic;
86	type Header = Header;
87	type Hash = <Self::Header as traits::Header>::Hash;
88
89	fn header(&self) -> &Self::Header {
90		&self.header
91	}
92	fn extrinsics(&self) -> &[Self::Extrinsic] {
93		&self.extrinsics[..]
94	}
95	fn deconstruct(self) -> (Self::Header, Vec<Self::Extrinsic>) {
96		(self.header, self.extrinsics)
97	}
98	fn new(header: Self::Header, extrinsics: Vec<Self::Extrinsic>) -> Self {
99		Block { header, extrinsics }
100	}
101	fn encode_from(header: &Self::Header, extrinsics: &[Self::Extrinsic]) -> Vec<u8> {
102		(header, extrinsics).encode()
103	}
104}
105
106/// Abstraction over a tetcore block and justification.
107#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
108#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
109#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
110#[cfg_attr(feature = "std", serde(deny_unknown_fields))]
111pub struct SignedBlock<Block> {
112	/// Full block.
113	pub block: Block,
114	/// Block justification.
115	pub justification: Option<Justification>,
116}