Skip to content

Commit

Permalink
Fix node shared secrets schedulers (#546)
Browse files Browse the repository at this point in the history
* Fix last network address in verification tx
* Fix node shared secrets scheduler indexing
  • Loading branch information
samuelmanzanera committed Aug 31, 2022
1 parent ce3c6db commit f12fc94
Show file tree
Hide file tree
Showing 5 changed files with 359 additions and 19 deletions.
14 changes: 8 additions & 6 deletions lib/archethic/mining/pending_transaction_validation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,16 @@ defmodule Archethic.Mining.PendingTransactionValidation do
) do
last_scheduling_date = Reward.get_last_scheduling_date(validation_time)

network_pool_address = SharedSecrets.get_network_pool_address()
genesis_address = DB.list_addresses_by_type(:mint_rewards) |> Stream.take(1) |> Enum.at(0)
{last_network_pool_address, _} = DB.get_last_chain_address(genesis_address)

previous_address = Transaction.previous_address(tx)

time_validation =
with {:ok, %Transaction{type: :node_rewards}} <-
TransactionChain.get_transaction(previous_address, [:type]),
{^network_pool_address, _} <-
DB.get_last_chain_address(network_pool_address, last_scheduling_date) do
{^last_network_pool_address, _} <-
DB.get_last_chain_address(genesis_address, last_scheduling_date) do
:ok
else
{:ok, %Transaction{type: :mint_rewards}} ->
Expand Down Expand Up @@ -505,12 +506,13 @@ defmodule Archethic.Mining.PendingTransactionValidation do
_
) do
total_fee = DB.get_latest_burned_fees()
genesis_address = DB.list_addresses_by_type(:mint_rewards) |> Stream.take(1) |> Enum.at(0)
{last_address, _} = DB.get_last_chain_address(genesis_address)

with :ok <- verify_token_creation(content),
{:ok, %{"supply" => ^total_fee}} <- Jason.decode(content),
network_pool_address <- SharedSecrets.get_network_pool_address(),
{^network_pool_address, _} <-
DB.get_last_chain_address(network_pool_address, Reward.last_scheduling_date()) do
{^last_address, _} <-
DB.get_last_chain_address(genesis_address, Reward.last_scheduling_date()) do
:ok
else
{:ok, %{"supply" => _}} ->
Expand Down
11 changes: 5 additions & 6 deletions lib/archethic/shared_secrets/node_renewal.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,18 @@ defmodule Archethic.SharedSecrets.NodeRenewal do
@doc """
Determine if the local node is the initiator of the node renewal
"""
@spec initiator? :: boolean()
def initiator?(index \\ 0) do
@spec initiator?(binary) :: boolean()
def initiator?(address, index \\ 0) do
%Node{first_public_key: initiator_key} =
next_address()
address
|> Election.storage_nodes(P2P.authorized_and_available_nodes())
|> Enum.at(index)

initiator_key == Crypto.first_node_public_key()
end

@spec next_address() :: binary()
def next_address do
key_index = Crypto.number_of_node_shared_secrets_keys()
@spec next_address(non_neg_integer()) :: binary()
def next_address(key_index) do
next_public_key = Crypto.node_shared_secrets_public_key(key_index + 1)
Crypto.derive_address(next_public_key)
end
Expand Down
8 changes: 4 additions & 4 deletions lib/archethic/shared_secrets/node_renewal_scheduler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ defmodule Archethic.SharedSecrets.NodeRenewalScheduler do
end
end

def handle_event(:internal, :schedule, _state, data = %{interval: interval}) do
def handle_event(:internal, :schedule, _state, data = %{interval: interval, index: index}) do
timer =
case Map.get(data, :timer) do
nil ->
Expand All @@ -85,7 +85,7 @@ defmodule Archethic.SharedSecrets.NodeRenewalScheduler do
new_data =
data
|> Map.put(:timer, timer)
|> Map.put(:next_address, NodeRenewal.next_address())
|> Map.put(:next_address, NodeRenewal.next_address(index))

{:next_state, :scheduled, new_data}
end
Expand Down Expand Up @@ -176,14 +176,14 @@ defmodule Archethic.SharedSecrets.NodeRenewalScheduler do
index
)

if NodeRenewal.initiator?() do
if NodeRenewal.initiator?(tx.address) do
Logger.info("Node shared secrets renewal creation...")
make_renewal(tx)
{:keep_state, data}
else
{:ok, pid} =
DetectNodeResponsiveness.start_link(tx.address, fn count ->
if NodeRenewal.initiator?(count) do
if NodeRenewal.initiator?(tx.address, count) do
Logger.info("Node shared secret renewal creation...attempt #{count}")
make_renewal(tx)
end
Expand Down
Loading

0 comments on commit f12fc94

Please sign in to comment.