Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure download of self repair on other locally available nodes #649

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions lib/archethic/self_repair/sync.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down