2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2016-03-13 18:07:10 +01:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2016-03-13 18:07:10 +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.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2016-03-13 18:07:10 +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
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-03-13 18:07:10 +01:00
|
|
|
|
2016-04-06 10:07:24 +02:00
|
|
|
//! Disk-backed, ref-counted `JournalDB` implementation.
|
2016-03-13 18:07:10 +01:00
|
|
|
|
2017-07-29 17:12:07 +02:00
|
|
|
use std::collections::HashMap;
|
2018-07-06 15:09:39 +02:00
|
|
|
use std::io;
|
2017-07-29 21:56:42 +02:00
|
|
|
use std::sync::Arc;
|
2018-07-02 18:50:05 +02:00
|
|
|
|
|
|
|
use bytes::Bytes;
|
|
|
|
use ethereum_types::H256;
|
2019-06-19 13:54:05 +02:00
|
|
|
use hash_db::{HashDB, Prefix, EMPTY_PREFIX};
|
|
|
|
use parity_util_mem::{MallocSizeOf, allocators::new_malloc_size_ops};
|
2018-07-02 18:50:05 +02:00
|
|
|
use keccak_hasher::KeccakHasher;
|
2018-10-09 22:07:25 +02:00
|
|
|
use kvdb::{KeyValueDB, DBTransaction, DBValue};
|
2018-07-02 18:50:05 +02:00
|
|
|
use overlaydb::OverlayDB;
|
|
|
|
use rlp::{encode, decode};
|
2016-07-28 23:46:24 +02:00
|
|
|
use super::{DB_PREFIX_LEN, LATEST_ERA_KEY};
|
2016-03-13 18:07:10 +01:00
|
|
|
use super::traits::JournalDB;
|
2018-03-15 11:14:38 +01:00
|
|
|
use util::{DatabaseKey, DatabaseValueView, DatabaseValueRef};
|
2016-03-13 18:07:10 +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-13 18:07:10 +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-03-13 18:07:10 +01:00
|
|
|
/// immediately. Rather some age (based on a linear but arbitrary metric) must pass before
|
|
|
|
/// the removals actually take effect.
|
2016-09-26 17:14:44 +02:00
|
|
|
///
|
|
|
|
/// journal format:
|
2018-05-11 11:34:07 +02:00
|
|
|
/// ```text
|
2016-09-26 17:14:44 +02:00
|
|
|
/// [era, 0] => [ id, [insert_0, ...], [remove_0, ...] ]
|
|
|
|
/// [era, 1] => [ id, [insert_0, ...], [remove_0, ...] ]
|
|
|
|
/// [era, n] => [ ... ]
|
2016-10-27 08:28:12 +02:00
|
|
|
/// ```
|
2016-09-26 17:14:44 +02:00
|
|
|
///
|
|
|
|
/// when we make a new commit, we journal the inserts and removes.
|
2016-10-27 08:28:12 +02:00
|
|
|
/// for each `end_era` that we journaled that we are no passing by,
|
2016-09-26 17:14:44 +02:00
|
|
|
/// we remove all of its removes assuming it is canonical and all
|
|
|
|
/// of its inserts otherwise.
|
|
|
|
// TODO: store last_era, reclaim_period.
|
2016-03-13 18:07:10 +01:00
|
|
|
pub struct RefCountedDB {
|
|
|
|
forward: OverlayDB,
|
2017-02-20 17:21:55 +01:00
|
|
|
backing: Arc<KeyValueDB>,
|
2016-03-13 18:19:52 +01:00
|
|
|
latest_era: Option<u64>,
|
2016-03-13 18:07:10 +01:00
|
|
|
inserts: Vec<H256>,
|
|
|
|
removes: Vec<H256>,
|
2016-07-28 23:46:24 +02:00
|
|
|
column: Option<u32>,
|
2016-03-13 18:07:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RefCountedDB {
|
|
|
|
/// Create a new instance given a `backing` database.
|
2018-05-08 11:22:12 +02:00
|
|
|
pub fn new(backing: Arc<KeyValueDB>, column: Option<u32>) -> RefCountedDB {
|
|
|
|
let latest_era = backing.get(column, &LATEST_ERA_KEY)
|
|
|
|
.expect("Low-level database error.")
|
|
|
|
.map(|v| decode::<u64>(&v).expect("decoding db value failed"));
|
2016-03-13 18:19:52 +01:00
|
|
|
|
2016-03-13 18:07:10 +01:00
|
|
|
RefCountedDB {
|
2018-05-08 11:22:12 +02:00
|
|
|
forward: OverlayDB::new(backing.clone(), column),
|
|
|
|
backing,
|
2016-03-13 18:07:10 +01:00
|
|
|
inserts: vec![],
|
|
|
|
removes: vec![],
|
2018-05-08 11:22:12 +02:00
|
|
|
latest_era,
|
|
|
|
column,
|
2016-03-13 18:07:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 22:07:25 +02:00
|
|
|
impl HashDB<KeccakHasher, DBValue> for RefCountedDB {
|
2019-06-19 13:54:05 +02:00
|
|
|
fn get(&self, key: &H256, prefix: Prefix) -> Option<DBValue> { self.forward.get(key, prefix) }
|
|
|
|
fn contains(&self, key: &H256, prefix: Prefix) -> bool { self.forward.contains(key, prefix) }
|
|
|
|
fn insert(&mut self, prefix: Prefix, value: &[u8]) -> H256 { let r = self.forward.insert(prefix, value); self.inserts.push(r.clone()); r }
|
|
|
|
fn emplace(&mut self, key: H256, prefix: Prefix, value: DBValue) { self.inserts.push(key.clone()); self.forward.emplace(key, prefix, value); }
|
|
|
|
fn remove(&mut self, key: &H256, _prefix: Prefix) { self.removes.push(key.clone()); }
|
2016-03-13 18:07:10 +01:00
|
|
|
}
|
|
|
|
|
2019-02-20 19:09:34 +01:00
|
|
|
impl ::traits::KeyedHashDB for RefCountedDB {
|
|
|
|
fn keys(&self) -> HashMap<H256, i32> { self.forward.keys() }
|
|
|
|
}
|
|
|
|
|
2016-03-13 18:07:10 +01:00
|
|
|
impl JournalDB for RefCountedDB {
|
2016-03-28 09:42:50 +02:00
|
|
|
fn boxed_clone(&self) -> Box<JournalDB> {
|
2016-03-13 18:07:10 +01:00
|
|
|
Box::new(RefCountedDB {
|
|
|
|
forward: self.forward.clone(),
|
|
|
|
backing: self.backing.clone(),
|
2016-03-13 18:19:52 +01:00
|
|
|
latest_era: self.latest_era,
|
2016-03-13 18:07:10 +01:00
|
|
|
inserts: self.inserts.clone(),
|
|
|
|
removes: self.removes.clone(),
|
2016-07-28 23:46:24 +02:00
|
|
|
column: self.column.clone(),
|
2016-03-13 18:07:10 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mem_used(&self) -> usize {
|
2019-06-19 13:54:05 +02:00
|
|
|
let mut ops = new_malloc_size_ops();
|
|
|
|
self.inserts.size_of(&mut ops) + self.removes.size_of(&mut ops)
|
2016-03-13 18:07:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_empty(&self) -> bool {
|
2016-03-13 18:19:52 +01:00
|
|
|
self.latest_era.is_none()
|
2016-03-13 18:07:10 +01:00
|
|
|
}
|
|
|
|
|
2017-02-20 17:21:55 +01:00
|
|
|
fn backing(&self) -> &Arc<KeyValueDB> {
|
2016-07-28 23:46:24 +02:00
|
|
|
&self.backing
|
|
|
|
}
|
|
|
|
|
2016-04-12 03:42:50 +02:00
|
|
|
fn latest_era(&self) -> Option<u64> { self.latest_era }
|
2016-04-12 00:51:14 +02:00
|
|
|
|
2016-07-11 12:34:29 +02:00
|
|
|
fn state(&self, id: &H256) -> Option<Bytes> {
|
2017-06-28 09:36:42 +02:00
|
|
|
self.backing.get_by_prefix(self.column, &id[0..DB_PREFIX_LEN]).map(|b| b.into_vec())
|
2016-07-11 12:34:29 +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-13 18:07:10 +01:00
|
|
|
// record new commit's details.
|
2018-03-15 11:14:38 +01:00
|
|
|
let mut db_key = DatabaseKey {
|
|
|
|
era: now,
|
|
|
|
index: 0usize,
|
|
|
|
};
|
2016-09-26 17:14:44 +02:00
|
|
|
let mut last;
|
2016-03-13 18:07:10 +01:00
|
|
|
|
2016-12-27 12:53:56 +01:00
|
|
|
while self.backing.get(self.column, {
|
2018-03-15 11:14:38 +01:00
|
|
|
last = encode(&db_key);
|
2016-09-26 17:14:44 +02:00
|
|
|
&last
|
2016-12-27 12:53:56 +01:00
|
|
|
})?.is_some() {
|
2018-03-15 11:14:38 +01:00
|
|
|
db_key.index += 1;
|
2016-09-26 17:14:44 +02:00
|
|
|
}
|
2016-03-18 10:14:19 +01:00
|
|
|
|
2018-03-15 11:14:38 +01:00
|
|
|
{
|
|
|
|
let value_ref = DatabaseValueRef {
|
|
|
|
id,
|
|
|
|
inserts: &self.inserts,
|
|
|
|
deletes: &self.removes,
|
|
|
|
};
|
|
|
|
|
|
|
|
batch.put(self.column, &last, &encode(&value_ref));
|
|
|
|
}
|
2016-03-13 21:21:30 +01:00
|
|
|
|
2016-09-26 17:14:44 +02:00
|
|
|
let ops = self.inserts.len() + self.removes.len();
|
2016-03-13 18:19:52 +01:00
|
|
|
|
2018-03-15 11:14:38 +01:00
|
|
|
trace!(target: "rcdb", "new journal for time #{}.{} => {}: inserts={:?}, removes={:?}", now, db_key.index, id, self.inserts, self.removes);
|
2016-09-26 17:14:44 +02:00
|
|
|
|
|
|
|
self.inserts.clear();
|
|
|
|
self.removes.clear();
|
|
|
|
|
|
|
|
if self.latest_era.map_or(true, |e| now > e) {
|
|
|
|
batch.put(self.column, &LATEST_ERA_KEY, &encode(&now));
|
|
|
|
self.latest_era = Some(now);
|
2016-03-13 18:07:10 +01:00
|
|
|
}
|
|
|
|
|
2016-09-26 17:14:44 +02:00
|
|
|
Ok(ops as u32)
|
|
|
|
}
|
|
|
|
|
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-03-13 18:07:10 +01:00
|
|
|
// apply old commits' details
|
2018-03-15 11:14:38 +01:00
|
|
|
let mut db_key = DatabaseKey {
|
|
|
|
era: end_era,
|
|
|
|
index: 0usize,
|
|
|
|
};
|
2016-09-26 17:14:44 +02:00
|
|
|
let mut last;
|
|
|
|
while let Some(rlp_data) = {
|
2016-12-27 12:53:56 +01:00
|
|
|
self.backing.get(self.column, {
|
2018-03-15 11:14:38 +01:00
|
|
|
last = encode(&db_key);
|
2016-09-26 17:14:44 +02:00
|
|
|
&last
|
2016-12-27 12:53:56 +01:00
|
|
|
})?
|
2016-09-26 17:14:44 +02:00
|
|
|
} {
|
2018-03-20 03:02:07 +01:00
|
|
|
let view = DatabaseValueView::from_rlp(&rlp_data);
|
2018-03-15 11:14:38 +01:00
|
|
|
let our_id = view.id().expect("rlp read from db; qed");
|
|
|
|
let to_remove = if canon_id == &our_id {
|
|
|
|
view.deletes()
|
|
|
|
} else {
|
|
|
|
view.inserts()
|
|
|
|
}.expect("rlp read from db; qed");
|
|
|
|
trace!(target: "rcdb", "delete journal for time #{}.{}=>{}, (canon was {}): deleting {:?}", end_era, db_key.index, our_id, canon_id, to_remove);
|
2016-09-26 17:14:44 +02:00
|
|
|
for i in &to_remove {
|
2019-06-19 13:54:05 +02:00
|
|
|
self.forward.remove(i, EMPTY_PREFIX);
|
2016-03-13 18:07:10 +01:00
|
|
|
}
|
2016-09-26 17:14:44 +02:00
|
|
|
batch.delete(self.column, &last);
|
2018-03-15 11:14:38 +01:00
|
|
|
db_key.index += 1;
|
2016-03-13 18:07:10 +01:00
|
|
|
}
|
|
|
|
|
2016-12-27 12:53:56 +01:00
|
|
|
let r = self.forward.commit_to_batch(batch)?;
|
2016-03-13 18:07:10 +01:00
|
|
|
Ok(r)
|
|
|
|
}
|
2016-08-03 16:34:32 +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
|
|
|
self.inserts.clear();
|
|
|
|
for remove in self.removes.drain(..) {
|
2019-06-19 13:54:05 +02:00
|
|
|
self.forward.remove(&remove, EMPTY_PREFIX);
|
2016-08-03 16:34:32 +02:00
|
|
|
}
|
2016-08-10 16:29:40 +02:00
|
|
|
self.forward.commit_to_batch(batch)
|
2016-08-03 16:34:32 +02:00
|
|
|
}
|
2016-08-25 14:28:45 +02:00
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
fn consolidate(&mut self, mut with: super::MemoryDB) {
|
2016-08-25 14:28:45 +02:00
|
|
|
for (key, (value, rc)) in with.drain() {
|
|
|
|
for _ in 0..rc {
|
2019-06-19 13:54:05 +02:00
|
|
|
self.emplace(key, EMPTY_PREFIX, value.clone());
|
2016-08-25 14:28:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _ in rc..0 {
|
2019-06-19 13:54:05 +02:00
|
|
|
self.remove(&key, EMPTY_PREFIX);
|
2016-08-25 14:28:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-13 18:07:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2016-03-18 10:14:19 +01:00
|
|
|
|
2017-08-30 16:20:21 +02:00
|
|
|
use keccak::keccak;
|
2019-06-19 13:54:05 +02:00
|
|
|
use hash_db::{HashDB, EMPTY_PREFIX};
|
2016-03-13 18:07:10 +01:00
|
|
|
use super::*;
|
2017-10-17 10:40:45 +02:00
|
|
|
use {JournalDB, kvdb_memorydb};
|
2016-03-13 18:07:10 +01:00
|
|
|
|
2017-10-13 17:10:03 +02:00
|
|
|
fn new_db() -> RefCountedDB {
|
2017-10-15 16:17:15 +02:00
|
|
|
let backing = Arc::new(kvdb_memorydb::create(0));
|
2017-10-13 17:10:03 +02:00
|
|
|
RefCountedDB::new(backing, None)
|
|
|
|
}
|
|
|
|
|
2016-03-13 18:07:10 +01:00
|
|
|
#[test]
|
|
|
|
fn long_history() {
|
|
|
|
// history is 3
|
2017-10-13 17:10:03 +02:00
|
|
|
let mut jdb = new_db();
|
2019-06-19 13:54:05 +02:00
|
|
|
let h = jdb.insert(EMPTY_PREFIX, b"foo");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(0, &keccak(b"0"), None).unwrap();
|
2019-06-19 13:54:05 +02:00
|
|
|
assert!(jdb.contains(&h, EMPTY_PREFIX));
|
|
|
|
jdb.remove(&h, EMPTY_PREFIX);
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(1, &keccak(b"1"), None).unwrap();
|
2019-06-19 13:54:05 +02:00
|
|
|
assert!(jdb.contains(&h, EMPTY_PREFIX));
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(2, &keccak(b"2"), None).unwrap();
|
2019-06-19 13:54:05 +02:00
|
|
|
assert!(jdb.contains(&h, EMPTY_PREFIX));
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(3, &keccak(b"3"), Some((0, keccak(b"0")))).unwrap();
|
2019-06-19 13:54:05 +02:00
|
|
|
assert!(jdb.contains(&h, EMPTY_PREFIX));
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(4, &keccak(b"4"), Some((1, keccak(b"1")))).unwrap();
|
2019-06-19 13:54:05 +02:00
|
|
|
assert!(!jdb.contains(&h, EMPTY_PREFIX));
|
2016-03-13 18:07:10 +01:00
|
|
|
}
|
|
|
|
|
2016-04-12 00:51:14 +02:00
|
|
|
#[test]
|
|
|
|
fn latest_era_should_work() {
|
|
|
|
// history is 3
|
2017-10-13 17:10:03 +02:00
|
|
|
let mut jdb = new_db();
|
2016-04-12 00:51:14 +02:00
|
|
|
assert_eq!(jdb.latest_era(), None);
|
2019-06-19 13:54:05 +02:00
|
|
|
let h = jdb.insert(EMPTY_PREFIX, b"foo");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(0, &keccak(b"0"), None).unwrap();
|
2016-04-12 00:51:14 +02:00
|
|
|
assert_eq!(jdb.latest_era(), Some(0));
|
2019-06-19 13:54:05 +02:00
|
|
|
jdb.remove(&h, EMPTY_PREFIX);
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(1, &keccak(b"1"), None).unwrap();
|
2016-04-12 00:51:14 +02:00
|
|
|
assert_eq!(jdb.latest_era(), Some(1));
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(2, &keccak(b"2"), None).unwrap();
|
2016-04-12 00:51:14 +02:00
|
|
|
assert_eq!(jdb.latest_era(), Some(2));
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(3, &keccak(b"3"), Some((0, keccak(b"0")))).unwrap();
|
2016-04-12 00:51:14 +02:00
|
|
|
assert_eq!(jdb.latest_era(), Some(3));
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(4, &keccak(b"4"), Some((1, keccak(b"1")))).unwrap();
|
2016-04-12 00:51:14 +02:00
|
|
|
assert_eq!(jdb.latest_era(), Some(4));
|
|
|
|
}
|
|
|
|
|
2016-03-13 18:07:10 +01:00
|
|
|
#[test]
|
|
|
|
fn complex() {
|
|
|
|
// history is 1
|
2017-10-13 17:10:03 +02:00
|
|
|
let mut jdb = new_db();
|
2016-03-13 18:07:10 +01:00
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
let foo = jdb.insert(EMPTY_PREFIX, b"foo");
|
|
|
|
let bar = jdb.insert(EMPTY_PREFIX, b"bar");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(0, &keccak(b"0"), None).unwrap();
|
2019-06-19 13:54:05 +02:00
|
|
|
assert!(jdb.contains(&foo, EMPTY_PREFIX));
|
|
|
|
assert!(jdb.contains(&bar, EMPTY_PREFIX));
|
2016-03-13 18:07:10 +01:00
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
jdb.remove(&foo, EMPTY_PREFIX);
|
|
|
|
jdb.remove(&bar, EMPTY_PREFIX);
|
|
|
|
let baz = jdb.insert(EMPTY_PREFIX, b"baz");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(1, &keccak(b"1"), Some((0, keccak(b"0")))).unwrap();
|
2019-06-19 13:54:05 +02:00
|
|
|
assert!(jdb.contains(&foo, EMPTY_PREFIX));
|
|
|
|
assert!(jdb.contains(&bar, EMPTY_PREFIX));
|
|
|
|
assert!(jdb.contains(&baz, EMPTY_PREFIX));
|
2016-03-13 18:07:10 +01:00
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
let foo = jdb.insert(EMPTY_PREFIX, b"foo");
|
|
|
|
jdb.remove(&baz, EMPTY_PREFIX);
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(2, &keccak(b"2"), Some((1, keccak(b"1")))).unwrap();
|
2019-06-19 13:54:05 +02:00
|
|
|
assert!(jdb.contains(&foo, EMPTY_PREFIX));
|
|
|
|
assert!(!jdb.contains(&bar, EMPTY_PREFIX));
|
|
|
|
assert!(jdb.contains(&baz, EMPTY_PREFIX));
|
2016-03-13 18:07:10 +01:00
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
jdb.remove(&foo, EMPTY_PREFIX);
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(3, &keccak(b"3"), Some((2, keccak(b"2")))).unwrap();
|
2019-06-19 13:54:05 +02:00
|
|
|
assert!(jdb.contains(&foo, EMPTY_PREFIX));
|
|
|
|
assert!(!jdb.contains(&bar, EMPTY_PREFIX));
|
|
|
|
assert!(!jdb.contains(&baz, EMPTY_PREFIX));
|
2016-03-13 18:07:10 +01:00
|
|
|
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(4, &keccak(b"4"), Some((3, keccak(b"3")))).unwrap();
|
2019-06-19 13:54:05 +02:00
|
|
|
assert!(!jdb.contains(&foo, EMPTY_PREFIX));
|
|
|
|
assert!(!jdb.contains(&bar, EMPTY_PREFIX));
|
|
|
|
assert!(!jdb.contains(&baz, EMPTY_PREFIX));
|
2016-03-13 18:07:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fork() {
|
|
|
|
// history is 1
|
2017-10-13 17:10:03 +02:00
|
|
|
let mut jdb = new_db();
|
2016-03-13 18:07:10 +01:00
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
let foo = jdb.insert(EMPTY_PREFIX, b"foo");
|
|
|
|
let bar = jdb.insert(EMPTY_PREFIX, b"bar");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(0, &keccak(b"0"), None).unwrap();
|
2019-06-19 13:54:05 +02:00
|
|
|
assert!(jdb.contains(&foo, EMPTY_PREFIX));
|
|
|
|
assert!(jdb.contains(&bar, EMPTY_PREFIX));
|
2016-03-13 18:07:10 +01:00
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
jdb.remove(&foo, EMPTY_PREFIX);
|
|
|
|
let baz = jdb.insert(EMPTY_PREFIX, b"baz");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(1, &keccak(b"1a"), Some((0, keccak(b"0")))).unwrap();
|
2016-03-13 18:07:10 +01:00
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
jdb.remove(&bar, EMPTY_PREFIX);
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(1, &keccak(b"1b"), Some((0, keccak(b"0")))).unwrap();
|
2016-03-13 18:07:10 +01:00
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
assert!(jdb.contains(&foo, EMPTY_PREFIX));
|
|
|
|
assert!(jdb.contains(&bar, EMPTY_PREFIX));
|
|
|
|
assert!(jdb.contains(&baz, EMPTY_PREFIX));
|
2016-03-13 18:07:10 +01:00
|
|
|
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(2, &keccak(b"2b"), Some((1, keccak(b"1b")))).unwrap();
|
2019-06-19 13:54:05 +02:00
|
|
|
assert!(jdb.contains(&foo, EMPTY_PREFIX));
|
|
|
|
assert!(!jdb.contains(&baz, EMPTY_PREFIX));
|
|
|
|
assert!(!jdb.contains(&bar, EMPTY_PREFIX));
|
2016-03-13 18:07:10 +01:00
|
|
|
}
|
2016-08-03 16:34:32 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inject() {
|
2017-10-13 17:10:03 +02:00
|
|
|
let mut jdb = new_db();
|
2019-06-19 13:54:05 +02:00
|
|
|
let key = jdb.insert(EMPTY_PREFIX, b"dog");
|
2016-08-03 16:34:32 +02:00
|
|
|
jdb.inject_batch().unwrap();
|
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
assert_eq!(jdb.get(&key, EMPTY_PREFIX).unwrap(), DBValue::from_slice(b"dog"));
|
|
|
|
jdb.remove(&key, EMPTY_PREFIX);
|
2016-08-03 16:34:32 +02:00
|
|
|
jdb.inject_batch().unwrap();
|
|
|
|
|
2019-06-19 13:54:05 +02:00
|
|
|
assert!(jdb.get(&key, EMPTY_PREFIX).is_none());
|
2016-08-03 16:34:32 +02:00
|
|
|
}
|
2016-03-13 18:07:10 +01:00
|
|
|
}
|