Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Make HashDB generic (#8739)
Browse files Browse the repository at this point in the history
The `patricia_trie` crate is generic over the hasher (by way of HashDB) and node encoding scheme. Adds a new `patricia_trie_ethereum` crate with concrete impls for Keccak/RLP.
  • Loading branch information
dvdplm authored Jul 2, 2018
1 parent 202c54d commit 9caa868
Show file tree
Hide file tree
Showing 89 changed files with 1,947 additions and 1,267 deletions.
161 changes: 133 additions & 28 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ members = [
"transaction-pool",
"whisper",
"whisper/cli",
"util/keccak-hasher",
"util/patricia-trie-ethereum",
]

[patch.crates-io]
Expand Down
6 changes: 4 additions & 2 deletions ethcore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ fetch = { path = "../util/fetch" }
hashdb = { path = "../util/hashdb" }
memorydb = { path = "../util/memorydb" }
patricia-trie = { path = "../util/patricia_trie" }
patricia-trie-ethereum = { path = "../util/patricia-trie-ethereum" }
ethcore-crypto = { path = "crypto" }
error-chain = { version = "0.12", default-features = false }
ethcore-io = { path = "../util/io" }
Expand Down Expand Up @@ -66,8 +67,9 @@ keccak-hash = { path = "../util/hash" }
triehash = { path = "../util/triehash" }
unexpected = { path = "../util/unexpected" }
journaldb = { path = "../util/journaldb" }
tempdir = { version = "0.3", optional = true }
keccak-hasher = { path = "../util/keccak-hasher" }
kvdb-rocksdb = { path = "../util/kvdb-rocksdb" }
tempdir = {version="0.3", optional = true}

[target.'cfg(any(target_os = "linux", target_os = "macos", target_os = "windows", target_os = "android"))'.dependencies]
hardware-wallet = { path = "../hw" }
Expand All @@ -76,8 +78,8 @@ hardware-wallet = { path = "../hw" }
fake-hardware-wallet = { path = "../util/fake-hardware-wallet" }

[dev-dependencies]
trie-standardmap = { path = "../util/trie-standardmap" }
tempdir = "0.3"
trie-standardmap = { path = "../util/trie-standardmap" }

[features]
# Display EVM debug traces.
Expand Down
2 changes: 2 additions & 0 deletions ethcore/light/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ ethcore-transaction = { path = "../transaction" }
ethereum-types = "0.3"
memorydb = { path = "../../util/memorydb" }
patricia-trie = { path = "../../util/patricia_trie" }
patricia-trie-ethereum = { path = "../../util/patricia-trie-ethereum" }
ethcore-network = { path = "../../util/network" }
ethcore-io = { path = "../../util/io" }
hashdb = { path = "../../util/hashdb" }
Expand All @@ -32,6 +33,7 @@ serde_derive = "1.0"
parking_lot = "0.5"
stats = { path = "../../util/stats" }
keccak-hash = { path = "../../util/hash" }
keccak-hasher = { path = "../../util/keccak-hasher" }
triehash = { path = "../../util/triehash" }
kvdb = { path = "../../util/kvdb" }
memory-cache = { path = "../../util/memory_cache" }
Expand Down
16 changes: 9 additions & 7 deletions ethcore/light/src/cht.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
use ethcore::ids::BlockId;
use ethereum_types::{H256, U256};
use hashdb::HashDB;
use keccak_hasher::KeccakHasher;
use memorydb::MemoryDB;
use bytes::Bytes;
use trie::{self, TrieMut, TrieDBMut, Trie, TrieDB, Recorder};
use trie::{TrieMut, Trie, Recorder};
use ethtrie::{self, TrieDB, TrieDBMut};
use rlp::{RlpStream, Rlp};

// encode a key.
Expand All @@ -50,13 +52,13 @@ pub const SIZE: u64 = 2048;
/// A canonical hash trie. This is generic over any database it can query.
/// See module docs for more details.
#[derive(Debug, Clone)]
pub struct CHT<DB: HashDB> {
pub struct CHT<DB: HashDB<KeccakHasher>> {
db: DB,
root: H256, // the root of this CHT.
number: u64,
}

impl<DB: HashDB> CHT<DB> {
impl<DB: HashDB<KeccakHasher>> CHT<DB> {
/// Query the root of the CHT.
pub fn root(&self) -> H256 { self.root }

Expand All @@ -66,7 +68,7 @@ impl<DB: HashDB> CHT<DB> {
/// Generate an inclusion proof for the entry at a specific block.
/// Nodes before level `from_level` will be omitted.
/// Returns an error on an incomplete trie, and `Ok(None)` on an unprovable request.
pub fn prove(&self, num: u64, from_level: u32) -> trie::Result<Option<Vec<Bytes>>> {
pub fn prove(&self, num: u64, from_level: u32) -> ethtrie::Result<Option<Vec<Bytes>>> {
if block_to_cht_number(num) != Some(self.number) { return Ok(None) }

let mut recorder = Recorder::with_depth(from_level);
Expand All @@ -90,10 +92,10 @@ pub struct BlockInfo {
/// Build an in-memory CHT from a closure which provides necessary information
/// about blocks. If the fetcher ever fails to provide the info, the CHT
/// will not be generated.
pub fn build<F>(cht_num: u64, mut fetcher: F) -> Option<CHT<MemoryDB>>
pub fn build<F>(cht_num: u64, mut fetcher: F) -> Option<CHT<MemoryDB<KeccakHasher>>>
where F: FnMut(BlockId) -> Option<BlockInfo>
{
let mut db = MemoryDB::new();
let mut db = MemoryDB::<KeccakHasher>::new();

// start from the last block by number and work backwards.
let last_num = start_number(cht_num + 1) - 1;
Expand Down Expand Up @@ -147,7 +149,7 @@ pub fn compute_root<I>(cht_num: u64, iterable: I) -> Option<H256>
/// verify the given trie branch and extract the canonical hash and total difficulty.
// TODO: better support for partially-checked queries.
pub fn check_proof(proof: &[Bytes], num: u64, root: H256) -> Option<(H256, U256)> {
let mut db = MemoryDB::new();
let mut db = MemoryDB::<KeccakHasher>::new();

for node in proof { db.insert(&node[..]); }
let res = match TrieDB::new(&db, &root) {
Expand Down
19 changes: 6 additions & 13 deletions ethcore/light/src/client/header_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,21 @@
use std::collections::BTreeMap;
use std::sync::Arc;

use cache::Cache;
use cht;

use ethcore::block_status::BlockStatus;
use ethcore::error::{Error, BlockImportError, BlockImportErrorKind, BlockError};
use ethcore::encoded;
use ethcore::engines::epoch::{Transition as EpochTransition, PendingTransition as PendingEpochTransition};
use ethcore::error::{Error, BlockImportError, BlockImportErrorKind, BlockError};
use ethcore::header::Header;
use ethcore::ids::BlockId;
use ethcore::spec::{Spec, SpecHardcodedSync};
use ethcore::engines::epoch::{
Transition as EpochTransition,
PendingTransition as PendingEpochTransition
};

use rlp::{Encodable, Decodable, DecoderError, RlpStream, Rlp};
use heapsize::HeapSizeOf;
use ethereum_types::{H256, H264, U256};
use plain_hasher::H256FastMap;
use heapsize::HeapSizeOf;
use kvdb::{DBTransaction, KeyValueDB};

use cache::Cache;
use parking_lot::{Mutex, RwLock};

use plain_hasher::H256FastMap;
use rlp::{Encodable, Decodable, DecoderError, RlpStream, Rlp};
use smallvec::SmallVec;

/// Store at least this many candidate headers at all times.
Expand Down
2 changes: 2 additions & 0 deletions ethcore/light/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ extern crate hashdb;
extern crate heapsize;
extern crate futures;
extern crate itertools;
extern crate keccak_hasher;
extern crate memorydb;
extern crate patricia_trie as trie;
extern crate patricia_trie_ethereum as ethtrie;
extern crate plain_hasher;
extern crate rand;
extern crate rlp;
Expand Down
16 changes: 7 additions & 9 deletions ethcore/light/src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,20 @@
use transaction::UnverifiedTransaction;

use io::TimerToken;
use network::{NetworkProtocolHandler, NetworkContext, PeerId};
use rlp::{RlpStream, Rlp};
use ethereum_types::{H256, U256};
use io::TimerToken;
use kvdb::DBValue;
use network::{NetworkProtocolHandler, NetworkContext, PeerId};
use parking_lot::{Mutex, RwLock};
use std::time::{Duration, Instant};

use provider::Provider;
use request::{Request, NetworkRequests as Requests, Response};
use rlp::{RlpStream, Rlp};
use std::collections::{HashMap, HashSet};
use std::fmt;
use std::ops::{BitOr, BitAnd, Not};
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::ops::{BitOr, BitAnd, Not};

use provider::Provider;
use request::{Request, NetworkRequests as Requests, Response};
use std::time::{Duration, Instant};

use self::request_credits::{Credits, FlowParams};
use self::context::{Ctx, TickCtx};
Expand Down
2 changes: 1 addition & 1 deletion ethcore/light/src/net/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

//! Peer status and capabilities.
use rlp::{DecoderError, Encodable, Decodable, RlpStream, Rlp};
use ethereum_types::{H256, U256};
use rlp::{DecoderError, Encodable, Decodable, RlpStream, Rlp};

use super::request_credits::FlowParams;

Expand Down
12 changes: 5 additions & 7 deletions ethcore/light/src/net/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,18 @@
use ethcore::blockchain_info::BlockChainInfo;
use ethcore::client::{EachBlockWith, TestBlockChainClient};
use ethcore::ids::BlockId;
use ethcore::encoded;
use network::{PeerId, NodeId};
use transaction::{Action, PendingTransaction};

use ethcore::ids::BlockId;
use ethereum_types::{H256, U256, Address};
use net::{LightProtocol, Params, packet, Peer};
use net::context::IoContext;
use net::status::{Capabilities, Status};
use net::{LightProtocol, Params, packet, Peer};
use network::{PeerId, NodeId};
use provider::Provider;
use request;
use request::*;

use rlp::{Rlp, RlpStream};
use ethereum_types::{H256, U256, Address};
use transaction::{Action, PendingTransaction};

use std::sync::Arc;
use std::time::Instant;
Expand Down
26 changes: 13 additions & 13 deletions ethcore/light/src/on_demand/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,25 @@
use std::sync::Arc;

use bytes::Bytes;
use ethcore::basic_account::BasicAccount;
use ethcore::encoded;
use ethcore::engines::{EthEngine, StateDependentProof};
use ethcore::machine::EthereumMachine;
use ethcore::receipt::Receipt;
use ethcore::state::{self, ProvedExecution};
use transaction::SignedTransaction;
use vm::EnvInfo;
use hash::{KECCAK_NULL_RLP, KECCAK_EMPTY, KECCAK_EMPTY_LIST_RLP, keccak};

use request::{self as net_request, IncompleteRequest, CompleteRequest, Output, OutputKind, Field};

use rlp::{RlpStream, Rlp};
use ethereum_types::{H256, U256, Address};
use parking_lot::Mutex;
use ethtrie::{TrieError, TrieDB};
use hash::{KECCAK_NULL_RLP, KECCAK_EMPTY, KECCAK_EMPTY_LIST_RLP, keccak};
use hashdb::HashDB;
use kvdb::DBValue;
use bytes::Bytes;
use memorydb::MemoryDB;
use trie::{Trie, TrieDB, TrieError};
use parking_lot::Mutex;
use request::{self as net_request, IncompleteRequest, CompleteRequest, Output, OutputKind, Field};
use rlp::{RlpStream, Rlp};
use transaction::SignedTransaction;
use trie::Trie;
use vm::EnvInfo;

const SUPPLIED_MATCHES: &'static str = "supplied responses always match produced requests; enforced by `check_response`; qed";

Expand Down Expand Up @@ -935,11 +934,12 @@ mod tests {
use ethereum_types::{H256, Address};
use memorydb::MemoryDB;
use parking_lot::Mutex;
use trie::{Trie, TrieMut, SecTrieDB, SecTrieDBMut};
use trie::recorder::Recorder;
use trie::{Trie, TrieMut};
use ethtrie::{SecTrieDB, SecTrieDBMut};
use trie::Recorder;
use hash::keccak;

use ethcore::client::{BlockChainClient, BlockInfo, TestBlockChainClient, EachBlockWith};
use ::ethcore::client::{BlockChainClient, BlockInfo, TestBlockChainClient, EachBlockWith};
use ethcore::header::Header;
use ethcore::encoded;
use ethcore::receipt::{Receipt, TransactionOutcome};
Expand Down
1 change: 1 addition & 0 deletions ethcore/private-tx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ keccak-hash = { path = "../../util/hash" }
log = "0.3"
parking_lot = "0.5"
patricia-trie = { path = "../../util/patricia_trie" }
patricia-trie-ethereum = { path = "../../util/patricia-trie-ethereum" }
rand = "0.3"
rlp = { path = "../../util/rlp" }
rlp_derive = { path = "../../util/rlp_derive" }
Expand Down
2 changes: 1 addition & 1 deletion ethcore/private-tx/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use ethereum_types::Address;
use rlp::DecoderError;
use trie::TrieError;
use ethtrie::TrieError;
use ethcore::account_provider::SignError;
use ethcore::error::{Error as EthcoreError, ExecutionError};
use transaction::Error as TransactionError;
Expand Down
1 change: 1 addition & 0 deletions ethcore/private-tx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ extern crate futures;
extern crate keccak_hash as hash;
extern crate parking_lot;
extern crate patricia_trie as trie;
extern crate patricia_trie_ethereum as ethtrie;
extern crate rlp;
extern crate url;
extern crate rustc_hex;
Expand Down
Loading

0 comments on commit 9caa868

Please sign in to comment.