Skip to content

Commit

Permalink
feat: hazop findings (#6020)
Browse files Browse the repository at this point in the history
Description
---
Findings from hazop meeting
  • Loading branch information
SWvheerden authored Dec 8, 2023
1 parent 1193399 commit a68d0dd
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion base_layer/chat_ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "minotari_chat_ffi"
authors = ["The Tari Development Community"]
description = "Tari cryptocurrency chat C FFI bindings"
license = "BSD-3-Clause"
version = "0.53.0-pre.0"
version = "1.0.0-pre.0"
edition = "2018"

[dependencies]
Expand Down
27 changes: 13 additions & 14 deletions base_layer/core/src/chain_storage/blockchain_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ use crate::{
Reorg,
TargetDifficulties,
},
common::rolling_vec::RollingVec,
common::{rolling_vec::RollingVec, BanPeriod},
consensus::{
chain_strength_comparer::ChainStrengthComparer,
ConsensusConstants,
Expand Down Expand Up @@ -1855,23 +1855,22 @@ fn reorganize_chain<T: BlockchainBackend>(
backend: &mut T,
block_validator: &dyn CandidateBlockValidator<T>,
fork_hash: HashOutput,
chain: &VecDeque<Arc<ChainBlock>>,
new_chain_from_fork: &VecDeque<Arc<ChainBlock>>,
consensus: &ConsensusManager,
) -> Result<Vec<Arc<ChainBlock>>, ChainStorageError> {
let removed_blocks = rewind_to_hash(backend, fork_hash)?;
debug!(
target: LOG_TARGET,
"Validate and add {} chain block(s) from block {}. Rewound blocks: [{}]",
chain.len(),
new_chain_from_fork.len(),
fork_hash,
removed_blocks
.iter()
.map(|b| b.height().to_string())
.collect::<Vec<_>>()
.join(", ")
);

for block in chain {
for (i, block) in new_chain_from_fork.iter().enumerate() {
let mut txn = DbTransaction::new();
let block_hash = *block.hash();
txn.delete_orphan(block_hash);
Expand All @@ -1884,8 +1883,15 @@ fn reorganize_chain<T: BlockchainBackend>(
block_hash,
e
);
txn.insert_bad_block(block.header().hash(), block.header().height);
remove_orphan(backend, block_hash)?;
if e.get_ban_reason().is_some() && e.get_ban_reason().unwrap().ban_duration != BanPeriod::Short {
txn.insert_bad_block(block.header().hash(), block.header().height);
}
// We removed a block from the orphan chain, so the chain is now "broken", so we remove the rest of the
// remaining blocks as well.
for block in new_chain_from_fork.iter().skip(i + 1) {
txn.delete_orphan(*block.hash());
}
backend.write(txn)?;

info!(target: LOG_TARGET, "Restoring previous chain after failed reorg.");
restore_reorged_chain(backend, fork_hash, removed_blocks, consensus)?;
Expand Down Expand Up @@ -2276,13 +2282,6 @@ fn get_previous_timestamps<T: BlockchainBackend>(
Ok(timestamps)
}

// Discard the the orphan block from the orphan pool that corresponds to the provided block hash.
fn remove_orphan<T: BlockchainBackend>(db: &mut T, hash: HashOutput) -> Result<(), ChainStorageError> {
let mut txn = DbTransaction::new();
txn.delete_orphan(hash);
db.write(txn)
}

/// Gets all blocks ordered from the the block that connects (via prev_hash) to the main chain, to the orphan tip.
#[allow(clippy::ptr_arg)]
fn get_orphan_link_main_chain<T: BlockchainBackend>(
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub struct BanReason {
pub ban_duration: BanPeriod,
}

#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum BanPeriod {
Short,
Long,
Expand Down

0 comments on commit a68d0dd

Please sign in to comment.