Skip to content

Commit

Permalink
Handle timeout of the network coodinates task
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmanzanera committed Jun 9, 2023
1 parent b27527a commit e2fc98b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
26 changes: 23 additions & 3 deletions lib/archethic/beacon_chain/subset.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ defmodule Archethic.BeaconChain.Subset do

alias Archethic.PubSub

alias Archethic.SelfRepair

alias Archethic.TaskSupervisor
alias Archethic.TransactionChain.TransactionSummary

alias Archethic.Utils
Expand Down Expand Up @@ -268,7 +271,7 @@ defmodule Archethic.BeaconChain.Subset do
end

defp forward_attestation?(slot_time, subset, node_public_key) do
# We need to forward the transaction if this is the last slot of a summary
# We need to forward the transaction if this is the last slot of a summary
# and we are not a summary node because the current slot will not be sent to summary node
summary_time?(slot_time) and not beacon_summary_node?(subset, slot_time, node_public_key)
end
Expand Down Expand Up @@ -374,7 +377,8 @@ defmodule Archethic.BeaconChain.Subset do
else
Logger.debug("Create beacon summary", beacon_subset: Base.encode16(subset))

patch_task = Task.async(fn -> get_network_patches(subset, time) end)
patch_task =
Task.Supervisor.async_nolink(TaskSupervisor, fn -> get_network_patches(subset, time) end)

summary =
%Summary{
Expand All @@ -386,7 +390,23 @@ defmodule Archethic.BeaconChain.Subset do
P2PSampling.list_nodes_to_sample(subset)
)

network_patches = Task.await(patch_task)
network_patches_timeout =
SelfRepair.next_repair_time()
|> DateTime.diff(DateTime.utc_now())
# We take 10% of the next repair time to determine the timeout
|> Kernel.*(0.9)
|> Kernel.*(1000)
|> round()

network_patches =
case Task.yield(patch_task, network_patches_timeout) || Task.shutdown(patch_task) do
{:ok, network_patches} ->
network_patches

_ ->
[]
end

BeaconChain.write_beacon_summary(%{summary | network_patches: network_patches})

:ok
Expand Down
5 changes: 5 additions & 0 deletions test/archethic/beacon_chain/subset_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ defmodule Archethic.BeaconChain.SubsetTest do
alias Archethic.P2P.Message.Ping
alias Archethic.P2P.Node

alias Archethic.SelfRepair.Scheduler, as: SelfRepairScheduler

alias Archethic.TransactionChain.TransactionSummary

import Mox
Expand Down Expand Up @@ -350,6 +352,9 @@ defmodule Archethic.BeaconChain.SubsetTest do

summary_interval = "*/3 * * * *"

# This is needed to get network coordinates's task timeout
start_supervised!({SelfRepairScheduler, interval: "*/10 * * * *"})

start_supervised!({SummaryTimer, interval: summary_interval})
start_supervised!({SlotTimer, interval: "*/1 * * * *"})
start_supervised!(SummaryCache)
Expand Down

0 comments on commit e2fc98b

Please sign in to comment.