def test_slowly_mined_block(): last_block = Block.mine_block(Block.genesis(), 'foo') time.sleep( MINE_RATE / SECONDS ) # Delay to ensure it is mined slower than mine_rate, takes argument in second mined_block = Block.mine_block(last_block, 'bar') assert mined_block.difficulty == last_block.difficulty - 1
def test_genesis(): genesis = Block.genesis() assert isinstance(genesis, Block) for key, value in GENESIS_DATA.items(): getattr( genesis, key ) == value # Checks if all values of genesis object are same as that in original dictionary
def test_mine_block(): last_block = Block.genesis() data = 'test-data' block = Block.mine_block(last_block, data) assert isinstance( block, Block ) # Check if the block we created became an instance or object of Block class assert block.data == data assert block.last_hash == last_block.hash assert hex_to_binary( block.hash)[0:block.difficulty] == '0' * block.difficulty
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 last_block(): return Block.genesis()
def test_quickly_mined_block(): last_block = Block.mine_block(Block.genesis(), 'foo') mined_block = Block.mine_block(last_block, 'bar') assert mined_block.difficulty == last_block.difficulty + 1
def __init__(self): self.chain = [Block.genesis()]