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=NULL_BLOCK_IDENTIFIER) # 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_hash="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_hash="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=NULL_BLOCK_IDENTIFIER) # 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_hash="Reader's Digest") WaitCertificate.create_wait_certificate(wait_timer=valid_wt, block_hash="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=NULL_BLOCK_IDENTIFIER) # 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_hash="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_hash="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( poet_enclave_module=self.poet_enclave_module, wait_timer=None, block_hash="Reader's Digest")
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=NULL_BLOCK_IDENTIFIER) # 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_hash="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=NULL_BLOCK_IDENTIFIER) 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_hash="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_hash, "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_hash="Pepto Bismol") another_wc.check_valid([wc], 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=NULL_BLOCK_IDENTIFIER) 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_hash="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_hash, wc_copy.block_hash) 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 __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 test_create_wait_certificate_with_reused_wait_timer(self): # Need to create signup information SignupInfo.create_signup_info( poet_enclave_module=self.poet_enclave_module, validator_address='1660 Pennsylvania Avenue NW', originator_public_key_hash=self._originator_public_key_hash, most_recent_wait_certificate_id=NULL_BLOCK_IDENTIFIER) # 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( poet_enclave_module=self.poet_enclave_module, validator_address='1660 Pennsylvania Avenue NW', certificates=[]) wc = \ WaitCertificate.create_wait_certificate( poet_enclave_module=self.poet_enclave_module, wait_timer=wt, block_hash="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( poet_enclave_module=self.poet_enclave_module, wait_timer=consumed_wt, block_hash="Reader's Digest") wt = \ WaitTimer.create_wait_timer( poet_enclave_module=self.poet_enclave_module, validator_address='1660 Pennsylvania Avenue NW', certificates=[wc]) with self.assertRaises(ValueError): WaitCertificate.create_wait_certificate( poet_enclave_module=self.poet_enclave_module, wait_timer=consumed_wt, block_hash="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( poet_enclave_module=self.poet_enclave_module, wait_timer=wt, block_hash="Reader's Digest")
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_hash = hasher.hexdigest() self.wait_certificate = \ WaitCertificate.create_wait_certificate( wait_timer=self.wait_timer, block_hash=block_hash) if self.wait_certificate: self.wait_timer = None
def deserialize_wait_certificate(block, poet_enclave_module): """Deserializes the wait certificate associated with the block. Args: block (Block or BlockWrapper): The block that has the wait certificate poet_enclave_module (module): The PoET enclave module Returns: WaitCertificate: The reconstituted wait certificate associated with the block """ # The wait certificate is a JSON string placed in the consensus # field/property of the block header. Parse the JSON and then use the # serialized wait certificate and signature to create a # WaitCertificate object. wait_certificate_dict = json.loads(block.header.consensus.decode()) return \ WaitCertificate.wait_certificate_from_serialized( poet_enclave_module=poet_enclave_module, serialized=wait_certificate_dict.get('SerializedCertificate'), signature=wait_certificate_dict.get('Signature'))
def finalize_block(self, block_header): """Finalize a block to be claimed. Provide any signatures and data updates that need to be applied to the block before it is signed and broadcast to the network. Args: block_header (BlockHeader): The block header for the candidate block that needs to be finalized. Returns: Boolean: True if the candidate block good and should be generated. False if the block should be abandoned. """ # To compute the block hash, we are going to perform a hash using the # previous block ID and the batch IDs contained in the block hasher = hashlib.sha256(block_header.previous_block_id.encode()) for batch_id in block_header.batch_ids: hasher.update(batch_id.encode()) block_hash = hasher.hexdigest() # We need to create a wait certificate for the block and then serialize # that into the block header consensus field. try: wait_certificate = \ WaitCertificate.create_wait_certificate( poet_enclave_module=self._poet_enclave_module, wait_timer=self._wait_timer, block_hash=block_hash) block_header.consensus = \ json.dumps(wait_certificate.dump()).encode() except ValueError as ve: LOGGER.error('Failed to create wait certificate: %s', ve) return False LOGGER.debug('Created wait certificate: %s', wait_certificate) return True