Skip to content

Commit

Permalink
stop double process of blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
SWvheerden committed Dec 13, 2024
1 parent 26085b4 commit 3d9efb3
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 @@ -219,17 +219,19 @@ impl P2Chain {

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 @@ -251,7 +253,7 @@ impl P2Chain {
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 3d9efb3

Please sign in to comment.