beacon.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from clock import SynchedNTPClock
  2. from vrf import VRF
  3. import threading
  4. import time
  5. '''
  6. \class TrustedBeacon
  7. the trusted beacon is decentralized, such that at the onset of the Epoch,
  8. the leader of the first slot generated the signed seed, and release the signature,
  9. proof, and base to the genesis block.
  10. #TODO implement trustedbeacon as a node
  11. '''
  12. class TrustedBeacon(SynchedNTPClock, threading.Thread):
  13. def __init__(self, node, epoch_length):
  14. SynchedNTPClock.__init__(self.epoch_length)
  15. threading.Thread.__init__(self)
  16. self.daemon=True
  17. self.epoch_length=epoch_length # how many slots in a a block
  18. self.node = node #stakeholder
  19. self.vrf = VRF(self.node.vrf_pk, self.node.vrf_sk, self.node.vrk_base)
  20. self.current_slot = self.slot
  21. def run(self):
  22. self.__background()
  23. def __background(self):
  24. current_epoch = self.slot
  25. while True:
  26. if self.slot != current_epoch:
  27. current_epoch = self.slot
  28. self.__callback()
  29. def __callback(self):
  30. self.current_slot = self.slot
  31. sigma, proof = self.vrf.sign(self.current_slot)
  32. if self.slot%self.epoch_length==0:
  33. self.node.new_slot(self.current_slot, sigma, proof)
  34. else:
  35. self.node.new_slot(self.current_slot, sigma, proof, True)
  36. def verify(self, y, pi, pk_raw, g):
  37. return VRF.verify(self.current_slot, y, pi, pk_raw, g)