Skip to content

Commit

Permalink
Iterative impl of explode_gap (#31)
Browse files Browse the repository at this point in the history
* Iterative impl of `explode_gap`

* Fmt

* Fix lint
  • Loading branch information
michaelsproul authored Jan 25, 2024
1 parent 74c03ed commit c7f570d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
14 changes: 9 additions & 5 deletions background_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,17 @@ def run(self):
def explode_gap(start_slot, end_slot, sprp):
next_boundary = (start_slot // sprp + 1) * sprp

if end_slot > next_boundary:
return [(start_slot, next_boundary)] + explode_gap(
next_boundary + 1, end_slot, sprp
)
else:
if end_slot <= next_boundary:
return [(start_slot, end_slot)]

gaps = []
while end_slot > next_boundary:
gaps.append((start_slot, next_boundary))
start_slot = next_boundary + 1
next_boundary += sprp

return gaps


def explode_gaps(gaps, sprp=2048):
"Divide sync gaps into manageable chunks aligned to Lighthouse's restore points"
Expand Down
35 changes: 35 additions & 0 deletions tests/test_explode_gaps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from background_tasks import explode_gap


def check_gaps(start_slot, end_slot, sprp):
result = explode_gap(start_slot, end_slot, sprp=sprp)

assert len(result) > 0

prev_end = None
for start, end in result:
assert start < end
assert start == start_slot or (start == prev_end + 1 and start % sprp == 1)
assert end == end_slot or (end < end_slot and end % sprp == 0)
prev_end = end


def test_explode_large_gap():
start_slot = 14273
end_slot = 7530327
sprp = 2048
check_gaps(start_slot, end_slot, sprp)


def test_explode_small_gap_unaligned():
start_slot = 1
end_slot = 10
sprp = 2048
check_gaps(start_slot, end_slot, sprp)


def test_explode_small_gap_aligned():
start_slot = 1
end_slot = 2048
sprp = 2048
check_gaps(start_slot, end_slot, sprp)

0 comments on commit c7f570d

Please sign in to comment.