merge accountdb_migration

This commit is contained in:
Robert Habermeier
2016-07-11 11:08:41 +02:00
67 changed files with 1112 additions and 674 deletions

View File

@@ -31,7 +31,7 @@ use util::network::*;
use util::io::*;
use util::rlp;
use util::sha3::*;
use util::Bytes;
use util::{Bytes, Lockable, RwLockable};
use util::rlp::{RlpStream, Rlp, UntrustedRlp};
use util::journaldb;
use util::journaldb::JournalDB;
@@ -281,7 +281,7 @@ impl Client {
// Enact Verified Block
let parent = chain_has_parent.unwrap();
let last_hashes = self.build_last_hashes(header.parent_hash.clone());
let db = self.state_db.lock().unwrap().boxed_clone();
let db = self.state_db.locked().boxed_clone();
let enact_result = enact_verified(&block, engine, self.tracedb.tracing_enabled(), db, &parent, last_hashes, &self.vm_factory, self.trie_factory.clone());
if let Err(e) = enact_result {
@@ -357,7 +357,7 @@ impl Client {
let route = self.commit_block(closed_block, &header.hash(), &block.bytes);
import_results.push(route);
self.report.write().unwrap().accrue_block(&block);
self.report.unwrapped_write().accrue_block(&block);
trace!(target: "client", "Imported #{} ({})", header.number(), header.hash());
}
@@ -455,7 +455,7 @@ impl Client {
};
self.block_header(id).and_then(|header| {
let db = self.state_db.lock().unwrap().boxed_clone();
let db = self.state_db.locked().boxed_clone();
// early exit for pruned blocks
if db.is_pruned() && self.chain.best_block_number() >= block_number + HISTORY {
@@ -471,7 +471,7 @@ impl Client {
/// Get a copy of the best block's state.
pub fn state(&self) -> State {
State::from_existing(
self.state_db.lock().unwrap().boxed_clone(),
self.state_db.locked().boxed_clone(),
HeaderView::new(&self.best_block_header()).state_root(),
self.engine.account_start_nonce(),
self.trie_factory.clone())
@@ -485,8 +485,8 @@ impl Client {
/// Get the report.
pub fn report(&self) -> ClientReport {
let mut report = self.report.read().unwrap().clone();
report.state_db_mem = self.state_db.lock().unwrap().mem_used();
let mut report = self.report.unwrapped_read().clone();
report.state_db_mem = self.state_db.locked().mem_used();
report
}
@@ -498,7 +498,7 @@ impl Client {
match self.mode {
Mode::Dark(timeout) => {
let mut ss = self.sleep_state.lock().unwrap();
let mut ss = self.sleep_state.locked();
if let Some(t) = ss.last_activity {
if Instant::now() > t + timeout {
self.sleep();
@@ -507,7 +507,7 @@ impl Client {
}
}
Mode::Passive(timeout, wakeup_after) => {
let mut ss = self.sleep_state.lock().unwrap();
let mut ss = self.sleep_state.locked();
let now = Instant::now();
if let Some(t) = ss.last_activity {
if now > t + timeout {
@@ -580,20 +580,20 @@ impl Client {
} else {
trace!(target: "mode", "sleep: Cannot sleep - syncing ongoing.");
// TODO: Consider uncommenting.
//*self.last_activity.lock().unwrap() = Some(Instant::now());
//*self.last_activity.locked() = Some(Instant::now());
}
}
}
/// Notify us that the network has been started.
pub fn network_started(&self, url: &String) {
let mut previous_enode = self.previous_enode.lock().unwrap();
pub fn network_started(&self, url: &str) {
let mut previous_enode = self.previous_enode.locked();
if let Some(ref u) = *previous_enode {
if u == url {
return;
}
}
*previous_enode = Some(url.clone());
*previous_enode = Some(url.into());
info!(target: "mode", "Public node URL: {}", url.apply(Colour::White.bold()));
}
}
@@ -641,7 +641,7 @@ impl BlockChainClient for Client {
fn keep_alive(&self) {
if self.mode != Mode::Active {
self.wake_up();
(*self.sleep_state.lock().unwrap()).last_activity = Some(Instant::now());
(*self.sleep_state.locked()).last_activity = Some(Instant::now());
}
}
@@ -765,7 +765,7 @@ impl BlockChainClient for Client {
}
fn state_data(&self, hash: &H256) -> Option<Bytes> {
self.state_db.lock().unwrap().state(hash)
self.state_db.locked().state(hash)
}
fn block_receipts(&self, hash: &H256) -> Option<Bytes> {
@@ -926,7 +926,7 @@ impl MiningBlockChainClient for Client {
&self.vm_factory,
self.trie_factory.clone(),
false, // TODO: this will need to be parameterised once we want to do immediate mining insertion.
self.state_db.lock().unwrap().boxed_clone(),
self.state_db.locked().boxed_clone(),
&self.chain.block_header(&h).expect("h is best block hash: so it's header must exist: qed"),
self.build_last_hashes(h.clone()),
author,

View File

@@ -18,6 +18,7 @@
use std::sync::atomic::{AtomicUsize, Ordering as AtomicOrder};
use util::*;
use devtools::*;
use transaction::{Transaction, LocalizedTransaction, SignedTransaction, Action};
use blockchain::TreeRoute;
use client::{BlockChainClient, MiningBlockChainClient, BlockChainInfo, BlockStatus, BlockID,
@@ -29,7 +30,7 @@ use log_entry::LocalizedLogEntry;
use receipt::{Receipt, LocalizedReceipt};
use blockchain::extras::BlockReceipts;
use error::{ImportResult};
use evm::Factory as EvmFactory;
use evm::{Factory as EvmFactory, VMType};
use miner::{Miner, MinerService};
use spec::Spec;
@@ -67,6 +68,10 @@ pub struct TestBlockChainClient {
pub queue_size: AtomicUsize,
/// Miner
pub miner: Arc<Miner>,
/// Spec
pub spec: Spec,
/// VM Factory
pub vm_factory: EvmFactory,
}
#[derive(Clone)]
@@ -106,40 +111,42 @@ impl TestBlockChainClient {
receipts: RwLock::new(HashMap::new()),
queue_size: AtomicUsize::new(0),
miner: Arc::new(Miner::with_spec(Spec::new_test())),
spec: Spec::new_test(),
vm_factory: EvmFactory::new(VMType::Interpreter),
};
client.add_blocks(1, EachBlockWith::Nothing); // add genesis block
client.genesis_hash = client.last_hash.read().unwrap().clone();
client.genesis_hash = client.last_hash.unwrapped_read().clone();
client
}
/// Set the transaction receipt result
pub fn set_transaction_receipt(&self, id: TransactionID, receipt: LocalizedReceipt) {
self.receipts.write().unwrap().insert(id, receipt);
self.receipts.unwrapped_write().insert(id, receipt);
}
/// Set the execution result.
pub fn set_execution_result(&self, result: Executed) {
*self.execution_result.write().unwrap() = Some(result);
*self.execution_result.unwrapped_write() = Some(result);
}
/// Set the balance of account `address` to `balance`.
pub fn set_balance(&self, address: Address, balance: U256) {
self.balances.write().unwrap().insert(address, balance);
self.balances.unwrapped_write().insert(address, balance);
}
/// Set nonce of account `address` to `nonce`.
pub fn set_nonce(&self, address: Address, nonce: U256) {
self.nonces.write().unwrap().insert(address, nonce);
self.nonces.unwrapped_write().insert(address, nonce);
}
/// Set `code` at `address`.
pub fn set_code(&self, address: Address, code: Bytes) {
self.code.write().unwrap().insert(address, code);
self.code.unwrapped_write().insert(address, code);
}
/// Set storage `position` to `value` for account `address`.
pub fn set_storage(&self, address: Address, position: H256, value: H256) {
self.storage.write().unwrap().insert((address, position), value);
self.storage.unwrapped_write().insert((address, position), value);
}
/// Set block queue size for testing
@@ -149,11 +156,11 @@ impl TestBlockChainClient {
/// Add blocks to test client.
pub fn add_blocks(&self, count: usize, with: EachBlockWith) {
let len = self.numbers.read().unwrap().len();
let len = self.numbers.unwrapped_read().len();
for n in len..(len + count) {
let mut header = BlockHeader::new();
header.difficulty = From::from(n);
header.parent_hash = self.last_hash.read().unwrap().clone();
header.parent_hash = self.last_hash.unwrapped_read().clone();
header.number = n as BlockNumber;
header.gas_limit = U256::from(1_000_000);
let uncles = match with {
@@ -161,7 +168,7 @@ impl TestBlockChainClient {
let mut uncles = RlpStream::new_list(1);
let mut uncle_header = BlockHeader::new();
uncle_header.difficulty = From::from(n);
uncle_header.parent_hash = self.last_hash.read().unwrap().clone();
uncle_header.parent_hash = self.last_hash.unwrapped_read().clone();
uncle_header.number = n as BlockNumber;
uncles.append(&uncle_header);
header.uncles_hash = uncles.as_raw().sha3();
@@ -174,7 +181,7 @@ impl TestBlockChainClient {
let mut txs = RlpStream::new_list(1);
let keypair = KeyPair::create().unwrap();
// Update nonces value
self.nonces.write().unwrap().insert(keypair.address(), U256::one());
self.nonces.unwrapped_write().insert(keypair.address(), U256::one());
let tx = Transaction {
action: Action::Create,
value: U256::from(100),
@@ -207,7 +214,7 @@ impl TestBlockChainClient {
rlp.append(&header);
rlp.append_raw(&rlp::NULL_RLP, 1);
rlp.append_raw(&rlp::NULL_RLP, 1);
self.blocks.write().unwrap().insert(hash, rlp.out());
self.blocks.unwrapped_write().insert(hash, rlp.out());
}
/// Make a bad block by setting invalid parent hash.
@@ -219,12 +226,12 @@ impl TestBlockChainClient {
rlp.append(&header);
rlp.append_raw(&rlp::NULL_RLP, 1);
rlp.append_raw(&rlp::NULL_RLP, 1);
self.blocks.write().unwrap().insert(hash, rlp.out());
self.blocks.unwrapped_write().insert(hash, rlp.out());
}
/// TODO:
pub fn block_hash_delta_minus(&mut self, delta: usize) -> H256 {
let blocks_read = self.numbers.read().unwrap();
let blocks_read = self.numbers.unwrapped_read();
let index = blocks_read.len() - delta;
blocks_read[&index].clone()
}
@@ -232,30 +239,56 @@ impl TestBlockChainClient {
fn block_hash(&self, id: BlockID) -> Option<H256> {
match id {
BlockID::Hash(hash) => Some(hash),
BlockID::Number(n) => self.numbers.read().unwrap().get(&(n as usize)).cloned(),
BlockID::Earliest => self.numbers.read().unwrap().get(&0).cloned(),
BlockID::Latest => self.numbers.read().unwrap().get(&(self.numbers.read().unwrap().len() - 1)).cloned()
BlockID::Number(n) => self.numbers.unwrapped_read().get(&(n as usize)).cloned(),
BlockID::Earliest => self.numbers.unwrapped_read().get(&0).cloned(),
BlockID::Latest => self.numbers.unwrapped_read().get(&(self.numbers.unwrapped_read().len() - 1)).cloned()
}
}
}
pub fn get_temp_journal_db() -> GuardedTempResult<Box<JournalDB>> {
let temp = RandomTempPath::new();
let journal_db = journaldb::new(temp.as_str(), journaldb::Algorithm::EarlyMerge, DatabaseConfig::default());
GuardedTempResult {
_temp: temp,
result: Some(journal_db)
}
}
impl MiningBlockChainClient for TestBlockChainClient {
fn prepare_open_block(&self, _author: Address, _gas_range_target: (U256, U256), _extra_data: Bytes) -> OpenBlock {
unimplemented!();
let engine = &self.spec.engine;
let genesis_header = self.spec.genesis_header();
let mut db_result = get_temp_journal_db();
let mut db = db_result.take();
self.spec.ensure_db_good(db.as_hashdb_mut());
let last_hashes = vec![genesis_header.hash()];
OpenBlock::new(
engine.deref(),
self.vm_factory(),
Default::default(),
false,
db,
&genesis_header,
last_hashes,
Address::zero(),
(3141562.into(), 31415620.into()),
vec![]
).expect("Opening block for tests will not fail.")
}
fn vm_factory(&self) -> &EvmFactory {
unimplemented!();
&self.vm_factory
}
fn import_sealed_block(&self, _block: SealedBlock) -> ImportResult {
unimplemented!();
Ok(H256::default())
}
}
impl BlockChainClient for TestBlockChainClient {
fn call(&self, _t: &SignedTransaction, _analytics: CallAnalytics) -> Result<Executed, ExecutionError> {
Ok(self.execution_result.read().unwrap().clone().unwrap())
Ok(self.execution_result.unwrapped_read().clone().unwrap())
}
fn block_total_difficulty(&self, _id: BlockID) -> Option<U256> {
@@ -268,7 +301,7 @@ impl BlockChainClient for TestBlockChainClient {
fn nonce(&self, address: &Address, id: BlockID) -> Option<U256> {
match id {
BlockID::Latest => Some(self.nonces.read().unwrap().get(address).cloned().unwrap_or_else(U256::zero)),
BlockID::Latest => Some(self.nonces.unwrapped_read().get(address).cloned().unwrap_or_else(U256::zero)),
_ => None,
}
}
@@ -278,12 +311,12 @@ impl BlockChainClient for TestBlockChainClient {
}
fn code(&self, address: &Address) -> Option<Bytes> {
self.code.read().unwrap().get(address).cloned()
self.code.unwrapped_read().get(address).cloned()
}
fn balance(&self, address: &Address, id: BlockID) -> Option<U256> {
if let BlockID::Latest = id {
Some(self.balances.read().unwrap().get(address).cloned().unwrap_or_else(U256::zero))
Some(self.balances.unwrapped_read().get(address).cloned().unwrap_or_else(U256::zero))
} else {
None
}
@@ -295,7 +328,7 @@ impl BlockChainClient for TestBlockChainClient {
fn storage_at(&self, address: &Address, position: &H256, id: BlockID) -> Option<H256> {
if let BlockID::Latest = id {
Some(self.storage.read().unwrap().get(&(address.clone(), position.clone())).cloned().unwrap_or_else(H256::new))
Some(self.storage.unwrapped_read().get(&(address.clone(), position.clone())).cloned().unwrap_or_else(H256::new))
} else {
None
}
@@ -310,7 +343,7 @@ impl BlockChainClient for TestBlockChainClient {
}
fn transaction_receipt(&self, id: TransactionID) -> Option<LocalizedReceipt> {
self.receipts.read().unwrap().get(&id).cloned()
self.receipts.unwrapped_read().get(&id).cloned()
}
fn blocks_with_bloom(&self, _bloom: &H2048, _from_block: BlockID, _to_block: BlockID) -> Option<Vec<BlockNumber>> {
@@ -326,11 +359,11 @@ impl BlockChainClient for TestBlockChainClient {
}
fn block_header(&self, id: BlockID) -> Option<Bytes> {
self.block_hash(id).and_then(|hash| self.blocks.read().unwrap().get(&hash).map(|r| Rlp::new(r).at(0).as_raw().to_vec()))
self.block_hash(id).and_then(|hash| self.blocks.unwrapped_read().get(&hash).map(|r| Rlp::new(r).at(0).as_raw().to_vec()))
}
fn block_body(&self, id: BlockID) -> Option<Bytes> {
self.block_hash(id).and_then(|hash| self.blocks.read().unwrap().get(&hash).map(|r| {
self.block_hash(id).and_then(|hash| self.blocks.unwrapped_read().get(&hash).map(|r| {
let mut stream = RlpStream::new_list(2);
stream.append_raw(Rlp::new(&r).at(1).as_raw(), 1);
stream.append_raw(Rlp::new(&r).at(2).as_raw(), 1);
@@ -339,13 +372,13 @@ impl BlockChainClient for TestBlockChainClient {
}
fn block(&self, id: BlockID) -> Option<Bytes> {
self.block_hash(id).and_then(|hash| self.blocks.read().unwrap().get(&hash).cloned())
self.block_hash(id).and_then(|hash| self.blocks.unwrapped_read().get(&hash).cloned())
}
fn block_status(&self, id: BlockID) -> BlockStatus {
match id {
BlockID::Number(number) if (number as usize) < self.blocks.read().unwrap().len() => BlockStatus::InChain,
BlockID::Hash(ref hash) if self.blocks.read().unwrap().get(hash).is_some() => BlockStatus::InChain,
BlockID::Number(number) if (number as usize) < self.blocks.unwrapped_read().len() => BlockStatus::InChain,
BlockID::Hash(ref hash) if self.blocks.unwrapped_read().get(hash).is_some() => BlockStatus::InChain,
_ => BlockStatus::Unknown
}
}
@@ -356,7 +389,7 @@ impl BlockChainClient for TestBlockChainClient {
ancestor: H256::new(),
index: 0,
blocks: {
let numbers_read = self.numbers.read().unwrap();
let numbers_read = self.numbers.unwrapped_read();
let mut adding = false;
let mut blocks = Vec::new();
@@ -413,11 +446,11 @@ impl BlockChainClient for TestBlockChainClient {
let header = Rlp::new(&b).val_at::<BlockHeader>(0);
let h = header.hash();
let number: usize = header.number as usize;
if number > self.blocks.read().unwrap().len() {
panic!("Unexpected block number. Expected {}, got {}", self.blocks.read().unwrap().len(), number);
if number > self.blocks.unwrapped_read().len() {
panic!("Unexpected block number. Expected {}, got {}", self.blocks.unwrapped_read().len(), number);
}
if number > 0 {
match self.blocks.read().unwrap().get(&header.parent_hash) {
match self.blocks.unwrapped_read().get(&header.parent_hash) {
Some(parent) => {
let parent = Rlp::new(parent).val_at::<BlockHeader>(0);
if parent.number != (header.number - 1) {
@@ -429,27 +462,27 @@ impl BlockChainClient for TestBlockChainClient {
}
}
}
let len = self.numbers.read().unwrap().len();
let len = self.numbers.unwrapped_read().len();
if number == len {
{
let mut difficulty = self.difficulty.write().unwrap();
let mut difficulty = self.difficulty.unwrapped_write();
*difficulty.deref_mut() = *difficulty.deref() + header.difficulty;
}
mem::replace(self.last_hash.write().unwrap().deref_mut(), h.clone());
self.blocks.write().unwrap().insert(h.clone(), b);
self.numbers.write().unwrap().insert(number, h.clone());
mem::replace(self.last_hash.unwrapped_write().deref_mut(), h.clone());
self.blocks.unwrapped_write().insert(h.clone(), b);
self.numbers.unwrapped_write().insert(number, h.clone());
let mut parent_hash = header.parent_hash;
if number > 0 {
let mut n = number - 1;
while n > 0 && self.numbers.read().unwrap()[&n] != parent_hash {
*self.numbers.write().unwrap().get_mut(&n).unwrap() = parent_hash.clone();
while n > 0 && self.numbers.unwrapped_read()[&n] != parent_hash {
*self.numbers.unwrapped_write().get_mut(&n).unwrap() = parent_hash.clone();
n -= 1;
parent_hash = Rlp::new(&self.blocks.read().unwrap()[&parent_hash]).val_at::<BlockHeader>(0).parent_hash;
parent_hash = Rlp::new(&self.blocks.unwrapped_read()[&parent_hash]).val_at::<BlockHeader>(0).parent_hash;
}
}
}
else {
self.blocks.write().unwrap().insert(h.clone(), b.to_vec());
self.blocks.unwrapped_write().insert(h.clone(), b.to_vec());
}
Ok(h)
}
@@ -470,11 +503,11 @@ impl BlockChainClient for TestBlockChainClient {
fn chain_info(&self) -> BlockChainInfo {
BlockChainInfo {
total_difficulty: *self.difficulty.read().unwrap(),
pending_total_difficulty: *self.difficulty.read().unwrap(),
total_difficulty: *self.difficulty.unwrapped_read(),
pending_total_difficulty: *self.difficulty.unwrapped_read(),
genesis_hash: self.genesis_hash.clone(),
best_block_hash: self.last_hash.read().unwrap().clone(),
best_block_number: self.blocks.read().unwrap().len() as BlockNumber - 1,
best_block_hash: self.last_hash.unwrapped_read().clone(),
best_block_number: self.blocks.unwrapped_read().len() as BlockNumber - 1,
}
}