protocol.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from streamlet import Block
  2. from ouroboros import VRF
  3. from node import Node
  4. import math
  5. import numpy as np
  6. # Genesis block is generated.
  7. genesis_block = Block("⊥", 0, '⊥')
  8. # We create some nodes to participate in the Protocol.
  9. # There are in total n nodes numbered.
  10. node0 = Node(0, "clock", "node_password0", genesis_block)
  11. node1 = Node(1, "clock", "node_password1", genesis_block)
  12. node4 = Node(4, "clock", "node_password4", genesis_block)
  13. nodes = [node0, node1, node4]
  14. # We simulate some rounds to test consistency.
  15. epoch = 1
  16. # Nodes receive transactions and broacasts them between them.
  17. # node0 receives input and broadcasts it to rest nodes.
  18. node0.receive_transaction("tx0")
  19. node0.broadcast_transaction([node1, node4], "tx0")
  20. # node1 receives input and broadcasts it to rest nodes.
  21. node1.receive_transaction("tx2")
  22. node1.broadcast_transaction([node0, node4], "tx2")
  23. # node4 receives input and broadcasts it to rest nodes.
  24. node4.receive_transaction("tx3")
  25. node4.broadcast_transaction([node0, node1], "tx3")
  26. vrf = VRF()
  27. x = epoch
  28. y, pi, g = vrf.sign(x)
  29. Y = np.array(y)
  30. y_hypotenuse2 = np.sum(Y[1]**2+Y[2]**2)
  31. # A random leader is selected.
  32. leader = nodes[math.ceil(y_hypotenuse2)%len(nodes)]
  33. print(f"proposed {x}, {y}, {pi}, {vrf.pk}, {g}")
  34. # Leader forms a block and broadcasts it.
  35. leader.propose_block(1, y, pi, vrf.pk, g, nodes)
  36. # Nodes vote on the block and broadcast their vote to rest nodes.
  37. for node in nodes:
  38. node.vote_on_round_block(nodes)
  39. # We verify that all nodes have the same blockchain on round end.
  40. assert(node0.output() == node1.output() == node4.output())