mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 23:57:56 +00:00
04fcc71809
* primitives/sr-primitives -> primitives/runtime * update
113 lines
3.6 KiB
Rust
113 lines
3.6 KiB
Rust
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
|
|
// This file is part of Substrate.
|
|
|
|
// Substrate is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
|
|
// Substrate is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//! Generic implementation of a block and associated items.
|
|
|
|
#[cfg(feature = "std")]
|
|
use std::fmt;
|
|
|
|
#[cfg(feature = "std")]
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use rstd::prelude::*;
|
|
use primitives::RuntimeDebug;
|
|
use crate::codec::{Codec, Encode, Decode};
|
|
use crate::traits::{self, Member, Block as BlockT, Header as HeaderT, MaybeSerialize};
|
|
use crate::Justification;
|
|
|
|
/// Something to identify a block.
|
|
#[derive(PartialEq, Eq, Clone, RuntimeDebug)]
|
|
#[cfg_attr(feature = "std", derive(Serialize))]
|
|
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
|
|
#[cfg_attr(feature = "std", serde(deny_unknown_fields))]
|
|
pub enum BlockId<Block: BlockT> {
|
|
/// Identify by block header hash.
|
|
Hash(<<Block as BlockT>::Header as HeaderT>::Hash),
|
|
/// Identify by block number.
|
|
Number(<<Block as BlockT>::Header as HeaderT>::Number),
|
|
}
|
|
|
|
impl<Block: BlockT> BlockId<Block> {
|
|
/// Create a block ID from a hash.
|
|
pub fn hash(hash: Block::Hash) -> Self {
|
|
BlockId::Hash(hash)
|
|
}
|
|
|
|
/// Create a block ID from a number.
|
|
pub fn number(number: <Block::Header as HeaderT>::Number) -> Self {
|
|
BlockId::Number(number)
|
|
}
|
|
}
|
|
|
|
impl<Block: BlockT> Copy for BlockId<Block> {}
|
|
|
|
#[cfg(feature = "std")]
|
|
impl<Block: BlockT> fmt::Display for BlockId<Block> {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
write!(f, "{:?}", self)
|
|
}
|
|
}
|
|
|
|
/// Abstraction over a substrate block.
|
|
#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
|
|
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
|
|
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
|
|
#[cfg_attr(feature = "std", serde(deny_unknown_fields))]
|
|
pub struct Block<Header, Extrinsic: MaybeSerialize> {
|
|
/// The block header.
|
|
pub header: Header,
|
|
/// The accompanying extrinsics.
|
|
pub extrinsics: Vec<Extrinsic>,
|
|
}
|
|
|
|
impl<Header, Extrinsic: MaybeSerialize> traits::Block for Block<Header, Extrinsic>
|
|
where
|
|
Header: HeaderT,
|
|
Extrinsic: Member + Codec + traits::Extrinsic,
|
|
{
|
|
type Extrinsic = Extrinsic;
|
|
type Header = Header;
|
|
type Hash = <Self::Header as traits::Header>::Hash;
|
|
|
|
fn header(&self) -> &Self::Header {
|
|
&self.header
|
|
}
|
|
fn extrinsics(&self) -> &[Self::Extrinsic] {
|
|
&self.extrinsics[..]
|
|
}
|
|
fn deconstruct(self) -> (Self::Header, Vec<Self::Extrinsic>) {
|
|
(self.header, self.extrinsics)
|
|
}
|
|
fn new(header: Self::Header, extrinsics: Vec<Self::Extrinsic>) -> Self {
|
|
Block { header, extrinsics }
|
|
}
|
|
fn encode_from(header: &Self::Header, extrinsics: &[Self::Extrinsic]) -> Vec<u8> {
|
|
(header, extrinsics).encode()
|
|
}
|
|
}
|
|
|
|
/// Abstraction over a substrate block and justification.
|
|
#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
|
|
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
|
|
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
|
|
#[cfg_attr(feature = "std", serde(deny_unknown_fields))]
|
|
pub struct SignedBlock<Block> {
|
|
/// Full block.
|
|
pub block: Block,
|
|
/// Block justification.
|
|
pub justification: Option<Justification>,
|
|
}
|