added genesis method to ethjson blockchain

This commit is contained in:
debris 2016-03-17 14:03:53 +01:00
parent 0621da8535
commit 0f889d4222
5 changed files with 70 additions and 28 deletions

View File

@ -20,6 +20,7 @@ use bytes::Bytes;
use blockchain::state::State; use blockchain::state::State;
use blockchain::header::Header; use blockchain::header::Header;
use blockchain::block::Block; use blockchain::block::Block;
use spec::Genesis;
/// Blockchain deserialization. /// Blockchain deserialization.
#[derive(Debug, PartialEq, Deserialize)] #[derive(Debug, PartialEq, Deserialize)]
@ -45,6 +46,21 @@ impl BlockChain {
pub fn blocks_rlp(&self) -> Vec<Vec<u8>> { pub fn blocks_rlp(&self) -> Vec<Vec<u8>> {
self.blocks.iter().map(|block| block.rlp()).collect() self.blocks.iter().map(|block| block.rlp()).collect()
} }
/// Returns spec compatible genesis struct.
pub fn genesis(&self) -> Genesis {
Genesis {
nonce: Some(self.genesis_block.nonce.clone()),
mix_hash: Some(self.genesis_block.mix_hash.clone()),
seal_fields: None,
seal_rlp: None,
difficulty: self.genesis_block.difficulty,
author: self.genesis_block.author.clone(),
timestamp: self.genesis_block.timestamp,
parent_hash: self.genesis_block.parent_hash.clone(),
gas_limit: self.genesis_block.gas_limit,
}
}
} }
#[cfg(test)] #[cfg(test)]

View File

@ -23,31 +23,48 @@ use bytes::Bytes;
/// Blockchain test header deserializer. /// Blockchain test header deserializer.
#[derive(Debug, PartialEq, Deserialize)] #[derive(Debug, PartialEq, Deserialize)]
pub struct Header { pub struct Header {
bloom: Bloom, /// Blocks bloom.
coinbase: Address, pub bloom: Bloom,
difficulty: Uint, /// Blocks author.
#[serde(rename="coinbase")]
pub author: Address,
/// Difficulty.
pub difficulty: Uint,
#[serde(rename="extraData")] #[serde(rename="extraData")]
extra_data: Bytes, /// Extra data.
pub extra_data: Bytes,
/// Gas limit.
#[serde(rename="gasLimit")] #[serde(rename="gasLimit")]
gas_limit: Uint, pub gas_limit: Uint,
/// Gas used.
#[serde(rename="gasUsed")] #[serde(rename="gasUsed")]
gas_used: Uint, pub gas_used: Uint,
hash: H256, /// Hash.
pub hash: H256,
#[serde(rename="mixHash")] #[serde(rename="mixHash")]
mix_hash: H256, /// Mix hash.
nonce: H64, pub mix_hash: H256,
number: Uint, /// Seal nonce.
pub nonce: H64,
/// Block number.
pub number: Uint,
/// Parent hash.
#[serde(rename="parentHash")] #[serde(rename="parentHash")]
parent_hash: H256, pub parent_hash: H256,
/// Receipt root.
#[serde(rename="receiptTrie")] #[serde(rename="receiptTrie")]
receipt_trie: H256, pub receipt_root: H256,
/// State root.
#[serde(rename="stateRoot")] #[serde(rename="stateRoot")]
state_root: H256, pub state_root: H256,
timestamp: Uint, /// Timestamp.
pub timestamp: Uint,
/// Transactions root.
#[serde(rename="transactionsTrie")] #[serde(rename="transactionsTrie")]
transactions_trie: H256, pub transactions_root: H256,
/// Uncles hash.
#[serde(rename="uncleHash")] #[serde(rename="uncleHash")]
uncle_hash: H256, pub uncles_hash: H256,
} }
#[cfg(test)] #[cfg(test)]

View File

@ -25,7 +25,7 @@ use util::hash::{H64 as Hash64, Address as Hash160, H256 as Hash256, H2048 as Ha
macro_rules! impl_hash { macro_rules! impl_hash {
($name: ident, $inner: ident) => { ($name: ident, $inner: ident) => {
/// Lenient hash json deserialization for test json files. /// Lenient hash json deserialization for test json files.
#[derive(Default, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] #[derive(Default, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Clone)]
pub struct $name($inner); pub struct $name($inner);
impl Into<$inner> for $name { impl Into<$inner> for $name {

View File

@ -17,30 +17,39 @@
//! Spec genesis deserialization. //! Spec genesis deserialization.
use uint::Uint; use uint::Uint;
use hash::{Address, H256}; use hash::{H64, Address, H256};
use bytes::Bytes; use bytes::Bytes;
/// Spec genesis. /// Spec genesis.
#[derive(Debug, PartialEq, Deserialize)] #[derive(Debug, PartialEq, Deserialize)]
pub struct Genesis { pub struct Genesis {
// old seal // old seal
nonce: Option<Uint>, /// Seal nonce.
pub nonce: Option<H64>,
#[serde(rename="mixHash")] #[serde(rename="mixHash")]
mix_hash: Option<H256>, /// Seal mix hash.
pub mix_hash: Option<H256>,
// new seal // TODO: consider moving it to a separate seal structure // new seal // TODO: consider moving it to a separate seal structure
#[serde(rename="sealFields")] #[serde(rename="sealFields")]
seal_fields: Option<Uint>, /// Number of seal fields.
pub seal_fields: Option<Uint>,
#[serde(rename="sealRlp")] #[serde(rename="sealRlp")]
seal_rlp: Option<Bytes>, /// Seal rlp.
pub seal_rlp: Option<Bytes>,
difficulty: Uint, /// Difficulty.
author: Address, pub difficulty: Uint,
timestamp: Uint, /// Block author.
pub author: Address,
/// Block timestamp.
pub timestamp: Uint,
/// Parent hash.
#[serde(rename="parentHash")] #[serde(rename="parentHash")]
parent_hash: H256, pub parent_hash: H256,
/// Gas limit.
#[serde(rename="gasLimit")] #[serde(rename="gasLimit")]
gas_limit: Uint, pub gas_limit: Uint,
} }
#[cfg(test)] #[cfg(test)]

View File

@ -22,7 +22,7 @@ use serde::de::Visitor;
use util::numbers::{U256, Uint as U}; use util::numbers::{U256, Uint as U};
/// Lenient uint json deserialization for test json files. /// Lenient uint json deserialization for test json files.
#[derive(Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
pub struct Uint(U256); pub struct Uint(U256);
impl Into<U256> for Uint { impl Into<U256> for Uint {