Refactor out some duped code

This commit is contained in:
Gav
2018-02-19 16:25:09 +01:00
parent 70db89270f
commit d3a22e12a6
3 changed files with 51 additions and 84 deletions
+48 -34
View File
@@ -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<Transaction: PartialEq + Eq + Clone> {
/// The block header.
pub header: Header,
/// All relay-chain transactions.
pub transactions: Vec<Transaction>,
}
impl<T: PartialEq + Eq + Clone> Slicable for Block<T> where Vec<T>: Slicable {
fn decode<I: Input>(input: &mut I) -> Option<Self> {
Some(Block {
header: try_opt!(Slicable::decode(input)),
transactions: try_opt!(Slicable::decode(input)),
})
}
fn encode(&self) -> Vec<u8> {
let mut v: Vec<u8> = Vec::new();
v.extend(self.header.encode());
v.extend(self.transactions.encode());
v
}
}
impl<T: PartialEq + Eq + Clone> NonTrivialSlicable for Block<T> where Vec<T>: Slicable { }
}
/// The body of a block is just a bunch of transactions.
pub type Body = Vec<Transaction>;
pub type Block = generic::Block<Transaction>;
/// 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<I: Input>(input: &mut I) -> Option<Self> {
Some(Block {
header: try_opt!(Slicable::decode(input)),
transactions: try_opt!(Slicable::decode(input)),
})
}
fn encode(&self) -> Vec<u8> {
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,
}
@@ -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 <http://www.gnu.org/licenses/>.
//! 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<UncheckedTransaction>,
}
impl Slicable for Block {
fn decode<I: Input>(input: &mut I) -> Option<Self> {
Some(Block {
header: Slicable::decode(input)?,
transactions: Slicable::decode(input)?,
})
}
fn encode(&self) -> Vec<u8> {
Vec::new()
.and(&self.header)
.and(&self.transactions)
}
}
impl ::codec::NonTrivialSlicable for Block {}
+3 -2
View File
@@ -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<UncheckedTransaction>;
/// An identifier for an account on this system.
pub type AccountId = AuthorityId;
/// Signature for our transactions.