Skip to content

Commit

Permalink
handle edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
sdbondi committed Nov 26, 2021
1 parent 3a48a44 commit f532271
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl<'a, B: BlockchainBackend + 'static> HorizonStateSynchronization<'a, B> {
let new_prune_height = cmp::min(local_metadata.height_of_longest_chain(), self.horizon_sync_height);
if local_metadata.pruned_height() < new_prune_height {
debug!(target: LOG_TARGET, "Pruning block chain to height {}", new_prune_height);
db.prune_to_height(new_prune_height + 1).await?;
db.prune_to_height(new_prune_height).await?;
}

self.full_bitmap = Some(db.fetch_deleted_bitmap_at_tip().await?.into_bitmap());
Expand Down Expand Up @@ -637,7 +637,8 @@ impl<'a, B: BlockchainBackend + 'static> HorizonStateSynchronization<'a, B> {
header.accumulated_data().total_accumulated_difficulty,
metadata.best_block().clone(),
)
.set_pruned_height(header.height(), calc_kernel_sum, calc_utxo_sum)
.set_pruned_height(header.height())
.set_horizon_data(calc_kernel_sum, calc_utxo_sum)
.commit()
.await?;

Expand Down
6 changes: 5 additions & 1 deletion base_layer/core/src/base_node/sync/rpc/sync_utxos_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ where B: BlockchainBackend + 'static
include_pruned_utxos,
include_deleted_bitmaps
);
while current_header.height <= end_header.height {
loop {
let timer = Instant::now();
let current_header_hash = current_header.hash();

Expand Down Expand Up @@ -223,6 +223,10 @@ where B: BlockchainBackend + 'static
);

prev_utxo_mmr_size = current_header.output_mmr_size;
if current_header.height + 1 > end_header.height {
break;
}

current_header = self
.db
.fetch_header(current_header.height + 1)
Expand Down
9 changes: 7 additions & 2 deletions base_layer/core/src/chain_storage/async_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,13 @@ impl<'a, B: BlockchainBackend + 'static> AsyncDbTransaction<'a, B> {
self
}

pub fn set_pruned_height(&mut self, height: u64, kernel_sum: Commitment, utxo_sum: Commitment) -> &mut Self {
self.transaction.set_pruned_height(height, kernel_sum, utxo_sum);
pub fn set_pruned_height(&mut self, height: u64) -> &mut Self {
self.transaction.set_pruned_height(height);
self
}

pub fn set_horizon_data(&mut self, kernel_sum: Commitment, utxo_sum: Commitment) -> &mut Self {
self.transaction.set_horizon_data(kernel_sum, utxo_sum);
self
}

Expand Down
5 changes: 4 additions & 1 deletion base_layer/core/src/chain_storage/blockchain_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ where B: BlockchainBackend
kernel_sum: Some(kernel_sum.clone()),
..Default::default()
});
txn.set_pruned_height(0, kernel_sum, utxo_sum);
txn.set_pruned_height(0);
txn.set_horizon_data(kernel_sum, utxo_sum);
blockchain_db.write(txn)?;
blockchain_db.store_pruning_horizon(config.pruning_horizon)?;
}
Expand Down Expand Up @@ -2137,6 +2138,8 @@ fn prune_to_height<T: BlockchainBackend>(db: &mut T, target_horizon_height: u64)
txn.delete_all_inputs_in_block(header.hash().clone());
}

txn.set_pruned_height(target_horizon_height);

db.write(txn)?;
Ok(())
}
Expand Down
20 changes: 13 additions & 7 deletions base_layer/core/src/chain_storage/db_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use std::{
sync::Arc,
};

use crate::chain_storage::HorizonData;
use croaring::Bitmap;
use tari_common_types::types::{BlockHash, Commitment, HashOutput};
use tari_crypto::tari_utilities::{
Expand Down Expand Up @@ -241,11 +242,14 @@ impl DbTransaction {
self
}

pub fn set_pruned_height(&mut self, height: u64, kernel_sum: Commitment, utxo_sum: Commitment) -> &mut Self {
self.operations.push(WriteOperation::SetPrunedHeight {
height,
kernel_sum,
utxo_sum,
pub fn set_pruned_height(&mut self, height: u64) -> &mut Self {
self.operations.push(WriteOperation::SetPrunedHeight { height });
self
}

pub fn set_horizon_data(&mut self, kernel_sum: Commitment, utxo_sum: Commitment) -> &mut Self {
self.operations.push(WriteOperation::SetHorizonData {
horizon_data: HorizonData::new(kernel_sum, utxo_sum),
});
self
}
Expand Down Expand Up @@ -320,8 +324,9 @@ pub enum WriteOperation {
SetPruningHorizonConfig(u64),
SetPrunedHeight {
height: u64,
kernel_sum: Commitment,
utxo_sum: Commitment,
},
SetHorizonData {
horizon_data: HorizonData,
},
}

Expand Down Expand Up @@ -409,6 +414,7 @@ impl fmt::Display for WriteOperation {
SetPrunedHeight { height, .. } => write!(f, "Set pruned height to {}", height),
DeleteHeader(height) => write!(f, "Delete header at height: {}", height),
DeleteOrphan(hash) => write!(f, "Delete orphan with hash: {}", hash.to_hex()),
SetHorizonData { .. } => write!(f, "Set horizon data"),
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions base_layer/core/src/chain_storage/lmdb_db/lmdb_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,20 +383,18 @@ impl LMDBDatabase {
MetadataValue::PruningHorizon(*pruning_horizon),
)?;
},
SetPrunedHeight {
height,
kernel_sum,
utxo_sum,
} => {
SetPrunedHeight { height } => {
self.set_metadata(
&write_txn,
MetadataKey::PrunedHeight,
MetadataValue::PrunedHeight(*height),
)?;
},
SetHorizonData { horizon_data } => {
self.set_metadata(
&write_txn,
MetadataKey::HorizonData,
MetadataValue::HorizonData(HorizonData::new(kernel_sum.clone(), utxo_sum.clone())),
MetadataValue::HorizonData(horizon_data.clone()),
)?;
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ mod test {
)
.unwrap(),
);
assert!(tx_output3.verify_range_proof(&factories.range_proof).is_ok());
tx_output3.verify_range_proof(&factories.range_proof).unwrap_err();
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/tests/chain_storage_tests/chain_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1718,7 +1718,7 @@ fn pruned_mode_cleanup_and_fetch_block() {
let _block5 = append_block(&store, &block4, vec![], &consensus_manager, 1.into()).unwrap();

let metadata = store.get_chain_metadata().unwrap();
assert_eq!(metadata.pruned_height(), 1);
assert_eq!(metadata.pruned_height(), 2);
assert_eq!(metadata.height_of_longest_chain(), 5);
assert_eq!(metadata.pruning_horizon(), 3);
}
Expand Down

0 comments on commit f532271

Please sign in to comment.