def test_is_valid_block_jumped_difficulty(last_block, block): jumped_difficulty = 10 block.difficulty = jumped_difficulty block.hash = f'{"0" * jumped_difficulty}111abc' with pytest.raises(Exception, match='The block difficulty must only adjust by 1.'): Block.is_valid_block(last_block, block)
def is_valid_chain(chain): """ Validate the incoming chain. Enforce the following rules of the blockchain: - the chain must start with the genesis block. - blocks must be formatted correctly. """ if chain[0] != Block.genesis_block(): raise Exception('The genesis block must be valid.') for i in range(1, len(chain)): block = chain[i] # Previous block: last_block = chain[i - 1] Block.is_valid_block(last_block, block) Blockchain.is_valid_transaction_chain(chain)
def test_is_valid_block_bad_block_hash(last_block, block): block.hash = '000000000000000012abbbccccbc' with pytest.raises(Exception, match='The block hash must be correct.'): Block.is_valid_block(last_block, block)
def test_invalid_block_bad_proof_of_work(last_block, block): block.hash = 'fff' with pytest.raises(Exception, match='The proof of requirement was not met.'): Block.is_valid_block(last_block, block)
def test_is_valid_block_bad_last_hash(last_block, block): block.last_hash = 'evil_last_hash' with pytest.raises(Exception, match='last_hash must be correct'): Block.is_valid_block(last_block, block)
def test_is_valid_block(last_block, block): Block.is_valid_block(last_block, block)