Improves simulator and adds basic appointment test

The getblock rpc call was missing the block and previousblock hashes.
This commit is contained in:
Sergi Delgado Segura
2019-08-19 18:18:10 +01:00
parent 51bafa323a
commit 157456f164
5 changed files with 67 additions and 5 deletions

View File

View File

@@ -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)

View File

@@ -1,9 +1,10 @@
from pisa.conf import FEED_PROTOCOL, FEED_ADDR, FEED_PORT from pisa.conf import FEED_PROTOCOL, FEED_ADDR, FEED_PORT
from flask import Flask, request, Response, abort 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 threading import Thread
from pisa.rpc_errors import * from pisa.rpc_errors import *
from pisa.tools import check_txid_format from pisa.tools import check_txid_format
import logging
import binascii import binascii
import json import json
import os 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 getblockcount: the block count will be get from the mining simulator by querying how many blocks have been
emited so far. emited so far.
getblock: querying for a block will return a dictionary with a two fields: "tx" representing a list of getblock: querying for a block will return a dictionary with a three fields: "tx" representing a list
transactions, and "height" representing the block height. Both will be got from the mining of transactions, "height" representing the block height and "hash" representing the block
simulator. 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 getblockhash: a block hash is only queried by pisad on bootstrapping to check the network bitcoind is
running on. running on.
@@ -117,6 +118,7 @@ def process_request():
block = blocks.get(blockid) block = blocks.get(blockid)
if block: if block:
block["hash"] = blockid
response["result"] = block response["result"] = block
else: else:
@@ -164,6 +166,7 @@ def load_data():
def simulate_mining(): def simulate_mining():
global mempool, mined_transactions, blocks, blockchain global mempool, mined_transactions, blocks, blockchain
prev_block_hash = None
while True: while True:
block_hash = binascii.hexlify(os.urandom(32)).decode('utf-8') block_hash = binascii.hexlify(os.urandom(32)).decode('utf-8')
@@ -179,9 +182,13 @@ def simulate_mining():
for tx in txs_to_mine: for tx in txs_to_mine:
mined_transactions[tx] = block_hash 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)) mining_simulator.publish_data(binascii.unhexlify(block_hash))
blockchain.append(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) time.sleep(10)
@@ -198,4 +205,7 @@ if __name__ == '__main__':
mining_thread = Thread(target=simulate_mining) mining_thread = Thread(target=simulate_mining)
mining_thread.start() 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) app.run(host=HOST, port=PORT)