diff --git a/pisa/api.py b/pisa/api.py index 9468166..ec8dbda 100644 --- a/pisa/api.py +++ b/pisa/api.py @@ -17,8 +17,9 @@ logger = Logger("API") class API: - def __init__(self, watcher): + def __init__(self, watcher, config): self.watcher = watcher + self.config = config def add_appointment(self): """ @@ -41,7 +42,7 @@ class API: # Check content type once if properly defined request_data = json.loads(request.get_json()) - inspector = Inspector() + inspector = Inspector(self.config) appointment = inspector.inspect( request_data.get("appointment"), request_data.get("signature"), request_data.get("public_key") ) diff --git a/pisa/inspector.py b/pisa/inspector.py index 00a7fc2..fcc570e 100644 --- a/pisa/inspector.py +++ b/pisa/inspector.py @@ -5,7 +5,6 @@ from common.constants import LOCATOR_LEN_HEX from common.cryptographer import Cryptographer from pisa import errors -import pisa.conf as conf from common.logger import Logger from common.appointment import Appointment from pisa.block_processor import BlockProcessor @@ -23,6 +22,9 @@ class Inspector: The :class:`Inspector` class is in charge of verifying that the appointment data provided by the user is correct. """ + def __init__(self, config): + self.config = config + def inspect(self, appointment_data, signature, public_key): """ Inspects whether the data provided by the user is correct. @@ -221,8 +223,7 @@ class Inspector: return rcode, message - @staticmethod - def check_to_self_delay(to_self_delay): + def check_to_self_delay(self, to_self_delay): """ Checks if the provided ``to_self_delay`` is correct. @@ -255,10 +256,10 @@ class Inspector: rcode = errors.APPOINTMENT_WRONG_FIELD_TYPE message = "wrong to_self_delay data type ({})".format(t) - elif to_self_delay < conf.MIN_TO_SELF_DELAY: + elif to_self_delay < self.config.get("MIN_TO_SELF_DELAY"): rcode = errors.APPOINTMENT_FIELD_TOO_SMALL message = "to_self_delay too small. The to_self_delay should be at least {} (current: {})".format( - conf.MIN_TO_SELF_DELAY, to_self_delay + self.config.get("MIN_TO_SELF_DELAY"), to_self_delay ) if message is not None: diff --git a/pisa/pisad.py b/pisa/pisad.py index c5b8dff..be9d571 100644 --- a/pisa/pisad.py +++ b/pisa/pisad.py @@ -7,16 +7,7 @@ from common.logger import Logger from pisa.api import API from pisa.watcher import Watcher from pisa.builder import Builder -from pisa.conf import ( - BTC_NETWORK, - FEED_PROTOCOL, - FEED_ADDR, - FEED_PORT, - MAX_APPOINTMENTS, - EXPIRY_DELTA, - MIN_TO_SELF_DELAY, - PISA_SECRET_KEY, -) +import pisa.conf as conf from pisa.responder import Responder from pisa.db_manager import DBManager from pisa.block_processor import BlockProcessor @@ -108,7 +99,7 @@ if __name__ == "__main__": watcher.block_queue = Builder.build_block_queue(missed_blocks_watcher) # Fire the API - API(watcher).start() + API(watcher, config=pisa_config).start() except Exception as e: logger.error("An error occurred: {}. Shutting down".format(e)) diff --git a/test/pisa/unit/test_api.py b/test/pisa/unit/test_api.py index 75c5f8f..820b11a 100644 --- a/test/pisa/unit/test_api.py +++ b/test/pisa/unit/test_api.py @@ -9,7 +9,6 @@ from pisa.api import API from pisa.watcher import Watcher from pisa.tools import bitcoin_cli from pisa import HOST, PORT -from pisa.conf import MAX_APPOINTMENTS, EXPIRY_DELTA from test.pisa.unit.conftest import ( generate_block, @@ -29,6 +28,8 @@ MULTIPLE_APPOINTMENTS = 10 appointments = [] locator_dispute_tx_map = {} +config = get_config() + @pytest.fixture(scope="module") def run_api(db_manager): @@ -40,7 +41,7 @@ def run_api(db_manager): ) watcher = Watcher(db_manager, sk_der, get_config()) - api_thread = Thread(target=API(watcher).start) + api_thread = Thread(target=API(watcher, config).start) api_thread.daemon = True api_thread.start() @@ -103,7 +104,7 @@ def test_request_multiple_appointments_same_locator(new_appt_data, n=MULTIPLE_AP def test_add_too_many_appointment(new_appt_data): - for _ in range(MAX_APPOINTMENTS - len(appointments)): + for _ in range(config.get("MAX_APPOINTMENTS") - len(appointments)): r = add_appointment(new_appt_data) assert r.status_code == 200 diff --git a/test/pisa/unit/test_inspector.py b/test/pisa/unit/test_inspector.py index 7b18460..4dbafce 100644 --- a/test/pisa/unit/test_inspector.py +++ b/test/pisa/unit/test_inspector.py @@ -10,13 +10,13 @@ from common.appointment import Appointment from pisa.block_processor import BlockProcessor from pisa.conf import MIN_TO_SELF_DELAY -from test.pisa.unit.conftest import get_random_value_hex, generate_dummy_appointment_data, generate_keypair +from test.pisa.unit.conftest import get_random_value_hex, generate_dummy_appointment_data, generate_keypair, get_config from common.constants import LOCATOR_LEN_BYTES, LOCATOR_LEN_HEX from common.cryptographer import Cryptographer -inspector = Inspector() +inspector = Inspector(get_config()) APPOINTMENT_OK = (0, None) NO_HEX_STRINGS = [ @@ -126,21 +126,21 @@ def test_check_to_self_delay(): # Right value, right format to_self_delays = [MIN_TO_SELF_DELAY, MIN_TO_SELF_DELAY + 1, MIN_TO_SELF_DELAY + 1000] for to_self_delay in to_self_delays: - assert Inspector.check_to_self_delay(to_self_delay) == APPOINTMENT_OK + assert inspector.check_to_self_delay(to_self_delay) == APPOINTMENT_OK # to_self_delay too small to_self_delays = [MIN_TO_SELF_DELAY - 1, MIN_TO_SELF_DELAY - 2, 0, -1, -1000] for to_self_delay in to_self_delays: - assert Inspector.check_to_self_delay(to_self_delay)[0] == APPOINTMENT_FIELD_TOO_SMALL + assert inspector.check_to_self_delay(to_self_delay)[0] == APPOINTMENT_FIELD_TOO_SMALL # Empty field to_self_delay = None - assert Inspector.check_to_self_delay(to_self_delay)[0] == APPOINTMENT_EMPTY_FIELD + assert inspector.check_to_self_delay(to_self_delay)[0] == APPOINTMENT_EMPTY_FIELD # Wrong data type to_self_delays = WRONG_TYPES for to_self_delay in to_self_delays: - assert Inspector.check_to_self_delay(to_self_delay)[0] == APPOINTMENT_WRONG_FIELD_TYPE + assert inspector.check_to_self_delay(to_self_delay)[0] == APPOINTMENT_WRONG_FIELD_TYPE def test_check_blob():