diff --git a/Cargo.lock b/Cargo.lock index 903098a434..47a637a2cc 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 219768d91d..fffa20a275 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 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)?; @@ -2276,13 +2282,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( 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,