beacon.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from ouroboros.clock import SynchedNTPClock
  2. from ouroboros.vrf import VRF
  3. from ouroboros.logger import Logger
  4. import threading
  5. import time
  6. '''
  7. \class TrustedBeacon
  8. the trusted beacon is decentralized, such that at the onset of the Epoch,
  9. the leader of the first slot generated the signed seed, and release the signature,
  10. proof, and base to the genesis block.
  11. #TODO implement trustedbeacon as a node
  12. '''
  13. class TrustedBeacon(SynchedNTPClock, threading.Thread):
  14. def __init__(self, node, vrf_sk, epoch_length):
  15. self.epoch_length=epoch_length # how many slots in a a block
  16. SynchedNTPClock.__init__(self)
  17. threading.Thread.__init__(self)
  18. self.daemon=True
  19. self.node = node #stakeholder
  20. self.vrf = VRF(self.node.vrf_pk, vrf_sk, self.node.vrf_base)
  21. self.current_slot = self.slot
  22. self.log = Logger(self)
  23. self.log.info("[TrustedBeacon]")
  24. def __repr__(self):
  25. return f"trustedbeadon\n"
  26. def run(self):
  27. self.log.info("[TrustedBeacon] thread [start]")
  28. self.__background()
  29. self.log.info("[TrustedBeacon] thread [end]")
  30. def __background(self):
  31. current_epoch = self.slot
  32. while True:
  33. if self.slot != current_epoch:
  34. current_epoch = self.slot
  35. self.__callback()
  36. def __callback(self):
  37. self.current_slot = self.slot
  38. sigmas = []
  39. proofs = []
  40. self.log.info(f"[TrustedBeacon] new slot of idx: {self.current_slot}")
  41. for i in range(self.epoch_length):
  42. y, pi = self.vrf.sign(self.current_slot)
  43. sigmas.append(y)
  44. proofs.append(pi)
  45. if self.current_slot%self.epoch_length==0:
  46. self.log.info(["[TrustedBeacon] new slot"])
  47. self.node.new_slot(self.current_slot, sigmas[0], proofs[0])
  48. else:
  49. self.log.info([f"[TrustedBeacon] new epoch with simgas of size:{len(sigmas)}, proofs: {len(proofs)}"])
  50. self.node.new_epoch(self.current_slot, sigmas, proofs)
  51. def verify(self, y, pi, pk_raw, g):
  52. return VRF.verify(self.current_slot, y, pi, pk_raw, g)