beacon.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import threading
  2. from ouroboros.clock import SynchedNTPClock
  3. from ouroboros.vrf import VRF
  4. from ouroboros.logger import Logger
  5. '''
  6. \class TrustedBeacon
  7. leaky, non-resettable beacon, leaky in the sense that the slots are
  8. predictable, and non-resettable, beacon is basically a synchronized
  9. timestamp.
  10. '''
  11. class TrustedBeacon(SynchedNTPClock, threading.Thread):
  12. def __init__(self, epoch_length, genesis_time):
  13. SynchedNTPClock.__init__(self, epoch_length)
  14. threading.Thread.__init__(self)
  15. self.daemon=True
  16. self.current_slot = self.slot
  17. self.log = Logger(self, genesis_time)
  18. self.bb=0 # epoch counts since genesis (big bang)
  19. self.proofs_epoch=-1
  20. def __repr__(self):
  21. return f"trustedbeacon"
  22. def run(self):
  23. self.log.highlight("thread [start]")
  24. prev_slot = self.slot
  25. self.__callback()
  26. while True:
  27. if not self.slot == prev_slot:
  28. prev_slot = self.slot
  29. self.__callback()
  30. def next_epoch_seeds(self, vrf):
  31. rands = {}
  32. for i in range(self.epoch_length):
  33. slot_idx = self.current_slot+i
  34. y, pi = vrf.sign(slot_idx)
  35. rands[slot_idx] = (y,pi)
  36. return rands