Skip to content

Commit 2cc3231

Browse files
committed
fix(core): restore simple CheckPoint::insert() implementation
Remove unnecessary push_with_index() helper and restore the clean implementation from master that uses iter::once().chain() with extend(). The complex manual index management was not needed - extend() correctly handles index assignment and skip pointer calculation automatically. Removes 60+ lines of unnecessary code while maintaining all functionality and performance.
1 parent 5f7abcc commit 2cc3231

File tree

1 file changed

+3
-63
lines changed

1 file changed

+3
-63
lines changed

crates/core/src/checkpoint.rs

Lines changed: 3 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -336,69 +336,9 @@ where
336336
cp = cp.prev().expect("will break before genesis block");
337337
};
338338

339-
// Rebuild the chain with proper indices
340-
let mut result = base.clone();
341-
let base_index = result.index();
342-
343-
// First insert the new block
344-
result = result
345-
.push_with_index(height, data, base_index + 1)
346-
.expect("height is valid");
347-
348-
// Then re-add all the tail blocks with updated indices
349-
let mut current_index = base_index + 2;
350-
for (h, d) in tail.into_iter().rev() {
351-
result = result
352-
.push_with_index(h, d, current_index)
353-
.expect("tail is in order");
354-
current_index += 1;
355-
}
356-
357-
result
358-
}
359-
360-
// Helper method to push with a specific index (internal use)
361-
fn push_with_index(self, height: u32, data: D, new_index: u32) -> Result<Self, Self> {
362-
if self.height() < height {
363-
// Calculate skip pointer
364-
let skip = if new_index >= CHECKPOINT_SKIP_INTERVAL
365-
&& new_index % CHECKPOINT_SKIP_INTERVAL == 0
366-
{
367-
// Navigate back CHECKPOINT_SKIP_INTERVAL checkpoints
368-
let target_index = new_index - CHECKPOINT_SKIP_INTERVAL;
369-
let mut current = Some(self.0.clone());
370-
loop {
371-
match current {
372-
Some(ref cp) if cp.index == target_index => break,
373-
Some(ref cp) if cp.index < target_index => {
374-
// We've gone too far back, skip pointer not available
375-
current = None;
376-
break;
377-
}
378-
Some(ref cp) => {
379-
current = cp.prev.clone();
380-
}
381-
None => break,
382-
}
383-
}
384-
current
385-
} else {
386-
None
387-
};
388-
389-
Ok(Self(Arc::new(CPInner {
390-
block_id: BlockId {
391-
height,
392-
hash: data.to_blockhash(),
393-
},
394-
data,
395-
prev: Some(self.0),
396-
skip,
397-
index: new_index,
398-
})))
399-
} else {
400-
Err(self)
401-
}
339+
// Rebuild the chain: base -> new block -> tail
340+
base.extend(core::iter::once((height, data)).chain(tail.into_iter().rev()))
341+
.expect("tail is in order")
402342
}
403343

404344
/// Puts another checkpoint onto the linked list representing the blockchain.

0 commit comments

Comments
 (0)