2020-09-22 14:53:52 +02:00
|
|
|
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of OpenEthereum.
|
2016-03-11 13:50:39 +01:00
|
|
|
|
2020-09-22 14:53:52 +02:00
|
|
|
// OpenEthereum is free software: you can redistribute it and/or modify
|
2016-03-11 13:50:39 +01: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-09-22 14:53:52 +02:00
|
|
|
// OpenEthereum is distributed in the hope that it will be useful,
|
2016-03-11 13:50:39 +01: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-09-22 14:53:52 +02:00
|
|
|
// along with OpenEthereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-03-11 13:50:39 +01:00
|
|
|
|
2016-04-06 10:07:24 +02:00
|
|
|
//! Disk-backed `HashDB` implementation.
|
2016-03-11 13:50:39 +01:00
|
|
|
|
2017-07-29 17:12:07 +02:00
|
|
|
use std::{
|
2020-09-14 16:08:57 +02:00
|
|
|
collections::{hash_map::Entry, BTreeMap, HashMap},
|
2018-07-06 15:09:39 +02:00
|
|
|
io,
|
2017-07-29 21:56:42 +02:00
|
|
|
sync::Arc,
|
|
|
|
};
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-07-29 21:56:42 +02:00
|
|
|
use super::{
|
2020-08-24 14:18:03 +02:00
|
|
|
error_key_already_exists, error_negatively_reference_hash, memory_db::*, LATEST_ERA_KEY,
|
2020-08-05 06:08:03 +02:00
|
|
|
};
|
2021-07-05 17:06:35 +02:00
|
|
|
use bytes::Bytes;
|
2021-03-03 22:44:35 +01:00
|
|
|
use ethcore_db::{DBTransaction, DBValue, KeyValueDB};
|
2018-07-02 18:50:05 +02:00
|
|
|
use ethereum_types::H256;
|
2019-02-20 19:09:34 +01:00
|
|
|
use hash_db::HashDB;
|
2018-07-02 18:50:05 +02:00
|
|
|
use keccak_hasher::KeccakHasher;
|
|
|
|
use rlp::{decode, encode};
|
2017-10-16 16:12:54 +02:00
|
|
|
use traits::JournalDB;
|
2021-07-05 17:06:35 +02:00
|
|
|
use DB_PREFIX_LEN;
|
2016-03-11 13:50:39 +01:00
|
|
|
|
2016-04-06 10:07:24 +02:00
|
|
|
/// Implementation of the `HashDB` trait for a disk-backed database with a memory overlay
|
2016-03-11 13:50:39 +01:00
|
|
|
/// and latent-removal semantics.
|
|
|
|
///
|
2016-04-06 10:07:24 +02:00
|
|
|
/// Like `OverlayDB`, there is a memory overlay; `commit()` must be called in order to
|
|
|
|
/// write operations out to disk. Unlike `OverlayDB`, `remove()` operations do not take effect
|
2016-09-26 17:14:44 +02:00
|
|
|
/// immediately. As this is an "archive" database, nothing is ever removed. This means
|
|
|
|
/// that the states of any block the node has ever processed will be accessible.
|
2016-03-11 13:50:39 +01:00
|
|
|
pub struct ArchiveDB {
|
2018-10-09 22:07:25 +02:00
|
|
|
overlay: MemoryDB<KeccakHasher, DBValue>,
|
2020-07-29 10:36:15 +02:00
|
|
|
backing: Arc<dyn KeyValueDB>,
|
2016-03-13 18:19:52 +01:00
|
|
|
latest_era: Option<u64>,
|
2016-07-28 23:46:24 +02:00
|
|
|
column: Option<u32>,
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ArchiveDB {
|
2017-02-20 17:21:55 +01:00
|
|
|
/// Create a new instance from a key-value db.
|
2020-07-29 10:36:15 +02:00
|
|
|
pub fn new(backing: Arc<dyn KeyValueDB>, column: Option<u32>) -> ArchiveDB {
|
2018-05-08 11:22:12 +02:00
|
|
|
let latest_era = backing
|
|
|
|
.get(column, &LATEST_ERA_KEY)
|
|
|
|
.expect("Low-level database error.")
|
|
|
|
.map(|val| decode::<u64>(&val).expect("decoding db value failed"));
|
2016-03-11 13:50:39 +01:00
|
|
|
ArchiveDB {
|
2019-02-20 19:09:34 +01:00
|
|
|
overlay: ::new_memory_db(),
|
2018-05-08 11:22:12 +02:00
|
|
|
backing,
|
|
|
|
latest_era,
|
|
|
|
column,
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-10-26 13:53:47 +02:00
|
|
|
fn payload(&self, key: &H256) -> Option<DBValue> {
|
|
|
|
self.backing
|
2021-03-12 10:12:42 +01:00
|
|
|
.get(self.column, key.as_bytes())
|
2016-10-26 13:53:47 +02:00
|
|
|
.expect("Low-level database error. Some issue with your hard disk?")
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 22:07:25 +02:00
|
|
|
impl HashDB<KeccakHasher, DBValue> for ArchiveDB {
|
2016-10-26 13:53:47 +02:00
|
|
|
fn get(&self, key: &H256) -> Option<DBValue> {
|
2017-08-26 19:09:32 +02:00
|
|
|
if let Some((d, rc)) = self.overlay.raw(key) {
|
|
|
|
if rc > 0 {
|
2019-02-20 19:09:34 +01:00
|
|
|
return Some(d.clone());
|
2017-08-26 19:09:32 +02:00
|
|
|
}
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
2016-10-26 13:53:47 +02:00
|
|
|
self.payload(key)
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-06-23 11:16:11 +02:00
|
|
|
fn contains(&self, key: &H256) -> bool {
|
|
|
|
self.get(key).is_some()
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
fn insert(&mut self, value: &[u8]) -> H256 {
|
|
|
|
self.overlay.insert(value)
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-10-26 13:53:47 +02:00
|
|
|
fn emplace(&mut self, key: H256, value: DBValue) {
|
2016-03-11 13:50:39 +01:00
|
|
|
self.overlay.emplace(key, value);
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-06-23 11:16:11 +02:00
|
|
|
fn remove(&mut self, key: &H256) {
|
|
|
|
self.overlay.remove(key);
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-20 19:09:34 +01:00
|
|
|
impl ::traits::KeyedHashDB for ArchiveDB {
|
|
|
|
fn keys(&self) -> HashMap<H256, i32> {
|
|
|
|
let mut ret: HashMap<H256, i32> = self
|
|
|
|
.backing
|
|
|
|
.iter(self.column)
|
|
|
|
.map(|(key, _)| (H256::from_slice(&*key), 1))
|
|
|
|
.collect();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2019-02-20 19:09:34 +01:00
|
|
|
for (key, refs) in self.overlay.keys() {
|
|
|
|
match ret.entry(key) {
|
|
|
|
Entry::Occupied(mut entry) => {
|
|
|
|
*entry.get_mut() += refs;
|
|
|
|
}
|
|
|
|
Entry::Vacant(entry) => {
|
|
|
|
entry.insert(refs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
impl JournalDB for ArchiveDB {
|
2020-07-29 10:36:15 +02:00
|
|
|
fn boxed_clone(&self) -> Box<dyn JournalDB> {
|
2016-03-11 13:50:39 +01:00
|
|
|
Box::new(ArchiveDB {
|
2016-03-27 14:35:27 +02:00
|
|
|
overlay: self.overlay.clone(),
|
2016-03-11 13:50:39 +01:00
|
|
|
backing: self.backing.clone(),
|
2016-03-13 19:22:42 +01:00
|
|
|
latest_era: self.latest_era,
|
2016-07-28 23:46:24 +02:00
|
|
|
column: self.column.clone(),
|
2016-03-11 13:50:39 +01:00
|
|
|
})
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2020-09-14 16:08:57 +02:00
|
|
|
fn get_sizes(&self, sizes: &mut BTreeMap<String, usize>) {
|
|
|
|
sizes.insert(String::from("db_archive_overlay"), self.overlay.len());
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
fn is_empty(&self) -> bool {
|
2016-03-13 18:19:52 +01:00
|
|
|
self.latest_era.is_none()
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-07-06 15:09:39 +02:00
|
|
|
fn journal_under(
|
|
|
|
&mut self,
|
|
|
|
batch: &mut DBTransaction,
|
|
|
|
now: u64,
|
|
|
|
_id: &H256,
|
|
|
|
) -> io::Result<u32> {
|
2016-03-11 13:50:39 +01:00
|
|
|
let mut inserts = 0usize;
|
|
|
|
let mut deletes = 0usize;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-10-27 08:28:12 +02:00
|
|
|
for i in self.overlay.drain() {
|
2016-03-11 13:50:39 +01:00
|
|
|
let (key, (value, rc)) = i;
|
|
|
|
if rc > 0 {
|
2021-03-12 10:12:42 +01:00
|
|
|
batch.put(self.column, key.as_bytes(), &value);
|
2016-03-11 13:50:39 +01:00
|
|
|
inserts += 1;
|
|
|
|
}
|
|
|
|
if rc < 0 {
|
|
|
|
assert!(rc == -1);
|
|
|
|
deletes += 1;
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-13 19:22:42 +01:00
|
|
|
if self.latest_era.map_or(true, |e| now > e) {
|
2016-08-18 09:43:56 +02:00
|
|
|
batch.put(self.column, &LATEST_ERA_KEY, &encode(&now));
|
2016-03-13 19:22:42 +01:00
|
|
|
self.latest_era = Some(now);
|
|
|
|
}
|
2016-03-11 13:50:39 +01:00
|
|
|
Ok((inserts + deletes) as u32)
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-07-06 15:09:39 +02:00
|
|
|
fn mark_canonical(
|
|
|
|
&mut self,
|
|
|
|
_batch: &mut DBTransaction,
|
|
|
|
_end_era: u64,
|
|
|
|
_canon_id: &H256,
|
|
|
|
) -> io::Result<u32> {
|
2016-09-26 17:14:44 +02:00
|
|
|
// keep everything! it's an archive, after all.
|
|
|
|
Ok(0)
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-07-06 15:09:39 +02:00
|
|
|
fn inject(&mut self, batch: &mut DBTransaction) -> io::Result<u32> {
|
2016-08-03 16:34:32 +02:00
|
|
|
let mut inserts = 0usize;
|
|
|
|
let mut deletes = 0usize;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-10-27 08:28:12 +02:00
|
|
|
for i in self.overlay.drain() {
|
2016-08-03 16:34:32 +02:00
|
|
|
let (key, (value, rc)) = i;
|
|
|
|
if rc > 0 {
|
2021-03-12 10:12:42 +01:00
|
|
|
if self.backing.get(self.column, key.as_bytes())?.is_some() {
|
2018-07-06 15:09:39 +02:00
|
|
|
return Err(error_key_already_exists(&key));
|
2016-08-03 16:34:32 +02:00
|
|
|
}
|
2021-03-12 10:12:42 +01:00
|
|
|
batch.put(self.column, key.as_bytes(), &value);
|
2016-08-03 16:34:32 +02:00
|
|
|
inserts += 1;
|
|
|
|
}
|
|
|
|
if rc < 0 {
|
|
|
|
assert!(rc == -1);
|
2021-03-12 10:12:42 +01:00
|
|
|
if self.backing.get(self.column, key.as_bytes())?.is_none() {
|
2018-07-06 15:09:39 +02:00
|
|
|
return Err(error_negatively_reference_hash(&key));
|
2016-08-03 16:34:32 +02:00
|
|
|
}
|
2021-03-12 10:12:42 +01:00
|
|
|
batch.delete(self.column, key.as_bytes());
|
2016-08-03 16:34:32 +02:00
|
|
|
deletes += 1;
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
2016-08-03 16:34:32 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-08-03 16:34:32 +02:00
|
|
|
Ok((inserts + deletes) as u32)
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-04-12 03:42:50 +02:00
|
|
|
fn latest_era(&self) -> Option<u64> {
|
|
|
|
self.latest_era
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-06-03 12:10:10 +02:00
|
|
|
fn is_pruned(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2020-07-29 10:36:15 +02:00
|
|
|
fn backing(&self) -> &Arc<dyn KeyValueDB> {
|
2016-07-28 23:46:24 +02:00
|
|
|
&self.backing
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-10-09 22:07:25 +02:00
|
|
|
fn consolidate(&mut self, with: MemoryDB<KeccakHasher, DBValue>) {
|
2016-08-25 14:28:45 +02:00
|
|
|
self.overlay.consolidate(with);
|
|
|
|
}
|
2021-07-05 17:06:35 +02:00
|
|
|
|
|
|
|
fn state(&self, id: &H256) -> Option<Bytes> {
|
|
|
|
self.backing
|
|
|
|
.get_by_prefix(self.column, &id[0..DB_PREFIX_LEN])
|
|
|
|
.map(|b| b.into_vec())
|
|
|
|
}
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2016-03-18 10:14:19 +01:00
|
|
|
|
2017-08-30 16:20:21 +02:00
|
|
|
use super::*;
|
2021-07-05 17:06:35 +02:00
|
|
|
use ethcore_db::InMemoryWithMetrics;
|
2019-02-20 19:09:34 +01:00
|
|
|
use hash_db::HashDB;
|
2016-03-11 13:50:39 +01:00
|
|
|
use keccak::keccak;
|
2017-10-17 10:40:45 +02:00
|
|
|
use JournalDB;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
#[test]
|
|
|
|
fn insert_same_in_fork() {
|
|
|
|
// history is 1
|
2021-03-03 22:44:35 +01:00
|
|
|
let mut jdb = ArchiveDB::new(Arc::new(ethcore_db::InMemoryWithMetrics::create(0)), None);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
let x = jdb.insert(b"X");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(1, &keccak(b"1"), None).unwrap();
|
|
|
|
jdb.commit_batch(2, &keccak(b"2"), None).unwrap();
|
|
|
|
jdb.commit_batch(3, &keccak(b"1002a"), Some((1, keccak(b"1"))))
|
|
|
|
.unwrap();
|
|
|
|
jdb.commit_batch(4, &keccak(b"1003a"), Some((2, keccak(b"2"))))
|
|
|
|
.unwrap();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
jdb.remove(&x);
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(3, &keccak(b"1002b"), Some((1, keccak(b"1"))))
|
|
|
|
.unwrap();
|
2016-03-11 13:50:39 +01:00
|
|
|
let x = jdb.insert(b"X");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(4, &keccak(b"1003b"), Some((2, keccak(b"2"))))
|
|
|
|
.unwrap();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(5, &keccak(b"1004a"), Some((3, keccak(b"1002a"))))
|
|
|
|
.unwrap();
|
|
|
|
jdb.commit_batch(6, &keccak(b"1005a"), Some((4, keccak(b"1003a"))))
|
|
|
|
.unwrap();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&x));
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
#[test]
|
|
|
|
fn long_history() {
|
|
|
|
// history is 3
|
2021-03-03 22:44:35 +01:00
|
|
|
let mut jdb = ArchiveDB::new(Arc::new(ethcore_db::InMemoryWithMetrics::create(0)), None);
|
2016-03-11 13:50:39 +01:00
|
|
|
let h = jdb.insert(b"foo");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(0, &keccak(b"0"), None).unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&h));
|
2016-03-11 13:50:39 +01:00
|
|
|
jdb.remove(&h);
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(1, &keccak(b"1"), None).unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&h));
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(2, &keccak(b"2"), None).unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&h));
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(3, &keccak(b"3"), Some((0, keccak(b"0"))))
|
|
|
|
.unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&h));
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(4, &keccak(b"4"), Some((1, keccak(b"1"))))
|
|
|
|
.unwrap();
|
2017-06-09 18:13:50 +02:00
|
|
|
assert!(jdb.contains(&h));
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-06-09 18:23:25 +02:00
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn multiple_owed_removal_not_allowed() {
|
2021-03-03 22:44:35 +01:00
|
|
|
let mut jdb = ArchiveDB::new(Arc::new(ethcore_db::InMemoryWithMetrics::create(0)), None);
|
2017-06-09 18:23:25 +02:00
|
|
|
let h = jdb.insert(b"foo");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(0, &keccak(b"0"), None).unwrap();
|
2017-06-09 18:23:25 +02:00
|
|
|
assert!(jdb.contains(&h));
|
|
|
|
jdb.remove(&h);
|
|
|
|
jdb.remove(&h);
|
|
|
|
// commit_batch would call journal_under(),
|
|
|
|
// and we don't allow multiple owned removals.
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(1, &keccak(b"1"), None).unwrap();
|
2017-06-09 18:23:25 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
#[test]
|
|
|
|
fn complex() {
|
|
|
|
// history is 1
|
2021-03-03 22:44:35 +01:00
|
|
|
let mut jdb = ArchiveDB::new(Arc::new(ethcore_db::InMemoryWithMetrics::create(0)), None);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
let foo = jdb.insert(b"foo");
|
|
|
|
let bar = jdb.insert(b"bar");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(0, &keccak(b"0"), None).unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
|
|
|
assert!(jdb.contains(&bar));
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
jdb.remove(&foo);
|
|
|
|
jdb.remove(&bar);
|
|
|
|
let baz = jdb.insert(b"baz");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(1, &keccak(b"1"), Some((0, keccak(b"0"))))
|
|
|
|
.unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
|
|
|
assert!(jdb.contains(&bar));
|
|
|
|
assert!(jdb.contains(&baz));
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
let foo = jdb.insert(b"foo");
|
|
|
|
jdb.remove(&baz);
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(2, &keccak(b"2"), Some((1, keccak(b"1"))))
|
|
|
|
.unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
|
|
|
assert!(jdb.contains(&baz));
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
jdb.remove(&foo);
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(3, &keccak(b"3"), Some((2, keccak(b"2"))))
|
|
|
|
.unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(4, &keccak(b"4"), Some((3, keccak(b"3"))))
|
|
|
|
.unwrap();
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
#[test]
|
|
|
|
fn fork() {
|
|
|
|
// history is 1
|
2021-03-03 22:44:35 +01:00
|
|
|
let mut jdb = ArchiveDB::new(Arc::new(ethcore_db::InMemoryWithMetrics::create(0)), None);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
let foo = jdb.insert(b"foo");
|
|
|
|
let bar = jdb.insert(b"bar");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(0, &keccak(b"0"), None).unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
|
|
|
assert!(jdb.contains(&bar));
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
jdb.remove(&foo);
|
|
|
|
let baz = jdb.insert(b"baz");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(1, &keccak(b"1a"), Some((0, keccak(b"0"))))
|
|
|
|
.unwrap();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
jdb.remove(&bar);
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(1, &keccak(b"1b"), Some((0, keccak(b"0"))))
|
|
|
|
.unwrap();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
|
|
|
assert!(jdb.contains(&bar));
|
|
|
|
assert!(jdb.contains(&baz));
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(2, &keccak(b"2b"), Some((1, keccak(b"1b"))))
|
|
|
|
.unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
#[test]
|
|
|
|
fn overwrite() {
|
|
|
|
// history is 1
|
2021-03-03 22:44:35 +01:00
|
|
|
let mut jdb = ArchiveDB::new(Arc::new(ethcore_db::InMemoryWithMetrics::create(0)), None);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
let foo = jdb.insert(b"foo");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(0, &keccak(b"0"), None).unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
jdb.remove(&foo);
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(1, &keccak(b"1"), Some((0, keccak(b"0"))))
|
|
|
|
.unwrap();
|
2016-03-11 13:50:39 +01:00
|
|
|
jdb.insert(b"foo");
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(2, &keccak(b"2"), Some((1, keccak(b"1"))))
|
|
|
|
.unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(3, &keccak(b"2"), Some((0, keccak(b"2"))))
|
|
|
|
.unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
#[test]
|
|
|
|
fn fork_same_key() {
|
|
|
|
// history is 1
|
2021-03-03 22:44:35 +01:00
|
|
|
let mut jdb = ArchiveDB::new(Arc::new(ethcore_db::InMemoryWithMetrics::create(0)), None);
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(0, &keccak(b"0"), None).unwrap();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
let foo = jdb.insert(b"foo");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(1, &keccak(b"1a"), Some((0, keccak(b"0"))))
|
|
|
|
.unwrap();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
jdb.insert(b"foo");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(1, &keccak(b"1b"), Some((0, keccak(b"0"))))
|
|
|
|
.unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(2, &keccak(b"2a"), Some((1, keccak(b"1a"))))
|
|
|
|
.unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
#[test]
|
|
|
|
fn reopen() {
|
2021-03-03 22:44:35 +01:00
|
|
|
let shared_db = Arc::new(ethcore_db::InMemoryWithMetrics::create(0));
|
2016-03-11 13:50:39 +01:00
|
|
|
let bar = H256::random();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
let foo = {
|
2017-10-13 17:10:03 +02:00
|
|
|
let mut jdb = ArchiveDB::new(shared_db.clone(), None);
|
2016-03-11 13:50:39 +01:00
|
|
|
// history is 1
|
|
|
|
let foo = jdb.insert(b"foo");
|
2016-10-26 13:53:47 +02:00
|
|
|
jdb.emplace(bar.clone(), DBValue::from_slice(b"bar"));
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(0, &keccak(b"0"), None).unwrap();
|
2016-03-11 13:50:39 +01:00
|
|
|
foo
|
|
|
|
};
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
{
|
2017-10-13 17:10:03 +02:00
|
|
|
let mut jdb = ArchiveDB::new(shared_db.clone(), None);
|
2016-03-11 13:50:39 +01:00
|
|
|
jdb.remove(&foo);
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(1, &keccak(b"1"), Some((0, keccak(b"0"))))
|
|
|
|
.unwrap();
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
{
|
2017-10-13 17:10:03 +02:00
|
|
|
let mut jdb = ArchiveDB::new(shared_db, None);
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
|
|
|
assert!(jdb.contains(&bar));
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(2, &keccak(b"2"), Some((1, keccak(b"1"))))
|
|
|
|
.unwrap();
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
#[test]
|
|
|
|
fn reopen_remove() {
|
2021-03-03 22:44:35 +01:00
|
|
|
let shared_db = Arc::new(ethcore_db::InMemoryWithMetrics::create(0));
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
let foo = {
|
2017-10-13 17:10:03 +02:00
|
|
|
let mut jdb = ArchiveDB::new(shared_db.clone(), None);
|
2016-03-11 13:50:39 +01:00
|
|
|
// history is 1
|
|
|
|
let foo = jdb.insert(b"foo");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(0, &keccak(b"0"), None).unwrap();
|
|
|
|
jdb.commit_batch(1, &keccak(b"1"), Some((0, keccak(b"0"))))
|
|
|
|
.unwrap();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
// foo is ancient history.
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
jdb.insert(b"foo");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(2, &keccak(b"2"), Some((1, keccak(b"1"))))
|
|
|
|
.unwrap();
|
2016-03-11 13:50:39 +01:00
|
|
|
foo
|
|
|
|
};
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
{
|
2017-10-13 17:10:03 +02:00
|
|
|
let mut jdb = ArchiveDB::new(shared_db, None);
|
2016-03-11 13:50:39 +01:00
|
|
|
jdb.remove(&foo);
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(3, &keccak(b"3"), Some((2, keccak(b"2"))))
|
|
|
|
.unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
2016-03-11 13:50:39 +01:00
|
|
|
jdb.remove(&foo);
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(4, &keccak(b"4"), Some((3, keccak(b"3"))))
|
|
|
|
.unwrap();
|
|
|
|
jdb.commit_batch(5, &keccak(b"5"), Some((4, keccak(b"4"))))
|
|
|
|
.unwrap();
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
#[test]
|
|
|
|
fn reopen_fork() {
|
2021-03-03 22:44:35 +01:00
|
|
|
let shared_db = Arc::new(ethcore_db::InMemoryWithMetrics::create(0));
|
2016-03-11 22:43:59 +01:00
|
|
|
let (foo, _, _) = {
|
2017-10-13 17:10:03 +02:00
|
|
|
let mut jdb = ArchiveDB::new(shared_db.clone(), None);
|
2016-03-11 13:50:39 +01:00
|
|
|
// history is 1
|
|
|
|
let foo = jdb.insert(b"foo");
|
|
|
|
let bar = jdb.insert(b"bar");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(0, &keccak(b"0"), None).unwrap();
|
2016-03-11 13:50:39 +01:00
|
|
|
jdb.remove(&foo);
|
|
|
|
let baz = jdb.insert(b"baz");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(1, &keccak(b"1a"), Some((0, keccak(b"0"))))
|
|
|
|
.unwrap();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
jdb.remove(&bar);
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(1, &keccak(b"1b"), Some((0, keccak(b"0"))))
|
|
|
|
.unwrap();
|
2016-03-11 13:50:39 +01:00
|
|
|
(foo, bar, baz)
|
|
|
|
};
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-03-11 13:50:39 +01:00
|
|
|
{
|
2017-10-13 17:10:03 +02:00
|
|
|
let mut jdb = ArchiveDB::new(shared_db, None);
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(2, &keccak(b"2b"), Some((1, keccak(b"1b"))))
|
|
|
|
.unwrap();
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
|
|
|
|
2016-08-03 16:34:32 +02:00
|
|
|
#[test]
|
|
|
|
fn inject() {
|
2021-03-03 22:44:35 +01:00
|
|
|
let mut jdb = ArchiveDB::new(Arc::new(ethcore_db::InMemoryWithMetrics::create(0)), None);
|
2016-08-03 16:34:32 +02:00
|
|
|
let key = jdb.insert(b"dog");
|
|
|
|
jdb.inject_batch().unwrap();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-10-26 13:53:47 +02:00
|
|
|
assert_eq!(jdb.get(&key).unwrap(), DBValue::from_slice(b"dog"));
|
2016-08-03 16:34:32 +02:00
|
|
|
jdb.remove(&key);
|
|
|
|
jdb.inject_batch().unwrap();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-08-03 16:34:32 +02:00
|
|
|
assert!(jdb.get(&key).is_none());
|
|
|
|
}
|
2021-07-05 17:06:35 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn returns_state() {
|
|
|
|
let shared_db = Arc::new(InMemoryWithMetrics::create(0));
|
|
|
|
|
|
|
|
let key = {
|
|
|
|
let mut jdb = ArchiveDB::new(shared_db.clone(), None);
|
|
|
|
let key = jdb.insert(b"foo");
|
|
|
|
jdb.commit_batch(0, &keccak(b"0"), None).unwrap();
|
|
|
|
key
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
let jdb = ArchiveDB::new(shared_db, None);
|
|
|
|
let state = jdb.state(&key);
|
|
|
|
assert!(state.is_some());
|
|
|
|
}
|
|
|
|
}
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|