|
| 1 | +import enum |
| 2 | +from typing import Tuple |
| 3 | + |
| 4 | +from eth_typing import BlockNumber |
| 5 | + |
| 6 | +from eth.exceptions import GapTrackingCorrupted |
| 7 | +from eth.typing import BlockRange |
| 8 | + |
| 9 | + |
| 10 | +class GapChange(enum.Enum): |
| 11 | + |
| 12 | + NoChange = enum.auto() |
| 13 | + NewGap = enum.auto() |
| 14 | + GapSplit = enum.auto() |
| 15 | + GapShrink = enum.auto() |
| 16 | + TailWrite = enum.auto() |
| 17 | + |
| 18 | + |
| 19 | +GapInfo = Tuple[GapChange, Tuple[BlockRange, ...]] |
| 20 | + |
| 21 | + |
| 22 | +def calculate_gaps(newly_persisted: BlockNumber, base_gaps: Tuple[BlockRange, ...]) -> GapInfo: |
| 23 | + |
| 24 | + # If we have a fresh chain, our highest missing number can only be 1 |
| 25 | + highest_missing_number = 1 if base_gaps == () else base_gaps[-1][0] |
| 26 | + |
| 27 | + if newly_persisted == highest_missing_number: |
| 28 | + # This is adding a consecutive header at the very tail |
| 29 | + new_last_marker = (newly_persisted + 1, -1) |
| 30 | + new_gaps = base_gaps[:-1] + (new_last_marker,) |
| 31 | + gap_change = GapChange.TailWrite |
| 32 | + elif newly_persisted > highest_missing_number: |
| 33 | + # We are creating a gap in the chain |
| 34 | + gap_end = newly_persisted - 1 |
| 35 | + new_tail = ((highest_missing_number, gap_end), (newly_persisted + 1, -1),) |
| 36 | + new_gaps = base_gaps[:-1] + new_tail |
| 37 | + gap_change = GapChange.NewGap |
| 38 | + elif newly_persisted < highest_missing_number: |
| 39 | + # We are patching a gap which may either shrink an existing gap or divide it |
| 40 | + matching_gaps = [ |
| 41 | + (index, pair) for index, pair in enumerate(base_gaps) |
| 42 | + if newly_persisted >= pair[0] and newly_persisted <= pair[1] |
| 43 | + ] |
| 44 | + |
| 45 | + if len(matching_gaps) > 1: |
| 46 | + raise GapTrackingCorrupted( |
| 47 | + "Corrupted chain gap tracking", |
| 48 | + f"No {newly_persisted} appears to be missing in multiple gaps", |
| 49 | + f"1st gap goes from {matching_gaps[0][1][0]} to {matching_gaps[0][1][1]}" |
| 50 | + f"2nd gap goes from {matching_gaps[1][1][0]} to {matching_gaps[1][1][1]}" |
| 51 | + ) |
| 52 | + elif len(matching_gaps) == 0: |
| 53 | + # Looks like we are just overwriting an existing header. |
| 54 | + return GapChange.NoChange, base_gaps |
| 55 | + elif len(matching_gaps) == 1: |
| 56 | + gap_index, gap = matching_gaps[0] |
| 57 | + if newly_persisted == gap[0] and newly_persisted == gap[1]: |
| 58 | + updated_center: Tuple[Tuple[int, int], ...] = () |
| 59 | + gap_change = GapChange.GapShrink |
| 60 | + elif newly_persisted == gap[0]: |
| 61 | + # we are shrinking the gap at the start |
| 62 | + updated_center = ((gap[0] + 1, gap[1],),) |
| 63 | + gap_change = GapChange.GapShrink |
| 64 | + elif newly_persisted == gap[1]: |
| 65 | + # we are shrinking the gap at the tail |
| 66 | + updated_center = ((gap[0], gap[1] - 1,),) |
| 67 | + gap_change = GapChange.GapShrink |
| 68 | + else: |
| 69 | + # we are dividing the gap |
| 70 | + first_new_gap = (gap[0], newly_persisted - 1) |
| 71 | + second_new_gap = (newly_persisted + 1, gap[1]) |
| 72 | + updated_center = (first_new_gap, second_new_gap,) |
| 73 | + gap_change = GapChange.GapSplit |
| 74 | + |
| 75 | + before_gap = base_gaps[:gap_index] |
| 76 | + after_gap = base_gaps[gap_index + 1:] |
| 77 | + new_gaps = before_gap + updated_center + after_gap |
| 78 | + |
| 79 | + else: |
| 80 | + raise Exception("Invariant") |
| 81 | + |
| 82 | + return gap_change, new_gaps |
0 commit comments