Skip to content

Commit

Permalink
Add back FindBatchContainingMessageIndex, was removed in the nitro la…
Browse files Browse the repository at this point in the history
…test head
  • Loading branch information
amsanghi committed Mar 28, 2024
1 parent 86fddb2 commit 17a9d02
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions staker/stateless_block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,39 @@ func GlobalStatePositionsAtCount(
return startPos, GlobalStatePosition{batch, posInBatch + 1}, nil
}

func FindBatchContainingMessageIndex(
tracker InboxTrackerInterface, pos arbutil.MessageIndex, high uint64,
) (uint64, error) {
var low uint64
// Iteration preconditions:
// - high >= low
// - msgCount(low - 1) <= pos implies low <= target
// - msgCount(high) > pos implies high >= target
// Therefore, if low == high, then low == high == target
for high > low {
// Due to integer rounding, mid >= low && mid < high
mid := (low + high) / 2
count, err := tracker.GetBatchMessageCount(mid)
if err != nil {
return 0, err
}
if count < pos {
// Must narrow as mid >= low, therefore mid + 1 > low, therefore newLow > oldLow
// Keeps low precondition as msgCount(mid) < pos
low = mid + 1
} else if count == pos {
return mid + 1, nil
} else if count == pos+1 || mid == low { // implied: count > pos
return mid, nil
} else { // implied: count > pos + 1
// Must narrow as mid < high, therefore newHigh < lowHigh
// Keeps high precondition as msgCount(mid) > pos
high = mid
}
}
return low, nil
}

type ValidationEntryStage uint32

const (
Expand Down

0 comments on commit 17a9d02

Please sign in to comment.