Skip to content

Commit

Permalink
Ensure to fetch missed transaction from the beacon in the self repair (
Browse files Browse the repository at this point in the history
…#98)

Tries to fetch the transaction from the closest nodes, but retry until the end of the node list.
Because it's not subject to important latency here, in case of issue of sync from one node, try to load from the next one.
  • Loading branch information
Samuel authored Sep 22, 2021
1 parent 82668b1 commit f744226
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 23 deletions.
23 changes: 21 additions & 2 deletions lib/archethic/self_repair/sync/beacon_summary_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,31 @@ defmodule ArchEthic.SelfRepair.Sync.BeaconSummaryHandler do
Election.beacon_storage_nodes(subset, summary_time, [P2P.get_node_info() | nodes])

with true <- Utils.key_in_node_list?(beacon_storage_nodes, Crypto.first_node_public_key()),
false <- TransactionChain.transaction_exists?(address) do
{:ok, tx = %Transaction{}} = P2P.reply_first(nodes, %GetTransaction{address: address})
false <- TransactionChain.transaction_exists?(address),
{:ok, tx} <- fetch_transaction(nodes, address) do
TransactionChain.write_transaction(tx)
end
end

defp fetch_transaction([node | rest], address) do
case P2P.send_message(node, %GetTransaction{address: address}) do
{:ok, tx = %Transaction{}} ->
{:ok, tx}

_ ->
fetch_transaction(rest, address)
end
end

defp fetch_transaction([], address) do
Logger.error("Cannot fetch beacon summary transaction to store",
transaction_address: Base.encode16(address),
type: :beacon_summary
)

{:error, :network_issue}
end

@doc """
Process beacon slots to synchronize the transactions involving.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ defmodule ArchEthic.SelfRepair.Sync.BeaconSummaryHandler.TransactionHandler do

alias ArchEthic.P2P
alias ArchEthic.P2P.Message.GetTransaction
alias ArchEthic.P2P.Message.NotFound

alias ArchEthic.Replication

Expand Down Expand Up @@ -51,7 +50,7 @@ defmodule ArchEthic.SelfRepair.Sync.BeaconSummaryHandler.TransactionHandler do
@spec download_transaction(TransactionSummary.t(), patch :: binary()) ::
:ok | {:error, :invalid_transaction}
def download_transaction(
%TransactionSummary{address: address, type: type, timestamp: timestamp},
%TransactionSummary{address: address, type: type, timestamp: _timestamp},
node_patch
)
when is_binary(node_patch) do
Expand All @@ -61,20 +60,12 @@ defmodule ArchEthic.SelfRepair.Sync.BeaconSummaryHandler.TransactionHandler do
)

storage_nodes =
case P2P.authorized_nodes(timestamp) do
[] ->
Replication.chain_storage_nodes_with_type(address, type)

nodes ->
Replication.chain_storage_nodes_with_type(address, type, nodes)
end

response =
storage_nodes
address
|> Replication.chain_storage_nodes_with_type(type)
|> Enum.reject(&(&1.first_public_key == Crypto.first_node_public_key()))
|> P2P.reply_first(%GetTransaction{address: address})
|> P2P.nearest_nodes()

case response do
case fetch_transaction(storage_nodes, address) do
{:ok, tx = %Transaction{}} ->
node_list = [P2P.get_node_info() | P2P.authorized_nodes()] |> P2P.distinct_nodes()

Expand All @@ -94,17 +85,25 @@ defmodule ArchEthic.SelfRepair.Sync.BeaconSummaryHandler.TransactionHandler do

Replication.process_transaction(tx, roles, self_repair?: true)

{:ok, %NotFound{}} ->
Logger.error("Transaction not found from remote nodes during self repair",
transaction_address: Base.encode16(address),
transaction_type: type
)

{:error, :network_issue} ->
Logger.error("Network issue during during self repair",
transaction_address: Base.encode16(address),
transaction_type: type
)
end
end

defp fetch_transaction([node | rest], address) do
case P2P.send_message(node, %GetTransaction{address: address}) do
{:ok, tx = %Transaction{}} ->
{:ok, tx}

_ ->
fetch_transaction(rest, address)
end
end

defp fetch_transaction([], _) do
{:error, :network_issue}
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule ArchEthic.MixProject do
def project do
[
app: :archethic,
version: "0.12.0",
version: "0.12.1",
build_path: "_build",
config_path: "config/config.exs",
deps_path: "deps",
Expand Down

0 comments on commit f744226

Please sign in to comment.