diff --git a/lib/archethic/bootstrap.ex b/lib/archethic/bootstrap.ex index 32a890396e..7a36dcf291 100644 --- a/lib/archethic/bootstrap.ex +++ b/lib/archethic/bootstrap.ex @@ -19,6 +19,7 @@ defmodule Archethic.Bootstrap do require Logger use Task + alias Archethic.TransactionChain @doc """ Start the bootstrapping as a task @@ -206,12 +207,48 @@ defmodule Archethic.Bootstrap do Logger.info("Synchronization finished") end + resync_network_chain([:oracle, :node_shared_secrets]) + Sync.publish_end_of_sync() SelfRepair.start_scheduler() - :persistent_term.put(:archethic_up, :up) end + def resync_network_chain(type_list) do + # Bootstrap process should not crash + try do + nodes = Archethic.P2P.authorized_and_available_nodes() + Logger.debug("Resync Starting") + + Archethic.Utils.async_no_link_pmap( + type_list, + &fn type = &1 -> + Logger.debug("Resync #{type}") + + try do + TransactionChain.list_addresses_by_type(type) + |> Enum.at(0) + |> TransactionChain.fetch_last_address_remotely(nodes) + |> elem(1) + |> TransactionChain.fetch_transaction_remotely(nodes) + |> elem(1) + |> Archethic.Replication.validate_and_store_transaction_chain() + + :ok + rescue + error -> + Logger.debug("Resync of chain #{type} failed #{error} ") + :ok + end + end + ) + rescue + error -> + Logger.debug("Resync failed nodes NA. #{error}") + :ok + end + end + defp first_initialization( ip, port, diff --git a/lib/archethic/utils.ex b/lib/archethic/utils.ex index cff2a669c3..5b8821c637 100644 --- a/lib/archethic/utils.ex +++ b/lib/archethic/utils.ex @@ -680,4 +680,11 @@ defmodule Archethic.Utils do end) end) end + + @spec async_no_link_pmap(any, any) :: list + def async_no_link_pmap(collection, func) do + collection + |> Enum.map(&Task.Supervisor.async_nolink(Archethic.TaskSupervisor, fn -> func.(&1) end)) + |> Task.await_many() + end end