Merge branch 'master' of github.com:gavofyork/ethcore into network
This commit is contained in:
commit
48abca6669
4
.gitignore
vendored
4
.gitignore
vendored
@ -12,4 +12,8 @@ Cargo.lock
|
||||
# Generated by Cargo
|
||||
/target/
|
||||
|
||||
# vim stuff
|
||||
*.swp
|
||||
|
||||
# mac stuff
|
||||
.DS_Store
|
||||
|
@ -10,8 +10,12 @@ authors = ["Ethcore <admin@ethcore.io>"]
|
||||
log = "0.3"
|
||||
env_logger = "0.3"
|
||||
ethcore-util = { path = "../ethcore-util" }
|
||||
evmjit = { path = "rust-evmjit", optional = true }
|
||||
rustc-serialize = "0.3"
|
||||
flate2 = "0.2"
|
||||
rocksdb = "0.2.1"
|
||||
heapsize = "0.2.0"
|
||||
|
||||
evmjit = { path = "rust-evmjit", optional = true }
|
||||
|
||||
[features]
|
||||
jit = ["evmjit"]
|
||||
|
1
res/genesis_frontier
Normal file
1
res/genesis_frontier
Normal file
File diff suppressed because one or more lines are too long
@ -6,6 +6,7 @@ use util::bytes::*;
|
||||
use util::trie::*;
|
||||
use util::rlp::*;
|
||||
use util::uint::*;
|
||||
use std::cell::*;
|
||||
|
||||
pub const SHA3_EMPTY: H256 = H256( [0xc5, 0xd2, 0x46, 0x01, 0x86, 0xf7, 0x23, 0x3c, 0x92, 0x7e, 0x7d, 0xb2, 0xdc, 0xc7, 0x03, 0xc0, 0xe5, 0x00, 0xb6, 0x53, 0xca, 0x82, 0x27, 0x3b, 0x7b, 0xfa, 0xd8, 0x04, 0x5d, 0x85, 0xa4, 0x70] );
|
||||
|
||||
@ -19,7 +20,7 @@ pub struct Account {
|
||||
// Trie-backed storage.
|
||||
storage_root: H256,
|
||||
// Overlay on trie-backed storage.
|
||||
storage_overlay: HashMap<H256, H256>,
|
||||
storage_overlay: RefCell<HashMap<H256, H256>>,
|
||||
// Code hash of the account. If None, means that it's a contract whose code has not yet been set.
|
||||
code_hash: Option<H256>,
|
||||
// Code cache of the account.
|
||||
@ -33,19 +34,19 @@ impl Account {
|
||||
balance: balance,
|
||||
nonce: nonce,
|
||||
storage_root: SHA3_NULL_RLP,
|
||||
storage_overlay: storage,
|
||||
storage_overlay: RefCell::new(storage),
|
||||
code_hash: Some(code.sha3()),
|
||||
code_cache: code
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new account with the given balance.
|
||||
pub fn new_basic(balance: U256) -> Account {
|
||||
pub fn new_basic(balance: U256, nonce: U256) -> Account {
|
||||
Account {
|
||||
balance: balance,
|
||||
nonce: U256::from(0u8),
|
||||
nonce: nonce,
|
||||
storage_root: SHA3_NULL_RLP,
|
||||
storage_overlay: HashMap::new(),
|
||||
storage_overlay: RefCell::new(HashMap::new()),
|
||||
code_hash: Some(SHA3_EMPTY),
|
||||
code_cache: vec![],
|
||||
}
|
||||
@ -58,7 +59,7 @@ impl Account {
|
||||
nonce: r.val_at(0),
|
||||
balance: r.val_at(1),
|
||||
storage_root: r.val_at(2),
|
||||
storage_overlay: HashMap::new(),
|
||||
storage_overlay: RefCell::new(HashMap::new()),
|
||||
code_hash: Some(r.val_at(3)),
|
||||
code_cache: vec![],
|
||||
}
|
||||
@ -71,7 +72,7 @@ impl Account {
|
||||
balance: balance,
|
||||
nonce: U256::from(0u8),
|
||||
storage_root: SHA3_NULL_RLP,
|
||||
storage_overlay: HashMap::new(),
|
||||
storage_overlay: RefCell::new(HashMap::new()),
|
||||
code_hash: None,
|
||||
code_cache: vec![],
|
||||
}
|
||||
@ -86,20 +87,14 @@ impl Account {
|
||||
|
||||
/// Set (and cache) the contents of the trie's storage at `key` to `value`.
|
||||
pub fn set_storage(&mut self, key: H256, value: H256) {
|
||||
self.storage_overlay.insert(key, value);
|
||||
self.storage_overlay.borrow_mut().insert(key, value);
|
||||
}
|
||||
|
||||
/// Get (and cache) the contents of the trie's storage at `key`.
|
||||
pub fn storage_at(&mut self, db: &mut HashDB, key: H256) -> H256 {
|
||||
match self.storage_overlay.get(&key) {
|
||||
Some(x) => { return x.clone() },
|
||||
_ => {}
|
||||
}
|
||||
// fetch - cannot be done in match because of the borrow rules.
|
||||
let t = TrieDBMut::new_existing(db, &mut self.storage_root);
|
||||
let r = H256::from_slice(t.get(key.bytes()).unwrap_or(&[0u8;32][..]));
|
||||
self.storage_overlay.insert(key, r.clone());
|
||||
r
|
||||
pub fn storage_at(&self, db: &HashDB, key: &H256) -> H256 {
|
||||
self.storage_overlay.borrow_mut().entry(key.clone()).or_insert_with(||{
|
||||
H256::from_slice(TrieDB::new(db, &self.storage_root).get(key.bytes()).unwrap_or(&[0u8;32][..]))
|
||||
}).clone()
|
||||
}
|
||||
|
||||
/// return the balance associated with this account.
|
||||
@ -119,6 +114,7 @@ impl Account {
|
||||
match self.code_hash {
|
||||
Some(SHA3_EMPTY) | None if self.code_cache.is_empty() => Some(&self.code_cache),
|
||||
Some(_) if !self.code_cache.is_empty() => Some(&self.code_cache),
|
||||
None => Some(&self.code_cache),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@ -161,10 +157,10 @@ impl Account {
|
||||
pub fn base_root(&self) -> &H256 { &self.storage_root }
|
||||
|
||||
/// return the storage root associated with this account or None if it has been altered via the overlay.
|
||||
pub fn storage_root(&self) -> Option<&H256> { if self.storage_overlay.is_empty() {Some(&self.storage_root)} else {None} }
|
||||
pub fn storage_root(&self) -> Option<&H256> { if self.storage_overlay.borrow().is_empty() {Some(&self.storage_root)} else {None} }
|
||||
|
||||
/// rturn the storage overlay.
|
||||
pub fn storage_overlay(&self) -> &HashMap<H256, H256> { &self.storage_overlay }
|
||||
pub fn storage_overlay(&self) -> Ref<HashMap<H256, H256>> { self.storage_overlay.borrow() }
|
||||
|
||||
/// Increment the nonce of the account by one.
|
||||
pub fn inc_nonce(&mut self) { self.nonce = self.nonce + U256::from(1u8); }
|
||||
@ -178,19 +174,23 @@ impl Account {
|
||||
/// Commit the `storage_overlay` to the backing DB and update `storage_root`.
|
||||
pub fn commit_storage(&mut self, db: &mut HashDB) {
|
||||
let mut t = TrieDBMut::new(db, &mut self.storage_root);
|
||||
for (k, v) in self.storage_overlay.iter() {
|
||||
for (k, v) in self.storage_overlay.borrow().iter() {
|
||||
// cast key and value to trait type,
|
||||
// so we can call overloaded `to_bytes` method
|
||||
t.insert(k, v);
|
||||
}
|
||||
self.storage_overlay.clear();
|
||||
self.storage_overlay.borrow_mut().clear();
|
||||
}
|
||||
|
||||
/// Commit any unsaved code. `code_hash` will always return the hash of the `code_cache` after this.
|
||||
pub fn commit_code(&mut self, db: &mut HashDB) {
|
||||
println!("Commiting code of {:?} - {:?}, {:?}", self, self.code_hash.is_none(), self.code_cache.is_empty());
|
||||
match (self.code_hash.is_none(), self.code_cache.is_empty()) {
|
||||
(true, true) => self.code_hash = Some(self.code_cache.sha3()),
|
||||
(true, false) => self.code_hash = Some(db.insert(&self.code_cache)),
|
||||
(true, true) => self.code_hash = Some(SHA3_EMPTY),
|
||||
(true, false) => {
|
||||
println!("Writing into DB {:?}", self.code_cache);
|
||||
self.code_hash = Some(db.insert(&self.code_cache));
|
||||
},
|
||||
(false, _) => {},
|
||||
}
|
||||
}
|
||||
@ -213,7 +213,6 @@ use super::*;
|
||||
use std::collections::HashMap;
|
||||
use util::hash::*;
|
||||
use util::bytes::*;
|
||||
use util::trie::*;
|
||||
use util::rlp::*;
|
||||
use util::uint::*;
|
||||
use util::overlaydb::*;
|
||||
@ -230,10 +229,10 @@ fn storage_at() {
|
||||
a.rlp()
|
||||
};
|
||||
|
||||
let mut a = Account::from_rlp(&rlp);
|
||||
let a = Account::from_rlp(&rlp);
|
||||
assert_eq!(a.storage_root().unwrap().hex(), "3541f181d6dad5c504371884684d08c29a8bad04926f8ceddf5e279dbc3cc769");
|
||||
assert_eq!(a.storage_at(&mut db, H256::from(&U256::from(0x00u64))), H256::from(&U256::from(0x1234u64)));
|
||||
assert_eq!(a.storage_at(&mut db, H256::from(&U256::from(0x01u64))), H256::new());
|
||||
assert_eq!(a.storage_at(&mut db, &H256::from(&U256::from(0x00u64))), H256::from(&U256::from(0x1234u64)));
|
||||
assert_eq!(a.storage_at(&mut db, &H256::from(&U256::from(0x01u64))), H256::new());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
681
src/blockchain.rs
Normal file
681
src/blockchain.rs
Normal file
@ -0,0 +1,681 @@
|
||||
//! Fast access to blockchain data.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::cell::RefCell;
|
||||
use std::path::Path;
|
||||
use std::hash::Hash;
|
||||
use rocksdb::{DB, WriteBatch, Writable};
|
||||
use heapsize::HeapSizeOf;
|
||||
use util::hash::*;
|
||||
use util::uint::*;
|
||||
use util::rlp::*;
|
||||
use util::hashdb::*;
|
||||
use util::sha3::*;
|
||||
use util::bytes::*;
|
||||
use util::squeeze::*;
|
||||
use header::*;
|
||||
use extras::*;
|
||||
use transaction::*;
|
||||
use views::*;
|
||||
|
||||
/// Represents a tree route between `from` block and `to` block:
|
||||
///
|
||||
/// - `blocks` - a vector of hashes of all blocks, ordered from `from` to `to`.
|
||||
///
|
||||
/// - `ancestor` - best common ancestor of these blocks.
|
||||
///
|
||||
/// - `index` - an index where best common ancestor would be.
|
||||
pub struct TreeRoute {
|
||||
pub blocks: Vec<H256>,
|
||||
pub ancestor: H256,
|
||||
pub index: usize
|
||||
}
|
||||
|
||||
/// Represents blockchain's in-memory cache size in bytes.
|
||||
#[derive(Debug)]
|
||||
pub struct CacheSize {
|
||||
pub blocks: usize,
|
||||
pub block_details: usize,
|
||||
pub transaction_addresses: usize,
|
||||
pub block_logs: usize,
|
||||
pub blocks_blooms: usize
|
||||
}
|
||||
|
||||
/// Information about best block gathered together
|
||||
struct BestBlock {
|
||||
pub hash: H256,
|
||||
pub number: U256,
|
||||
pub total_difficulty: U256
|
||||
}
|
||||
|
||||
impl BestBlock {
|
||||
fn new() -> BestBlock {
|
||||
BestBlock {
|
||||
hash: H256::new(),
|
||||
number: U256::from(0),
|
||||
total_difficulty: U256::from(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Structure providing fast access to blockchain data.
|
||||
///
|
||||
/// **Does not do input data verification.**
|
||||
pub struct BlockChain {
|
||||
best_block: RefCell<BestBlock>,
|
||||
|
||||
// block cache
|
||||
blocks: RefCell<HashMap<H256, Bytes>>,
|
||||
|
||||
// extra caches
|
||||
block_details: RefCell<HashMap<H256, BlockDetails>>,
|
||||
block_hashes: RefCell<HashMap<U256, H256>>,
|
||||
transaction_addresses: RefCell<HashMap<H256, TransactionAddress>>,
|
||||
block_logs: RefCell<HashMap<H256, BlockLogBlooms>>,
|
||||
blocks_blooms: RefCell<HashMap<H256, BlocksBlooms>>,
|
||||
|
||||
extras_db: DB,
|
||||
blocks_db: DB
|
||||
}
|
||||
|
||||
impl BlockChain {
|
||||
/// Create new instance of blockchain from given Genesis
|
||||
///
|
||||
/// ```rust
|
||||
/// extern crate ethcore_util as util;
|
||||
/// extern crate ethcore;
|
||||
/// use std::env;
|
||||
/// use std::str::FromStr;
|
||||
/// use ethcore::genesis::*;
|
||||
/// use ethcore::blockchain::*;
|
||||
/// use util::hash::*;
|
||||
/// use util::uint::*;
|
||||
///
|
||||
/// fn main() {
|
||||
/// let genesis = Genesis::new_frontier();
|
||||
///
|
||||
/// let mut dir = env::temp_dir();
|
||||
/// dir.push(H32::random().hex());
|
||||
///
|
||||
/// let bc = BlockChain::new(genesis.block(), &dir);
|
||||
///
|
||||
/// let genesis_hash = "d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3";
|
||||
/// assert_eq!(bc.genesis_hash(), H256::from_str(genesis_hash).unwrap());
|
||||
/// assert!(bc.is_known(&bc.genesis_hash()));
|
||||
/// assert_eq!(bc.genesis_hash(), bc.block_hash(&U256::from(0u8)).unwrap());
|
||||
/// }
|
||||
/// ```
|
||||
pub fn new(genesis: &[u8], path: &Path) -> BlockChain {
|
||||
// open extras db
|
||||
let mut extras_path = path.to_path_buf();
|
||||
extras_path.push("extras");
|
||||
let extras_db = DB::open_default(extras_path.to_str().unwrap()).unwrap();
|
||||
|
||||
// open blocks db
|
||||
let mut blocks_path = path.to_path_buf();
|
||||
blocks_path.push("blocks");
|
||||
let blocks_db = DB::open_default(blocks_path.to_str().unwrap()).unwrap();
|
||||
|
||||
let bc = BlockChain {
|
||||
best_block: RefCell::new(BestBlock::new()),
|
||||
blocks: RefCell::new(HashMap::new()),
|
||||
block_details: RefCell::new(HashMap::new()),
|
||||
block_hashes: RefCell::new(HashMap::new()),
|
||||
transaction_addresses: RefCell::new(HashMap::new()),
|
||||
block_logs: RefCell::new(HashMap::new()),
|
||||
blocks_blooms: RefCell::new(HashMap::new()),
|
||||
extras_db: extras_db,
|
||||
blocks_db: blocks_db
|
||||
};
|
||||
|
||||
// load best block
|
||||
let best_block_hash = match bc.extras_db.get(b"best").unwrap() {
|
||||
Some(best) => H256::from_slice(&best),
|
||||
None => {
|
||||
// best block does not exist
|
||||
// we need to insert genesis into the cache
|
||||
let block = BlockView::new(genesis);
|
||||
let header = block.header_view();
|
||||
let hash = block.sha3();
|
||||
|
||||
let details = BlockDetails {
|
||||
number: header.number(),
|
||||
total_difficulty: header.difficulty(),
|
||||
parent: header.parent_hash(),
|
||||
children: vec![]
|
||||
};
|
||||
|
||||
bc.blocks_db.put(&hash, genesis).unwrap();
|
||||
|
||||
let batch = WriteBatch::new();
|
||||
batch.put_extras(&hash, &details);
|
||||
batch.put_extras(&header.number(), &hash);
|
||||
batch.put(b"best", &hash).unwrap();
|
||||
bc.extras_db.write(batch).unwrap();
|
||||
|
||||
hash
|
||||
}
|
||||
};
|
||||
|
||||
{
|
||||
let mut best_block = bc.best_block.borrow_mut();
|
||||
best_block.number = bc.block_number(&best_block_hash).unwrap();
|
||||
best_block.total_difficulty = bc.block_details(&best_block_hash).unwrap().total_difficulty;
|
||||
best_block.hash = best_block_hash;
|
||||
}
|
||||
|
||||
bc
|
||||
}
|
||||
|
||||
/// Returns a tree route between `from` and `to`, which is a tuple of:
|
||||
///
|
||||
/// - a vector of hashes of all blocks, ordered from `from` to `to`.
|
||||
///
|
||||
/// - common ancestor of these blocks.
|
||||
///
|
||||
/// - an index where best common ancestor would be
|
||||
///
|
||||
/// 1.) from newer to older
|
||||
///
|
||||
/// - bc: `A1 -> A2 -> A3 -> A4 -> A5`
|
||||
/// - from: A5, to: A4
|
||||
/// - route:
|
||||
///
|
||||
/// ```json
|
||||
/// { blocks: [A5], ancestor: A4, index: 1 }
|
||||
/// ```
|
||||
///
|
||||
/// 2.) from older to newer
|
||||
///
|
||||
/// - bc: `A1 -> A2 -> A3 -> A4 -> A5`
|
||||
/// - from: A3, to: A4
|
||||
/// - route:
|
||||
///
|
||||
/// ```json
|
||||
/// { blocks: [A4], ancestor: A3, index: 0 }
|
||||
/// ```
|
||||
///
|
||||
/// 3.) fork:
|
||||
///
|
||||
/// - bc:
|
||||
///
|
||||
/// ```text
|
||||
/// A1 -> A2 -> A3 -> A4
|
||||
/// -> B3 -> B4
|
||||
/// ```
|
||||
/// - from: B4, to: A4
|
||||
/// - route:
|
||||
///
|
||||
/// ```json
|
||||
/// { blocks: [B4, B3, A3, A4], ancestor: A2, index: 2 }
|
||||
/// ```
|
||||
pub fn tree_route(&self, from: H256, to: H256) -> TreeRoute {
|
||||
let from_details = self.block_details(&from).expect("from hash is invalid!");
|
||||
let to_details = self.block_details(&to).expect("to hash is invalid!");
|
||||
|
||||
self._tree_route((from_details, from), (to_details, to))
|
||||
}
|
||||
|
||||
/// Similar to `tree_route` function, but can be used to return a route
|
||||
/// between blocks which may not be in database yet.
|
||||
fn _tree_route(&self, from: (BlockDetails, H256), to: (BlockDetails, H256)) -> TreeRoute {
|
||||
let mut from_branch = vec![];
|
||||
let mut to_branch = vec![];
|
||||
|
||||
let mut from_details = from.0;
|
||||
let mut to_details = to.0;
|
||||
let mut current_from = from.1;
|
||||
let mut current_to = to.1;
|
||||
|
||||
// reset from && to to the same level
|
||||
while from_details.number > to_details.number {
|
||||
from_branch.push(current_from);
|
||||
current_from = from_details.parent.clone();
|
||||
from_details = self.block_details(&from_details.parent).unwrap();
|
||||
}
|
||||
|
||||
while to_details.number > from_details.number {
|
||||
to_branch.push(current_to);
|
||||
current_to = to_details.parent.clone();
|
||||
to_details = self.block_details(&to_details.parent).unwrap();
|
||||
}
|
||||
|
||||
assert_eq!(from_details.number, to_details.number);
|
||||
|
||||
// move to shared parent
|
||||
while current_from != current_to {
|
||||
from_branch.push(current_from);
|
||||
current_from = from_details.parent.clone();
|
||||
from_details = self.block_details(&from_details.parent).unwrap();
|
||||
|
||||
to_branch.push(current_to);
|
||||
current_to = to_details.parent.clone();
|
||||
to_details = self.block_details(&to_details.parent).unwrap();
|
||||
}
|
||||
|
||||
let index = from_branch.len();
|
||||
|
||||
from_branch.extend(to_branch.into_iter().rev());
|
||||
|
||||
TreeRoute {
|
||||
blocks: from_branch,
|
||||
ancestor: current_from,
|
||||
index: index
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Inserts the block into backing cache database.
|
||||
/// Expects the block to be valid and already verified.
|
||||
/// If the block is already known, does nothing.
|
||||
pub fn insert_block(&self, bytes: &[u8]) {
|
||||
// create views onto rlp
|
||||
let block = BlockView::new(bytes);
|
||||
let header = block.header_view();
|
||||
let hash = block.sha3();
|
||||
|
||||
if self.is_known(&hash) {
|
||||
return;
|
||||
}
|
||||
|
||||
// store block in db
|
||||
self.blocks_db.put(&hash, &bytes).unwrap();
|
||||
let (batch, new_best) = self.block_to_extras_insert_batch(bytes);
|
||||
|
||||
// update best block
|
||||
let mut best_block = self.best_block.borrow_mut();
|
||||
if let Some(b) = new_best {
|
||||
*best_block = b;
|
||||
}
|
||||
|
||||
// update caches
|
||||
let mut write = self.block_details.borrow_mut();
|
||||
write.remove(&header.parent_hash());
|
||||
|
||||
// update extras database
|
||||
self.extras_db.write(batch).unwrap();
|
||||
}
|
||||
|
||||
/// Transforms block into WriteBatch that may be written into database
|
||||
/// Additionally, if it's new best block it returns new best block object.
|
||||
fn block_to_extras_insert_batch(&self, bytes: &[u8]) -> (WriteBatch, Option<BestBlock>) {
|
||||
// create views onto rlp
|
||||
let block = BlockView::new(bytes);
|
||||
let header = block.header_view();
|
||||
|
||||
// prepare variables
|
||||
let hash = block.sha3();
|
||||
let mut parent_details = self.block_details(&header.parent_hash()).expect("Invalid parent hash.");
|
||||
let total_difficulty = parent_details.total_difficulty + header.difficulty();
|
||||
let is_new_best = total_difficulty > self.best_block_total_difficulty();
|
||||
let parent_hash = header.parent_hash();
|
||||
|
||||
// create current block details
|
||||
let details = BlockDetails {
|
||||
number: header.number(),
|
||||
total_difficulty: total_difficulty,
|
||||
parent: parent_hash.clone(),
|
||||
children: vec![]
|
||||
};
|
||||
|
||||
// prepare the batch
|
||||
let batch = WriteBatch::new();
|
||||
|
||||
// insert new block details
|
||||
batch.put_extras(&hash, &details);
|
||||
|
||||
// update parent details
|
||||
parent_details.children.push(hash.clone());
|
||||
batch.put_extras(&parent_hash, &parent_details);
|
||||
|
||||
// if it's not new best block, just return
|
||||
if !is_new_best {
|
||||
return (batch, None);
|
||||
}
|
||||
|
||||
// if its new best block we need to make sure that all ancestors
|
||||
// are moved to "canon chain"
|
||||
// find the route between old best block and the new one
|
||||
let best_hash = self.best_block_hash();
|
||||
let best_details = self.block_details(&best_hash).expect("best block hash is invalid!");
|
||||
let route = self._tree_route((best_details, best_hash), (details, hash.clone()));
|
||||
|
||||
match route.blocks.len() {
|
||||
// its our parent
|
||||
1 => batch.put_extras(&header.number(), &hash),
|
||||
// it is a fork
|
||||
i if i > 1 => {
|
||||
let ancestor_number = self.block_number(&route.ancestor).unwrap();
|
||||
let start_number = ancestor_number + U256::from(1u8);
|
||||
for (index, hash) in route.blocks.iter().skip(route.index).enumerate() {
|
||||
batch.put_extras(&(start_number + U256::from(index as u64)), hash);
|
||||
}
|
||||
},
|
||||
// route.blocks.len() could be 0 only if inserted block is best block,
|
||||
// and this is not possible at this stage
|
||||
_ => { unreachable!(); }
|
||||
};
|
||||
|
||||
// this is new best block
|
||||
batch.put(b"best", &hash).unwrap();
|
||||
|
||||
let best_block = BestBlock {
|
||||
hash: hash,
|
||||
number: header.number(),
|
||||
total_difficulty: total_difficulty
|
||||
};
|
||||
|
||||
(batch, Some(best_block))
|
||||
}
|
||||
|
||||
/// Returns true if the given block is known
|
||||
/// (though not necessarily a part of the canon chain).
|
||||
pub fn is_known(&self, hash: &H256) -> bool {
|
||||
self.query_extras_exist(hash, &self.block_details)
|
||||
}
|
||||
|
||||
/// Returns true if transaction is known.
|
||||
pub fn is_known_transaction(&self, hash: &H256) -> bool {
|
||||
self.query_extras_exist(hash, &self.transaction_addresses)
|
||||
}
|
||||
|
||||
/// Returns reference to genesis hash.
|
||||
pub fn genesis_hash(&self) -> H256 {
|
||||
self.block_hash(&U256::from(0u8)).expect("Genesis hash should always exist")
|
||||
}
|
||||
|
||||
/// Get the partial-header of a block.
|
||||
pub fn block_header(&self, hash: &H256) -> Option<Header> {
|
||||
self.block(hash).map(|bytes| BlockView::new(&bytes).header())
|
||||
}
|
||||
|
||||
/// Get a list of transactions for a given block.
|
||||
/// Returns None if block deos not exist.
|
||||
pub fn transactions(&self, hash: &H256) -> Option<Vec<Transaction>> {
|
||||
self.block(hash).map(|bytes| BlockView::new(&bytes).transactions())
|
||||
}
|
||||
|
||||
/// Get a list of transaction hashes for a given block.
|
||||
/// Returns None if block does not exist.
|
||||
pub fn transaction_hashes(&self, hash: &H256) -> Option<Vec<H256>> {
|
||||
self.block(hash).map(|bytes| BlockView::new(&bytes).transaction_hashes())
|
||||
}
|
||||
|
||||
/// Get a list of uncles for a given block.
|
||||
/// Returns None if block deos not exist.
|
||||
pub fn uncles(&self, hash: &H256) -> Option<Vec<Header>> {
|
||||
self.block(hash).map(|bytes| BlockView::new(&bytes).uncles())
|
||||
}
|
||||
|
||||
/// Get a list of uncle hashes for a given block.
|
||||
/// Returns None if block does not exist.
|
||||
pub fn uncle_hashes(&self, hash: &H256) -> Option<Vec<H256>> {
|
||||
self.block(hash).map(|bytes| BlockView::new(&bytes).uncle_hashes())
|
||||
}
|
||||
|
||||
/// Get the familial details concerning a block.
|
||||
pub fn block_details(&self, hash: &H256) -> Option<BlockDetails> {
|
||||
self.query_extras(hash, &self.block_details)
|
||||
}
|
||||
|
||||
/// Get the hash of given block's number.
|
||||
pub fn block_hash(&self, hash: &U256) -> Option<H256> {
|
||||
self.query_extras(hash, &self.block_hashes)
|
||||
}
|
||||
|
||||
/// Get best block hash.
|
||||
pub fn best_block_hash(&self) -> H256 {
|
||||
self.best_block.borrow().hash.clone()
|
||||
}
|
||||
|
||||
/// Get best block number.
|
||||
pub fn best_block_number(&self) -> U256 {
|
||||
self.best_block.borrow().number
|
||||
}
|
||||
|
||||
/// Get best block total difficulty.
|
||||
pub fn best_block_total_difficulty(&self) -> U256 {
|
||||
self.best_block.borrow().total_difficulty
|
||||
}
|
||||
|
||||
/// Get the number of given block's hash.
|
||||
pub fn block_number(&self, hash: &H256) -> Option<U256> {
|
||||
self.block(hash).map(|bytes| BlockView::new(&bytes).header_view().number())
|
||||
}
|
||||
|
||||
/// Get the transactions' log blooms of a block.
|
||||
pub fn log_blooms(&self, hash: &H256) -> Option<BlockLogBlooms> {
|
||||
self.query_extras(hash, &self.block_logs)
|
||||
}
|
||||
|
||||
fn block(&self, hash: &H256) -> Option<Bytes> {
|
||||
{
|
||||
let read = self.blocks.borrow();
|
||||
match read.get(hash) {
|
||||
Some(v) => return Some(v.clone()),
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
|
||||
let opt = self.blocks_db.get(hash)
|
||||
.expect("Low level database error. Some issue with disk?");
|
||||
|
||||
match opt {
|
||||
Some(b) => {
|
||||
let bytes: Bytes = b.to_vec();
|
||||
let mut write = self.blocks.borrow_mut();
|
||||
write.insert(hash.clone(), bytes.clone());
|
||||
Some(bytes)
|
||||
},
|
||||
None => None
|
||||
}
|
||||
}
|
||||
|
||||
fn query_extras<K, T>(&self, hash: &K, cache: &RefCell<HashMap<K, T>>) -> Option<T> where
|
||||
T: Clone + Decodable + ExtrasIndexable,
|
||||
K: ExtrasSliceConvertable + Eq + Hash + Clone {
|
||||
{
|
||||
let read = cache.borrow();
|
||||
match read.get(hash) {
|
||||
Some(v) => return Some(v.clone()),
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
|
||||
self.extras_db.get_extras(hash).map(| t: T | {
|
||||
let mut write = cache.borrow_mut();
|
||||
write.insert(hash.clone(), t.clone());
|
||||
t
|
||||
})
|
||||
}
|
||||
|
||||
fn query_extras_exist<K, T>(&self, hash: &K, cache: &RefCell<HashMap<K, T>>) -> bool where
|
||||
K: ExtrasSliceConvertable + Eq + Hash + Clone,
|
||||
T: ExtrasIndexable {
|
||||
{
|
||||
let read = cache.borrow();
|
||||
match read.get(hash) {
|
||||
Some(_) => return true,
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
|
||||
self.extras_db.extras_exists::<_, T>(hash)
|
||||
}
|
||||
|
||||
/// Get current cache size.
|
||||
pub fn cache_size(&self) -> CacheSize {
|
||||
CacheSize {
|
||||
blocks: self.blocks.heap_size_of_children(),
|
||||
block_details: self.block_details.heap_size_of_children(),
|
||||
transaction_addresses: self.transaction_addresses.heap_size_of_children(),
|
||||
block_logs: self.block_logs.heap_size_of_children(),
|
||||
blocks_blooms: self.blocks_blooms.heap_size_of_children()
|
||||
}
|
||||
}
|
||||
|
||||
/// Tries to squeeze the cache if its too big.
|
||||
pub fn squeeze_to_fit(&self, size: CacheSize) {
|
||||
self.blocks.borrow_mut().squeeze(size.blocks);
|
||||
self.block_details.borrow_mut().squeeze(size.block_details);
|
||||
self.transaction_addresses.borrow_mut().squeeze(size.transaction_addresses);
|
||||
self.block_logs.borrow_mut().squeeze(size.block_logs);
|
||||
self.blocks_blooms.borrow_mut().squeeze(size.blocks_blooms);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::env;
|
||||
use std::str::FromStr;
|
||||
use rustc_serialize::hex::FromHex;
|
||||
use util::hash::*;
|
||||
use util::uint::*;
|
||||
use blockchain::*;
|
||||
|
||||
#[test]
|
||||
fn valid_tests_extra32() {
|
||||
let genesis = "f901fcf901f7a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0925002c3260b44e44c3edebad1cc442142b03020209df1ab8bb86752edbd2cd7a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000080832fefd8808454c98c8142a0363659b251bf8b819179874c8cce7b9b983d7f3704cbb58a3b334431f7032871889032d09c281e1236c0c0".from_hex().unwrap();
|
||||
|
||||
let mut dir = env::temp_dir();
|
||||
dir.push(H32::random().hex());
|
||||
|
||||
let bc = BlockChain::new(&genesis, &dir);
|
||||
|
||||
let genesis_hash = H256::from_str("3caa2203f3d7c136c0295ed128a7d31cea520b1ca5e27afe17d0853331798942").unwrap();
|
||||
|
||||
assert_eq!(bc.genesis_hash(), genesis_hash.clone());
|
||||
assert_eq!(bc.best_block_number(), U256::from(0u8));
|
||||
assert_eq!(bc.best_block_hash(), genesis_hash.clone());
|
||||
assert_eq!(bc.block_hash(&U256::from(0u8)), Some(genesis_hash.clone()));
|
||||
assert_eq!(bc.block_hash(&U256::from(1u8)), None);
|
||||
|
||||
|
||||
let first = "f90285f90219a03caa2203f3d7c136c0295ed128a7d31cea520b1ca5e27afe17d0853331798942a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0bac6177a79e910c98d86ec31a09ae37ac2de15b754fd7bed1ba52362c49416bfa0d45893a296c1490a978e0bd321b5f2635d8280365c1fe9f693d65f233e791344a0c7778a7376099ee2e5c455791c1885b5c361b95713fddcbe32d97fd01334d296b90100000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000400000000000000000000000000000000000000000000000000000008302000001832fefd882560b845627cb99a00102030405060708091011121314151617181920212223242526272829303132a08ccb2837fb2923bd97e8f2d08ea32012d6e34be018c73e49a0f98843e8f47d5d88e53be49fec01012ef866f864800a82c35094095e7baea6a6c7c4c2dfeb977efac326af552d8785012a05f200801ba0cb088b8d2ff76a7b2c6616c9d02fb6b7a501afbf8b69d7180b09928a1b80b5e4a06448fe7476c606582039bb72a9f6f4b4fad18507b8dfbd00eebbe151cc573cd2c0".from_hex().unwrap();
|
||||
|
||||
bc.insert_block(&first);
|
||||
|
||||
let first_hash = H256::from_str("a940e5af7d146b3b917c953a82e1966b906dace3a4e355b5b0a4560190357ea1").unwrap();
|
||||
|
||||
assert_eq!(bc.block_hash(&U256::from(0u8)), Some(genesis_hash.clone()));
|
||||
assert_eq!(bc.best_block_number(), U256::from(1u8));
|
||||
assert_eq!(bc.best_block_hash(), first_hash.clone());
|
||||
assert_eq!(bc.block_hash(&U256::from(1u8)), Some(first_hash.clone()));
|
||||
assert_eq!(bc.block_details(&first_hash).unwrap().parent, genesis_hash.clone());
|
||||
assert_eq!(bc.block_details(&genesis_hash).unwrap().children, vec![first_hash.clone()]);
|
||||
assert_eq!(bc.block_hash(&U256::from(2u8)), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_small_fork() {
|
||||
let genesis = "f901fcf901f7a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a07dba07d6b448a186e9612e5f737d1c909dce473e53199901a302c00646d523c1a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000080832fefd8808454c98c8142a059262c330941f3fe2a34d16d6e3c7b30d2ceb37c6a0e9a994c494ee1a61d2410885aa4c8bf8e56e264c0c0".from_hex().unwrap();
|
||||
let b1 = "f90261f901f9a05716670833ec874362d65fea27a7cd35af5897d275b31a44944113111e4e96d2a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0cb52de543653d86ccd13ba3ddf8b052525b04231c6884a4db3188a184681d878a0e78628dd45a1f8dc495594d83b76c588a3ee67463260f8b7d4a42f574aeab29aa0e9244cf7503b79c03d3a099e07a80d2dbc77bb0b502d8a89d51ac0d68dd31313b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000001832fefd882520884562791e580a051b3ecba4e3f2b49c11d42dd0851ec514b1be3138080f72a2b6e83868275d98f8877671f479c414b47f862f86080018304cb2f94095e7baea6a6c7c4c2dfeb977efac326af552d870a801ca09e2709d7ec9bbe6b1bbbf0b2088828d14cd5e8642a1fee22dc74bfa89761a7f9a04bd8813dee4be989accdb708b1c2e325a7e9c695a8024e30e89d6c644e424747c0".from_hex().unwrap();
|
||||
let b2 = "f902ccf901f9a0437e51676ff10756fcfee5edd9159fa41dbcb1b2c592850450371cbecd54ee4fa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0c70a5dc56146e5ef025e4e5726a6373c6f12fd2f6784093a19ead0a7d17fb292a040645cbce4fd399e7bb9160b4c30c40d7ee616a030d4e18ef0ed3b02bdb65911a086e608555f63628417032a011d107b36427af37d153f0da02ce3f90fdd5e8c08b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302004002832fefd882c0e384562791e880a0e3cc39ff775cc0a32f175995b92e84b729e5c9a3563ff899e3555b908bc21d75887c3cde283f4846a6f8cdf8cb01018304cb2f8080b87e6060604052606e8060106000396000f360606040526000357c010000000000000000000000000000000000000000000000000000000090048063c0406226146037576035565b005b60406004506056565b6040518082815260200191505060405180910390f35b6000600560006000508190555060059050606b565b90561ba05258615c63503c0a600d6994b12ea5750d45b3c69668e2a371b4fbfb9eeff6b8a0a11be762bc90491231274a2945be35a43f23c27775b1ff24dd521702fe15f73ec0".from_hex().unwrap();
|
||||
let b3a = "f90261f901f9a036fde1253128666fcb95a5956da14a73489e988bb72738717ec1d31e1cee781aa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a05fb2b4bfdef7b314451cb138a534d225c922fc0e5fbe25e451142732c3e25c25a09dc4b1357c0b7b8108f8a098f4f9a1a274957bc9ebc22a9ae67ae81739e5b19ca007c6fdfa8eea7e86b81f5b0fc0f78f90cc19f4aa60d323151e0cac660199e9a1b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302008003832fefd882524d84562791eb80a074861666bd346c025889745c793b91ab9cd1e2ca19b5cf3c50d04d135b0a4d2b8809fe9587ea4cdc04f862f86002018304cb2f94ec0e71ad0a90ffe1909d27dac207f7680abba42d01801ba06fd84874d36d5de9e8e48978c03619b53a96b7ae0a4cd1ac118f103098b44801a00572596974dd7df4f9f69bd7456585618c568d8434ef6453391b89281ce12ae1c0".from_hex().unwrap();
|
||||
let b3b = "f90265f901f9a036fde1253128666fcb95a5956da14a73489e988bb72738717ec1d31e1cee781aa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0ab87dc338bfd6f662b1cd90bc0c9e40a1b2146a095312393c9e13ce3a5008b09a0e609b7a7d4b8a2403ec1268627ecd98783627246e8f1b26addb3ff504f76a054a0592fabf92476512952db3a69a2481a42912e668a1ee28c4c322e703bb665f8beb90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302008003832fefd882a1f084562791ee80a0fe7098fa7e4ac5d637eea81fb23f8f78346826dbab430068dd9a249d0afa99818853e1a6b201ae3545f866f86402018304cb2f94ec0e71ad0a90ffe1909d27dac207f7680abba42d0284c04062261ca06edc9ce8e7da4cc34067beb325dcad59e5655a164a5100a50bc3eb681b12c716a0abf9053d5de65b1be81fe50d327b84de685efbeecea34e7b747180a6c6023e44c0".from_hex().unwrap();
|
||||
|
||||
let genesis_hash = H256::from_str("5716670833ec874362d65fea27a7cd35af5897d275b31a44944113111e4e96d2").unwrap();
|
||||
let b1_hash = H256::from_str("437e51676ff10756fcfee5edd9159fa41dbcb1b2c592850450371cbecd54ee4f").unwrap();
|
||||
let b2_hash = H256::from_str("36fde1253128666fcb95a5956da14a73489e988bb72738717ec1d31e1cee781a").unwrap();
|
||||
let b3a_hash = H256::from_str("c208f88c9f5bf7e00840439742c12e5226d9752981f3ec0521bdcb6dd08af277").unwrap();
|
||||
let b3b_hash = H256::from_str("bf72270ae0d95c9ea39a6adab994793fddb8c10fba7391e26279474124605d54").unwrap();
|
||||
|
||||
// b3a is a part of canon chain, whereas b3b is part of sidechain
|
||||
let best_block_hash = H256::from_str("c208f88c9f5bf7e00840439742c12e5226d9752981f3ec0521bdcb6dd08af277").unwrap();
|
||||
|
||||
let mut dir = env::temp_dir();
|
||||
dir.push(H32::random().hex());
|
||||
|
||||
let bc = BlockChain::new(&genesis, &dir);
|
||||
bc.insert_block(&b1);
|
||||
bc.insert_block(&b2);
|
||||
bc.insert_block(&b3a);
|
||||
bc.insert_block(&b3b);
|
||||
|
||||
assert_eq!(bc.best_block_hash(), best_block_hash);
|
||||
assert_eq!(bc.block_number(&genesis_hash).unwrap(), U256::from(0));
|
||||
assert_eq!(bc.block_number(&b1_hash).unwrap(), U256::from(1));
|
||||
assert_eq!(bc.block_number(&b2_hash).unwrap(), U256::from(2));
|
||||
assert_eq!(bc.block_number(&b3a_hash).unwrap(), U256::from(3));
|
||||
assert_eq!(bc.block_number(&b3b_hash).unwrap(), U256::from(3));
|
||||
|
||||
assert_eq!(bc.block_hash(&U256::from(0)).unwrap(), genesis_hash);
|
||||
assert_eq!(bc.block_hash(&U256::from(1)).unwrap(), b1_hash);
|
||||
assert_eq!(bc.block_hash(&U256::from(2)).unwrap(), b2_hash);
|
||||
assert_eq!(bc.block_hash(&U256::from(3)).unwrap(), b3a_hash);
|
||||
|
||||
// test trie route
|
||||
let r0_1 = bc.tree_route(genesis_hash.clone(), b1_hash.clone());
|
||||
assert_eq!(r0_1.ancestor, genesis_hash);
|
||||
assert_eq!(r0_1.blocks, [b1_hash.clone()]);
|
||||
assert_eq!(r0_1.index, 0);
|
||||
|
||||
let r0_2 = bc.tree_route(genesis_hash.clone(), b2_hash.clone());
|
||||
assert_eq!(r0_2.ancestor, genesis_hash);
|
||||
assert_eq!(r0_2.blocks, [b1_hash.clone(), b2_hash.clone()]);
|
||||
assert_eq!(r0_2.index, 0);
|
||||
|
||||
let r1_3a = bc.tree_route(b1_hash.clone(), b3a_hash.clone());
|
||||
assert_eq!(r1_3a.ancestor, b1_hash);
|
||||
assert_eq!(r1_3a.blocks, [b2_hash.clone(), b3a_hash.clone()]);
|
||||
assert_eq!(r1_3a.index, 0);
|
||||
|
||||
let r1_3b = bc.tree_route(b1_hash.clone(), b3b_hash.clone());
|
||||
assert_eq!(r1_3b.ancestor, b1_hash);
|
||||
assert_eq!(r1_3b.blocks, [b2_hash.clone(), b3b_hash.clone()]);
|
||||
assert_eq!(r1_3b.index, 0);
|
||||
|
||||
let r3a_3b = bc.tree_route(b3a_hash.clone(), b3b_hash.clone());
|
||||
assert_eq!(r3a_3b.ancestor, b2_hash);
|
||||
assert_eq!(r3a_3b.blocks, [b3a_hash.clone(), b3b_hash.clone()]);
|
||||
assert_eq!(r3a_3b.index, 1);
|
||||
|
||||
let r1_0 = bc.tree_route(b1_hash.clone(), genesis_hash.clone());
|
||||
assert_eq!(r1_0.ancestor, genesis_hash);
|
||||
assert_eq!(r1_0.blocks, [b1_hash.clone()]);
|
||||
assert_eq!(r1_0.index, 1);
|
||||
|
||||
let r2_0 = bc.tree_route(b2_hash.clone(), genesis_hash.clone());
|
||||
assert_eq!(r2_0.ancestor, genesis_hash);
|
||||
assert_eq!(r2_0.blocks, [b2_hash.clone(), b1_hash.clone()]);
|
||||
assert_eq!(r2_0.index, 2);
|
||||
|
||||
let r3a_1 = bc.tree_route(b3a_hash.clone(), b1_hash.clone());
|
||||
assert_eq!(r3a_1.ancestor, b1_hash);
|
||||
assert_eq!(r3a_1.blocks, [b3a_hash.clone(), b2_hash.clone()]);
|
||||
assert_eq!(r3a_1.index, 2);
|
||||
|
||||
let r3b_1 = bc.tree_route(b3b_hash.clone(), b1_hash.clone());
|
||||
assert_eq!(r3b_1.ancestor, b1_hash);
|
||||
assert_eq!(r3b_1.blocks, [b3b_hash.clone(), b2_hash.clone()]);
|
||||
assert_eq!(r3b_1.index, 2);
|
||||
|
||||
let r3b_3a = bc.tree_route(b3b_hash.clone(), b3a_hash.clone());
|
||||
assert_eq!(r3b_3a.ancestor, b2_hash);
|
||||
assert_eq!(r3b_3a.blocks, [b3b_hash.clone(), b3a_hash.clone()]);
|
||||
assert_eq!(r3b_3a.index, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_reopen_blockchain_db() {
|
||||
let genesis = "f901fcf901f7a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a07dba07d6b448a186e9612e5f737d1c909dce473e53199901a302c00646d523c1a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000080832fefd8808454c98c8142a059262c330941f3fe2a34d16d6e3c7b30d2ceb37c6a0e9a994c494ee1a61d2410885aa4c8bf8e56e264c0c0".from_hex().unwrap();
|
||||
let b1 = "f90261f901f9a05716670833ec874362d65fea27a7cd35af5897d275b31a44944113111e4e96d2a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0cb52de543653d86ccd13ba3ddf8b052525b04231c6884a4db3188a184681d878a0e78628dd45a1f8dc495594d83b76c588a3ee67463260f8b7d4a42f574aeab29aa0e9244cf7503b79c03d3a099e07a80d2dbc77bb0b502d8a89d51ac0d68dd31313b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000001832fefd882520884562791e580a051b3ecba4e3f2b49c11d42dd0851ec514b1be3138080f72a2b6e83868275d98f8877671f479c414b47f862f86080018304cb2f94095e7baea6a6c7c4c2dfeb977efac326af552d870a801ca09e2709d7ec9bbe6b1bbbf0b2088828d14cd5e8642a1fee22dc74bfa89761a7f9a04bd8813dee4be989accdb708b1c2e325a7e9c695a8024e30e89d6c644e424747c0".from_hex().unwrap();
|
||||
let genesis_hash = H256::from_str("5716670833ec874362d65fea27a7cd35af5897d275b31a44944113111e4e96d2").unwrap();
|
||||
let b1_hash = H256::from_str("437e51676ff10756fcfee5edd9159fa41dbcb1b2c592850450371cbecd54ee4f").unwrap();
|
||||
|
||||
let mut dir = env::temp_dir();
|
||||
dir.push(H32::random().hex());
|
||||
|
||||
{
|
||||
let bc = BlockChain::new(&genesis, &dir);
|
||||
assert_eq!(bc.best_block_hash(), genesis_hash);
|
||||
bc.insert_block(&b1);
|
||||
assert_eq!(bc.best_block_hash(), b1_hash);
|
||||
}
|
||||
|
||||
{
|
||||
let bc = BlockChain::new(&genesis, &dir);
|
||||
assert_eq!(bc.best_block_hash(), b1_hash);
|
||||
}
|
||||
}
|
||||
}
|
10
src/builtin.rs
Normal file
10
src/builtin.rs
Normal file
@ -0,0 +1,10 @@
|
||||
use util::uint::*;
|
||||
|
||||
/// Definition of a contract whose implementation is built-in.
|
||||
pub struct Builtin {
|
||||
/// The gas cost of running this built-in for the given size of input data.
|
||||
pub cost: Box<Fn(usize) -> U256>, // TODO: U256 should be bignum.
|
||||
/// Run this built-in function with the input being the first argument and the output
|
||||
/// being placed into the second.
|
||||
pub execute: Box<Fn(&[u8], &mut [u8])>,
|
||||
}
|
51
src/engine.rs
Normal file
51
src/engine.rs
Normal file
@ -0,0 +1,51 @@
|
||||
use std::collections::hash_map::*;
|
||||
use util::bytes::*;
|
||||
use util::semantic_version::*;
|
||||
use util::error::*;
|
||||
use header::Header;
|
||||
use transaction::Transaction;
|
||||
use spec::Spec;
|
||||
use evm_schedule::EvmSchedule;
|
||||
use env_info::EnvInfo;
|
||||
|
||||
/// A consensus mechanism for the chain. Generally either proof-of-work or proof-of-stake-based.
|
||||
/// Provides hooks into each of the major parts of block import.
|
||||
pub trait Engine {
|
||||
/// The name of this engine.
|
||||
fn name(&self) -> &str;
|
||||
/// The version of this engine. Should be of the form
|
||||
fn version(&self) -> SemanticVersion { SemanticVersion::new(0, 0, 0) }
|
||||
|
||||
/// The number of additional header fields required for this engine.
|
||||
fn seal_fields(&self) -> u32 { 0 }
|
||||
/// Default values of the additional fields RLP-encoded in a raw (non-list) harness.
|
||||
fn seal_rlp(&self) -> Bytes { vec![] }
|
||||
|
||||
/// Additional engine-specific information for the user/developer concerning `header`.
|
||||
fn extra_info(&self, _header: &Header) -> HashMap<String, String> { HashMap::new() }
|
||||
|
||||
/// Get the general parameters of the chain.
|
||||
fn spec(&self) -> &Spec;
|
||||
|
||||
/// Get the EVM schedule for
|
||||
fn evm_schedule(&self, env_info: &EnvInfo) -> EvmSchedule;
|
||||
|
||||
/// Verify that `header` is valid.
|
||||
/// `parent` (the parent header) and `block` (the header's full block) may be provided for additional
|
||||
/// checks. Returns either a null `Ok` or a general error detailing the problem with import.
|
||||
fn verify(&self, _header: &Header, _parent: Option<&Header>, _block: Option<&[u8]>) -> Result<(), EthcoreError> { Ok(()) }
|
||||
|
||||
/// Additional verification for transactions in blocks.
|
||||
// TODO: Add flags for which bits of the transaction to check.
|
||||
fn verify_transaction(&self, _t: &Transaction, _header: &Header) -> Result<(), EthcoreError> { Ok(()) }
|
||||
|
||||
/// Don't forget to call Super::populateFromParent when subclassing & overriding.
|
||||
fn populate_from_parent(&self, _header: &mut Header, _parent: &Header) -> Result<(), EthcoreError> { Ok(()) }
|
||||
|
||||
// TODO: buildin contract routing - this will require removing the built-in configuration reading logic from Spec
|
||||
// into here and removing the Spec::builtins field. It's a big job.
|
||||
/* fn is_builtin(&self, a: Address) -> bool;
|
||||
fn cost_of_builtin(&self, a: Address, in: &[u8]) -> bignum;
|
||||
fn execute_builtin(&self, a: Address, in: &[u8], out: &mut [u8]);
|
||||
*/
|
||||
}
|
24
src/env_info.rs
Normal file
24
src/env_info.rs
Normal file
@ -0,0 +1,24 @@
|
||||
use util::uint::*;
|
||||
use util::hash::*;
|
||||
|
||||
/// Simple vector of hashes, should be at most 256 items large, can be smaller if being used
|
||||
/// for a block whose number is less than 257.
|
||||
pub type LastHashes = Vec<H256>;
|
||||
|
||||
/// Information concerning the execution environment for a message-call/contract-creation.
|
||||
pub struct EnvInfo {
|
||||
/// The block number.
|
||||
pub number: U256,
|
||||
/// The block author.
|
||||
pub author: Address,
|
||||
/// The block timestamp.
|
||||
pub timestamp: U256,
|
||||
/// The block difficulty.
|
||||
pub difficulty: U256,
|
||||
/// The block gas limit.
|
||||
pub gas_limit: U256,
|
||||
/// The last 256 block hashes.
|
||||
pub last_hashes: LastHashes,
|
||||
/// The gas used.
|
||||
pub gas_used: U256,
|
||||
}
|
@ -27,7 +27,7 @@ pub struct BlockQueueStats;
|
||||
pub struct TreeRoute;
|
||||
|
||||
pub type BlockNumber = u32;
|
||||
pub type BlockHeader = ::blockheader::Header;
|
||||
pub type BlockHeader = ::header::Header;
|
||||
|
||||
pub trait BlockChainClient : Sync {
|
||||
fn block_header(&self, h: &H256) -> Option<BlockHeader>;
|
||||
|
80
src/evm_schedule.rs
Normal file
80
src/evm_schedule.rs
Normal file
@ -0,0 +1,80 @@
|
||||
/// Definition of the cost schedule and other parameterisations for the EVM.
|
||||
pub struct EvmSchedule {
|
||||
pub exceptional_failed_code_deposit: bool,
|
||||
pub have_delegate_call: bool,
|
||||
pub stack_limit: usize,
|
||||
pub tier_step_gas: [usize; 8],
|
||||
pub exp_gas: usize,
|
||||
pub exp_byte_gas: usize,
|
||||
pub sha3_gas: usize,
|
||||
pub sha3_word_gas: usize,
|
||||
pub sload_gas: usize,
|
||||
pub sstore_set_gas: usize,
|
||||
pub sstore_reset_gas: usize,
|
||||
pub sstore_refund_gas: usize,
|
||||
pub jumpdest_gas: usize,
|
||||
pub log_gas: usize,
|
||||
pub log_data_gas: usize,
|
||||
pub log_topic_gas: usize,
|
||||
pub create_gas: usize,
|
||||
pub call_gas: usize,
|
||||
pub call_stipend: usize,
|
||||
pub call_value_transfer_gas: usize,
|
||||
pub call_new_account_gas: usize,
|
||||
pub suicide_refund_gas: usize,
|
||||
pub memory_gas: usize,
|
||||
pub quad_coeff_div: usize,
|
||||
pub create_data_gas: usize,
|
||||
pub tx_gas: usize,
|
||||
pub tx_create_gas: usize,
|
||||
pub tx_data_zero_gas: usize,
|
||||
pub tx_data_non_zero_gas: usize,
|
||||
pub copy_gas: usize,
|
||||
}
|
||||
|
||||
impl EvmSchedule {
|
||||
/// Schedule for the Frontier-era of the Ethereum main net.
|
||||
pub fn new_frontier() -> EvmSchedule {
|
||||
Self::new(false, false, 21000)
|
||||
}
|
||||
|
||||
/// Schedule for the Homestead-era of the Ethereum main net.
|
||||
pub fn new_homestead() -> EvmSchedule {
|
||||
Self::new(true, true, 53000)
|
||||
}
|
||||
|
||||
fn new(efcd: bool, hdc: bool, tcg: usize) -> EvmSchedule {
|
||||
EvmSchedule{
|
||||
exceptional_failed_code_deposit: efcd,
|
||||
have_delegate_call: hdc,
|
||||
stack_limit: 1024,
|
||||
tier_step_gas: [0usize, 2, 3, 4, 5, 6, 10, 20],
|
||||
exp_gas: 10,
|
||||
exp_byte_gas: 10,
|
||||
sha3_gas: 30,
|
||||
sha3_word_gas: 6,
|
||||
sload_gas: 50,
|
||||
sstore_set_gas: 20000,
|
||||
sstore_reset_gas: 5000,
|
||||
sstore_refund_gas: 15000,
|
||||
jumpdest_gas: 1,
|
||||
log_gas: 375,
|
||||
log_data_gas: 8,
|
||||
log_topic_gas: 375,
|
||||
create_gas: 32000,
|
||||
call_gas: 40,
|
||||
call_stipend: 2300,
|
||||
call_value_transfer_gas: 9000,
|
||||
call_new_account_gas: 25000,
|
||||
suicide_refund_gas: 24000,
|
||||
memory_gas: 3,
|
||||
quad_coeff_div: 512,
|
||||
create_data_gas: 200,
|
||||
tx_gas: 21000,
|
||||
tx_create_gas: tcg,
|
||||
tx_data_zero_gas: 4,
|
||||
tx_data_non_zero_gas: 68,
|
||||
copy_gas: 3,
|
||||
}
|
||||
}
|
||||
}
|
252
src/extras.rs
Normal file
252
src/extras.rs
Normal file
@ -0,0 +1,252 @@
|
||||
use heapsize::HeapSizeOf;
|
||||
use rocksdb::{DB, Writable};
|
||||
use util::uint::*;
|
||||
use util::hash::*;
|
||||
use util::rlp::*;
|
||||
|
||||
/// Represents index of extra data in database
|
||||
#[derive(Copy, Clone)]
|
||||
pub enum ExtrasIndex {
|
||||
BlockDetails = 0,
|
||||
BlockHash = 1,
|
||||
TransactionAddress = 2,
|
||||
BlockLogBlooms = 3,
|
||||
BlocksBlooms = 4
|
||||
}
|
||||
|
||||
/// trait used to write Extras data to db
|
||||
pub trait ExtrasWritable {
|
||||
fn put_extras<K, T>(&self, hash: &K, value: &T) where
|
||||
T: ExtrasIndexable + Encodable,
|
||||
K: ExtrasSliceConvertable;
|
||||
}
|
||||
|
||||
/// trait used to read Extras data from db
|
||||
pub trait ExtrasReadable {
|
||||
fn get_extras<K, T>(&self, hash: &K) -> Option<T> where
|
||||
T: ExtrasIndexable + Decodable,
|
||||
K: ExtrasSliceConvertable;
|
||||
|
||||
fn extras_exists<K, T>(&self, hash: &K) -> bool where
|
||||
T: ExtrasIndexable,
|
||||
K: ExtrasSliceConvertable;
|
||||
}
|
||||
|
||||
impl<W> ExtrasWritable for W where W: Writable {
|
||||
fn put_extras<K, T>(&self, hash: &K, value: &T) where
|
||||
T: ExtrasIndexable + Encodable,
|
||||
K: ExtrasSliceConvertable {
|
||||
|
||||
self.put(&hash.to_extras_slice(T::extras_index()), &encode(value)).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl ExtrasReadable for DB {
|
||||
fn get_extras<K, T>(&self, hash: &K) -> Option<T> where
|
||||
T: ExtrasIndexable + Decodable,
|
||||
K: ExtrasSliceConvertable {
|
||||
|
||||
self.get(&hash.to_extras_slice(T::extras_index())).unwrap()
|
||||
.map(|v| decode(&v))
|
||||
}
|
||||
|
||||
fn extras_exists<K, T>(&self, hash: &K) -> bool where
|
||||
T: ExtrasIndexable,
|
||||
K: ExtrasSliceConvertable {
|
||||
|
||||
self.get(&hash.to_extras_slice(T::extras_index())).unwrap().is_some()
|
||||
}
|
||||
}
|
||||
|
||||
/// Implementations should convert arbitrary type to database key slice
|
||||
pub trait ExtrasSliceConvertable {
|
||||
fn to_extras_slice(&self, i: ExtrasIndex) -> H264;
|
||||
}
|
||||
|
||||
impl ExtrasSliceConvertable for H256 {
|
||||
fn to_extras_slice(&self, i: ExtrasIndex) -> H264 {
|
||||
let mut slice = H264::from_slice(self);
|
||||
slice[32] = i as u8;
|
||||
slice
|
||||
}
|
||||
}
|
||||
|
||||
impl ExtrasSliceConvertable for U256 {
|
||||
fn to_extras_slice(&self, i: ExtrasIndex) -> H264 {
|
||||
H256::from(self).to_extras_slice(i)
|
||||
}
|
||||
}
|
||||
|
||||
/// Types implementing this trait can be indexed in extras database
|
||||
pub trait ExtrasIndexable {
|
||||
fn extras_index() -> ExtrasIndex;
|
||||
}
|
||||
|
||||
impl ExtrasIndexable for H256 {
|
||||
fn extras_index() -> ExtrasIndex {
|
||||
ExtrasIndex::BlockHash
|
||||
}
|
||||
}
|
||||
|
||||
/// Familial details concerning a block
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct BlockDetails {
|
||||
pub number: U256,
|
||||
pub total_difficulty: U256,
|
||||
pub parent: H256,
|
||||
pub children: Vec<H256>
|
||||
}
|
||||
|
||||
impl ExtrasIndexable for BlockDetails {
|
||||
fn extras_index() -> ExtrasIndex {
|
||||
ExtrasIndex::BlockDetails
|
||||
}
|
||||
}
|
||||
|
||||
impl HeapSizeOf for BlockDetails {
|
||||
fn heap_size_of_children(&self) -> usize {
|
||||
self.children.heap_size_of_children()
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for BlockDetails {
|
||||
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
|
||||
let d = try!(decoder.as_list());
|
||||
let details = BlockDetails {
|
||||
number: try!(Decodable::decode(&d[0])),
|
||||
total_difficulty: try!(Decodable::decode(&d[1])),
|
||||
parent: try!(Decodable::decode(&d[2])),
|
||||
children: try!(Decodable::decode(&d[3]))
|
||||
};
|
||||
Ok(details)
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodable for BlockDetails {
|
||||
fn encode<E>(&self, encoder: &mut E) where E: Encoder {
|
||||
encoder.emit_list(| e | {
|
||||
self.number.encode(e);
|
||||
self.total_difficulty.encode(e);
|
||||
self.parent.encode(e);
|
||||
self.children.encode(e);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Log blooms of certain block
|
||||
#[derive(Clone)]
|
||||
pub struct BlockLogBlooms {
|
||||
pub blooms: Vec<H2048>
|
||||
}
|
||||
|
||||
impl ExtrasIndexable for BlockLogBlooms {
|
||||
fn extras_index() -> ExtrasIndex {
|
||||
ExtrasIndex::BlockLogBlooms
|
||||
}
|
||||
}
|
||||
|
||||
impl HeapSizeOf for BlockLogBlooms {
|
||||
fn heap_size_of_children(&self) -> usize {
|
||||
self.blooms.heap_size_of_children()
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for BlockLogBlooms {
|
||||
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
|
||||
let block_blooms = BlockLogBlooms {
|
||||
blooms: try!(Decodable::decode(decoder))
|
||||
};
|
||||
|
||||
Ok(block_blooms)
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodable for BlockLogBlooms {
|
||||
fn encode<E>(&self, encoder: &mut E) where E: Encoder {
|
||||
self.blooms.encode(encoder);
|
||||
}
|
||||
}
|
||||
|
||||
/// Neighboring log blooms on certain level
|
||||
pub struct BlocksBlooms {
|
||||
pub blooms: [H2048; 16]
|
||||
}
|
||||
|
||||
impl ExtrasIndexable for BlocksBlooms {
|
||||
fn extras_index() -> ExtrasIndex {
|
||||
ExtrasIndex::BlocksBlooms
|
||||
}
|
||||
}
|
||||
|
||||
impl HeapSizeOf for BlocksBlooms {
|
||||
fn heap_size_of_children(&self) -> usize { 0 }
|
||||
}
|
||||
|
||||
impl Clone for BlocksBlooms {
|
||||
fn clone(&self) -> Self {
|
||||
let mut blooms: [H2048; 16] = unsafe { ::std::mem::uninitialized() };
|
||||
|
||||
for i in 0..self.blooms.len() {
|
||||
blooms[i] = self.blooms[i].clone();
|
||||
}
|
||||
|
||||
BlocksBlooms {
|
||||
blooms: blooms
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for BlocksBlooms {
|
||||
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
|
||||
let blocks_blooms = BlocksBlooms {
|
||||
blooms: try!(Decodable::decode(decoder))
|
||||
};
|
||||
|
||||
Ok(blocks_blooms)
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodable for BlocksBlooms {
|
||||
fn encode<E>(&self, encoder: &mut E) where E: Encoder {
|
||||
let blooms_ref: &[H2048] = &self.blooms;
|
||||
blooms_ref.encode(encoder);
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents address of certain transaction within block
|
||||
#[derive(Clone)]
|
||||
pub struct TransactionAddress {
|
||||
pub block_hash: H256,
|
||||
pub index: u64
|
||||
}
|
||||
|
||||
impl ExtrasIndexable for TransactionAddress {
|
||||
fn extras_index() -> ExtrasIndex {
|
||||
ExtrasIndex::TransactionAddress
|
||||
}
|
||||
}
|
||||
|
||||
impl HeapSizeOf for TransactionAddress {
|
||||
fn heap_size_of_children(&self) -> usize { 0 }
|
||||
}
|
||||
|
||||
impl Decodable for TransactionAddress {
|
||||
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
|
||||
let d = try!(decoder.as_list());
|
||||
let tx_address = TransactionAddress {
|
||||
block_hash: try!(Decodable::decode(&d[0])),
|
||||
index: try!(Decodable::decode(&d[1]))
|
||||
};
|
||||
|
||||
Ok(tx_address)
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodable for TransactionAddress {
|
||||
fn encode<E>(&self, encoder: &mut E) where E: Encoder {
|
||||
encoder.emit_list(| e | {
|
||||
self.block_hash.encode(e);
|
||||
self.index.encode(e);
|
||||
})
|
||||
}
|
||||
}
|
123
src/genesis.rs
Normal file
123
src/genesis.rs
Normal file
@ -0,0 +1,123 @@
|
||||
use std::io::Read;
|
||||
use std::str::FromStr;
|
||||
use std::collections::HashMap;
|
||||
use rustc_serialize::base64::FromBase64;
|
||||
use rustc_serialize::json::Json;
|
||||
use rustc_serialize::hex::FromHex;
|
||||
use flate2::read::GzDecoder;
|
||||
use util::rlp::*;
|
||||
use util::hash::*;
|
||||
use util::uint::*;
|
||||
use util::sha3::*;
|
||||
use account::*;
|
||||
use header::*;
|
||||
|
||||
/// 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: HashMap<Address, Account>
|
||||
}
|
||||
|
||||
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"));
|
||||
let (header, state) = Self::load_genesis_json(&json, &root);
|
||||
Self::new_from_header_and_state(header, state)
|
||||
}
|
||||
|
||||
/// Creates genesis block from header and state hashmap
|
||||
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
|
||||
fn load_genesis_json(json: &Json, state_root: &H256) -> (Header, HashMap<Address, Account>) {
|
||||
// once we commit ourselves to some json parsing library (serde?)
|
||||
// move it to proper data structure
|
||||
|
||||
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 header = Header {
|
||||
parent_hash: H256::from_str(&json["parentHash"].as_string().unwrap()[2..]).unwrap(),
|
||||
uncles_hash: empty_list_sha3.clone(),
|
||||
author: Address::from_str(&json["coinbase"].as_string().unwrap()[2..]).unwrap(),
|
||||
state_root: state_root.clone(),
|
||||
transactions_root: empty_data_sha3.clone(),
|
||||
receipts_root: empty_data_sha3.clone(),
|
||||
log_bloom: H2048::new(),
|
||||
difficulty: U256::from_str(&json["difficulty"].as_string().unwrap()[2..]).unwrap(),
|
||||
number: U256::from(0u8),
|
||||
gas_limit: U256::from_str(&json["gasLimit"].as_string().unwrap()[2..]).unwrap(),
|
||||
gas_used: U256::from(0u8),
|
||||
timestamp: U256::from_str(&json["timestamp"].as_string().unwrap()[2..]).unwrap(),
|
||||
extra_data: json["extraData"].as_string().unwrap()[2..].from_hex().unwrap(),
|
||||
seal: {
|
||||
// ethash specific fields
|
||||
let mixhash = H256::from_str(&json["mixhash"].as_string().unwrap()[2..]).unwrap();
|
||||
let nonce = H64::from_str(&json["nonce"].as_string().unwrap()[2..]).unwrap();
|
||||
vec![mixhash.to_vec(), nonce.to_vec()]
|
||||
}
|
||||
};
|
||||
|
||||
let mut state = 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();
|
||||
state.insert(addr, Account::new_basic(balance, U256::from(0)));
|
||||
}
|
||||
|
||||
(header, state)
|
||||
}
|
||||
|
||||
/// Returns genesis block
|
||||
pub fn block(&self) -> &[u8] {
|
||||
&self.block
|
||||
}
|
||||
|
||||
/// Returns genesis block state
|
||||
pub fn state(&self) -> &HashMap<Address, Account> {
|
||||
&self.state
|
||||
}
|
||||
|
||||
// not sure if this one is needed
|
||||
pub fn drain(self) -> (Vec<u8>, HashMap<Address, Account>) {
|
||||
(self.block, self.state)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_genesis() {
|
||||
use views::*;
|
||||
|
||||
let g = Genesis::new_frontier();
|
||||
let view = BlockView::new(&g.block).header_view();
|
||||
let genesis_hash = H256::from_str("d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3").unwrap();
|
||||
assert_eq!(view.sha3(), genesis_hash);
|
||||
}
|
@ -11,23 +11,23 @@ pub type LogBloom = H2048;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Header {
|
||||
parent_hash: H256,
|
||||
timestamp: U256,
|
||||
number: U256,
|
||||
author: Address,
|
||||
pub parent_hash: H256,
|
||||
pub timestamp: U256,
|
||||
pub number: U256,
|
||||
pub author: Address,
|
||||
|
||||
transactions_root: H256,
|
||||
uncles_hash: H256,
|
||||
extra_data: Bytes,
|
||||
pub transactions_root: H256,
|
||||
pub uncles_hash: H256,
|
||||
pub extra_data: Bytes,
|
||||
|
||||
state_root: H256,
|
||||
receipts_root: H256,
|
||||
log_bloom: LogBloom,
|
||||
gas_used: U256,
|
||||
gas_limit: U256,
|
||||
pub state_root: H256,
|
||||
pub receipts_root: H256,
|
||||
pub log_bloom: LogBloom,
|
||||
pub gas_used: U256,
|
||||
pub gas_limit: U256,
|
||||
|
||||
difficulty: U256,
|
||||
seal: Vec<Bytes>,
|
||||
pub difficulty: U256,
|
||||
pub seal: Vec<Bytes>,
|
||||
}
|
||||
|
||||
impl Header {
|
28
src/lib.rs
28
src/lib.rs
@ -1,3 +1,5 @@
|
||||
#![feature(cell_extras)]
|
||||
|
||||
//! Ethcore's ethereum implementation
|
||||
//!
|
||||
//! ### Rust version
|
||||
@ -71,8 +73,12 @@
|
||||
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
extern crate env_logger;
|
||||
extern crate rustc_serialize;
|
||||
extern crate flate2;
|
||||
extern crate rocksdb;
|
||||
extern crate heapsize;
|
||||
|
||||
extern crate env_logger;
|
||||
#[cfg(feature = "jit" )]
|
||||
extern crate evmjit;
|
||||
extern crate ethcore_util as util;
|
||||
@ -82,16 +88,22 @@ pub use util::hash::*;
|
||||
pub use util::uint::*;
|
||||
pub use util::bytes::*;
|
||||
|
||||
pub mod env_info;
|
||||
pub mod engine;
|
||||
pub mod state;
|
||||
pub mod account;
|
||||
pub mod blockheader;
|
||||
pub mod header;
|
||||
pub mod transaction;
|
||||
pub mod networkparams;
|
||||
pub mod receipt;
|
||||
pub mod denominations;
|
||||
pub mod null_engine;
|
||||
pub mod evm_schedule;
|
||||
pub mod builtin;
|
||||
pub mod spec;
|
||||
pub mod genesis;
|
||||
pub mod views;
|
||||
pub mod blockchain;
|
||||
pub mod extras;
|
||||
|
||||
pub mod eth;
|
||||
|
||||
pub mod sync;
|
||||
|
||||
#[test]
|
||||
fn it_works() {
|
||||
}
|
||||
|
@ -1,71 +0,0 @@
|
||||
use util::uint::*;
|
||||
use denominations::*;
|
||||
|
||||
/// Network related const params
|
||||
/// TODO: make it configurable from json file
|
||||
pub struct NetworkParams {
|
||||
maximum_extra_data_size: U256,
|
||||
min_gas_limit: U256,
|
||||
gas_limit_bounds_divisor: U256,
|
||||
minimum_difficulty: U256,
|
||||
difficulty_bound_divisor: U256,
|
||||
duration_limit: U256,
|
||||
block_reward: U256,
|
||||
gas_floor_target: U256,
|
||||
account_start_nonce: U256
|
||||
}
|
||||
|
||||
impl NetworkParams {
|
||||
pub fn olympic() -> NetworkParams {
|
||||
NetworkParams {
|
||||
maximum_extra_data_size: U256::from(1024u64),
|
||||
min_gas_limit: U256::from(125_000u64),
|
||||
gas_floor_target: U256::from(3_141_592u64),
|
||||
gas_limit_bounds_divisor: U256::from(1024u64),
|
||||
minimum_difficulty: U256::from(131_072u64),
|
||||
difficulty_bound_divisor: U256::from(2048u64),
|
||||
duration_limit: U256::from(8u64),
|
||||
block_reward: finney() * U256::from(1500u64),
|
||||
account_start_nonce: U256::from(0u64)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn frontier() -> NetworkParams {
|
||||
NetworkParams {
|
||||
maximum_extra_data_size: U256::from(32u64),
|
||||
min_gas_limit: U256::from(5000u64),
|
||||
gas_floor_target: U256::from(3_141_592u64),
|
||||
gas_limit_bounds_divisor: U256::from(1024u64),
|
||||
minimum_difficulty: U256::from(131_072u64),
|
||||
difficulty_bound_divisor: U256::from(2048u64),
|
||||
duration_limit: U256::from(13u64),
|
||||
block_reward: ether() * U256::from(5u64),
|
||||
account_start_nonce: U256::from(0u64)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn morden() -> NetworkParams {
|
||||
NetworkParams {
|
||||
maximum_extra_data_size: U256::from(32u64),
|
||||
min_gas_limit: U256::from(5000u64),
|
||||
gas_floor_target: U256::from(3_141_592u64),
|
||||
gas_limit_bounds_divisor: U256::from(1024u64),
|
||||
minimum_difficulty: U256::from(131_072u64),
|
||||
difficulty_bound_divisor: U256::from(2048u64),
|
||||
duration_limit: U256::from(13u64),
|
||||
block_reward: ether() * U256::from(5u64),
|
||||
account_start_nonce: U256::from(1u64) << 20
|
||||
}
|
||||
}
|
||||
|
||||
pub fn maximum_extra_data_size(&self) -> U256 { self.maximum_extra_data_size }
|
||||
pub fn min_gas_limit(&self) -> U256 { self.min_gas_limit }
|
||||
pub fn gas_limit_bounds_divisor(&self) -> U256 { self.gas_limit_bounds_divisor }
|
||||
pub fn minimum_difficulty(&self) -> U256 { self.minimum_difficulty }
|
||||
pub fn difficulty_bound_divisor(&self) -> U256 { self.difficulty_bound_divisor }
|
||||
pub fn duration_limit(&self) -> U256 { self.duration_limit }
|
||||
pub fn block_reward(&self) -> U256 { self.block_reward }
|
||||
pub fn gas_floor_target(&self) -> U256 { self.gas_floor_target }
|
||||
pub fn account_start_nonce(&self) -> U256 { self.account_start_nonce }
|
||||
}
|
||||
|
21
src/null_engine.rs
Normal file
21
src/null_engine.rs
Normal file
@ -0,0 +1,21 @@
|
||||
use engine::Engine;
|
||||
use spec::Spec;
|
||||
use evm_schedule::EvmSchedule;
|
||||
use env_info::EnvInfo;
|
||||
|
||||
/// An engine which does not provide any consensus mechanism.
|
||||
pub struct NullEngine {
|
||||
spec: Spec,
|
||||
}
|
||||
|
||||
impl NullEngine {
|
||||
pub fn new_boxed(spec: Spec) -> Box<Engine> {
|
||||
Box::new(NullEngine{spec: spec})
|
||||
}
|
||||
}
|
||||
|
||||
impl Engine for NullEngine {
|
||||
fn name(&self) -> &str { "NullEngine" }
|
||||
fn spec(&self) -> &Spec { &self.spec }
|
||||
fn evm_schedule(&self, _env_info: &EnvInfo) -> EvmSchedule { EvmSchedule::new_frontier() }
|
||||
}
|
7
src/receipt.rs
Normal file
7
src/receipt.rs
Normal file
@ -0,0 +1,7 @@
|
||||
use util::hash::*;
|
||||
|
||||
/// Information describing execution of a transaction.
|
||||
pub struct Receipt {
|
||||
// TODO
|
||||
pub state_root: H256,
|
||||
}
|
189
src/spec.rs
Normal file
189
src/spec.rs
Normal file
@ -0,0 +1,189 @@
|
||||
use std::collections::hash_map::*;
|
||||
use std::cell::*;
|
||||
use std::str::FromStr;
|
||||
use util::uint::*;
|
||||
use util::hash::*;
|
||||
use util::bytes::*;
|
||||
use util::triehash::*;
|
||||
use util::error::*;
|
||||
use util::rlp::*;
|
||||
use account::Account;
|
||||
use engine::Engine;
|
||||
use builtin::Builtin;
|
||||
use null_engine::NullEngine;
|
||||
use denominations::*;
|
||||
|
||||
/// 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 {
|
||||
// What engine are we using for this?
|
||||
pub engine_name: String,
|
||||
|
||||
// Various parameters for the chain operation.
|
||||
pub block_reward: U256,
|
||||
pub maximum_extra_data_size: U256,
|
||||
pub account_start_nonce: U256,
|
||||
pub misc: HashMap<String, Bytes>,
|
||||
|
||||
// Builtin-contracts are here for now but would like to abstract into Engine API eventually.
|
||||
pub builtins: HashMap<Address, Builtin>,
|
||||
|
||||
// Genesis params.
|
||||
pub parent_hash: H256,
|
||||
pub author: Address,
|
||||
pub difficulty: U256,
|
||||
pub gas_limit: U256,
|
||||
pub gas_used: U256,
|
||||
pub timestamp: U256,
|
||||
pub extra_data: Bytes,
|
||||
pub genesis_state: HashMap<Address, Account>,
|
||||
pub seal_fields: usize,
|
||||
pub seal_rlp: Bytes,
|
||||
|
||||
// May be prepopulated if we know this in advance.
|
||||
state_root_memo: RefCell<Option<H256>>,
|
||||
}
|
||||
|
||||
impl Spec {
|
||||
/// Convert this object into a boxed Engine of the right underlying type.
|
||||
// TODO avoid this hard-coded nastiness - use dynamic-linked plugin framework instead.
|
||||
pub fn to_engine(self) -> Result<Box<Engine>, EthcoreError> {
|
||||
match self.engine_name.as_ref() {
|
||||
"NullEngine" => Ok(NullEngine::new_boxed(self)),
|
||||
_ => Err(EthcoreError::UnknownName)
|
||||
}
|
||||
}
|
||||
|
||||
/// Return the state root for the genesis state, memoising accordingly.
|
||||
pub fn state_root(&self) -> Ref<H256> {
|
||||
if self.state_root_memo.borrow().is_none() {
|
||||
*self.state_root_memo.borrow_mut() = Some(trie_root(self.genesis_state.iter().map(|(k, v)| (k.to_vec(), v.rlp())).collect()));
|
||||
}
|
||||
Ref::map(self.state_root_memo.borrow(), |x|x.as_ref().unwrap())
|
||||
}
|
||||
|
||||
pub fn block_reward(&self) -> U256 { self.block_reward }
|
||||
pub fn maximum_extra_data_size(&self) -> U256 { self.maximum_extra_data_size }
|
||||
pub fn account_start_nonce(&self) -> U256 { self.account_start_nonce }
|
||||
|
||||
/// Compose the genesis block for this chain.
|
||||
pub fn genesis_block(&self) -> Bytes {
|
||||
// TODO
|
||||
unimplemented!();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl Spec {
|
||||
pub fn olympic() -> Spec {
|
||||
Spec {
|
||||
engine_name: "Ethash".to_string(),
|
||||
block_reward: finney() * U256::from(1500u64),
|
||||
maximum_extra_data_size: U256::from(1024u64),
|
||||
account_start_nonce: U256::from(0u64),
|
||||
misc: vec![
|
||||
("gas_limit_bounds_divisor", encode(&1024u64)),
|
||||
("minimum_difficulty", encode(&131_072u64)),
|
||||
("difficulty_bound_divisor", encode(&2048u64)),
|
||||
("duration_limit", encode(&8u64)),
|
||||
("min_gas_limit", encode(&125_000u64)),
|
||||
("gas_floor_target", encode(&3_141_592u64)),
|
||||
].into_iter().fold(HashMap::new(), | mut acc, vec | {
|
||||
acc.insert(vec.0.to_string(), vec.1);
|
||||
acc
|
||||
}),
|
||||
builtins: HashMap::new(), // TODO: make correct
|
||||
parent_hash: H256::new(),
|
||||
author: Address::new(),
|
||||
difficulty: U256::from(131_072u64),
|
||||
gas_limit: U256::from(0u64),
|
||||
gas_used: U256::from(0u64),
|
||||
timestamp: U256::from(0u64),
|
||||
extra_data: vec![],
|
||||
genesis_state: vec![ // TODO: make correct
|
||||
(Address::new(), Account::new_basic(U256::from(1) << 200, U256::from(0)))
|
||||
].into_iter().fold(HashMap::new(), | mut acc, vec | {
|
||||
acc.insert(vec.0, vec.1);
|
||||
acc
|
||||
}),
|
||||
seal_fields: 2,
|
||||
seal_rlp: { let mut r = RlpStream::new_list(2); r.append(&0x2au64); r.append(&H256::new()); r.out() }, // TODO: make correct
|
||||
state_root_memo: RefCell::new(None),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn frontier() -> Spec {
|
||||
Spec {
|
||||
engine_name: "Ethash".to_string(),
|
||||
block_reward: ether() * U256::from(5u64),
|
||||
maximum_extra_data_size: U256::from(32u64),
|
||||
account_start_nonce: U256::from(0u64),
|
||||
misc: vec![
|
||||
("gas_limit_bounds_divisor", encode(&1024u64)),
|
||||
("minimum_difficulty", encode(&131_072u64)),
|
||||
("difficulty_bound_divisor", encode(&2048u64)),
|
||||
("duration_limit", encode(&13u64)),
|
||||
("min_gas_limit", encode(&5000u64)),
|
||||
("gas_floor_target", encode(&3_141_592u64)),
|
||||
].into_iter().fold(HashMap::new(), | mut acc, vec | {
|
||||
acc.insert(vec.0.to_string(), vec.1);
|
||||
acc
|
||||
}),
|
||||
builtins: HashMap::new(), // TODO: make correct
|
||||
parent_hash: H256::new(),
|
||||
author: Address::new(),
|
||||
difficulty: U256::from(131_072u64),
|
||||
gas_limit: U256::from(0u64),
|
||||
gas_used: U256::from(0u64),
|
||||
timestamp: U256::from(0u64),
|
||||
extra_data: vec![],
|
||||
genesis_state: vec![ // TODO: make correct
|
||||
(Address::new(), Account::new_basic(U256::from(1) << 200, U256::from(0)))
|
||||
].into_iter().fold(HashMap::new(), | mut acc, vec | {
|
||||
acc.insert(vec.0, vec.1);
|
||||
acc
|
||||
}),
|
||||
seal_fields: 2,
|
||||
seal_rlp: { let mut r = RlpStream::new_list(2); r.append(&0x42u64); r.append(&H256::new()); r.out() },
|
||||
state_root_memo: RefCell::new(None),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn morden() -> Spec {
|
||||
Spec {
|
||||
engine_name: "Ethash".to_string(),
|
||||
block_reward: ether() * U256::from(5u64),
|
||||
maximum_extra_data_size: U256::from(32u64),
|
||||
account_start_nonce: U256::from(1u64) << 20,
|
||||
misc: vec![
|
||||
("gas_limit_bounds_divisor", encode(&1024u64)),
|
||||
("minimum_difficulty", encode(&131_072u64)),
|
||||
("difficulty_bound_divisor", encode(&2048u64)),
|
||||
("duration_limit", encode(&13u64)),
|
||||
("min_gas_limit", encode(&5000u64)),
|
||||
("gas_floor_target", encode(&3_141_592u64)),
|
||||
].into_iter().fold(HashMap::new(), | mut acc, vec | {
|
||||
acc.insert(vec.0.to_string(), vec.1);
|
||||
acc
|
||||
}),
|
||||
builtins: HashMap::new(), // TODO: make correct
|
||||
parent_hash: H256::new(),
|
||||
author: Address::new(),
|
||||
difficulty: U256::from(131_072u64),
|
||||
gas_limit: U256::from(0u64),
|
||||
gas_used: U256::from(0u64),
|
||||
timestamp: U256::from(0u64),
|
||||
extra_data: vec![],
|
||||
genesis_state: vec![ // TODO: make correct
|
||||
(Address::new(), Account::new_basic(U256::from(1) << 200, U256::from(0)))
|
||||
].into_iter().fold(HashMap::new(), | mut acc, vec | {
|
||||
acc.insert(vec.0, vec.1);
|
||||
acc
|
||||
}),
|
||||
seal_fields: 2,
|
||||
seal_rlp: { let mut r = RlpStream::new_list(2); r.append(&0x00006d6f7264656eu64); r.append(&H256::from_str("00000000000000000000000000000000000000647572616c65787365646c6578").unwrap()); r.out() }, // TODO: make correct
|
||||
state_root_memo: RefCell::new(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
210
src/state.rs
210
src/state.rs
@ -1,43 +1,29 @@
|
||||
use std::cell::*;
|
||||
use std::ops::*;
|
||||
use std::collections::HashMap;
|
||||
use util::hash::*;
|
||||
use util::hashdb::*;
|
||||
use util::overlaydb::*;
|
||||
use util::trie::*;
|
||||
use util::bytes::*;
|
||||
use util::rlp::*;
|
||||
use util::uint::*;
|
||||
use std::mem;
|
||||
//use std::cell::*;
|
||||
//use std::ops::*;
|
||||
use account::Account;
|
||||
/*
|
||||
enum ValueOrRef<'self, 'db: 'self> {
|
||||
Value(OverlayDB),
|
||||
Ref(&'db mut OverlayDB)
|
||||
}
|
||||
use transaction::Transaction;
|
||||
use receipt::Receipt;
|
||||
use env_info::EnvInfo;
|
||||
use engine::Engine;
|
||||
|
||||
impl<'self, 'db> ValueOrRef<'self, 'db: 'self> {
|
||||
pub fn get_mut(&mut self) -> &mut OverlayDB {
|
||||
match self {
|
||||
Value(ref mut x) => x,
|
||||
Ref(x) => x,
|
||||
}
|
||||
}
|
||||
pub fn get(&self) -> &OverlayDB {
|
||||
match self {
|
||||
Value(ref x) => x,
|
||||
Ref(x) => x,
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
/// Information concerning the result of the `State::apply` operation.
|
||||
pub struct ApplyResult; // TODO
|
||||
|
||||
/// Representation of the entire state of all accounts in the system.
|
||||
pub struct State {
|
||||
db: OverlayDB,
|
||||
root: H256,
|
||||
cache: HashMap<Address, Option<Account>>,
|
||||
cache: RefCell<HashMap<Address, Option<Account>>>,
|
||||
|
||||
_account_start_nonce: U256,
|
||||
account_start_nonce: U256,
|
||||
}
|
||||
|
||||
impl State {
|
||||
@ -52,8 +38,8 @@ impl State {
|
||||
State {
|
||||
db: db,
|
||||
root: root,
|
||||
cache: HashMap::new(),
|
||||
_account_start_nonce: account_start_nonce,
|
||||
cache: RefCell::new(HashMap::new()),
|
||||
account_start_nonce: account_start_nonce,
|
||||
}
|
||||
}
|
||||
|
||||
@ -61,14 +47,14 @@ impl State {
|
||||
pub fn new_existing(mut db: OverlayDB, mut root: H256, account_start_nonce: U256) -> State {
|
||||
{
|
||||
// trie should panic! if root does not exist
|
||||
let _ = TrieDBMut::new_existing(&mut db, &mut root);
|
||||
let _ = TrieDB::new(&mut db, &mut root);
|
||||
}
|
||||
|
||||
State {
|
||||
db: db,
|
||||
root: root,
|
||||
cache: HashMap::new(),
|
||||
_account_start_nonce: account_start_nonce,
|
||||
cache: RefCell::new(HashMap::new()),
|
||||
account_start_nonce: account_start_nonce,
|
||||
}
|
||||
}
|
||||
|
||||
@ -77,33 +63,41 @@ impl State {
|
||||
Self::new(OverlayDB::new_temp(), U256::from(0u8))
|
||||
}
|
||||
|
||||
/// Destroy the current object and return root and database.
|
||||
pub fn drop(self) -> (H256, OverlayDB) {
|
||||
(self.root, self.db)
|
||||
}
|
||||
|
||||
/// Return reference to root
|
||||
pub fn root(&self) -> &H256 {
|
||||
&self.root
|
||||
}
|
||||
|
||||
/// Desttroy the current database and return it.
|
||||
/// WARNING: the struct should be dropped immediately following this.
|
||||
pub fn take_db(&mut self) -> OverlayDB {
|
||||
mem::replace(&mut self.db, OverlayDB::new_temp())
|
||||
}
|
||||
|
||||
/// Destroy the current object and return root and database.
|
||||
pub fn drop(mut self) -> (H256, OverlayDB) {
|
||||
(mem::replace(&mut self.root, H256::new()), mem::replace(&mut self.db, OverlayDB::new_temp()))
|
||||
}
|
||||
|
||||
/// Expose the underlying database; good to use for calling `state.db().commit()`.
|
||||
pub fn db(&mut self) -> &mut OverlayDB {
|
||||
&mut self.db
|
||||
}
|
||||
|
||||
/// Get the balance of account `a`.
|
||||
// TODO: make immutable
|
||||
pub fn balance(&mut self, a: &Address) -> U256 {
|
||||
pub fn balance(&self, a: &Address) -> U256 {
|
||||
self.get(a, false).as_ref().map(|account| account.balance().clone()).unwrap_or(U256::from(0u8))
|
||||
}
|
||||
|
||||
/// Get the nonce of account `a`.
|
||||
pub fn nonce(&self, a: &Address) -> U256 {
|
||||
self.get(a, false).as_ref().map(|account| account.nonce().clone()).unwrap_or(U256::from(0u8))
|
||||
}
|
||||
|
||||
/// Mutate storage of account `a` so that it is `value` for `key`.
|
||||
pub fn storage_at(&self, a: &Address, key: &H256) -> H256 {
|
||||
self.get(a, false).as_ref().map(|a|a.storage_at(&self.db, key)).unwrap_or(H256::new())
|
||||
}
|
||||
|
||||
/// Mutate storage of account `a` so that it is `value` for `key`.
|
||||
pub fn code(&self, a: &Address) -> Option<Vec<u8>> {
|
||||
self.get(a, true).as_ref().map(|a|a.code().map(|x|x.to_vec())).unwrap_or(None)
|
||||
}
|
||||
|
||||
/// Add `incr` to the balance of account `a`.
|
||||
pub fn add_balance(&mut self, a: &Address, incr: &U256) {
|
||||
self.require(a, false).add_balance(incr)
|
||||
@ -114,10 +108,10 @@ impl State {
|
||||
self.require(a, false).sub_balance(decr)
|
||||
}
|
||||
|
||||
/// Get the nonce of account `a`.
|
||||
// TODO: make immutable
|
||||
pub fn nonce(&mut self, a: &Address) -> U256 {
|
||||
self.get(a, false).as_ref().map(|account| account.nonce().clone()).unwrap_or(U256::from(0u8))
|
||||
/// Subtracts `by` from the balance of `from` and adds it to that of `to`.
|
||||
pub fn transfer_balance(&mut self, from: &Address, to: &Address, by: &U256) {
|
||||
self.sub_balance(from, by);
|
||||
self.add_balance(to, by);
|
||||
}
|
||||
|
||||
/// Increment the nonce of account `a` by 1.
|
||||
@ -125,6 +119,27 @@ impl State {
|
||||
self.require(a, false).inc_nonce()
|
||||
}
|
||||
|
||||
/// Mutate storage of account `a` so that it is `value` for `key`.
|
||||
pub fn set_storage(&mut self, a: &Address, key: H256, value: H256) {
|
||||
self.require(a, false).set_storage(key, value);
|
||||
}
|
||||
|
||||
/// Mutate storage of account `a` so that it is `value` for `key`.
|
||||
pub fn set_code(&mut self, a: &Address, code: Bytes) {
|
||||
self.require_or_from(a, true, || Account::new_contract(U256::from(0u8))).set_code(code);
|
||||
}
|
||||
|
||||
/// Execute a given transaction.
|
||||
/// This will change the state accordingly.
|
||||
pub fn apply(_env_info: &EnvInfo, _engine: &Engine, _t: &Transaction, _is_permanent: bool) -> (ApplyResult, Receipt) {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
/// Convert into a JSON representation.
|
||||
pub fn as_json(&self) -> String {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
/// Commit accounts to TrieDBMut. This is similar to cpp-ethereum's dev::eth::commit.
|
||||
/// `accounts` is mutable because we may need to commit the code or storage and record that.
|
||||
pub fn commit_into(db: &mut HashDB, mut root: H256, accounts: &mut HashMap<Address, Option<Account>>) -> H256 {
|
||||
@ -155,47 +170,49 @@ impl State {
|
||||
/// Commits our cached account changes into the trie.
|
||||
pub fn commit(&mut self) {
|
||||
let r = self.root.clone(); // would prefer not to do this, really.
|
||||
self.root = Self::commit_into(&mut self.db, r, &mut self.cache);
|
||||
self.root = Self::commit_into(&mut self.db, r, self.cache.borrow_mut().deref_mut());
|
||||
}
|
||||
|
||||
/// Pull account `a` in our cache from the trie DB. `require_code` requires that the code be cached, too.
|
||||
/// `force_create` creates a new, empty basic account if there is not currently an active account.
|
||||
// TODO: make immutable.
|
||||
fn get(&mut self, a: &Address, require_code: bool) -> Option<&Account> {
|
||||
if self.cache.get(a).is_none() {
|
||||
// load from trie.
|
||||
let t = TrieDBMut::new_existing(&mut self.db, &mut self.root);
|
||||
self.cache.insert(a.clone(), t.get(&a).map(|rlp| { println!("RLP: {:?}", rlp); Account::from_rlp(rlp) }));
|
||||
}
|
||||
/// Populate the state from `accounts`. Just uses `commit_into`.
|
||||
pub fn populate_from(&mut self, _accounts: &mut HashMap<Address, Option<Account>>) {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
let db = &self.db;
|
||||
self.cache.get_mut(a).unwrap().as_mut().map(|account| {
|
||||
if require_code {
|
||||
account.cache_code(db);
|
||||
/// Pull account `a` in our cache from the trie DB and return it.
|
||||
/// `require_code` requires that the code be cached, too.
|
||||
fn get(&self, a: &Address, require_code: bool) -> Ref<Option<Account>> {
|
||||
self.cache.borrow_mut().entry(a.clone()).or_insert_with(||
|
||||
TrieDB::new(&self.db, &self.root).get(&a).map(|rlp| Account::from_rlp(rlp)));
|
||||
if require_code {
|
||||
if let Some(ref mut account) = self.cache.borrow_mut().get_mut(a).unwrap().as_mut() {
|
||||
account.cache_code(&self.db);
|
||||
}
|
||||
account as &Account
|
||||
})
|
||||
}
|
||||
Ref::map(self.cache.borrow(), |m| m.get(a).unwrap())
|
||||
}
|
||||
|
||||
/// Pull account `a` in our cache from the trie DB. `require_code` requires that the code be cached, too.
|
||||
/// `force_create` creates a new, empty basic account if there is not currently an active account.
|
||||
fn require(&mut self, a: &Address, require_code: bool) -> &mut Account {
|
||||
if self.cache.get(a).is_none() {
|
||||
// load from trie.
|
||||
self.cache.insert(a.clone(), TrieDBMut::new(&mut self.db, &mut self.root).get(&a).map(|rlp| Account::from_rlp(rlp)));
|
||||
fn require(&self, a: &Address, require_code: bool) -> RefMut<Account> {
|
||||
self.require_or_from(a, require_code, || Account::new_basic(U256::from(0u8), self.account_start_nonce))
|
||||
}
|
||||
|
||||
/// Pull account `a` in our cache from the trie DB. `require_code` requires that the code be cached, too.
|
||||
/// `force_create` creates a new, empty basic account if there is not currently an active account.
|
||||
fn require_or_from<F: FnOnce() -> Account>(&self, a: &Address, require_code: bool, default: F) -> RefMut<Account> {
|
||||
self.cache.borrow_mut().entry(a.clone()).or_insert_with(||
|
||||
TrieDB::new(&self.db, &self.root).get(&a).map(|rlp| Account::from_rlp(rlp)));
|
||||
if self.cache.borrow().get(a).unwrap().is_none() {
|
||||
self.cache.borrow_mut().insert(a.clone(), Some(default()));
|
||||
}
|
||||
|
||||
if self.cache.get(a).unwrap().is_none() {
|
||||
self.cache.insert(a.clone(), Some(Account::new_basic(U256::from(0u8))));
|
||||
}
|
||||
|
||||
let db = &self.db;
|
||||
self.cache.get_mut(a).unwrap().as_mut().map(|account| {
|
||||
let b = self.cache.borrow_mut();
|
||||
RefMut::map(b, |m| m.get_mut(a).unwrap().as_mut().map(|account| {
|
||||
if require_code {
|
||||
account.cache_code(db);
|
||||
account.cache_code(&self.db);
|
||||
}
|
||||
account
|
||||
}).unwrap()
|
||||
}).unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
@ -208,6 +225,38 @@ use util::trie::*;
|
||||
use util::rlp::*;
|
||||
use util::uint::*;
|
||||
use std::str::FromStr;
|
||||
use account::*;
|
||||
|
||||
#[test]
|
||||
fn code_from_database() {
|
||||
let a = Address::from_str("0000000000000000000000000000000000000000").unwrap();
|
||||
let (r, db) = {
|
||||
let mut s = State::new_temp();
|
||||
s.require_or_from(&a, false, ||Account::new_contract(U256::from(42u32)));
|
||||
s.set_code(&a, vec![1, 2, 3]);
|
||||
assert_eq!(s.code(&a), Some([1u8, 2, 3].to_vec()));
|
||||
s.commit();
|
||||
assert_eq!(s.code(&a), Some([1u8, 2, 3].to_vec()));
|
||||
s.drop()
|
||||
};
|
||||
|
||||
let s = State::new_existing(db, r, U256::from(0u8));
|
||||
assert_eq!(s.code(&a), Some([1u8, 2, 3].to_vec()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn storage_at_from_database() {
|
||||
let a = Address::from_str("0000000000000000000000000000000000000000").unwrap();
|
||||
let (r, db) = {
|
||||
let mut s = State::new_temp();
|
||||
s.set_storage(&a, H256::from(&U256::from(01u64)), H256::from(&U256::from(69u64)));
|
||||
s.commit();
|
||||
s.drop()
|
||||
};
|
||||
|
||||
let s = State::new_existing(db, r, U256::from(0u8));
|
||||
assert_eq!(s.storage_at(&a, &H256::from(&U256::from(01u64))), H256::from(&U256::from(69u64)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_from_database() {
|
||||
@ -218,10 +267,10 @@ fn get_from_database() {
|
||||
s.add_balance(&a, &U256::from(69u64));
|
||||
s.commit();
|
||||
assert_eq!(s.balance(&a), U256::from(69u64));
|
||||
(s.root().clone(), s.take_db())
|
||||
s.drop()
|
||||
};
|
||||
|
||||
let mut s = State::new_existing(db, r, U256::from(0u8));
|
||||
let s = State::new_existing(db, r, U256::from(0u8));
|
||||
assert_eq!(s.balance(&a), U256::from(69u64));
|
||||
assert_eq!(s.nonce(&a), U256::from(1u64));
|
||||
}
|
||||
@ -230,6 +279,7 @@ fn get_from_database() {
|
||||
fn alter_balance() {
|
||||
let mut s = State::new_temp();
|
||||
let a = Address::from_str("0000000000000000000000000000000000000000").unwrap();
|
||||
let b = Address::from_str("0000000000000000000000000000000000000001").unwrap();
|
||||
s.add_balance(&a, &U256::from(69u64));
|
||||
assert_eq!(s.balance(&a), U256::from(69u64));
|
||||
s.commit();
|
||||
@ -238,6 +288,12 @@ fn alter_balance() {
|
||||
assert_eq!(s.balance(&a), U256::from(27u64));
|
||||
s.commit();
|
||||
assert_eq!(s.balance(&a), U256::from(27u64));
|
||||
s.transfer_balance(&a, &b, &U256::from(18u64));
|
||||
assert_eq!(s.balance(&a), U256::from(9u64));
|
||||
assert_eq!(s.balance(&b), U256::from(18u64));
|
||||
s.commit();
|
||||
assert_eq!(s.balance(&a), U256::from(9u64));
|
||||
assert_eq!(s.balance(&b), U256::from(18u64));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -3,20 +3,23 @@ use util::bytes::*;
|
||||
use util::uint::*;
|
||||
use util::rlp::*;
|
||||
|
||||
/// A set of information describing an externally-originating message call
|
||||
/// or contract creation operation.
|
||||
pub struct Transaction {
|
||||
nonce: U256,
|
||||
gas_price: U256,
|
||||
gas: U256,
|
||||
receive_address: Option<Address>,
|
||||
to: Option<Address>,
|
||||
value: U256,
|
||||
data: Bytes,
|
||||
}
|
||||
|
||||
impl Transaction {
|
||||
/// Is this transaction meant to create a contract?
|
||||
pub fn is_contract_creation(&self) -> bool {
|
||||
self.receive_address.is_none()
|
||||
self.to.is_none()
|
||||
}
|
||||
|
||||
/// Is this transaction meant to send a message?
|
||||
pub fn is_message_call(&self) -> bool {
|
||||
!self.is_contract_creation()
|
||||
}
|
||||
@ -28,7 +31,7 @@ impl Encodable for Transaction {
|
||||
self.nonce.encode(e);
|
||||
self.gas_price.encode(e);
|
||||
self.gas.encode(e);
|
||||
self.receive_address.encode(e);
|
||||
self.to.encode(e);
|
||||
self.value.encode(e);
|
||||
self.data.encode(e);
|
||||
})
|
||||
@ -36,14 +39,14 @@ impl Encodable for Transaction {
|
||||
}
|
||||
|
||||
impl Decodable for Transaction {
|
||||
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
|
||||
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
|
||||
let d = try!(decoder.as_list());
|
||||
|
||||
let transaction = Transaction {
|
||||
nonce: try!(Decodable::decode(&d[0])),
|
||||
gas_price: try!(Decodable::decode(&d[1])),
|
||||
gas: try!(Decodable::decode(&d[2])),
|
||||
receive_address: try!(Decodable::decode(&d[3])),
|
||||
to: try!(Decodable::decode(&d[3])),
|
||||
value: try!(Decodable::decode(&d[4])),
|
||||
data: try!(Decodable::decode(&d[5])),
|
||||
};
|
||||
|
148
src/views.rs
Normal file
148
src/views.rs
Normal file
@ -0,0 +1,148 @@
|
||||
//! Block oriented views onto rlp.
|
||||
use util::bytes::*;
|
||||
use util::hash::*;
|
||||
use util::uint::*;
|
||||
use util::rlp::*;
|
||||
use util::sha3::*;
|
||||
use header::*;
|
||||
use transaction::*;
|
||||
|
||||
/// View onto block rlp.
|
||||
pub struct BlockView<'a> {
|
||||
rlp: Rlp<'a>
|
||||
}
|
||||
|
||||
impl<'a> BlockView<'a> {
|
||||
/// Creates new view onto block from raw bytes.
|
||||
pub fn new(bytes: &'a [u8]) -> BlockView<'a> {
|
||||
BlockView {
|
||||
rlp: Rlp::new(bytes)
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates new view onto block from rlp.
|
||||
pub fn new_from_rlp(rlp: Rlp<'a>) -> BlockView<'a> {
|
||||
BlockView {
|
||||
rlp: rlp
|
||||
}
|
||||
}
|
||||
|
||||
/// Return reference to underlaying rlp.
|
||||
pub fn rlp(&self) -> &Rlp<'a> {
|
||||
&self.rlp
|
||||
}
|
||||
|
||||
/// Create new Header object from header rlp.
|
||||
pub fn header(&self) -> Header {
|
||||
self.rlp.val_at(0)
|
||||
}
|
||||
|
||||
/// Create new header view obto block head rlp.
|
||||
pub fn header_view(&self) -> HeaderView<'a> {
|
||||
HeaderView::new_from_rlp(self.rlp.at(0))
|
||||
}
|
||||
|
||||
/// Return List of transactions in given block.
|
||||
pub fn transactions(&self) -> Vec<Transaction> {
|
||||
self.rlp.val_at(1)
|
||||
}
|
||||
|
||||
/// Return transaction hashes.
|
||||
pub fn transaction_hashes(&self) -> Vec<H256> {
|
||||
self.rlp.at(1).iter().map(|rlp| rlp.raw().sha3()).collect()
|
||||
}
|
||||
|
||||
/// Return list of uncles of given block.
|
||||
pub fn uncles(&self) -> Vec<Header> {
|
||||
self.rlp.val_at(2)
|
||||
}
|
||||
|
||||
/// Return list of uncle hashes of given block.
|
||||
pub fn uncle_hashes(&self) -> Vec<H256> {
|
||||
self.rlp.at(2).iter().map(|rlp| rlp.raw().sha3()).collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Hashable for BlockView<'a> {
|
||||
fn sha3(&self) -> H256 {
|
||||
self.header_view().sha3()
|
||||
}
|
||||
}
|
||||
|
||||
/// View onto block header rlp.
|
||||
pub struct HeaderView<'a> {
|
||||
rlp: Rlp<'a>
|
||||
}
|
||||
|
||||
impl<'a> HeaderView<'a> {
|
||||
/// Creates new view onto header from raw bytes.
|
||||
pub fn new(bytes: &'a [u8]) -> HeaderView<'a> {
|
||||
HeaderView {
|
||||
rlp: Rlp::new(bytes)
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates new view onto header from rlp.
|
||||
pub fn new_from_rlp(rlp: Rlp<'a>) -> HeaderView<'a> {
|
||||
HeaderView {
|
||||
rlp: rlp
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns raw rlp.
|
||||
pub fn rlp(&self) -> &Rlp<'a> { &self.rlp }
|
||||
|
||||
/// Returns parent hash.
|
||||
pub fn parent_hash(&self) -> H256 { self.rlp.val_at(0) }
|
||||
|
||||
/// Returns uncles hash.
|
||||
pub fn uncles_hash(&self) -> H256 { self.rlp.val_at(1) }
|
||||
|
||||
/// Returns author.
|
||||
pub fn author(&self) -> Address { self.rlp.val_at(2) }
|
||||
|
||||
/// Returns state root.
|
||||
pub fn state_root(&self) -> H256 { self.rlp.val_at(3) }
|
||||
|
||||
/// Returns transactions root.
|
||||
pub fn transactions_root(&self) -> H256 { self.rlp.val_at(4) }
|
||||
|
||||
/// Returns block receipts root.
|
||||
pub fn receipts_root(&self) -> H256 { self.rlp.val_at(5) }
|
||||
|
||||
/// Returns block log bloom.
|
||||
pub fn log_bloom(&self) -> H2048 { self.rlp.val_at(6) }
|
||||
|
||||
/// Returns block difficulty.
|
||||
pub fn difficulty(&self) -> U256 { self.rlp.val_at(7) }
|
||||
|
||||
/// Returns block number.
|
||||
pub fn number(&self) -> U256 { self.rlp.val_at(8) }
|
||||
|
||||
/// Returns block gas limit.
|
||||
pub fn gas_limit(&self) -> U256 { self.rlp.val_at(9) }
|
||||
|
||||
/// Returns block gas used.
|
||||
pub fn gas_used(&self) -> U256 { self.rlp.val_at(10) }
|
||||
|
||||
/// Returns timestamp.
|
||||
pub fn timestamp(&self) -> U256 { self.rlp.val_at(11) }
|
||||
|
||||
/// Returns block extra data.
|
||||
pub fn extra_data(&self) -> Bytes { self.rlp.val_at(12) }
|
||||
|
||||
/// Returns block seal.
|
||||
pub fn seal(&self) -> Vec<Bytes> {
|
||||
let mut seal = vec![];
|
||||
for i in 13..self.rlp.item_count() {
|
||||
seal.push(self.rlp.val_at(i));
|
||||
}
|
||||
seal
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Hashable for HeaderView<'a> {
|
||||
fn sha3(&self) -> H256 {
|
||||
self.rlp.raw().sha3()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user