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

Loading…
Cancel
Save