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]
end
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
{:halt, last}
else
{:cont, full_chunk_last}
end
defp chunk_range(first..last = range, _, size) do
sign = sign(range)
step = sign * size
{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))
|> Stream.iterate(&(&1 + step))
|> Enum.reduce_while([], fn chunk_first, acc ->
next_chunk_first = chunk_first - size
full_chunk_last = next_chunk_first + 1
next_chunk_first = chunk_first + step
full_chunk_last = next_chunk_first - sign
{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}
else
{:cont, full_chunk_last}
@ -326,6 +310,9 @@ defmodule Indexer.BlockFetcher do
|> Enum.reverse()
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
{: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)

Loading…
Cancel
Save