1use crate::{
20 bytes::Bytes,
21 hash::{Address, H256},
22 spec::Seal,
23 uint::{self, Uint},
24};
25use serde::Deserialize;
26
27#[derive(Debug, PartialEq, Deserialize)]
29#[serde(deny_unknown_fields)]
30#[serde(rename_all = "camelCase")]
31pub struct Genesis {
32 pub seal: Seal,
34 pub difficulty: Uint,
36 pub author: Option<Address>,
38 pub timestamp: Option<Uint>,
40 pub parent_hash: Option<H256>,
42 #[serde(deserialize_with="uint::validate_non_zero")]
44 pub gas_limit: Uint,
45 pub transactions_root: Option<H256>,
47 pub receipts_root: Option<H256>,
49 pub state_root: Option<H256>,
51 pub gas_used: Option<Uint>,
53 pub extra_data: Option<Bytes>,
55}
56
57#[cfg(test)]
58mod tests {
59 use std::str::FromStr;
60 use super::{Address, Bytes, Genesis, H256, Uint};
61 use crate::{
62 hash::H64,
63 spec::{Ethereum, Seal}
64 };
65 use ethereum_types::{U256, H160, H64 as Eth64, H256 as Eth256};
66
67 #[test]
68 fn genesis_deserialization() {
69 let s = r#"{
70 "difficulty": "0x400000000",
71 "seal": {
72 "ethereum": {
73 "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
74 "nonce": "0x00006d6f7264656e"
75 }
76 },
77 "author": "0x1000000000000000000000000000000000000001",
78 "timestamp": "0x07",
79 "parentHash": "0x9000000000000000000000000000000000000000000000000000000000000000",
80 "extraData": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa",
81 "gasLimit": "0x1388",
82 "stateRoot": "0xd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544"
83 }"#;
84 let deserialized: Genesis = serde_json::from_str(s).unwrap();
85 assert_eq!(deserialized, Genesis {
86 seal: Seal::Ethereum(Ethereum {
87 nonce: H64(Eth64::from_str("00006d6f7264656e").unwrap()),
88 mix_hash: H256(Eth256::from_str("0000000000000000000000000000000000000000000000000000000000000000").unwrap())
89 }),
90 difficulty: Uint(U256::from(0x400000000u64)),
91 author: Some(Address(H160::from_str("1000000000000000000000000000000000000001").unwrap())),
92 timestamp: Some(Uint(U256::from(0x07))),
93 parent_hash: Some(H256(Eth256::from_str("9000000000000000000000000000000000000000000000000000000000000000").unwrap())),
94 gas_limit: Uint(U256::from(0x1388)),
95 transactions_root: None,
96 receipts_root: None,
97 state_root: Some(H256(Eth256::from_str("d7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544").unwrap())),
98 gas_used: None,
99 extra_data: Some(Bytes::from_str("11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa").unwrap()),
100 });
101 }
102}