Skip to content

Commit

Permalink
feat: stop double process of blocks (#227)
Browse files Browse the repository at this point in the history
Description
---
If there are competing chains, they might be orphans of each other. This
causes blocks to be verified more than once. This stops that from
happening
  • Loading branch information
SWvheerden authored Dec 17, 2024
1 parent 74128f6 commit 77bb3f5
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/sharechain/p2chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use std::{
collections::{HashMap, VecDeque},
collections::{HashMap, HashSet, VecDeque},
fmt,
fmt::{Display, Formatter},
ops::Deref,
sync::Arc,
};

use itertools::Itertools;
use log::{debug, error, info};
use log::*;
use tari_common_types::types::FixedHash;
use tari_core::proof_of_work::{lwma_diff::LinearWeightedMovingAverage, AccumulatedDifficulty};
use tari_utilities::hex::Hex;
Expand Down Expand Up @@ -218,17 +218,19 @@ impl<T: BlockCache> P2Chain<T> {

fn verify_chain(&mut self, new_block_height: u64, hash: FixedHash) -> Result<ChainAddResult, ShareChainError> {
let mut next_level = VecDeque::new();
let mut processed = HashSet::new();
next_level.push_back((new_block_height, hash));
let mut new_tip = ChainAddResult::default();
while let Some((next_height, next_hash)) = next_level.pop_front() {
match self.verify_chain_inner(next_height, next_hash) {
Ok((add_result, do_next_level)) => {
processed.insert(next_hash);
new_tip.combine(add_result);
if new_tip.missing_blocks.len() >= MAX_MISSING_PARENTS {
return Ok(new_tip);
}
for item in do_next_level {
if next_level.contains(&item) {
if next_level.contains(&item) || processed.contains(&item.1) {
continue;
}
// Don't get into an infinite loop
Expand All @@ -250,7 +252,7 @@ impl<T: BlockCache> P2Chain<T> {
new_block_height: u64,
hash: FixedHash,
) -> Result<(ChainAddResult, Vec<(u64, FixedHash)>), ShareChainError> {
info!(target: LOG_TARGET, "Trying to verify new block to add: {}:{}", new_block_height, &hash.to_hex()[0..8]);
trace!(target: LOG_TARGET, "Trying to verify new block to add: {}:{}", new_block_height, &hash.to_hex()[0..8]);
// we should validate what we can if a block is invalid, we should delete it.
let mut new_tip = ChainAddResult::default();
let block = self
Expand Down

0 comments on commit 77bb3f5

Please sign in to comment.