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 bootstrap node index #892

Merged
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
72 changes: 35 additions & 37 deletions lib/archethic/bootstrap.ex
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ defmodule Archethic.Bootstrap do
http_port,
transport,
bootstrapping_seeds,
last_sync_date,
network_patch,
reward_address
)
Expand Down Expand Up @@ -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(
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down
14 changes: 11 additions & 3 deletions lib/archethic/bootstrap/sync.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
6 changes: 4 additions & 2 deletions lib/archethic/mining/pending_transaction_validation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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{}

Expand All @@ -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{}} ->
Expand Down
8 changes: 3 additions & 5 deletions lib/archethic/self_repair/sync/transaction_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions test/archethic/bootstrap_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down