Fixes from PR review

This commit is contained in:
Salvatore Ingala
2019-10-10 18:22:33 +07:00
parent aea1d1f1e0
commit 9f25ef8603
12 changed files with 33 additions and 39 deletions

View File

@@ -2,7 +2,6 @@ import re
import os import os
import sys import sys
import json import json
import logging
import requests import requests
from sys import argv from sys import argv
from hashlib import sha256 from hashlib import sha256
@@ -10,11 +9,15 @@ from binascii import unhexlify
from getopt import getopt, GetoptError from getopt import getopt, GetoptError
from requests import ConnectTimeout, ConnectionError from requests import ConnectTimeout, ConnectionError
from pisa.logger import Logger
from apps.cli.blob import Blob from apps.cli.blob import Blob
from apps.cli.help import help_add_appointment, help_get_appointment from apps.cli.help import help_add_appointment, help_get_appointment
from apps.cli import DEFAULT_PISA_API_SERVER, DEFAULT_PISA_API_PORT from apps.cli import DEFAULT_PISA_API_SERVER, DEFAULT_PISA_API_PORT
logger = Logger("Client")
# FIXME: TESTING ENDPOINT, WON'T BE THERE IN PRODUCTION # FIXME: TESTING ENDPOINT, WON'T BE THERE IN PRODUCTION
def generate_dummy_appointment(): def generate_dummy_appointment():
get_block_count_end_point = "http://{}:{}/get_block_count".format(pisa_api_server, pisa_api_port) get_block_count_end_point = "http://{}:{}/get_block_count".format(pisa_api_server, pisa_api_port)
@@ -49,14 +52,14 @@ def add_appointment(args):
if os.path.isfile(fin): if os.path.isfile(fin):
appointment_data = json.load(open(fin)) appointment_data = json.load(open(fin))
else: else:
logging.error("[Client] can't find file " + fin) logger.error("Can't find file " + fin)
else: else:
logging.error("[Client] no file provided as appointment. " + use_help) logger.error("No file provided as appointment. " + use_help)
else: else:
appointment_data = json.loads(arg_opt) appointment_data = json.loads(arg_opt)
except json.JSONDecodeError: except json.JSONDecodeError:
logging.error("[Client] non-JSON encoded data provided as appointment. " + use_help) logger.error("Non-JSON encoded data provided as appointment. " + use_help)
if appointment_data: if appointment_data:
valid_locator = check_txid_format(appointment_data.get('tx_id')) valid_locator = check_txid_format(appointment_data.get('tx_id'))
@@ -67,22 +70,22 @@ def add_appointment(args):
appointment_data.get('start_time'), appointment_data.get('end_time'), appointment_data.get('start_time'), appointment_data.get('end_time'),
appointment_data.get('dispute_delta')) appointment_data.get('dispute_delta'))
logging.info("[Client] sending appointment to PISA") logger.info("Sending appointment to PISA")
try: try:
r = requests.post(url=add_appointment_endpoint, json=json.dumps(appointment), timeout=5) r = requests.post(url=add_appointment_endpoint, json=json.dumps(appointment), timeout=5)
logging.info("[Client] {} (code: {}).".format(r.text, r.status_code)) logger.info("{} (code: {}).".format(r.text, r.status_code))
except ConnectTimeout: except ConnectTimeout:
logging.error("[Client] can't connect to pisa API. Connection timeout.") logger.error("Can't connect to pisa API. Connection timeout.")
except ConnectionError: except ConnectionError:
logging.error("[Client] can't connect to pisa API. Server cannot be reached.") logger.error("Can't connect to pisa API. Server cannot be reached.")
else: else:
logging.error("[Client] the provided locator is not valid.") logger.error("The provided locator is not valid.")
else: else:
logging.error("[Client] no appointment data provided. " + use_help) logger.error("No appointment data provided. " + use_help)
def get_appointment(args): def get_appointment(args):
@@ -104,16 +107,16 @@ def get_appointment(args):
print(json.dumps(r.json(), indent=4, sort_keys=True)) print(json.dumps(r.json(), indent=4, sort_keys=True))
except ConnectTimeout: except ConnectTimeout:
logging.error("[Client] can't connect to pisa API. Connection timeout.") logger.error("Can't connect to pisa API. Connection timeout.")
except ConnectionError: except ConnectionError:
logging.error("[Client] can't connect to pisa API. Server cannot be reached.") logger.error("Can't connect to pisa API. Server cannot be reached.")
else: else:
logging.error("[Client] the provided locator is not valid.") logger.error("The provided locator is not valid.")
else: else:
logging.error("[Client] the provided locator is not valid.") logger.error("The provided locator is not valid.")
def build_appointment(tx, tx_id, start_block, end_block, dispute_delta): def build_appointment(tx, tx_id, start_block, end_block, dispute_delta):
@@ -199,7 +202,7 @@ if __name__ == '__main__':
sys.exit(help_get_appointment()) sys.exit(help_get_appointment())
else: else:
logging.error("[Client] unknown command. Use help to check the list of available commands") logger.error("Unknown command. Use help to check the list of available commands")
else: else:
sys.exit(show_usage()) sys.exit(show_usage())
@@ -210,14 +213,14 @@ if __name__ == '__main__':
generate_dummy_appointment() generate_dummy_appointment()
else: else:
logging.error("[Client] unknown command. Use help to check the list of available commands") logger.error("Unknown command. Use help to check the list of available commands")
else: else:
logging.error("[Client] no command provided. Use help to check the list of available commands.") logger.error("No command provided. Use help to check the list of available commands.")
except GetoptError as e: except GetoptError as e:
logging.error("[Client] {}".format(e)) logger.error("{}".format(e))
except json.JSONDecodeError as e: except json.JSONDecodeError as e:
logging.error("[Client] non-JSON encoded appointment passed as parameter.") logger.error("Non-JSON encoded appointment passed as parameter.")

