From d95a217f75b02313b9acc1afa7475be1e46a83cb Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Thu, 21 Feb 2019 15:50:57 +0300 Subject: [PATCH 1/9] add temporary module to fix failed internal transactions --- .../temporary/failed_created_addresses.ex | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 apps/indexer/lib/indexer/temporary/failed_created_addresses.ex diff --git a/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex b/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex new file mode 100644 index 0000000000..3514ec1430 --- /dev/null +++ b/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex @@ -0,0 +1,42 @@ +defmodule Indexer.Temporary.FailedCreatedAddresses do + @moduledoc """ + Temporary module to fix internal transactions and their created transactions if a parent transaction has failed. + """ + + import Ecto.Query + + alias Explorer.Chain.{InternalTransaction, Transaction} + alias Explorer.Repo + + def run(json_rpc_named_arguments) do + query = + from(it in InternalTransaction, + left_join: t in assoc(it, :transaction), + where: t.error == 0 and not is_nil(it.created_contract_address_hash), + preload: :transaction + ) + + query + |> Repo.all() + |> Enum.each(fn internal_transaction -> + internal_transaction + |> code_entry() + |> Indexer.Code.Fetcher.run(json_rpc_named_arguments) + + internal_transaction.transaction_index + |> transaction_entry() + |> Indexer.InternalTransaction.Fetcher.run(json_rpc_named_arguments) + end) + end + + def code_entry(%InternalTransaction{ + block_number: block_number, + created_contract_address_hash: %{bytes: created_contract_bytes} + }) do + [{block_number, created_contract_bytes}] + end + + def transaction_entry(%Transaction{hash: %{bytes: bytes}, index: index, block_number: block_number}) do + [{block_number, bytes, index}] + end +end From c59512502393b61fe0a468852dbc7d529c7bb9f6 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Fri, 22 Feb 2019 13:24:56 +0300 Subject: [PATCH 2/9] fix query, add test --- .../temporary/failed_created_addresses.ex | 7 +- .../failed_created_addresses_test.exs | 91 +++++++++++++++++++ 2 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 apps/indexer/test/indexer/temporary/failed_created_addresses_test.exs diff --git a/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex b/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex index 3514ec1430..ebed35ddfa 100644 --- a/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex +++ b/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex @@ -11,8 +11,9 @@ defmodule Indexer.Temporary.FailedCreatedAddresses do def run(json_rpc_named_arguments) do query = from(it in InternalTransaction, - left_join: t in assoc(it, :transaction), - where: t.error == 0 and not is_nil(it.created_contract_address_hash), + left_join: t in Transaction, + on: it.transaction_hash == t.hash, + where: t.status == ^0 and not is_nil(it.created_contract_address_hash), preload: :transaction ) @@ -33,7 +34,7 @@ defmodule Indexer.Temporary.FailedCreatedAddresses do block_number: block_number, created_contract_address_hash: %{bytes: created_contract_bytes} }) do - [{block_number, created_contract_bytes}] + [{block_number, created_contract_bytes, <<>>}] end def transaction_entry(%Transaction{hash: %{bytes: bytes}, index: index, block_number: block_number}) do diff --git a/apps/indexer/test/indexer/temporary/failed_created_addresses_test.exs b/apps/indexer/test/indexer/temporary/failed_created_addresses_test.exs new file mode 100644 index 0000000000..4880cc3e05 --- /dev/null +++ b/apps/indexer/test/indexer/temporary/failed_created_addresses_test.exs @@ -0,0 +1,91 @@ +defmodule Indexer.Temporary.FailedCreatedAddressesTest do + use Explorer.DataCase, async: false + use EthereumJSONRPC.Case, async: false + + import Mox + + alias Explorer.Repo + alias Explorer.Chain.InternalTransaction + alias Indexer.Temporary.FailedCreatedAddresses + + @moduletag capture_log: true + + setup :set_mox_global + + setup :verify_on_exit! + + describe "run/1" do + test "updates failed replaced transactions", %{json_rpc_named_arguments: json_rpc_named_arguments} do + transaction = :transaction |> insert(status: 0, error: "Reverted") |> with_block() + address = insert(:address) + + internal_transaction = + insert(:internal_transaction, + block_number: transaction.block_number, + transaction: transaction, + index: 0, + created_contract_address_hash: address.hash + ) + + if json_rpc_named_arguments[:transport] == EthereumJSONRPC.Mox do + EthereumJSONRPC.Mox + |> expect(:json_rpc, fn _json, _options -> + {:ok, [%{id: 0, jsonrpc: "2.0", result: "0x"}]} + end) + |> expect(:json_rpc, fn [%{id: id, method: "eth_getBalance", params: [_address, _block_quantity]}], _options -> + {:ok, [%{id: id, result: "0x0"}]} + end) + |> expect(:json_rpc, fn _json, _options -> + {:ok, + [ + %{ + id: 0, + jsonrpc: "2.0", + result: %{ + "output" => "0x", + "stateDiff" => nil, + "trace" => [ + %{ + "action" => %{ + "callType" => "call", + "from" => "0xc73add416e2119d20ce80e0904fc1877e33ef246", + "gas" => "0x13388", + "input" => "0xc793bf97", + "to" => "0x2d07e106b5d280e4ccc2d10deee62441c91d4340", + "value" => "0x0" + }, + "error" => "Reverted", + "subtraces" => 1, + "traceAddress" => [], + "type" => "call" + }, + %{ + "action" => %{ + "from" => "0x2d07e106b5d280e4ccc2d10deee62441c91d4340", + "gas" => "0xb2ab", + "init" => + "0x608060405234801561001057600080fd5b5060d38061001f6000396000f3fe6080604052600436106038577c010000000000000000000000000000000000000000000000000000000060003504633ccfd60b8114604f575b3360009081526020819052604090208025434019055005b348015605a57600080fd5b5060616063565b005b33600081815260208190526040808220805490839055905190929183156108fc02918491818181858888f1935050505015801560a3573d6000803e3d6000fd5b505056fea165627a7a72305820e9a226f249def650de957dd8b4127b85a3049d6bfa818cadc4e2d3c44b6a53530029", + "value" => "0x0" + }, + "result" => %{ + "address" => "0xf4a5afe28b91cf928c2568805cfbb36d477f0b75", + "code" => + "0x6080604052600436106038577c010000000000000000000000000000000000000000000000000000000060003504633ccfd60b8114604f575b336000908152602081905260409020805434019055005b348015605a57600080fd5b5060616063565b005b33600081815260208190526040808220805490839055905190929183156108fc02918491818181858888f1935050505015801560a3573d6000803e3d6000fd5b505056fea165627a7a72305820e9a226f249def650de957dd8b4127b85a3049d6bfa818cadc4e2d3c44b6a53530029", + "gasUsed" => "0xa535" + }, + "subtraces" => 0, + "traceAddress" => [0], + "type" => "create" + } + ], + "vmTrace" => nil + } + } + ]} + end) + end + + FailedCreatedAddresses.run(json_rpc_named_arguments) + end + end +end From a92df4cbf5b7c1b840310ddb86df96f6899845e1 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Fri, 22 Feb 2019 15:13:57 +0300 Subject: [PATCH 3/9] fix query --- .../explorer/chain/import/runner/addresses.ex | 2 +- .../temporary/failed_created_addresses.ex | 2 +- .../failed_created_addresses_test.exs | 53 +++++++++++++++++-- 3 files changed, 50 insertions(+), 7 deletions(-) diff --git a/apps/explorer/lib/explorer/chain/import/runner/addresses.ex b/apps/explorer/lib/explorer/chain/import/runner/addresses.ex index 23530a74e7..8a3ab2eb02 100644 --- a/apps/explorer/lib/explorer/chain/import/runner/addresses.ex +++ b/apps/explorer/lib/explorer/chain/import/runner/addresses.ex @@ -87,7 +87,7 @@ defmodule Explorer.Chain.Import.Runner.Addresses do from(address in Address, update: [ set: [ - contract_code: fragment("COALESCE(?, EXCLUDED.contract_code)", address.contract_code), + contract_code: fragment("COALESCE(EXCLUDED.contract_code, ?)", address.contract_code), # ARGMAX on two columns fetched_coin_balance: fragment( diff --git a/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex b/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex index ebed35ddfa..00b7a45960 100644 --- a/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex +++ b/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex @@ -24,7 +24,7 @@ defmodule Indexer.Temporary.FailedCreatedAddresses do |> code_entry() |> Indexer.Code.Fetcher.run(json_rpc_named_arguments) - internal_transaction.transaction_index + internal_transaction.transaction |> transaction_entry() |> Indexer.InternalTransaction.Fetcher.run(json_rpc_named_arguments) end) diff --git a/apps/indexer/test/indexer/temporary/failed_created_addresses_test.exs b/apps/indexer/test/indexer/temporary/failed_created_addresses_test.exs index 4880cc3e05..ac4591dd0b 100644 --- a/apps/indexer/test/indexer/temporary/failed_created_addresses_test.exs +++ b/apps/indexer/test/indexer/temporary/failed_created_addresses_test.exs @@ -4,9 +4,12 @@ defmodule Indexer.Temporary.FailedCreatedAddressesTest do import Mox + import Ecto.Query + alias Explorer.Repo - alias Explorer.Chain.InternalTransaction + alias Explorer.Chain.{Address, InternalTransaction, Transaction} alias Indexer.Temporary.FailedCreatedAddresses + alias Indexer.CoinBalance @moduletag capture_log: true @@ -15,9 +18,27 @@ defmodule Indexer.Temporary.FailedCreatedAddressesTest do setup :verify_on_exit! describe "run/1" do + @tag :no_parity + @tag :no_geth test "updates failed replaced transactions", %{json_rpc_named_arguments: json_rpc_named_arguments} do - transaction = :transaction |> insert(status: 0, error: "Reverted") |> with_block() - address = insert(:address) + CoinBalance.Supervisor.Case.start_supervised!(json_rpc_named_arguments: json_rpc_named_arguments) + + block = insert(:block) + + transaction = + :transaction + |> insert( + status: 0, + error: "Reverted", + internal_transactions_indexed_at: DateTime.utc_now(), + block: block, + block_number: block.number, + cumulative_gas_used: 200, + gas_used: 100, + index: 0 + ) + + address = insert(:address, contract_code: "0x0102030405") internal_transaction = insert(:internal_transaction, @@ -63,8 +84,7 @@ defmodule Indexer.Temporary.FailedCreatedAddressesTest do "action" => %{ "from" => "0x2d07e106b5d280e4ccc2d10deee62441c91d4340", "gas" => "0xb2ab", - "init" => - "0x608060405234801561001057600080fd5b5060d38061001f6000396000f3fe6080604052600436106038577c010000000000000000000000000000000000000000000000000000000060003504633ccfd60b8114604f575b3360009081526020819052604090208025434019055005b348015605a57600080fd5b5060616063565b005b33600081815260208190526040808220805490839055905190929183156108fc02918491818181858888f1935050505015801560a3573d6000803e3d6000fd5b505056fea165627a7a72305820e9a226f249def650de957dd8b4127b85a3049d6bfa818cadc4e2d3c44b6a53530029", + "init" => "0x4bb278f3", "value" => "0x0" }, "result" => %{ @@ -86,6 +106,29 @@ defmodule Indexer.Temporary.FailedCreatedAddressesTest do end FailedCreatedAddresses.run(json_rpc_named_arguments) + + query = + from(t in Transaction, + where: t.hash == ^transaction.hash, + preload: [internal_transactions: :created_contract_address] + ) + + fetched_transaction = Repo.one(query) + + assert Enum.count(fetched_transaction.internal_transactions) == 2 + + assert Enum.all?(fetched_transaction.internal_transactions, fn it -> + it.error && is_nil(it.created_contract_address_hash) + end) + + fetched_address = + Repo.one( + from(a in Address, + where: a.hash == ^address.hash + ) + ) + + assert fetched_address.contract_code == %Explorer.Chain.Data{bytes: ""} end end end From 61c9781ee00624aec95b6f1643fd55c18ee54a76 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Mon, 25 Feb 2019 12:32:01 +0300 Subject: [PATCH 4/9] add supervisor to supervise failed created addresses fixer --- .../temporary/failed_created_addresses.ex | 46 ++++++++++++++----- .../failed_created_addresses/supervisor.ex | 38 +++++++++++++++ .../failed_created_addresses_test.exs | 10 +++- 3 files changed, 81 insertions(+), 13 deletions(-) create mode 100644 apps/indexer/lib/indexer/temporary/failed_created_addresses/supervisor.ex diff --git a/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex b/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex index 00b7a45960..d066f5ce28 100644 --- a/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex +++ b/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex @@ -3,10 +3,26 @@ defmodule Indexer.Temporary.FailedCreatedAddresses do Temporary module to fix internal transactions and their created transactions if a parent transaction has failed. """ + use GenServer + import Ecto.Query alias Explorer.Chain.{InternalTransaction, Transaction} alias Explorer.Repo + alias Indexer.Temporary.FailedCreatedAddresses.TaskSupervisor + + @task_options [max_concurrency: 3, timeout: 15_000] + + def start_link([json_rpc_named_arguments, gen_server_options]) do + GenServer.start_link(__MODULE__, json_rpc_named_arguments, gen_server_options) + end + + @impl GenServer + def init(json_rpc_named_arguments) do + run(json_rpc_named_arguments) + + {:ok, json_rpc_named_arguments} + end def run(json_rpc_named_arguments) do query = @@ -17,17 +33,25 @@ defmodule Indexer.Temporary.FailedCreatedAddresses do preload: :transaction ) - query - |> Repo.all() - |> Enum.each(fn internal_transaction -> - internal_transaction - |> code_entry() - |> Indexer.Code.Fetcher.run(json_rpc_named_arguments) - - internal_transaction.transaction - |> transaction_entry() - |> Indexer.InternalTransaction.Fetcher.run(json_rpc_named_arguments) - end) + found_internal_transactions = Repo.all(query) + + TaskSupervisor + |> Task.Supervisor.async_stream( + found_internal_transactions, + fn internal_transaction -> fix_internal_transaction(internal_transaction, json_rpc_named_arguments) end, + @task_options + ) + |> Enum.to_list() + end + + def fix_internal_transaction(internal_transaction, json_rpc_named_arguments) do + internal_transaction + |> code_entry() + |> Indexer.Code.Fetcher.run(json_rpc_named_arguments) + + internal_transaction.transaction + |> transaction_entry() + |> Indexer.InternalTransaction.Fetcher.run(json_rpc_named_arguments) end def code_entry(%InternalTransaction{ diff --git a/apps/indexer/lib/indexer/temporary/failed_created_addresses/supervisor.ex b/apps/indexer/lib/indexer/temporary/failed_created_addresses/supervisor.ex new file mode 100644 index 0000000000..1e92cde5ee --- /dev/null +++ b/apps/indexer/lib/indexer/temporary/failed_created_addresses/supervisor.ex @@ -0,0 +1,38 @@ +defmodule Indexer.Temporary.FailedCreatedAddresses.Supervisor do + @moduledoc """ + Supervises `Indexer.Temporary.FailedCreatedAddresses`. + """ + + use Supervisor + + alias Indexer.Temporary.FailedCreatedAddresses + + def child_spec([init_arguments]) do + child_spec([init_arguments, []]) + end + + def child_spec([_init_arguments, _gen_server_options] = start_link_arguments) do + default = %{ + id: __MODULE__, + start: {__MODULE__, :start_link, start_link_arguments}, + type: :supervisor + } + + Supervisor.child_spec(default, []) |> IO.inspect() + end + + def start_link(json_rpc_named_arguments, gen_server_options \\ []) do + Supervisor.start_link(__MODULE__, json_rpc_named_arguments, gen_server_options) + end + + @impl Supervisor + def init(json_rpc_named_arguments) do + Supervisor.init( + [ + {Task.Supervisor, name: Indexer.Temporary.FailedCreatedAddresses.TaskSupervisor}, + {FailedCreatedAddresses, [json_rpc_named_arguments, [name: FailedCreatedAddresses]]} + ], + strategy: :rest_for_one + ) + end +end diff --git a/apps/indexer/test/indexer/temporary/failed_created_addresses_test.exs b/apps/indexer/test/indexer/temporary/failed_created_addresses_test.exs index ac4591dd0b..693797bdab 100644 --- a/apps/indexer/test/indexer/temporary/failed_created_addresses_test.exs +++ b/apps/indexer/test/indexer/temporary/failed_created_addresses_test.exs @@ -8,7 +8,7 @@ defmodule Indexer.Temporary.FailedCreatedAddressesTest do alias Explorer.Repo alias Explorer.Chain.{Address, InternalTransaction, Transaction} - alias Indexer.Temporary.FailedCreatedAddresses + alias Indexer.Temporary.FailedCreatedAddresses.Supervisor alias Indexer.CoinBalance @moduletag capture_log: true @@ -105,7 +105,13 @@ defmodule Indexer.Temporary.FailedCreatedAddressesTest do end) end - FailedCreatedAddresses.run(json_rpc_named_arguments) + params = [json_rpc_named_arguments, [name: TestFailedCreatedAddresses]] + + params + |> Supervisor.child_spec() + |> ExUnit.Callbacks.start_supervised!() + + Process.sleep(3_000) query = from(t in Transaction, From f619016587a0562f0f514ee594e52db4030c9ace Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Mon, 25 Feb 2019 12:41:30 +0300 Subject: [PATCH 5/9] fix credeo, add query timeout --- .../indexer/temporary/failed_created_addresses.ex | 3 ++- .../failed_created_addresses/supervisor.ex | 2 +- .../temporary/failed_created_addresses_test.exs | 15 +++++++-------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex b/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex index d066f5ce28..a7da5ae847 100644 --- a/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex +++ b/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex @@ -12,6 +12,7 @@ defmodule Indexer.Temporary.FailedCreatedAddresses do alias Indexer.Temporary.FailedCreatedAddresses.TaskSupervisor @task_options [max_concurrency: 3, timeout: 15_000] + @query_timeout :infinity def start_link([json_rpc_named_arguments, gen_server_options]) do GenServer.start_link(__MODULE__, json_rpc_named_arguments, gen_server_options) @@ -33,7 +34,7 @@ defmodule Indexer.Temporary.FailedCreatedAddresses do preload: :transaction ) - found_internal_transactions = Repo.all(query) + found_internal_transactions = Repo.all(query, timeout: @query_timeout) TaskSupervisor |> Task.Supervisor.async_stream( diff --git a/apps/indexer/lib/indexer/temporary/failed_created_addresses/supervisor.ex b/apps/indexer/lib/indexer/temporary/failed_created_addresses/supervisor.ex index 1e92cde5ee..9ec54e7812 100644 --- a/apps/indexer/lib/indexer/temporary/failed_created_addresses/supervisor.ex +++ b/apps/indexer/lib/indexer/temporary/failed_created_addresses/supervisor.ex @@ -18,7 +18,7 @@ defmodule Indexer.Temporary.FailedCreatedAddresses.Supervisor do type: :supervisor } - Supervisor.child_spec(default, []) |> IO.inspect() + Supervisor.child_spec(default, []) end def start_link(json_rpc_named_arguments, gen_server_options \\ []) do diff --git a/apps/indexer/test/indexer/temporary/failed_created_addresses_test.exs b/apps/indexer/test/indexer/temporary/failed_created_addresses_test.exs index 693797bdab..7743600a9f 100644 --- a/apps/indexer/test/indexer/temporary/failed_created_addresses_test.exs +++ b/apps/indexer/test/indexer/temporary/failed_created_addresses_test.exs @@ -7,7 +7,7 @@ defmodule Indexer.Temporary.FailedCreatedAddressesTest do import Ecto.Query alias Explorer.Repo - alias Explorer.Chain.{Address, InternalTransaction, Transaction} + alias Explorer.Chain.{Address, Transaction} alias Indexer.Temporary.FailedCreatedAddresses.Supervisor alias Indexer.CoinBalance @@ -40,13 +40,12 @@ defmodule Indexer.Temporary.FailedCreatedAddressesTest do address = insert(:address, contract_code: "0x0102030405") - internal_transaction = - insert(:internal_transaction, - block_number: transaction.block_number, - transaction: transaction, - index: 0, - created_contract_address_hash: address.hash - ) + insert(:internal_transaction, + block_number: transaction.block_number, + transaction: transaction, + index: 0, + created_contract_address_hash: address.hash + ) if json_rpc_named_arguments[:transport] == EthereumJSONRPC.Mox do EthereumJSONRPC.Mox From 659c26d4a2dee97ca68aaf28a19d11bdf4edc7e4 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Mon, 25 Feb 2019 14:30:08 +0300 Subject: [PATCH 6/9] add logging --- apps/indexer/config/dev.exs | 5 ++ apps/indexer/config/prod.exs | 5 ++ apps/indexer/config/test.exs | 5 ++ apps/indexer/lib/indexer/block/supervisor.ex | 6 ++ .../temporary/failed_created_addresses.ex | 65 ++++++++++++++++--- config/config.exs | 1 + 6 files changed, 79 insertions(+), 8 deletions(-) diff --git a/apps/indexer/config/dev.exs b/apps/indexer/config/dev.exs index ffd1d6b0ee..94f2fe82dc 100644 --- a/apps/indexer/config/dev.exs +++ b/apps/indexer/config/dev.exs @@ -11,6 +11,11 @@ config :logger, :indexer_token_balances, path: Path.absname("logs/dev/indexer/token_balances/error.log"), metadata_filter: [fetcher: :token_balances] +config :logger, :failed_contract_creations, + level: :debug, + path: Path.absname("logs/dev/indexer/failed_contract_creations.log"), + metadata_filter: [fetcher: :failed_created_addresses] + variant = if is_nil(System.get_env("ETHEREUM_JSONRPC_VARIANT")) do "ganache" diff --git a/apps/indexer/config/prod.exs b/apps/indexer/config/prod.exs index 49800bcc15..4455dc4c90 100644 --- a/apps/indexer/config/prod.exs +++ b/apps/indexer/config/prod.exs @@ -13,6 +13,11 @@ config :logger, :indexer_token_balances, metadata_filter: [fetcher: :token_balances], rotate: %{max_bytes: 52_428_800, keep: 19} +config :logger, :failed_contract_creations, + level: :prod, + path: Path.absname("logs/prod/indexer/failed_contract_creations.log"), + metadata_filter: [fetcher: :failed_created_addresses] + variant = if is_nil(System.get_env("ETHEREUM_JSONRPC_VARIANT")) do "parity" diff --git a/apps/indexer/config/test.exs b/apps/indexer/config/test.exs index e9c5505405..af40fe5ad6 100644 --- a/apps/indexer/config/test.exs +++ b/apps/indexer/config/test.exs @@ -10,3 +10,8 @@ config :logger, :indexer_token_balances, level: :debug, path: Path.absname("logs/test/indexer/token_balances/error.log"), metadata_filter: [fetcher: :token_balances] + +config :logger, :failed_contract_creations, + level: :debug, + path: Path.absname("logs/test/indexer/failed_contract_creations.log"), + metadata_filter: [fetcher: :failed_created_addresses] diff --git a/apps/indexer/lib/indexer/block/supervisor.ex b/apps/indexer/lib/indexer/block/supervisor.ex index bfc5fb20f4..6365aba2d7 100644 --- a/apps/indexer/lib/indexer/block/supervisor.ex +++ b/apps/indexer/lib/indexer/block/supervisor.ex @@ -5,6 +5,7 @@ defmodule Indexer.Block.Supervisor do alias Indexer.Block alias Indexer.Block.{Catchup, InvalidConsensus, Realtime, Reward, Uncle} + alias Indexer.Temporary.FailedCreatedAddresses use Supervisor @@ -45,6 +46,11 @@ defmodule Indexer.Block.Supervisor do [ [json_rpc_named_arguments: json_rpc_named_arguments, memory_monitor: memory_monitor], [name: Reward.Supervisor] + ]}, + {FailedCreatedAddresses.Supervisor, + [ + json_rpc_named_arguments, + [name: FailedCreatedAddresses.Supervisor] ]} ], strategy: :one_for_one diff --git a/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex b/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex index a7da5ae847..8e7e256844 100644 --- a/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex +++ b/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex @@ -2,9 +2,10 @@ defmodule Indexer.Temporary.FailedCreatedAddresses do @moduledoc """ Temporary module to fix internal transactions and their created transactions if a parent transaction has failed. """ - use GenServer + require Logger + import Ecto.Query alias Explorer.Chain.{InternalTransaction, Transaction} @@ -26,6 +27,13 @@ defmodule Indexer.Temporary.FailedCreatedAddresses do end def run(json_rpc_named_arguments) do + Logger.debug( + [ + "Started query to fetch internal transactions that need to be fixed" + ], + fetcher: :failed_created_addresses + ) + query = from(it in InternalTransaction, left_join: t in Transaction, @@ -36,8 +44,17 @@ defmodule Indexer.Temporary.FailedCreatedAddresses do found_internal_transactions = Repo.all(query, timeout: @query_timeout) + Logger.debug( + [ + "Finished query to fetch internal transactions that need to be fixed. Number of records is #{ + Enum.count(found_internal_transactions) + }" + ], + fetcher: :failed_created_addresses + ) + TaskSupervisor - |> Task.Supervisor.async_stream( + |> Task.Supervisor.async_stream_nolink( found_internal_transactions, fn internal_transaction -> fix_internal_transaction(internal_transaction, json_rpc_named_arguments) end, @task_options @@ -46,13 +63,45 @@ defmodule Indexer.Temporary.FailedCreatedAddresses do end def fix_internal_transaction(internal_transaction, json_rpc_named_arguments) do - internal_transaction - |> code_entry() - |> Indexer.Code.Fetcher.run(json_rpc_named_arguments) + try do + Logger.debug( + [ + "Started fixing internal transaction #{internal_transaction.index} for transaction with hash #{ + to_string(internal_transaction.transaction_hash) + }" + ], + fetcher: :failed_created_addresses + ) + + :ok = + internal_transaction + |> code_entry() + |> Indexer.Code.Fetcher.run(json_rpc_named_arguments) - internal_transaction.transaction - |> transaction_entry() - |> Indexer.InternalTransaction.Fetcher.run(json_rpc_named_arguments) + :ok = + internal_transaction.transaction + |> transaction_entry() + |> Indexer.InternalTransaction.Fetcher.run(json_rpc_named_arguments) + + Logger.debug( + [ + "Finished fixing internal transaction #{internal_transaction.index} for transaction with hash #{ + to_string(internal_transaction.transaction_hash) + }" + ], + fetcher: :failed_created_addresses + ) + rescue + e -> + Logger.debug( + [ + "Failed to fix internal transaction #{internal_transaction.index} for transaction with hash #{ + to_string(internal_transaction.transaction_hash) + } because of #{inspect(e)}" + ], + fetcher: :failed_created_addresses + ) + end end def code_entry(%InternalTransaction{ diff --git a/config/config.exs b/config/config.exs index dd53c84375..0d5d0ad976 100644 --- a/config/config.exs +++ b/config/config.exs @@ -26,6 +26,7 @@ config :logger, # only :indexer, but all levels {LoggerFileBackend, :indexer}, {LoggerFileBackend, :indexer_token_balances}, + {LoggerFileBackend, :failed_contract_creations}, {LoggerFileBackend, :reading_token_functions} ] From 2ac41c6137fbce1dda84b3e242af5fbb95cb0752 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Mon, 25 Feb 2019 14:37:22 +0300 Subject: [PATCH 7/9] fix credo for temp module --- apps/indexer/lib/indexer/temporary/failed_created_addresses.ex | 1 + 1 file changed, 1 insertion(+) diff --git a/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex b/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex index 8e7e256844..b28b0d226c 100644 --- a/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex +++ b/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex @@ -63,6 +63,7 @@ defmodule Indexer.Temporary.FailedCreatedAddresses do end def fix_internal_transaction(internal_transaction, json_rpc_named_arguments) do + # credo:disable-for-next-line try do Logger.debug( [ From 37f120d281908ad3e780eda9837f9776dd01dbce Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Mon, 25 Feb 2019 15:03:28 +0300 Subject: [PATCH 8/9] make query lighter --- .../temporary/failed_created_addresses.ex | 47 ++++++++++--------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex b/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex index b28b0d226c..cdf6451965 100644 --- a/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex +++ b/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex @@ -35,19 +35,18 @@ defmodule Indexer.Temporary.FailedCreatedAddresses do ) query = - from(it in InternalTransaction, - left_join: t in Transaction, + from(t in Transaction, + left_join: it in InternalTransaction, on: it.transaction_hash == t.hash, - where: t.status == ^0 and not is_nil(it.created_contract_address_hash), - preload: :transaction + where: t.status == ^0 and not is_nil(it.created_contract_address_hash) ) - found_internal_transactions = Repo.all(query, timeout: @query_timeout) + found_transactions = Repo.all(query, timeout: @query_timeout) Logger.debug( [ "Finished query to fetch internal transactions that need to be fixed. Number of records is #{ - Enum.count(found_internal_transactions) + Enum.count(found_transactions) }" ], fetcher: :failed_created_addresses @@ -55,40 +54,44 @@ defmodule Indexer.Temporary.FailedCreatedAddresses do TaskSupervisor |> Task.Supervisor.async_stream_nolink( - found_internal_transactions, - fn internal_transaction -> fix_internal_transaction(internal_transaction, json_rpc_named_arguments) end, + found_transactions, + fn transaction -> fix_internal_transaction(transaction, json_rpc_named_arguments) end, @task_options ) |> Enum.to_list() end - def fix_internal_transaction(internal_transaction, json_rpc_named_arguments) do + def fix_internal_transaction(transaction, json_rpc_named_arguments) do # credo:disable-for-next-line try do Logger.debug( [ - "Started fixing internal transaction #{internal_transaction.index} for transaction with hash #{ - to_string(internal_transaction.transaction_hash) - }" + "Started fixing transaction #{to_string(transaction.hash)}" ], fetcher: :failed_created_addresses ) - :ok = - internal_transaction - |> code_entry() - |> Indexer.Code.Fetcher.run(json_rpc_named_arguments) + transaction_with_internal_transactions = Repo.preload(transaction, [:internal_transactions]) + + transaction_with_internal_transactions.internal_transactions + |> Enum.filter(fn internal_transaction -> + internal_transaction.created_contract_address_hash + end) + |> Enum.each(fn internal_transaction -> + :ok = + internal_transaction + |> code_entry() + |> Indexer.Code.Fetcher.run(json_rpc_named_arguments) + end) :ok = - internal_transaction.transaction + transaction |> transaction_entry() |> Indexer.InternalTransaction.Fetcher.run(json_rpc_named_arguments) Logger.debug( [ - "Finished fixing internal transaction #{internal_transaction.index} for transaction with hash #{ - to_string(internal_transaction.transaction_hash) - }" + "Finished fixing transaction #{to_string(transaction.hash)}" ], fetcher: :failed_created_addresses ) @@ -96,9 +99,7 @@ defmodule Indexer.Temporary.FailedCreatedAddresses do e -> Logger.debug( [ - "Failed to fix internal transaction #{internal_transaction.index} for transaction with hash #{ - to_string(internal_transaction.transaction_hash) - } because of #{inspect(e)}" + "Failed fixing transaction #{to_string(transaction.hash)} because of #{inspect(e)}" ], fetcher: :failed_created_addresses ) From 55b4ce6a95fe1362d49895dab8ba6be0cb41c19f Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Tue, 26 Feb 2019 08:46:15 +0300 Subject: [PATCH 9/9] fix timeout for task --- apps/indexer/lib/indexer/temporary/failed_created_addresses.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex b/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex index cdf6451965..1a5dd99d07 100644 --- a/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex +++ b/apps/indexer/lib/indexer/temporary/failed_created_addresses.ex @@ -12,7 +12,7 @@ defmodule Indexer.Temporary.FailedCreatedAddresses do alias Explorer.Repo alias Indexer.Temporary.FailedCreatedAddresses.TaskSupervisor - @task_options [max_concurrency: 3, timeout: 15_000] + @task_options [max_concurrency: 3, timeout: :infinity] @query_timeout :infinity def start_link([json_rpc_named_arguments, gen_server_options]) do