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 or None if cannot deserialize """ # 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 = None try: wait_certificate_dict = json.loads(block.header.consensus.decode()) wait_certificate = \ WaitCertificate.wait_certificate_from_serialized( poet_enclave_module=poet_enclave_module, serialized=wait_certificate_dict['SerializedCertificate'], signature=wait_certificate_dict['Signature']) except (json.decoder.JSONDecodeError, KeyError): pass return wait_certificate
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 or None if cannot deserialize """ # 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 = None if block is not None: try: wait_certificate_dict = \ json.loads(block.header.consensus.decode()) wait_certificate = \ WaitCertificate.wait_certificate_from_serialized( poet_enclave_module=poet_enclave_module, serialized=wait_certificate_dict['SerializedCertificate'], signature=wait_certificate_dict['Signature']) except (json.decoder.JSONDecodeError, KeyError): pass return wait_certificate
def test_serialization(self): # Need to create signup information and wait timer first signup_info = \ 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) wt = \ WaitTimer.create_wait_timer( poet_enclave_module=self.poet_enclave_module, 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( poet_enclave_module=self.poet_enclave_module, 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( poet_enclave_module=self.poet_enclave_module, serialized=dumped.get('SerializedCertificate'), signature=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(poet_enclave_module=self.poet_enclave_module, certificates=[], poet_public_key=signup_info.poet_public_key)
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 test_serialization(self): # Need to create signup information and wait timer first signup_info = \ SignupInfo.create_signup_info( poet_enclave_module=self.poet_enclave_module, originator_public_key_hash=self._originator_public_key_hash, nonce=NULL_BLOCK_IDENTIFIER) # create mock_poet_enclave_wait_timer mock_poet_enclave_wait_timer = \ mock.Mock(validator_address='1060 W Addison Street', duration=1.0, previous_certificate_id=NULL_BLOCK_IDENTIFIER, local_mean=5.0, signature='00112233445566778899aabbccddeeff', serialized_timer=None, request_time=time.time()) # create mock_poet_enclave_wait_certificate mock_poet_enclave_wait_certificate = \ mock.Mock(duration=1.0, previous_certificate_id=NULL_BLOCK_IDENTIFIER, local_mean=5.0, request_time=time.time(), validator_address='1060 W Addison Street', nonce=NULL_BLOCK_IDENTIFIER, block_hash="Reader's Digest", signature='00112233445566778899aabbccddeeff', serialized_certificate=None) # create mock_poet_enclave_module mock_poet_enclave_module = mock.Mock() mock_poet_enclave_module.create_wait_timer.return_value = \ mock_poet_enclave_wait_timer # set the mock enclave wait certificate and wait timer to # have the same previous_certificate_id mock_poet_enclave_wait_certificate.previous_certificate_id = \ mock_poet_enclave_wait_timer.previous_certificate_id mock_poet_enclave_module.create_wait_certificate.return_value = \ mock_poet_enclave_wait_certificate mock_poet_enclave_module.deserialize_wait_certificate.return_value = \ mock_poet_enclave_wait_certificate wt = \ WaitTimer.create_wait_timer( poet_enclave_module=mock_poet_enclave_module, validator_address='1660 Pennsylvania Avenue NW', previous_certificate_id=NULL_BLOCK_IDENTIFIER, consensus_state=self.mock_consensus_state, poet_settings_view=self.mock_poet_settings_view) # Now we can create a wait certificate and serialize wc = \ WaitCertificate.create_wait_certificate( poet_enclave_module=mock_poet_enclave_module, 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( poet_enclave_module=mock_poet_enclave_module, serialized=dumped.get('SerializedCertificate'), signature=dumped.get('Signature')) self.assertEqual( wc.previous_certificate_id, wc_copy.previous_certificate_id) self.assertEqual(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( poet_enclave_module=mock_poet_enclave_module, previous_certificate_id=NULL_BLOCK_IDENTIFIER, poet_public_key=signup_info.poet_public_key, consensus_state=self.mock_consensus_state, poet_settings_view=self.mock_poet_settings_view)
def test_serialization(self): # Need to create signup information and wait timer first signup_info = \ SignupInfo.create_signup_info( poet_enclave_module=self.poet_enclave_module, originator_public_key_hash=self._originator_public_key_hash, nonce=NULL_BLOCK_IDENTIFIER) # create mock_poet_enclave_wait_timer mock_poet_enclave_wait_timer = \ mock.Mock(validator_address='1060 W Addison Street', duration=1.0, previous_certificate_id=NULL_BLOCK_IDENTIFIER, local_mean=5.0, signature='00112233445566778899aabbccddeeff', serialized_timer=None, request_time=time.time()) # create mock_poet_enclave_wait_certificate mock_poet_enclave_wait_certificate = \ mock.Mock(duration=1.0, previous_certificate_id=NULL_BLOCK_IDENTIFIER, local_mean=5.0, request_time=time.time(), validator_address='1060 W Addison Street', nonce=NULL_BLOCK_IDENTIFIER, block_hash="Reader's Digest", signature='00112233445566778899aabbccddeeff', serialized_certificate=None) # create mock_poet_enclave_module mock_poet_enclave_module = mock.Mock() mock_poet_enclave_module.create_wait_timer.return_value = \ mock_poet_enclave_wait_timer # set the mock enclave wait certificate and wait timer to # have the same previous_certificate_id mock_poet_enclave_wait_certificate.previous_certificate_id = \ mock_poet_enclave_wait_timer.previous_certificate_id mock_poet_enclave_module.create_wait_certificate.return_value = \ mock_poet_enclave_wait_certificate mock_poet_enclave_module.deserialize_wait_certificate.return_value = \ mock_poet_enclave_wait_certificate wt = \ WaitTimer.create_wait_timer( poet_enclave_module=mock_poet_enclave_module, validator_address='1660 Pennsylvania Avenue NW', previous_certificate_id=NULL_BLOCK_IDENTIFIER, consensus_state=self.mock_consensus_state, poet_settings_view=self.mock_poet_settings_view) # Now we can create a wait certificate and serialize wc = \ WaitCertificate.create_wait_certificate( poet_enclave_module=mock_poet_enclave_module, 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( poet_enclave_module=mock_poet_enclave_module, serialized=dumped.get('SerializedCertificate'), signature=dumped.get('Signature')) self.assertEqual(wc.previous_certificate_id, wc_copy.previous_certificate_id) self.assertEqual(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(poet_enclave_module=mock_poet_enclave_module, previous_certificate_id=NULL_BLOCK_IDENTIFIER, poet_public_key=signup_info.poet_public_key, consensus_state=self.mock_consensus_state, poet_settings_view=self.mock_poet_settings_view)