nomos-specs/carnot/overlay.py
Daniel Sanchez 5a169039b5
Random beacon v1 (#24)
* Implement beacon verification and handling module

* Create beacon tests and fix encountered problems

* Refactor tests

* Add mixed happy/unhappy test

* Clean unused import

* Add requirements.txt

* Add beacon to package

* Resolve relative import

* Fmt

* Refactor BeaconHandler -> RandomBeaconHandler
Remove unused verification calls

* Change view bytes encoding
Extract generating private key

* Bring back old trusty carnot

* Added beaconized carnot module

* Implement flat overlay

* Refactor overlay next leader

* Implement beaconized carnot

* Fill proposed block on beaconized carnot

* Sketch and update for testing purposes

* Step up beaconized test

* Fix missing leader selection

* Fix random beacon test

* Use recovery mode for random beacon initialization

* Expose entropy as constructor parameter
2023-05-18 18:29:28 +02:00

57 lines
1.4 KiB
Python

import random
from abc import abstractmethod
from typing import Set, Optional, List
from carnot import Overlay, Id, Committee, View
class EntropyOverlay(Overlay):
@abstractmethod
def set_entropy(self, entropy: bytes):
pass
class FlatOverlay(EntropyOverlay):
def set_entropy(self, entropy: bytes):
self.entropy = entropy
def is_leader(self, _id: Id):
return _id == self.leader()
def leader(self) -> Id:
random.seed(a=self.entropy, version=2)
return random.choice(self.nodes)
def is_member_of_leaf_committee(self, _id: Id) -> bool:
return True
def is_member_of_root_committee(self, _id: Id) -> bool:
return True
def is_member_of_child_committee(self, parent: Id, child: Id) -> bool:
return False
def parent_committee(self, _id: Id) -> Optional[Committee]:
return None
def leaf_committees(self) -> Set[Committee]:
return {frozenset(self.nodes)}
def root_committee(self) -> Committee:
return set(self.nodes)
def is_child_of_root_committee(self, _id: Id) -> bool:
return True
def leader_super_majority_threshold(self, _id: Id) -> int:
return ((len(self.nodes) * 2) // 3) + 1
def super_majority_threshold(self, _id: Id) -> int:
return 0
def __init__(self, nodes: List[Id]):
self.nodes = nodes
self.entropy = None