From e6a9b4090d2902db4ee0ab8c9c9a044bb3517fef Mon Sep 17 00:00:00 2001 From: SW van Heerden Date: Thu, 7 Dec 2023 12:32:43 +0200 Subject: [PATCH 1/2] hazop findings update version lock update --- Cargo.lock | 2 +- base_layer/chat_ffi/Cargo.toml | 2 +- .../src/chain_storage/blockchain_database.rs | 20 ++++++++++++------- base_layer/core/src/common/mod.rs | 2 +- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7048a0183c..95f82d651f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3048,7 +3048,7 @@ dependencies = [ [[package]] name = "minotari_chat_ffi" -version = "0.53.0-pre.0" +version = "1.0.0-pre.0" dependencies = [ "cbindgen", "chrono", diff --git a/base_layer/chat_ffi/Cargo.toml b/base_layer/chat_ffi/Cargo.toml index 4619c1644a..dcdd9155a9 100644 --- a/base_layer/chat_ffi/Cargo.toml +++ b/base_layer/chat_ffi/Cargo.toml @@ -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] diff --git a/base_layer/core/src/chain_storage/blockchain_database.rs b/base_layer/core/src/chain_storage/blockchain_database.rs index 69f7a4db2d..f66bbe0de6 100644 --- a/base_layer/core/src/chain_storage/blockchain_database.rs +++ b/base_layer/core/src/chain_storage/blockchain_database.rs @@ -81,7 +81,7 @@ use crate::{ Reorg, TargetDifficulties, }, - common::rolling_vec::RollingVec, + common::{rolling_vec::RollingVec, BanPeriod}, consensus::{ chain_strength_comparer::ChainStrengthComparer, ConsensusConstants, @@ -1855,14 +1855,14 @@ fn reorganize_chain( backend: &mut T, block_validator: &dyn CandidateBlockValidator, fork_hash: HashOutput, - chain: &VecDeque>, + new_chain_from_fork: &VecDeque>, consensus: &ConsensusManager, ) -> Result>, 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() @@ -1870,8 +1870,7 @@ fn reorganize_chain( .collect::>() .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); @@ -1884,8 +1883,15 @@ fn reorganize_chain( 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 j in (i + 1)..new_chain_from_fork.len() { + txn.delete_orphan(*new_chain_from_fork[j].hash()); + } + backend.write(txn)?; info!(target: LOG_TARGET, "Restoring previous chain after failed reorg."); restore_reorged_chain(backend, fork_hash, removed_blocks, consensus)?; diff --git a/base_layer/core/src/common/mod.rs b/base_layer/core/src/common/mod.rs index 6b3ffcdc63..9eefcc7b0b 100644 --- a/base_layer/core/src/common/mod.rs +++ b/base_layer/core/src/common/mod.rs @@ -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, From 9eb9157a134232529a7ced540f85b2bc986454ab Mon Sep 17 00:00:00 2001 From: SW van Heerden Date: Fri, 8 Dec 2023 13:08:38 +0200 Subject: [PATCH 2/2] clippy fixes --- .../core/src/chain_storage/blockchain_database.rs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/base_layer/core/src/chain_storage/blockchain_database.rs b/base_layer/core/src/chain_storage/blockchain_database.rs index f66bbe0de6..108e56bcd3 100644 --- a/base_layer/core/src/chain_storage/blockchain_database.rs +++ b/base_layer/core/src/chain_storage/blockchain_database.rs @@ -1888,8 +1888,8 @@ fn reorganize_chain( } // 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 j in (i + 1)..new_chain_from_fork.len() { - txn.delete_orphan(*new_chain_from_fork[j].hash()); + for block in new_chain_from_fork.iter().skip(i + 1) { + txn.delete_orphan(*block.hash()); } backend.write(txn)?; @@ -2285,13 +2285,6 @@ fn get_previous_timestamps( Ok(timestamps) } -// Discard the the orphan block from the orphan pool that corresponds to the provided block hash. -fn remove_orphan(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(