View File

@@ -1,11 +1,10 @@
import json import json
from flask import Flask, request, Response, abort, jsonify from flask import Flask, request, Response, abort, jsonify
from pisa import HOST, PORT, logging, bitcoin_cli from pisa import HOST, PORT, logging
from pisa.logger import Logger from pisa.logger import Logger
from pisa.watcher import Watcher from pisa.watcher import Watcher
from pisa.inspector import Inspector from pisa.inspector import Inspector
from pisa import HOST, PORT, logging
from pisa.appointment import Appointment from pisa.appointment import Appointment
from pisa.block_processor import BlockProcessor from pisa.block_processor import BlockProcessor

View File

@@ -27,4 +27,7 @@ class Logger(object):
logging.debug(StructuredMessage(self._add_prefix(msg), actor=self.actor, **kwargs)) logging.debug(StructuredMessage(self._add_prefix(msg), actor=self.actor, **kwargs))
def error(self, msg, **kwargs): def error(self, msg, **kwargs):
logging.error(StructuredMessage(self._add_prefix(msg), actor=self.actor, **kwargs)) logging.error(StructuredMessage(self._add_prefix(msg), actor=self.actor, **kwargs))
def warn(self, msg, **kwargs):
logging.warn(StructuredMessage(self._add_prefix(msg), actor=self.actor, **kwargs))

View File

@@ -5,7 +5,7 @@ from pisa.logger import Logger
from pisa.api import start_api from pisa.api import start_api
from pisa.tools import can_connect_to_bitcoind, in_correct_network from pisa.tools import can_connect_to_bitcoind, in_correct_network
logger = Logger("Pisad") logger = Logger("Daemon")
if __name__ == '__main__': if __name__ == '__main__':
debug = False debug = False
@@ -23,4 +23,4 @@ if __name__ == '__main__':
logger.error("bitcoind is running on a different network, check conf.py and bitcoin.conf. Shutting down") logger.error("bitcoind is running on a different network, check conf.py and bitcoin.conf. Shutting down")
else: else:
logger.error("can't connect to bitcoind. Shutting down") logger.error("Can't connect to bitcoind. Shutting down")

