Beispiel #1
0
def testReplaceChainBadChain(Blockchain3Blocks):
    blockchain = Blockchain()
    Blockchain3Blocks.chain[1].hash = 'evil_hash'

    with pytest.raises(Exception, match='The incoming chain is invalid'):
        blockchain.replaceChain(Blockchain3Blocks.chain)
    return jsonify({'address': wallet.address, 'balance': wallet.balance})


@app.route('/blockchain/mine')
def routeBlockchainMine():
    transactionData = transactionPool.transactionData()
    transactionData.append(Trasaction.rewardTransaction(wallet).to_json())
    blockchain.addBlock(transactionData)
    block = blockchain.chain[-1]
    pubsub.broadcastBlock(block)
    transactionPool.clearTransaction(blockchain)

    return jsonify(block.to_json())


ROOT_PORT = 5000
PORT = ROOT_PORT

if os.environ.get('PEER') == True:
    PORT = random.randint(5001, 6000)

    result = requests.get(f'http://localhost:{ROOT_PORT}/blockchain')
    resultBlockchain = Blockchain.fromJson(result.json())
    try:
        blockchain.replaceChain(resultBlockchain.chain)
        print('\n-- Successful synchronized the local chain')
    except Exception as e:
        print(f'\n-- Error synchornized:{e}')

app.run(port=PORT)
Beispiel #3
0
def testReplaceChain(Blockchain3Blocks):
    blockchain = Blockchain()
    blockchain.replaceChain(Blockchain3Blocks.chain)

    assert blockchain.chain == Blockchain3Blocks.chain