diff --git a/lib/archethic/mining/transaction_context.ex b/lib/archethic/mining/transaction_context.ex index 3016146aca..58156482b1 100644 --- a/lib/archethic/mining/transaction_context.ex +++ b/lib/archethic/mining/transaction_context.ex @@ -10,7 +10,6 @@ defmodule Archethic.Mining.TransactionContext do alias Archethic.Election alias Archethic.P2P - alias Archethic.P2P.Message alias Archethic.P2P.Message.Ok alias Archethic.P2P.Message.Ping alias Archethic.P2P.Node @@ -61,7 +60,7 @@ defmodule Archethic.Mining.TransactionContext do prev_tx_task = request_previous_tx(previous_address, prev_tx_nodes_split) nodes_view_task = request_nodes_view(node_public_keys) - prev_tx = Task.await(prev_tx_task, Message.get_max_timeout()) + prev_tx = Task.await(prev_tx_task, 4500) nodes_view = Task.await(nodes_view_task) %{ @@ -102,15 +101,16 @@ defmodule Archethic.Mining.TransactionContext do Task.Supervisor.async( TaskSupervisor, fn -> - case TransactionChain.fetch_transaction_remotely(previous_address, nodes) do + # Timeout of 4 sec because the coordinator node wait 5 sec to get the context + # from the cross validation nodes + case TransactionChain.fetch_transaction_remotely(previous_address, nodes, 4000) do {:ok, tx} -> tx {:error, _} -> nil end - end, - timeout: Message.get_max_timeout() + end ) end diff --git a/lib/archethic/replication.ex b/lib/archethic/replication.ex index 89a3fb4973..18afd8ee55 100644 --- a/lib/archethic/replication.ex +++ b/lib/archethic/replication.ex @@ -268,10 +268,10 @@ defmodule Archethic.Replication do {previous_transaction, inputs} = if self_repair? do - {Task.await(t1, Message.get_max_timeout()), []} + {Task.await(t1, Message.get_max_timeout() + 1000), []} else - t2 = Task.Supervisor.async(TaskSupervisor, fn -> fetch_inputs(tx, download_nodes) end) - {Task.await(t1, Message.get_max_timeout()), Task.await(t2)} + inputs = fetch_inputs(tx, download_nodes) + {Task.await(t1, Message.get_max_timeout() + 1000), inputs} end Logger.debug("Previous transaction #{inspect(previous_transaction)}", diff --git a/lib/archethic/transaction_chain.ex b/lib/archethic/transaction_chain.ex index d5d131d3d1..49b01a20e1 100644 --- a/lib/archethic/transaction_chain.ex +++ b/lib/archethic/transaction_chain.ex @@ -687,14 +687,21 @@ defmodule Archethic.TransactionChain do If no nodes are available to answer the request, `{:error, :network_issue}` is returned. """ - @spec fetch_transaction_remotely(address :: Crypto.versioned_hash(), list(Node.t())) :: + @spec fetch_transaction_remotely( + address :: Crypto.versioned_hash(), + list(Node.t()), + non_neg_integer() + ) :: {:ok, Transaction.t()} | {:error, :transaction_not_exists} | {:error, :transaction_invalid} | {:error, :network_issue} - def fetch_transaction_remotely(_, []), do: {:error, :transaction_not_exists} + def fetch_transaction_remotely(address, nodes, timeout \\ Message.get_max_timeout()) + + def fetch_transaction_remotely(_, [], _), do: {:error, :transaction_not_exists} - def fetch_transaction_remotely(address, nodes) when is_binary(address) and is_list(nodes) do + def fetch_transaction_remotely(address, nodes, timeout) + when is_binary(address) and is_list(nodes) do conflict_resolver = fn results -> # Prioritize transactions results over not found with nil <- Enum.find(results, &match?(%Transaction{}, &1)), @@ -709,7 +716,8 @@ defmodule Archethic.TransactionChain do case P2P.quorum_read( nodes, %GetTransaction{address: address}, - conflict_resolver + conflict_resolver, + timeout ) do {:ok, %NotFound{}} -> {:error, :transaction_not_exists} diff --git a/test/archethic/bootstrap_test.exs b/test/archethic/bootstrap_test.exs index 5718a0f513..f313b8a471 100644 --- a/test/archethic/bootstrap_test.exs +++ b/test/archethic/bootstrap_test.exs @@ -495,7 +495,7 @@ defmodule Archethic.BootstrapTest do assert :ok = Bootstrap.do_resync_network_chain( - [:node_shared_secrets], + :node_shared_secrets, _nodes = P2P.authorized_and_available_nodes() ) end @@ -523,7 +523,7 @@ defmodule Archethic.BootstrapTest do assert :ok = Bootstrap.do_resync_network_chain( - [:node_shared_secrets], + :node_shared_secrets, _nodes = P2P.authorized_and_available_nodes() ) @@ -598,7 +598,7 @@ defmodule Archethic.BootstrapTest do assert :ok = Bootstrap.do_resync_network_chain( - [:node_shared_secrets], + :node_shared_secrets, _nodes = P2P.authorized_and_available_nodes() )