diff --git a/tests/simulator/__init__.py b/tests/simulator/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/simulator/appointment_test.py b/tests/simulator/appointment_test.py new file mode 100644 index 0000000..2af8496 --- /dev/null +++ b/tests/simulator/appointment_test.py @@ -0,0 +1,52 @@ +import os +import json +import requests +import time +from hashlib import sha256 +from binascii import hexlify, unhexlify +from apps.cli.blob import Blob +from pisa import HOST, PORT +from pisa.utils.authproxy import AuthServiceProxy +from pisa.conf import BTC_RPC_USER, BTC_RPC_PASSWD, BTC_RPC_HOST, BTC_RPC_PORT + +PISA_API = "http://{}:{}".format(HOST, PORT) + + +def generate_dummy_appointment(dispute_txid): + r = requests.get(url=PISA_API+'/get_block_count', timeout=5) + + current_height = r.json().get("block_count") + + dummy_appointment_data = {"tx": hexlify(os.urandom(32)).decode('utf-8'), + "tx_id": dispute_txid, "start_time": current_height + 5, + "end_time": current_height + 10, "dispute_delta": 20} + + cipher = "AES-GCM-128" + hash_function = "SHA256" + + locator = sha256(unhexlify(dummy_appointment_data.get("tx_id"))).hexdigest() + blob = Blob(dummy_appointment_data.get("tx"), cipher, hash_function) + + encrypted_blob = blob.encrypt((dummy_appointment_data.get("tx_id")), debug=False, logging=False) + + appointment = {"locator": locator, "start_time": dummy_appointment_data.get("start_time"), + "end_time": dummy_appointment_data.get("end_time"), + "dispute_delta": dummy_appointment_data.get("dispute_delta"), + "encrypted_blob": encrypted_blob, "cipher": cipher, "hash_function": hash_function} + + return appointment + + +dispute_txid = hexlify(os.urandom(32)).decode('utf-8') +appointment = generate_dummy_appointment(dispute_txid) + +print("Sending appointment (locator: {}) to PISA".format(appointment.get("locator"))) +r = requests.post(url=PISA_API, json=json.dumps(appointment), timeout=5) +print(r, r.reason) + +print("Sleeping 10 sec") +time.sleep(10) +bitcoin_cli = AuthServiceProxy("http://%s:%s@%s:%d" % (BTC_RPC_USER, BTC_RPC_PASSWD, BTC_RPC_HOST, BTC_RPC_PORT)) + +print("Triggering PISA with dispute tx") +bitcoin_cli.sendrawtransaction(dispute_txid) \ No newline at end of file diff --git a/tests/bitcoin_sim_tests.py b/tests/simulator/bitcoin_sim_tests.py similarity index 100% rename from tests/bitcoin_sim_tests.py rename to tests/simulator/bitcoin_sim_tests.py diff --git a/tests/bitcoind_sim.py b/tests/simulator/bitcoind_sim.py similarity index 90% rename from tests/bitcoind_sim.py rename to tests/simulator/bitcoind_sim.py index 01ce712..06cfeae 100644 --- a/tests/bitcoind_sim.py +++ b/tests/simulator/bitcoind_sim.py @@ -1,9 +1,10 @@ from pisa.conf import FEED_PROTOCOL, FEED_ADDR, FEED_PORT from flask import Flask, request, Response, abort -from tests.zmq_publisher import ZMQPublisher +from tests.simulator.zmq_publisher import ZMQPublisher from threading import Thread from pisa.rpc_errors import * from pisa.tools import check_txid_format +import logging import binascii import json import os @@ -38,9 +39,9 @@ def process_request(): getblockcount: the block count will be get from the mining simulator by querying how many blocks have been emited so far. - getblock: querying for a block will return a dictionary with a two fields: "tx" representing a list of - transactions, and "height" representing the block height. Both will be got from the mining - simulator. + getblock: querying for a block will return a dictionary with a three fields: "tx" representing a list + of transactions, "height" representing the block height and "hash" representing the block + hash. Both will be got from the mining simulator. getblockhash: a block hash is only queried by pisad on bootstrapping to check the network bitcoind is running on. @@ -117,6 +118,7 @@ def process_request(): block = blocks.get(blockid) if block: + block["hash"] = blockid response["result"] = block else: @@ -164,6 +166,7 @@ def load_data(): def simulate_mining(): global mempool, mined_transactions, blocks, blockchain + prev_block_hash = None while True: block_hash = binascii.hexlify(os.urandom(32)).decode('utf-8') @@ -179,9 +182,13 @@ def simulate_mining(): for tx in txs_to_mine: mined_transactions[tx] = block_hash - blocks[block_hash] = {"tx": txs_to_mine, "height": len(blockchain)} + blocks[block_hash] = {"tx": txs_to_mine, "height": len(blockchain), "previousblockhash": prev_block_hash} mining_simulator.publish_data(binascii.unhexlify(block_hash)) blockchain.append(block_hash) + prev_block_hash = block_hash + + print("New block mined: {}".format(block_hash)) + print("\tTransactions: {}".format(txs_to_mine)) time.sleep(10) @@ -198,4 +205,7 @@ if __name__ == '__main__': mining_thread = Thread(target=simulate_mining) mining_thread.start() + # Setting Flask log to ERROR only so it does not mess with out logging + logging.getLogger('werkzeug').setLevel(logging.ERROR) + app.run(host=HOST, port=PORT) diff --git a/tests/zmq_publisher.py b/tests/simulator/zmq_publisher.py similarity index 100% rename from tests/zmq_publisher.py rename to tests/simulator/zmq_publisher.py