-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Make HashDB generic #8739
Make HashDB generic #8739
Changes from 24 commits
e0c064a
1cd7ded
4bf7f6d
fd666d6
5325a70
041d6a2
f716105
8ce39eb
a7ff7c2
ef1e48a
3a7c490
83342c0
aa268bd
bf01073
013666f
aabe7dd
1f892fe
f98e905
b42f92e
5e9fda0
841abc4
f29132c
201cdcb
6bf7e5b
fc058e6
03ff20e
8082d40
c9fc6a9
13d1de3
33e0531
ef707b0
ada0b7e
56af2c4
cb61a01
3f1cfc5
4e2e81b
c78037c
c149786
68fa728
c198d0f
a7e9cbf
8bc455c
6d89eba
3d69e55
e905494
2a579a3
12ea521
4fd7990
829325e
494e58f
aaf5f4c
80f5545
89e22d1
f374cc5
e6e35b7
7d2a414
ae55ca4
6b4095e
4288d49
1d5f5e2
33f4e79
a567462
b146e3c
f3dd59b
6f48370
1741d81
0374aa8
1a1f98e
5d12612
b9f1760
4d733bc
fe91723
468525e
071fa8f
03d266d
9ce95cd
5db5de9
b2e8b93
4f172f7
a6fd313
073c213
cf51e79
574a22a
42df94e
f081b35
c6333af
c8a1c93
a66596d
c5c6021
8b219ad
f6cd87b
f57565c
c2c7789
046b5d6
91fd334
57b79b8
f3da9b1
5f47442
b0c715b
730099a
0e5fdfa
5ac48b3
67bf644
b8e9764
c1eee0c
60e7c90
1d7dc83
50d66fd
28358da
acd757d
3376c7d
46609fc
40c4beb
9cd8145
1eeaa1f
bc54358
f5c0440
7ccb350
15a754b
f1be1a1
b48e2c8
c366b2a
4febf0d
5f3ba13
80af9db
bcd1ad2
ce5d5b9
1d5c2b3
438e21c
27fbbb3
9c84453
0142e70
3e85645
deed06e
5666873
8bd8847
e54ff1f
027862a
37685ad
0aab7e2
b5e4485
ae5346e
66d3b76
06fd57e
d1ae199
7f004a9
75d2d23
c061f38
5d03e71
54ab636
7cbe20f
db2346d
31c4cb4
4fcd004
aab4077
a88616f
17e6c92
e1c4bea
03604e8
bfcf114
c45cad9
627567d
550f852
f55d96a
6bd549a
e4cb2ac
fc3d48c
1cf7c28
44bb8b6
29bf049
7be2120
211882e
609ebde
308f281
1aaaa63
b76fffa
92582c7
988bfaf
f487a69
d625d8e
67003be
412ae9c
07dfdc7
afbf3a9
b9e310b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
[package] | ||
name = "hashdb" | ||
version = "0.1.1" | ||
version = "0.2.0" | ||
authors = ["Parity Technologies <[email protected]>"] | ||
description = "trait for hash-keyed databases." | ||
license = "GPL-3.0" | ||
|
||
[dependencies] | ||
elastic-array = "0.10" | ||
ethereum-types = "0.3" | ||
heapsize = "0.4" | ||
tiny-keccak = "1.4.2" | ||
rlp = "*" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,62 +17,89 @@ | |
//! Database of byte-slices keyed to their Keccak hash. | ||
extern crate elastic_array; | ||
extern crate ethereum_types; | ||
extern crate heapsize; | ||
extern crate tiny_keccak; | ||
extern crate rlp; | ||
|
||
use std::collections::HashMap; | ||
use elastic_array::ElasticArray128; | ||
use ethereum_types::H256; | ||
use heapsize::HeapSizeOf; | ||
use std::collections::HashMap; | ||
use std::{fmt::Debug, hash::Hash}; | ||
use tiny_keccak::Keccak; | ||
|
||
pub trait Hasher: Sync + Send { | ||
type Out: AsRef<[u8]> + Debug + PartialEq + Eq + Clone + Copy + Hash + Send + Sync /* REVIEW: how do I get around this? --> */ + HeapSizeOf /* …and this? -> */ + rlp::Decodable; | ||
const HASHED_NULL_RLP: Self::Out; | ||
fn hash(x: &[u8]) -> Self::Out; | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq)] | ||
// REVIEW: Where do the concrete Hasher implementations go? Own crate? | ||
pub struct KeccakHasher; | ||
impl Hasher for KeccakHasher { | ||
type Out = H256; | ||
const HASHED_NULL_RLP: H256 = H256( [0x56, 0xe8, 0x1f, 0x17, 0x1b, 0xcc, 0x55, 0xa6, 0xff, 0x83, 0x45, 0xe6, 0x92, 0xc0, 0xf8, 0x6e, 0x5b, 0x48, 0xe0, 0x1b, 0x99, 0x6c, 0xad, 0xc0, 0x01, 0x62, 0x2f, 0xb5, 0xe3, 0x63, 0xb4, 0x21] ); | ||
|
||
fn hash(x: &[u8]) -> Self::Out { | ||
let mut out = [0;32]; | ||
Keccak::keccak256(x, &mut out); | ||
out.into() | ||
} | ||
} | ||
|
||
/// `HashDB` value type. | ||
pub type DBValue = ElasticArray128<u8>; | ||
|
||
/// Trait modelling datastore keyed by a 32-byte Keccak hash. | ||
pub trait HashDB: AsHashDB + Send + Sync { | ||
pub trait HashDB: Send + Sync { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do I best fit in |
||
type H: Hasher; | ||
/// Get the keys in the database together with number of underlying references. | ||
fn keys(&self) -> HashMap<H256, i32>; | ||
fn keys(&self) -> HashMap<<Self::H as Hasher>::Out, i32>; | ||
|
||
/// Look up a given hash into the bytes that hash to it, returning None if the | ||
/// hash is not known. | ||
fn get(&self, key: &H256) -> Option<DBValue>; | ||
fn get(&self, key: &<Self::H as Hasher>::Out) -> Option<DBValue>; | ||
|
||
/// Check for the existance of a hash-key. | ||
fn contains(&self, key: &H256) -> bool; | ||
fn contains(&self, key: &<Self::H as Hasher>::Out) -> bool; | ||
|
||
/// Insert a datum item into the DB and return the datum's hash for a later lookup. Insertions | ||
/// are counted and the equivalent number of `remove()`s must be performed before the data | ||
/// is considered dead. | ||
fn insert(&mut self, value: &[u8]) -> H256; | ||
fn insert(&mut self, value: &[u8]) -> <Self::H as Hasher>::Out; | ||
|
||
/// Like `insert()` , except you provide the key and the data is all moved. | ||
fn emplace(&mut self, key: H256, value: DBValue); | ||
fn emplace(&mut self, key: <Self::H as Hasher>::Out, value: DBValue); | ||
|
||
/// Remove a datum previously inserted. Insertions can be "owed" such that the same number of `insert()`s may | ||
/// happen without the data being eventually being inserted into the DB. It can be "owed" more than once. | ||
fn remove(&mut self, key: &H256); | ||
fn remove(&mut self, key: &<Self::H as Hasher>::Out); | ||
} | ||
|
||
/// Upcast trait. | ||
pub trait AsHashDB { | ||
pub trait AsHashDB<H: Hasher> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is essentially There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the idea a lot, but there's a cycle in there so the compiler won't have it. :/ |
||
/// Perform upcast to HashDB for anything that derives from HashDB. | ||
fn as_hashdb(&self) -> &HashDB; | ||
fn as_hashdb(&self) -> &HashDB<H=H>; | ||
/// Perform mutable upcast to HashDB for anything that derives from HashDB. | ||
fn as_hashdb_mut(&mut self) -> &mut HashDB; | ||
fn as_hashdb_mut(&mut self) -> &mut HashDB<H=H>; | ||
} | ||
|
||
impl<T: HashDB> AsHashDB for T { | ||
fn as_hashdb(&self) -> &HashDB { | ||
impl<HF: Hasher, T: HashDB<H=HF>> AsHashDB<HF> for T { | ||
fn as_hashdb(&self) -> &HashDB<H=HF> { | ||
self | ||
} | ||
fn as_hashdb_mut(&mut self) -> &mut HashDB { | ||
fn as_hashdb_mut(&mut self) -> &mut HashDB<H=HF> { | ||
self | ||
} | ||
} | ||
|
||
impl<'a> AsHashDB for &'a mut HashDB { | ||
fn as_hashdb(&self) -> &HashDB { | ||
impl<'a, HF: Hasher> AsHashDB<HF> for &'a mut HashDB<H=HF> { | ||
fn as_hashdb(&self) -> &HashDB<H=HF> { | ||
&**self | ||
} | ||
|
||
fn as_hashdb_mut(&mut self) -> &mut HashDB { | ||
fn as_hashdb_mut(&mut self) -> &mut HashDB<H=HF> { | ||
&mut **self | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
[package] | ||
name = "journaldb" | ||
version = "0.1.0" | ||
version = "0.2.0" | ||
authors = ["Parity Technologies <[email protected]>"] | ||
description = "A `HashDB` which can manage a short-term journal potentially containing many forks of mutually exclusive actions" | ||
license = "GPL3" | ||
|
||
[dependencies] | ||
ethcore-bytes = { path = "../bytes" } | ||
ethereum-types = "0.3" | ||
hashdb = { path = "../hashdb" } | ||
hashdb = { version = "0.2.0", path = "../hashdb" } | ||
heapsize = "0.4" | ||
kvdb = { path = "../kvdb" } | ||
log = "0.3" | ||
memorydb = { path = "../memorydb" } | ||
memorydb = { version="0.2.0", path = "../memorydb" } | ||
parking_lot = "0.5" | ||
plain_hasher = { path = "../plain_hasher" } | ||
rlp = { path = "../rlp" } | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
docs! on all public items (associated types and constants included)