nomos-specs/carnot/test_beacon_verification.py
Daniel Sanchez 5a169039b5
Random beacon v1 (#24)
* Implement beacon verification and handling module

* Create beacon tests and fix encountered problems

* Refactor tests

* Add mixed happy/unhappy test

* Clean unused import

* Add requirements.txt

* Add beacon to package

* Resolve relative import

* Fmt

* Refactor BeaconHandler -> RandomBeaconHandler
Remove unused verification calls

* Change view bytes encoding
Extract generating private key

* Bring back old trusty carnot

* Added beaconized carnot module

* Implement flat overlay

* Refactor overlay next leader

* Implement beaconized carnot

* Fill proposed block on beaconized carnot

* Sketch and update for testing purposes

* Step up beaconized test

* Fix missing leader selection

* Fix random beacon test

* Use recovery mode for random beacon initialization

* Expose entropy as constructor parameter
2023-05-18 18:29:28 +02:00

74 lines
2.3 KiB
Python

from typing import Tuple
from unittest import TestCase
from beacon import *
from random import randint
class TestRandomBeaconVerification(TestCase):
@staticmethod
def happy_entropy_and_proof(view: View) -> Tuple[Beacon, Proof]:
sk = generate_random_sk()
beacon = NormalMode.generate_beacon(sk, view)
return bytes(beacon), bytes(sk.get_g1())
@staticmethod
def unhappy_entropy(last_beacon: Beacon, view: View) -> Beacon:
return RecoveryMode.generate_beacon(last_beacon, view)
def setUp(self):
entropy, proof = self.happy_entropy_and_proof(0)
self.beacon = RandomBeaconHandler(
beacon=RandomBeacon(
version=0,
context=0,
entropy=entropy,
proof=proof
)
)
def test_happy(self):
for i in range(3):
entropy, proof = self.happy_entropy_and_proof(i)
new_beacon = RandomBeacon(
version=0,
context=i,
entropy=entropy,
proof=proof
)
self.beacon.verify_happy(new_beacon)
self.assertEqual(self.beacon.last_beacon.context, 2)
def test_unhappy(self):
for i in range(1, 3):
entropy = self.unhappy_entropy(self.beacon.last_beacon.entropy, i)
new_beacon = RandomBeacon(
version=0,
context=i,
entropy=entropy,
proof=b""
)
self.beacon.verify_unhappy(new_beacon)
self.assertEqual(self.beacon.last_beacon.context, 2)
def test_mixed(self):
for i in range(1, 6, 2):
entropy, proof = self.happy_entropy_and_proof(i)
new_beacon = RandomBeacon(
version=0,
context=i,
entropy=entropy,
proof=proof
)
self.beacon.verify_happy(new_beacon)
entropy = self.unhappy_entropy(self.beacon.last_beacon.entropy, i+1)
new_beacon = RandomBeacon(
version=0,
context=i+1,
entropy=entropy,
proof=b""
)
self.beacon.verify_unhappy(new_beacon)
self.assertEqual(self.beacon.last_beacon.context, 6)