loading genesis state

This commit is contained in:
debris 2015-12-12 06:05:36 +01:00
parent 04b6743ba8
commit ada79ed62a
7 changed files with 133 additions and 0 deletions

4
.gitignore vendored
View File

@ -12,4 +12,8 @@ Cargo.lock
# Generated by Cargo
/target/
# vim stuff
*.swp
# mac stuff
.DS_Store

View File

@ -11,6 +11,8 @@ log = "0.3"
env_logger = "0.3"
ethcore-util = "0.1.0"
evmjit = { path = "rust-evmjit", optional = true }
rustc-serialize = "0.3"
flate2 = "0.2"
[features]
jit = ["evmjit"]

1
res/genesis_frontier Normal file

File diff suppressed because one or more lines are too long

View File

@ -10,6 +10,15 @@ pub struct Account {
}
impl Account {
pub fn new_with_balance(balance: U256) -> Account {
Account {
balance: balance,
code: vec![],
nonce: U256::from(0u8),
storage: HashMap::new()
}
}
pub fn balance(&self) -> &U256 { &self.balance }
pub fn code(&self) -> &[u8] { &self.code }
pub fn nonce(&self) -> &U256 { &self.nonce }
@ -21,5 +30,11 @@ pub struct AccountMap {
}
impl AccountMap {
pub fn new(accounts: HashMap<Address, Account>) -> AccountMap {
AccountMap {
accounts: accounts
}
}
pub fn accounts(&self) -> &HashMap<Address, Account> { &self.accounts }
}

103
src/genesis.rs Normal file
View File

@ -0,0 +1,103 @@
use std::io::Read;
use std::str::FromStr;
use std::collections::HashMap;
use rustc_serialize::base64::FromBase64;
use rustc_serialize::json::Json;
use flate2::read::GzDecoder;
use util::rlp::*;
use util::hash::*;
use util::uint::*;
use util::sha3::*;
use account::*;
/// Converts file from base64 gzipped bytes to json
fn base_to_json(source: &[u8]) -> Json {
// there is probably no need to store genesis in based64 gzip,
// but that's what go does, and it was easy to load it this way
let data = source.from_base64().expect("Genesis block is malformed!");
let data_ref: &[u8] = &data;
let mut decoder = GzDecoder::new(data_ref).expect("Gzip is invalid");
let mut s: String = "".to_string();
decoder.read_to_string(&mut s).expect("Gzip is invalid");
Json::from_str(&s).expect("Json is invalid")
}
pub struct Genesis {
block: Vec<u8>,
state: AccountMap
}
impl Genesis {
/// Creates genesis block for frontier network
pub fn new_frontier() -> Genesis {
let root = H256::from_str("d7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544").unwrap();
let json = base_to_json(include_bytes!("../res/genesis_frontier"));
Self::new_from_json(&json, &root)
}
/// Loads genesis block from json file
pub fn new_from_json(json: &Json, state_root: &H256) -> Genesis {
// once we commit ourselves to some json parsing library (serde?)
// move it to proper data structure
let mixhash = H256::from_str(&json["mixhash"].as_string().unwrap()[2..]).unwrap();
let parent_hash = H256::from_str(&json["parentHash"].as_string().unwrap()[2..]).unwrap();
let coinbase = Address::from_str(&json["coinbase"].as_string().unwrap()[2..]).unwrap();
let difficulty = U256::from_str(&json["difficulty"].as_string().unwrap()[2..]).unwrap();
let gas_limit = U256::from_str(&json["gasLimit"].as_string().unwrap()[2..]).unwrap();
let timestamp = U256::from_str(&json["timestamp"].as_string().unwrap()[2..]).unwrap();
let extra_data: Vec<u8> = From::from(&json["extraData"].as_string().unwrap()[2..]);
let nonce = H64::from_str(&json["nonce"].as_string().unwrap()[2..]).unwrap();
let log_bloom = H2048::new();
let number = 0u16;
let gas_used = 0u16;
let empty_list = RlpStream::new_list(0).out();
let empty_list_sha3 = empty_list.sha3();
let empty_data = encode(&"");
let empty_data_sha3 = empty_data.sha3();
let mut stream = RlpStream::new_list(3);
stream.append_list(15);
stream.append(&parent_hash);
// uncles - empty list sha3
stream.append(&empty_list_sha3);
stream.append(&coinbase);
stream.append(state_root);
// transactions
stream.append(&empty_data_sha3);
// receipts
stream.append(&empty_data_sha3);
stream.append(&log_bloom);
stream.append(&difficulty);
stream.append(&number);
stream.append(&gas_limit);
stream.append(&gas_used);
stream.append(&timestamp);
stream.append(&extra_data);
stream.append(&mixhash);
stream.append(&nonce);
stream.append_raw(&empty_list, 1);
stream.append_raw(&empty_list, 1);
let mut map = HashMap::new();
let accounts = json["alloc"].as_object().expect("Missing genesis state");
for (address, acc) in accounts.iter() {
let addr = Address::from_str(address).unwrap();
let o = acc.as_object().unwrap();
let balance = U256::from_dec_str(o["balance"].as_string().unwrap()).unwrap();
map.insert(addr, Account::new_with_balance(balance));
}
Genesis {
block: stream.out(),
state: AccountMap::new(map)
}
}
}
#[test]
fn test_genesis() {
let g = Genesis::new_default();
}

View File

@ -71,6 +71,8 @@
#[macro_use]
extern crate log;
extern crate rustc_serialize;
extern crate flate2;
extern crate ethcore_util as util;
#[cfg(feature = "jit" )]
@ -81,6 +83,7 @@ pub mod block;
pub mod blockchain;
pub mod blockheader;
pub mod transaction;
pub mod genesis;
pub mod verifiedblock;
pub mod importroute;

View File

@ -46,6 +46,11 @@ impl State {
Self::new(OverlayDB::new_temp())
}
/// Return reference to root
pub fn root(&self) -> &H256 {
&self.root
}
/// Commit everything to the disk
pub fn commit_db(&mut self) {
self.db.commit().expect("Number of kills exceeded number of inserts!");