replace db printlns with panics

This commit is contained in:
debris 2016-11-18 01:07:19 +01:00
parent 6fa1ce326a
commit 09537c3a84
2 changed files with 23 additions and 40 deletions

View File

@ -7,7 +7,7 @@ use byteorder::{LittleEndian, ByteOrder};
use primitives::hash::H256; use primitives::hash::H256;
use primitives::bytes::Bytes; use primitives::bytes::Bytes;
use super::{BlockRef, BestBlock, BlockLocation}; use super::{BlockRef, BestBlock, BlockLocation};
use serialization; use serialization::{self, deserialize};
use chain::{self, RepresentH256}; use chain::{self, RepresentH256};
use parking_lot::RwLock; use parking_lot::RwLock;
use transaction_meta::TransactionMeta; use transaction_meta::TransactionMeta;
@ -110,21 +110,11 @@ impl Storage {
self.read_meta(key).map(|val| LittleEndian::read_u32(&val)) self.read_meta(key).map(|val| LittleEndian::read_u32(&val))
} }
/// is invoked on database non-fatal query errors
fn db_error(&self, msg: String) {
println!("Low-level database error: {}", &msg);
}
/// get the value of the key in the database /// get the value of the key in the database
/// if the key is not present, reports non-fatal error and returns nothing
fn get(&self, col: u32, key: &[u8]) -> Option<Bytes> { fn get(&self, col: u32, key: &[u8]) -> Option<Bytes> {
let res = self.database.get(Some(col), key); match self.database.get(Some(col), key) {
match res { Ok(val) => val,
Err(msg) => { Err(msg) => panic!("{}", msg),
self.db_error(msg);
None
},
Ok(val) => val.map(|v| v.into()),
} }
} }
@ -140,7 +130,7 @@ impl Storage {
/// loads block transaction list by the provided block hash /// loads block transaction list by the provided block hash
fn block_transaction_hashes_by_hash(&self, h: &H256) -> Vec<H256> { fn block_transaction_hashes_by_hash(&self, h: &H256) -> Vec<H256> {
self.get(COL_BLOCK_TRANSACTIONS, &**h) self.get(COL_BLOCK_TRANSACTIONS, &**h)
.unwrap_or(Vec::new().into()) .unwrap_or_else(Bytes::new)
.chunks(H256::size()) .chunks(H256::size())
.map(H256::from) .map(H256::from)
.collect() .collect()
@ -150,25 +140,19 @@ impl Storage {
self.block_transaction_hashes_by_hash(h) self.block_transaction_hashes_by_hash(h)
.into_iter() .into_iter()
.filter_map(|tx_hash| { .filter_map(|tx_hash| {
self.transaction_bytes(&tx_hash).and_then(|tx_bytes| { self.transaction_bytes(&tx_hash)
match serialization::deserialize::<_, chain::Transaction>(tx_bytes.as_ref()) { .map(|tx_bytes| {
Ok(tx) => Some(tx), deserialize::<_, chain::Transaction>(tx_bytes.as_ref())
Err(e) => { .expect("Error deserializing transaction, possible db corruption")
self.db_error(format!("Error deserializing transaction, possible db corruption ({:?})", e)); })
None
}
}
})
}) })
.collect() .collect()
} }
fn block_header_by_hash(&self, h: &H256) -> Option<chain::BlockHeader> { fn block_header_by_hash(&self, h: &H256) -> Option<chain::BlockHeader> {
self.get(COL_BLOCK_HEADERS, &**h).and_then(|val| self.get(COL_BLOCK_HEADERS, &**h).map(|val| {
serialization::deserialize(val.as_ref()).map_err( deserialize(val.as_ref()).expect("Error deserializing block header, possible db corruption")
|e| self.db_error(format!("Error deserializing block header, possible db corruption ({:?})", e)) })
).ok()
)
} }
@ -330,7 +314,7 @@ impl Storage {
Err(e) => { Err(e) => {
// todo: log error here // todo: log error here
context.restore(); context.restore();
println!("Error while reorganizing to {}: {:?}", hash, e); error!("Error while reorganizing to {}: {:?}", hash, e);
Err(e) Err(e)
} }
} }
@ -410,16 +394,11 @@ impl BlockProvider for Storage {
fn block(&self, block_ref: BlockRef) -> Option<chain::Block> { fn block(&self, block_ref: BlockRef) -> Option<chain::Block> {
self.resolve_hash(block_ref).and_then(|block_hash| self.resolve_hash(block_ref).and_then(|block_hash|
self.get(COL_BLOCK_HEADERS, &*block_hash) self.get(COL_BLOCK_HEADERS, &*block_hash)
.and_then(|header_bytes| { .map(|header_bytes| {
let transactions = self.block_transactions_by_hash(&block_hash);; let transactions = self.block_transactions_by_hash(&block_hash);
let maybe_header = match serialization::deserialize::<_, chain::BlockHeader>(header_bytes.as_ref()) { let header = deserialize::<_, chain::BlockHeader>(header_bytes.as_ref())
Ok(header) => Some(header), .expect("Error deserializing header, possible db corruption");
Err(e) => { chain::Block::new(header, transactions)
self.db_error(format!("Error deserializing header, possible db corruption ({:?})", e));
None
}
};
maybe_header.map(|header| chain::Block::new(header, transactions))
}) })
) )
} }

View File

@ -5,6 +5,10 @@ use hex::{ToHex, FromHex, FromHexError};
pub struct Bytes(Vec<u8>); pub struct Bytes(Vec<u8>);
impl Bytes { impl Bytes {
pub fn new() -> Self {
Bytes::default()
}
pub fn new_with_len(len: usize) -> Self { pub fn new_with_len(len: usize) -> Self {
Bytes(vec![0; len]) Bytes(vec![0; len])
} }