Skip to content

Commit

Permalink
Fix notifier genesis_address in ShardRepair msg (#1551)
Browse files Browse the repository at this point in the history
  • Loading branch information
Neylix authored Aug 20, 2024
1 parent f7ebcd2 commit 455e4e2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 29 deletions.
32 changes: 14 additions & 18 deletions lib/archethic/p2p/message/shard_repair.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,21 +14,21 @@ 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())
}

@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
Expand All @@ -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:
Expand All @@ -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
<<first_address::binary, 0::1, VarInt.from_value(length(io_addresses))::binary,
<<genesis_address::binary, 0::1, VarInt.from_value(length(io_addresses))::binary,
:erlang.list_to_binary(io_addresses)::binary>>
end

def serialize(%__MODULE__{
first_address: first_address,
genesis_address: genesis_address,
storage_address: storage_address,
io_addresses: io_addresses
}) do
<<first_address::binary, 1::1, storage_address::binary,
<<genesis_address::binary, 1::1, storage_address::binary,
VarInt.from_value(length(io_addresses))::binary,
:erlang.list_to_binary(io_addresses)::binary>>
end
Expand All @@ -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:
Expand All @@ -111,21 +111,17 @@ defmodule Archethic.P2P.Message.ShardRepair do
"""
def deserialize(bin) do
{first_address, <<storage_address?::1, rest::bitstring>>} = Utils.deserialize_address(bin)
{genesis_address, <<storage_address?::1, rest::bitstring>>} = 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}
Expand Down
15 changes: 6 additions & 9 deletions lib/archethic/self_repair/notifier.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -133,7 +130,7 @@ defmodule Archethic.SelfRepair.Notifier do
|> Stream.filter(&notify?(&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(
Expand Down Expand Up @@ -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,
Expand All @@ -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
})
Expand Down
2 changes: 1 addition & 1 deletion test/archethic/p2p/messages_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>,
Expand Down
2 changes: 1 addition & 1 deletion test/archethic/self_repair/notifier_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 455e4e2

Please sign in to comment.