Skip to content

Commit

Permalink
Add process clenaup in standalone workflow (#550)
Browse files Browse the repository at this point in the history
* Add timeout in standalone workflow
* Stop mining process with enough confirmations
  • Loading branch information
samuelmanzanera committed Sep 9, 2022
1 parent ef14fef commit 76e4fa3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
2 changes: 2 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ config :archethic, ArchethicWeb.Endpoint,
layout: {ArchethicWeb.LayoutView, "live.html"}
]

config :archethic, Archethic.Mining.StandaloneWorkflow, global_timeout: 10_000

config :archethic, Archethic.Mining.DistributedWorkflow,
global_timeout: 60_000,
coordinator_timeout_supplement: 2_000,
Expand Down
30 changes: 28 additions & 2 deletions lib/archethic/mining/standalone_workflow.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ defmodule Archethic.Mining.StandaloneWorkflow do

require Logger

@mining_timeout Application.compile_env!(:archethic, [__MODULE__, :global_timeout])

def start_link(arg \\ []) do
GenServer.start_link(__MODULE__, arg)
end
Expand Down Expand Up @@ -129,7 +131,7 @@ defmodule Archethic.Mining.StandaloneWorkflow do
%{
context: validation_context,
confirmations: []
}}
}, @mining_timeout}
end

defp validate(context = %ValidationContext{}) do
Expand Down Expand Up @@ -221,7 +223,7 @@ defmodule Archethic.Mining.StandaloneWorkflow do

if ValidationContext.enough_storage_confirmations?(new_context) do
notify(new_state)
{:noreply, new_state}
{:stop, :normal, new_state}
else
{:noreply, new_state}
end
Expand All @@ -237,6 +239,30 @@ defmodule Archethic.Mining.StandaloneWorkflow do
end
end

def handle_info(
:timeout,
state = %{context: %ValidationContext{transaction: tx, welcome_node: welcome_node}}
) do
Logger.warning("Timeout reached during mining",
transaction_type: tx.type,
transaction_address: Base.encode16(tx.address)
)

# Notify error to the welcome node
message = %ValidationError{context: :network_issue, reason: "timeout", address: tx.address}

Task.Supervisor.async_nolink(Archethic.TaskSupervisor, fn ->
P2P.send_message(
welcome_node,
message
)

:ok
end)

{:stop, :normal, state}
end

defp notify(%{context: context}) do
notify_attestation(context)
notify_io_nodes(context)
Expand Down

0 comments on commit 76e4fa3

Please sign in to comment.