From 375c48fb92d6e3ec9048bc223b1f1e2c8d49d5aa Mon Sep 17 00:00:00 2001 From: danielSanchezQ <3danimanimal@gmail.com> Date: Thu, 17 Jul 2025 07:28:55 +0000 Subject: [PATCH] DeclarationId to ProviderId --- da/assignations/refill.py | 22 +++++++++++----------- da/assignations/test_refill.py | 10 +++++----- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/da/assignations/refill.py b/da/assignations/refill.py index 4e79d91..0936a65 100644 --- a/da/assignations/refill.py +++ b/da/assignations/refill.py @@ -7,8 +7,8 @@ from collections import Counter from heapq import heappush, heappop, heapify -DeclarationId: TypeAlias = bytes -Assignations: TypeAlias = List[Set[DeclarationId]] +ProviderId: TypeAlias = bytes +Assignations: TypeAlias = List[Set[ProviderId]] BlakeRng: TypeAlias = Any @@ -17,13 +17,13 @@ class Participant: # Participant's wrapper class # Used for keeping ordering in the heap by the participation first and the declaration id second participation: int # prioritize participation count first - declaration_id: DeclarationId # sort by id on default + provider_id: ProviderId # sort by id on default @dataclass class Subnetwork: # Subnetwork wrapper that keeps the subnetwork id [0..2048) and the set of participants in that subnetwork - participants: Set[DeclarationId] + participants: Set[ProviderId] subnetwork_id: int def __lt__(self, other): @@ -47,7 +47,7 @@ def all_nodes_assigned(participants: Sequence[Participant], average_participatio def heappop_next_for_subnetwork(subnetwork: Subnetwork, participants: List[Participant]) -> Participant: poped = [] participant = heappop(participants) - while participant.declaration_id in subnetwork.participants: + while participant.provider_id in subnetwork.participants: poped.append(participant) participant = heappop(participants) for poped in poped: @@ -83,7 +83,7 @@ def fill_subnetworks( participant = heappop_next_for_subnetwork(subnetwork, available_nodes) # fill into subnetwork - subnetwork.participants.add(participant.declaration_id) + subnetwork.participants.add(participant.provider_id) participant.participation += 1 # push to heaps heappush(available_nodes, participant) @@ -112,11 +112,11 @@ def balance_subnetworks_grow( ): for participant in filter(lambda x: x.participation > average_participation, sorted(participants)): for subnework in sample( - sorted(filter(lambda subnetwork: participant.declaration_id in subnetwork.participants, subnetworks)), + sorted(filter(lambda subnetwork: participant.provider_id in subnetwork.participants, subnetworks)), random, k=participant.participation - average_participation ): - subnework.participants.remove(participant.declaration_id) + subnework.participants.remove(participant.provider_id) participant.participation -= 1 @@ -129,7 +129,7 @@ def rand(seed: bytes): def calculate_subnetwork_assignations( - new_nodes_list: Sequence[DeclarationId], + new_nodes_list: Sequence[ProviderId], previous_subnets: Assignations, replication_factor: int, random_seed: bytes, @@ -169,11 +169,11 @@ def calculate_subnetwork_assignations( active_assignations = [subnet - unavailable_nodes for subnet in previous_subnets] # count participation per assigned node - assigned_count: Counter[DeclarationId] = Counter(chain.from_iterable(active_assignations)) + assigned_count: Counter[ProviderId] = Counter(chain.from_iterable(active_assignations)) # available nodes heap available_nodes = [ - Participant(participation=assigned_count.get(_id, 0), declaration_id=_id) for _id in new_nodes + Participant(participation=assigned_count.get(_id, 0), provider_id=_id) for _id in new_nodes ] # subnetworks heap diff --git a/da/assignations/test_refill.py b/da/assignations/test_refill.py index c62d240..cc0af5c 100644 --- a/da/assignations/test_refill.py +++ b/da/assignations/test_refill.py @@ -2,7 +2,7 @@ import random from itertools import chain from typing import List, Counter from unittest import TestCase -from da.assignations.refill import calculate_subnetwork_assignations, Assignations, DeclarationId +from da.assignations.refill import calculate_subnetwork_assignations, Assignations, ProviderId class TestRefill(TestCase): @@ -71,21 +71,21 @@ class TestRefill(TestCase): @classmethod - def mutate_nodes(cls, nodes: List[DeclarationId], count: int): + def mutate_nodes(cls, nodes: List[ProviderId], count: int): assert count < len(nodes) for i in random.choices(list(range(len(nodes))), k=count): nodes[i] = random.randbytes(32) @classmethod - def expand_nodes(cls, nodes: List[DeclarationId], count: int) -> List[DeclarationId]: + def expand_nodes(cls, nodes: List[ProviderId], count: int) -> List[ProviderId]: return [*nodes, *(random.randbytes(32) for _ in range(count))] @classmethod - def shrink_nodes(cls, nodes: List[DeclarationId], count: int) -> List[DeclarationId]: + def shrink_nodes(cls, nodes: List[ProviderId], count: int) -> List[ProviderId]: return list(random.sample(nodes, k=count)) - def assert_assignations(self, assignations: Assignations, nodes: List[DeclarationId], replication_factor: int): + def assert_assignations(self, assignations: Assignations, nodes: List[ProviderId], replication_factor: int): self.assertEqual( len(set(chain.from_iterable(assignations))), len(nodes),