Skip to content

Commit

Permalink
update enforce resync
Browse files Browse the repository at this point in the history
  • Loading branch information
apoorv-2204 committed Aug 19, 2022
1 parent d607e8e commit 1befdf3
Showing 1 changed file with 41 additions and 17 deletions.
58 changes: 41 additions & 17 deletions lib/archethic/bootstrap.ex
Original file line number Diff line number Diff line change
Expand Up @@ -208,34 +208,58 @@ defmodule Archethic.Bootstrap do
Logger.info("Synchronization finished")
end

resync_network_chain([:oracle, :node_shared_secrets])
nodes = P2P.authorized_and_available_nodes()
resync_network_chain([:oracle, :node_shared_secrets], nodes)

Sync.publish_end_of_sync()
SelfRepair.start_scheduler()
:persistent_term.put(:archethic_up, :up)
end

def resync_network_chain(type_list) do
nodes = P2P.authorized_and_available_nodes()
def resync_network_chain(_type_list, _nodes = []),
do: Logger.info("Enforced Resync of Network Txs: failure, No-Nodes")

def resync_network_chain(type_list, nodes) when is_list(nodes) do
Task.Supervisor.async_stream_nolink(Archethic.TaskSupervisor, type_list, fn type ->
if addr = TransactionChain.list_addresses_by_type(type) |> Enum.at(0) do
with {:ok, last_addr} <- TransactionChain.fetch_last_address_remotely(addr, nodes),
{:ok, last_tx} <- TransactionChain.fetch_transaction_remotely(last_addr, nodes),
:ok <- Replication.validate_and_store_transaction_chain(last_tx) do
Logger.info("Enforced Resync of Network Txs: Success", type: type)
else
error ->
Logger.info("Enforced Resync of Network Txs: failure",
error: error,
transaction_type: type
)
end
else
Logger.info("Enforced Resync of Network Txs: failure", transaction_type: type)
case TransactionChain.list_addresses_by_type(type) |> Enum.at(0) do
nil ->
Logger.info("Enforced Resync of Network Txs: failure , Tx_addr: NA , type: #{type}")

:ok

addr ->
last_addr = TransactionChain.get_last_address(addr)

try do
TransactionChain.stream_remotely(last_addr, nodes, last_addr)
|> Enum.to_list()
|> List.flatten()
|> Enum.each(fn tx ->
if last_addr != tx.address do
Logger.info(
"Enforced Resyncing #{type} Network Tx: , #{tx.address |> Base.encode16()}"
)

Replication.validate_and_store_transaction(tx)
end
end)

:ok
rescue
e ->
Logger.info(
"Enforced Resync of Network Txs: failure, error in fetching Network Tx Chain #{type}"
)

Logger.debug(e)

:ok
end
end
end)
|> Stream.run()

Logger.info("Enforced Resync of Network Txs: exiting")
end

defp first_initialization(
Expand Down

0 comments on commit 1befdf3

Please sign in to comment.