From 7401d3314a23b5466278e85937fa162eb0b6534a Mon Sep 17 00:00:00 2001 From: Hansie Odendaal Date: Fri, 26 Jan 2024 14:07:15 +0200 Subject: [PATCH] fix prune_outputs_spent_at_hash --- .../states/horizon_state_sync.rs | 9 +-- .../core/src/chain_storage/lmdb_db/lmdb_db.rs | 65 ++++++++++++------- 2 files changed, 47 insertions(+), 27 deletions(-) diff --git a/base_layer/core/src/base_node/state_machine_service/states/horizon_state_sync.rs b/base_layer/core/src/base_node/state_machine_service/states/horizon_state_sync.rs index 05adb23e92..ef79a01a23 100644 --- a/base_layer/core/src/base_node/state_machine_service/states/horizon_state_sync.rs +++ b/base_layer/core/src/base_node/state_machine_service/states/horizon_state_sync.rs @@ -58,11 +58,12 @@ impl HorizonStateSync { }; let sync_peers = &mut self.sync_peers; - // TODO: Hansie // // Order sync peers according to accumulated difficulty - // sync_peers.sort_by(|a, b| - // b.claimed_chain_metadata().accumulated_difficulty().cmp(&a.claimed_chain_metadata(). - // accumulated_difficulty())); + sync_peers.sort_by(|a, b| { + b.claimed_chain_metadata() + .accumulated_difficulty() + .cmp(&a.claimed_chain_metadata().accumulated_difficulty()) + }); let remote_metadata = match sync_peers.first() { Some(peer) => peer.claimed_chain_metadata(), None => return StateEvent::FatalError("No sync peers".into()), 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 6316139efa..e26b4737d4 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 @@ -97,6 +97,7 @@ use crate::{ transactions::{ aggregated_body::AggregateBody, transaction_components::{ + OutputType, SpentOutput, TransactionInput, TransactionKernel, @@ -525,11 +526,6 @@ impl LMDBDatabase { ] } - fn prune_output(&self, txn: &WriteTransaction<'_>, key: OutputKey) -> Result<(), ChainStorageError> { - lmdb_delete(txn, &self.utxos_db, &key.convert_to_comp_key(), "utxos_db")?; - Ok(()) - } - fn insert_output( &self, txn: &WriteTransaction<'_>, @@ -1414,24 +1410,45 @@ impl LMDBDatabase { let inputs = lmdb_fetch_matching_after::(write_txn, &self.inputs_db, block_hash.as_slice())?; - for input in inputs { - if let Ok(val) = input.input.commitment() { - lmdb_delete( - write_txn, - &self.utxo_commitment_index, - val.as_bytes(), - "utxo_commitment_index", - )?; + for input_data in inputs { + let input = input_data.input; + // From 'utxo_commitment_index::utxo_commitment_index' + if let SpentOutput::OutputData { + features, commitment, .. + } = input.spent_output.clone() + { + if !matches!(features.output_type, OutputType::Burn) { + debug!(target: LOG_TARGET, "Pruning output from 'utxo_commitment_index': key '{}'", commitment.to_hex()); + lmdb_delete( + write_txn, + &self.utxo_commitment_index, + commitment.as_bytes(), + "utxo_commitment_index", + )?; + } } + // From 'utxos_db::utxos_db' + if let Some(key_bytes) = + lmdb_get::<_, Vec>(write_txn, &self.txos_hash_to_index_db, input.output_hash().as_slice())? + { + let mut buffer = [0u8; 32]; + buffer.copy_from_slice(&key_bytes[0..32]); + let key = OutputKey::new(&FixedHash::from(buffer), &input.output_hash())?; + debug!(target: LOG_TARGET, "Pruning output from 'utxos_db': key '{}'", key.0); + lmdb_delete(write_txn, &self.utxos_db, &key.convert_to_comp_key(), "utxos_db")?; + }; + // From 'txos_hash_to_index_db::utxos_db' + debug!( + target: LOG_TARGET, + "Pruning output from 'txos_hash_to_index_db': key '{}'", + input.output_hash().to_hex() + ); lmdb_delete( write_txn, &self.txos_hash_to_index_db, - input.hash.as_slice(), + input.output_hash().as_slice(), "utxos_db", )?; - let key = OutputKey::new(block_hash, &input.hash)?; - debug!(target: LOG_TARGET, "Pruning output: {:?}", key); - self.prune_output(write_txn, key)?; } Ok(()) @@ -1445,24 +1462,26 @@ impl LMDBDatabase { ) -> Result<(), ChainStorageError> { match lmdb_get::<_, Vec>(write_txn, &self.txos_hash_to_index_db, output_hash.as_slice())? { Some(key_bytes) => { - let mut buffer = [0u8; 32]; - buffer.copy_from_slice(&key_bytes[0..32]); - let key = OutputKey::new(&FixedHash::from(buffer), output_hash)?; - + debug!(target: LOG_TARGET, "Pruning output from 'utxo_commitment_index': key '{}'", commitment.to_hex()); lmdb_delete( write_txn, &self.utxo_commitment_index, commitment.as_bytes(), "utxo_commitment_index", )?; + debug!(target: LOG_TARGET, "Pruning output from 'txos_hash_to_index_db': key '{}'", output_hash.to_hex()); lmdb_delete( write_txn, &self.txos_hash_to_index_db, output_hash.as_slice(), "utxos_db", )?; - debug!(target: LOG_TARGET, "Pruning output: {:?}", key); - self.prune_output(write_txn, key)?; + + let mut buffer = [0u8; 32]; + buffer.copy_from_slice(&key_bytes[0..32]); + let key = OutputKey::new(&FixedHash::from(buffer), output_hash)?; + debug!(target: LOG_TARGET, "Pruning output from 'utxos_db': key '{}'", key.0); + lmdb_delete(write_txn, &self.utxos_db, &key.convert_to_comp_key(), "utxos_db")?; }, None => return Err(ChainStorageError::InvalidOperation("Output key not found".to_string())), }