Skip to content

Commit

Permalink
Merge pull request blockscout#4437 from blockscout/np-fix-pending-san…
Browse files Browse the repository at this point in the history
…itizer

Complete fix pending sanitizer
  • Loading branch information
vbaranov authored Jul 26, 2021
2 parents 066ec27 + d3d4247 commit 726e3ae
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- [#4353](https://github.com/blockscout/blockscout/pull/4353) - Added live-reload on the token holders page

### Fixes
- [#4437](https://github.com/blockscout/blockscout/pull/4437) - Fix `PendingTransactionsSanitizer` for non-consensus blocks
- [#4430](https://github.com/blockscout/blockscout/pull/4430) - Fix current token balance on-demand fetcher
- [#4429](https://github.com/blockscout/blockscout/pull/4429), [#4431](https://github.com/blockscout/blockscout/pull/4431) - Fix 500 response on `/tokens/{addressHash}/token-holders?type=JSON` when total supply is zero
- [#4419](https://github.com/blockscout/blockscout/pull/4419) - Order contracts in the search by inserted_at in descending order
Expand All @@ -21,7 +22,7 @@
- [#4398](https://github.com/blockscout/blockscout/pull/4398) - Speed up the transactions loading on the front-end
- [#4384](https://github.com/blockscout/blockscout/pull/4384) - Fix Elixir version in `.tool-versions`
- [#4382](https://github.com/blockscout/blockscout/pull/4382) - Replace awesomplete with autocomplete.js
- [#4371] - (https://github.com/blockscout/blockscout/pull/4371) - Place search outside of burger in mobile view
- [#4371](https://github.com/blockscout/blockscout/pull/4371) - Place search outside of burger in mobile view
- [#4355](https://github.com/blockscout/blockscout/pull/4355) - Do not redirect to 404 page with empty string in the search field


Expand Down
26 changes: 19 additions & 7 deletions apps/indexer/lib/indexer/pending_transactions_sanitizer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ defmodule Indexer.PendingTransactionsSanitizer do
require Logger

import EthereumJSONRPC, only: [json_rpc: 2, request: 1]
import EthereumJSONRPC.Receipt, only: [to_elixir: 1]

alias Ecto.Changeset
alias Explorer.{Chain, Repo}
alias Explorer.Chain.Hash.Full, as: Hash
alias Explorer.Chain.Import.Runner.Blocks
alias Explorer.Chain.Transaction

Expand Down Expand Up @@ -72,7 +74,7 @@ defmodule Indexer.PendingTransactionsSanitizer do
pending_tx_hash_str = "0x" <> Base.encode16(pending_tx.hash.bytes, case: :lower)

with {:ok, result} <-
%{id: ind, method: "eth_getTransactionByHash", params: [pending_tx_hash_str]}
%{id: ind, method: "eth_getTransactionReceipt", params: [pending_tx_hash_str]}
|> request()
|> json_rpc(json_rpc_named_arguments) do
if result do
Expand All @@ -84,7 +86,7 @@ defmodule Indexer.PendingTransactionsSanitizer do
fetcher: :pending_transactions_to_refetch
)

fetch_block_and_invalidate(block_hash, pending_tx)
fetch_block_and_invalidate(block_hash, pending_tx, result)
else
Logger.debug(
"Transaction with hash #{pending_tx_hash_str} is still pending. Do nothing.",
Expand Down Expand Up @@ -130,7 +132,7 @@ defmodule Indexer.PendingTransactionsSanitizer do
end
end

defp fetch_block_and_invalidate(block_hash, pending_tx) do
defp fetch_block_and_invalidate(block_hash, pending_tx, tx) do
case Chain.fetch_block_by_hash(block_hash) do
%{number: number, consensus: consensus} ->
Logger.debug(
Expand All @@ -140,7 +142,7 @@ defmodule Indexer.PendingTransactionsSanitizer do
fetcher: :pending_transactions_to_refetch
)

invalidate_block(number, block_hash, consensus, pending_tx)
invalidate_block(number, block_hash, consensus, pending_tx, tx)

_ ->
Logger.debug(
Expand All @@ -150,7 +152,7 @@ defmodule Indexer.PendingTransactionsSanitizer do
end
end

defp invalidate_block(block_number, block_hash, consensus, pending_tx) do
defp invalidate_block(block_number, block_hash, consensus, pending_tx, tx) do
if consensus do
opts = %{
timeout: 60_000,
Expand All @@ -159,15 +161,25 @@ defmodule Indexer.PendingTransactionsSanitizer do

Blocks.lose_consensus(Repo, [], [block_number], [], opts)
else
{:ok, hash} = Hash.cast(block_hash)
tx_info = to_elixir(tx)

changeset =
pending_tx
|> Transaction.changeset()
|> Changeset.put_change(:cumulative_gas_used, tx_info["cumulativeGasUsed"])
|> Changeset.put_change(:gas_used, tx_info["gasUsed"])
|> Changeset.put_change(:index, tx_info["transactionIndex"])
|> Changeset.put_change(:block_number, block_number)
|> Changeset.put_change(:block_hash, block_hash)
|> Changeset.put_change(:block_hash, hash)

Repo.update(changeset)

Logger.debug("Pending tx with hash #{pending_tx.hash} assigned to block ##{block_number}")
Logger.debug(
"Pending tx with hash #{"0x" <> Base.encode16(pending_tx.hash.bytes, case: :lower)} assigned to block ##{
block_number
} with hash #{block_hash}"
)
end
end
end

0 comments on commit 726e3ae

Please sign in to comment.