Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
remove impossible panickers related to infallible db transaction (#1947)
Browse files Browse the repository at this point in the history
  • Loading branch information
rphmeier authored and gavofyork committed Aug 18, 2016
1 parent 108024e commit 57dbdaa
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 68 deletions.
20 changes: 10 additions & 10 deletions ethcore/src/blockchain/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,12 +366,12 @@ impl BlockChain {
};

let batch = DBTransaction::new(&db);
batch.put(DB_COL_HEADERS, &hash, block.header_rlp().as_raw()).unwrap();
batch.put(DB_COL_BODIES, &hash, &Self::block_to_body(genesis)).unwrap();
batch.put(DB_COL_HEADERS, &hash, block.header_rlp().as_raw());
batch.put(DB_COL_BODIES, &hash, &Self::block_to_body(genesis));

batch.write(DB_COL_EXTRA, &hash, &details);
batch.write(DB_COL_EXTRA, &header.number(), &hash);
batch.put(DB_COL_EXTRA, b"best", &hash).unwrap();
batch.put(DB_COL_EXTRA, b"best", &hash);
bc.db.write(batch).expect("Low level database error. Some issue with disk?");
hash
}
Expand Down Expand Up @@ -415,15 +415,15 @@ impl BlockChain {
}
if let Some(extras) = self.db.read(DB_COL_EXTRA, &best_block_hash) as Option<BlockDetails> {
type DetailsKey = Key<BlockDetails, Target=H264>;
batch.delete(DB_COL_EXTRA, &(DetailsKey::key(&best_block_hash))).unwrap();
batch.delete(DB_COL_EXTRA, &(DetailsKey::key(&best_block_hash)));
let hash = extras.parent;
let range = extras.number as bc::Number .. extras.number as bc::Number;
let chain = bc::group::BloomGroupChain::new(self.blooms_config, self);
let changes = chain.replace(&range, vec![]);
for (k, v) in changes.into_iter() {
batch.write(DB_COL_EXTRA, &LogGroupPosition::from(k), &BloomGroup::from(v));
}
batch.put(DB_COL_EXTRA, b"best", &hash).unwrap();
batch.put(DB_COL_EXTRA, b"best", &hash);

let best_block_total_difficulty = self.block_details(&hash).unwrap().total_difficulty;
let best_block_rlp = self.block(&hash).unwrap();
Expand Down Expand Up @@ -566,8 +566,8 @@ impl BlockChain {
let compressed_body = UntrustedRlp::new(&Self::block_to_body(bytes)).compress(RlpType::Blocks);

// store block in db
batch.put(DB_COL_HEADERS, &hash, &compressed_header).unwrap();
batch.put(DB_COL_BODIES, &hash, &compressed_body).unwrap();
batch.put(DB_COL_HEADERS, &hash, &compressed_header);
batch.put(DB_COL_BODIES, &hash, &compressed_body);

let maybe_parent = self.block_details(&header.parent_hash());

Expand Down Expand Up @@ -669,8 +669,8 @@ impl BlockChain {
assert!(self.pending_best_block.read().is_none());

// store block in db
batch.put_compressed(DB_COL_HEADERS, &hash, block.header_rlp().as_raw().to_vec()).unwrap();
batch.put_compressed(DB_COL_BODIES, &hash, Self::block_to_body(bytes)).unwrap();
batch.put_compressed(DB_COL_HEADERS, &hash, block.header_rlp().as_raw().to_vec());
batch.put_compressed(DB_COL_BODIES, &hash, Self::block_to_body(bytes));

let info = self.block_info(&header);

Expand Down Expand Up @@ -768,7 +768,7 @@ impl BlockChain {
match update.info.location {
BlockLocation::Branch => (),
_ => if is_best {
batch.put(DB_COL_EXTRA, b"best", &update.info.hash).unwrap();
batch.put(DB_COL_EXTRA, b"best", &update.info.hash);
*best_block = Some(BestBlock {
hash: update.info.hash,
number: update.info.number,
Expand Down
2 changes: 1 addition & 1 deletion ethcore/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ impl Client {
retracted: route.retracted.len()
});
// Final commit to the DB
self.db.write_buffered(batch).expect("DB write failed.");
self.db.write_buffered(batch);
self.chain.commit();

self.update_last_hashes(&parent, hash);
Expand Down
5 changes: 1 addition & 4 deletions ethcore/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,7 @@ pub trait Readable {

impl Writable for DBTransaction {
fn write<T, R>(&self, col: Option<u32>, key: &Key<T, Target = R>, value: &T) where T: Encodable, R: Deref<Target = [u8]> {
let result = self.put(col, &key.key(), &encode(value));
if let Err(err) = result {
panic!("db put failed, key: {:?}, err: {:?}", &key.key() as &[u8], err);
}
self.put(col, &key.key(), &encode(value));
}
}

Expand Down
4 changes: 2 additions & 2 deletions ethcore/src/trace/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ impl<T> TraceDB<T> where T: DatabaseExtras {
};

let batch = DBTransaction::new(&tracesdb);
batch.put(DB_COL_TRACE, b"enabled", &encoded_tracing).unwrap();
batch.put(DB_COL_TRACE, b"version", TRACE_DB_VER).unwrap();
batch.put(DB_COL_TRACE, b"enabled", &encoded_tracing);
batch.put(DB_COL_TRACE, b"version", TRACE_DB_VER);
tracesdb.write(batch).unwrap();

let db = TraceDB {
Expand Down
12 changes: 6 additions & 6 deletions util/src/journaldb/archivedb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl JournalDB for ArchiveDB {
let (key, (value, rc)) = i;
if rc > 0 {
assert!(rc == 1);
batch.put(self.column, &key, &value).expect("Low-level database error. Some issue with your hard disk?");
batch.put(self.column, &key, &value);
inserts += 1;
}
if rc < 0 {
Expand All @@ -175,11 +175,11 @@ impl JournalDB for ArchiveDB {

for (mut key, value) in self.overlay.drain_aux().into_iter() {
key.push(AUX_FLAG);
batch.put(self.column, &key, &value).expect("Low-level database error. Some issue with your hard disk?");
batch.put(self.column, &key, &value);
}

if self.latest_era.map_or(true, |e| now > e) {
try!(batch.put(self.column, &LATEST_ERA_KEY, &encode(&now)));
batch.put(self.column, &LATEST_ERA_KEY, &encode(&now));
self.latest_era = Some(now);
}
Ok((inserts + deletes) as u32)
Expand All @@ -196,22 +196,22 @@ impl JournalDB for ArchiveDB {
if try!(self.backing.get(self.column, &key)).is_some() {
return Err(BaseDataError::AlreadyExists(key).into());
}
try!(batch.put(self.column, &key, &value));
batch.put(self.column, &key, &value);
inserts += 1;
}
if rc < 0 {
assert!(rc == -1);
if try!(self.backing.get(self.column, &key)).is_none() {
return Err(BaseDataError::NegativelyReferencedHash(key).into());
}
try!(batch.delete(self.column, &key));
batch.delete(self.column, &key);
deletes += 1;
}
}

for (mut key, value) in self.overlay.drain_aux().into_iter() {
key.push(AUX_FLAG);
try!(batch.put(self.column, &key, &value));
batch.put(self.column, &key, &value);
}

Ok((inserts + deletes) as u32)
Expand Down
20 changes: 10 additions & 10 deletions util/src/journaldb/earlymergedb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ impl EarlyMergeDB {
}

// The next three are valid only as long as there is an insert operation of `key` in the journal.
fn set_already_in(batch: &DBTransaction, col: Option<u32>, key: &H256) { batch.put(col, &Self::morph_key(key, 0), &[1u8]).expect("Low-level database error. Some issue with your hard disk?"); }
fn reset_already_in(batch: &DBTransaction, col: Option<u32>, key: &H256) { batch.delete(col, &Self::morph_key(key, 0)).expect("Low-level database error. Some issue with your hard disk?"); }
fn set_already_in(batch: &DBTransaction, col: Option<u32>, key: &H256) { batch.put(col, &Self::morph_key(key, 0), &[1u8]); }
fn reset_already_in(batch: &DBTransaction, col: Option<u32>, key: &H256) { batch.delete(col, &Self::morph_key(key, 0)); }
fn is_already_in(backing: &Database, col: Option<u32>, key: &H256) -> bool {
backing.get(col, &Self::morph_key(key, 0)).expect("Low-level database error. Some issue with your hard disk?").is_some()
}
Expand Down Expand Up @@ -132,7 +132,7 @@ impl EarlyMergeDB {
// 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));
batch.put(col, h, d).expect("Low-level database error. Some issue with your hard disk?");
batch.put(col, h, d);
refs.insert(h.clone(), RefInfo{queue_refs: 1, in_archive: false});
if trace {
trace!(target: "jdb.fine", " insert({}): New to queue, not in DB: Inserting into queue and DB", h);
Expand Down Expand Up @@ -193,15 +193,15 @@ impl EarlyMergeDB {
}
Some(RefInfo{queue_refs: 1, in_archive: false}) => {
refs.remove(h);
batch.delete(col, h).expect("Low-level database error. Some issue with your hard disk?");
batch.delete(col, h);
if trace {
trace!(target: "jdb.fine", " remove({}): Not in archive, only 1 ref in queue: Removing from queue and DB", h);
}
}
None => {
// 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));
batch.delete(col, h).expect("Low-level database error. Some issue with your hard disk?");
batch.delete(col, h);
if trace {
trace!(target: "jdb.fine", " remove({}): Not in queue - MUST BE IN ARCHIVE: Removing from DB", h);
}
Expand Down Expand Up @@ -436,9 +436,9 @@ impl JournalDB for EarlyMergeDB {
trace!(target: "jdb.ops", " Inserts: {:?}", ins);
trace!(target: "jdb.ops", " Deletes: {:?}", removes);
}
try!(batch.put(self.column, &last, r.as_raw()));
batch.put(self.column, &last, r.as_raw());
if self.latest_era.map_or(true, |e| now > e) {
try!(batch.put(self.column, &LATEST_ERA_KEY, &encode(&now)));
batch.put(self.column, &LATEST_ERA_KEY, &encode(&now));
self.latest_era = Some(now);
}
}
Expand Down Expand Up @@ -499,7 +499,7 @@ impl JournalDB for EarlyMergeDB {
Self::remove_keys(&inserts, &mut refs, batch, self.column, RemoveFrom::Queue, trace);
}

try!(batch.delete(self.column, &last));
batch.delete(self.column, &last);
index += 1;
}
if trace {
Expand All @@ -525,13 +525,13 @@ impl JournalDB for EarlyMergeDB {
if try!(self.backing.get(self.column, &key)).is_some() {
return Err(BaseDataError::AlreadyExists(key).into());
}
try!(batch.put(self.column, &key, &value))
batch.put(self.column, &key, &value)
}
-1 => {
if try!(self.backing.get(self.column, &key)).is_none() {
return Err(BaseDataError::NegativelyReferencedHash(key).into());
}
try!(batch.delete(self.column, &key))
batch.delete(self.column, &key)
}
_ => panic!("Attempted to inject invalid state."),
}
Expand Down
14 changes: 7 additions & 7 deletions util/src/journaldb/overlayrecentdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ impl JournalDB for OverlayRecentDB {
k.append(&now);
k.append(&index);
k.append(&&PADDING[..]);
try!(batch.put_vec(self.column, &k.drain(), r.out()));
batch.put_vec(self.column, &k.drain(), r.out());
if journal_overlay.latest_era.map_or(true, |e| now > e) {
try!(batch.put_vec(self.column, &LATEST_ERA_KEY, encode(&now).to_vec()));
batch.put_vec(self.column, &LATEST_ERA_KEY, encode(&now).to_vec());
journal_overlay.latest_era = Some(now);
}
journal_overlay.journal.entry(now).or_insert_with(Vec::new).push(JournalEntry { id: id.clone(), insertions: inserted_keys, deletions: removed_keys });
Expand All @@ -272,7 +272,7 @@ impl JournalDB for OverlayRecentDB {
r.append(&end_era);
r.append(&index);
r.append(&&PADDING[..]);
try!(batch.delete(self.column, &r.drain()));
batch.delete(self.column, &r.drain());
trace!("commit: Delete journal for time #{}.{}: {}, (canon was {}): +{} -{} entries", end_era, index, journal.id, canon_id, journal.insertions.len(), journal.deletions.len());
{
if canon_id == journal.id {
Expand All @@ -291,7 +291,7 @@ impl JournalDB for OverlayRecentDB {
}
// apply canon inserts first
for (k, v) in canon_insertions {
try!(batch.put(self.column, &k, &v));
batch.put(self.column, &k, &v);
journal_overlay.pending_overlay.insert(to_short_key(&k), v);
}
// update the overlay
Expand All @@ -301,7 +301,7 @@ impl JournalDB for OverlayRecentDB {
// apply canon deletions
for k in canon_deletions {
if !journal_overlay.backing_overlay.contains(&to_short_key(&k)) {
try!(batch.delete(self.column, &k));
batch.delete(self.column, &k);
}
}
}
Expand All @@ -325,13 +325,13 @@ impl JournalDB for OverlayRecentDB {
if try!(self.backing.get(self.column, &key)).is_some() {
return Err(BaseDataError::AlreadyExists(key).into());
}
try!(batch.put(self.column, &key, &value))
batch.put(self.column, &key, &value)
}
-1 => {
if try!(self.backing.get(self.column, &key)).is_none() {
return Err(BaseDataError::NegativelyReferencedHash(key).into());
}
try!(batch.delete(self.column, &key))
batch.delete(self.column, &key)
}
_ => panic!("Attempted to inject invalid state."),
}
Expand Down
6 changes: 3 additions & 3 deletions util/src/journaldb/refcounteddb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,15 @@ impl JournalDB for RefCountedDB {
r.append(id);
r.append(&self.inserts);
r.append(&self.removes);
try!(batch.put(self.column, &last, r.as_raw()));
batch.put(self.column, &last, r.as_raw());

trace!(target: "rcdb", "new journal for time #{}.{} => {}: inserts={:?}, removes={:?}", now, index, id, self.inserts, self.removes);

self.inserts.clear();
self.removes.clear();

if self.latest_era.map_or(true, |e| now > e) {
try!(batch.put(self.column, &LATEST_ERA_KEY, &encode(&now)));
batch.put(self.column, &LATEST_ERA_KEY, &encode(&now));
self.latest_era = Some(now);
}
}
Expand All @@ -176,7 +176,7 @@ impl JournalDB for RefCountedDB {
for i in &to_remove {
self.forward.remove(i);
}
try!(batch.delete(self.column, &last));
batch.delete(self.column, &last);
index += 1;
}
}
Expand Down
Loading

0 comments on commit 57dbdaa

Please sign in to comment.