def test_replace_chain_invalid(populated_blockchain): blockchain = Blockchain() populated_blockchain.chain[1].hash = 'bad_hash' with pytest.raises(Exception, match='Cannot replace! Incoming chain is not valid!'): blockchain.replace_chain(populated_blockchain.chain)
def test_replace_chain_chain_is_invalid(blockchain_three_blocks): bc = Blockchain() blockchain_three_blocks.chain[1].hash = 'evil hash' with pytest.raises(Exception, match='Cannot replace. The incoming chain is invalid'): bc.replace_chain(blockchain_three_blocks.chain)
def test_replace_chain_bad_chain(blockchain_three_blocks): blockchain = Blockchain() blockchain_three_blocks.chain[1].hash = 'evil_hash' with pytest.raises(Exception, match='Cannot replace, the incoming chain is invalid'): blockchain.replace_chain(blockchain_three_blocks.chain)
def test_replace_chain(): blockchain = Blockchain() blockchain_three_blocks = Blockchain() for i in range(3): blockchain_three_blocks.add_block(i) blockchain.replace_chain(blockchain_three_blocks.chain) assert blockchain_three_blocks.chain == blockchain.chain
def test_replace_chain_not_longer(): blockchain_three_blocks = Blockchain() for i in range(3): blockchain_three_blocks.add_block(i) blockchain = Blockchain() with pytest.raises(Exception, match="Cannot replace. The incomming chain must be longer"): blockchain_three_blocks.replace_chain(blockchain.chain)
def test_replace_chain_bad_chain(): blockchain = Blockchain() blockchain_three_blocks = Blockchain() for i in range(3): blockchain_three_blocks.add_block(i) blockchain_three_blocks.chain[1].hash = 'bad-hash' with pytest.raises(Exception, match="Cannot replace. The incomming chain is invalid"): blockchain.replace_chain(blockchain_three_blocks.chain)
def test_replace_chain(blockchain_3b): ''' Purpose: Test whether a given blockchain can perform Chain Replacement. Assert that chain replacement can be done if the replacement-chain is longer than the replaced-chain and is formatted correctly. ''' blockchain = Blockchain() blockchain.replace_chain(blockchain_3b.chain) assert(blockchain.chain == blockchain_3b.chain)
def test_replace_chain_invalid_chain(blockchain_3b): ''' Purpose: Test whether a given blockchain can perform Chain Replacement. Assert that chain replacement can not be done if the replacement-chain is invalid. ''' blockchain = Blockchain() blockchain_3b.chain[1].hash = 'test-bad-hash' with pytest.raises(Exception, match='incoming chain is invalid'): blockchain.replace_chain(blockchain_3b.chain)
def test_replace_cahin_invalid(blockchain_three_blocks): blockchain = Blockchain() blockchain_three_blocks.chain[1].hash = 'evil_hash' for index in range(0, len(blockchain_three_blocks.chain)): temp_chain = blockchain_three_blocks.chain[:] temp_chain[index].hash = 'evil_hash' with pytest.raises(Exception, match='incoming chain is invalid'): blockchain.replace_chain(temp_chain)
def test_replace_chain_bad_chain(blockchain_three_blocks): blockchain = Blockchain() blockchain_three_blocks.chain[1].hash = 'corrupted_hash' with pytest.raises(Exception, match='The incoming chain is invalid'): blockchain.replace_chain(blockchain_three_blocks.chain)
def test_replace_chain(foo_chain): blockchain = Blockchain() blockchain.replace_chain(foo_chain.chain) assert blockchain.chain == foo_chain.chain
def test_replace_chain_bad_chain(foo_chain): blockchain = Blockchain() foo_chain.chain[1].data = "foo" with pytest.raises(Exception, match="The chain is invalid:"): blockchain.replace_chain(foo_chain.chain)
return block.to_json() @app.route("/wallet/transact", methods=['POST']) def route_wallet_transact(): transaction_data = request.get_json() transaction = Transaction(sender_wallet, transaction_data['recipient'], transaction_data['amount']) print(f"{transaction.to_json()}") return jsonify(transaction.to_json()) ROOT_PORT = 5000 PORT = ROOT_PORT if os.environ.get("PEER") == "True": PORT = random.randint(5001, 6000) response = requests.get(f"http://localhost:{ROOT_PORT}/blockchain") root_blockchain = Blockchain.from_json(response.json()) try: blockchain.replace_chain(root_blockchain) print(f"Blockchain synchronized successfully\n") except Exception as e: print(f"Failed to synchronize blockchain !!!\n Error: {e}") app.run(port=PORT)
def test_replace_blockchain_invalid_chain(blockchain_four_blocks): blockchain = Blockchain() blockchain_four_blocks.chain[1].hash = 'wrong_hash' with pytest.raises(Exception, match = 'Incoming chain is invalid'): blockchain.replace_chain(blockchain_four_blocks.chain)
def test_replace_chain(blockchain_three_blocks): blockchain = Blockchain() blockchain.replace_chain(blockchain_three_blocks.chain) assert blockchain.chain == blockchain_three_blocks.chain
def test_replace_chain_bad_chain(blockchain_three_blocks): blockchain = Blockchain() blockchain_three_blocks.chain[1].hash = "evil_hash" with pytest.raises(Exception, match="The incoming chain is invalid."): blockchain.replace_chain(blockchain_three_blocks.chain)
def test_replace_chain_bad_chain(blockchain_three_blocks): blockchain = Blockchain() blockchain_three_blocks.chain[1].hash = 'evil' with pytest.raises(Exception): blockchain.replace_chain(blockchain_three_blocks.chain)
'wallet_address': wallet.address, 'balance': wallet.balance }) 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') new_blockchain = Blockchain.from_json(result.json()) try: blockchain.replace_chain(new_blockchain.chain) print('Synchronized the local blockchain') except Exception as e: print(f'Synchronization error: {e}') for i in range(10): blockchain.add_block([ Transaction.new_transaction(Wallet(), Wallet().address, random.randrange(10, 40, 10)).to_json(), Transaction.new_transaction(Wallet(), Wallet().address, random.randrange(10, 40, 10)).to_json() ]) for i in range(5):
def test_replace_chain_bad_chain(blockchain_three_blocks): blockchain = Blockchain() blockchain_three_blocks.chain[0].hash = '000000000addda' with pytest.raises(Exception, match='Cannot replace. The incoming chain is invalid'): blockchain.replace_chain(blockchain_three_blocks.chain)
def test_replace_chain(populated_blockchain): blockchain = Blockchain() blockchain.replace_chain(populated_blockchain.chain) assert blockchain.chain == populated_blockchain.chain
def test_replace_chain_invalid_chain(blockchain_with_blocks): blockchain = Blockchain() blockchain_with_blocks.chain[2].hash = "tampered_hash" with pytest.raises(Exception, match="chain is not valid"): blockchain.replace_chain(blockchain_with_blocks.chain)
def test_replace_chain(blockchain): old_blockchain = Blockchain() old_blockchain.replace_chain(blockchain.chain) assert old_blockchain.chain == blockchain.chain
def test_replace_chain_bad_chain(blockchain): old_blockchain = Blockchain() blockchain.chain[1].data = 'evil_hash' with raises(Exception, match='Invalid incoming chain'): old_blockchain.replace_chain(blockchain.chain)
else: transaction = Transaction(wallet, transaction_data["recipient"], transaction_data["amount"]) pubsub.broadcast_transaction(transaction) return jsonify(transaction.to_json()) @app.route("/wallet/info") def route_wallet_info(): return jsonify({"address": wallet.address, "balance": wallet.balance}) 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") result_blockchain = Blockchain.from_json(result.json()) try: blockchain.replace_chain(result_blockchain.chain) print("\n -- Successfully synchronised the local chain") except Exception as e: print(f"\n -- Error synchronising: {e}") app.run(port=PORT)
def test_replace_chain_bad_chain(blockchain_three_blocks): blockchain = Blockchain() blockchain_three_blocks.chain[1] = 'very_bad_chain' with pytest.raises(Exception, match='The incomming chain is invalid'): blockchain.replace_chain(blockchain_three_blocks.chain)