Move DatabaseExtras back to trace (#10868)

* Move DatabaseExtras back to trace
Add a new BlockChainWithExtras newtype to ethcore
Impl DatabaseExtras for BlockChainWithExtras

* Avoid double Arcs
Impl From for BlockChainWithExtras for convenient instantiation
Change TraceDB::new to take a T: DatabaseExtras (instead of an Arc)

* Use local type for BlockNumber and reduce dependencies

* Update ethcore/src/client/client.rs

Co-Authored-By: Tomasz Drwięga <tomusdrw@users.noreply.github.com>

* Update ethcore/src/client/client.rs

Co-Authored-By: Tomasz Drwięga <tomusdrw@users.noreply.github.com>

* Revert "Update ethcore/src/client/client.rs"

This reverts commit cbf8a251d4acaff8b29e999aedcdaac473ddf300.

* Revert "Update ethcore/src/client/client.rs"

This reverts commit 2518873b3139e832fa57c11793624bf1bfe48d05.

* address grumbles

* Be explicit about using the BlockProvider provided block_hash()
This commit is contained in:
David 2019-07-14 18:35:43 +02:00 committed by Marek Kotewicz
parent 5baa7e8fb5
commit 14e7641835
9 changed files with 55 additions and 82 deletions

1
Cargo.lock generated
View File

@ -4339,7 +4339,6 @@ dependencies = [
name = "trace" name = "trace"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"common-types 0.1.0",
"ethcore 1.12.0", "ethcore 1.12.0",
"ethcore-blockchain 0.1.0", "ethcore-blockchain 0.1.0",
"ethcore-db 0.1.0", "ethcore-db 0.1.0",

View File

@ -1,53 +0,0 @@
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
// This file is part of Parity Ethereum.
// Parity Ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity Ethereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
//! Provides a `DatabaseExtras` trait that defines an interface to query for block data not
//! contained in a TraceDB.
use common_types::BlockNumber;
use ethereum_types::H256;
use ethcore_db::keys::TransactionAddress;
use crate::blockchain::{BlockProvider, BlockChain};
/// `DatabaseExtras` provides an interface to query extra data which is not stored in TraceDB,
/// but necessary to work correctly.
pub trait DatabaseExtras {
/// Returns hash of given block number.
fn block_hash(&self, block_number: BlockNumber) -> Option<H256>;
/// Returns hash of transaction at given position.
fn transaction_hash(&self, block_number: BlockNumber, tx_position: usize) -> Option<H256>;
}
/// Bridge between TraceDB and Blockchain.
impl DatabaseExtras for BlockChain {
fn block_hash(&self, block_number: BlockNumber) -> Option<H256> {
(self as &dyn BlockProvider).block_hash(block_number)
}
fn transaction_hash(&self, block_number: BlockNumber, tx_position: usize) -> Option<H256> {
(self as &dyn BlockProvider).block_hash(block_number)
.and_then(|block_hash| {
let tx_address = TransactionAddress {
block_hash,
index: tx_position
};
self.transaction(&tx_address)
})
.map(|tx| tx.hash())
}
}

View File

@ -28,7 +28,6 @@ mod cache;
mod config; mod config;
mod import_route; mod import_route;
mod update; mod update;
mod database_extras;
pub mod generator; pub mod generator;
@ -36,7 +35,6 @@ pub use crate::{
blockchain::{BlockProvider, BlockChain, BlockChainDB, BlockChainDBHandler}, blockchain::{BlockProvider, BlockChain, BlockChainDB, BlockChainDBHandler},
cache::CacheSize, cache::CacheSize,
config::Config, config::Config,
database_extras::DatabaseExtras,
import_route::ImportRoute, import_route::ImportRoute,
update::ExtrasInsert, update::ExtrasInsert,
}; };

View File

@ -684,7 +684,7 @@ impl Importer {
chain.insert_epoch_transition(&mut batch, header.number(), EpochTransition { chain.insert_epoch_transition(&mut batch, header.number(), EpochTransition {
block_hash: header.hash(), block_hash: header.hash(),
block_number: header.number(), block_number: header.number(),
proof: proof, proof,
}); });
// always write the batch directly since epoch transition proofs are // always write the batch directly since epoch transition proofs are

View File

@ -6,7 +6,6 @@ authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018" edition = "2018"
[dependencies] [dependencies]
common-types = { path = "../types"}
ethcore-blockchain = { path = "../blockchain" } ethcore-blockchain = { path = "../blockchain" }
ethcore-db = { path = "../db" } ethcore-db = { path = "../db" }
ethereum-types = "0.6" ethereum-types = "0.6"

View File

@ -18,8 +18,7 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
use common_types::BlockNumber; use ethcore_blockchain::{BlockProvider, BlockChainDB, TransactionAddress};
use ethcore_blockchain::{BlockChainDB, DatabaseExtras};
use ethcore_db::{ use ethcore_db::{
self as db, self as db,
cache_manager::CacheManager, cache_manager::CacheManager,
@ -31,6 +30,7 @@ use parity_util_mem::MallocSizeOfExt;
use parking_lot::RwLock; use parking_lot::RwLock;
use crate::{ use crate::{
BlockNumber,
LocalizedTrace, Config, Filter, Database as TraceDatabase, ImportRequest, LocalizedTrace, Config, Filter, Database as TraceDatabase, ImportRequest,
flat::{FlatTrace, FlatBlockTraces, FlatTransactionTraces}, flat::{FlatTrace, FlatBlockTraces, FlatTransactionTraces},
}; };
@ -57,6 +57,34 @@ impl Key<FlatBlockTraces> for H256 {
} }
} }
/// `DatabaseExtras` provides an interface to query extra data which is not stored in TraceDB,
/// but necessary to work correctly.
pub trait DatabaseExtras {
/// Returns hash of given block number.
fn block_hash(&self, block_number: BlockNumber) -> Option<H256>;
/// Returns hash of transaction at given position.
fn transaction_hash(&self, block_number: BlockNumber, tx_position: usize) -> Option<H256>;
}
impl<T: BlockProvider> DatabaseExtras for T {
fn block_hash(&self, block_number: BlockNumber) -> Option<H256> {
(&*self as &dyn BlockProvider).block_hash(block_number)
}
fn transaction_hash(&self, block_number: BlockNumber, tx_position: usize) -> Option<H256> {
self.block_hash(block_number)
.and_then(|block_hash| {
let tx_address = TransactionAddress {
block_hash,
index: tx_position
};
self.transaction(&tx_address)
})
.map(|tx| tx.hash())
}
}
/// Database to store transaction execution trace. /// Database to store transaction execution trace.
/// ///
/// Whenever a transaction is executed by EVM it's execution trace is stored /// Whenever a transaction is executed by EVM it's execution trace is stored
@ -91,7 +119,7 @@ impl<T> TraceDB<T> where T: DatabaseExtras {
cache_manager: RwLock::new(CacheManager::new(config.pref_cache_size, config.max_cache_size, 10 * 1024)), cache_manager: RwLock::new(CacheManager::new(config.pref_cache_size, config.max_cache_size, 10 * 1024)),
db, db,
enabled: config.enabled, enabled: config.enabled,
extras: extras, extras,
} }
} }
@ -175,8 +203,8 @@ impl<T> TraceDB<T> where T: DatabaseExtras {
trace_address: trace.trace_address.into_iter().collect(), trace_address: trace.trace_address.into_iter().collect(),
transaction_number: trace_tx_number, transaction_number: trace_tx_number,
transaction_hash: trace_tx_hash, transaction_hash: trace_tx_hash,
block_number: block_number, block_number,
block_hash: block_hash block_hash,
}), }),
false => None false => None
} }
@ -254,8 +282,8 @@ impl<T> TraceDatabase for TraceDB<T> where T: DatabaseExtras {
trace_address: trace.trace_address.into_iter().collect(), trace_address: trace.trace_address.into_iter().collect(),
transaction_number: Some(tx_position), transaction_number: Some(tx_position),
transaction_hash: Some(tx_hash), transaction_hash: Some(tx_hash),
block_number: block_number, block_number,
block_hash: block_hash, block_hash,
} }
}) })
) )
@ -278,8 +306,8 @@ impl<T> TraceDatabase for TraceDB<T> where T: DatabaseExtras {
trace_address: trace.trace_address.into_iter().collect(), trace_address: trace.trace_address.into_iter().collect(),
transaction_number: Some(tx_position), transaction_number: Some(tx_position),
transaction_hash: Some(tx_hash.clone()), transaction_hash: Some(tx_hash.clone()),
block_number: block_number, block_number,
block_hash: block_hash block_hash,
}) })
.collect() .collect()
}) })
@ -308,8 +336,8 @@ impl<T> TraceDatabase for TraceDB<T> where T: DatabaseExtras {
trace_address: trace.trace_address.into_iter().collect(), trace_address: trace.trace_address.into_iter().collect(),
transaction_number: trace_tx_number, transaction_number: trace_tx_number,
transaction_hash: trace_tx_hash, transaction_hash: trace_tx_hash,
block_number: block_number, block_number,
block_hash: block_hash, block_hash,
}) })
.collect::<Vec<LocalizedTrace>>() .collect::<Vec<LocalizedTrace>>()
}) })
@ -343,15 +371,14 @@ mod tests {
collections::HashMap, collections::HashMap,
sync::Arc, sync::Arc,
}; };
use common_types::BlockNumber;
use ethcore_blockchain::DatabaseExtras;
use ethcore::test_helpers::new_db; use ethcore::test_helpers::new_db;
use ethereum_types::{H256, U256, Address}; use ethereum_types::{H256, U256, Address};
use evm::CallType; use evm::CallType;
use kvdb::DBTransaction; use kvdb::DBTransaction;
use crate::{ use crate::{
Config, TraceDB, Database as TraceDatabase, ImportRequest, BlockNumber, Config, TraceDB, Database as TraceDatabase, ImportRequest, DatabaseExtras,
Filter, LocalizedTrace, AddressesFilter, TraceError, Filter, LocalizedTrace, AddressesFilter, TraceError,
trace::{Call, Action, Res}, trace::{Call, Action, Res},
flat::{FlatTrace, FlatBlockTraces, FlatTransactionTraces} flat::{FlatTrace, FlatBlockTraces, FlatTransactionTraces}
@ -443,7 +470,7 @@ mod tests {
result: Res::FailedCall(TraceError::OutOfGas), result: Res::FailedCall(TraceError::OutOfGas),
}])]), }])]),
block_hash: block_hash.clone(), block_hash: block_hash.clone(),
block_number: block_number, block_number,
enacted: vec![block_hash], enacted: vec![block_hash],
retracted: 0, retracted: 0,
} }
@ -465,7 +492,7 @@ mod tests {
result: Res::FailedCall(TraceError::OutOfGas), result: Res::FailedCall(TraceError::OutOfGas),
}])]), }])]),
block_hash: block_hash.clone(), block_hash: block_hash.clone(),
block_number: block_number, block_number,
enacted: vec![], enacted: vec![],
retracted: 0, retracted: 0,
} }
@ -486,8 +513,8 @@ mod tests {
subtraces: 0, subtraces: 0,
transaction_number: Some(0), transaction_number: Some(0),
transaction_hash: Some(tx_hash), transaction_hash: Some(tx_hash),
block_number: block_number, block_number,
block_hash: block_hash, block_hash,
} }
} }

