From d3a22e12a65b3b4709f412e5af5bbcd01e1e41f7 Mon Sep 17 00:00:00 2001 From: Gav Date: Mon, 19 Feb 2018 16:25:09 +0100 Subject: [PATCH] Refactor out some duped code --- substrate/substrate/primitives/src/block.rs | 82 +++++++++++-------- substrate/substrate/test-runtime/src/block.rs | 48 ----------- substrate/substrate/test-runtime/src/lib.rs | 5 +- 3 files changed, 51 insertions(+), 84 deletions(-) delete mode 100644 substrate/substrate/test-runtime/src/block.rs diff --git a/substrate/substrate/primitives/src/block.rs b/substrate/substrate/primitives/src/block.rs index 36640873ba..0bc577eb9c 100644 --- a/substrate/substrate/primitives/src/block.rs +++ b/substrate/substrate/primitives/src/block.rs @@ -21,7 +21,7 @@ use rstd::vec::Vec; #[cfg(feature = "std")] use bytes; use Hash; -use codec::{Input, Slicable}; +use codec::{Input, Slicable, NonTrivialSlicable}; /// Used to refer to a block number. pub type Number = u64; @@ -47,7 +47,9 @@ impl Slicable for Transaction { } } -impl ::codec::NonTrivialSlicable for Transaction { } +impl NonTrivialSlicable for Transaction { } + + /// Execution log (event) #[derive(PartialEq, Eq, Clone)] @@ -64,7 +66,9 @@ impl Slicable for Log { } } -impl ::codec::NonTrivialSlicable for Log { } +impl NonTrivialSlicable for Log { } + + /// The digest of a block, useful for light-clients. #[derive(Clone, Default, PartialEq, Eq)] @@ -84,40 +88,47 @@ impl Slicable for Digest { } } +impl NonTrivialSlicable for Digest { } + + + +pub mod generic { + use super::{Header, Slicable, Input, NonTrivialSlicable}; + + #[derive(PartialEq, Eq, Clone)] + #[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))] + pub struct Block { + /// The block header. + pub header: Header, + /// All relay-chain transactions. + pub transactions: Vec, + } + + impl Slicable for Block where Vec: Slicable { + fn decode(input: &mut I) -> Option { + Some(Block { + header: try_opt!(Slicable::decode(input)), + transactions: try_opt!(Slicable::decode(input)), + }) + } + + fn encode(&self) -> Vec { + let mut v: Vec = Vec::new(); + v.extend(self.header.encode()); + v.extend(self.transactions.encode()); + v + } + } + + impl NonTrivialSlicable for Block where Vec: Slicable { } +} + /// The body of a block is just a bunch of transactions. pub type Body = Vec; +pub type Block = generic::Block; -/// A Substrate relay chain block. -#[derive(PartialEq, Eq, Clone)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))] -pub struct Block { - /// The block header. - pub header: Header, - /// All relay-chain transactions. - pub transactions: Body, -} - -impl Slicable for Block { - fn decode(input: &mut I) -> Option { - Some(Block { - header: try_opt!(Slicable::decode(input)), - transactions: try_opt!(Slicable::decode(input)), - }) - } - - fn encode(&self) -> Vec { - let mut v = Vec::new(); - - v.extend(self.header.encode()); - v.extend(self.transactions.encode()); - - v - } -} - -/// A relay chain block header. -/// -/// https://github.com/w3f/polkadot-spec/blob/master/spec.md#header +/// A substrate chain block header. +// TODO: split out into light-client-specific fields and runtime-specific fields. #[derive(PartialEq, Eq, Clone)] #[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))] #[cfg_attr(feature = "std", serde(rename_all = "camelCase"))] @@ -131,6 +142,9 @@ pub struct Header { pub state_root: Hash, /// The root of the trie that represents this block's transactions, indexed by a 32-byte integer. pub transaction_root: Hash, + // TODO... +// /// The root of the trie that represents the receipts from this block's transactions +// pub receipts_root: Hash, /// The digest of activity on the block. pub digest: Digest, } diff --git a/substrate/substrate/test-runtime/src/block.rs b/substrate/substrate/test-runtime/src/block.rs deleted file mode 100644 index f85bbd882e..0000000000 --- a/substrate/substrate/test-runtime/src/block.rs +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright 2017 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 . - -//! A toy unchecked transaction complete with signature. - -use rstd::prelude::*; -use codec::{Input, Slicable, Joiner}; -use super::{Header, UncheckedTransaction}; - -#[derive(PartialEq, Eq, Clone)] -#[cfg_attr(feature = "std", derive(Debug))] -/// A coupling between a header and a list of transactions. -pub struct Block { - /// The block header. - pub header: Header, - /// The list of transactions in the block. - pub transactions: Vec, -} - -impl Slicable for Block { - fn decode(input: &mut I) -> Option { - Some(Block { - header: Slicable::decode(input)?, - transactions: Slicable::decode(input)?, - }) - } - - fn encode(&self) -> Vec { - Vec::new() - .and(&self.header) - .and(&self.transactions) - } -} - -impl ::codec::NonTrivialSlicable for Block {} diff --git a/substrate/substrate/test-runtime/src/lib.rs b/substrate/substrate/test-runtime/src/lib.rs index 52fedf788f..fb09970820 100644 --- a/substrate/substrate/test-runtime/src/lib.rs +++ b/substrate/substrate/test-runtime/src/lib.rs @@ -31,19 +31,20 @@ extern crate substrate_codec as codec; pub mod system; mod transaction; mod unchecked_transaction; -mod block; use rstd::prelude::*; use codec::Slicable; use primitives::AuthorityId; use primitives::hash::H512; +use primitives::block::generic; pub use primitives::hash::H256; pub use primitives::block::{Header, Number as BlockNumber, Digest}; pub use transaction::Transaction; pub use unchecked_transaction::UncheckedTransaction; -pub use block::Block; +/// A test block. +pub type Block = generic::Block; /// An identifier for an account on this system. pub type AccountId = AuthorityId; /// Signature for our transactions.