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

Fix self repair recipient consolidation #1628

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
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
54 changes: 33 additions & 21 deletions lib/archethic/self_repair/sync.ex
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ defmodule Archethic.SelfRepair.Sync do
previous_summary_time
)

consolidated_attestation = consolidate_recipients(attestation, tx)
consolidated_attestation = consolidate_recipients(attestation, tx, download_nodes)
{consolidated_attestation, tx, inputs}
end,
max_concurrency: System.schedulers_online() * 2,
Expand All @@ -507,29 +507,15 @@ defmodule Archethic.SelfRepair.Sync do
movements_addresses: movements_addresses
}
},
%Transaction{validation_stamp: %ValidationStamp{recipients: recipients = [_ | _]}}
%Transaction{validation_stamp: %ValidationStamp{recipients: recipients = [_ | _]}},
download_nodes
) do
authorized_nodes = P2P.authorized_and_available_nodes()
resolved_addresses =
Enum.chunk_every(movements_addresses, 2) |> Enum.map(&List.to_tuple/1) |> Map.new()

consolidated_movements_addresses =
recipients
|> Task.async_stream(
fn recipient ->
genesis_nodes = Election.chain_storage_nodes(recipient, authorized_nodes)

case TransactionChain.fetch_genesis_address(recipient, genesis_nodes,
acceptance_resolver: :accept_different_genesis
) do
{:ok, genesis_address} ->
[recipient, genesis_address]

{:error, reason} ->
raise SelfRepair.Error,
function: "consolidate_recipients",
message: "Failed to fetch genesis address with error #{inspect(reason)}",
address: recipient
end
end,
|> Task.async_stream(&do_consolidate_recipient(&1, resolved_addresses, download_nodes),
max_concurrency: length(recipients)
)
|> Stream.flat_map(fn {:ok, addresses} -> addresses end)
Expand All @@ -543,7 +529,33 @@ defmodule Archethic.SelfRepair.Sync do
%ReplicationAttestation{attestation | transaction_summary: adjusted_summary}
end

defp consolidate_recipients(attestation, _tx), do: attestation
defp consolidate_recipients(attestation, _tx, _), do: attestation

defp do_consolidate_recipient(recipient, resolved_addresses, download_nodes) do
case Map.fetch(resolved_addresses, recipient) do
{:ok, genesis} ->
[recipient, genesis]

_ ->
genesis_nodes = Election.chain_storage_nodes(recipient, download_nodes)

case TransactionChain.fetch_genesis_address(recipient, genesis_nodes,
acceptance_resolver: :accept_different_genesis
) do
{:ok, genesis_address} ->
[recipient, genesis_address]

{:error, :acceptance_failed} ->
[recipient, recipient]

{:error, reason} ->
raise SelfRepair.Error,
function: "consolidate_recipients",
message: "Failed to fetch genesis address with error #{inspect(reason)}",
address: recipient
end
end
end

defp sync_node(end_of_node_synchronizations) do
end_of_node_synchronizations
Expand Down
9 changes: 3 additions & 6 deletions lib/archethic/transaction_chain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ defmodule Archethic.TransactionChain do
It queries the the network for genesis address.
"""
@spec fetch_genesis_address(address :: binary(), nodes :: list(Node.t()), opts :: Keyword.t()) ::
{:ok, binary()} | {:error, :network_issue}
{:ok, binary()} | {:error, :network_issue} | {:error, :acceptance_failed}
def fetch_genesis_address(address, nodes, opts \\ []) when is_binary(address) do
case find_genesis_address(address) do
{:error, :not_found} ->
Expand Down Expand Up @@ -851,11 +851,8 @@ defmodule Archethic.TransactionChain do
timeout: timeout,
acceptance_resolver: acceptance_resolver
) do
{:ok, %GenesisAddress{address: genesis_address}} ->
{:ok, genesis_address}

_ ->
{:error, :network_issue}
{:ok, %GenesisAddress{address: genesis_address}} -> {:ok, genesis_address}
error -> error
end

res ->
Expand Down
4 changes: 2 additions & 2 deletions test/archethic/bootstrap/network_init_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,8 @@ defmodule Archethic.Bootstrap.NetworkInitTest do
_, %GetTransactionChainLength{}, _ ->
%TransactionChainLength{length: 1}

_, %GetGenesisAddress{}, _ ->
{:ok, %NotFound{}}
_, %GetGenesisAddress{address: address}, _ ->
{:ok, %GenesisAddress{address: address, timestamp: DateTime.utc_now()}}
end)

P2P.add_and_connect_node(%Node{
Expand Down
10 changes: 4 additions & 6 deletions test/archethic/bootstrap_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ defmodule Archethic.BootstrapTest do
EncryptedStorageNonce,
GetBootstrappingNodes,
GetGenesisAddress,
GenesisAddress,
GetLastTransactionAddress,
GetStorageNonce,
GetTransaction,
Expand Down Expand Up @@ -126,14 +127,11 @@ defmodule Archethic.BootstrapTest do
_, %GetTransactionChainLength{}, _ ->
%TransactionChainLength{length: 1}

_, %GetGenesisAddress{}, _ ->
{:ok, %NotFound{}}
_, %GetGenesisAddress{address: address}, _ ->
{:ok, %GenesisAddress{address: address, timestamp: DateTime.utc_now()}}

_, %GetCurrentReplicationAttestations{}, _ ->
{:ok,
%CurrentReplicationAttestations{
replication_attestations: []
}}
{:ok, %CurrentReplicationAttestations{replication_attestations: []}}
end)

{:ok, daily_nonce_agent} = Agent.start_link(fn -> %{} end)
Expand Down
Loading