Skip to content

Commit

Permalink
Remove availability_history from Node struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Neylix committed Jun 15, 2023
1 parent a4e8c2d commit 16d3bc0
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 225 deletions.
20 changes: 10 additions & 10 deletions lib/archethic/mining/validation_context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,20 @@ defmodule Archethic.Mining.ValidationContext do
iex> ValidationContext.new(
...> transaction: %Transaction{},
...> welcome_node: %Node{last_public_key: "key1", availability_history: <<1::1>>},
...> coordinator_node: %Node{last_public_key: "key2", availability_history: <<1::1>>},
...> cross_validation_nodes: [%Node{last_public_key: "key3", availability_history: <<1::1>>}],
...> chain_storage_nodes: [%Node{last_public_key: "key4", availability_history: <<1::1>>}, %Node{last_public_key: "key5", availability_history: <<1::1>>}],
...> beacon_storage_nodes: [%Node{last_public_key: "key6", availability_history: <<1::1>>}, %Node{last_public_key: "key7", availability_history: <<1::1>>}]
...> welcome_node: %Node{last_public_key: "key1"},
...> coordinator_node: %Node{last_public_key: "key2"},
...> cross_validation_nodes: [%Node{last_public_key: "key3"}],
...> chain_storage_nodes: [%Node{last_public_key: "key4"}, %Node{last_public_key: "key5"}],
...> beacon_storage_nodes: [%Node{last_public_key: "key6"}, %Node{last_public_key: "key7"}]
...> )
%ValidationContext{
transaction: %Transaction{},
welcome_node: %Node{last_public_key: "key1", availability_history: <<1::1>>},
coordinator_node: %Node{last_public_key: "key2", availability_history: <<1::1>>},
cross_validation_nodes: [%Node{last_public_key: "key3", availability_history: <<1::1>>}],
welcome_node: %Node{last_public_key: "key1"},
coordinator_node: %Node{last_public_key: "key2"},
cross_validation_nodes: [%Node{last_public_key: "key3"}],
cross_validation_nodes_confirmation: <<0::1>>,
chain_storage_nodes: [%Node{last_public_key: "key4", availability_history: <<1::1>>}, %Node{last_public_key: "key5", availability_history: <<1::1>>}],
beacon_storage_nodes: [%Node{last_public_key: "key6", availability_history: <<1::1>>}, %Node{last_public_key: "key7", availability_history: <<1::1>>}]
chain_storage_nodes: [%Node{last_public_key: "key4"}, %Node{last_public_key: "key5"}],
beacon_storage_nodes: [%Node{last_public_key: "key6"}, %Node{last_public_key: "key7"}]
}
"""
@spec new(opts :: Keyword.t()) :: t()
Expand Down
5 changes: 0 additions & 5 deletions lib/archethic/p2p/client/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ defmodule Archethic.P2P.Client.Connection do
alias Archethic.P2P.Client.ConnectionRegistry
alias Archethic.P2P.Client.ConnectionSupervisor

alias Archethic.P2P.MemTable
alias Archethic.P2P.Message
alias Archethic.P2P.MessageEnvelop

Expand Down Expand Up @@ -150,7 +149,6 @@ defmodule Archethic.P2P.Client.Connection do
) do
Logger.warning("Connection closed", node: Base.encode16(node_public_key))

MemTable.decrease_node_availability(node_public_key)
ConnectionSupervisor.set_node_disconnected(node_public_key)

# Stop availability timer
Expand Down Expand Up @@ -185,7 +183,6 @@ defmodule Archethic.P2P.Client.Connection do
{:connected, _socket},
data = %{node_public_key: node_public_key}
) do
MemTable.increase_node_availability(node_public_key)
ConnectionSupervisor.set_node_connected(node_public_key)

# Start availability timer
Expand Down Expand Up @@ -347,7 +344,6 @@ defmodule Archethic.P2P.Client.Connection do
message_id: msg_id
)

MemTable.decrease_node_availability(node_public_key)
ConnectionSupervisor.set_node_disconnected(node_public_key)

# Stop availability timer
Expand Down Expand Up @@ -455,7 +451,6 @@ defmodule Archethic.P2P.Client.Connection do
{:next_state, :disconnected, data}

{:ok, msg} ->
MemTable.increase_node_availability(node_public_key)
ConnectionSupervisor.set_node_connected(node_public_key)

# Start availability timer
Expand Down
138 changes: 28 additions & 110 deletions lib/archethic/p2p/mem_table.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@ defmodule Archethic.P2P.MemTable do
geo_patch: 6,
network_patch: 7,
average_availability: 8,
availability_history: 9,
enrollment_date: 10,
transport: 11,
reward_address: 12,
last_address: 13,
origin_public_key: 14,
synced?: 15,
last_update_date: 16,
available?: 17,
availability_update: 18
enrollment_date: 9,
transport: 10,
reward_address: 11,
last_address: 12,
origin_public_key: 13,
synced?: 14,
last_update_date: 15,
available?: 16,
availability_update: 17
]

@doc """
Expand All @@ -55,6 +54,21 @@ defmodule Archethic.P2P.MemTable do
{:ok, []}
end

