diff --git a/lib/archethic/bootstrap.ex b/lib/archethic/bootstrap.ex index 7eb13ba09..8de340e79 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 d35373aa0..f81b11635 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/lib/archethic/mining/pending_transaction_validation.ex b/lib/archethic/mining/pending_transaction_validation.ex index cfa9fbe10..4d0b6eabe 100644 --- a/lib/archethic/mining/pending_transaction_validation.ex +++ b/lib/archethic/mining/pending_transaction_validation.ex @@ -15,6 +15,7 @@ defmodule Archethic.Mining.PendingTransactionValidation do P2P.Message.FirstPublicKey, P2P.Message.GetFirstPublicKey, P2P.Message.GetTransactionSummary, + P2P.Message.TransactionSummaryMessage, P2P.Message.NotFound, P2P.Node, Reward, @@ -113,7 +114,7 @@ defmodule Archethic.Mining.PendingTransactionValidation do conflict_resolver = fn results -> # Prioritize transactions results over not found - case Enum.filter(results, &match?(%TransactionSummary{}, &1)) do + case Enum.filter(results, &match?(%TransactionSummaryMessage{}, &1)) do [] -> %NotFound{} @@ -128,7 +129,8 @@ defmodule Archethic.Mining.PendingTransactionValidation do %GetTransactionSummary{address: address}, conflict_resolver ) do - {:ok, %TransactionSummary{address: ^address}} -> + {:ok, + %TransactionSummaryMessage{transaction_summary: %TransactionSummary{address: ^address}}} -> {:error, "Transaction already exists"} {:ok, %NotFound{}} -> diff --git a/lib/archethic/self_repair/sync/transaction_handler.ex b/lib/archethic/self_repair/sync/transaction_handler.ex index 0c2534a76..80d17b34f 100644 --- a/lib/archethic/self_repair/sync/transaction_handler.ex +++ b/lib/archethic/self_repair/sync/transaction_handler.ex @@ -40,11 +40,9 @@ defmodule Archethic.SelfRepair.Sync.TransactionHandler do true else Enum.any?(mvt_addresses, fn address -> - io_storage_nodes = Election.chain_storage_nodes(address, node_list) - node_pool_address = Crypto.hash(Crypto.last_node_public_key()) - - Utils.key_in_node_list?(io_storage_nodes, Crypto.first_node_public_key()) or - address == node_pool_address + address + |> Election.chain_storage_nodes(node_list) + |> Utils.key_in_node_list?(Crypto.first_node_public_key()) end) end end diff --git a/test/archethic/bootstrap_test.exs b/test/archethic/bootstrap_test.exs index ff688169d..3b31ea32a 100644 --- a/test/archethic/bootstrap_test.exs +++ b/test/archethic/bootstrap_test.exs @@ -97,6 +97,9 @@ defmodule Archethic.BootstrapTest do ] end) + MockCrypto.NodeKeystore + |> stub(:set_node_key_index, fn _ -> :ok end) + start_supervised!(RewardMemTable) start_supervised!(RewardTableLoader)