From b683a08906a6a06b16f9ac2cdd8f6e98f205086d Mon Sep 17 00:00:00 2001 From: Neylix Date: Fri, 21 Oct 2022 12:06:40 +0200 Subject: [PATCH] Ensure download of self repair on other locally available nodes (#649) --- lib/archethic/self_repair/sync.ex | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/lib/archethic/self_repair/sync.ex b/lib/archethic/self_repair/sync.ex index 03bc570b0..41080b399 100644 --- a/lib/archethic/self_repair/sync.ex +++ b/lib/archethic/self_repair/sync.ex @@ -161,25 +161,30 @@ defmodule Archethic.SelfRepair.Sync do |> Stream.map(fn {:ok, {:ok, aggregate}} -> aggregate end) end - defp ensure_download_last_aggregate(last_aggregate = %SummaryAggregate{}) do + defp ensure_download_last_aggregate( + last_aggregate = %SummaryAggregate{summary_time: summary_time} + ) do # Make sure the last beacon aggregate have been synchronized # from remote nodes to avoid self-repair to be acknowledged if those # cannot be reached - node_public_key = Crypto.first_node_public_key() - - case P2P.authorized_and_available_nodes() do - [%Node{first_public_key: ^node_public_key}] -> - :ok - authorized_nodes -> - remaining_nodes = - authorized_nodes - |> Enum.reject(&(&1.first_public_key == node_public_key)) - |> Enum.count() + nodes = + P2P.authorized_nodes(summary_time) + |> Enum.filter(& &1.available?) - if remaining_nodes > 0 and SummaryAggregate.empty?(last_aggregate) do + # If number of authorized node is <= 2 and current node is part of it + # we accept the self repair as the other node may be unavailable and so + # we need to do the self even if no other node respond + with true <- P2P.authorized_node?(), + true <- length(nodes) <= 2 do + :ok + else + _ -> + if SummaryAggregate.empty?(last_aggregate) do raise "Cannot make the self repair - Last aggregate not fetched" end + + :ok end end