From bb5dfa3e26170c030f57ff2a656890ee867f20cd Mon Sep 17 00:00:00 2001 From: Neylix Date: Mon, 6 Feb 2023 15:08:49 +0100 Subject: [PATCH] Fix bootstrap node index --- lib/archethic/bootstrap.ex | 72 +++++++++++++++---------------- lib/archethic/bootstrap/sync.ex | 14 ++++-- test/archethic/bootstrap_test.exs | 3 ++ 3 files changed, 49 insertions(+), 40 deletions(-) diff --git a/lib/archethic/bootstrap.ex b/lib/archethic/bootstrap.ex index 7eb13ba096..8de340e79d 100644 --- a/lib/archethic/bootstrap.ex +++ b/lib/archethic/bootstrap.ex @@ -115,7 +115,6 @@ defmodule Archethic.Bootstrap do http_port, transport, bootstrapping_seeds, - last_sync_date, network_patch, reward_address ) @@ -151,25 +150,31 @@ defmodule Archethic.Bootstrap do http_port, transport, bootstrapping_seeds, - last_sync_date, network_patch, reward_address ) do Logger.info("Bootstrapping starting") - if Sync.should_initialize_network?(bootstrapping_seeds) do - Logger.info("This node should initialize the network!!") - Logger.debug("Create first node transaction") + cond do + Sync.should_initialize_network?(bootstrapping_seeds) -> + Logger.info("This node should initialize the network!!") + Logger.debug("Create first node transaction") + + tx = + TransactionHandler.create_node_transaction( + ip, + port, + http_port, + transport, + reward_address + ) - tx = - TransactionHandler.create_node_transaction(ip, port, http_port, transport, reward_address) + Sync.initialize_network(tx) - Sync.initialize_network(tx) + post_bootstrap(sync?: false) + SelfRepair.put_last_sync_date(DateTime.utc_now()) - post_bootstrap(sync?: false) - SelfRepair.put_last_sync_date(DateTime.utc_now()) - else - if Crypto.first_node_public_key() == Crypto.last_node_public_key() do + Crypto.first_node_public_key() == Crypto.previous_node_public_key() -> Logger.info("Node initialization...") first_initialization( @@ -183,25 +188,21 @@ defmodule Archethic.Bootstrap do ) post_bootstrap(sync?: true) - else - if Sync.require_update?(ip, port, http_port, transport, last_sync_date) do - Logger.info("Update node chain...") - update_node( - ip, - port, - http_port, - transport, - network_patch, - bootstrapping_seeds, - reward_address - ) + true -> + Logger.info("Update node chain...") - post_bootstrap(sync?: true) - else - post_bootstrap(sync?: false) - end - end + update_node( + ip, + port, + http_port, + transport, + network_patch, + bootstrapping_seeds, + reward_address + ) + + post_bootstrap(sync?: true) end end @@ -300,15 +301,12 @@ defmodule Archethic.Bootstrap do |> Enum.reject(&(&1.first_public_key == Crypto.first_node_public_key())) # In case node had lose it's DB, we ask the network if the node chain already exists - case Crypto.next_node_public_key() - |> Crypto.derive_address() - |> TransactionChain.fetch_size_remotely(closest_nodes) do - {:ok, length} when length > 0 -> - Crypto.set_node_key_index(length) + {:ok, length} = + Crypto.first_node_public_key() + |> Crypto.derive_address() + |> TransactionChain.fetch_size_remotely(closest_nodes) - _ -> - :skip - end + Crypto.set_node_key_index(length) tx = TransactionHandler.create_node_transaction(ip, port, http_port, transport, reward_address) diff --git a/lib/archethic/bootstrap/sync.ex b/lib/archethic/bootstrap/sync.ex index d35373aa0b..f81b11635f 100644 --- a/lib/archethic/bootstrap/sync.ex +++ b/lib/archethic/bootstrap/sync.ex @@ -182,9 +182,17 @@ defmodule Archethic.Bootstrap.Sync do {:ok, %BootstrappingNodes{closest_nodes: closest_nodes, new_seeds: new_seeds}} -> :ok = P2P.new_bootstrapping_seeds(new_seeds) - (new_seeds ++ closest_nodes) - |> P2P.distinct_nodes() - |> Enum.each(&P2P.add_and_connect_node/1) + closest_nodes = + (new_seeds ++ closest_nodes) + |> P2P.distinct_nodes() + |> Task.async_stream(fn node -> + P2P.add_and_connect_node(node) + # Wait for connection time + Process.sleep(500) + P2P.get_node_info!(node.first_public_key) + end) + |> Enum.filter(&match?({:ok, _}, &1)) + |> Enum.map(fn {:ok, node} -> node end) Logger.info("Closest nodes and seeds loaded in the P2P view") diff --git a/test/archethic/bootstrap_test.exs b/test/archethic/bootstrap_test.exs index bd1ce4a79b..0b2b1e93e9 100644 --- a/test/archethic/bootstrap_test.exs +++ b/test/archethic/bootstrap_test.exs @@ -99,6 +99,9 @@ defmodule Archethic.BootstrapTest do ] end) + MockCrypto + |> stub(:set_node_key_index, fn _ -> :ok end) + start_supervised!(RewardMemTable) start_supervised!(RewardTableLoader)