Skip to content

Commit

Permalink
fix 🐛 (election): Fix sort of nodes during election's refinement
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmanzanera authored and Neylix committed Dec 21, 2023
1 parent e12e1b8 commit f925ff1
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions lib/archethic/election.ex
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,10 @@ defmodule Archethic.Election do
min_geo_patch,
storage_nodes
) do
tx_hash =
tx
|> Transaction.to_pending()
|> Transaction.serialize()
|> Crypto.hash()
sorted_nodes = sort_validation_nodes(authorized_nodes, tx, sorting_seed)

authorized_nodes
|> sort_validation_nodes_by_key_rotation(sorting_seed, tx_hash)
|> Enum.reduce_while(
Enum.reduce_while(
sorted_nodes,
%{nb_nodes: 0, nodes: [], zones: MapSet.new()},
fn node = %Node{geo_patch: geo_patch, last_public_key: last_public_key}, acc ->
if validation_constraints_satisfied?(
Expand Down Expand Up @@ -215,7 +210,25 @@ defmodule Archethic.Election do
)
|> Map.get(:nodes)
|> Enum.reverse()
|> refine_necessary_nodes(authorized_nodes, nb_validations)
|> refine_necessary_nodes(sorted_nodes, nb_validations)
end

@doc """
Sort the validation nodes with the given sorting seed
"""
@spec sort_validation_nodes(
node_list :: list(Node.t()),
transaction :: Transaction.t(),
sorting_seed :: binary()
) :: list(Node.t())
def sort_validation_nodes(node_list, tx, sorting_seed) do
tx_hash =
tx
|> Transaction.to_pending()
|> Transaction.serialize()
|> Crypto.hash()

sort_validation_nodes_by_key_rotation(node_list, sorting_seed, tx_hash)
end

defp validation_constraints_satisfied?(nb_validations, min_geo_patch, nb_nodes, zones) do
Expand Down

0 comments on commit f925ff1

Please sign in to comment.