|
|
@ -6,6 +6,7 @@ defmodule Explorer.Chain.Import do |
|
|
|
import Ecto.Query, only: [from: 2, update: 2] |
|
|
|
import Ecto.Query, only: [from: 2, update: 2] |
|
|
|
|
|
|
|
|
|
|
|
alias Ecto.{Changeset, Multi} |
|
|
|
alias Ecto.{Changeset, Multi} |
|
|
|
|
|
|
|
alias Ecto.Adapters.SQL |
|
|
|
|
|
|
|
|
|
|
|
alias Explorer.Chain.{ |
|
|
|
alias Explorer.Chain.{ |
|
|
|
Address, |
|
|
|
Address, |
|
|
@ -378,12 +379,22 @@ defmodule Explorer.Chain.Import do |
|
|
|
%{Block => blocks_changes} -> |
|
|
|
%{Block => blocks_changes} -> |
|
|
|
timestamps = Map.fetch!(options, :timestamps) |
|
|
|
timestamps = Map.fetch!(options, :timestamps) |
|
|
|
blocks_timeout = options[:blocks][:timeout] || @insert_blocks_timeout |
|
|
|
blocks_timeout = options[:blocks][:timeout] || @insert_blocks_timeout |
|
|
|
|
|
|
|
where_forked = where_forked(blocks_changes) |
|
|
|
|
|
|
|
|
|
|
|
multi |
|
|
|
multi |
|
|
|
|
|
|
|
|> Multi.run(:derive_transaction_forks, fn _ -> |
|
|
|
|
|
|
|
derive_transaction_forks(%{ |
|
|
|
|
|
|
|
timeout: options[:transaction_forks][:timeout] || @insert_transaction_forks_timeout, |
|
|
|
|
|
|
|
timestamps: timestamps, |
|
|
|
|
|
|
|
where_forked: where_forked |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
end) |
|
|
|
|
|
|
|
# MUST be after `:derive_transaction_forks`, which depends on values in `transactions` table |
|
|
|
|> Multi.run(:fork_transactions, fn _ -> |
|
|
|
|> Multi.run(:fork_transactions, fn _ -> |
|
|
|
fork_transactions(blocks_changes, %{ |
|
|
|
fork_transactions(%{ |
|
|
|
timeout: options[:transactions][:timeout] || @insert_transactions_timeout, |
|
|
|
timeout: options[:transactions][:timeout] || @insert_transactions_timeout, |
|
|
|
timestamps: timestamps |
|
|
|
timestamps: timestamps, |
|
|
|
|
|
|
|
where_forked: where_forked |
|
|
|
}) |
|
|
|
}) |
|
|
|
end) |
|
|
|
end) |
|
|
|
|> Multi.run(:lose_consenus, fn _ -> |
|
|
|
|> Multi.run(:lose_consenus, fn _ -> |
|
|
@ -980,14 +991,9 @@ defmodule Explorer.Chain.Import do |
|
|
|
{:ok, inserted} |
|
|
|
{:ok, inserted} |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
defp fork_transactions(blocks_changes, %{ |
|
|
|
defp fork_transactions(%{timeout: timeout, timestamps: %{updated_at: updated_at}, where_forked: where_forked}) do |
|
|
|
timeout: timeout, |
|
|
|
|
|
|
|
timestamps: %{updated_at: updated_at} |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
when is_list(blocks_changes) do |
|
|
|
|
|
|
|
query = |
|
|
|
query = |
|
|
|
Transaction |
|
|
|
where_forked |
|
|
|
|> where_forked(blocks_changes) |
|
|
|
|
|
|
|
|> update( |
|
|
|
|> update( |
|
|
|
set: [ |
|
|
|
set: [ |
|
|
|
block_hash: nil, |
|
|
|
block_hash: nil, |
|
|
@ -1007,12 +1013,12 @@ defmodule Explorer.Chain.Import do |
|
|
|
{:ok, result} |
|
|
|
{:ok, result} |
|
|
|
rescue |
|
|
|
rescue |
|
|
|
postgrex_error in Postgrex.Error -> |
|
|
|
postgrex_error in Postgrex.Error -> |
|
|
|
{:error, %{exception: postgrex_error, blocks_changes: blocks_changes}} |
|
|
|
{:error, %{exception: postgrex_error}} |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
defp where_forked(query, blocks_changes) when is_list(blocks_changes) do |
|
|
|
defp where_forked(blocks_changes) when is_list(blocks_changes) do |
|
|
|
Enum.reduce(blocks_changes, query, fn %{consensus: consensus, hash: hash, number: number}, acc -> |
|
|
|
Enum.reduce(blocks_changes, Transaction, fn %{consensus: consensus, hash: hash, number: number}, acc -> |
|
|
|
case consensus do |
|
|
|
case consensus do |
|
|
|
false -> |
|
|
|
false -> |
|
|
|
from(transaction in acc, or_where: transaction.block_hash == ^hash and transaction.block_number == ^number) |
|
|
|
from(transaction in acc, or_where: transaction.block_hash == ^hash and transaction.block_number == ^number) |
|
|
@ -1023,6 +1029,42 @@ defmodule Explorer.Chain.Import do |
|
|
|
end) |
|
|
|
end) |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# sobelow_skip ["SQL.Query"] |
|
|
|
|
|
|
|
defp derive_transaction_forks(%{ |
|
|
|
|
|
|
|
timeout: timeout, |
|
|
|
|
|
|
|
timestamps: %{inserted_at: inserted_at, updated_at: updated_at}, |
|
|
|
|
|
|
|
where_forked: where_forked |
|
|
|
|
|
|
|
}) do |
|
|
|
|
|
|
|
query = |
|
|
|
|
|
|
|
from(transaction in where_forked, |
|
|
|
|
|
|
|
select: [ |
|
|
|
|
|
|
|
transaction.block_hash, |
|
|
|
|
|
|
|
transaction.index, |
|
|
|
|
|
|
|
transaction.hash, |
|
|
|
|
|
|
|
type(^inserted_at, transaction.inserted_at), |
|
|
|
|
|
|
|
type(^updated_at, transaction.updated_at) |
|
|
|
|
|
|
|
] |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{sql, parameters} = SQL.to_sql(:all, Repo, query) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{:ok, %Postgrex.Result{columns: ["uncle_hash", "hash"], command: :insert, rows: rows}} = |
|
|
|
|
|
|
|
SQL.query( |
|
|
|
|
|
|
|
Repo, |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
INSERT INTO transaction_forks (uncle_hash, index, hash, inserted_at, updated_at) |
|
|
|
|
|
|
|
#{sql} |
|
|
|
|
|
|
|
RETURNING uncle_hash, hash |
|
|
|
|
|
|
|
""", |
|
|
|
|
|
|
|
parameters, |
|
|
|
|
|
|
|
timeout: timeout |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
derived_transaction_forks = Enum.map(rows, fn [uncle_hash, hash] -> %{uncle_hash: uncle_hash, hash: hash} end) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{:ok, derived_transaction_forks} |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
defp lose_consensus(blocks_changes, %{timeout: timeout, timestamps: %{updated_at: updated_at}}) |
|
|
|
defp lose_consensus(blocks_changes, %{timeout: timeout, timestamps: %{updated_at: updated_at}}) |
|
|
|
when is_list(blocks_changes) do |
|
|
|
when is_list(blocks_changes) do |
|
|
|
ordered_block_number = |
|
|
|
ordered_block_number = |
|
|
|