Merge pull request #414 from ethcore/state

Use latest era instead of end era as journal marker
This commit is contained in:
Gav Wood 2016-02-11 18:44:52 +01:00
commit 0a41dedc05
2 changed files with 37 additions and 9 deletions

View File

@ -179,7 +179,7 @@ pub struct Client {
} }
const HISTORY: u64 = 1000; const HISTORY: u64 = 1000;
const CLIENT_DB_VER_STR: &'static str = "2.0"; const CLIENT_DB_VER_STR: &'static str = "2.1";
impl Client { impl Client {
/// Create a new client with given spec and DB path. /// Create a new client with given spec and DB path.

View File

@ -47,10 +47,10 @@ impl Clone for JournalDB {
} }
} }
const LAST_ERA_KEY : [u8; 4] = [ b'l', b'a', b's', b't' ]; const LATEST_ERA_KEY : [u8; 4] = [ b'l', b'a', b's', b't' ];
const VERSION_KEY : [u8; 4] = [ b'j', b'v', b'e', b'r' ]; const VERSION_KEY : [u8; 4] = [ b'j', b'v', b'e', b'r' ];
const DB_VERSION: u32 = 1; const DB_VERSION: u32 = 2;
impl JournalDB { impl JournalDB {
/// Create a new instance given a `backing` database. /// Create a new instance given a `backing` database.
@ -87,7 +87,7 @@ impl JournalDB {
/// Check if this database has any commits /// Check if this database has any commits
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
self.backing.get(&LAST_ERA_KEY).expect("Low level database error").is_none() self.backing.get(&LATEST_ERA_KEY).expect("Low level database error").is_none()
} }
/// Commit all recent insert operations and historical removals from the old era /// Commit all recent insert operations and historical removals from the old era
@ -144,6 +144,7 @@ impl JournalDB {
r.append(&inserts); r.append(&inserts);
r.append(&removes); r.append(&removes);
try!(batch.put(&last, r.as_raw())); try!(batch.put(&last, r.as_raw()));
try!(batch.put(&LATEST_ERA_KEY, &encode(&now)));
} }
// apply old commits' details // apply old commits' details
@ -181,7 +182,6 @@ impl JournalDB {
try!(batch.delete(&h)); try!(batch.delete(&h));
deletes += 1; deletes += 1;
} }
try!(batch.put(&LAST_ERA_KEY, &encode(&end_era)));
trace!("JournalDB: delete journal for time #{}.{}, (canon was {}): {} entries", end_era, index, canon_id, deletes); trace!("JournalDB: delete journal for time #{}.{}, (canon was {}): {} entries", end_era, index, canon_id, deletes);
} }
@ -228,8 +228,8 @@ impl JournalDB {
fn read_counters(db: &DB) -> HashMap<H256, i32> { fn read_counters(db: &DB) -> HashMap<H256, i32> {
let mut res = HashMap::new(); let mut res = HashMap::new();
if let Some(val) = db.get(&LAST_ERA_KEY).expect("Low-level database error.") { if let Some(val) = db.get(&LATEST_ERA_KEY).expect("Low-level database error.") {
let mut era = decode::<u64>(&val) + 1; let mut era = decode::<u64>(&val);
loop { loop {
let mut index = 0usize; let mut index = 0usize;
while let Some(rlp_data) = db.get({ while let Some(rlp_data) = db.get({
@ -245,10 +245,10 @@ impl JournalDB {
} }
index += 1; index += 1;
}; };
if index == 0 { if index == 0 || era == 0 {
break; break;
} }
era += 1; era -= 1;
} }
} }
trace!("Recovered {} counters", res.len()); trace!("Recovered {} counters", res.len());
@ -426,4 +426,32 @@ mod tests {
jdb.commit(2, &b"2a".sha3(), Some((1, b"1a".sha3()))).unwrap(); jdb.commit(2, &b"2a".sha3(), Some((1, b"1a".sha3()))).unwrap();
assert!(jdb.exists(&foo)); assert!(jdb.exists(&foo));
} }
#[test]
fn reopen() {
use rocksdb::DB;
let mut dir = ::std::env::temp_dir();
dir.push(H32::random().hex());
let foo = {
let mut jdb = JournalDB::new(DB::open_default(dir.to_str().unwrap()).unwrap());
// history is 1
let foo = jdb.insert(b"foo");
jdb.commit(0, &b"0".sha3(), None).unwrap();
foo
};
{
let mut jdb = JournalDB::new(DB::open_default(dir.to_str().unwrap()).unwrap());
jdb.remove(&foo);
jdb.commit(1, &b"1".sha3(), Some((0, b"0".sha3()))).unwrap();
}
{
let mut jdb = JournalDB::new(DB::open_default(dir.to_str().unwrap()).unwrap());
assert!(jdb.exists(&foo));
jdb.commit(2, &b"2".sha3(), Some((1, b"1".sha3()))).unwrap();
assert!(!jdb.exists(&foo));
}
}
} }