Address #1366 Avoid uint64 overflow in getBlockRange when skipStep is large
This commit is contained in:
parent
2152dc6136
commit
080609eee1
|
@ -543,16 +543,28 @@ func getBlockRange*(
|
||||||
|
|
||||||
let
|
let
|
||||||
runway = uint64(headSlot - startSlot)
|
runway = uint64(headSlot - startSlot)
|
||||||
skipStep = max(skipStep, 1) # Treat 0 step as 1
|
|
||||||
count = min(1'u64 + (runway div skipStep), requestedCount)
|
# This is the number of blocks that will follow the start block
|
||||||
endSlot = startSlot + count * skipStep
|
extraBlocks = min(runway div skipStep, requestedCount - 1)
|
||||||
|
|
||||||
|
# If `skipStep` is very large, `extraBlocks` should be 0 from
|
||||||
|
# the previous line, so `endSlot` will be equal to `startSlot`:
|
||||||
|
endSlot = startSlot + extraBlocks * skipStep
|
||||||
|
|
||||||
var
|
var
|
||||||
b = dag.head.atSlot(endSlot)
|
b = dag.head.atSlot(endSlot)
|
||||||
o = output.len
|
o = output.len
|
||||||
for i in 0..<count:
|
|
||||||
for j in 0..<skipStep:
|
# Process all blocks that follow the start block (may be zero blocks)
|
||||||
|
for i in 1..extraBlocks:
|
||||||
|
if b.blck.slot == b.slot:
|
||||||
|
dec o
|
||||||
|
output[o] = b.blck
|
||||||
|
for j in 1..skipStep:
|
||||||
b = b.parent
|
b = b.parent
|
||||||
|
|
||||||
|
# We should now be at the start block.
|
||||||
|
# Like any "block slot", it may be a missing/skipped block:
|
||||||
if b.blck.slot == b.slot:
|
if b.blck.slot == b.slot:
|
||||||
dec o
|
dec o
|
||||||
output[o] = b.blck
|
output[o] = b.blck
|
||||||
|
|
Loading…
Reference in New Issue