def test_surrogate_chain_when_invalid_chain(clone_blockchain): blockchain = Blockchain() clone_blockchain.chain[1].hash = "invalidate" with pytest.raises(Exception, match="Invalid chain."): blockchain.surrogate_chain(clone_blockchain.chain)
@app.route("/transactions") def tx_route(): return jsonify(transaction_pool.serialize_to_json()) ROOT_PORT = 5000 PORT = ROOT_PORT # peer instance, supports up to 1,000 peers if os.environ.get("PEER") == "True": PORT = random.randint(5001, 6000) # fetch blockchain instance res = requests.get(f"http://localhost:{ROOT_PORT}/blockchain") res_blockchain = Blockchain.deserialize_from_json(res.json()) try: blockchain.surrogate_chain(res_blockchain) print("\n -- Successfully synchronized local blockchain instance.") except Exception as err: print(f"\n -- Error synchronizing local blockchain instance. See: {err}") # seeded test instance - creates 10 blocks w/2 Tx ea if os.environ.get("SEED") == "True": for i in range(10): blockchain.add_block([ Transaction(Wallet(),Wallet().address, random.randint(2, 50)).serialize_to_json(), Transaction(Wallet(),Wallet().address, random.randint(2, 50)).serialize_to_json() ]) # set Tx in pool for i in range(3): transaction_pool.set_transaction( Transaction(Wallet(),Wallet().address, random.randint(2, 50))
def test_surrogate_chain(clone_blockchain): blockchain = Blockchain() blockchain.surrogate_chain(clone_blockchain.chain) assert blockchain.chain == clone_blockchain.chain