Exclude pending transactions for unfetched internal transactions

Parity doesn't support asking for the internal transactions of pending
transactions.
pull/255/head
Luke Imhoff 7 years ago
parent 5d62977973
commit a0befb0b86
  1. 37
      apps/explorer/lib/explorer/chain.ex
  2. 4
      apps/explorer/lib/explorer/chain/transaction.ex
  3. 32
      apps/explorer/test/explorer/indexer/internal_transaction_fetcher_test.exs
  4. 2
      apps/explorer/test/support/factory.ex
  5. 2
      coveralls.json

@ -1109,7 +1109,34 @@ defmodule Explorer.Chain do
end end
@doc """ @doc """
Returns a stream of all transactions with unfetched internal transactions. Returns a stream of all collated transactions with unfetched internal transactions.
Only transactions that have been collated into a block are returned; pending transactions not in a block are filtered
out.
iex> pending = insert(:transaction)
iex> unfetched_collated =
...> :transaction |>
...> insert() |>
...> with_block()
iex> fetched_collated =
...> :transaction |>
...> insert() |>
...> with_block(internal_transactions_indexed_at: DateTime.utc_now())
iex> {:ok, hash_set} = Explorer.Chain.stream_transactions_with_unfetched_internal_transactions(
...> [:hash],
...> MapSet.new(),
...> fn %Explorer.Chain.Transaction{hash: hash}, acc ->
...> MapSet.put(acc, hash)
...> end
...> )
iex> pending.hash in hash_set
false
iex> unfetched_collated.hash in hash_set
true
iex> fetched_collated.hash in hash_set
false
""" """
@spec stream_transactions_with_unfetched_internal_transactions( @spec stream_transactions_with_unfetched_internal_transactions(
fields :: [ fields :: [
@ -1137,7 +1164,13 @@ defmodule Explorer.Chain do
def stream_transactions_with_unfetched_internal_transactions(fields, initial, reducer) when is_function(reducer, 2) do def stream_transactions_with_unfetched_internal_transactions(fields, initial, reducer) when is_function(reducer, 2) do
Repo.transaction( Repo.transaction(
fn -> fn ->
query = from(t in Transaction, where: is_nil(t.internal_transactions_indexed_at), select: ^fields) query =
from(
t in Transaction,
# exclude pending transactions
where: not is_nil(t.block_hash) and is_nil(t.internal_transactions_indexed_at),
select: ^fields
)
query query
|> Repo.stream(timeout: :infinity) |> Repo.stream(timeout: :infinity)

@ -7,8 +7,8 @@ defmodule Explorer.Chain.Transaction do
alias Explorer.Chain.{Address, Block, Data, Gas, Hash, InternalTransaction, Log, Wei} alias Explorer.Chain.{Address, Block, Data, Gas, Hash, InternalTransaction, Log, Wei}
alias Explorer.Chain.Transaction.Status alias Explorer.Chain.Transaction.Status
@optional_attrs ~w(block_hash block_number cumulative_gas_used from_address_hash gas_used index status @optional_attrs ~w(block_hash block_number cumulative_gas_used from_address_hash gas_used index
to_address_hash)a internal_transactions_indexed_at status to_address_hash)a
@required_attrs ~w(gas gas_price hash input nonce public_key r s standard_v v value)a @required_attrs ~w(gas gas_price hash input nonce public_key r s standard_v v value)a
@typedoc """ @typedoc """

@ -0,0 +1,32 @@
defmodule Explorer.Indexer.InternalTransactionFetcherTest do
use Explorer.DataCase, async: true
alias Explorer.Indexer.InternalTransactionFetcher
describe "init/2" do
test "does not buffer pending transactions" do
insert(:transaction)
assert InternalTransactionFetcher.init([], fn hash_string, acc -> [hash_string | acc] end) == []
end
test "buffers collated transactions with unfetched internal transactions" do
collated_unfetched_transaction =
:transaction
|> insert()
|> with_block()
assert InternalTransactionFetcher.init([], fn hash_string, acc -> [hash_string | acc] end) == [
to_string(collated_unfetched_transaction.hash)
]
end
test "does not buffer collated transactions with fetched internal transactions" do
:transaction
|> insert()
|> with_block(internal_transactions_indexed_at: DateTime.utc_now())
assert InternalTransactionFetcher.init([], fn hash_string, acc -> [hash_string | acc] end) == []
end
end
end

@ -95,6 +95,7 @@ defmodule Explorer.Factory do
cumulative_gas_used = collated_params[:cumulative_gas_used] || Enum.random(21_000..100_000) cumulative_gas_used = collated_params[:cumulative_gas_used] || Enum.random(21_000..100_000)
gas_used = collated_params[:gas_used] || Enum.random(21_000..100_000) gas_used = collated_params[:gas_used] || Enum.random(21_000..100_000)
internal_transactions_indexed_at = collated_params[:internal_transactions_indexed_at]
status = collated_params[:status] || Enum.random(0..1) status = collated_params[:status] || Enum.random(0..1)
transaction transaction
@ -104,6 +105,7 @@ defmodule Explorer.Factory do
cumulative_gas_used: cumulative_gas_used, cumulative_gas_used: cumulative_gas_used,
gas_used: gas_used, gas_used: gas_used,
index: next_transaction_index, index: next_transaction_index,
internal_transactions_indexed_at: internal_transactions_indexed_at,
status: status status: status
}) })
|> Repo.update!() |> Repo.update!()

@ -1,7 +1,7 @@
{ {
"coverage_options": { "coverage_options": {
"treat_no_relevant_lines_as_covered": true, "treat_no_relevant_lines_as_covered": true,
"minimum_coverage": 92.7 "minimum_coverage": 92.9
}, },
"terminal_options": { "terminal_options": {
"file_column_width": 120 "file_column_width": 120

Loading…
Cancel
Save