예제 #1
0
def test_mined_block_difficulty_limits_at_1():
    last_block = Block(time.time_ns(), 'test_last_hash', 'test_hash',
                       'test_data', 1, 0)

    time.sleep(MINE_RATE / SECONDS)
    mined_block = Block.mine_block(last_block, 'bar')
    assert mined_block.difficulty == 1
예제 #2
0
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='difficulty must only adjust by 1'):
        Block.is_valid_block(last_block, block)
예제 #3
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
예제 #4
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
예제 #5
0
def test_genesis():
    genesis = Block.genesis()

    assert isinstance(genesis, Block)

    for key, value in GENESIS_DATA.items():
        getattr(genesis, key) == value
예제 #6
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)
예제 #7
0
 def from_json(chain_json):
     """
     Deserialize a list of serialized blocks into a Blockchain instance.
     The results will containg a chain list of Block instances.
     """
     blockchain = Blockchain()
     blockchain.chain = list(
         map(lambda block_json: Block.from_json(block_json), chain_json))
     return blockchain
예제 #8
0
    def message(self, pubnub, message_object):
        print(
            f'\n-- Channel: {message_object.channel} | Message: {message_object.message}'
        )

        if message_object.channel == CHANNELS['BLOCK']:
            block = Block.from_json(message_object.message)
            # [:] -> all the elements in the array, in that case all blocks in chain
            potential_chain = self.blockchain.chain[:]
            potential_chain.append(block)

            try:
                self.blockchain.replace_chain(potential_chain)
                self.transaction_pool.clear_blockchain_transactions(
                    self.blockchain)
                print('\n -- Successfully replaced the local chain')
            except Exception as e:
                print(f'\n -- Did not replace chain: {e}')
        elif message_object.channel == CHANNELS['TRANSACTION']:
            transaction = Transaction.from_json(message_object.message)
            self.transaction_pool.set_transaction(transaction)
            print('\n -- Set the new transaction in the transaction pool')
예제 #9
0
def last_block():
    return Block.genesis()
예제 #10
0
def test_is_valid_block_bad_proof_of_work(last_block, block):
    block.hash = 'fff'

    with pytest.raises(Exception,
                       match='proof of work requirement was not meet'):
        Block.is_valid_block(last_block, block)
예제 #11
0
 def add_block(self, data):
     self.chain.append(Block.mine_block(self.chain[-1], data))
예제 #12
0
def block(last_block):
    return Block.mine_block(last_block, 'test_data')
예제 #13
0
 def __init__(self):
     self.chain = [Block.genesis()]
예제 #14
0
def test_is_valid_block_bad_block_hash(last_block, block):
    block.hash = '0000000000bbbabc'

    with pytest.raises(Exception, match='block hash must be correct'):
        Block.is_valid_block(last_block, block)
예제 #15
0
def test_is_valid_block(last_block, block):
    Block.is_valid_block(last_block, block)
예제 #16
0
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)
예제 #17
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