From e175a9bec92915167f8ee3233bfbd3df5237694a Mon Sep 17 00:00:00 2001 From: SW van Heerden Date: Tue, 7 Nov 2023 13:38:52 +0200 Subject: [PATCH 1/3] small nits --- .../base_node/sync/block_sync/synchronizer.rs | 4 ++++ .../src/chain_storage/blockchain_backend.rs | 6 +++--- .../src/chain_storage/blockchain_database.rs | 2 -- .../core/src/chain_storage/lmdb_db/lmdb_db.rs | 21 +++++++------------ .../core/src/test_helpers/blockchain.rs | 4 ++-- 5 files changed, 16 insertions(+), 21 deletions(-) diff --git a/base_layer/core/src/base_node/sync/block_sync/synchronizer.rs b/base_layer/core/src/base_node/sync/block_sync/synchronizer.rs index af56083a28..7ff60f6b09 100644 --- a/base_layer/core/src/base_node/sync/block_sync/synchronizer.rs +++ b/base_layer/core/src/base_node/sync/block_sync/synchronizer.rs @@ -356,6 +356,10 @@ impl<'a, B: BlockchainBackend + 'static> BlockSynchronizer<'a, B> { block.header().pow_algo(), block.block().body.to_counts_string(), ); + trace!( + target: LOG_TARGET, + "{}",block + ); let timer = Instant::now(); self.db diff --git a/base_layer/core/src/chain_storage/blockchain_backend.rs b/base_layer/core/src/chain_storage/blockchain_backend.rs index 875ba9b3a5..d291a136a6 100644 --- a/base_layer/core/src/chain_storage/blockchain_backend.rs +++ b/base_layer/core/src/chain_storage/blockchain_backend.rs @@ -94,11 +94,11 @@ pub trait BlockchainBackend: Send + Sync { spend_status_at_header: Option, ) -> Result, ChainStorageError>; - /// Fetch a specific output. Returns the output and the leaf index in the output MMR + /// Fetch a specific output. Returns the output fn fetch_output(&self, output_hash: &HashOutput) -> Result, ChainStorageError>; - /// Fetch a specific input. Returns the output and the leaf index in the output MMR - fn fetch_input(&self, input_hash: &HashOutput) -> Result, ChainStorageError>; + /// Fetch a specific input. Returns the input + fn fetch_input(&self, output_hash: &HashOutput) -> Result, ChainStorageError>; /// Returns the unspent TransactionOutput output that matches the given commitment if it exists in the current UTXO /// set, otherwise None is returned. diff --git a/base_layer/core/src/chain_storage/blockchain_database.rs b/base_layer/core/src/chain_storage/blockchain_database.rs index 20900719d0..328b9bf699 100644 --- a/base_layer/core/src/chain_storage/blockchain_database.rs +++ b/base_layer/core/src/chain_storage/blockchain_database.rs @@ -1565,8 +1565,6 @@ fn fetch_block(db: &T, height: u64, compact: bool) -> Resu }) .collect::, _>>()?; - // let inputs = db.fetch_inputs_in_block(&accumulated_data.hash)?; - let block = header .into_builder() .add_inputs(inputs) 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 43cd38cd7e..d61f705a81 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 @@ -620,22 +620,15 @@ impl LMDBDatabase { &self.utxo_commitment_index, input.commitment()?.as_bytes(), "utxo_commitment_index", - ) - .or_else(|err| match err { - // The commitment may not yet be included in the DB in the 0-conf transaction case - ChainStorageError::ValueNotFound { .. } => Ok(()), - _ => Err(err), - })?; - // do I need to look into changing this index to be input.hash -> header hash - // does the key need to include the mmr pos - // make index safe key + )?; + let hash = input.canonical_hash(); let output_hash = input.output_hash(); let key = InputKey::new(header_hash, &hash)?; lmdb_insert( txn, &self.deleted_txo_hash_to_header_index, - &output_hash.to_vec(), + output_hash.as_slice(), &(key.clone().convert_to_comp_key().to_vec()), "deleted_txo_hash_to_header_index", )?; @@ -912,7 +905,7 @@ impl LMDBDatabase { lmdb_delete( txn, &self.deleted_txo_hash_to_header_index, - &output_hash.to_vec(), + output_hash.as_slice(), "deleted_txo_hash_to_header_index", )?; if output_rows.iter().any(|r| r.hash == output_hash) { @@ -1884,10 +1877,10 @@ impl BlockchainBackend for LMDBDatabase { self.fetch_output_in_txn(&txn, output_hash.as_slice()) } - fn fetch_input(&self, input_hash: &HashOutput) -> Result, ChainStorageError> { - debug!(target: LOG_TARGET, "Fetch input: {}", input_hash.to_hex()); + fn fetch_input(&self, output_hash: &HashOutput) -> Result, ChainStorageError> { + debug!(target: LOG_TARGET, "Fetch input: {}", output_hash.to_hex()); let txn = self.read_transaction()?; - self.fetch_input_in_txn(&txn, input_hash.as_slice()) + self.fetch_input_in_txn(&txn, output_hash.as_slice()) } fn fetch_unspent_output_hash_by_commitment( diff --git a/base_layer/core/src/test_helpers/blockchain.rs b/base_layer/core/src/test_helpers/blockchain.rs index 9e10947acd..31e126ad1f 100644 --- a/base_layer/core/src/test_helpers/blockchain.rs +++ b/base_layer/core/src/test_helpers/blockchain.rs @@ -282,8 +282,8 @@ impl BlockchainBackend for TempDatabase { self.db.as_ref().unwrap().fetch_output(output_hash) } - fn fetch_input(&self, input_hash: &HashOutput) -> Result, ChainStorageError> { - self.db.as_ref().unwrap().fetch_input(input_hash) + fn fetch_input(&self, output_hash: &HashOutput) -> Result, ChainStorageError> { + self.db.as_ref().unwrap().fetch_input(output_hash) } fn fetch_unspent_output_hash_by_commitment( From 8bc052b3eeaa240df4bd671cae89585fb2a05aaa Mon Sep 17 00:00:00 2001 From: SW van Heerden Date: Tue, 7 Nov 2023 15:05:53 +0200 Subject: [PATCH 2/3] fixed key in lmdb db --- base_layer/core/src/chain_storage/lmdb_db/lmdb_db.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 d61f705a81..3eab3c2041 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 @@ -170,7 +170,7 @@ pub fn create_lmdb_database>( .add_database(LMDB_DB_UTXO_COMMITMENT_INDEX, flags) .add_database(LMDB_DB_UNIQUE_ID_INDEX, flags) .add_database(LMDB_DB_CONTRACT_ID_INDEX, flags) - .add_database(LMDB_DB_DELETED_TXO_HASH_TO_HEADER_INDEX, flags | db::INTEGERKEY) + .add_database(LMDB_DB_DELETED_TXO_HASH_TO_HEADER_INDEX, flags) .add_database(LMDB_DB_ORPHANS, flags) .add_database(LMDB_DB_ORPHAN_HEADER_ACCUMULATED_DATA, flags) .add_database(LMDB_DB_MONERO_SEED_HEIGHT, flags) From 7f95a18b4028a7e47a6ef5c8354f7630de02d473 Mon Sep 17 00:00:00 2001 From: Hansie Odendaal Date: Tue, 7 Nov 2023 15:38:15 +0200 Subject: [PATCH 3/3] logging --- .../core/src/chain_storage/lmdb_db/lmdb.rs | 39 ++++++++++++------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/base_layer/core/src/chain_storage/lmdb_db/lmdb.rs b/base_layer/core/src/chain_storage/lmdb_db/lmdb.rs index f091ac29ce..991b9bdef1 100644 --- a/base_layer/core/src/chain_storage/lmdb_db/lmdb.rs +++ b/base_layer/core/src/chain_storage/lmdb_db/lmdb.rs @@ -63,22 +63,35 @@ where V: Serialize + Debug, { let val_buf = serialize(val)?; - trace!(target: LOG_TARGET, "LMDB: {} bytes inserted", val_buf.len()); match txn.access().put(db, key, &val_buf, put::NOOVERWRITE) { - Ok(_) => Ok(()), - Err(lmdb_zero::Error::Code(lmdb_zero::error::KEYEXIST)) => Err(ChainStorageError::KeyExists { - table_name, - key: to_hex(key.as_lmdb_bytes()), - }), - Err(lmdb_zero::Error::Code(lmdb_zero::error::MAP_FULL)) => Err(ChainStorageError::DbResizeRequired), - Err(e) => { + Ok(_) => { + trace!( + target: LOG_TARGET, "Inserted {} bytes with key '{}' into '{}'", + val_buf.len(), to_hex(key.as_lmdb_bytes()), table_name + ); + Ok(()) + }, + err @ Err(lmdb_zero::Error::Code(lmdb_zero::error::KEYEXIST)) => { error!( - target: LOG_TARGET, - "Could not insert value into lmdb {} ({}/{:?}): {:?}", + target: LOG_TARGET, "Could not insert {} bytes with key '{}' into '{}' ({:?})", + val_buf.len(), to_hex(key.as_lmdb_bytes()), table_name, err + ); + Err(ChainStorageError::KeyExists { table_name, - to_hex(key.as_lmdb_bytes()), - val, - e, + key: to_hex(key.as_lmdb_bytes()), + }) + }, + err @ Err(lmdb_zero::Error::Code(lmdb_zero::error::MAP_FULL)) => { + error!( + target: LOG_TARGET, "Could not insert {} bytes with key '{}' into '{}' ({:?})", + val_buf.len(), to_hex(key.as_lmdb_bytes()), table_name, err + ); + Err(ChainStorageError::DbResizeRequired) + }, + Err(e) => { + error!( + target: LOG_TARGET, "Could not insert {} bytes with key '{}' into '{}' ({:?})", + val_buf.len(), to_hex(key.as_lmdb_bytes()), table_name, e ); Err(ChainStorageError::InsertError { table: table_name,