def code_change("1.1.1", state, _extra) do
# This match_spec remove the key :"$9" which is the availability_history
match_spec = [
{{:"$1", :"$2", :"$3", :"$4", :"$5", :"$6", :"$7", :"$8", :"$9", :"$10", :"$11", :"$12",
:"$13", :"$14", :"$15", :"$16", :"$17", :"$18"}, [],
[
{{:"$1", :"$2", :"$3", :"$4", :"$5", :"$6", :"$7", :"$8", :"$10", :"$11", :"$12", :"$13",
:"$14", :"$15", :"$16", :"$17", :"$18"}}
]}
]

:ets.select_replace(@discovery_table, match_spec)
{:ok, state}
end

@doc """
Add a node into the P2P view.
Expand All @@ -71,7 +85,6 @@ defmodule Archethic.P2P.MemTable do
...> last_public_key: "key2",
...> geo_patch: "AFZ",
...> network_patch: "AAA",
...> availability_history: <<1::1, 1::1>>,
...> average_availability: 0.9,
...> available?: true,
...> synced?: true,
Expand All @@ -97,7 +110,7 @@ defmodule Archethic.P2P.MemTable do
{
# Discovery table
[{
"key1", "key2", {127, 0, 0, 1}, 3000, 4000, "AFZ", "AAA", 0.9, <<1::1, 1::1>>, ~U[2020-10-22 23:19:45.797109Z], :tcp,
"key1", "key2", {127, 0, 0, 1}, 3000, 4000, "AFZ", "AAA", 0.9, ~U[2020-10-22 23:19:45.797109Z], :tcp,
<<0, 163, 237, 233, 93, 14, 241, 241, 8, 144, 218, 105, 16, 138, 243, 223, 17, 182,
87, 9, 7, 53, 146, 174, 125, 5, 244, 42, 35, 209, 142, 24, 164>>,
<<0, 165, 32, 187, 102, 112, 133, 38, 17, 232, 54, 228, 173, 254, 94, 179, 32, 173,
Expand All @@ -123,7 +136,6 @@ defmodule Archethic.P2P.MemTable do
...> last_public_key: "key2",
...> geo_patch: "AFZ",
...> network_patch: "AAA",
...> availability_history: <<1::1, 1::1>>,
...> average_availability: 0.9,
...> available?: true,
...> synced?: true,
Expand All @@ -148,7 +160,6 @@ defmodule Archethic.P2P.MemTable do
...> first_public_key: "key1",
...> last_public_key: "key5",
...> average_availability: 90,
...> availability_history: <<1::1, 1::1>>,
...> last_update_date: ~U[2020-10-22 23:20:45.797109Z],
...> synced?: false,
...> availability_update: ~U[2020-10-23 23:20:45.797109Z],
Expand All @@ -171,7 +182,6 @@ defmodule Archethic.P2P.MemTable do
"AFZ",
"AAA",
90,
<<1::1, 1::1>>,
~U[2020-10-22 23:19:45.797109Z],
:sctp,
<<0, 163, 237, 233, 93, 14, 241, 241, 8, 144, 218, 105, 16, 138, 243, 223, 17, 182, 87, 9, 7, 53, 146, 174, 125, 5, 244, 42, 35, 209, 142, 24, 164>>,
Expand Down Expand Up @@ -230,7 +240,6 @@ defmodule Archethic.P2P.MemTable do
enrollment_date: enrollment_date,
synced?: synced?,
average_availability: average_availability,
availability_history: availability_history,
transport: transport,
reward_address: reward_address,
last_address: last_address,
Expand All @@ -239,17 +248,11 @@ defmodule Archethic.P2P.MemTable do
available?: available?,
availability_update: availability_update
}) do
availability_history =
if first_public_key == Crypto.first_node_public_key(),
do: <<1::1>>,
else: availability_history

:ets.insert(
@discovery_table,
{first_public_key, last_public_key, ip, port, http_port, geo_patch, network_patch,
average_availability, availability_history, enrollment_date, transport, reward_address,
last_address, origin_public_key, synced?, last_update_date, available?,
availability_update}
average_availability, enrollment_date, transport, reward_address, last_address,
origin_public_key, synced?, last_update_date, available?, availability_update}
)
end

Expand Down Expand Up @@ -836,86 +839,6 @@ defmodule Archethic.P2P.MemTable do
:ok
end

@doc """
Set the node as available if previously flagged as offline
## Examples
iex> MemTable.start_link()
iex> node = %Node{
...> ip: {127, 0, 0, 1},
...> port: 3000,
...> http_port: 4000,
...> first_public_key: "key1",
...> last_public_key: "key2",
...> availability_history: <<0::1>>
...> }
iex> MemTable.add_node(node)
iex> :ok = MemTable.increase_node_availability("key1")
iex> {:ok, %Node{availability_history: <<1::1, 0::1>>}} = MemTable.get_node("key1")
"""
@spec increase_node_availability(first_public_key :: Crypto.key()) :: :ok
def increase_node_availability(first_public_key) when is_binary(first_public_key) do
if :ets.member(@discovery_table, first_public_key) do
tuple_pos = Keyword.fetch!(@discovery_index_position, :availability_history)

case :ets.lookup_element(@discovery_table, first_public_key, tuple_pos) do
<<1::1, _::bitstring>> ->
:ok

<<0::1, _::bitstring>> = history ->
new_history = <<1::1, history::bitstring>>
true = :ets.update_element(@discovery_table, first_public_key, {tuple_pos, new_history})
Logger.info("P2P availability increase", node: Base.encode16(first_public_key))
notify_node_update(first_public_key)
:ok
end
else
:ok
end
end

@doc """
Set the node as unavailable if previously flagged as online
## Examples
iex> MemTable.start_link()
iex> node = %Node{
...> ip: {127, 0, 0, 1},
...> port: 3000,
...> http_port: 4000,
...> first_public_key: "key1",
...> last_public_key: "key2",
...> availability_history: <<1::1>>
...> }
iex> MemTable.add_node(node)
iex> :ok = MemTable.decrease_node_availability("key1")
iex> {:ok, %Node{availability_history: <<0::1, 1::1>>}} = MemTable.get_node("key1")
"""
@spec decrease_node_availability(first_public_key :: Crypto.key()) :: :ok
def decrease_node_availability(first_public_key) when is_binary(first_public_key) do
if :ets.member(@discovery_table, first_public_key) do
tuple_pos = Keyword.fetch!(@discovery_index_position, :availability_history)

case :ets.lookup_element(@discovery_table, first_public_key, tuple_pos) do
<<0::1, _::bitstring>> ->
:ok

<<1::1, _::bitstring>> = history ->
new_history = <<0::1, history::bitstring>>

true = :ets.update_element(@discovery_table, first_public_key, {tuple_pos, new_history})

Logger.info("P2P availability decrease", node: Base.encode16(first_public_key))
notify_node_update(first_public_key)
:ok
end
else
:ok
end
end

@doc """
Update the average availability of the node and reset the history
Expand All @@ -941,15 +864,10 @@ defmodule Archethic.P2P.MemTable do
def update_node_average_availability(first_public_key, avg_availability)
when is_binary(first_public_key) and is_float(avg_availability) do
avg_availability_pos = Keyword.fetch!(@discovery_index_position, :average_availability)
availability_history_pos = Keyword.fetch!(@discovery_index_position, :availability_history)

