Skip to content

Commit

Permalink
fix prune_outputs_spent_at_hash
Browse files Browse the repository at this point in the history
  • Loading branch information
hansieodendaal committed Jan 26, 2024
1 parent b4d4a5b commit 7401d33
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
65 changes: 42 additions & 23 deletions base_layer/core/src/chain_storage/lmdb_db/lmdb_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ use crate::{
transactions::{
aggregated_body::AggregateBody,
transaction_components::{
OutputType,
SpentOutput,
TransactionInput,
TransactionKernel,
Expand Down Expand Up @@ -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<'_>,
Expand Down Expand Up @@ -1414,24 +1410,45 @@ impl LMDBDatabase {
let inputs =
lmdb_fetch_matching_after::<TransactionInputRowData>(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<u8>>(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(())
Expand All @@ -1445,24 +1462,26 @@ impl LMDBDatabase {
) -> Result<(), ChainStorageError> {
match lmdb_get::<_, Vec<u8>>(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())),
}
Expand Down

0 comments on commit 7401d33

Please sign in to comment.