Unify chunk_range/3 when count > size

pull/411/head
Luke Imhoff 6 years ago
parent 9ee7ab64f5
commit 2ca6ac9fa3
  1. 33
      apps/indexer/lib/indexer/block_fetcher.ex

@ -288,34 +288,18 @@ defmodule Indexer.BlockFetcher do
[range] [range]
end end
defp chunk_range(first..last, _, size) when first < last do defp chunk_range(first..last = range, _, size) do
first sign = sign(range)
|> Stream.iterate(&(&1 + size)) step = sign * 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}
end
{action, [chunk_first..chunk_last | acc]}
end)
|> Enum.reverse()
end
defp chunk_range(first..last, _, size) when last < first do
first first
|> Stream.iterate(&(&1 - size)) |> Stream.iterate(&(&1 + step))
|> Enum.reduce_while([], fn chunk_first, acc -> |> Enum.reduce_while([], fn chunk_first, acc ->
next_chunk_first = chunk_first - size next_chunk_first = chunk_first + step
full_chunk_last = next_chunk_first + 1 full_chunk_last = next_chunk_first - sign
{action, chunk_last} = {action, chunk_last} =
if full_chunk_last <= last do if (sign > 0 and full_chunk_last >= last) or (sign < 0 and full_chunk_last <= last) do
{:halt, last} {:halt, last}
else else
{:cont, full_chunk_last} {:cont, full_chunk_last}
@ -326,6 +310,9 @@ defmodule Indexer.BlockFetcher do
|> Enum.reverse() |> Enum.reverse()
end end
defp sign(first..last) when first <= last, do: 1
defp sign(_first.._last), do: -1
defp realtime_task(%{json_rpc_named_arguments: json_rpc_named_arguments} = state) do defp realtime_task(%{json_rpc_named_arguments: json_rpc_named_arguments} = state) do
{:ok, latest_block_number} = EthereumJSONRPC.fetch_block_number_by_tag("latest", json_rpc_named_arguments) {:ok, latest_block_number} = EthereumJSONRPC.fetch_block_number_by_tag("latest", json_rpc_named_arguments)
{:ok, seq} = Sequence.start_link(first: latest_block_number, step: 2) {:ok, seq} = Sequence.start_link(first: latest_block_number, step: 2)

Loading…
Cancel
Save