Beispiel #1
0
def test_slowly_mined_block():
    last_block = Block.mine_block(Block.genesis(), 'foo')
    #sleep(MINE_RATE/SECONDS) cause that mine algorythm will decrease difficulty
    time.sleep(MINE_RATE / SECONDS)
    mined_block = Block.mine_block(last_block, 'bar')

    assert mined_block.difficulty == last_block.difficulty - 1
Beispiel #2
0
def test_genesis():
    genesis = Block.genesis()

    assert isinstance(genesis, Block)

    for key, value in GENESIS_DATA.items():
        getattr(genesis, key) == value
Beispiel #3
0
def test_mine_block():
    last_block = Block.genesis()
    data = 'test-data'
    block = Block.mine_block(last_block, data)

    assert isinstance(block, Block)
    assert block.data == data
    assert block.last_hash == last_block.hash
    assert hex_to_binary(
        block.hash)[0:block.difficulty] == '0' * block.difficulty
Beispiel #4
0
    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():
            raise Exception('The genesis block must be valid')

        # skipping genesis block in validaiton process
        for i in range(1, len(chain)):
            block = chain[i]
            last_block = chain[i - 1]
            Block.is_valid_block(last_block, block)

        Blockchain.is_valid_transaction_chain(chain)
Beispiel #5
0
 def __init__(self):
     self.chain = [Block.genesis()]
Beispiel #6
0
def last_block():
    return Block.genesis()
Beispiel #7
0
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