View File

@ -16,9 +16,8 @@
//! Traces import request. //! Traces import request.
use ethereum_types::H256; use ethereum_types::H256;
use common_types::BlockNumber;
use crate::FlatBlockTraces; use crate::{FlatBlockTraces, BlockNumber};
/// Traces import request. /// Traces import request.
pub struct ImportRequest { pub struct ImportRequest {

View File

@ -16,7 +16,6 @@
//! Tracing //! Tracing
use common_types::BlockNumber;
use ethereum_types::{U256, Address}; use ethereum_types::{U256, Address};
use kvdb::DBTransaction; use kvdb::DBTransaction;
use vm::{Error as VmError, ActionParams}; use vm::{Error as VmError, ActionParams};
@ -32,7 +31,7 @@ mod types;
pub use crate::{ pub use crate::{
config::Config, config::Config,
db::TraceDB, db::{TraceDB, DatabaseExtras},
localized::LocalizedTrace, localized::LocalizedTrace,
executive_tracer::{ExecutiveTracer, ExecutiveVMTracer}, executive_tracer::{ExecutiveTracer, ExecutiveVMTracer},
import::ImportRequest, import::ImportRequest,
@ -47,6 +46,9 @@ pub use crate::{
} }
}; };
/// Type for block number.
pub(crate) type BlockNumber = u64;
/// This trait is used by executive to build traces. /// This trait is used by executive to build traces.
pub trait Tracer: Send { pub trait Tracer: Send {
/// Data returned when draining the Tracer. /// Data returned when draining the Tracer.

View File

@ -17,8 +17,10 @@
//! Localized traces type definitions //! Localized traces type definitions
use ethereum_types::H256; use ethereum_types::H256;
use super::trace::{Action, Res}; use crate::{
use common_types::BlockNumber; BlockNumber,
trace::{Action, Res}
};
/// Localized trace. /// Localized trace.
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]