Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter p2p view size on during aggregation of summaries #1573

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 20 additions & 41 deletions lib/archethic/beacon_chain/summary_aggregate.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ defmodule Archethic.BeaconChain.SummaryAggregate do

alias Archethic.Crypto

alias Archethic.BeaconChain.Subset.P2PSampling
alias Archethic.BeaconChain.ReplicationAttestation
alias Archethic.BeaconChain.Summary, as: BeaconSummary

Expand Down Expand Up @@ -86,7 +85,6 @@ defmodule Archethic.BeaconChain.SummaryAggregate do
],
fn prev ->
add_p2p_availabilities(
subset,
prev,
node_availabilities,
node_average_availabilities,
Expand All @@ -101,46 +99,20 @@ defmodule Archethic.BeaconChain.SummaryAggregate do
end

defp add_p2p_availabilities(
subset,
map,
node_availabilities,
node_average_availabilities,
end_of_node_synchronizations,
network_patches
) do
map =
map
|> Map.update!(
:end_of_node_synchronizations,
&Enum.concat(&1, end_of_node_synchronizations)
)
|> Map.update!(
:network_patches,
&Enum.concat(&1, [network_patches])
)

expected_subset_length = P2PSampling.list_nodes_to_sample(subset) |> Enum.count()

map =
if bit_size(node_availabilities) == expected_subset_length do
map
|> Map.update!(
:node_availabilities,
&Enum.concat(&1, [Utils.bitstring_to_integer_list(node_availabilities)])
)
else
map
end

if Enum.count(node_average_availabilities) == expected_subset_length do
map
|> Map.update!(
:node_average_availabilities,
&Enum.concat(&1, [node_average_availabilities])
)
else
map
end
map
|> Map.update!(:end_of_node_synchronizations, &Enum.concat(&1, end_of_node_synchronizations))
|> Map.update!(:network_patches, &Enum.concat(&1, [network_patches]))
|> Map.update!(
:node_availabilities,
&Enum.concat(&1, [Utils.bitstring_to_integer_list(node_availabilities)])
)
|> Map.update!(:node_average_availabilities, &Enum.concat(&1, [node_average_availabilities]))
end

@doc """
Expand Down Expand Up @@ -170,7 +142,7 @@ defmodule Archethic.BeaconChain.SummaryAggregate do
|> Map.update!(:node_availabilities, &aggregate_node_availabilities/1)
|> Map.update!(:node_average_availabilities, &aggregate_node_average_availabilities/1)
|> Map.update!(:end_of_node_synchronizations, &Enum.uniq/1)
|> Map.update!(:network_patches, &aggregate_network_patches(&1, subset))}
|> Map.update!(:network_patches, &aggregate_network_patches/1)}
end)
|> Enum.into(%{})
end)
Expand Down Expand Up @@ -205,6 +177,7 @@ defmodule Archethic.BeaconChain.SummaryAggregate do

defp aggregate_node_availabilities(node_availabilities) do
node_availabilities
|> filter_p2p_view_size_by_frequency()
|> Enum.zip()
|> Enum.map(&Tuple.to_list/1)
|> Enum.map(fn availabilities ->
Expand All @@ -226,18 +199,17 @@ defmodule Archethic.BeaconChain.SummaryAggregate do

defp aggregate_node_average_availabilities(avg_availabilities) do
avg_availabilities
|> filter_p2p_view_size_by_frequency()
|> Enum.zip()
|> Enum.map(&Tuple.to_list/1)
|> Enum.map(fn avg_availabilities ->
Float.round(Enum.sum(avg_availabilities) / length(avg_availabilities), 3)
end)
end

defp aggregate_network_patches(network_patches, subset) do
sampling_nodes = P2PSampling.list_nodes_to_sample(subset)

defp aggregate_network_patches(network_patches) do
network_patches
|> Enum.filter(&(length(&1) == length(sampling_nodes)))
|> filter_p2p_view_size_by_frequency()
|> Enum.zip()
|> Enum.map(fn network_patches ->
network_patches
Expand All @@ -248,6 +220,13 @@ defmodule Archethic.BeaconChain.SummaryAggregate do
|> List.flatten()
end

defp filter_p2p_view_size_by_frequency(list) do
list
|> Enum.group_by(&length/1)
|> Enum.max_by(&(elem(&1, 1) |> length()), fn -> {nil, []} end)
|> elem(1)
end

defp resolve_patches_conflicts([patch]), do: patch

defp resolve_patches_conflicts(conflicts_patches) do
Expand Down
30 changes: 27 additions & 3 deletions test/archethic/beacon_chain/summary_aggregate_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,30 @@ defmodule Archethic.BeaconChain.SummaryAggregateTest do
}
|> SummaryAggregate.aggregate()
end

test "should keep the node availabilities with the maximum frequency of list size" do
assert %SummaryAggregate{
p2p_availabilities: %{
<<0>> => %{
node_availabilities: <<1::1, 0::1>>,
node_average_availabilities: [0.93, 0.65],
end_of_node_synchronizations: [],
network_patches: ["BA6", "DEF"]
}
}
} =
%SummaryAggregate{
p2p_availabilities: %{
<<0>> => %{
node_availabilities: [[1, 0], [1, 0], [0]],
node_average_availabilities: [[0.95, 0.7], [0.91, 0.6], [0.4]],
end_of_node_synchronizations: [],
network_patches: [["ABC", "DEF"], ["C90", "DEF"], ["FFF"]]
}
}
}
|> SummaryAggregate.aggregate()
end
end

describe "add_summary/2" do
Expand Down Expand Up @@ -171,7 +195,7 @@ defmodule Archethic.BeaconChain.SummaryAggregateTest do
} = SummaryAggregate.add_summary(aggregate, summary)
end

test "should not add p2p view when summary one is invalid", %{
test "should add p2p view with any size of list size", %{
aggregate: aggregate = %SummaryAggregate{replication_attestations: previous_attestations},
attestation2: attestation2
} do
Expand All @@ -187,8 +211,8 @@ defmodule Archethic.BeaconChain.SummaryAggregateTest do
replication_attestations: [^attestation2 | ^previous_attestations],
p2p_availabilities: %{
<<0>> => %{
node_availabilities: [[1, 0]],
node_average_availabilities: [[0.95, 0.7]],
node_availabilities: [[1, 0], [1]],
node_average_availabilities: [[0.95, 0.7], [0.8]],
end_of_node_synchronizations: [],
network_patches: [["ABC", "DEF"], ["DEF", "ABC"]]
}
Expand Down
Loading