in pyqldbsamples/get_block.py [0:0]
def verify_block(ledger_name, block_address):
"""
Verify block by validating the proof returned in the getBlock response.
:type ledger_name: str
:param ledger_name: The ledger to get digest from.
:type block_address: str/:py:class:`amazon.ion.simple_types.IonPyDict`
:param block_address: The address of the block to verify.
:raises AssertionError: When verification failed.
"""
logger.info("Let's verify blocks for ledger with name={}.".format(ledger_name))
try:
logger.info("First, let's get a digest.")
digest_result = get_digest_result(ledger_name)
digest_tip_address = digest_result.get('DigestTipAddress')
digest_bytes = digest_result.get('Digest')
logger.info('Got a ledger digest. Digest end address={}, digest={}'.format(
value_holder_to_string(digest_tip_address.get('IonText')), to_base_64(digest_bytes)))
get_block_result = get_block_with_proof(ledger_name, block_address_to_dictionary(block_address),
digest_tip_address)
block = get_block_result.get('Block')
block_hash = parse_block(block)
verified = verify_document(block_hash, digest_bytes, get_block_result.get('Proof'))
if not verified:
raise AssertionError('Block is not verified!')
else:
logger.info('Success! The block is verified.')
altered_digest = flip_random_bit(digest_bytes)
logger.info("Let's try flipping one bit in the digest and assert that the block is NOT verified. "
"The altered digest is: {}".format(to_base_64(altered_digest)))
verified = verify_document(block_hash, altered_digest, get_block_result.get('Proof'))
if verified:
raise AssertionError('Expected block to not be verified against altered digest.')
else:
logger.info('Success! As expected flipping a bit in the digest causes verification to fail.')
altered_block_hash = flip_random_bit(block_hash)
logger.info("Let's try flipping one bit in the block's hash and assert that the block is NOT verified. "
"The altered block hash is: {}.".format(to_base_64(altered_block_hash)))
verified = verify_document(altered_block_hash, digest_bytes, get_block_result.get('Proof'))
if verified:
raise AssertionError('Expected altered block hash to not be verified against digest.')
else:
logger.info('Success! As expected flipping a bit in the block hash causes verification to fail.')
except Exception as e:
logger.exception('Failed to verify blocks in the ledger with name={}.'.format(ledger_name))
raise e