Fix chunk_range/3 for descending ranges

pull/411/head
Luke Imhoff 6 years ago
parent 3a9dac846d
commit 9ee7ab64f5
  1. 26
      apps/indexer/lib/indexer/block_fetcher.ex

@ -288,16 +288,34 @@ defmodule Indexer.BlockFetcher do
[range]
end
defp chunk_range(range, _, size) do
first..last = range
defp chunk_range(first..last, _, size) when first < last do
first
|> Stream.iterate(&(&1 + size))
|> Enum.reduce_while([], fn chunk_first, acc ->
next_chunk_first = chunk_first + size
full_chunk_last = next_chunk_first - 1
{action, chunk_last} = if full_chunk_last >= last do
{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
defp chunk_range(first..last, _, size) when last < first do
first
|> Stream.iterate(&(&1 - size))
|> Enum.reduce_while([], fn chunk_first, acc ->
next_chunk_first = chunk_first - size
full_chunk_last = next_chunk_first + 1
{action, chunk_last} =
if full_chunk_last <= last do
{:halt, last}
else
{:cont, full_chunk_last}

Loading…
Cancel
Save