Remove pending transactions from DB which are dropped from the node

pull/3715/head
Viktor Baranov 4 years ago
parent b4c049abb1
commit c00dfbc4d9
  1. 2
      apps/explorer/lib/explorer/chain.ex
  2. 42
      apps/indexer/lib/indexer/pending_transactions_sanitizer.ex

@ -2759,7 +2759,7 @@ defmodule Explorer.Chain do
) )
query query
|> Repo.all() |> Repo.all(timeout: :infinity)
end end
@doc """ @doc """

@ -10,6 +10,7 @@ defmodule Indexer.PendingTransactionsSanitizer do
import EthereumJSONRPC, only: [json_rpc: 2, request: 1] import EthereumJSONRPC, only: [json_rpc: 2, request: 1]
alias Ecto.Changeset
alias Explorer.{Chain, Repo} alias Explorer.{Chain, Repo}
alias Explorer.Chain.Import.Runner.Blocks alias Explorer.Chain.Import.Runner.Blocks
@ -70,18 +71,32 @@ defmodule Indexer.PendingTransactionsSanitizer do
pending_tx_hash_str = "0x" <> Base.encode16(pending_tx.hash.bytes, case: :lower) pending_tx_hash_str = "0x" <> Base.encode16(pending_tx.hash.bytes, case: :lower)
with {:ok, result} <- with {:ok, result} <-
%{id: ind, method: "eth_getTransactionReceipt", params: [pending_tx_hash_str]} %{id: ind, method: "eth_getTransactionByHash", params: [pending_tx_hash_str]}
|> request() |> request()
|> json_rpc(json_rpc_named_arguments) do |> json_rpc(json_rpc_named_arguments) do
if result do if result do
block_hash = Map.get(result, "blockHash") block_hash = Map.get(result, "blockHash")
if block_hash do
Logger.debug( Logger.debug(
"Transaction #{pending_tx_hash_str} already included into the block #{block_hash}. We should invalidate consensus for it in order to re-fetch transactions", "Transaction with hash #{pending_tx_hash_str} already included into the block #{block_hash}. We should invalidate consensus for it in order to re-fetch transactions",
fetcher: :pending_transactions_to_refetch fetcher: :pending_transactions_to_refetch
) )
fetch_block_and_invalidate(block_hash) fetch_block_and_invalidate(block_hash)
else
Logger.debug(
"Transaction with hash #{pending_tx_hash_str} is still pending. Do nothing.",
fetcher: :pending_transactions_to_refetch
)
end
else
Logger.debug(
"Transaction with hash #{pending_tx_hash_str} doesn't exist in the node anymore. We should remove it from Blockscout DB.",
fetcher: :pending_transactions_to_refetch
)
fetch_pending_transaction_and_delete(pending_tx)
end end
end end
end) end)
@ -91,6 +106,29 @@ defmodule Indexer.PendingTransactionsSanitizer do
) )
end end
defp fetch_pending_transaction_and_delete(transaction) do
pending_tx_hash_str = "0x" <> Base.encode16(transaction.hash.bytes, case: :lower)
case transaction
|> Changeset.change()
|> Repo.delete() do
{:ok, _transaction} ->
Logger.debug(
"Transaction with hash #{pending_tx_hash_str} successfully deleted from Blockscout DB because it doesn't exist in the archive node anymore",
fetcher: :pending_transactions_to_refetch
)
{:error, changeset} ->
Logger.debug(
[
"Deletion of pending transaction with hash #{pending_tx_hash_str} from Blockscout DB failed",
inspect(changeset)
],
fetcher: :pending_transactions_to_refetch
)
end
end
defp fetch_block_and_invalidate(block_hash) do defp fetch_block_and_invalidate(block_hash) do
case Chain.fetch_block_by_hash(block_hash) do case Chain.fetch_block_by_hash(block_hash) do
%{number: number, consensus: consensus} -> %{number: number, consensus: consensus} ->

Loading…
Cancel
Save