From 6b27a71eb7c5769584baba57c7b36f395cbc592a Mon Sep 17 00:00:00 2001 From: Neylix Date: Sun, 11 Dec 2022 13:56:42 +0100 Subject: [PATCH] node distribution return more node than just sample_size --- .../transaction_context/node_distribution.ex | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/archethic/mining/transaction_context/node_distribution.ex b/lib/archethic/mining/transaction_context/node_distribution.ex index 08a11339d..db48697a9 100644 --- a/lib/archethic/mining/transaction_context/node_distribution.ex +++ b/lib/archethic/mining/transaction_context/node_distribution.ex @@ -33,9 +33,12 @@ defmodule Archethic.Mining.TransactionContext.NodeDistribution do ...> %Node{first_public_key: "key4"} ...> ], 3, 3) [ - [%Node{first_public_key: "key1"}, %Node{first_public_key: "key4"}, %Node{first_public_key: "key3"}], - [%Node{first_public_key: "key2"}, %Node{first_public_key: "key1"}, %Node{first_public_key: "key4"}], - [%Node{first_public_key: "key3"}, %Node{first_public_key: "key2"}, %Node{first_public_key: "key1"}] + [%Node{first_public_key: "key1"}, %Node{first_public_key: "key4"}, %Node{first_public_key: "key3"}, + %Node{first_public_key: "key2"}], + [%Node{first_public_key: "key2"}, %Node{first_public_key: "key1"}, %Node{first_public_key: "key4"}, + %Node{first_public_key: "key3"}], + [%Node{first_public_key: "key3"}, %Node{first_public_key: "key2"}, %Node{first_public_key: "key1"}, + %Node{first_public_key: "key4"}] ] iex> NodeDistribution.split_storage_nodes([ @@ -56,7 +59,8 @@ defmodule Archethic.Mining.TransactionContext.NodeDistribution do ...> %Node{first_public_key: "key4"} ...> ], 5, 3) [ - [%Node{first_public_key: "key1"}, %Node{first_public_key: "key2"}, %Node{first_public_key: "key3"}], + [%Node{first_public_key: "key1"}, %Node{first_public_key: "key2"}, %Node{first_public_key: "key3"}, + %Node{first_public_key: "key4"}], [%Node{first_public_key: "key2"}, %Node{first_public_key: "key3"}, %Node{first_public_key: "key4"}], [%Node{first_public_key: "key3"}, %Node{first_public_key: "key4"}, %Node{first_public_key: "key1"}], [%Node{first_public_key: "key4"}, %Node{first_public_key: "key1"}, %Node{first_public_key: "key2"}], @@ -79,17 +83,13 @@ defmodule Archethic.Mining.TransactionContext.NodeDistribution do defp do_split(storage_nodes, nb_sublist, sample_size, sub_lists) do split = storage_nodes - |> Enum.reduce_while(sub_lists, fn node, acc -> - if length(acc) == nb_sublist and Enum.all?(acc, &(length(&1) == sample_size)) do - {:halt, acc} - else - smallest_sub_list = Enum.min_by(acc, &length/1) - sub_list_index_to_add = Enum.find_index(acc, &(&1 == smallest_sub_list)) - {:cont, List.update_at(acc, sub_list_index_to_add, &[node | &1])} - end + |> Enum.reduce(sub_lists, fn node, acc -> + smallest_sub_list = Enum.min_by(acc, &length/1) + sub_list_index_to_add = Enum.find_index(acc, &(&1 == smallest_sub_list)) + List.update_at(acc, sub_list_index_to_add, &[node | &1]) end) - if Enum.all?(split, &(length(&1) == sample_size)) do + if Enum.all?(split, &(length(&1) >= sample_size)) do Enum.map(split, fn list -> list |> Enum.reverse()