Fix off-by-1 errors in chunk_ranges

pull/411/head
Luke Imhoff 6 years ago
parent ff11541bde
commit 7a530f2b9e
  1. 26
      apps/indexer/lib/indexer/block_fetcher.ex

@ -276,22 +276,30 @@ defmodule Indexer.BlockFetcher do
@doc false
def chunk_ranges(ranges, size) do
Enum.flat_map(ranges, fn
first..last = range when last - first <= size ->
Enum.flat_map(ranges, fn range ->
count = Enum.count(range)
if count <= size do
[range]
else
first..last = range
first..last ->
first
|> Stream.iterate(&(&1 + size))
|> Enum.reduce_while([], fn
chunk_first, acc when chunk_first + size >= last ->
{:halt, [chunk_first..last | acc]}
|> Enum.reduce_while([], fn chunk_first, acc ->
next_chunk_first = chunk_first + size
full_chunk_last = next_chunk_first - 1
chunk_first, acc ->
chunk_last = chunk_first + size - 1
{:cont, [chunk_first..chunk_last | acc]}
{action, chunk_last} = if full_chunk_last >= last do
{:halt, last}
else
{:cont, full_chunk_last}
end
{action, [chunk_first..chunk_last | acc]}
end)
|> Enum.reverse()
end
end)
end

Loading…
Cancel
Save