Example #1
0
def test_mine_block():
    previous_block = Block.generate_genesis()
    data = '123'

    next_block = Block.mine_block(previous_block, data)

    assert isinstance(next_block, Block)
    assert next_block.data == data
    assert previous_block.hash == next_block.previous_hash
    assert hex_to_binary(next_block.hash).startswith('0' *
                                                     next_block.difficulty)
Example #2
0
    def is_valid_chain(chain):
        """
        Checks if incoming chain is valid
        """
        if chain[0] != Block.generate_genesis():
            raise Exception('First block must be genesis block')

        for i in range(1, len(chain)):
            block = chain[i]
            previous_block = chain[i - 1]
            if block.previous_hash != previous_block.hash or not Block.is_valid_proof(
                    block, block.hash):
                return False

        return True
Example #3
0
def test_generate_hash():
    block = Block.generate_genesis()
    assert generate_hash(block) == generate_hash(block)
    assert generate_hash(
        block
    ) == 'a65ae033a322f80df233c856ae2cbb2677b16a69eebeda4c95b3cea25a86092d'
Example #4
0
def test_genesis_block():
    genesis_block = Block.generate_genesis()

    assert isinstance(genesis_block, Block)
    for k, v in GENESIS_DATA.items():
        assert getattr(genesis_block, k) == v
Example #5
0
 def __init__(self):
     """
     Initializes list to store blocks
     """
     self.chain = [Block.generate_genesis()]