diff --git a/CHANGELOG.md b/CHANGELOG.md index 85059f7a3e..1e776c9aa2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -68,6 +68,7 @@ - [#2777](https://github.com/poanetwork/blockscout/pull/2777) - Remove duplicate blocks from changes_list before import - [#2770](https://github.com/poanetwork/blockscout/pull/2770) - do not re-fetch token instances without uris - [#2769](https://github.com/poanetwork/blockscout/pull/2769) - optimize token token transfers query +- [#2768](https://github.com/poanetwork/blockscout/pull/2768) - Remove nonconsensus blocks from cache after internal transactions importing - [#2761](https://github.com/poanetwork/blockscout/pull/2761) - add indexes for token instances fetching queries - [#2767](https://github.com/poanetwork/blockscout/pull/2767) - fix websocket subscriptions with token instances - [#2765](https://github.com/poanetwork/blockscout/pull/2765) - fixed width issue for cards in mobile view for Transaction Details page diff --git a/apps/explorer/lib/explorer/chain/cache/blocks.ex b/apps/explorer/lib/explorer/chain/cache/blocks.ex index d2a5b47061..ca968fb56d 100644 --- a/apps/explorer/lib/explorer/chain/cache/blocks.ex +++ b/apps/explorer/lib/explorer/chain/cache/blocks.ex @@ -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(numbers) when is_nil(numbers) or numbers == [], 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 diff --git a/apps/explorer/lib/explorer/chain/import/runner/internal_transactions.ex b/apps/explorer/lib/explorer/chain/import/runner/internal_transactions.ex index b4704d382b..9d16328555 100644 --- a/apps/explorer/lib/explorer/chain/import/runner/internal_transactions.ex +++ b/apps/explorer/lib/explorer/chain/import/runner/internal_transactions.ex @@ -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]] ) diff --git a/apps/indexer/lib/indexer/fetcher/internal_transaction.ex b/apps/indexer/lib/indexer/fetcher/internal_transaction.ex index 5ad7c5d506..77ba3cd6ec 100644 --- a/apps/indexer/lib/indexer/fetcher/internal_transaction.ex +++ b/apps/indexer/lib/indexer/fetcher/internal_transaction.ex @@ -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