ethjson/spec/
genesis.rs

1// Copyright 2015-2020 Parity Technologies (UK) Ltd.
2// This file is part of Parity Ethereum.
3
4// Parity Ethereum is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Parity Ethereum is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Parity Ethereum.  If not, see <http://www.gnu.org/licenses/>.
16
17//! Spec genesis deserialization.
18
19use crate::{
20	bytes::Bytes,
21	hash::{Address, H256},
22	spec::Seal,
23	uint::{self, Uint},
24};
25use serde::Deserialize;
26
27/// Spec genesis.
28#[derive(Debug, PartialEq, Deserialize)]
29#[serde(deny_unknown_fields)]
30#[serde(rename_all = "camelCase")]
31pub struct Genesis {
32	/// Seal.
33	pub seal: Seal,
34	/// Difficulty.
35	pub difficulty: Uint,
36	/// Block author, defaults to 0.
37	pub author: Option<Address>,
38	/// Block timestamp, defaults to 0.
39	pub timestamp: Option<Uint>,
40	/// Parent hash, defaults to 0.
41	pub parent_hash: Option<H256>,
42	/// Gas limit.
43	#[serde(deserialize_with="uint::validate_non_zero")]
44	pub gas_limit: Uint,
45	/// Transactions root.
46	pub transactions_root: Option<H256>,
47	/// Receipts root.
48	pub receipts_root: Option<H256>,
49	/// State root.
50	pub state_root: Option<H256>,
51	/// Gas used.
52	pub gas_used: Option<Uint>,
53	/// Extra data.
54	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}