Adds get_block_count endpoint and generate_dummy_block. Updates README accordingly

This commit is contained in:
Sergi Delgado Segura
2019-07-11 14:51:33 +02:00
parent d24368e505
commit 9cf0c9786d
3 changed files with 62 additions and 23 deletions

View File

@@ -15,7 +15,7 @@ Refer to [INSTALL.md](INSTALL.md)
#### Global options
- `-s, --server`: API server where to send the requests. Defaults to 35.177.25.32 (modifiable in \_\_init\_\_.py)
- `-s, --server`: API server where to send the requests. Defaults to btc.pisa.watch (modifiable in \_\_init\_\_.py)
- `-p, --port` : API port where to send the requests. Defaults to 9814 (modifiable in \_\_init\_\_.py)
- `-d, --debug`: shows debug information and stores it in pisa.log
- `-h --help`: shows a list of commands or help for a specific command.
@@ -66,11 +66,19 @@ An example of a json encoded appointment file can be found in `example_appointme
#### Example
Modify the provided `example_appointment_data.json` to make `start_time` and `end_time` match some future blocks.
The easier way to generate an appointment is using the testing command `generate_dummy_appointment`.
Run:
`python pisa-cli.py add_appointment -f example_appointment_data.json`
`python pisa-cli.py generate_dummy_appointment`
That will create a json file that follows the appointment data structure filled with dummy data and store it as `dummy_appointment_data.json`.
To send the appointment to the PISA server run:
`python pisa-cli.py add_appointment -f dummy_appointment_data.json `
**Notice that this appointment will never be fulfilled (it will eventually expire) since it does not , but will give you an easy wat to interact with the API.**
### get_appointment

View File

@@ -7,7 +7,7 @@ import requests
from sys import argv
from getopt import getopt, GetoptError
from hashlib import sha256
from binascii import unhexlify
from binascii import hexlify, unhexlify
from requests import ConnectTimeout, ConnectionError
from apps.cli import DEFAULT_PISA_API_SERVER, DEFAULT_PISA_API_PORT, CLIENT_LOG_FILE
from apps.cli.blob import Blob
@@ -21,6 +21,20 @@ def show_message(message, debug, logging):
sys.exit(message)
def generate_dummy_appointment():
# FIXME: TESTING ENDPOINT, WON'T BE THERE IN PRODUCTION
get_block_count_end_point = "http://{}:{}/get_block_count".format(pisa_api_server, pisa_api_port)
r = requests.get(url=get_block_count_end_point, timeout=5)
current_height = r.json().get("block_count")
dummy_appointment_data = {"tx": hexlify(os.urandom(192)).decode('utf-8'),
"tx_id": hexlify(os.urandom(32)).decode('utf-8'), "start_time": current_height + 5,
"end_time": current_height + 10, "dispute_delta": 20}
json.dump(dummy_appointment_data, open('dummy_appointment_data.json', 'w'))
def add_appointment(args, debug, logging):
appointment_data = None
use_help = "Use 'help add_appointment' for help of how to use the command."
@@ -76,7 +90,6 @@ def add_appointment(args, debug, logging):
def get_appointment(args, debug, logging):
if args:
arg_opt = args.pop(0)
@@ -90,7 +103,7 @@ def get_appointment(args, debug, logging):
get_appointment_endpoint = "http://{}:{}/get_appointment".format(pisa_api_server, pisa_api_port)
parameters = "?locator={}".format(locator)
try:
r = requests.get(url=get_appointment_endpoint+parameters, timeout=5)
r = requests.get(url=get_appointment_endpoint + parameters, timeout=5)
print(json.dumps(r.json(), indent=4, sort_keys=True))
@@ -103,7 +116,7 @@ def get_appointment(args, debug, logging):
else:
show_message("The provided locator is not valid.", debug, logging)
else:
show_message("The provided locator is not valid.", debug, logging)
show_message("The provided locator is not valid.", debug, logging)
def build_appointment(tx, tx_id, start_block, end_block, dispute_delta, debug, logging):
@@ -120,7 +133,7 @@ def build_appointment(tx, tx_id, start_block, end_block, dispute_delta, debug, l
appointment = {"locator": locator, "start_time": start_block, "end_time": end_block,
"dispute_delta": dispute_delta, "encrypted_blob": encrypted_blob, "cipher": cipher, "hash_function":
hash_function}
hash_function}
return appointment
@@ -133,19 +146,19 @@ def check_txid_format(txid):
def show_usage():
return('USAGE: '
'\n\tpython pisa-cli.py [global options] command [command options] [arguments]'
'\n\nCOMMANDS:'
'\n\tadd_appointment \tRegisters a json formatted appointment to the PISA server.'
'\n\tget_appointment \tGets json formatted data about an appointment from the PISA server.'
'\n\thelp \t\t\tShows a list of commands or help for a specific command.'
return ('USAGE: '
'\n\tpython pisa-cli.py [global options] command [command options] [arguments]'
'\n\nCOMMANDS:'
'\n\tadd_appointment \tRegisters a json formatted appointment to the PISA server.'
'\n\tget_appointment \tGets json formatted data about an appointment from the PISA server.'
'\n\thelp \t\t\tShows a list of commands or help for a specific command.'
'\n\nGLOBAL OPTIONS:'
'\n\t-s, --server \tAPI server where to send the requests. Defaults to 35.177.25.32 (modifiable in '
'__init__.py)'
'\n\t-p, --port \tAPI port where to send the requests. Defaults to 9814 (modifiable in __init__.py)'
'\n\t-d, --debug \tshows debug information and stores it in pisa.log'
'\n\t-h --help \tshows this message.')
'\n\nGLOBAL OPTIONS:'
'\n\t-s, --server \tAPI server where to send the requests. Defaults to btc.pisa.watch (modifiable in '
'__init__.py)'
'\n\t-p, --port \tAPI port where to send the requests. Defaults to 9814 (modifiable in __init__.py)'
'\n\t-d, --debug \tshows debug information and stores it in pisa.log'
'\n\t-h --help \tshows this message.')
if __name__ == '__main__':
@@ -153,6 +166,7 @@ if __name__ == '__main__':
pisa_api_server = DEFAULT_PISA_API_SERVER
pisa_api_port = DEFAULT_PISA_API_PORT
commands = ['add_appointment', 'get_appointment', 'help']
testing_commands = ['generate_dummy_appointment']
try:
opts, args = getopt(argv[1:], 's:p:dh', ['server', 'port', 'debug', 'help'])
@@ -204,6 +218,11 @@ if __name__ == '__main__':
else:
sys.exit(show_usage())
# FIXME: testing command, not for production
elif command in testing_commands:
if command == 'generate_dummy_appointment':
generate_dummy_appointment()
else:
show_message("Unknown command. Use help to check the list of available commands.", debug, logging)
else:
@@ -213,5 +232,3 @@ if __name__ == '__main__':
show_message(e, debug, logging)
except json.JSONDecodeError as e:
show_message('Non-JSON encoded appointment passed as parameter.', debug, logging)

View File

@@ -5,6 +5,12 @@ from pisa.appointment import Appointment
from flask import Flask, request, Response, abort, jsonify
import json
# FIXME: HERE FOR TESTING (get_block_count). REMOVE WHEN REMOVING THE FUNCTION
from utils.authproxy import AuthServiceProxy
from conf import BTC_RPC_USER, BTC_RPC_PASSWD, BTC_RPC_HOST, BTC_RPC_PORT
app = Flask(__name__)
HTTP_OK = 200
HTTP_BAD_REQUEST = 400
@@ -49,7 +55,7 @@ def add_appointment():
return Response(response, status=rcode, mimetype='text/plain')
# FIXME: THE NEXT TWO API ENDPOINTS ARE FOR TESTING AND SHOULD BE REMOVED / PROPERLY MANAGED BEFORE PRODUCTION!
# FIXME: THE NEXT THREE API ENDPOINTS ARE FOR TESTING AND SHOULD BE REMOVED / PROPERLY MANAGED BEFORE PRODUCTION!
@app.route('/get_appointment', methods=['GET'])
def get_appointment():
locator = request.args.get('locator')
@@ -106,6 +112,14 @@ def get_all_appointments():
return response
@app.route('/get_block_count', methods=['GET'])
def get_block_count():
bitcoin_cli = AuthServiceProxy("http://%s:%s@%s:%d" % (BTC_RPC_USER, BTC_RPC_PASSWD, BTC_RPC_HOST,
BTC_RPC_PORT))
return jsonify({"block_count": bitcoin_cli.getblockcount()})
def start_api(d, l):
# FIXME: Pretty ugly but I haven't found a proper way to pass it to add_appointment
global debug, logging, watcher, inspector