Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: better logging for lmdb_delete #3218

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ impl<'a, B: BlockchainBackend + 'static> HorizonStateSynchronization<'a, B> {
);
let header = self.db().fetch_header(self.horizon_sync_height).await?.ok_or_else(|| {
ChainStorageError::ValueNotFound {
entity: "Header".to_string(),
field: "height".to_string(),
entity: "Header",
field: "height",
value: self.horizon_sync_height.to_string(),
}
})?;
Expand Down
4 changes: 2 additions & 2 deletions base_layer/core/src/base_node/sync/header_sync/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ impl<B: BlockchainBackend + 'static> BlockHeaderSyncValidator<B> {
.fetch_header_accumulated_data(start_hash.clone())
.await?
.ok_or_else(|| ChainStorageError::ValueNotFound {
entity: "BlockHeaderAccumulatedData".to_string(),
field: "hash".to_string(),
entity: "BlockHeaderAccumulatedData",
field: "hash",
value: start_hash.to_hex(),
})?;
debug!(
Expand Down
43 changes: 21 additions & 22 deletions base_layer/core/src/chain_storage/blockchain_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,8 @@ where B: BlockchainBackend
let start_header =
self.fetch_header_by_block_hash(start_hash.clone())?
.ok_or_else(|| ChainStorageError::ValueNotFound {
entity: "BlockHeader".to_string(),
field: "start_hash".to_string(),
entity: "BlockHeader",
field: "start_hash",
value: start_hash.to_hex(),
})?;
let constants = self.consensus_manager.consensus_constants(start_header.height);
Expand Down Expand Up @@ -502,8 +502,8 @@ where B: BlockchainBackend
let accumulated_data =
db.fetch_header_accumulated_data(&hash)?
.ok_or_else(|| ChainStorageError::ValueNotFound {
entity: "BlockHeaderAccumulatedData".to_string(),
field: "hash".to_string(),
entity: "BlockHeaderAccumulatedData",
field: "hash",
value: hash.to_hex(),
})?;

Expand Down Expand Up @@ -568,8 +568,8 @@ where B: BlockchainBackend
let db = self.db_read_access()?;
db.fetch_block_accumulated_data(&at_hash)?
.ok_or_else(|| ChainStorageError::ValueNotFound {
entity: "BlockAccumulatedData".to_string(),
field: "at_hash".to_string(),
entity: "BlockAccumulatedData",
field: "at_hash",
value: at_hash.to_hex(),
})
}
Expand Down Expand Up @@ -807,8 +807,8 @@ where B: BlockchainBackend

if end > metadata.height_of_longest_chain() {
return Err(ChainStorageError::ValueNotFound {
entity: "Block".to_string(),
field: "end height".to_string(),
entity: "Block",
field: "end height",
value: end.to_string(),
});
}
Expand Down Expand Up @@ -948,8 +948,8 @@ pub fn calculate_mmr_roots<T: BlockchainBackend>(db: &T, block: &Block) -> Resul
} = db
.fetch_block_accumulated_data(&header.prev_hash)?
.ok_or_else(|| ChainStorageError::ValueNotFound {
entity: "BlockAccumulatedData".to_string(),
field: "header_hash".to_string(),
entity: "BlockAccumulatedData",
field: "header_hash",
value: header.prev_hash.to_hex(),
})?;

Expand Down Expand Up @@ -980,8 +980,8 @@ pub fn calculate_mmr_roots<T: BlockchainBackend>(db: &T, block: &Block) -> Resul
output_mmr
.find_leaf_index(&output_hash)?
.ok_or_else(|| ChainStorageError::ValueNotFound {
entity: "UTXO".to_string(),
field: "hash".to_string(),
entity: "UTXO",
field: "hash",
value: output_hash.to_hex(),
})?;
debug!(
Expand Down Expand Up @@ -1251,8 +1251,8 @@ fn fetch_block_with_kernel<T: BlockchainBackend>(
None => Ok(None),
},
Err(_) => Err(ChainStorageError::ValueNotFound {
entity: "Kernel".to_string(),
field: "Excess sig".to_string(),
entity: "Kernel",
field: "Excess sig",
value: excess_sig.get_signature().to_hex(),
}),
}
Expand All @@ -1271,8 +1271,8 @@ fn fetch_block_with_utxo<T: BlockchainBackend>(
None => Ok(None),
},
Err(_) => Err(ChainStorageError::ValueNotFound {
entity: "Output".to_string(),
field: "Commitment".to_string(),
entity: "Output",
field: "Commitment",
value: commitment.to_hex(),
}),
}
Expand Down Expand Up @@ -1425,12 +1425,11 @@ fn rewind_to_hash<T: BlockchainBackend>(
block_hash: BlockHash,
) -> Result<Vec<Arc<ChainBlock>>, ChainStorageError> {
let block_hash_hex = block_hash.to_hex();
let target_header =
fetch_header_by_block_hash(&*db, block_hash)?.ok_or_else(|| ChainStorageError::ValueNotFound {
entity: "BlockHeader".to_string(),
field: "block_hash".to_string(),
value: block_hash_hex,
})?;
let target_header = fetch_header_by_block_hash(&*db, block_hash)?.ok_or(ChainStorageError::ValueNotFound {
entity: "BlockHeader",
field: "block_hash",
value: block_hash_hex,
})?;
rewind_to_height(db, target_header.height)
}

