diff --git a/pisa/pisad.py b/pisa/pisad.py index 44d9adc..3bbf476 100644 --- a/pisa/pisad.py +++ b/pisa/pisad.py @@ -4,6 +4,7 @@ from signal import signal, SIGINT, SIGQUIT, SIGTERM from pisa.logger import Logger from pisa.api import start_api +from pisa.conf import BTC_NETWORK from pisa.tools import can_connect_to_bitcoind, in_correct_network logger = Logger("Daemon") @@ -29,7 +30,7 @@ if __name__ == '__main__': pass if can_connect_to_bitcoind(): - if in_correct_network(): + if in_correct_network(BTC_NETWORK): # Fire the api start_api() diff --git a/pisa/tools.py b/pisa/tools.py index c10f2a5..aeaa310 100644 --- a/pisa/tools.py +++ b/pisa/tools.py @@ -1,7 +1,6 @@ import re from http.client import HTTPException -import pisa.conf as conf from pisa import bitcoin_cli from pisa.logger import Logger from pisa.utils.auth_proxy import JSONRPCException @@ -46,18 +45,18 @@ def can_connect_to_bitcoind(): return can_connect -def in_correct_network(): +def in_correct_network(network): mainnet_genesis_block_hash = "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f" testnet3_genesis_block_hash = "000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943" correct_network = False genesis_block_hash = bitcoin_cli.getblockhash(0) - if conf.BTC_NETWORK == 'mainnet' and genesis_block_hash == mainnet_genesis_block_hash: + if network == 'mainnet' and genesis_block_hash == mainnet_genesis_block_hash: correct_network = True - elif conf.BTC_NETWORK == 'testnet' and genesis_block_hash == testnet3_genesis_block_hash: + elif network == 'testnet' and genesis_block_hash == testnet3_genesis_block_hash: correct_network = True - elif conf.BTC_NETWORK == 'regtest' and genesis_block_hash not in [mainnet_genesis_block_hash, testnet3_genesis_block_hash]: + elif network == 'regtest' and genesis_block_hash not in [mainnet_genesis_block_hash, testnet3_genesis_block_hash]: correct_network = True return correct_network diff --git a/test/unit/test_tools.py b/test/unit/test_tools.py index 0e96b7c..fd95e4f 100644 --- a/test/unit/test_tools.py +++ b/test/unit/test_tools.py @@ -1,9 +1,44 @@ -from pisa.tools import check_txid_format +import pytest +from time import sleep +from multiprocessing import Process + + from pisa import logging +from pisa.tools import check_txid_format +from test.simulator.bitcoind_sim import run_simulator +from pisa.tools import can_connect_to_bitcoind, in_correct_network logging.getLogger().disabled = True +@pytest.fixture(autouse=True, scope='session') +def run_bitcoind(): + bitcoind_process = Process(target=run_simulator) + bitcoind_process.start() + + # It takes a little bit of time to start the API (otherwise the requests are sent too early and they fail) + sleep(0.1) + + return bitcoind_process + + +def test_in_correct_network(): + # The simulator runs as if it was regtest, so every other network should fail + assert in_correct_network('mainnet') is False + assert in_correct_network('testnet') is False + assert in_correct_network('regtest') is True + + +def test_can_connect_to_bitcoind(): + assert can_connect_to_bitcoind() is True + + +def test_can_connect_to_bitcoind_bitcoin_not_running(run_bitcoind): + # Kill the simulator thread and test the check fails + run_bitcoind.kill() + assert can_connect_to_bitcoind() is False + + def test_check_txid_format(): assert(check_txid_format(None) is False) assert(check_txid_format("") is False)