Add last_block_number realtime chain event

pull/3400/head
PoaMan 4 years ago
parent 4810f85612
commit f0f9986763
  1. 1
      CHANGELOG.md
  2. 2
      apps/explorer/lib/explorer/chain/events/publisher.ex
  3. 2
      apps/explorer/lib/explorer/chain/events/subscriber.ex
  4. 12
      apps/explorer/lib/explorer/staking/contract_state.ex
  5. 2
      apps/explorer/test/explorer/staking/contract_state_test.exs
  6. 2
      apps/indexer/lib/indexer/block/realtime/fetcher.ex

@ -15,6 +15,7 @@
### Fixes ### Fixes
- [#3408](https://github.com/poanetwork/blockscout/pull/3408) - Fix (total) difficulty display - [#3408](https://github.com/poanetwork/blockscout/pull/3408) - Fix (total) difficulty display
- [#3401](https://github.com/poanetwork/blockscout/pull/3401) - Fix procedure of marking internal transactions as failed - [#3401](https://github.com/poanetwork/blockscout/pull/3401) - Fix procedure of marking internal transactions as failed
- [#3400](https://github.com/poanetwork/blockscout/pull/3400) - Add :last_block_number realtime chain event
- [#3399](https://github.com/poanetwork/blockscout/pull/3399) - Fix Token transfers CSV export - [#3399](https://github.com/poanetwork/blockscout/pull/3399) - Fix Token transfers CSV export
- [#3396](https://github.com/poanetwork/blockscout/pull/3396) - Handle exchange rates request throttled - [#3396](https://github.com/poanetwork/blockscout/pull/3396) - Handle exchange rates request throttled
- [#3382](https://github.com/poanetwork/blockscout/pull/3382) - Check ets table exists for know tokens - [#3382](https://github.com/poanetwork/blockscout/pull/3382) - Check ets table exists for know tokens

@ -3,7 +3,7 @@ defmodule Explorer.Chain.Events.Publisher do
Publishes events related to the Chain context. Publishes events related to the Chain context.
""" """
@allowed_events ~w(addresses address_coin_balances address_token_balances blocks block_rewards internal_transactions token_transfers transactions contract_verification_result)a @allowed_events ~w(addresses address_coin_balances address_token_balances blocks block_rewards internal_transactions last_block_number token_transfers transactions contract_verification_result)a
def broadcast(_data, false), do: :ok def broadcast(_data, false), do: :ok

@ -3,7 +3,7 @@ defmodule Explorer.Chain.Events.Subscriber do
Subscribes to events related to the Chain context. Subscribes to events related to the Chain context.
""" """
@allowed_broadcast_events ~w(addresses address_coin_balances address_token_balances blocks block_rewards internal_transactions token_transfers transactions contract_verification_result)a @allowed_broadcast_events ~w(addresses address_coin_balances address_token_balances blocks block_rewards internal_transactions last_block_number token_transfers transactions contract_verification_result)a
@allowed_broadcast_types ~w(catchup realtime on_demand contract_verification_result)a @allowed_broadcast_types ~w(catchup realtime on_demand contract_verification_result)a

@ -68,7 +68,7 @@ defmodule Explorer.Staking.ContractState do
write_concurrency: true write_concurrency: true
]) ])
Subscriber.to(:blocks, :realtime) Subscriber.to(:last_block_number, :realtime)
staking_abi = abi("StakingAuRa") staking_abi = abi("StakingAuRa")
validator_set_abi = abi("ValidatorSetAuRa") validator_set_abi = abi("ValidatorSetAuRa")
@ -126,12 +126,10 @@ defmodule Explorer.Staking.ContractState do
end end
@doc "Handles new blocks and decides to fetch fresh chain info" @doc "Handles new blocks and decides to fetch fresh chain info"
def handle_info({:chain_event, :blocks, :realtime, blocks}, state) do def handle_info({:chain_event, :last_block_number, :realtime, block_number}, state) do
latest_block = Enum.max_by(blocks, & &1.number, fn -> %{number: 0} end) if block_number > state.seen_block do
fetch_state(state.contracts, state.abi, block_number)
if latest_block.number > state.seen_block do {:noreply, %{state | seen_block: block_number}}
fetch_state(state.contracts, state.abi, latest_block.number)
{:noreply, %{state | seen_block: latest_block.number}}
else else
{:noreply, state} {:noreply, state}
end end

@ -30,7 +30,7 @@ defmodule Explorer.Staking.ContractStateTest do
start_supervised!(ContractState) start_supervised!(ContractState)
Publisher.broadcast([{:blocks, [%Explorer.Chain.Block{number: 76}]}], :realtime) Publisher.broadcast([{:last_block_number, 76}], :realtime)
Process.sleep(500) Process.sleep(500)

@ -28,6 +28,7 @@ defmodule Indexer.Block.Realtime.Fetcher do
alias EthereumJSONRPC.{Blocks, FetchedBalances, Subscription} alias EthereumJSONRPC.{Blocks, FetchedBalances, Subscription}
alias Explorer.Chain alias Explorer.Chain
alias Explorer.Chain.Cache.Accounts alias Explorer.Chain.Cache.Accounts
alias Explorer.Chain.Events.Publisher
alias Explorer.Counters.AverageBlockTime alias Explorer.Counters.AverageBlockTime
alias Indexer.{Block, Tracer} alias Indexer.{Block, Tracer}
alias Indexer.Block.Realtime.TaskSupervisor alias Indexer.Block.Realtime.TaskSupervisor
@ -85,6 +86,7 @@ defmodule Indexer.Block.Realtime.Fetcher do
) )
when is_binary(quantity) do when is_binary(quantity) do
number = quantity_to_integer(quantity) number = quantity_to_integer(quantity)
Publisher.broadcast([{:last_block_number, number}], :realtime)
# Subscriptions don't support getting all the blocks and transactions data, # Subscriptions don't support getting all the blocks and transactions data,
# so we need to go back and get the full block # so we need to go back and get the full block
start_fetch_and_import(number, block_fetcher, previous_number, max_number_seen) start_fetch_and_import(number, block_fetcher, previous_number, max_number_seen)

Loading…
Cancel
Save