Expand Down
6 changes: 3 additions & 3 deletions base_layer/core/src/chain_storage/db_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,9 @@ pub enum DbKey {
impl DbKey {
pub fn to_value_not_found_error(&self) -> ChainStorageError {
let (entity, field, value) = match self {
DbKey::BlockHeader(v) => ("BlockHeader".to_string(), "Height".to_string(), v.to_string()),
DbKey::BlockHash(v) => ("Block".to_string(), "Hash".to_string(), v.to_hex()),
DbKey::OrphanBlock(v) => ("Orphan".to_string(), "Hash".to_string(), v.to_hex()),
DbKey::BlockHeader(v) => ("BlockHeader", "Height", v.to_string()),
DbKey::BlockHash(v) => ("Block", "Hash", v.to_hex()),
DbKey::OrphanBlock(v) => ("Orphan", "Hash", v.to_hex()),
};
ChainStorageError::ValueNotFound { entity, field, value }
}
Expand Down
35 changes: 23 additions & 12 deletions base_layer/core/src/chain_storage/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ pub enum ChainStorageError {
},
#[error("The requested {entity} was not found via {field}:{value} in the database")]
ValueNotFound {
entity: String,
field: String,
entity: &'static str,
field: &'static str,
value: String,
},
#[error("MMR error: {source}")]
Expand Down Expand Up @@ -132,9 +132,9 @@ impl From<lmdb_zero::Error> for ChainStorageError {
use lmdb_zero::Error::*;
match err {
Code(c) if c == lmdb_zero::error::NOTFOUND => ChainStorageError::ValueNotFound {
entity: "LMDB".to_string(),
field: "unknown".to_string(),
value: "unknown".to_string(),
entity: "<unspecified entity>",
field: "<unknown>",
value: "<unknown>".to_string(),
},
_ => ChainStorageError::AccessError(err.to_string()),
}
Expand All @@ -156,21 +156,32 @@ impl<U> Optional<U> for Result<U, ChainStorageError> {
}

pub trait OrNotFound<U> {
fn or_not_found(self, entity: &str, field: &str, value: String) -> Result<U, ChainStorageError>;
fn or_not_found(self, entity: &'static str, field: &'static str, value: String) -> Result<U, ChainStorageError>;
}

impl<U> OrNotFound<U> for Result<Option<U>, ChainStorageError> {
fn or_not_found(self, entity: &str, field: &str, value: String) -> Result<U, ChainStorageError> {
fn or_not_found(self, entity: &'static str, field: &'static str, value: String) -> Result<U, ChainStorageError> {
match self {
Ok(inner) => match inner {
None => Err(ChainStorageError::ValueNotFound {
entity: entity.to_string(),
field: field.to_string(),
value,
}),
None => Err(ChainStorageError::ValueNotFound { entity, field, value }),
Some(v) => Ok(v),
},
Err(err) => Err(err),
}
}
}

impl<U> OrNotFound<U> for Result<U, lmdb_zero::Error> {
fn or_not_found(self, entity: &'static str, field: &'static str, value: String) -> Result<U, ChainStorageError> {
use lmdb_zero::Error::*;
match self {
Ok(v) => Ok(v),
Err(err) => match err {
Code(c) if c == lmdb_zero::error::NOTFOUND => {
Err(ChainStorageError::ValueNotFound { entity, field, value })
},
err => Err(err.into()),
},
}
}
}
17 changes: 13 additions & 4 deletions base_layer/core/src/chain_storage/lmdb_db/lmdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use crate::chain_storage::error::ChainStorageError;
use crate::chain_storage::{error::ChainStorageError, OrNotFound};
use lmdb_zero::{
del,
error::{self, LmdbResultExt},
Expand Down Expand Up @@ -141,9 +141,18 @@ where
}

/// Deletes the given key. An error is returned if the key does not exist
pub fn lmdb_delete<K>(txn: &WriteTransaction<'_>, db: &Database, key: &K) -> Result<(), ChainStorageError>
where K: AsLmdbBytes + ?Sized {
txn.access().del_key(&db, key)?;
pub fn lmdb_delete<K>(
txn: &WriteTransaction<'_>,
db: &Database,
key: &K,
table_name: &'static str,
) -> Result<(), ChainStorageError>
where
K: AsLmdbBytes + ?Sized,
{
txn.access()
.del_key(&db, key)
.or_not_found(table_name, "<unknown>", to_hex(key.as_lmdb_bytes()))?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.or_not_found(table_name, "<unknown>", to_hex(key.as_lmdb_bytes()))?;
.or_not_found(table_name, "key", to_hex(key.as_lmdb_bytes()))?;

Ok(())
}

Expand Down
Loading