mirror of
https://github.com/logos-blockchain/logos-blockchain-specs.git
synced 2026-01-05 14:43:11 +00:00
Specify shuffling algorithm
This commit is contained in:
parent
f635996d10
commit
3f1473e49a
@ -13,6 +13,20 @@ def blake2b_hash(committee: Committee) -> bytes:
|
||||
return hasher.digest()
|
||||
|
||||
|
||||
def fisher_yates_shuffle(nodes: List[Id], entropy: bytes):
|
||||
"""
|
||||
Fisher-yates shuffling algorithm
|
||||
It is the one used by python by default
|
||||
https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
|
||||
https://softwareengineering.stackexchange.com/a/215780
|
||||
:param nodes:
|
||||
:param entropy:
|
||||
:return:
|
||||
"""
|
||||
random.seed(a=entropy, version=2)
|
||||
random.shuffle(nodes)
|
||||
|
||||
|
||||
class CarnotTree:
|
||||
"""
|
||||
This balanced binary tree implementation uses a combination of indexes and keys to easily calculate parenting
|
||||
@ -134,8 +148,7 @@ class CarnotOverlay(EntropyOverlay):
|
||||
self.number_of_committees = number_of_committees
|
||||
self.nodes = nodes.copy()
|
||||
self.current_leader = current_leader
|
||||
random.seed(a=self.entropy, version=2)
|
||||
random.shuffle(self.nodes)
|
||||
fisher_yates_shuffle(self.nodes, self.entropy)
|
||||
self.carnot_tree = CarnotTree(nodes, number_of_committees)
|
||||
|
||||
def advance(self, entropy: bytes) -> Self:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user