View File

@@ -12,6 +12,7 @@ from pisa.utils.zmq_subscriber import ZMQHandler
logger = Logger("Watcher") logger = Logger("Watcher")
class Watcher: class Watcher:
def __init__(self, max_appointments=MAX_APPOINTMENTS): def __init__(self, max_appointments=MAX_APPOINTMENTS):
self.appointments = dict() self.appointments = dict()

View File

@@ -183,8 +183,3 @@ def test_get_all_appointments_responder():
assert (set(responder_jobs) == set(local_locators)) assert (set(responder_jobs) == set(local_locators))
assert (len(received_appointments["watcher_appointments"]) == 0) assert (len(received_appointments["watcher_appointments"]) == 0)

View File

@@ -87,5 +87,3 @@ def test_encrypt():
encrypted_blob2 = blob.encrypt(key) encrypted_blob2 = blob.encrypt(key)
assert(encrypted_blob == encrypted_blob2 and id(encrypted_blob) != id(encrypted_blob2)) assert(encrypted_blob == encrypted_blob2 and id(encrypted_blob) != id(encrypted_blob2))

View File

@@ -73,4 +73,3 @@ def test_potential_matches_random_data(locator_uuid_map):
# None of the txids should match # None of the txids should match
assert len(potential_matches) == 0 assert len(potential_matches) == 0

View File

@@ -78,4 +78,3 @@ def test_delete_completed_jobs():
Cleaner.delete_completed_jobs(jobs, tx_job_map, completed_jobs, 0) Cleaner.delete_completed_jobs(jobs, tx_job_map, completed_jobs, 0)
assert not set(completed_jobs).issubset(jobs.keys()) assert not set(completed_jobs).issubset(jobs.keys())

View File

@@ -34,6 +34,3 @@ def test_decrypt():
encrypted_blob = EncryptedBlob(encrypted_data) encrypted_blob = EncryptedBlob(encrypted_data)
assert(encrypted_blob.decrypt(key) == data) assert(encrypted_blob.decrypt(key) == data)

View File

@@ -230,4 +230,3 @@ def test_inspect():
and appointment.end_time == end_time and appointment.dispute_delta == dispute_delta and and appointment.end_time == end_time and appointment.dispute_delta == dispute_delta and
appointment.encrypted_blob.data == encrypted_blob and appointment.cipher == cipher and appointment.encrypted_blob.data == encrypted_blob and appointment.cipher == cipher and
appointment.hash_function == hash_function) appointment.hash_function == hash_function)

View File

@@ -8,9 +8,10 @@ def test_check_txid_format():
assert(check_txid_format(None) is False) assert(check_txid_format(None) is False)
assert(check_txid_format("") is False) assert(check_txid_format("") is False)
assert(check_txid_format(0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef) is False) # wrong type assert(check_txid_format(0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef) is False) # wrong type
assert(check_txid_format("0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef") is True) # lowercase assert(check_txid_format("abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcd") is True) # lowercase
assert(check_txid_format("0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF") is True) # uppercase assert(check_txid_format("ABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCD") is True) # uppercase
assert(check_txid_format("0123456789abcdef0123456789ABCDEF0123456789abcdef0123456789ABCDEF") is True) # mixed case assert(check_txid_format("0123456789abcdef0123456789ABCDEF0123456789abcdef0123456789ABCDEF") is True) # mixed case
assert(check_txid_format("0123456789012345678901234567890123456789012345678901234567890123") is True) # only nums
assert(check_txid_format("0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdf") is False) # too short assert(check_txid_format("0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdf") is False) # too short
assert(check_txid_format("0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0") is False) # too long assert(check_txid_format("0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0") is False) # too long
assert(check_txid_format("g123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef") is False) # non-hex assert(check_txid_format("g123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef") is False) # non-hex