2023-05-18 16:29:28 +00:00
|
|
|
import random
|
|
|
|
from abc import abstractmethod
|
2023-05-30 19:15:56 +00:00
|
|
|
from typing import Set, Optional, List, Self
|
2024-01-10 01:23:03 +00:00
|
|
|
from carnot.carnot import Overlay, Id, Committee, View
|
2023-05-18 16:29:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
class EntropyOverlay(Overlay):
|
|
|
|
@abstractmethod
|
2023-05-30 19:15:56 +00:00
|
|
|
def advance(self, entropy: bytes) -> Self:
|
2023-05-18 16:29:28 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class FlatOverlay(EntropyOverlay):
|
2023-05-30 19:15:56 +00:00
|
|
|
|
|
|
|
def __init__(self, current_leader: Id, nodes: List[Id], entropy: bytes):
|
|
|
|
self.current_leader = current_leader
|
|
|
|
self.nodes = nodes
|
2023-05-18 16:29:28 +00:00
|
|
|
self.entropy = entropy
|
|
|
|
|
2023-05-30 19:15:56 +00:00
|
|
|
def next_leader(self) -> Id:
|
|
|
|
random.seed(a=self.entropy, version=2)
|
|
|
|
return random.choice(self.nodes)
|
|
|
|
|
|
|
|
def advance(self, entropy: bytes):
|
|
|
|
return FlatOverlay(self.next_leader(), self.nodes, entropy)
|
|
|
|
|
2023-05-18 16:29:28 +00:00
|
|
|
def is_leader(self, _id: Id):
|
|
|
|
return _id == self.leader()
|
|
|
|
|
|
|
|
def leader(self) -> Id:
|
2023-05-30 19:15:56 +00:00
|
|
|
return self.current_leader
|
2023-05-18 16:29:28 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|