Skip to content

Commit

Permalink
Fix node shared secrets scheduler indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel authored and samuelmanzanera committed Aug 31, 2022
1 parent f1bb2bc commit 3b015c7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
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
6 changes: 3 additions & 3 deletions test/archethic/shared_secrets/node_renewal_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ defmodule Archethic.SharedSecrets.NodeRenewalTest do
assert {:ok, _, _} = NodeRenewal.decode_transaction_content(content)
end

describe "initiator?/0" do
describe "initiator?/2" do
test "should return false when the first elected node is not the current node" do
P2P.add_and_connect_node(%Node{
ip: {127, 0, 0, 1},
Expand Down Expand Up @@ -76,7 +76,7 @@ defmodule Archethic.SharedSecrets.NodeRenewalTest do
authorization_date: DateTime.utc_now() |> DateTime.add(-86_400)
})

assert false == NodeRenewal.initiator?()
assert false == NodeRenewal.initiator?("address")
end

test "should return true when the first elected node is the current node" do
Expand All @@ -92,7 +92,7 @@ defmodule Archethic.SharedSecrets.NodeRenewalTest do
authorization_date: DateTime.utc_now() |> DateTime.add(-86_400)
})

assert true == NodeRenewal.initiator?()
assert true == NodeRenewal.initiator?("address")
end
end

Expand Down

0 comments on commit 3b015c7

Please sign in to comment.