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

Fix network patch with negative eigen value #1639

Merged
merged 1 commit into from
Jan 20, 2025
Merged
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
28 changes: 22 additions & 6 deletions lib/archethic/beacon_chain/network_coordinates.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ defmodule Archethic.BeaconChain.NetworkCoordinates do
alias Archethic.SelfRepair
alias Archethic.Utils

require Logger

@doc """
Return the timeout to determine network patches
It is equivalent to 4m30s in production. 4.5s in dev.
Expand Down Expand Up @@ -126,7 +128,7 @@ defmodule Archethic.BeaconChain.NetworkCoordinates do
top_eigen_values =
sorted_eigen_values
|> Enum.take(2)
|> Enum.map(fn {val, _} -> val end)
|> Enum.map(fn {val, _} -> abs(val) end)
|> Nx.tensor()
|> Nx.sqrt()

Expand Down Expand Up @@ -165,7 +167,7 @@ defmodule Archethic.BeaconChain.NetworkCoordinates do
end)
end

defp get_patch(x_elem, y_elem, v, max) do
defp get_patch(x_elem, y_elem, v, max) when is_number(x_elem) and is_number(y_elem) do
%{x: x, y: y} =
Enum.reduce_while(0..15, %{x: "", y: ""}, fn j, acc ->
if acc.x != "" and acc.y != "" do
Expand All @@ -180,15 +182,29 @@ defmodule Archethic.BeaconChain.NetworkCoordinates do
end
end)

# Default to "0" if no digit was found
x = if x == "", do: "0", else: x
y = if y == "", do: "0", else: y

"#{x}#{y}"
end

defp get_digit(acc, coord_name, coord, digit_index, max, v)
when coord >= max - v * (digit_index + 1.0) and coord <= max - v * digit_index do
Map.put(acc, coord_name, Enum.at(@digits, digit_index))
# Handle NaN values
defp get_patch(_x_elem, _y_elem, _v, _max) do
Logger.warning("Error when calculating network patch, coordinates is NaN")
"00"
end

defp get_digit(acc, _, _, _, _, _), do: acc
defp get_digit(acc, coord_name, coord, digit_index, max, v) do
lower_bound = max - v * (digit_index + 1.0)
upper_bound = max - v * digit_index

if coord >= lower_bound and coord <= upper_bound do
Map.put(acc, coord_name, Enum.at(@digits, digit_index))
else
acc
end
end

@doc """
Fetch remotely the network stats for a given summary time
Expand Down
Loading