diff --git a/apps/indexer/lib/indexer/block_fetcher.ex b/apps/indexer/lib/indexer/block_fetcher.ex index 726e2826dc..800cd8cc40 100644 --- a/apps/indexer/lib/indexer/block_fetcher.ex +++ b/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 + + {action, chunk_last} = if full_chunk_last >= last do + {:halt, last} + else + {:cont, full_chunk_last} + end - chunk_first, acc -> - chunk_last = chunk_first + size - 1 - {:cont, [chunk_first..chunk_last | acc]} + {action, [chunk_first..chunk_last | acc]} end) |> Enum.reverse() + end end) end