<<last_history::1, _rest::bitstring>> =
:ets.lookup_element(@discovery_table, first_public_key, availability_history_pos)

true =
:ets.update_element(@discovery_table, first_public_key, [
{avg_availability_pos, avg_availability},
{availability_history_pos, <<last_history::1>>}
{avg_availability_pos, avg_availability}
])

Logger.info("New average availability: #{avg_availability}}",
Expand Down
8 changes: 2 additions & 6 deletions lib/archethic/p2p/node.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ defmodule Archethic.P2P.Node do
available?: false,
synced?: false,
average_availability: 1.0,
availability_history: <<0::1>>,
authorized?: false,
authorization_date: nil,
transport: :tcp,
Expand Down Expand Up @@ -178,7 +177,6 @@ defmodule Archethic.P2P.Node do
available?: boolean(),
synced?: boolean(),
average_availability: float(),
availability_history: bitstring(),
authorized?: boolean(),
enrollment_date: nil | DateTime.t(),
authorization_date: nil | DateTime.t(),
Expand All @@ -193,9 +191,8 @@ defmodule Archethic.P2P.Node do
@spec cast(tuple()) :: __MODULE__.t()
def cast(
{first_public_key, last_public_key, ip, port, http_port, geo_patch, network_patch,
average_availability, availability_history, enrollment_date, transport, reward_address,
last_address, origin_public_key, synced?, last_update_date, available?,
availability_update}
average_availability, enrollment_date, transport, reward_address, last_address,
origin_public_key, synced?, last_update_date, available?, availability_update}
) do
%__MODULE__{
ip: ip,
Expand All @@ -206,7 +203,6 @@ defmodule Archethic.P2P.Node do
geo_patch: geo_patch,
network_patch: network_patch,
average_availability: average_availability,
availability_history: availability_history,
enrollment_date: enrollment_date,
synced?: synced?,
transport: transport,
Expand Down
12 changes: 4 additions & 8 deletions test/archethic/beacon_chain_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ defmodule Archethic.BeaconChainTest do
available?: true,
authorization_date: summary_time |> DateTime.add(-10),
authorized?: true,
reward_address: <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>,
availability_history: <<1::1>>
reward_address: <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>
}

