-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move forced resync function to self_repair
- Loading branch information
1 parent
a9a40c5
commit 1c03c75
Showing
3 changed files
with
97 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
defmodule Archethic.SelfRepair.ResyncWorker do | ||
@moduledoc """ | ||
A Long Running generver impl, handles a const req of network resync. | ||
""" | ||
@vsn Mix.Project.config()[:version] | ||
|
||
alias Archethic.{Crypto, P2P, TransactionChain, Replication} | ||
|
||
use GenServer, restart: :permanent | ||
require Logger | ||
|
||
# ------------------------------------------- | ||
# API | ||
# ------------------------------------------- | ||
def sync_again(name \\ __MODULE__) do | ||
GenServer.cast(name, :resync_request) | ||
end | ||
|
||
# ------------------------------------------- | ||
# CALLBACKS | ||
# ------------------------------------------- | ||
|
||
def start_link(args \\ [], opts \\ [name: __MODULE__]) do | ||
GenServer.start_link(__MODULE__, args, opts) | ||
end | ||
|
||
def init(_args) do | ||
Logger.notice("[#{inspect(__MODULE__)}] Started") | ||
{:ok, %{scheduled: false}} | ||
end | ||
|
||
def handle_cast(:resync_request, server_data = %{scheduled: true}) do | ||
{:noreply, server_data} | ||
end | ||
|
||
def handle_cast(:resync_request, %{scheduled: false}) do | ||
{:noreply, schedule()} | ||
end | ||
|
||
def handle_info({:DOWN, _ref, :process, pid, _normal}, %{task_pid: task_pid}) | ||
when pid == task_pid do | ||
{:noreply, %{scheduled: false}} | ||
end | ||
|
||
def handle_info(_, data), do: {:noreply, data} | ||
|
||
# ------------------------------------------- | ||
# LOGIC IMPL | ||
# ------------------------------------------- | ||
|
||
defp schedule() do | ||
%Task{pid: pid} = Task.async(fn -> resync(:node_shared_secrets, P2P.exclusive_nodes()) end) | ||
%{task_pid: pid, scheduled: true} | ||
end | ||
|
||
@spec resync(atom(), list(P2P.Node.t()) | []) :: :ok | :error | ||
def resync(_, []), | ||
do: Logger.error("Enforce Resync of Network Txs: No-Nodes") | ||
|
||
def resync(type, nodes) do | ||
with addr when is_binary(addr) <- Crypto.genesis_address(type), | ||
{:ok, rem_last_addr} <- TransactionChain.resolve_last_address(addr), | ||
{local_last_addr, _} <- TransactionChain.get_last_address(addr), | ||
false <- rem_last_addr == local_last_addr, | ||
{:ok, tx} <- TransactionChain.fetch_transaction_remotely(rem_last_addr, nodes), | ||
:ok <- Replication.validate_and_store_transaction_chain(tx) do | ||
Logger.info("Enforced Resync: Success", transaction_type: type) | ||
:ok | ||
else | ||
true -> | ||
Logger.info("Enforced Resync: No new transaction to sync", transaction_type: type) | ||
:ok | ||
|
||
e when e in [nil, []] -> | ||
Logger.debug("Enforced Resync: Transaction not available", transaction_type: type) | ||
:ok | ||
|
||
e -> | ||
Logger.debug("Enforced Resync: Error #{inspect(e)}", transaction_type: type) | ||
end | ||
end | ||
end |