2016-02-05 13:40:41 +01:00
|
|
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity 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.
|
|
|
|
|
|
|
|
// Parity 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 Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-02-02 15:29:53 +01:00
|
|
|
//! Parameters for a block chain.
|
|
|
|
|
2016-01-09 12:30:41 +01:00
|
|
|
use common::*;
|
|
|
|
use engine::*;
|
2016-01-25 18:56:36 +01:00
|
|
|
use pod_state::*;
|
2016-01-09 12:30:41 +01:00
|
|
|
use null_engine::*;
|
2016-02-05 01:49:06 +01:00
|
|
|
use account_db::*;
|
2016-04-09 19:20:35 +02:00
|
|
|
use super::genesis::Genesis;
|
|
|
|
use super::seal::Generic as GenericSeal;
|
2016-03-17 15:15:10 +01:00
|
|
|
use ethereum;
|
2016-05-03 17:23:53 +02:00
|
|
|
use basic_authority::BasicAuthority;
|
2016-04-09 19:20:35 +02:00
|
|
|
use ethjson;
|
|
|
|
|
|
|
|
/// Parameters common to all engines.
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
pub struct CommonParams {
|
|
|
|
/// Account start nonce.
|
|
|
|
pub account_start_nonce: U256,
|
|
|
|
/// Maximum size of extra data.
|
|
|
|
pub maximum_extra_data_size: usize,
|
|
|
|
/// Network id.
|
|
|
|
pub network_id: U256,
|
|
|
|
/// Minimum gas limit.
|
|
|
|
pub min_gas_limit: U256,
|
2015-12-23 12:53:34 +01:00
|
|
|
}
|
|
|
|
|
2016-04-09 19:20:35 +02:00
|
|
|
impl From<ethjson::spec::Params> for CommonParams {
|
|
|
|
fn from(p: ethjson::spec::Params) -> Self {
|
|
|
|
CommonParams {
|
|
|
|
account_start_nonce: p.account_start_nonce.into(),
|
|
|
|
maximum_extra_data_size: p.maximum_extra_data_size.into(),
|
|
|
|
network_id: p.network_id.into(),
|
|
|
|
min_gas_limit: p.min_gas_limit.into(),
|
|
|
|
}
|
|
|
|
}
|
2015-12-21 22:08:42 +01:00
|
|
|
}
|
2015-12-20 21:45:43 +01:00
|
|
|
|
|
|
|
/// Parameters for a block chain; includes both those intrinsic to the design of the
|
|
|
|
/// chain and those to be interpreted by the active chain engine.
|
|
|
|
pub struct Spec {
|
2016-02-03 13:20:32 +01:00
|
|
|
/// User friendly spec name
|
2016-01-14 22:38:49 +01:00
|
|
|
pub name: String,
|
2016-02-03 13:20:32 +01:00
|
|
|
/// What engine are we using for this?
|
2016-04-09 19:20:35 +02:00
|
|
|
pub engine: Box<Engine>,
|
2015-12-20 21:45:43 +01:00
|
|
|
|
2016-01-23 23:53:20 +01:00
|
|
|
/// Known nodes on the network in enode format.
|
|
|
|
pub nodes: Vec<String>,
|
2015-12-20 21:45:43 +01:00
|
|
|
|
2016-04-09 19:20:35 +02:00
|
|
|
/// Parameters common to all engines.
|
|
|
|
pub params: CommonParams,
|
2015-12-20 21:45:43 +01:00
|
|
|
|
2016-02-03 13:20:32 +01:00
|
|
|
/// The genesis block's parent hash field.
|
2015-12-20 21:45:43 +01:00
|
|
|
pub parent_hash: H256,
|
2016-02-03 13:20:32 +01:00
|
|
|
/// The genesis block's author field.
|
2015-12-20 21:45:43 +01:00
|
|
|
pub author: Address,
|
2016-02-03 13:20:32 +01:00
|
|
|
/// The genesis block's difficulty field.
|
2015-12-20 21:45:43 +01:00
|
|
|
pub difficulty: U256,
|
2016-02-03 13:20:32 +01:00
|
|
|
/// The genesis block's gas limit field.
|
2015-12-20 21:45:43 +01:00
|
|
|
pub gas_limit: U256,
|
2016-02-03 13:20:32 +01:00
|
|
|
/// The genesis block's gas used field.
|
2015-12-20 21:45:43 +01:00
|
|
|
pub gas_used: U256,
|
2016-02-03 13:20:32 +01:00
|
|
|
/// The genesis block's timestamp field.
|
2016-01-10 22:55:07 +01:00
|
|
|
pub timestamp: u64,
|
2016-01-28 22:06:25 +01:00
|
|
|
/// Transactions root of the genesis block. Should be SHA3_NULL_RLP.
|
|
|
|
pub transactions_root: H256,
|
|
|
|
/// Receipts root of the genesis block. Should be SHA3_NULL_RLP.
|
|
|
|
pub receipts_root: H256,
|
2016-02-03 13:20:32 +01:00
|
|
|
/// The genesis block's extra data field.
|
2015-12-20 21:45:43 +01:00
|
|
|
pub extra_data: Bytes,
|
2016-02-03 13:20:32 +01:00
|
|
|
/// The number of seal fields in the genesis block.
|
2015-12-20 21:45:43 +01:00
|
|
|
pub seal_fields: usize,
|
2016-02-03 13:20:32 +01:00
|
|
|
/// Each seal field, expressed as RLP, concatenated.
|
2015-12-20 21:45:43 +01:00
|
|
|
pub seal_rlp: Bytes,
|
|
|
|
|
|
|
|
// May be prepopulated if we know this in advance.
|
2016-01-11 11:51:31 +01:00
|
|
|
state_root_memo: RwLock<Option<H256>>,
|
2016-02-03 13:20:32 +01:00
|
|
|
|
|
|
|
// Genesis state as plain old data.
|
|
|
|
genesis_state: PodState,
|
2015-12-20 21:45:43 +01:00
|
|
|
}
|
|
|
|
|
2016-04-09 19:20:35 +02:00
|
|
|
impl From<ethjson::spec::Spec> for Spec {
|
|
|
|
fn from(s: ethjson::spec::Spec) -> Self {
|
|
|
|
let builtins = s.accounts.builtins().into_iter().map(|p| (p.0.into(), From::from(p.1))).collect();
|
|
|
|
let g = Genesis::from(s.genesis);
|
|
|
|
let seal: GenericSeal = g.seal.into();
|
|
|
|
let params = CommonParams::from(s.params);
|
|
|
|
Spec {
|
|
|
|
name: s.name.into(),
|
|
|
|
params: params.clone(),
|
|
|
|
engine: Spec::engine(s.engine, params, builtins),
|
|
|
|
nodes: s.nodes.unwrap_or_else(Vec::new),
|
|
|
|
parent_hash: g.parent_hash,
|
|
|
|
transactions_root: g.transactions_root,
|
|
|
|
receipts_root: g.receipts_root,
|
|
|
|
author: g.author,
|
|
|
|
difficulty: g.difficulty,
|
|
|
|
gas_limit: g.gas_limit,
|
|
|
|
gas_used: g.gas_used,
|
|
|
|
timestamp: g.timestamp,
|
|
|
|
extra_data: g.extra_data,
|
|
|
|
seal_fields: seal.fields,
|
|
|
|
seal_rlp: seal.rlp,
|
|
|
|
state_root_memo: RwLock::new(g.state_root),
|
|
|
|
genesis_state: From::from(s.accounts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-20 21:45:43 +01:00
|
|
|
impl Spec {
|
2016-04-09 19:20:35 +02:00
|
|
|
/// Convert engine spec into a boxed Engine of the right underlying type.
|
|
|
|
/// TODO avoid this hard-coded nastiness - use dynamic-linked plugin framework instead.
|
|
|
|
fn engine(engine_spec: ethjson::spec::Engine, params: CommonParams, builtins: BTreeMap<Address, Builtin>) -> Box<Engine> {
|
|
|
|
match engine_spec {
|
|
|
|
ethjson::spec::Engine::Null => Box::new(NullEngine::new(params, builtins)),
|
2016-05-03 17:23:53 +02:00
|
|
|
ethjson::spec::Engine::Ethash(ethash) => Box::new(ethereum::Ethash::new(params, From::from(ethash.params), builtins)),
|
|
|
|
ethjson::spec::Engine::BasicAuthority(basic_authority) => Box::new(BasicAuthority::new(params, From::from(basic_authority.params), builtins)),
|
2015-12-20 21:45:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-05 00:11:16 +01:00
|
|
|
/// Return the state root for the genesis state, memoising accordingly.
|
2016-01-11 11:51:31 +01:00
|
|
|
pub fn state_root(&self) -> H256 {
|
|
|
|
if self.state_root_memo.read().unwrap().is_none() {
|
2016-01-25 18:56:36 +01:00
|
|
|
*self.state_root_memo.write().unwrap() = Some(self.genesis_state.root());
|
2015-12-20 21:45:43 +01:00
|
|
|
}
|
2016-01-11 11:51:31 +01:00
|
|
|
self.state_root_memo.read().unwrap().as_ref().unwrap().clone()
|
2015-12-20 21:45:43 +01:00
|
|
|
}
|
|
|
|
|
2016-01-23 23:53:20 +01:00
|
|
|
/// Get the known knodes of the network in enode format.
|
|
|
|
pub fn nodes(&self) -> &Vec<String> { &self.nodes }
|
|
|
|
|
2016-02-24 21:23:58 +01:00
|
|
|
/// Get the configured Network ID.
|
2016-04-09 19:20:35 +02:00
|
|
|
pub fn network_id(&self) -> U256 { self.params.network_id }
|
2016-02-24 21:23:58 +01:00
|
|
|
|
2016-02-03 13:20:32 +01:00
|
|
|
/// Get the header of the genesis block.
|
2016-01-09 12:30:41 +01:00
|
|
|
pub fn genesis_header(&self) -> Header {
|
2016-01-06 15:57:17 +01:00
|
|
|
Header {
|
2015-12-21 22:08:42 +01:00
|
|
|
parent_hash: self.parent_hash.clone(),
|
2016-01-10 21:55:03 +01:00
|
|
|
timestamp: self.timestamp,
|
|
|
|
number: 0,
|
2015-12-21 22:08:42 +01:00
|
|
|
author: self.author.clone(),
|
2016-01-28 22:06:25 +01:00
|
|
|
transactions_root: self.transactions_root.clone(),
|
2016-01-06 15:57:17 +01:00
|
|
|
uncles_hash: RlpStream::new_list(0).out().sha3(),
|
2015-12-21 22:08:42 +01:00
|
|
|
extra_data: self.extra_data.clone(),
|
|
|
|
state_root: self.state_root().clone(),
|
2016-03-07 14:33:00 +01:00
|
|
|
receipts_root: self.receipts_root.clone(),
|
2015-12-21 22:08:42 +01:00
|
|
|
log_bloom: H2048::new().clone(),
|
|
|
|
gas_used: self.gas_used.clone(),
|
|
|
|
gas_limit: self.gas_limit.clone(),
|
|
|
|
difficulty: self.difficulty.clone(),
|
|
|
|
seal: {
|
|
|
|
let seal = {
|
|
|
|
let mut s = RlpStream::new_list(self.seal_fields);
|
|
|
|
s.append_raw(&self.seal_rlp, self.seal_fields);
|
|
|
|
s.out()
|
|
|
|
};
|
|
|
|
let r = Rlp::new(&seal);
|
2016-01-08 16:00:32 +01:00
|
|
|
(0..self.seal_fields).map(|i| r.at(i).as_raw().to_vec()).collect()
|
2015-12-21 22:08:42 +01:00
|
|
|
},
|
2016-01-11 11:51:31 +01:00
|
|
|
hash: RefCell::new(None),
|
2016-01-17 12:00:34 +01:00
|
|
|
bare_hash: RefCell::new(None),
|
2016-01-06 15:57:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Compose the genesis block for this chain.
|
|
|
|
pub fn genesis_block(&self) -> Bytes {
|
|
|
|
let empty_list = RlpStream::new_list(0).out();
|
|
|
|
let header = self.genesis_header();
|
2015-12-21 22:08:42 +01:00
|
|
|
let mut ret = RlpStream::new_list(3);
|
|
|
|
ret.append(&header);
|
|
|
|
ret.append_raw(&empty_list, 1);
|
|
|
|
ret.append_raw(&empty_list, 1);
|
|
|
|
ret.out()
|
2015-12-20 21:45:43 +01:00
|
|
|
}
|
2016-01-25 18:56:36 +01:00
|
|
|
|
2016-03-17 15:15:10 +01:00
|
|
|
/// Overwrite the genesis components.
|
|
|
|
pub fn overwrite_genesis_params(&mut self, g: Genesis) {
|
2016-04-09 19:20:35 +02:00
|
|
|
let seal: GenericSeal = g.seal.into();
|
2016-03-17 15:15:10 +01:00
|
|
|
self.parent_hash = g.parent_hash;
|
|
|
|
self.transactions_root = g.transactions_root;
|
|
|
|
self.receipts_root = g.receipts_root;
|
|
|
|
self.author = g.author;
|
|
|
|
self.difficulty = g.difficulty;
|
|
|
|
self.gas_limit = g.gas_limit;
|
|
|
|
self.gas_used = g.gas_used;
|
|
|
|
self.timestamp = g.timestamp;
|
|
|
|
self.extra_data = g.extra_data;
|
2016-04-09 19:20:35 +02:00
|
|
|
self.seal_fields = seal.fields;
|
|
|
|
self.seal_rlp = seal.rlp;
|
2016-03-17 15:15:10 +01:00
|
|
|
self.state_root_memo = RwLock::new(g.state_root);
|
|
|
|
}
|
|
|
|
|
2016-01-25 23:24:51 +01:00
|
|
|
/// Alter the value of the genesis state.
|
|
|
|
pub fn set_genesis_state(&mut self, s: PodState) {
|
|
|
|
self.genesis_state = s;
|
|
|
|
*self.state_root_memo.write().unwrap() = None;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `false` if the memoized state root is invalid. `true` otherwise.
|
|
|
|
pub fn is_state_root_valid(&self) -> bool {
|
|
|
|
self.state_root_memo.read().unwrap().clone().map_or(true, |sr| sr == self.genesis_state.root())
|
|
|
|
}
|
2016-01-04 22:47:45 +01:00
|
|
|
|
2016-01-09 12:30:41 +01:00
|
|
|
/// Ensure that the given state DB has the trie nodes in for the genesis state.
|
2016-01-18 13:54:46 +01:00
|
|
|
pub fn ensure_db_good(&self, db: &mut HashDB) -> bool {
|
2016-01-09 12:30:41 +01:00
|
|
|
if !db.contains(&self.state_root()) {
|
2016-03-07 14:33:00 +01:00
|
|
|
let mut root = H256::new();
|
2016-01-14 22:38:49 +01:00
|
|
|
{
|
|
|
|
let mut t = SecTrieDBMut::new(db, &mut root);
|
2016-01-25 18:56:36 +01:00
|
|
|
for (address, account) in self.genesis_state.get().iter() {
|
2016-01-14 22:38:49 +01:00
|
|
|
t.insert(address.as_slice(), &account.rlp());
|
|
|
|
}
|
2016-01-09 12:30:41 +01:00
|
|
|
}
|
2016-02-04 02:40:35 +01:00
|
|
|
for (address, account) in self.genesis_state.get().iter() {
|
|
|
|
account.insert_additional(&mut AccountDBMut::new(db, address));
|
2016-01-26 15:00:22 +01:00
|
|
|
}
|
2016-01-14 22:38:49 +01:00
|
|
|
assert!(db.contains(&self.state_root()));
|
2016-01-18 13:54:46 +01:00
|
|
|
true
|
|
|
|
} else { false }
|
2016-01-09 12:30:41 +01:00
|
|
|
}
|
|
|
|
|
2016-04-09 19:20:35 +02:00
|
|
|
/// Loads spec from json file.
|
|
|
|
pub fn load(reader: &[u8]) -> Self {
|
|
|
|
From::from(ethjson::spec::Spec::load(reader).expect("invalid json file"))
|
2016-01-08 16:24:14 +01:00
|
|
|
}
|
|
|
|
|
2016-05-03 17:23:53 +02:00
|
|
|
/// Create a new Spec which conforms to the Frontier-era Morden chain except that it's a NullEngine consensus.
|
2016-04-09 19:20:35 +02:00
|
|
|
pub fn new_test() -> Spec {
|
|
|
|
Spec::load(include_bytes!("../../res/null_morden.json"))
|
|
|
|
}
|
2015-12-20 21:45:43 +01:00
|
|
|
}
|
|
|
|
|
2016-01-06 16:00:42 +01:00
|
|
|
#[cfg(test)]
|
2016-01-06 15:57:17 +01:00
|
|
|
mod tests {
|
|
|
|
use std::str::FromStr;
|
|
|
|
use util::hash::*;
|
|
|
|
use util::sha3::*;
|
|
|
|
use views::*;
|
|
|
|
use super::*;
|
|
|
|
|
2016-01-07 19:10:29 +01:00
|
|
|
#[test]
|
2016-01-09 18:20:31 +01:00
|
|
|
fn test_chain() {
|
|
|
|
let test_spec = Spec::new_test();
|
2016-01-08 16:24:14 +01:00
|
|
|
|
2016-01-11 11:51:31 +01:00
|
|
|
assert_eq!(test_spec.state_root(), H256::from_str("f3f4696bbf3b3b07775128eb7a3763279a394e382130f27c21e70233e04946a9").unwrap());
|
2016-01-09 18:20:31 +01:00
|
|
|
let genesis = test_spec.genesis_block();
|
2016-01-09 17:15:55 +01:00
|
|
|
assert_eq!(BlockView::new(&genesis).header_view().sha3(), H256::from_str("0cd786a2425d16f152c658316c423e6ce1181e15c3295826d7c9904cba9ce303").unwrap());
|
2016-01-07 19:10:29 +01:00
|
|
|
}
|
2016-01-09 10:26:31 +01:00
|
|
|
}
|