2020-01-17 14:27:28 +01:00
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
2020-03-05 12:19:39 +01:00
// This file is part of Open Ethereum.
2018-07-02 18:50:05 +02:00
2020-03-05 12:19:39 +01:00
// Open Ethereum is free software: you can redistribute it and/or modify
2018-07-02 18:50:05 +02:00
// 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.
2020-03-05 12:19:39 +01:00
// Open Ethereum is distributed in the hope that it will be useful,
2018-07-02 18:50:05 +02:00
// 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
2020-03-05 12:19:39 +01:00
// along with Open Ethereum. If not, see <http://www.gnu.org/licenses/>.
2018-07-02 18:50:05 +02:00
//! Façade crate for `patricia_trie` for Ethereum specific impls
2019-02-20 19:09:34 +01:00
pub extern crate trie_db as trie ; // `pub` because we need to import this crate for the tests in `patricia_trie` and there were issues: https://gist.github.com/dvdplm/869251ee557a1b4bd53adc7c971979aa
2018-07-02 18:50:05 +02:00
extern crate elastic_array ;
Delete crates from parity-ethereum and fetch them from parity-common instead (#9083)
Use crates from parity-common: hashdb, keccak-hash, kvdb, kvdb-memorydb, kvdb-rocksdb, memorydb, parity-bytes, parity-crypto, path, patricia_trie, plain_hasher, rlp, target, test-support, trie-standardmap, triehash
2018-07-10 14:59:19 +02:00
extern crate parity_bytes ;
2018-07-02 18:50:05 +02:00
extern crate ethereum_types ;
2019-02-20 19:09:34 +01:00
extern crate hash_db ;
2018-07-02 18:50:05 +02:00
extern crate keccak_hasher ;
extern crate rlp ;
mod rlp_node_codec ;
pub use rlp_node_codec ::RlpNodeCodec ;
use ethereum_types ::H256 ;
use keccak_hasher ::KeccakHasher ;
use rlp ::DecoderError ;
/// Convenience type alias to instantiate a Keccak-flavoured `RlpNodeCodec`
pub type RlpCodec = RlpNodeCodec < KeccakHasher > ;
2019-08-15 15:36:48 +02:00
#[ derive(Clone, Default) ]
/// Defines the working of a particular flavour of trie:
/// how keys are hashed, how values are encoded, does it use extension nodes or not.
pub struct Layout ;
impl trie_db ::TrieLayout for Layout {
const USE_EXTENSION : bool = true ;
type Hash = keccak_hasher ::KeccakHasher ;
type Codec = RlpNodeCodec < KeccakHasher > ;
}
2018-07-02 18:50:05 +02:00
/// Convenience type alias to instantiate a Keccak/Rlp-flavoured `TrieDB`
2018-09-04 20:13:51 +02:00
///
/// Use it as a `Trie` trait object. You can use `db()` to get the backing database object.
/// Use `get` and `contains` to query values associated with keys in the trie.
///
/// # Example
/// ```
2019-02-20 19:09:34 +01:00
/// extern crate trie_db as trie;
2018-09-04 20:13:51 +02:00
/// extern crate patricia_trie_ethereum as ethtrie;
2019-02-20 19:09:34 +01:00
/// extern crate hash_db;
2018-09-04 20:13:51 +02:00
/// extern crate keccak_hasher;
2019-02-20 19:09:34 +01:00
/// extern crate memory_db;
2018-09-04 20:13:51 +02:00
/// extern crate ethereum_types;
2018-10-09 22:07:25 +02:00
/// extern crate elastic_array;
2019-02-20 19:09:34 +01:00
/// extern crate journaldb;
2018-09-04 20:13:51 +02:00
///
/// use trie::*;
2019-02-20 19:09:34 +01:00
/// use hash_db::*;
2018-09-04 20:13:51 +02:00
/// use keccak_hasher::KeccakHasher;
2019-02-20 19:09:34 +01:00
/// use memory_db::*;
2018-09-04 20:13:51 +02:00
/// use ethereum_types::H256;
/// use ethtrie::{TrieDB, TrieDBMut};
2018-10-09 22:07:25 +02:00
/// use elastic_array::ElasticArray128;
2018-09-04 20:13:51 +02:00
///
2018-10-09 22:07:25 +02:00
/// type DBValue = ElasticArray128<u8>;
2018-09-04 20:13:51 +02:00
///
/// fn main() {
2019-02-20 19:09:34 +01:00
/// let mut memdb = journaldb::new_memory_db();
2019-06-03 15:36:21 +02:00
/// let mut root = H256::zero();
2018-09-04 20:13:51 +02:00
/// TrieDBMut::new(&mut memdb, &mut root).insert(b"foo", b"bar").unwrap();
/// let t = TrieDB::new(&memdb, &root).unwrap();
/// assert!(t.contains(b"foo").unwrap());
2019-12-20 12:27:38 +01:00
/// assert_eq!(t.get(b"foo").unwrap().unwrap(), b"bar".to_vec());
2018-09-04 20:13:51 +02:00
/// }
/// ```
2019-08-15 15:36:48 +02:00
pub type TrieDB < ' db > = trie ::TrieDB < ' db , Layout > ;
2018-07-02 18:50:05 +02:00
/// Convenience type alias to instantiate a Keccak/Rlp-flavoured `SecTrieDB`
2019-08-15 15:36:48 +02:00
pub type SecTrieDB < ' db > = trie ::SecTrieDB < ' db , Layout > ;
2018-07-02 18:50:05 +02:00
/// Convenience type alias to instantiate a Keccak/Rlp-flavoured `FatDB`
2019-08-15 15:36:48 +02:00
pub type FatDB < ' db > = trie ::FatDB < ' db , Layout > ;
2018-07-02 18:50:05 +02:00
/// Convenience type alias to instantiate a Keccak/Rlp-flavoured `TrieDBMut`
2018-09-04 20:13:51 +02:00
///
/// Use it as a `TrieMut` trait object. You can use `db()` to get the backing database object.
/// Note that changes are not committed to the database until `commit` is called.
/// Querying the root or dropping the trie will commit automatically.
/// # Example
/// ```
2019-02-20 19:09:34 +01:00
/// extern crate trie_db as trie;
2018-09-04 20:13:51 +02:00
/// extern crate patricia_trie_ethereum as ethtrie;
2019-02-20 19:09:34 +01:00
/// extern crate hash_db;
2018-09-04 20:13:51 +02:00
/// extern crate keccak_hash;
/// extern crate keccak_hasher;
2019-02-20 19:09:34 +01:00
/// extern crate memory_db;
2018-09-04 20:13:51 +02:00
/// extern crate ethereum_types;
2018-10-09 22:07:25 +02:00
/// extern crate elastic_array;
2019-02-20 19:09:34 +01:00
/// extern crate journaldb;
2018-09-04 20:13:51 +02:00
///
/// use keccak_hash::KECCAK_NULL_RLP;
/// use ethtrie::{TrieDBMut, trie::TrieMut};
/// use keccak_hasher::KeccakHasher;
2019-02-20 19:09:34 +01:00
/// use memory_db::*;
2018-09-04 20:13:51 +02:00
/// use ethereum_types::H256;
2018-10-09 22:07:25 +02:00
/// use elastic_array::ElasticArray128;
2019-08-15 15:36:48 +02:00
/// use trie::Trie;
2018-10-09 22:07:25 +02:00
///
/// type DBValue = ElasticArray128<u8>;
2018-09-04 20:13:51 +02:00
///
/// fn main() {
2019-02-20 19:09:34 +01:00
/// let mut memdb = journaldb::new_memory_db();
2019-06-03 15:36:21 +02:00
/// let mut root = H256::zero();
2018-09-04 20:13:51 +02:00
/// let mut t = TrieDBMut::new(&mut memdb, &mut root);
/// assert!(t.is_empty());
/// assert_eq!(*t.root(), KECCAK_NULL_RLP);
/// t.insert(b"foo", b"bar").unwrap();
/// assert!(t.contains(b"foo").unwrap());
2019-12-20 12:27:38 +01:00
/// assert_eq!(t.get(b"foo").unwrap().unwrap(), b"bar".to_vec());
2018-09-04 20:13:51 +02:00
/// t.remove(b"foo").unwrap();
/// assert!(!t.contains(b"foo").unwrap());
/// }
/// ```
2019-08-15 15:36:48 +02:00
pub type TrieDBMut < ' db > = trie ::TrieDBMut < ' db , Layout > ;
2018-07-02 18:50:05 +02:00
/// Convenience type alias to instantiate a Keccak/Rlp-flavoured `SecTrieDBMut`
2019-08-15 15:36:48 +02:00
pub type SecTrieDBMut < ' db > = trie ::SecTrieDBMut < ' db , Layout > ;
2018-07-02 18:50:05 +02:00
/// Convenience type alias to instantiate a Keccak/Rlp-flavoured `FatDBMut`
2019-08-15 15:36:48 +02:00
pub type FatDBMut < ' db > = trie ::FatDBMut < ' db , Layout > ;
2018-07-02 18:50:05 +02:00
/// Convenience type alias to instantiate a Keccak/Rlp-flavoured `TrieFactory`
2019-08-15 15:36:48 +02:00
pub type TrieFactory = trie ::TrieFactory < Layout > ;
2018-07-02 18:50:05 +02:00
/// Convenience type alias for Keccak/Rlp flavoured trie errors
pub type TrieError = trie ::TrieError < H256 , DecoderError > ;
/// Convenience type alias for Keccak/Rlp flavoured trie results
pub type Result < T > = trie ::Result < T , H256 , DecoderError > ;
2019-08-19 23:31:57 +02:00
#[ cfg(test) ]
mod tests {
use ethereum_types ::H256 ;
use trie ::Trie ;
2019-12-03 12:30:07 +01:00
use crate ::{ TrieDB , TrieDBMut , trie ::TrieMut } ;
2019-08-19 23:31:57 +02:00
#[ test ]
fn test_inline_encoding_branch ( ) {
let mut memdb = journaldb ::new_memory_db ( ) ;
let mut root = H256 ::zero ( ) ;
{
let mut triedbmut = TrieDBMut ::new ( & mut memdb , & mut root ) ;
triedbmut . insert ( b " foo " , b " bar " ) . unwrap ( ) ;
triedbmut . insert ( b " fog " , b " b " ) . unwrap ( ) ;
triedbmut . insert ( b " fot " , & vec! [ 0 u8 ; 33 ] [ .. ] ) . unwrap ( ) ;
}
let t = TrieDB ::new ( & memdb , & root ) . unwrap ( ) ;
assert! ( t . contains ( b " foo " ) . unwrap ( ) ) ;
assert! ( t . contains ( b " fog " ) . unwrap ( ) ) ;
assert_eq! ( t . get ( b " foo " ) . unwrap ( ) . unwrap ( ) , b " bar " . to_vec ( ) ) ;
assert_eq! ( t . get ( b " fog " ) . unwrap ( ) . unwrap ( ) , b " b " . to_vec ( ) ) ;
assert_eq! ( t . get ( b " fot " ) . unwrap ( ) . unwrap ( ) , vec! [ 0 u8 ; 33 ] ) ;
}
#[ test ]
fn test_inline_encoding_extension ( ) {
let mut memdb = journaldb ::new_memory_db ( ) ;
let mut root = H256 ::zero ( ) ;
{
let mut triedbmut = TrieDBMut ::new ( & mut memdb , & mut root ) ;
triedbmut . insert ( b " foo " , b " b " ) . unwrap ( ) ;
triedbmut . insert ( b " fog " , b " a " ) . unwrap ( ) ;
}
let t = TrieDB ::new ( & memdb , & root ) . unwrap ( ) ;
assert! ( t . contains ( b " foo " ) . unwrap ( ) ) ;
assert! ( t . contains ( b " fog " ) . unwrap ( ) ) ;
assert_eq! ( t . get ( b " foo " ) . unwrap ( ) . unwrap ( ) , b " b " . to_vec ( ) ) ;
assert_eq! ( t . get ( b " fog " ) . unwrap ( ) . unwrap ( ) , b " a " . to_vec ( ) ) ;
}
}