Exemplo n.º 1
0
def test_mine_block_no_competition(nodes, network, test_transaction):
    response = req.get(nodes[1] + '/blockchain/get_chain')
    bc_dict = response.json()
    expected_bc = Blockchain.from_dict(bc_dict)

    mine_resp = req.get(nodes[1] + '/blockchain/mine_block')
    block_dict = mine_resp.json()
    block = Block.from_dict(block_dict)
    assert mine_resp.status_code == 200
    assert Blockchain.is_valid_proof(block, block_dict['hash'], 3)
    assert not isinstance(expected_bc.add_block(block), str)

    response = req.post(nodes[1] + '/blockchain/add_block?broadcast=1',
                        json=block_dict)
    assert response.status_code == 200
    data = response.json()
    assert all([
        msg.endswith('accepted immediately.')
        for msg in data['network_messages']
    ])

    for i in range(len(nodes)):
        response = req.get(nodes[i] + '/blockchain/get_chain')
        bc_dict = response.json()
        bc = Blockchain.from_dict(bc_dict)
        assert bc == expected_bc
Exemplo n.º 2
0
def test_setup_node(nodes, get_info):
    bc = Blockchain()
    nw = [dict(id=5, ip='0.0.0.0:6666', public_key='123')]
    response = req.post(nodes[1] + '/setup_node',
                        json=dict(network=nw, blockchain=bc.to_dict()))
    assert response.status_code == 200

    info = get_info(1)
    assert info['chain_length'] == 1
    assert info['network'] == nw
Exemplo n.º 3
0
def test_chain_dump(test_trans_ins_outs):
    bch = Blockchain()
    tx, ins, outs = test_trans_ins_outs
    bch.add_transaction(tx)
    bch.mine()
    dump = bch.to_dict()
    bch_resurrected = Blockchain.from_dict(dump)
    assert not isinstance(bch_resurrected, str)
    assert bch.chain == bch_resurrected.chain
Exemplo n.º 4
0
def test_last_block(nodes, network):
    response = req.get(nodes[2] + '/blockchain/get_chain')
    bc = Blockchain.from_dict(response.json())

    response = req.get(nodes[2] + '/blockchain/get_last_block')
    last_block = Block.from_dict(response.json())

    assert last_block in bc
Exemplo n.º 5
0
def test_get_chain(test_client, node_setup, test_block):
    response = test_client.get('blockchain/get_chain')
    assert_json_200(response)
    data = response.get_json()
    bc = Blockchain.from_dict(data)

    assert len(node.blkchain) == data['length']
    assert bc == node.blkchain
Exemplo n.º 6
0
def setup_node():
    data = request.get_json()
    try:
        node.blkchain = Blockchain.from_dict(data['blockchain'])
    except (KeyError, TypeError):
        response, status = dict('Invalid blockchain JSON provided.'), 400
        return jsonify(response), status

    node.network = data['network']

    response, status = dict(message='Node setup complete.'), 200
    return jsonify(response), status
Exemplo n.º 7
0
def test_mine_block_with_competition(nodes, network, test_transaction):
    pool = Pool(processes=len(nodes))
    args = [(i, nodes) for i in range(len(nodes))]
    result = pool.map_async(mine_and_broadcast, args)
    responses = result.get(timeout=5)

    bcs = []
    for i in range(len(nodes)):
        network_messages = responses[i].get('network_messages')

        resp_chain = req.get(nodes[i] + '/blockchain/get_chain')
        bcs.append(Blockchain.from_dict(resp_chain.json()))

    assert all([bc == bcs[0] for bc in bcs])
Exemplo n.º 8
0
def node_setup():
    node.wallet = Wallet()
    node.node_id = 0
    node.blkchain = Blockchain()
    node.blkchain.utxos = {}

    address = node.wallet.address
    node.blkchain.utxos[address] = [
        TransactionOutput('0', address, 10),
        TransactionOutput('1', address, 10)
    ]
    node.blkchain.utxos['0'] = []

    node.network = [
        dict(public_key=node.wallet.public_key),
        dict(public_key='0')
    ]
def test_create_transaction(nodes, network):
    # Create document
    response = req.post(
        nodes[0] + '/transactions/create',
        json=dict(
            sender_address=network[0]['public_key'],
            recipient_address=network[1]['public_key'],
            amount=10
        )
    )
    assert response.status_code == 200
    tx_dict = response.json()
    tx = Transaction.from_dict(tx_dict)
    assert any(to.amount == 10 for to in tx.transaction_outputs)
    assert any(to.amount == 290 for to in tx.transaction_outputs)

    # Sign document
    response = req.post(
        nodes[0] + '/transactions/sign',
        json=tx_dict
    )
    assert response.status_code == 200
    data = response.json()
    signature = data['signature']

    # Submit to blockchain
    response = req.post(
        nodes[0] + '/transactions/submit?broadcast=1',
        json=dict(
            transaction=tx_dict,
            signature=signature
        )
    )
    assert response.status_code == 200

    # Test if it reached the others
    for i in range(0, 2):
        response = req.get(nodes[i] + '/blockchain/get_chain')
        assert response.status_code == 200

        bc_dict = response.json()
        bc = Blockchain.from_dict(bc_dict)
        assert tx in bc.unconfirmed_transactions
        for to in tx.transaction_outputs:
            assert to in bc.utxos[to.recipient_address]
Exemplo n.º 10
0
def test_blockchain_mine(test_trans_ins_outs):
    bch = Blockchain()
    tx, ins, outs = test_trans_ins_outs

    bch.add_transaction(tx)
    result = bch.add_block(bch.mine())

    assert not isinstance(result, str)
    assert len(bch.chain) == 2
    assert tx in bch.last_block.transactions
    assert bch.last_block.nonce
    assert bch.last_block.hash.startswith(bch.pow_difficulty * '0')
    assert bch.last_block.previous_hash == bch.chain[0].hash
    assert not bch.unconfirmed_transactions
Exemplo n.º 11
0
def setup_bootstrap():
    data = request.get_json()

    node.node_id = 0
    node.wallet = Wallet()
    init_tx = Transaction('0', node.wallet.public_key, data['initial_amount'])
    init_out = TransactionOutput(init_tx.transaction_id, node.wallet.public_key, data['initial_amount'])
    node.blkchain = Blockchain(
        create_genesis=True,
        initial_transaction=init_tx
    )
    node.blkchain.utxos[node.wallet.public_key] = [init_out]
    # print(node.blkchain.utxos)
    node.network = [dict(
        id=node.node_id,
        ip=request.host_url[:-1],
        public_key=node.wallet.public_key
    )]

    response, status = dict(message='Bootstrap node setup complete.'), 200
    return make_response(jsonify(response)), 200
Exemplo n.º 12
0
def get_longest_blockchain():
    cur_length = len(node.blkchain)
    cur_last_hash = node.blkchain.last_block.hash
    ret = node.blkchain
    for node_ in node.network:
        if not node_['id'] == node.node_id:
            response = requests.get(node_['ip'] + '/blockchain/get_chain')
            dump = response.json()
            chain_length = dump['length']
            chain_last_hash = dump['chain'][-1]['hash']

            # Imply stricter ordering using last block hash to ensure the same chain will always prevail.
            if cur_length < chain_length or (cur_length == chain_length and
                                             cur_last_hash < chain_last_hash):
                bc = Blockchain.from_dict(
                    dump)  # Parse the chain to ensure validity
                if isinstance(bc, Blockchain):
                    ret = bc
                    cur_length = dump['length']

    return ret