Skip to content
Permalink
Browse files

save first trace fields to transaction

  • Loading branch information
ayrat555 committed Dec 3, 2019
1 parent cd27d9e commit 72d4b129a7208ec69b7d6f05b1a896e8f8dc3d57
@@ -44,6 +44,9 @@ defmodule Explorer.Chain.Import.Runner.InternalTransactions do
|> Map.put_new(:timeout, @timeout)
|> Map.put(:timestamps, timestamps)

changes_list_without_first_traces = Enum.reject(changes_list, fn changes -> changes[:index] == 0 end)
first_traces = Enum.filter(changes_list, fn changes -> changes[:index] == 0 end)

transactions_timeout = options[Runner.Transactions.option_key()][:timeout] || Runner.Transactions.timeout()

update_transactions_options = %{timeout: transactions_timeout, timestamps: timestamps}
@@ -54,11 +57,14 @@ defmodule Explorer.Chain.Import.Runner.InternalTransactions do
acquire_transactions(repo, changes_list)
end)
|> Multi.run(:internal_transactions, fn repo, %{acquire_transactions: transactions} ->
insert(repo, changes_list, transactions, insert_options)
insert(repo, changes_list_without_first_traces, transactions, insert_options)
end)
|> Multi.run(:internal_transactions_indexed_at_transactions, fn repo, %{acquire_transactions: transactions} ->
update_transactions(repo, transactions, update_transactions_options)
end)
|> Multi.run(:set_first_trace_fields, fn repo, %{acquire_transactions: transactions} ->
set_first_trace_fields(repo, transactions, first_traces)
end)
|> Multi.run(
:remove_consensus_of_missing_transactions_blocks,
fn repo, %{internal_transactions: inserted} = results_map ->
@@ -224,6 +230,43 @@ defmodule Explorer.Chain.Import.Runner.InternalTransactions do
end
end

defp set_first_trace_fields(repo, transactions, first_traces) do
params =
Enum.map(first_traces, fn first_trace ->
%{
first_trace_gas_used: first_trace.gas_used,
first_trace_output: first_trace.output,
hash: first_trace.transaction_hash
}
end)

valid_params =
transactions
|> Enum.map(fn transaction ->
found_params =
Enum.find(params, fn param ->
param.hash == transaction.hash
end)

{transaction, found_params}
end)
|> Enum.reject(fn {tx, params} -> is_nil(params) || is_nil(tx.block_hash) end)
|> Enum.map(fn {_tx, params} -> params end)

try do
{_transaction_count, result} =
repo.insert_all(Transaction, valid_params,
on_conflict: {:replace, [:first_trace_gas_used, :first_trace_output]},
conflict_target: :hash
)

{:ok, result}
rescue
postgrex_error in Postgrex.Error ->
{:error, %{exception: postgrex_error, first_traces: first_traces}}
end
end

# If not using Parity this is not relevant
defp remove_consensus_of_missing_transactions_blocks(_, [], _, _), do: {:ok, []}

@@ -151,6 +151,8 @@ defmodule Explorer.Chain.Transaction do
hash: Hash.t(),
index: transaction_index | nil,
input: Data.t(),
first_trace_output: Data.t() | nil,
first_trace_gas_used: Gas.t() | nil,
internal_transactions: %Ecto.Association.NotLoaded{} | [InternalTransaction.t()],
internal_transactions_indexed_at: DateTime.t(),
logs: %Ecto.Association.NotLoaded{} | [Log.t()],
@@ -194,6 +196,8 @@ defmodule Explorer.Chain.Transaction do
field(:gas, :decimal)
field(:gas_price, Wei)
field(:gas_used, :decimal)
field(:first_trace_gas_used, :decimal)
field(:first_trace_output, Data)
field(:index, :integer)
field(:internal_transactions_indexed_at, :utc_datetime_usec)
field(:created_contract_code_indexed_at, :utc_datetime_usec)
@@ -390,6 +394,11 @@ defmodule Explorer.Chain.Transaction do
|> unique_constraint(:hash)
end

def first_trace_changeset(%__MODULE__{} = transaction, attrs \\ %{}) do
transaction
|> cast(attrs, [:first_trace_gas_used, :first_trace_output])
end

def preload_token_transfers(query, address_hash) do
token_transfers_query =
from(
@@ -0,0 +1,10 @@
defmodule Explorer.Repo.Migrations.AddFirstTraceFieldsToTransaction do
use Ecto.Migration

def change do
alter table(:transactions) do
add(:first_trace_gas_used, :numeric, precision: 100, null: true)
add(:first_trace_output, :bytea, null: true)
end
end
end

0 comments on commit 72d4b12

Please sign in to comment.
You can’t perform that action at this time.