Skip to content

Commit e1f2ff4

Browse files
evanlinjinclaude
andcommitted
refactor(core): simplify CheckPoint::push skip pointer logic
- Use early return pattern for readability - Add `needs_skip_pointer` variable for clarity - Simplify traversal to straightforward step counting 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 2cc3231 commit e1f2ff4

File tree

1 file changed

+34
-42
lines changed

1 file changed

+34
-42
lines changed

crates/core/src/checkpoint.rs

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -346,49 +346,41 @@ where
346346
/// Returns an `Err(self)` if the block you are pushing on is not at a greater height that the
347347
/// one you are pushing on to.
348348
pub fn push(self, height: u32, data: D) -> Result<Self, Self> {
349-
if self.height() < height {
350-
let new_index = self.0.index + 1;
351-
352-
// Calculate skip pointer
353-
let skip = if new_index >= CHECKPOINT_SKIP_INTERVAL
354-
&& new_index % CHECKPOINT_SKIP_INTERVAL == 0
355-
{
356-
// Navigate back CHECKPOINT_SKIP_INTERVAL checkpoints
357-
let mut current = Some(self.0.clone());
358-
let mut steps = 0;
359-
loop {
360-
match current {
361-
Some(ref cp) if cp.index == new_index - CHECKPOINT_SKIP_INTERVAL => break,
362-
Some(ref cp) => {
363-
current = cp.prev.clone();
364-
steps += 1;
365-
// Safety check to avoid infinite loop
366-
if steps > CHECKPOINT_SKIP_INTERVAL {
367-
current = None;
368-
break;
369-
}
370-
}
371-
None => break,
372-
}
373-
}
374-
current
375-
} else {
376-
None
377-
};
378-
379-
Ok(Self(Arc::new(CPInner {
380-
block_id: BlockId {
381-
height,
382-
hash: data.to_blockhash(),
383-
},
384-
data,
385-
prev: Some(self.0),
386-
skip,
387-
index: new_index,
388-
})))
389-
} else {
390-
Err(self)
349+
if self.height() >= height {
350+
return Err(self);
391351
}
352+
353+
let new_index = self.0.index + 1;
354+
355+
// Skip pointers are added every CHECKPOINT_SKIP_INTERVAL (100) checkpoints
356+
// e.g., checkpoints at index 100, 200, 300, etc. have skip pointers
357+
let needs_skip_pointer =
358+
new_index >= CHECKPOINT_SKIP_INTERVAL && new_index % CHECKPOINT_SKIP_INTERVAL == 0;
359+
360+
let skip = if needs_skip_pointer {
361+
// Skip pointer points back CHECKPOINT_SKIP_INTERVAL positions
362+
// e.g., checkpoint at index 200 points to checkpoint at index 100
363+
// We walk back CHECKPOINT_SKIP_INTERVAL - 1 steps since we start from self (index new_index - 1)
364+
let mut current = self.0.clone();
365+
for _ in 0..(CHECKPOINT_SKIP_INTERVAL - 1) {
366+
// This is safe: if we're at index >= 100, we must have at least 99 predecessors
367+
current = current.prev.clone().expect("chain has enough checkpoints");
368+
}
369+
Some(current)
370+
} else {
371+
None
372+
};
373+
374+
Ok(Self(Arc::new(CPInner {
375+
block_id: BlockId {
376+
height,
377+
hash: data.to_blockhash(),
378+
},
379+
data,
380+
prev: Some(self.0),
381+
skip,
382+
index: new_index,
383+
})))
392384
}
393385
}
394386

0 commit comments

Comments
 (0)