Merge pull request #1330 from poanetwork/ab-return-periodic-polling-to-realtime-fetcher

[WIP] return polling to realtime fetcher
pull/1333/head
Luke Imhoff 6 years ago committed by GitHub
commit d3bdcb8285
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 45
      apps/indexer/lib/indexer/block/realtime/fetcher.ex

@ -18,6 +18,8 @@ defmodule Indexer.Block.Realtime.Fetcher do
alias Indexer.{AddressExtraction, Block, TokenBalances, Tracer} alias Indexer.{AddressExtraction, Block, TokenBalances, Tracer}
alias Indexer.Block.Realtime.{ConsensusEnsurer, TaskSupervisor} alias Indexer.Block.Realtime.{ConsensusEnsurer, TaskSupervisor}
@polling_period 2_000
@behaviour Block.Fetcher @behaviour Block.Fetcher
@enforce_keys ~w(block_fetcher)a @enforce_keys ~w(block_fetcher)a
@ -77,13 +79,54 @@ defmodule Indexer.Block.Realtime.Fetcher do
new_max_number = new_max_number(number, max_number_seen) new_max_number = new_max_number(number, max_number_seen)
{:noreply, %{state | previous_number: number, max_number_seen: new_max_number}} schedule_polling()
{:noreply,
%{
state
| previous_number: number,
max_number_seen: new_max_number
}}
end
@impl GenServer
def handle_info(
:poll_latest_block_number,
%__MODULE__{
block_fetcher: %Block.Fetcher{json_rpc_named_arguments: json_rpc_named_arguments} = block_fetcher,
previous_number: previous_number,
max_number_seen: max_number_seen
} = state
) do
{number, new_max_number} =
case EthereumJSONRPC.fetch_block_number_by_tag("latest", json_rpc_named_arguments) do
{:ok, number} ->
start_fetch_and_import(number, block_fetcher, previous_number, max_number_seen)
{number, new_max_number(number, max_number_seen)}
{:error, _} ->
{previous_number, max_number_seen}
end
schedule_polling()
{:noreply,
%{
state
| previous_number: number,
max_number_seen: new_max_number
}}
end end
defp new_max_number(number, nil), do: number defp new_max_number(number, nil), do: number
defp new_max_number(number, max_number_seen), do: max(number, max_number_seen) defp new_max_number(number, max_number_seen), do: max(number, max_number_seen)
defp schedule_polling do
Process.send_after(self(), :poll_latest_block_number, @polling_period)
end
@import_options ~w(address_hash_to_fetched_balance_block_number)a @import_options ~w(address_hash_to_fetched_balance_block_number)a
@impl Block.Fetcher @impl Block.Fetcher

Loading…
Cancel
Save