From 4d29cc2a77097d73e247e7225571ebb8fad8e49e Mon Sep 17 00:00:00 2001 From: Qwerty5Uiop <105209995+Qwerty5Uiop@users.noreply.github.com> Date: Wed, 8 Jan 2025 17:37:17 +0400 Subject: [PATCH] fix: Fix tokens and transactions deadlocks (#11620) --- apps/explorer/lib/explorer/chain/import.ex | 1 + .../import/runner/signed_authorizations.ex | 3 ++- .../chain/import/stage/token_instances.ex | 27 +++++++++++++++++++ .../chain/import/stage/token_referencing.ex | 1 - 4 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 apps/explorer/lib/explorer/chain/import/stage/token_instances.ex diff --git a/apps/explorer/lib/explorer/chain/import.ex b/apps/explorer/lib/explorer/chain/import.ex index d2229641e069..5bfda72c8516 100644 --- a/apps/explorer/lib/explorer/chain/import.ex +++ b/apps/explorer/lib/explorer/chain/import.ex @@ -21,6 +21,7 @@ defmodule Explorer.Chain.Import do [ Import.Stage.BlockTransactionReferencing, Import.Stage.TokenReferencing, + Import.Stage.TokenInstances, Import.Stage.InternalTransactions, Import.Stage.ChainTypeSpecific ] diff --git a/apps/explorer/lib/explorer/chain/import/runner/signed_authorizations.ex b/apps/explorer/lib/explorer/chain/import/runner/signed_authorizations.ex index 147f5c821dc7..2683c87bdcbd 100644 --- a/apps/explorer/lib/explorer/chain/import/runner/signed_authorizations.ex +++ b/apps/explorer/lib/explorer/chain/import/runner/signed_authorizations.ex @@ -64,11 +64,12 @@ defmodule Explorer.Chain.Import.Runner.SignedAuthorizations do defp insert(repo, changes_list, %{timeout: timeout, timestamps: timestamps} = options) when is_list(changes_list) do on_conflict = Map.get_lazy(options, :on_conflict, &default_on_conflict/0) conflict_target = [:transaction_hash, :index] + ordered_changes_list = Enum.sort_by(changes_list, &{&1.transaction_hash, &1.index}) {:ok, _} = Import.insert_changes_list( repo, - changes_list, + ordered_changes_list, for: SignedAuthorization, on_conflict: on_conflict, conflict_target: conflict_target, diff --git a/apps/explorer/lib/explorer/chain/import/stage/token_instances.ex b/apps/explorer/lib/explorer/chain/import/stage/token_instances.ex new file mode 100644 index 000000000000..785930e6e18d --- /dev/null +++ b/apps/explorer/lib/explorer/chain/import/stage/token_instances.ex @@ -0,0 +1,27 @@ +defmodule Explorer.Chain.Import.Stage.TokenInstances do + @moduledoc """ + Import token instances. + """ + + alias Explorer.Chain.Import.{Runner, Stage} + + @behaviour Stage + + @runners [ + Runner.TokenInstances + ] + + @impl Stage + def runners, do: @runners + + @impl Stage + def all_runners, do: runners() + + @impl Stage + def multis(runner_to_changes_list, options) do + {final_multi, final_remaining_runner_to_changes_list} = + Stage.single_multi(runners(), runner_to_changes_list, options) + + {[final_multi], final_remaining_runner_to_changes_list} + end +end diff --git a/apps/explorer/lib/explorer/chain/import/stage/token_referencing.ex b/apps/explorer/lib/explorer/chain/import/stage/token_referencing.ex index 22cb8466e827..18af2e8563b9 100644 --- a/apps/explorer/lib/explorer/chain/import/stage/token_referencing.ex +++ b/apps/explorer/lib/explorer/chain/import/stage/token_referencing.ex @@ -8,7 +8,6 @@ defmodule Explorer.Chain.Import.Stage.TokenReferencing do @behaviour Stage @runners [ - Runner.TokenInstances, Runner.Address.TokenBalances, Runner.Address.CurrentTokenBalances ]