node2 = %Node{
Expand All @@ -135,8 +134,7 @@ defmodule Archethic.BeaconChainTest do
available?: true,
authorization_date: summary_time |> DateTime.add(-10),
authorized?: true,
reward_address: <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>,
availability_history: <<1::1>>
reward_address: <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>
}

node3 = %Node{
Expand All @@ -149,8 +147,7 @@ defmodule Archethic.BeaconChainTest do
available?: true,
authorization_date: summary_time |> DateTime.add(-10),
authorized?: true,
reward_address: <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>,
availability_history: <<1::1>>
reward_address: <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>
}

node4 = %Node{
Expand All @@ -163,8 +160,7 @@ defmodule Archethic.BeaconChainTest do
available?: true,
authorization_date: summary_time |> DateTime.add(-10),
authorized?: true,
reward_address: <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>,
availability_history: <<1::1>>
reward_address: <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>
}

P2P.add_and_connect_node(node1)
Expand Down
3 changes: 0 additions & 3 deletions test/archethic/bootstrap/sync_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,6 @@ defmodule Archethic.Bootstrap.SyncTest do
http_port: 4000,
first_public_key: "key1",
last_public_key: "key1",
availability_history: <<1::1>>,
authorized?: true,
available?: true,
authorization_date: DateTime.utc_now(),
Expand All @@ -372,7 +371,6 @@ defmodule Archethic.Bootstrap.SyncTest do
enrollment_date: DateTime.utc_now(),
authorized?: true,
available?: true,
availability_history: <<1::1>>,
network_patch: "AAA"
}

Expand All @@ -383,7 +381,6 @@ defmodule Archethic.Bootstrap.SyncTest do
first_public_key: "key2",
last_public_key: "key2",
authorized?: true,
availability_history: <<1::1>>,
available?: true
}

Expand Down
Loading

0 comments on commit 16d3bc0

Please sign in to comment.