From e8f0d442dfbb3e51e6b8b923f4067bfecdac7bf1 Mon Sep 17 00:00:00 2001 From: Neylix Date: Mon, 19 Aug 2024 14:56:51 +0200 Subject: [PATCH] Fix notifier genesis_address in ShardRepair msg --- lib/archethic/p2p/message/shard_repair.ex | 32 +++++++++----------- lib/archethic/self_repair/notifier.ex | 15 ++++----- test/archethic/p2p/messages_test.exs | 2 +- test/archethic/self_repair/notifier_test.exs | 2 +- 4 files changed, 22 insertions(+), 29 deletions(-) diff --git a/lib/archethic/p2p/message/shard_repair.ex b/lib/archethic/p2p/message/shard_repair.ex index 8181a9223..549f0b862 100644 --- a/lib/archethic/p2p/message/shard_repair.ex +++ b/lib/archethic/p2p/message/shard_repair.ex @@ -2,8 +2,8 @@ defmodule Archethic.P2P.Message.ShardRepair do @moduledoc """ Inform a shard to start repair. """ - @enforce_keys [:first_address, :storage_address, :io_addresses] - defstruct [:first_address, :storage_address, :io_addresses] + @enforce_keys [:genesis_address, :storage_address, :io_addresses] + defstruct [:genesis_address, :storage_address, :io_addresses] alias Archethic.Crypto alias Archethic.SelfRepair @@ -14,7 +14,7 @@ defmodule Archethic.P2P.Message.ShardRepair do alias Archethic.P2P.Message.Ok @type t :: %__MODULE__{ - first_address: Crypto.prepended_hash(), + genesis_address: Crypto.prepended_hash(), storage_address: Crypto.prepended_hash(), io_addresses: list(Crypto.prepended_hash()) } @@ -22,13 +22,13 @@ defmodule Archethic.P2P.Message.ShardRepair do @spec process(__MODULE__.t(), Crypto.key()) :: Ok.t() def process( %__MODULE__{ - first_address: first_address, + genesis_address: genesis_address, storage_address: storage_address, io_addresses: io_addresses }, _ ) do - SelfRepair.resync(first_address, storage_address, io_addresses) + SelfRepair.resync(genesis_address, storage_address, io_addresses) %Ok{} end @@ -37,7 +37,7 @@ defmodule Archethic.P2P.Message.ShardRepair do Serialize ShardRepair Struct iex> %ShardRepair{ - ...> first_address: + ...> genesis_address: ...> <<0, 0, 94, 5, 249, 103, 126, 31, 43, 57, 25, 14, 187, 133, 59, 234, 201, 172, 3, ...> 195, 43, 81, 81, 146, 164, 202, 147, 218, 207, 204, 31, 185, 73, 251>>, ...> storage_address: @@ -61,20 +61,20 @@ defmodule Archethic.P2P.Message.ShardRepair do 130>> """ def serialize(%__MODULE__{ - first_address: first_address, + genesis_address: genesis_address, storage_address: nil, io_addresses: io_addresses }) do - <> end def serialize(%__MODULE__{ - first_address: first_address, + genesis_address: genesis_address, storage_address: storage_address, io_addresses: io_addresses }) do - <> end @@ -93,7 +93,7 @@ defmodule Archethic.P2P.Message.ShardRepair do ...> |> ShardRepair.deserialize() { %ShardRepair{ - first_address: + genesis_address: <<0, 0, 94, 5, 249, 103, 126, 31, 43, 57, 25, 14, 187, 133, 59, 234, 201, 172, 3, 195, 43, 81, 81, 146, 164, 202, 147, 218, 207, 204, 31, 185, 73, 251>>, storage_address: @@ -111,21 +111,17 @@ defmodule Archethic.P2P.Message.ShardRepair do """ def deserialize(bin) do - {first_address, <>} = Utils.deserialize_address(bin) + {genesis_address, <>} = Utils.deserialize_address(bin) {storage_address, rest} = - if storage_address? == 1 do - Utils.deserialize_address(rest) - else - {nil, rest} - end + if storage_address? == 1, do: Utils.deserialize_address(rest), else: {nil, rest} {io_addresses_length, rest} = VarInt.get_value(rest) {io_addresses, rest} = Utils.deserialize_addresses(rest, io_addresses_length, []) {%__MODULE__{ - first_address: first_address, + genesis_address: genesis_address, storage_address: storage_address, io_addresses: io_addresses }, rest} diff --git a/lib/archethic/self_repair/notifier.ex b/lib/archethic/self_repair/notifier.ex index 269df8a33..f3e158cb1 100644 --- a/lib/archethic/self_repair/notifier.ex +++ b/lib/archethic/self_repair/notifier.ex @@ -96,11 +96,8 @@ defmodule Archethic.SelfRepair.Notifier do defp network_chain?(address) do case TransactionChain.get_transaction(address, [:type]) do - {:ok, %Transaction{type: type}} -> - Transaction.network_type?(type) - - _ -> - false + {:ok, %Transaction{type: type}} -> Transaction.network_type?(type) + _ -> false end end @@ -133,7 +130,7 @@ defmodule Archethic.SelfRepair.Notifier do |> Stream.filter(¬ify?(&1)) |> Stream.map(&new_storage_nodes(&1, new_available_nodes)) |> map_last_addresses_for_node() - |> notify_nodes(address) + |> notify_nodes(genesis_address) end defp get_previous_election( @@ -266,7 +263,7 @@ defmodule Archethic.SelfRepair.Notifier do ) end - defp notify_nodes(acc, first_address) do + defp notify_nodes(acc, genesis_address) do Task.Supervisor.async_stream_nolink( Archethic.TaskSupervisor, acc, @@ -275,11 +272,11 @@ defmodule Archethic.SelfRepair.Notifier do "Send Shard Repair message to #{Base.encode16(node_first_public_key)}" <> "with storage_address #{if last_address, do: Base.encode16(last_address), else: nil}, " <> "io_addresses #{inspect(Enum.map(io_addresses, &Base.encode16(&1)))}", - address: Base.encode16(first_address) + address: Base.encode16(genesis_address) ) P2P.send_message(node_first_public_key, %ShardRepair{ - first_address: first_address, + genesis_address: genesis_address, storage_address: last_address, io_addresses: io_addresses }) diff --git a/test/archethic/p2p/messages_test.exs b/test/archethic/p2p/messages_test.exs index f2ff6016c..74ac435fa 100644 --- a/test/archethic/p2p/messages_test.exs +++ b/test/archethic/p2p/messages_test.exs @@ -871,7 +871,7 @@ defmodule Archethic.P2P.MessageTest do test "%ShardRepair" do msg = %ShardRepair{ - first_address: <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>, + genesis_address: <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>, storage_address: <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>, io_addresses: [ <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>, diff --git a/test/archethic/self_repair/notifier_test.exs b/test/archethic/self_repair/notifier_test.exs index d6b25999d..8e4536005 100644 --- a/test/archethic/self_repair/notifier_test.exs +++ b/test/archethic/self_repair/notifier_test.exs @@ -170,7 +170,7 @@ defmodule Archethic.SelfRepair.NotifierTest do MockClient |> stub(:send_message, fn - node, %ShardRepair{first_address: "Alice1", storage_address: "Alice2"}, _ -> + node, %ShardRepair{genesis_address: "Alice1", storage_address: "Alice2"}, _ -> if Enum.member?(new_possible_nodes, node.first_public_key) do send(me, :new_node) end