2018-06-04 10:19:50 +02:00
|
|
|
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
|
2016-02-05 13:40:41 +01:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// 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
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-04-06 10:07:24 +02:00
|
|
|
//! Disk-backed `HashDB` implementation.
|
2016-01-18 12:41:31 +01:00
|
|
|
|
2017-07-29 17:12:07 +02:00
|
|
|
use std::collections::HashMap;
|
2017-08-26 19:09:32 +02:00
|
|
|
use std::collections::hash_map::Entry;
|
2017-07-29 21:56:42 +02:00
|
|
|
use std::sync::Arc;
|
2018-07-02 18:50:05 +02:00
|
|
|
|
|
|
|
use bytes::Bytes;
|
|
|
|
use error::{BaseDataError, UtilError};
|
|
|
|
use ethereum_types::H256;
|
2016-01-18 12:41:31 +01:00
|
|
|
use hashdb::*;
|
2018-07-02 18:50:05 +02:00
|
|
|
use heapsize::HeapSizeOf;
|
|
|
|
use keccak_hasher::KeccakHasher;
|
|
|
|
use kvdb::{KeyValueDB, DBTransaction};
|
2016-02-04 02:40:35 +01:00
|
|
|
use memorydb::*;
|
2018-07-02 18:50:05 +02:00
|
|
|
use parking_lot::RwLock;
|
|
|
|
use rlp::{encode, decode};
|
2016-07-28 23:46:24 +02:00
|
|
|
use super::{DB_PREFIX_LEN, LATEST_ERA_KEY};
|
2016-03-11 13:50:39 +01:00
|
|
|
use super::traits::JournalDB;
|
2018-03-15 11:14:38 +01:00
|
|
|
use util::{DatabaseKey, DatabaseValueView, DatabaseValueRef};
|
2016-01-18 12:41:31 +01:00
|
|
|
|
2018-02-19 14:05:00 +01:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2016-03-12 09:51:17 +01:00
|
|
|
struct RefInfo {
|
|
|
|
queue_refs: usize,
|
|
|
|
in_archive: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HeapSizeOf for RefInfo {
|
|
|
|
fn heap_size_of_children(&self) -> usize { 0 }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq)]
|
|
|
|
enum RemoveFrom {
|
|
|
|
Queue,
|
|
|
|
Archive,
|
|
|
|
}
|
|
|
|
|
2016-04-06 10:07:24 +02:00
|
|
|
/// Implementation of the `HashDB` trait for a disk-backed database with a memory overlay
|
2016-03-06 21:57:55 +01:00
|
|
|
/// and latent-removal semantics.
|
2016-01-18 12:41:31 +01:00
|
|
|
///
|
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-01-18 12:41:31 +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 make a journal of all blocks in the recent history and record
|
|
|
|
/// all keys that were inserted and deleted. The journal is ordered by era; multiple commits can
|
|
|
|
/// share the same era. This forms a data structure similar to a queue but whose items are tuples.
|
|
|
|
/// By the time comes to remove a tuple from the queue (i.e. then the era passes from recent history
|
|
|
|
/// into ancient history) then only one commit from the tuple is considered canonical. This commit
|
|
|
|
/// is kept in the main backing database, whereas any others from the same era are reverted.
|
|
|
|
///
|
|
|
|
/// It is possible that a key, properly available in the backing database be deleted and re-inserted
|
|
|
|
/// in the recent history queue, yet have both operations in commits that are eventually non-canonical.
|
|
|
|
/// To avoid the original, and still required, key from being deleted, we maintain a reference count
|
|
|
|
/// which includes an original key, if any.
|
|
|
|
///
|
|
|
|
/// The semantics of the `counter` are:
|
2018-05-11 11:34:07 +02:00
|
|
|
/// ```text
|
2016-09-26 17:14:44 +02:00
|
|
|
/// insert key k:
|
|
|
|
/// counter already contains k: count += 1
|
|
|
|
/// counter doesn't contain k:
|
|
|
|
/// backing db contains k: count = 1
|
|
|
|
/// backing db doesn't contain k: insert into backing db, count = 0
|
|
|
|
/// delete key k:
|
|
|
|
/// counter contains k (count is asserted to be non-zero):
|
|
|
|
/// count > 1: counter -= 1
|
|
|
|
/// count == 1: remove counter
|
|
|
|
/// count == 0: remove key from backing db
|
|
|
|
/// counter doesn't contain k: remove key from backing db
|
2016-10-27 08:28:12 +02:00
|
|
|
/// ```
|
2016-09-26 17:14:44 +02:00
|
|
|
///
|
|
|
|
/// Practically, this means that for each commit block turning from recent to ancient we do the
|
|
|
|
/// following:
|
2018-05-11 11:34:07 +02:00
|
|
|
/// ```text
|
2016-09-26 17:14:44 +02:00
|
|
|
/// is_canonical:
|
|
|
|
/// inserts: Ignored (left alone in the backing database).
|
|
|
|
/// deletes: Enacted; however, recent history queue is checked for ongoing references. This is
|
|
|
|
/// reduced as a preference to deletion from the backing database.
|
|
|
|
/// !is_canonical:
|
|
|
|
/// inserts: Reverted; however, recent history queue is checked for ongoing references. This is
|
|
|
|
/// reduced as a preference to deletion from the backing database.
|
|
|
|
/// deletes: Ignored (they were never inserted).
|
2016-10-27 08:28:12 +02:00
|
|
|
/// ```
|
2016-09-26 17:14:44 +02:00
|
|
|
///
|
2016-10-27 08:28:12 +02:00
|
|
|
/// TODO: `store_reclaim_period`
|
2016-03-12 11:19:42 +01:00
|
|
|
pub struct EarlyMergeDB {
|
2018-07-02 18:50:05 +02:00
|
|
|
overlay: MemoryDB<KeccakHasher>,
|
2017-02-20 17:21:55 +01:00
|
|
|
backing: Arc<KeyValueDB>,
|
2016-03-12 09:51:17 +01:00
|
|
|
refs: Option<Arc<RwLock<HashMap<H256, RefInfo>>>>,
|
2016-03-12 19:19:45 +01:00
|
|
|
latest_era: Option<u64>,
|
2016-07-28 23:46:24 +02:00
|
|
|
column: Option<u32>,
|
2016-01-18 12:41:31 +01:00
|
|
|
}
|
|
|
|
|
2016-03-12 11:19:42 +01:00
|
|
|
impl EarlyMergeDB {
|
2016-03-06 22:39:04 +01:00
|
|
|
/// Create a new instance from file
|
2017-02-20 17:21:55 +01:00
|
|
|
pub fn new(backing: Arc<KeyValueDB>, col: Option<u32>) -> EarlyMergeDB {
|
|
|
|
let (latest_era, refs) = EarlyMergeDB::read_refs(&*backing, col);
|
2016-03-12 17:30:46 +01:00
|
|
|
let refs = Some(Arc::new(RwLock::new(refs)));
|
2016-03-12 11:19:42 +01:00
|
|
|
EarlyMergeDB {
|
2016-02-04 02:40:35 +01:00
|
|
|
overlay: MemoryDB::new(),
|
2016-07-28 23:46:24 +02:00
|
|
|
backing: backing,
|
2016-03-12 09:51:17 +01:00
|
|
|
refs: refs,
|
2016-03-12 17:30:46 +01:00
|
|
|
latest_era: latest_era,
|
2016-07-28 23:46:24 +02:00
|
|
|
column: col,
|
2016-01-21 23:33:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-06 21:57:55 +01:00
|
|
|
fn morph_key(key: &H256, index: u8) -> Bytes {
|
2016-09-01 12:23:31 +02:00
|
|
|
let mut ret = (&**key).to_owned();
|
2016-03-06 21:57:55 +01:00
|
|
|
ret.push(index);
|
|
|
|
ret
|
2016-03-04 20:19:36 +01:00
|
|
|
}
|
|
|
|
|
2016-03-06 21:57:55 +01:00
|
|
|
// The next three are valid only as long as there is an insert operation of `key` in the journal.
|
2016-08-25 16:43:56 +02:00
|
|
|
fn set_already_in(batch: &mut DBTransaction, col: Option<u32>, key: &H256) { batch.put(col, &Self::morph_key(key, 0), &[1u8]); }
|
|
|
|
fn reset_already_in(batch: &mut DBTransaction, col: Option<u32>, key: &H256) { batch.delete(col, &Self::morph_key(key, 0)); }
|
2017-02-20 17:21:55 +01:00
|
|
|
fn is_already_in(backing: &KeyValueDB, col: Option<u32>, key: &H256) -> bool {
|
2016-07-28 23:46:24 +02:00
|
|
|
backing.get(col, &Self::morph_key(key, 0)).expect("Low-level database error. Some issue with your hard disk?").is_some()
|
2016-03-06 21:57:55 +01:00
|
|
|
}
|
|
|
|
|
2017-09-05 12:54:00 +02:00
|
|
|
fn insert_keys(inserts: &[(H256, DBValue)], backing: &KeyValueDB, col: Option<u32>, refs: &mut HashMap<H256, RefInfo>, batch: &mut DBTransaction) {
|
2016-03-06 21:57:55 +01:00
|
|
|
for &(ref h, ref d) in inserts {
|
2017-08-27 18:17:55 +02:00
|
|
|
match refs.entry(*h) {
|
|
|
|
Entry::Occupied(mut entry) => {
|
|
|
|
let info = entry.get_mut();
|
|
|
|
// already counting. increment.
|
|
|
|
info.queue_refs += 1;
|
2017-09-05 12:54:00 +02:00
|
|
|
trace!(target: "jdb.fine", " insert({}): In queue: Incrementing refs to {}", h, info.queue_refs);
|
2017-08-27 18:17:55 +02:00
|
|
|
},
|
|
|
|
Entry::Vacant(entry) => {
|
|
|
|
// this is the first entry for this node in the journal.
|
|
|
|
let in_archive = backing.get(col, h).expect("Low-level database error. Some issue with your hard disk?").is_some();
|
|
|
|
if in_archive {
|
|
|
|
// already in the backing DB. start counting, and remember it was already in.
|
|
|
|
Self::set_already_in(batch, col, h);
|
2017-09-05 12:54:00 +02:00
|
|
|
trace!(target: "jdb.fine", " insert({}): New to queue, in DB: Recording and inserting into queue", h);
|
2017-08-27 18:17:55 +02:00
|
|
|
} else {
|
|
|
|
// Gets removed when a key leaves the journal, so should never be set when we're placing a new key.
|
|
|
|
//Self::reset_already_in(&h);
|
|
|
|
assert!(!Self::is_already_in(backing, col, h));
|
2017-09-05 12:54:00 +02:00
|
|
|
trace!(target: "jdb.fine", " insert({}): New to queue, not in DB: Inserting into queue and DB", h);
|
2017-08-27 18:17:55 +02:00
|
|
|
batch.put(col, h, d);
|
|
|
|
}
|
|
|
|
entry.insert(RefInfo {
|
|
|
|
queue_refs: 1,
|
|
|
|
in_archive: in_archive,
|
|
|
|
});
|
|
|
|
},
|
2016-03-12 09:51:17 +01:00
|
|
|
}
|
2016-03-04 20:19:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-20 17:21:55 +01:00
|
|
|
fn replay_keys(inserts: &[H256], backing: &KeyValueDB, col: Option<u32>, refs: &mut HashMap<H256, RefInfo>) {
|
2016-03-12 09:51:17 +01:00
|
|
|
trace!(target: "jdb.fine", "replay_keys: inserts={:?}, refs={:?}", inserts, refs);
|
2016-03-06 21:57:55 +01:00
|
|
|
for h in inserts {
|
2017-08-27 18:17:55 +02:00
|
|
|
match refs.entry(*h) {
|
2016-03-06 21:57:55 +01:00
|
|
|
// already counting. increment.
|
2017-08-27 18:17:55 +02:00
|
|
|
Entry::Occupied(mut entry) => {
|
|
|
|
entry.get_mut().queue_refs += 1;
|
|
|
|
},
|
|
|
|
// this is the first entry for this node in the journal.
|
|
|
|
// it is initialised to 1 if it was already in.
|
|
|
|
Entry::Vacant(entry) => {
|
|
|
|
entry.insert(RefInfo {
|
|
|
|
queue_refs: 1,
|
|
|
|
in_archive: Self::is_already_in(backing, col, h),
|
|
|
|
});
|
|
|
|
},
|
2016-03-06 21:57:55 +01:00
|
|
|
}
|
|
|
|
}
|
2016-03-12 09:51:17 +01:00
|
|
|
trace!(target: "jdb.fine", "replay_keys: (end) refs={:?}", refs);
|
2016-03-06 21:57:55 +01:00
|
|
|
}
|
|
|
|
|
2017-09-05 12:54:00 +02:00
|
|
|
fn remove_keys(deletes: &[H256], refs: &mut HashMap<H256, RefInfo>, batch: &mut DBTransaction, col: Option<u32>, from: RemoveFrom) {
|
2016-06-23 11:16:11 +02:00
|
|
|
// with a remove on {queue_refs: 1, in_archive: true}, we have two options:
|
2016-03-12 09:51:17 +01:00
|
|
|
// - convert to {queue_refs: 1, in_archive: false} (i.e. remove it from the conceptual archive)
|
|
|
|
// - convert to {queue_refs: 0, in_archive: true} (i.e. remove it from the conceptual queue)
|
|
|
|
// (the latter option would then mean removing the RefInfo, since it would no longer be counted in the queue.)
|
|
|
|
// both are valid, but we switch between them depending on context.
|
|
|
|
// All inserts in queue (i.e. those which may yet be reverted) have an entry in refs.
|
2017-08-27 18:17:55 +02:00
|
|
|
for h in deletes {
|
|
|
|
match refs.entry(*h) {
|
|
|
|
Entry::Occupied(mut entry) => {
|
|
|
|
if entry.get().in_archive && from == RemoveFrom::Archive {
|
|
|
|
entry.get_mut().in_archive = false;
|
|
|
|
Self::reset_already_in(batch, col, h);
|
2017-09-05 12:54:00 +02:00
|
|
|
trace!(target: "jdb.fine", " remove({}): In archive, 1 in queue: Reducing to queue only and recording", h);
|
2017-08-27 18:17:55 +02:00
|
|
|
continue;
|
2016-03-12 09:51:17 +01:00
|
|
|
}
|
2017-08-27 18:17:55 +02:00
|
|
|
if entry.get().queue_refs > 1 {
|
|
|
|
entry.get_mut().queue_refs -= 1;
|
2017-09-05 12:54:00 +02:00
|
|
|
trace!(target: "jdb.fine", " remove({}): In queue > 1 refs: Decrementing ref count to {}", h, entry.get().queue_refs);
|
2017-08-27 18:17:55 +02:00
|
|
|
continue;
|
2016-03-12 09:51:17 +01:00
|
|
|
}
|
2017-08-27 18:17:55 +02:00
|
|
|
|
|
|
|
let queue_refs = entry.get().queue_refs;
|
|
|
|
let in_archive = entry.get().in_archive;
|
|
|
|
|
|
|
|
match (queue_refs, in_archive) {
|
|
|
|
(1, true) => {
|
|
|
|
entry.remove();
|
|
|
|
Self::reset_already_in(batch, col, h);
|
2017-09-05 12:54:00 +02:00
|
|
|
trace!(target: "jdb.fine", " remove({}): In archive, 1 in queue: Removing from queue and leaving in archive", h);
|
2017-08-27 18:17:55 +02:00
|
|
|
},
|
|
|
|
(1, false) => {
|
|
|
|
entry.remove();
|
|
|
|
batch.delete(col, h);
|
2017-09-05 12:54:00 +02:00
|
|
|
trace!(target: "jdb.fine", " remove({}): Not in archive, only 1 ref in queue: Removing from queue and DB", h);
|
2017-08-27 18:17:55 +02:00
|
|
|
},
|
|
|
|
_ => panic!("Invalid value in refs: {:?}", entry.get()),
|
2016-03-12 09:51:17 +01:00
|
|
|
}
|
2017-08-27 18:17:55 +02:00
|
|
|
},
|
|
|
|
Entry::Vacant(_entry) => {
|
2016-03-06 21:57:55 +01:00
|
|
|
// Gets removed when moving from 1 to 0 additional refs. Should never be here at 0 additional refs.
|
|
|
|
//assert!(!Self::is_already_in(db, &h));
|
2016-08-18 09:43:56 +02:00
|
|
|
batch.delete(col, h);
|
2017-09-05 12:54:00 +02:00
|
|
|
trace!(target: "jdb.fine", " remove({}): Not in queue - MUST BE IN ARCHIVE: Removing from DB", h);
|
2017-08-27 18:17:55 +02:00
|
|
|
},
|
2016-03-06 21:57:55 +01:00
|
|
|
}
|
|
|
|
}
|
2016-03-04 20:19:36 +01:00
|
|
|
}
|
|
|
|
|
2016-03-12 09:51:17 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
fn can_reconstruct_refs(&self) -> bool {
|
2017-02-20 17:21:55 +01:00
|
|
|
let (latest_era, reconstructed) = Self::read_refs(&*self.backing, self.column);
|
2016-07-13 19:59:59 +02:00
|
|
|
let refs = self.refs.as_ref().unwrap().write();
|
2016-03-12 17:30:46 +01:00
|
|
|
if *refs != reconstructed || latest_era != self.latest_era {
|
2016-03-12 09:51:17 +01:00
|
|
|
let clean_refs = refs.iter().filter_map(|(k, v)| if reconstructed.get(k) == Some(v) {None} else {Some((k.clone(), v.clone()))}).collect::<HashMap<_, _>>();
|
|
|
|
let clean_recon = reconstructed.into_iter().filter_map(|(k, v)| if refs.get(&k) == Some(&v) {None} else {Some((k.clone(), v.clone()))}).collect::<HashMap<_, _>>();
|
|
|
|
warn!(target: "jdb", "mem: {:?} != log: {:?}", clean_refs, clean_recon);
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-26 13:53:47 +02:00
|
|
|
fn payload(&self, key: &H256) -> Option<DBValue> {
|
|
|
|
self.backing.get(self.column, key).expect("Low-level database error. Some issue with your hard disk?")
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
|
|
|
|
2017-02-20 17:21:55 +01:00
|
|
|
fn read_refs(db: &KeyValueDB, col: Option<u32>) -> (Option<u64>, HashMap<H256, RefInfo>) {
|
2016-03-12 09:51:17 +01:00
|
|
|
let mut refs = HashMap::new();
|
2016-03-12 19:19:45 +01:00
|
|
|
let mut latest_era = None;
|
2016-07-28 23:46:24 +02:00
|
|
|
if let Some(val) = db.get(col, &LATEST_ERA_KEY).expect("Low-level database error.") {
|
2018-05-08 11:22:12 +02:00
|
|
|
let mut era = decode::<u64>(&val).expect("decoding db value failed");
|
2016-03-12 19:19:45 +01:00
|
|
|
latest_era = Some(era);
|
2016-03-11 13:50:39 +01:00
|
|
|
loop {
|
2018-03-15 11:14:38 +01:00
|
|
|
let mut db_key = DatabaseKey {
|
|
|
|
era,
|
|
|
|
index: 0usize,
|
|
|
|
};
|
|
|
|
while let Some(rlp_data) = db.get(col, &encode(&db_key)).expect("Low-level database error.") {
|
2018-03-20 03:02:07 +01:00
|
|
|
let inserts = DatabaseValueView::from_rlp(&rlp_data).inserts().expect("rlp read from db; qed");
|
2016-07-28 23:46:24 +02:00
|
|
|
Self::replay_keys(&inserts, db, col, &mut refs);
|
2018-03-15 11:14:38 +01:00
|
|
|
db_key.index += 1;
|
2016-03-11 13:50:39 +01:00
|
|
|
};
|
2018-03-15 11:14:38 +01:00
|
|
|
if db_key.index == 0 || era == 0 {
|
2016-03-11 13:50:39 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
era -= 1;
|
|
|
|
}
|
|
|
|
}
|
2016-03-12 17:30:46 +01:00
|
|
|
(latest_era, refs)
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
2016-07-28 23:46:24 +02:00
|
|
|
}
|
2016-03-11 13:50:39 +01:00
|
|
|
|
2018-07-02 18:50:05 +02:00
|
|
|
impl HashDB<KeccakHasher> for EarlyMergeDB {
|
2016-03-11 13:50:39 +01:00
|
|
|
fn keys(&self) -> HashMap<H256, i32> {
|
2017-08-26 19:09:32 +02:00
|
|
|
let mut ret: HashMap<H256, i32> = self.backing.iter(self.column)
|
|
|
|
.map(|(key, _)| (H256::from_slice(&*key), 1))
|
|
|
|
.collect();
|
2016-03-11 13:50:39 +01:00
|
|
|
|
2016-10-27 08:28:12 +02:00
|
|
|
for (key, refs) in self.overlay.keys() {
|
2017-08-26 19:09:32 +02:00
|
|
|
match ret.entry(key) {
|
|
|
|
Entry::Occupied(mut entry) => {
|
|
|
|
*entry.get_mut() += refs;
|
|
|
|
},
|
|
|
|
Entry::Vacant(entry) => {
|
2017-08-26 19:16:08 +02:00
|
|
|
entry.insert(refs);
|
2017-08-26 19:09:32 +02:00
|
|
|
}
|
|
|
|
}
|
2016-03-11 13:50:39 +01:00
|
|
|
}
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return Some(d)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
fn insert(&mut self, value: &[u8]) -> H256 {
|
|
|
|
self.overlay.insert(value)
|
|
|
|
}
|
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);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-12 11:19:42 +01:00
|
|
|
impl JournalDB for EarlyMergeDB {
|
2016-03-28 09:42:50 +02:00
|
|
|
fn boxed_clone(&self) -> Box<JournalDB> {
|
2016-03-12 11:19:42 +01:00
|
|
|
Box::new(EarlyMergeDB {
|
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-12 09:51:17 +01:00
|
|
|
refs: self.refs.clone(),
|
2016-03-12 19:19:45 +01:00
|
|
|
latest_era: self.latest_era.clone(),
|
2016-07-28 23:46:24 +02:00
|
|
|
column: self.column.clone(),
|
2016-03-11 13:50:39 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-03-12 09:51:17 +01:00
|
|
|
fn is_empty(&self) -> bool {
|
2016-07-28 23:46:24 +02:00
|
|
|
self.backing.get(self.column, &LATEST_ERA_KEY).expect("Low level database error").is_none()
|
|
|
|
}
|
|
|
|
|
2017-02-20 17:21:55 +01:00
|
|
|
fn backing(&self) -> &Arc<KeyValueDB> {
|
2016-07-28 23:46:24 +02:00
|
|
|
&self.backing
|
2016-03-12 09:51:17 +01:00
|
|
|
}
|
|
|
|
|
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-03-11 13:50:39 +01:00
|
|
|
fn mem_used(&self) -> usize {
|
2016-03-12 09:51:17 +01:00
|
|
|
self.overlay.mem_used() + match self.refs {
|
2016-07-13 19:59:59 +02:00
|
|
|
Some(ref c) => c.read().heap_size_of_children(),
|
2016-03-11 13:50:39 +01:00
|
|
|
None => 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-09-26 17:14:44 +02:00
|
|
|
fn journal_under(&mut self, batch: &mut DBTransaction, now: u64, id: &H256) -> Result<u32, UtilError> {
|
2016-01-18 12:41:31 +01:00
|
|
|
// record new commit's details.
|
2016-10-25 22:34:52 +02:00
|
|
|
let mut refs = match self.refs.as_ref() {
|
|
|
|
Some(refs) => refs.write(),
|
|
|
|
None => return Ok(0),
|
|
|
|
};
|
2016-09-26 17:14:44 +02:00
|
|
|
|
2016-01-18 12:41:31 +01:00
|
|
|
{
|
2018-03-15 11:14:38 +01:00
|
|
|
let mut db_key = DatabaseKey {
|
|
|
|
era: now,
|
|
|
|
index: 0usize,
|
|
|
|
};
|
2016-01-18 12:41:31 +01:00
|
|
|
let mut last;
|
|
|
|
|
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-03-06 21:57:55 +01: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-01-18 12:41:31 +01:00
|
|
|
}
|
|
|
|
|
2016-03-06 21:57:55 +01:00
|
|
|
let drained = self.overlay.drain();
|
2016-03-12 09:51:17 +01:00
|
|
|
|
2017-09-05 12:54:00 +02:00
|
|
|
trace!(target: "jdb", "commit: #{} ({})", now, id);
|
2016-03-12 09:51:17 +01:00
|
|
|
|
2016-03-06 21:57:55 +01:00
|
|
|
let removes: Vec<H256> = drained
|
|
|
|
.iter()
|
2016-03-07 14:33:00 +01:00
|
|
|
.filter_map(|(k, &(_, c))| if c < 0 {Some(k.clone())} else {None})
|
2016-03-06 21:57:55 +01:00
|
|
|
.collect();
|
2016-10-26 13:53:47 +02:00
|
|
|
let inserts: Vec<(H256, _)> = drained
|
2016-03-06 21:57:55 +01:00
|
|
|
.into_iter()
|
|
|
|
.filter_map(|(k, (v, r))| if r > 0 { assert!(r == 1); Some((k, v)) } else { assert!(r >= -1); None })
|
|
|
|
.collect();
|
|
|
|
|
2016-03-12 09:51:17 +01:00
|
|
|
// TODO: check all removes are in the db.
|
|
|
|
|
2016-03-06 21:57:55 +01:00
|
|
|
// Process the new inserts.
|
|
|
|
// We use the inserts for three things. For each:
|
|
|
|
// - we place into the backing DB or increment the counter if already in;
|
|
|
|
// - we note in the backing db that it was already in;
|
|
|
|
// - we write the key into our journal for this block;
|
|
|
|
|
2017-09-05 12:54:00 +02:00
|
|
|
Self::insert_keys(&inserts, &*self.backing, self.column, &mut refs, batch);
|
2016-09-26 17:14:44 +02:00
|
|
|
|
|
|
|
let ins = inserts.iter().map(|&(k, _)| k).collect::<Vec<_>>();
|
2018-03-15 11:14:38 +01:00
|
|
|
let value_ref = DatabaseValueRef {
|
|
|
|
id,
|
|
|
|
inserts: &ins,
|
|
|
|
deletes: &removes,
|
|
|
|
};
|
2016-09-26 17:14:44 +02:00
|
|
|
|
2017-09-05 12:54:00 +02:00
|
|
|
trace!(target: "jdb.ops", " Deletes: {:?}", removes);
|
|
|
|
trace!(target: "jdb.ops", " Inserts: {:?}", ins);
|
2016-09-26 17:14:44 +02:00
|
|
|
|
2018-03-15 11:14:38 +01:00
|
|
|
batch.put(self.column, &last, &encode(&value_ref));
|
2016-03-12 19:19:45 +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-12 19:19:45 +01:00
|
|
|
self.latest_era = Some(now);
|
2016-03-12 17:30:46 +01:00
|
|
|
}
|
2016-09-26 17:14:44 +02:00
|
|
|
|
|
|
|
Ok((ins.len() + removes.len()) as u32)
|
2016-01-18 12:41:31 +01:00
|
|
|
}
|
2016-09-26 17:14:44 +02:00
|
|
|
}
|
2016-01-18 12:41:31 +01:00
|
|
|
|
2016-09-26 17:14:44 +02:00
|
|
|
fn mark_canonical(&mut self, batch: &mut DBTransaction, end_era: u64, canon_id: &H256) -> Result<u32, UtilError> {
|
|
|
|
let mut refs = self.refs.as_ref().unwrap().write();
|
2016-03-12 09:51:17 +01:00
|
|
|
|
2016-09-26 17:14:44 +02: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;
|
|
|
|
|
2018-03-15 11:14:38 +01:00
|
|
|
while let Some(rlp_data) = {
|
|
|
|
last = encode(&db_key);
|
|
|
|
self.backing.get(self.column, &last)
|
|
|
|
}? {
|
2018-03-20 03:02:07 +01:00
|
|
|
let view = DatabaseValueView::from_rlp(&rlp_data);
|
2018-03-15 11:14:38 +01:00
|
|
|
let inserts = view.inserts().expect("rlp read from db; qed");
|
|
|
|
|
|
|
|
if canon_id == &view.id().expect("rlp read from db; qed") {
|
2016-09-26 17:14:44 +02:00
|
|
|
// Collect keys to be removed. Canon block - remove the (enacted) deletes.
|
2018-03-15 11:14:38 +01:00
|
|
|
let deletes = view.deletes().expect("rlp read from db; qed");
|
2016-09-26 17:14:44 +02:00
|
|
|
trace!(target: "jdb.ops", " Expunging: {:?}", deletes);
|
2017-09-05 12:54:00 +02:00
|
|
|
Self::remove_keys(&deletes, &mut refs, batch, self.column, RemoveFrom::Archive);
|
2016-09-26 17:14:44 +02:00
|
|
|
|
2017-09-05 12:54:00 +02:00
|
|
|
trace!(target: "jdb.ops", " Finalising: {:?}", inserts);
|
2016-09-26 17:14:44 +02:00
|
|
|
for k in &inserts {
|
|
|
|
match refs.get(k).cloned() {
|
|
|
|
None => {
|
|
|
|
// [in archive] -> SHIFT remove -> SHIFT insert None->Some{queue_refs: 1, in_archive: true} -> TAKE remove Some{queue_refs: 1, in_archive: true}->None -> TAKE insert
|
|
|
|
// already expunged from the queue (which is allowed since the key is in the archive).
|
|
|
|
// leave well alone.
|
|
|
|
}
|
|
|
|
Some( RefInfo{queue_refs: 1, in_archive: false} ) => {
|
|
|
|
// just delete the refs entry.
|
|
|
|
refs.remove(k);
|
|
|
|
}
|
|
|
|
Some( RefInfo{queue_refs: x, in_archive: false} ) => {
|
|
|
|
// must set already in; ,
|
|
|
|
Self::set_already_in(batch, self.column, k);
|
|
|
|
refs.insert(k.clone(), RefInfo{ queue_refs: x - 1, in_archive: true });
|
|
|
|
}
|
|
|
|
Some( RefInfo{in_archive: true, ..} ) => {
|
|
|
|
// Invalid! Reinserted the same key twice.
|
|
|
|
warn!("Key {} inserted twice into same fork.", k);
|
2016-03-12 09:51:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-26 17:14:44 +02:00
|
|
|
} else {
|
|
|
|
// Collect keys to be removed. Non-canon block - remove the (reverted) inserts.
|
|
|
|
trace!(target: "jdb.ops", " Reverting: {:?}", inserts);
|
2017-09-05 12:54:00 +02:00
|
|
|
Self::remove_keys(&inserts, &mut refs, batch, self.column, RemoveFrom::Queue);
|
2016-03-12 09:51:17 +01:00
|
|
|
}
|
2016-01-18 12:41:31 +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-12 09:51:17 +01:00
|
|
|
}
|
|
|
|
|
2018-03-15 11:14:38 +01:00
|
|
|
trace!(target: "jdb", "EarlyMergeDB: delete journal for time #{}.{}, (canon was {})", end_era, db_key.index, canon_id);
|
2017-09-05 12:54:00 +02:00
|
|
|
trace!(target: "jdb", "OK: {:?}", &*refs);
|
2016-09-26 17:14:44 +02:00
|
|
|
|
2016-03-06 21:57:55 +01:00
|
|
|
Ok(0)
|
2016-02-05 22:54:33 +01:00
|
|
|
}
|
2016-08-03 16:34:32 +02:00
|
|
|
|
2016-08-25 16:43:56 +02:00
|
|
|
fn inject(&mut self, batch: &mut DBTransaction) -> Result<u32, UtilError> {
|
2016-08-03 16:34:32 +02:00
|
|
|
let mut ops = 0;
|
|
|
|
for (key, (value, rc)) in self.overlay.drain() {
|
|
|
|
if rc != 0 { ops += 1 }
|
|
|
|
|
|
|
|
match rc {
|
|
|
|
0 => {}
|
|
|
|
1 => {
|
2016-12-27 12:53:56 +01:00
|
|
|
if self.backing.get(self.column, &key)?.is_some() {
|
2016-08-03 16:34:32 +02:00
|
|
|
return Err(BaseDataError::AlreadyExists(key).into());
|
|
|
|
}
|
2016-08-18 09:43:56 +02:00
|
|
|
batch.put(self.column, &key, &value)
|
2016-08-03 16:34:32 +02:00
|
|
|
}
|
|
|
|
-1 => {
|
2016-12-27 12:53:56 +01:00
|
|
|
if self.backing.get(self.column, &key)?.is_none() {
|
2016-08-03 16:34:32 +02:00
|
|
|
return Err(BaseDataError::NegativelyReferencedHash(key).into());
|
|
|
|
}
|
2016-08-18 09:43:56 +02:00
|
|
|
batch.delete(self.column, &key)
|
2016-08-03 16:34:32 +02:00
|
|
|
}
|
|
|
|
_ => panic!("Attempted to inject invalid state."),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(ops)
|
|
|
|
}
|
2016-08-25 14:28:45 +02:00
|
|
|
|
2018-07-02 18:50:05 +02:00
|
|
|
fn consolidate(&mut self, with: MemoryDB<KeccakHasher>) {
|
2016-08-25 14:28:45 +02:00
|
|
|
self.overlay.consolidate(with);
|
|
|
|
}
|
2016-03-11 12:54:48 +01:00
|
|
|
}
|
|
|
|
|
2016-01-18 13:30:01 +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;
|
2016-11-28 17:05:37 +01:00
|
|
|
use hashdb::{HashDB, DBValue};
|
2016-01-18 13:30:01 +01:00
|
|
|
use super::*;
|
2016-03-12 09:57:57 +01:00
|
|
|
use super::super::traits::JournalDB;
|
2017-03-22 06:23:40 +01:00
|
|
|
use ethcore_logger::init_log;
|
2017-10-15 16:17:15 +02:00
|
|
|
use kvdb_memorydb;
|
2016-01-18 13:30:01 +01:00
|
|
|
|
2016-03-06 21:57:55 +01:00
|
|
|
#[test]
|
|
|
|
fn insert_same_in_fork() {
|
|
|
|
// history is 1
|
2017-10-13 17:10:03 +02:00
|
|
|
let mut jdb = new_db();
|
2016-03-06 21:57:55 +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();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(2, &keccak(b"2"), None).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(3, &keccak(b"1002a"), Some((1, keccak(b"1")))).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(4, &keccak(b"1003a"), Some((2, keccak(b"2")))).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-03-06 21:57:55 +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-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-03-06 21:57:55 +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();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-03-06 21:57:55 +01:00
|
|
|
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(5, &keccak(b"1004a"), Some((3, keccak(b"1002a")))).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(6, &keccak(b"1005a"), Some((4, keccak(b"1003a")))).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-03-06 21:57:55 +01:00
|
|
|
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&x));
|
2016-03-06 21:57:55 +01:00
|
|
|
}
|
|
|
|
|
2016-03-13 11:50:09 +01:00
|
|
|
#[test]
|
|
|
|
fn insert_older_era() {
|
2017-10-13 17:10:03 +02:00
|
|
|
let mut jdb = new_db();
|
2016-03-13 11:50:09 +01:00
|
|
|
let foo = jdb.insert(b"foo");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(0, &keccak(b"0a"), None).unwrap();
|
2016-03-13 11:50:09 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
|
|
|
|
let bar = jdb.insert(b"bar");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(1, &keccak(b"1"), Some((0, keccak(b"0a")))).unwrap();
|
2016-03-13 11:50:09 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
|
|
|
|
jdb.remove(&bar);
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(0, &keccak(b"0b"), None).unwrap();
|
2016-03-13 11:50:09 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(2, &keccak(b"2"), Some((1, keccak(b"1")))).unwrap();
|
2016-03-13 11:50:09 +01:00
|
|
|
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
|
|
|
assert!(jdb.contains(&bar));
|
2016-03-13 11:50:09 +01:00
|
|
|
}
|
|
|
|
|
2016-01-18 13:30:01 +01:00
|
|
|
#[test]
|
|
|
|
fn long_history() {
|
|
|
|
// history is 3
|
2017-10-13 17:10:03 +02:00
|
|
|
let mut jdb = new_db();
|
2016-01-18 13:30:01 +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-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&h));
|
2016-01-18 13:30:01 +01:00
|
|
|
jdb.remove(&h);
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(1, &keccak(b"1"), None).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
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-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
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-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
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();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(!jdb.contains(&h));
|
2016-01-18 13:30:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn complex() {
|
|
|
|
// history is 1
|
2017-10-13 17:10:03 +02:00
|
|
|
let mut jdb = new_db();
|
2016-01-18 13:30:01 +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-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
|
|
|
assert!(jdb.contains(&bar));
|
2016-01-18 13:30:01 +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-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
|
|
|
assert!(jdb.contains(&bar));
|
|
|
|
assert!(jdb.contains(&baz));
|
2016-01-18 13:30:01 +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-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
|
|
|
assert!(!jdb.contains(&bar));
|
|
|
|
assert!(jdb.contains(&baz));
|
2016-01-18 13:30:01 +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-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
|
|
|
assert!(!jdb.contains(&bar));
|
|
|
|
assert!(!jdb.contains(&baz));
|
2016-01-18 13:30:01 +01:00
|
|
|
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(4, &keccak(b"4"), Some((3, keccak(b"3")))).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(!jdb.contains(&foo));
|
|
|
|
assert!(!jdb.contains(&bar));
|
|
|
|
assert!(!jdb.contains(&baz));
|
2016-01-18 13:30:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fork() {
|
|
|
|
// history is 1
|
2017-10-13 17:10:03 +02:00
|
|
|
let mut jdb = new_db();
|
2016-01-18 13:30:01 +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-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
|
|
|
assert!(jdb.contains(&bar));
|
2016-01-18 13:30:01 +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();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-01-18 13:30:01 +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-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-01-18 13:30:01 +01:00
|
|
|
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
|
|
|
assert!(jdb.contains(&bar));
|
|
|
|
assert!(jdb.contains(&baz));
|
2016-01-18 13:30:01 +01:00
|
|
|
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(2, &keccak(b"2b"), Some((1, keccak(b"1b")))).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
|
|
|
assert!(!jdb.contains(&baz));
|
|
|
|
assert!(!jdb.contains(&bar));
|
2016-01-18 13:30:01 +01:00
|
|
|
}
|
2016-02-04 21:33:30 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overwrite() {
|
|
|
|
// history is 1
|
2017-10-13 17:10:03 +02:00
|
|
|
let mut jdb = new_db();
|
2016-02-04 21:33:30 +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-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
2016-02-04 21:33:30 +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-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-02-04 21:33:30 +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-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
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-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
2016-02-04 21:33:30 +01:00
|
|
|
}
|
2016-02-05 22:54:33 +01:00
|
|
|
|
|
|
|
#[test]
|
2016-03-12 09:51:17 +01:00
|
|
|
fn fork_same_key_one() {
|
|
|
|
|
2017-10-13 17:10:03 +02:00
|
|
|
let mut jdb = new_db();
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(0, &keccak(b"0"), None).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-02-05 22:54:33 +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();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-02-05 22:54:33 +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-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
|
|
|
|
jdb.insert(b"foo");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(1, &keccak(b"1c"), Some((0, keccak(b"0")))).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
2016-02-05 22:54:33 +01:00
|
|
|
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(2, &keccak(b"2a"), Some((1, keccak(b"1a")))).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
2016-03-12 09:51:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fork_same_key_other() {
|
2017-10-13 17:10:03 +02:00
|
|
|
let mut jdb = new_db();
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(0, &keccak(b"0"), None).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
|
|
|
|
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();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
|
|
|
|
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-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
|
|
|
|
jdb.insert(b"foo");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(1, &keccak(b"1c"), Some((0, keccak(b"0")))).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
2016-03-12 09:51:17 +01:00
|
|
|
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(2, &keccak(b"2b"), Some((1, keccak(b"1b")))).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
2016-02-05 22:54:33 +01:00
|
|
|
}
|
2016-03-06 22:39:04 +01:00
|
|
|
|
2016-03-12 09:51:17 +01:00
|
|
|
#[test]
|
|
|
|
fn fork_ins_del_ins() {
|
2017-10-13 17:10:03 +02:00
|
|
|
let mut jdb = new_db();
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(0, &keccak(b"0"), None).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
|
|
|
|
let foo = jdb.insert(b"foo");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(1, &keccak(b"1"), None).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
|
|
|
|
jdb.remove(&foo);
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(2, &keccak(b"2a"), Some((0, keccak(b"0")))).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
|
|
|
|
jdb.remove(&foo);
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(2, &keccak(b"2b"), Some((0, keccak(b"0")))).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
|
|
|
|
jdb.insert(b"foo");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(3, &keccak(b"3a"), Some((1, keccak(b"1")))).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
|
|
|
|
jdb.insert(b"foo");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(3, &keccak(b"3b"), Some((1, keccak(b"1")))).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(4, &keccak(b"4a"), Some((2, keccak(b"2a")))).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(5, &keccak(b"5a"), Some((3, keccak(b"3a")))).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
}
|
2016-03-06 22:39:04 +01:00
|
|
|
|
2017-10-13 17:10:03 +02:00
|
|
|
fn new_db() -> EarlyMergeDB {
|
2017-10-15 16:17:15 +02:00
|
|
|
let backing = Arc::new(kvdb_memorydb::create(0));
|
2017-10-13 17:10:03 +02:00
|
|
|
EarlyMergeDB::new(backing, None)
|
2016-07-28 23:46:24 +02:00
|
|
|
}
|
|
|
|
|
2016-03-06 22:39:04 +01:00
|
|
|
#[test]
|
|
|
|
fn reopen() {
|
2017-10-15 16:17:15 +02:00
|
|
|
let shared_db = Arc::new(kvdb_memorydb::create(0));
|
2016-03-06 22:39:04 +01:00
|
|
|
let bar = H256::random();
|
|
|
|
|
|
|
|
let foo = {
|
2017-10-13 17:10:03 +02:00
|
|
|
let mut jdb = EarlyMergeDB::new(shared_db.clone(), None);
|
2016-03-06 22:39:04 +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-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-03-06 22:39:04 +01:00
|
|
|
foo
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
2017-10-13 17:10:03 +02:00
|
|
|
let mut jdb = EarlyMergeDB::new(shared_db.clone(), None);
|
2016-03-06 22:39:04 +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-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-03-06 22:39:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-10-13 17:10:03 +02:00
|
|
|
let mut jdb = EarlyMergeDB::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-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(!jdb.contains(&foo));
|
2016-03-06 22:39:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-03-12 09:51:17 +01:00
|
|
|
fn insert_delete_insert_delete_insert_expunge() {
|
|
|
|
init_log();
|
2016-03-06 22:39:04 +01:00
|
|
|
|
2017-10-13 17:10:03 +02:00
|
|
|
let mut jdb = new_db();
|
2016-03-12 09:51:17 +01:00
|
|
|
|
|
|
|
// history is 4
|
|
|
|
let foo = jdb.insert(b"foo");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(0, &keccak(b"0"), None).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
jdb.remove(&foo);
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(1, &keccak(b"1"), None).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
jdb.insert(b"foo");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(2, &keccak(b"2"), None).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
jdb.remove(&foo);
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(3, &keccak(b"3"), None).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
jdb.insert(b"foo");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(4, &keccak(b"4"), Some((0, keccak(b"0")))).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
// expunge foo
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(5, &keccak(b"5"), Some((1, keccak(b"1")))).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn forked_insert_delete_insert_delete_insert_expunge() {
|
|
|
|
init_log();
|
2017-10-13 17:10:03 +02:00
|
|
|
let mut jdb = new_db();
|
2016-03-12 09:51:17 +01:00
|
|
|
|
|
|
|
// history is 4
|
|
|
|
let foo = jdb.insert(b"foo");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(0, &keccak(b"0"), None).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
|
|
|
|
jdb.remove(&foo);
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(1, &keccak(b"1a"), None).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
|
|
|
|
jdb.remove(&foo);
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(1, &keccak(b"1b"), None).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
|
|
|
|
jdb.insert(b"foo");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(2, &keccak(b"2a"), None).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
|
|
|
|
jdb.insert(b"foo");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(2, &keccak(b"2b"), None).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
|
|
|
|
jdb.remove(&foo);
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(3, &keccak(b"3a"), None).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
|
|
|
|
jdb.remove(&foo);
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(3, &keccak(b"3b"), None).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
|
|
|
|
jdb.insert(b"foo");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(4, &keccak(b"4a"), Some((0, keccak(b"0")))).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
|
|
|
|
jdb.insert(b"foo");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(4, &keccak(b"4b"), Some((0, keccak(b"0")))).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
|
|
|
|
// expunge foo
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(5, &keccak(b"5"), Some((1, keccak(b"1a")))).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn broken_assert() {
|
2017-10-13 17:10:03 +02:00
|
|
|
let mut jdb = new_db();
|
2016-03-12 09:51:17 +01:00
|
|
|
|
|
|
|
// history is 1
|
|
|
|
let foo = jdb.insert(b"foo");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(1, &keccak(b"1"), Some((0, keccak(b"0")))).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
|
|
|
|
// foo is ancient history.
|
|
|
|
|
|
|
|
jdb.remove(&foo);
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(2, &keccak(b"2"), Some((1, keccak(b"1")))).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
|
|
|
|
jdb.insert(b"foo");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(3, &keccak(b"3"), Some((2, keccak(b"2")))).unwrap(); // BROKEN
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
2016-03-12 09:51:17 +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();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(5, &keccak(b"5"), Some((4, keccak(b"4")))).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(!jdb.contains(&foo));
|
2016-03-12 09:51:17 +01:00
|
|
|
}
|
2016-03-13 21:06:24 +01:00
|
|
|
|
2016-03-12 09:51:17 +01:00
|
|
|
#[test]
|
|
|
|
fn reopen_test() {
|
2017-10-13 17:10:03 +02:00
|
|
|
let mut jdb = new_db();
|
2016-03-12 09:51:17 +01:00
|
|
|
|
|
|
|
// history is 4
|
|
|
|
let foo = jdb.insert(b"foo");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(0, &keccak(b"0"), None).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(1, &keccak(b"1"), None).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(2, &keccak(b"2"), None).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(3, &keccak(b"3"), None).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(4, &keccak(b"4"), Some((0, keccak(b"0")))).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
|
|
|
|
// foo is ancient history.
|
|
|
|
|
|
|
|
jdb.insert(b"foo");
|
|
|
|
let bar = jdb.insert(b"bar");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(5, &keccak(b"5"), Some((1, keccak(b"1")))).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
jdb.remove(&foo);
|
|
|
|
jdb.remove(&bar);
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(6, &keccak(b"6"), Some((2, keccak(b"2")))).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
jdb.insert(b"foo");
|
|
|
|
jdb.insert(b"bar");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(7, &keccak(b"7"), Some((3, keccak(b"3")))).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
|
|
|
}
|
2016-03-13 21:06:24 +01:00
|
|
|
|
2016-03-12 09:51:17 +01:00
|
|
|
#[test]
|
|
|
|
fn reopen_remove_three() {
|
|
|
|
init_log();
|
|
|
|
|
2017-10-15 16:17:15 +02:00
|
|
|
let shared_db = Arc::new(kvdb_memorydb::create(0));
|
2017-08-30 16:20:21 +02:00
|
|
|
let foo = keccak(b"foo");
|
2016-03-12 09:51:17 +01:00
|
|
|
|
|
|
|
{
|
2017-10-13 17:10:03 +02:00
|
|
|
let mut jdb = EarlyMergeDB::new(shared_db.clone(), None);
|
2016-03-06 22:39:04 +01:00
|
|
|
// history is 1
|
2016-03-12 09:51:17 +01:00
|
|
|
jdb.insert(b"foo");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(0, &keccak(b"0"), None).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(1, &keccak(b"1"), None).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-03-07 09:10:02 +01:00
|
|
|
|
|
|
|
// foo is ancient history.
|
|
|
|
|
2016-03-12 09:51:17 +01:00
|
|
|
jdb.remove(&foo);
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(2, &keccak(b"2"), Some((0, keccak(b"0")))).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
2016-03-12 09:51:17 +01:00
|
|
|
|
2016-03-06 22:39:04 +01:00
|
|
|
jdb.insert(b"foo");
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(3, &keccak(b"3"), Some((1, keccak(b"1")))).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
2016-03-12 09:51:17 +01:00
|
|
|
|
|
|
|
// incantation to reopen the db
|
2016-07-28 23:46:24 +02:00
|
|
|
}; {
|
2017-10-13 17:10:03 +02:00
|
|
|
let mut jdb = EarlyMergeDB::new(shared_db.clone(), None);
|
2016-03-06 22:39:04 +01:00
|
|
|
|
|
|
|
jdb.remove(&foo);
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(4, &keccak(b"4"), Some((2, keccak(b"2")))).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
2016-03-12 09:51:17 +01:00
|
|
|
|
|
|
|
// incantation to reopen the db
|
2016-07-28 23:46:24 +02:00
|
|
|
}; {
|
2017-10-13 17:10:03 +02:00
|
|
|
let mut jdb = EarlyMergeDB::new(shared_db.clone(), None);
|
2016-03-12 09:51:17 +01:00
|
|
|
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(5, &keccak(b"5"), Some((3, keccak(b"3")))).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
2016-03-12 09:51:17 +01:00
|
|
|
|
|
|
|
// incantation to reopen the db
|
2016-07-28 23:46:24 +02:00
|
|
|
}; {
|
2017-10-13 17:10:03 +02:00
|
|
|
let mut jdb = EarlyMergeDB::new(shared_db, None);
|
2016-03-12 09:51:17 +01:00
|
|
|
|
2017-08-30 16:20:21 +02:00
|
|
|
jdb.commit_batch(6, &keccak(b"6"), Some((4, keccak(b"4")))).unwrap();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(!jdb.contains(&foo));
|
2016-03-06 22:39:04 +01:00
|
|
|
}
|
|
|
|
}
|
2016-03-13 21:06:24 +01:00
|
|
|
|
2016-03-06 22:39:04 +01:00
|
|
|
#[test]
|
|
|
|
fn reopen_fork() {
|
2017-10-15 16:17:15 +02:00
|
|
|
let shared_db = Arc::new(kvdb_memorydb::create(0));
|
2017-10-13 17:10:03 +02:00
|
|
|
|
2016-03-06 22:39:04 +01:00
|
|
|
let (foo, bar, baz) = {
|
2017-10-13 17:10:03 +02:00
|
|
|
let mut jdb = EarlyMergeDB::new(shared_db.clone(), None);
|
2016-03-06 22:39:04 +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-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-03-06 22:39:04 +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();
|
2016-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-03-06 22:39:04 +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-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-03-06 22:39:04 +01:00
|
|
|
(foo, bar, baz)
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
2017-10-13 17:10:03 +02:00
|
|
|
let mut jdb = EarlyMergeDB::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-03-12 09:51:17 +01:00
|
|
|
assert!(jdb.can_reconstruct_refs());
|
2016-06-23 11:16:11 +02:00
|
|
|
assert!(jdb.contains(&foo));
|
|
|
|
assert!(!jdb.contains(&baz));
|
|
|
|
assert!(!jdb.contains(&bar));
|
2016-03-06 22:39:04 +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();
|
2016-08-03 16:34:32 +02:00
|
|
|
let key = jdb.insert(b"dog");
|
|
|
|
jdb.inject_batch().unwrap();
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
assert!(jdb.get(&key).is_none());
|
|
|
|
}
|
2016-01-18 13:30:01 +01:00
|
|
|
}
|