From a249f7da7cec80a3d7df07fa7fcb294d0dfbda5e Mon Sep 17 00:00:00 2001 From: Hansie Odendaal Date: Thu, 19 Oct 2023 10:16:07 +0200 Subject: [PATCH 1/2] Fix erroneous warning message Fixed erroneous warning message about 'orphans_db' and 'orphan_header_accumulated_data_db' being out of sync --- base_layer/core/src/chain_storage/lmdb_db/lmdb_db.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/base_layer/core/src/chain_storage/lmdb_db/lmdb_db.rs b/base_layer/core/src/chain_storage/lmdb_db/lmdb_db.rs index dbe2c4facc..6577b75004 100644 --- a/base_layer/core/src/chain_storage/lmdb_db/lmdb_db.rs +++ b/base_layer/core/src/chain_storage/lmdb_db/lmdb_db.rs @@ -1147,12 +1147,18 @@ impl LMDBDatabase { parent_hash.to_hex() ); } - } else { + } else if lmdb_exists(txn, &self.orphans_db, parent_hash.as_slice())? != + lmdb_exists(txn, &self.orphan_header_accumulated_data_db, parent_hash.as_slice())? + { warn!( target: LOG_TARGET, - "'orphans_db' and 'orphan_header_accumulated_data_db' out of sync, missing parent hash '{}' entry", + "'orphans_db' ({}) and 'orphan_header_accumulated_data_db' ({}) out of sync, missing parent hash '{}' entry", + lmdb_exists(txn, &self.orphans_db, parent_hash.as_slice())?, + lmdb_exists(txn, &self.orphan_header_accumulated_data_db, parent_hash.as_slice())?, parent_hash.to_hex() ); + } else { + // Nothing here } } From 2b0013bad29f39cb46cb2934250ba536bcb4d03d Mon Sep 17 00:00:00 2001 From: Hansie Odendaal Date: Thu, 19 Oct 2023 12:29:14 +0200 Subject: [PATCH 2/2] review comments --- .../core/src/chain_storage/lmdb_db/lmdb_db.rs | 72 ++++++++++--------- 1 file changed, 40 insertions(+), 32 deletions(-) diff --git a/base_layer/core/src/chain_storage/lmdb_db/lmdb_db.rs b/base_layer/core/src/chain_storage/lmdb_db/lmdb_db.rs index 6577b75004..51fe57c1f1 100644 --- a/base_layer/core/src/chain_storage/lmdb_db/lmdb_db.rs +++ b/base_layer/core/src/chain_storage/lmdb_db/lmdb_db.rs @@ -1121,44 +1121,52 @@ impl LMDBDatabase { // Orphan is a tip hash if lmdb_exists(txn, &self.orphan_chain_tips_db, hash.as_slice())? { + // We get rid of the orphan tip lmdb_delete(txn, &self.orphan_chain_tips_db, hash.as_slice(), "orphan_chain_tips_db")?; - - // Parent becomes a tip hash - if lmdb_exists(txn, &self.orphans_db, parent_hash.as_slice())? && - lmdb_exists(txn, &self.orphan_header_accumulated_data_db, parent_hash.as_slice())? - { - let orphan_parent_accum: Option = - lmdb_get(txn, &self.orphan_header_accumulated_data_db, parent_hash.as_slice())?; - if let Some(val) = orphan_parent_accum { - lmdb_insert( - txn, - &self.orphan_chain_tips_db, - parent_hash.as_slice(), - &ChainTipData { - hash: parent_hash, - total_accumulated_difficulty: val.total_accumulated_difficulty, + // If an orphan parent exists, it must be promoted + match ( + lmdb_exists(txn, &self.orphans_db, parent_hash.as_slice())?, + lmdb_exists(txn, &self.orphan_header_accumulated_data_db, parent_hash.as_slice())?, + ) { + (true, true) => { + // Parent becomes a tip hash + let orphan_parent_accum: Option = + lmdb_get(txn, &self.orphan_header_accumulated_data_db, parent_hash.as_slice())?; + match orphan_parent_accum { + Some(val) => { + lmdb_insert( + txn, + &self.orphan_chain_tips_db, + parent_hash.as_slice(), + &ChainTipData { + hash: parent_hash, + total_accumulated_difficulty: val.total_accumulated_difficulty, + }, + "orphan_chain_tips_db", + )?; }, - "orphan_chain_tips_db", - )?; - } else { + None => { + warn!( + target: LOG_TARGET, + "Empty 'BlockHeaderAccumulatedData' for parent hash '{}'", + parent_hash.to_hex() + ); + }, + } + }, + (false, false) => { + // No entries, nothing here + }, + _ => { + // Some previous database operations were not atomic warn!( target: LOG_TARGET, - "Empty 'BlockHeaderAccumulatedData' for parent hash '{}'", + "'orphans_db' ({}) and 'orphan_header_accumulated_data_db' ({}) out of sync, missing parent hash '{}' entry", + lmdb_exists(txn, &self.orphans_db, parent_hash.as_slice())?, + lmdb_exists(txn, &self.orphan_header_accumulated_data_db, parent_hash.as_slice())?, parent_hash.to_hex() ); - } - } else if lmdb_exists(txn, &self.orphans_db, parent_hash.as_slice())? != - lmdb_exists(txn, &self.orphan_header_accumulated_data_db, parent_hash.as_slice())? - { - warn!( - target: LOG_TARGET, - "'orphans_db' ({}) and 'orphan_header_accumulated_data_db' ({}) out of sync, missing parent hash '{}' entry", - lmdb_exists(txn, &self.orphans_db, parent_hash.as_slice())?, - lmdb_exists(txn, &self.orphan_header_accumulated_data_db, parent_hash.as_slice())?, - parent_hash.to_hex() - ); - } else { - // Nothing here + }, } }