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 blockchain using following rules - chain must start with genesis block - blocks must be formatted correctly ''' if chain[0].__dict__ != Block.genesis( ).__dict__: # Python can't check equality of two objects, but dictionaries raise Exception('The genesis block must be valid') for i in range(1, len(chain)): block = chain[i] last_block = chain[i - 1] Block.is_valid_block(last_block, block)
def test_is_valid_block_bad_block_hash(last_block, block): block.hash = '000000000000000111abc' with pytest.raises(Exception, match='The block hash must be correct'): Block.is_valid_block(last_block, block)
def test_is_valid_block_bad_proof_of_work(last_block, block): block.hash = 'fff' with pytest.raises(Exception, match='The proof of work requirement 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='The block last hash must be correct' ): # Tells python that we are expecting this part of code to raise an exception with given exception message Block.is_valid_block(last_block, block)
def test_is_valid_block(last_block, block): Block.is_valid_block(last_block, block)