genesis_state in progress

This commit is contained in:
debris 2015-12-11 14:41:18 +01:00
parent 0b29ac79c5
commit 04b6743ba8
4 changed files with 71 additions and 4 deletions

25
src/account.rs Normal file
View File

@ -0,0 +1,25 @@
use std::collections::HashMap;
use util::uint::*;
use util::hash::*;
pub struct Account {
balance: U256,
code: Vec<u8>,
nonce: U256,
storage: HashMap<U256, U256>
}
impl Account {
pub fn balance(&self) -> &U256 { &self.balance }
pub fn code(&self) -> &[u8] { &self.code }
pub fn nonce(&self) -> &U256 { &self.nonce }
pub fn storage(&self) -> &HashMap<U256, U256> { &self.storage }
}
pub struct AccountMap {
accounts: HashMap<Address, Account>
}
impl AccountMap {
pub fn accounts(&self) -> &HashMap<Address, Account> { &self.accounts }
}

View File

@ -7,20 +7,23 @@ use blockheader::*;
use block::*; use block::*;
use verifiedblock::*; use verifiedblock::*;
use importroute::*; use importroute::*;
use account::*;
pub struct BlockChain { pub struct BlockChain {
genesis_block: Vec<u8>, genesis_block: Vec<u8>,
genesis_hash: H256 genesis_hash: H256,
genesis_state: AccountMap
} }
impl BlockChain { impl BlockChain {
pub fn new(genesis_block: Vec<u8>) -> BlockChain { pub fn new(genesis_block: Vec<u8>, genesis_state: AccountMap) -> BlockChain {
// consider creating `GenesisView` for genesis block RLP // consider creating `GenesisView` for genesis block RLP
let genesis_hash = BlockView::new(&genesis_block).parent_hash().sha3(); let genesis_hash = BlockView::new(&genesis_block).parent_hash().sha3();
BlockChain { BlockChain {
genesis_block: genesis_block, genesis_block: genesis_block,
genesis_hash: genesis_hash genesis_hash: genesis_hash,
genesis_state: genesis_state
} }
} }
@ -32,7 +35,7 @@ impl BlockChain {
} }
let mut block = Block::new(db.clone()); let mut block = Block::new(db.clone());
// TODO: commit genesis state (accounts) to block.state block.mutable_state().insert_accounts(&self.genesis_state);
block.mutable_state().commit_db(); block.mutable_state().commit_db();
// TODO: set previous block // TODO: set previous block
// TODO: reset current // TODO: reset current

View File

@ -76,6 +76,7 @@ extern crate ethcore_util as util;
#[cfg(feature = "jit" )] #[cfg(feature = "jit" )]
extern crate evmjit; extern crate evmjit;
pub mod account;
pub mod block; pub mod block;
pub mod blockchain; pub mod blockchain;
pub mod blockheader; pub mod blockheader;

View File

@ -1,7 +1,12 @@
use util::hash::*; use util::hash::*;
use util::hashdb::*; use util::hashdb::*;
use util::memorydb::*;
use util::overlaydb::*; use util::overlaydb::*;
use util::bytes::*;
use util::trie::*; use util::trie::*;
use util::rlp::*;
use util::sha3::*;
use account::*;
pub struct State { pub struct State {
db: OverlayDB, db: OverlayDB,
@ -45,4 +50,37 @@ impl State {
pub fn commit_db(&mut self) { pub fn commit_db(&mut self) {
self.db.commit().expect("Number of kills exceeded number of inserts!"); self.db.commit().expect("Number of kills exceeded number of inserts!");
} }
/// Commit accounts to TrieDB. This is simplified version of
/// cpp-ethereum's dev::eth::commit.
pub fn insert_accounts(&mut self, map: &AccountMap) {
let mut trie = TrieDB::new_existing(&mut self.db, &mut self.root);
for (address, account) in map.accounts().iter() {
let mut stream = RlpStream::new_list(4);
stream.append(account.nonce());
stream.append(account.balance());
let mut root = H256::new();
{
let mut db = MemoryDB::new();
let mut t = TrieDB::new(&mut db, &mut root);
for (k, v) in account.storage().iter() {
// cast key and value to trait type,
// so we can call overloaded `to_bytes` method
let kas: &ToBytes = k;
let vas: &ToBytes = v;
t.insert(&kas.to_bytes(), &vas.to_bytes());
}
}
stream.append(&root);
let code_hash = account.code().sha3();
stream.append(&code_hash);
if account.code().len() > 0 {
trie.insert(&code_hash, account.code());
}
trie.insert(address, &stream.out());
}
}
} }