Skip to content

Commit 4c10014

Browse files
author
Ludo Galabru
committed
fix: change forking behavior
1 parent d1c83e7 commit 4c10014

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

components/chainhook-event-observer/src/indexer/stacks/blocks_pool.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use std::collections::{hash_map::Entry, BTreeMap, BTreeSet, HashMap, HashSet};
1313

1414
pub struct StacksBlockPool {
1515
canonical_fork_id: usize,
16+
number_of_blocks_since_last_reorg: u16,
1617
orphans: BTreeSet<BlockIdentifier>,
1718
block_store: HashMap<BlockIdentifier, StacksBlockData>,
1819
forks: BTreeMap<usize, ChainSegment>,
@@ -28,6 +29,7 @@ impl StacksBlockPool {
2829
forks.insert(0, ChainSegment::new());
2930
StacksBlockPool {
3031
canonical_fork_id: 0,
32+
number_of_blocks_since_last_reorg: 0,
3133
block_store: HashMap::new(),
3234
orphans: BTreeSet::new(),
3335
forks,
@@ -214,7 +216,9 @@ impl StacksBlockPool {
214216
_ => return Ok(None),
215217
};
216218

217-
self.collect_and_prune_confirmed_blocks(&mut chain_event, ctx);
219+
if self.number_of_blocks_since_last_reorg > 4 {
220+
self.collect_and_prune_confirmed_blocks(&mut chain_event, ctx);
221+
}
218222

219223
Ok(Some(chain_event))
220224
}
@@ -844,6 +848,8 @@ impl StacksBlockPool {
844848
if let Ok(divergence) = canonical_segment.try_identify_divergence(other_segment, false, ctx)
845849
{
846850
if divergence.block_ids_to_rollback.is_empty() {
851+
self.number_of_blocks_since_last_reorg =
852+
self.number_of_blocks_since_last_reorg.saturating_add(1);
847853
let mut new_blocks = vec![];
848854
for i in 0..divergence.block_ids_to_apply.len() {
849855
let block_identifier = &divergence.block_ids_to_apply[i];
@@ -893,6 +899,7 @@ impl StacksBlockPool {
893899
},
894900
));
895901
} else {
902+
self.number_of_blocks_since_last_reorg = 0;
896903
let blocks_to_rollback = divergence
897904
.block_ids_to_rollback
898905
.iter()

0 commit comments

Comments
 (0)