def test_create_wait_certificate_after_wait_timer_timed_out(self): # Need to create signup information SignupInfo.create_signup_info( validator_address='1660 Pennsylvania Avenue NW', originator_public_key_hash=self._originator_public_key_hash, most_recent_wait_certificate_id=NullIdentifier) # Create a wait certificate for the genesis block so that we can # create another wait certificate that has to play by the rules. wt = \ WaitTimer.create_wait_timer( validator_address='1660 Pennsylvania Avenue NW', certificates=[]) wc = \ WaitCertificate.create_wait_certificate( wait_timer=wt, block_digest="Reader's Digest") wt = \ WaitTimer.create_wait_timer( validator_address='1660 Pennsylvania Avenue NW', certificates=[wc]) while not wt.has_expired(time.time()): time.sleep(1) time.sleep(WaitTimer.poet_enclave.TIMER_TIMEOUT_PERIOD + 1) with self.assertRaises(ValueError): WaitCertificate.create_wait_certificate( wait_timer=wt, block_digest="Reader's Digest")
def test_create_wait_certificate_with_wrong_wait_timer(self): # Need to create signup information SignupInfo.create_signup_info( validator_address='1660 Pennsylvania Avenue NW', originator_public_key_hash=self._originator_public_key_hash, most_recent_wait_certificate_id=NullIdentifier) # Create two timers and try to create the wait certificate with the # first one, which should fail as it is not the current wait timer invalid_wt = \ WaitTimer.create_wait_timer( validator_address='1660 Pennsylvania Avenue NW', certificates=[]) valid_wt = \ WaitTimer.create_wait_timer( validator_address='1660 Pennsylvania Avenue NW', certificates=[]) # Verify that we cannot create a wait certificate with the old wait # timer, but we can with the new one with self.assertRaises(ValueError): WaitCertificate.create_wait_certificate( wait_timer=invalid_wt, block_digest="Reader's Digest") WaitCertificate.create_wait_certificate( wait_timer=valid_wt, block_digest="Reader's Digest")
def test_create_wait_certificate_before_create_signup_info(self): # Make sure that trying to create a wait certificate before signup # information is provided causes an error with self.assertRaises(ValueError): WaitCertificate.create_wait_certificate( wait_timer=None, block_digest="Reader's Digest")
def test_create_wait_certificate_before_wait_timer_expires(self): # Need to create signup information SignupInfo.create_signup_info( validator_address='1660 Pennsylvania Avenue NW', originator_public_key_hash=self._originator_public_key_hash, most_recent_wait_certificate_id=NullIdentifier) # Create a wait certificate for the genesis block so that we can # create another wait certificate that has to play by the rules. wt = \ WaitTimer.create_wait_timer( validator_address='1660 Pennsylvania Avenue NW', certificates=[]) wc = \ WaitCertificate.create_wait_certificate( wait_timer=wt, block_digest="Reader's Digest") wt = \ WaitTimer.create_wait_timer( validator_address='1660 Pennsylvania Avenue NW', certificates=[wc]) with self.assertRaises(ValueError): WaitCertificate.create_wait_certificate( wait_timer=wt, block_digest="Reader's Digest")
def test_create_wait_certificate_with_wrong_wait_timer(self): # Need to create signup information SignupInfo.create_signup_info( validator_address='1660 Pennsylvania Avenue NW', originator_public_key_hash=self._originator_public_key_hash, most_recent_wait_certificate_id=NullIdentifier) # Create two timers and try to create the wait certificate with the # first one, which should fail as it is not the current wait timer invalid_wt = \ WaitTimer.create_wait_timer( validator_address='1660 Pennsylvania Avenue NW', certificates=[]) valid_wt = \ WaitTimer.create_wait_timer( validator_address='1660 Pennsylvania Avenue NW', certificates=[]) # Verify that we cannot create a wait certificate with the old wait # timer, but we can with the new one with self.assertRaises(ValueError): WaitCertificate.create_wait_certificate( wait_timer=invalid_wt, block_digest="Reader's Digest") WaitCertificate.create_wait_certificate(wait_timer=valid_wt, block_digest="Reader's Digest")
def test_create_wait_certificate_before_create_signup_info(self): # Make sure that trying to create a wait certificate before signup # information is provided causes an error with self.assertRaises(ValueError): WaitCertificate.create_wait_certificate( wait_timer=None, block_digest="Reader's Digest")
def test_create_wait_certificate_after_wait_timer_timed_out(self): # Need to create signup information SignupInfo.create_signup_info( validator_address='1660 Pennsylvania Avenue NW', originator_public_key_hash=self._originator_public_key_hash, most_recent_wait_certificate_id=NullIdentifier) # Create a wait certificate for the genesis block so that we can # create another wait certificate that has to play by the rules. wt = \ WaitTimer.create_wait_timer( validator_address='1660 Pennsylvania Avenue NW', certificates=[]) wc = \ WaitCertificate.create_wait_certificate( wait_timer=wt, block_digest="Reader's Digest") wt = \ WaitTimer.create_wait_timer( validator_address='1660 Pennsylvania Avenue NW', certificates=[wc]) while not wt.has_expired(time.time()): time.sleep(1) time.sleep(WaitTimer.poet_enclave.TIMER_TIMEOUT_PERIOD + 1) with self.assertRaises(ValueError): WaitCertificate.create_wait_certificate( wait_timer=wt, block_digest="Reader's Digest")
def test_create_wait_certificate_before_wait_timer_expires(self): # Need to create signup information SignupInfo.create_signup_info( validator_address='1660 Pennsylvania Avenue NW', originator_public_key_hash=self._originator_public_key_hash, most_recent_wait_certificate_id=NullIdentifier) # Create a wait certificate for the genesis block so that we can # create another wait certificate that has to play by the rules. wt = \ WaitTimer.create_wait_timer( validator_address='1660 Pennsylvania Avenue NW', certificates=[]) wc = \ WaitCertificate.create_wait_certificate( wait_timer=wt, block_digest="Reader's Digest") wt = \ WaitTimer.create_wait_timer( validator_address='1660 Pennsylvania Avenue NW', certificates=[wc]) with self.assertRaises(ValueError): WaitCertificate.create_wait_certificate( wait_timer=wt, block_digest="Reader's Digest")
def test_create_wait_certificate_before_create_wait_timer(self): # Need to create signup information SignupInfo.create_signup_info( originator_public_key_hash=self._originator_public_key_hash, most_recent_wait_certificate_id=NullIdentifier) with self.assertRaises(WaitCertificateError): WaitCertificate.create_wait_certificate("Reader's Digest")
def test_wait_certificate_serialization(self): # Need to create signup information and wait timer first signup_info = \ SignupInfo.create_signup_info( validator_address='1660 Pennsylvania Avenue NW', originator_public_key_hash=self._originator_public_key_hash, most_recent_wait_certificate_id=NullIdentifier) wt = \ WaitTimer.create_wait_timer( validator_address='1660 Pennsylvania Avenue NW', certificates=[]) while not wt.has_expired(time.time()): time.sleep(1) # Now we can create a wait certificate and serialize wc = \ WaitCertificate.create_wait_certificate( wait_timer=None, block_digest="Reader's Digest") dumped = wc.dump() self.assertIsNotNone(dumped.get('SerializedCertificate')) self.assertIsNotNone(dumped.get('Signature')) # Deserialize and verify that wait certificates are the same # and that deserialized one is valid wc_copy = \ WaitCertificate.wait_certificate_from_serialized( dumped.get('SerializedCertificate'), dumped.get('Signature')) self.assertEquals( wc.previous_certificate_id, wc_copy.previous_certificate_id) self.assertAlmostEquals(wc.local_mean, wc_copy.local_mean) self.assertAlmostEquals(wc.request_time, wc_copy.request_time) self.assertAlmostEquals(wc.duration, wc_copy.duration) self.assertEqual(wc.validator_address, wc_copy.validator_address) self.assertEqual(wc.block_digest, wc_copy.block_digest) self.assertEqual(wc.signature, wc_copy.signature) self.assertEqual(wc.identifier, wc_copy.identifier) # Serialize the copy and verify that its serialization and # signature are the same dumped_copy = wc_copy.dump() self.assertTrue( dumped.get('SerializedCertificate'), dumped_copy.get('SerializedCertificate')) self.assertTrue( dumped.get('Signature'), dumped_copy.get('Signature')) self.assertTrue(wc_copy.is_valid([], signup_info.poet_public_key))
def test_wait_certificate_serialization(self): # Need to create signup information and wait timer first signup_info = \ SignupInfo.create_signup_info( validator_address='1660 Pennsylvania Avenue NW', originator_public_key_hash=self._originator_public_key_hash, most_recent_wait_certificate_id=NullIdentifier) wt = \ WaitTimer.create_wait_timer( validator_address='1660 Pennsylvania Avenue NW', certificates=[]) while not wt.has_expired(time.time()): time.sleep(1) # Now we can create a wait certificate and serialize wc = \ WaitCertificate.create_wait_certificate( wait_timer=wt, block_digest="Reader's Digest") dumped = wc.dump() self.assertIsNotNone(dumped.get('SerializedCertificate')) self.assertIsNotNone(dumped.get('Signature')) # Deserialize and verify that wait certificates are the same # and that deserialized one is valid wc_copy = \ WaitCertificate.wait_certificate_from_serialized( dumped.get('SerializedCertificate'), dumped.get('Signature')) self.assertEqual( wc.previous_certificate_id, wc_copy.previous_certificate_id) self.assertAlmostEqual(wc.local_mean, wc_copy.local_mean) self.assertAlmostEqual(wc.request_time, wc_copy.request_time) self.assertAlmostEqual(wc.duration, wc_copy.duration) self.assertEqual(wc.validator_address, wc_copy.validator_address) self.assertEqual(wc.block_digest, wc_copy.block_digest) self.assertEqual(wc.signature, wc_copy.signature) self.assertEqual(wc.identifier, wc_copy.identifier) # Serialize the copy and verify that its serialization and # signature are the same dumped_copy = wc_copy.dump() self.assertTrue( dumped.get('SerializedCertificate'), dumped_copy.get('SerializedCertificate')) self.assertTrue( dumped.get('Signature'), dumped_copy.get('Signature')) wc_copy.check_valid([], signup_info.poet_public_key)
def test_create_wait_certificate_before_create_wait_timer(self): # Need to create signup information SignupInfo.create_signup_info( validator_address='1660 Pennsylvania Avenue NW', originator_public_key_hash=self._originator_public_key_hash, most_recent_wait_certificate_id=NullIdentifier) with self.assertRaises(ValueError): WaitCertificate.create_wait_certificate( wait_timer=None, block_digest="Reader's Digest")
def test_create_wait_certificate(self): # Need to create signup information and wait timer first signup_info = \ SignupInfo.create_signup_info( validator_address='1660 Pennsylvania Avenue NW', originator_public_key_hash=self._originator_public_key_hash, most_recent_wait_certificate_id=NullIdentifier) wt = \ WaitTimer.create_wait_timer( validator_address='1660 Pennsylvania Avenue NW', certificates=[]) while not wt.has_expired(time.time()): time.sleep(1) # Now we can create a wait certificate and verify that it correlates # to the wait timer we just created wc = \ WaitCertificate.create_wait_certificate( wait_timer=None, block_digest="Reader's Digest") self.assertIsNotNone(wc) self.assertEquals( wc.previous_certificate_id, wt.previous_certificate_id) self.assertAlmostEquals(wc.local_mean, wt.local_mean) self.assertAlmostEquals(wc.request_time, wt.request_time) self.assertAlmostEquals(wc.duration, wt.duration) self.assertEqual(wc.validator_address, wt.validator_address) self.assertEqual(wc.block_digest, "Reader's Digest") self.assertIsNotNone(wc.signature) self.assertIsNotNone(wc.identifier) # A newly-created wait certificate should be valid self.assertTrue(wc.is_valid([], signup_info.poet_public_key)) # Create another wait certificate and verify it is valid also wt = \ WaitTimer.create_wait_timer( validator_address='1660 Pennsylvania Avenue NW', certificates=[wc]) while not wt.has_expired(time.time()): time.sleep(1) # Now we can create a wait certificate and verify that it correlates # to the wait timer we just created another_wc = \ WaitCertificate.create_wait_certificate( wait_timer=None, block_digest="Pepto Bismol") self.assertTrue( another_wc.is_valid([wc], signup_info.poet_public_key))
def test_create_wait_certificate(self): # Need to create signup information and wait timer first signup_info = \ SignupInfo.create_signup_info( validator_address='1660 Pennsylvania Avenue NW', originator_public_key_hash=self._originator_public_key_hash, most_recent_wait_certificate_id=NullIdentifier) wt = \ WaitTimer.create_wait_timer( validator_address='1660 Pennsylvania Avenue NW', certificates=[]) while not wt.has_expired(time.time()): time.sleep(1) # Now we can create a wait certificate and verify that it correlates # to the wait timer we just created wc = \ WaitCertificate.create_wait_certificate( wait_timer=wt, block_digest="Reader's Digest") self.assertIsNotNone(wc) self.assertEqual( wc.previous_certificate_id, wt.previous_certificate_id) self.assertAlmostEqual(wc.local_mean, wt.local_mean) self.assertAlmostEqual(wc.request_time, wt.request_time) self.assertAlmostEqual(wc.duration, wt.duration) self.assertEqual(wc.validator_address, wt.validator_address) self.assertEqual(wc.block_digest, "Reader's Digest") self.assertIsNotNone(wc.signature) self.assertIsNotNone(wc.identifier) # A newly-created wait certificate should be valid wc.check_valid([], signup_info.poet_public_key) # Create another wait certificate and verify it is valid also wt = \ WaitTimer.create_wait_timer( validator_address='1660 Pennsylvania Avenue NW', certificates=[wc]) while not wt.has_expired(time.time()): time.sleep(1) # Now we can create a wait certificate and verify that it correlates # to the wait timer we just created another_wc = \ WaitCertificate.create_wait_certificate( wait_timer=wt, block_digest="Pepto Bismol") another_wc.check_valid([wc], signup_info.poet_public_key)
def test_create_wait_certificate_before_create_wait_timer(self): # Need to create signup information SignupInfo.create_signup_info( validator_address='1660 Pennsylvania Avenue NW', originator_public_key_hash=self._originator_public_key_hash, most_recent_wait_certificate_id=NullIdentifier) # Make sure that trying to create a wait certificate before creating # a wait timer causes an error with self.assertRaises(ValueError): WaitCertificate.create_wait_certificate( wait_timer=None, block_digest="Reader's Digest")
def test_create_wait_certificate_before_wait_timer_expires(self): # Need to create signup information SignupInfo.create_signup_info( originator_public_key_hash=self._originator_public_key_hash, most_recent_wait_certificate_id=NullIdentifier) # Create a wait certificate for the genesis block so that we can # create another wait certificate that has to play by the rules. wt = WaitTimer.create_wait_timer([]) wc = WaitCertificate.create_wait_certificate("Reader's Digest") wt = WaitTimer.create_wait_timer([wc]) with self.assertRaises(WaitCertificateError): wc = WaitCertificate.create_wait_certificate("Reader's Digest")
def __init__(self, minfo=None): """Constructor for the PoetTransactionBlock class. Args: minfo (dict): A dict of values for initializing PoetTransactionBlocks. """ if minfo is None: minfo = {} super(PoetTransactionBlock, self).__init__(minfo) self._lock = RLock() self.wait_timer = None self.wait_certificate = None self.poet_public_key = minfo.get('PoetPublicKey') if 'WaitCertificate' in minfo: wc = minfo.get('WaitCertificate') serialized_certificate = wc.get('SerializedCertificate') signature = wc.get('Signature') self.wait_certificate = \ WaitCertificate.wait_certificate_from_serialized( serialized=serialized_certificate, signature=signature) self.aggregate_local_mean = 0.0
def __init__(self, minfo=None): """Constructor for the PoetTransactionBlock class. Args: minfo (dict): A dict of values for initializing PoetTransactionBlocks. """ if minfo is None: minfo = {} super(PoetTransactionBlock, self).__init__(minfo) self._lock = RLock() self.wait_timer = None self.wait_certificate = None self.poet_public_key = minfo.get('PoetPublicKey') if 'WaitCertificate' in minfo: wc = minfo.get('WaitCertificate') serialized_certificate = wc.get('SerializedCertificate') signature = wc.get('Signature') self.wait_certificate = \ WaitCertificate.wait_certificate_from_serialized( serialized=serialized_certificate, signature=signature) self.aggregate_local_mean = 0.0
def create_wait_certificate(self): """Create a wait certificate for the journal based on the wait timer. """ with self._lock: LOGGER.debug("WAIT_TIMER: %s", str(self.wait_timer)) hasher = hashlib.sha256() for tid in self.TransactionIDs: hasher.update(tid) block_hash = hasher.hexdigest() self.wait_certificate = \ WaitCertificate.create_wait_certificate( block_digest=block_hash) if self.wait_certificate: self.wait_timer = None
def create_wait_certificate(self): """Create a wait certificate for the journal based on the wait timer. """ with self._lock: LOGGER.debug("WAIT_TIMER: %s", str(self.wait_timer)) hasher = hashlib.sha256() for tid in self.TransactionIDs: hasher.update(tid.encode()) block_digest = hasher.hexdigest() self.wait_certificate = \ WaitCertificate.create_wait_certificate( wait_timer=self.wait_timer, block_digest=block_digest) if self.wait_certificate: self.wait_timer = None
def test_create_wait_certificate_with_reused_wait_timer(self): # Need to create signup information SignupInfo.create_signup_info( validator_address='1660 Pennsylvania Avenue NW', originator_public_key_hash=self._originator_public_key_hash, most_recent_wait_certificate_id=NullIdentifier) # Create a wait certificate for the genesis block so that we can # create another wait certificate that has to play by the rules. wt = \ WaitTimer.create_wait_timer( validator_address='1660 Pennsylvania Avenue NW', certificates=[]) wc = \ WaitCertificate.create_wait_certificate( wait_timer=wt, block_digest="Reader's Digest") consumed_wt = wt # Verify that we cannot use the consumed wait timer to create a wait # certificate either before or after creating a new wait timer with self.assertRaises(ValueError): WaitCertificate.create_wait_certificate( wait_timer=consumed_wt, block_digest="Reader's Digest") wt = \ WaitTimer.create_wait_timer( validator_address='1660 Pennsylvania Avenue NW', certificates=[wc]) with self.assertRaises(ValueError): WaitCertificate.create_wait_certificate( wait_timer=consumed_wt, block_digest="Reader's Digest") # Verify that once the new timer expires, we can create a wait # certificate with it while not wt.has_expired(time.time()): time.sleep(1) WaitCertificate.create_wait_certificate( wait_timer=wt, block_digest="Reader's Digest")
def test_create_wait_certificate_with_reused_wait_timer(self): # Need to create signup information SignupInfo.create_signup_info( validator_address='1660 Pennsylvania Avenue NW', originator_public_key_hash=self._originator_public_key_hash, most_recent_wait_certificate_id=NullIdentifier) # Create a wait certificate for the genesis block so that we can # create another wait certificate that has to play by the rules. wt = \ WaitTimer.create_wait_timer( validator_address='1660 Pennsylvania Avenue NW', certificates=[]) wc = \ WaitCertificate.create_wait_certificate( wait_timer=wt, block_digest="Reader's Digest") consumed_wt = wt # Verify that we cannot use the consumed wait timer to create a wait # certificate either before or after creating a new wait timer with self.assertRaises(ValueError): WaitCertificate.create_wait_certificate( wait_timer=consumed_wt, block_digest="Reader's Digest") wt = \ WaitTimer.create_wait_timer( validator_address='1660 Pennsylvania Avenue NW', certificates=[wc]) with self.assertRaises(ValueError): WaitCertificate.create_wait_certificate( wait_timer=consumed_wt, block_digest="Reader's Digest") # Verify that once the new timer expires, we can create a wait # certificate with it while not wt.has_expired(time.time()): time.sleep(1) WaitCertificate.create_wait_certificate(wait_timer=wt, block_digest="Reader's Digest")
def __init__(self, minfo=None): """Constructor for the PoetTransactionBlock class. Args: minfo (dict): A dict of values for initializing PoetTransactionBlocks. """ if minfo is None: minfo = {} super(PoetTransactionBlock, self).__init__(minfo) self._lock = RLock() self.wait_timer = None self.wait_certificate = None self.poet_public_key = None if 'PoetPublicKey' in minfo: self.poet_public_key = minfo.get('PoetPublicKey') if 'WaitCertificate' in minfo: wc = minfo.get('WaitCertificate') serialized_certificate = wc.get('SerializedCertificate') signature = wc.get('Signature') # # To make this work properly we need to be able to take an # Originator ID and translate that to the # Disguised PoET public key that corresponds to the # Originator which was placed in the validator registry. # poet_public_key = None self.wait_certificate = \ WaitCertificate.wait_certificate_from_serialized( serialized=serialized_certificate, signature=signature, poet_public_key=poet_public_key) self.aggregate_local_mean = 0.0