Remove nonconsensus blocks from cache after internal transactions importing

Problem: after PR #2726 some blocks may lose consensus after internal transactions are imported.
The cache is not aware of this and may hold outdated values.

Solution: notify the cache to drop the blocks that lost consensus in such cases.
pull/2768/head
pasqu4le 5 years ago
parent 0a6aeec21b
commit f04052c4c4
No known key found for this signature in database
GPG Key ID: 8F3EE01F1DC90687
  1. 1
      CHANGELOG.md
  2. 18
      apps/explorer/lib/explorer/chain/cache/blocks.ex
  3. 1
      apps/explorer/lib/explorer/chain/import/runner/internal_transactions.ex
  4. 3
      apps/indexer/lib/indexer/fetcher/internal_transaction.ex

@ -15,6 +15,7 @@
- [#2642](https://github.com/poanetwork/blockscout/pull/2642) - add ERC721 coin instance page
### Fixes
- [#2768](https://github.com/poanetwork/blockscout/pull/2768) - Remove nonconsensus blocks from cache after internal transactions importing
- [#2753](https://github.com/poanetwork/blockscout/pull/2753) - fix nft token instance images
- [#2750](https://github.com/poanetwork/blockscout/pull/2750) - fixed contract buttons color for NFT token instance on each theme
- [#2746](https://github.com/poanetwork/blockscout/pull/2746) - fixed wrong alignment in logs decoded view

@ -20,4 +20,22 @@ defmodule Explorer.Chain.Cache.Blocks do
@type id :: non_neg_integer()
def element_to_id(%Block{number: number}), do: number
def drop_nonconsensus([]), do: :ok
def drop_nonconsensus(numbers) when is_list(numbers) do
ConCache.update(cache_name(), ids_list_key(), fn ids ->
nonconsensus = MapSet.new(numbers)
{lost_consensus, kept_consensus} = Enum.split_with(ids, &MapSet.member?(nonconsensus, &1))
# immediately delete the blocks that lost consensus
Enum.each(lost_consensus, &ConCache.delete(cache_name(), &1))
# ids_list is set to never expire
{:ok, %ConCache.Item{value: kept_consensus, ttl: :infinity}}
end)
end
def drop_nonconsensus(number) when not is_nil(number), do: drop_nonconsensus([number])
end

@ -241,6 +241,7 @@ defmodule Explorer.Chain.Import.Runner.InternalTransactions do
b in Block,
where: b.number in ^missing_transactions_block_numbers,
where: b.hash in ^block_hashes,
select: b.number,
# ShareLocks order already enforced by `internal_transactions_indexed_at_blocks` (see docs: sharelocks.md)
update: [set: [consensus: false, internal_transactions_indexed_at: nil]]
)

@ -14,7 +14,7 @@ defmodule Indexer.Fetcher.InternalTransaction do
alias Explorer.Chain
alias Explorer.Chain.{Block, Hash}
alias Explorer.Chain.Cache.Accounts
alias Explorer.Chain.Cache.{Accounts, Blocks}
alias Indexer.{BufferedTask, Tracer}
alias Indexer.Transform.Addresses
@ -220,6 +220,7 @@ defmodule Indexer.Fetcher.InternalTransaction do
case imports do
{:ok, imported} ->
Accounts.drop(imported[:addreses])
Blocks.drop_nonconsensus(imported[:remove_consensus_of_missing_transactions_blocks])
async_import_coin_balances(imported, %{
address_hash_to_fetched_balance_block_number: address_hash_to_block_number

Loading…
Cancel
Save