Skip to content

Commit

Permalink
Fix bootstrap node index
Browse files Browse the repository at this point in the history
  • Loading branch information
Neylix committed Feb 8, 2023
1 parent 2f97502 commit bb5dfa3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 40 deletions.
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
3 changes: 3 additions & 0 deletions test/archethic/bootstrap_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ defmodule Archethic.BootstrapTest do
]
end)

MockCrypto
|> stub(:set_node_key_index, fn _ -> :ok end)

start_supervised!(RewardMemTable)
start_supervised!(RewardTableLoader)

Expand Down

0 comments on commit bb5dfa3

Please sign in to comment.