fixed loading genesis

This commit is contained in:
debris 2015-12-17 19:26:21 +01:00
parent dbeae3ade3
commit dad010f8b1
2 changed files with 55 additions and 57 deletions

View File

@ -11,23 +11,23 @@ pub type LogBloom = H2048;
#[derive(Debug)] #[derive(Debug)]
pub struct Header { pub struct Header {
parent_hash: H256, pub parent_hash: H256,
timestamp: U256, pub timestamp: U256,
number: U256, pub number: U256,
author: Address, pub author: Address,
transactions_root: H256, pub transactions_root: H256,
uncles_hash: H256, pub uncles_hash: H256,
extra_data: Bytes, pub extra_data: Bytes,
state_root: H256, pub state_root: H256,
receipts_root: H256, pub receipts_root: H256,
log_bloom: LogBloom, pub log_bloom: LogBloom,
gas_used: U256, pub gas_used: U256,
gas_limit: U256, pub gas_limit: U256,
difficulty: U256, pub difficulty: U256,
seal: Vec<Bytes>, pub seal: Vec<Bytes>,
} }
impl Header { impl Header {

View File

@ -10,6 +10,7 @@ use util::hash::*;
use util::uint::*; use util::uint::*;
use util::sha3::*; use util::sha3::*;
use account::*; use account::*;
use blockheader::*;
/// Converts file from base64 gzipped bytes to json /// Converts file from base64 gzipped bytes to json
fn base_to_json(source: &[u8]) -> Json { fn base_to_json(source: &[u8]) -> Json {
@ -33,54 +34,54 @@ impl Genesis {
pub fn new_frontier() -> Genesis { pub fn new_frontier() -> Genesis {
let root = H256::from_str("d7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544").unwrap(); let root = H256::from_str("d7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544").unwrap();
let json = base_to_json(include_bytes!("../res/genesis_frontier")); let json = base_to_json(include_bytes!("../res/genesis_frontier"));
Self::new_from_json(&json, &root) let (header, state) = Self::load_genesis_json(&json, &root);
Self::new_from_header_and_state(header, state)
}
pub fn new_from_header_and_state(header: Header, state: HashMap<Address, Account>) -> Genesis {
let empty_list = RlpStream::new_list(0).out();
let mut stream = RlpStream::new_list(3);
stream.append(&header);
stream.append_raw(&empty_list, 1);
stream.append_raw(&empty_list, 1);
Genesis {
block: stream.out(),
state: state
}
} }
/// Loads genesis block from json file /// Loads genesis block from json file
pub fn new_from_json(json: &Json, state_root: &H256) -> Genesis { fn load_genesis_json(json: &Json, state_root: &H256) -> (Header, HashMap<Address, Account>) {
// once we commit ourselves to some json parsing library (serde?) // once we commit ourselves to some json parsing library (serde?)
// move it to proper data structure // 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> = json["extraData"].as_string().unwrap()[2..].from_hex().unwrap();
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 = RlpStream::new_list(0).out();
let empty_list_sha3 = empty_list.sha3(); let empty_list_sha3 = empty_list.sha3();
let empty_data = encode(&""); let empty_data = encode(&"");
let empty_data_sha3 = empty_data.sha3(); let empty_data_sha3 = empty_data.sha3();
let mut stream = RlpStream::new_list(3); let header = Header {
stream.append_list(15); parent_hash: H256::from_str(&json["parentHash"].as_string().unwrap()[2..]).unwrap(),
stream.append(&parent_hash); uncles_hash: empty_list_sha3.clone(),
// uncles - empty list sha3 author: Address::from_str(&json["coinbase"].as_string().unwrap()[2..]).unwrap(),
stream.append(&empty_list_sha3); state_root: state_root.clone(),
stream.append(&coinbase); transactions_root: empty_data_sha3.clone(),
stream.append(state_root); receipts_root: empty_data_sha3.clone(),
// transactions log_bloom: H2048::new(),
stream.append(&empty_data_sha3); difficulty: U256::from_str(&json["difficulty"].as_string().unwrap()[2..]).unwrap(),
// receipts number: U256::from(0u8),
stream.append(&empty_data_sha3); gas_limit: U256::from_str(&json["gasLimit"].as_string().unwrap()[2..]).unwrap(),
stream.append(&log_bloom); gas_used: U256::from(0u8),
stream.append(&difficulty); timestamp: U256::from_str(&json["timestamp"].as_string().unwrap()[2..]).unwrap(),
stream.append(&number); extra_data: json["extraData"].as_string().unwrap()[2..].from_hex().unwrap(),
stream.append(&gas_limit); seal: {
stream.append(&gas_used); let mixhash = H256::from_str(&json["mixhash"].as_string().unwrap()[2..]).unwrap();
stream.append(&timestamp); let nonce = H64::from_str(&json["nonce"].as_string().unwrap()[2..]).unwrap();
stream.append(&extra_data); vec![mixhash.to_vec(), nonce.to_vec()]
stream.append(&mixhash); }
stream.append(&nonce); };
stream.append_raw(&empty_list, 1);
stream.append_raw(&empty_list, 1);
let mut state = HashMap::new(); let mut state = HashMap::new();
let accounts = json["alloc"].as_object().expect("Missing genesis state"); let accounts = json["alloc"].as_object().expect("Missing genesis state");
for (address, acc) in accounts.iter() { for (address, acc) in accounts.iter() {
@ -90,10 +91,7 @@ impl Genesis {
state.insert(addr, Account::new_basic(balance)); state.insert(addr, Account::new_basic(balance));
} }
Genesis { (header, state)
block: stream.out(),
state: state
}
} }
/// Returns genesis block /// Returns genesis block