From 0c1ce056ebe026ad134563287e587fbd706bdff1 Mon Sep 17 00:00:00 2001 From: Sergi Delgado Segura Date: Fri, 5 Jul 2019 15:18:42 +0100 Subject: [PATCH] Adds sanity checks to make sure pisad and bitcoind are configured in the same way --- pisa-btc/pisa/pisad.py | 18 ++++++++++++++---- pisa-btc/pisa/tools.py | 29 +++++++++++++++++++++++++++++ pisa-btc/sample_conf.py | 2 +- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/pisa-btc/pisa/pisad.py b/pisa-btc/pisa/pisad.py index 137e12a..2224eed 100644 --- a/pisa-btc/pisa/pisad.py +++ b/pisa-btc/pisa/pisad.py @@ -4,6 +4,9 @@ from getopt import getopt from conf import SERVER_LOG_FILE from threading import Thread from pisa.api import start_api +from pisa.tools import can_connect_to_bitcoind, in_correct_network +from utils.authproxy import AuthServiceProxy +from conf import BTC_RPC_USER, BTC_RPC_PASSWD, BTC_RPC_HOST, BTC_RPC_PORT, BTC_NETWORK if __name__ == '__main__': @@ -19,8 +22,15 @@ if __name__ == '__main__': logging.StreamHandler() ]) - api_thread = Thread(target=start_api, args=[debug, logging]) - api_thread.start() + bitcoin_cli = AuthServiceProxy("http://%s:%s@%s:%d" % (BTC_RPC_USER, BTC_RPC_PASSWD, BTC_RPC_HOST, + BTC_RPC_PORT)) - # ToDO: Run sanity checks to ensure that bitcoin rpc requests can be run, if the bitcoin.conf is not consistent with - # conf.py the pisa daemon will fail upon performing the first rpc request. + if can_connect_to_bitcoind(bitcoin_cli): + if in_correct_network(bitcoin_cli, BTC_NETWORK): + api_thread = Thread(target=start_api, args=[debug, logging]) + api_thread.start() + else: + logging.error("[Pisad] bitcoind is running on a different network, check conf.py and bitcoin.conf. " + "Shutting down") + else: + logging.error("[Pisad] can't connect to bitcoind. Shutting down") diff --git a/pisa-btc/pisa/tools.py b/pisa-btc/pisa/tools.py index 7969361..a1f5411 100644 --- a/pisa-btc/pisa/tools.py +++ b/pisa-btc/pisa/tools.py @@ -25,3 +25,32 @@ def check_tx_in_chain(bitcoin_cli, tx_id, debug, logging, parent='', tx_label='t logging.error("[{}] JSONRPCException. Error code {}".format(parent, e)) return tx_in_chain, confirmations + + +def can_connect_to_bitcoind(bitcoin_cli): + can_connect = True + + try: + bitcoin_cli.help() + except ConnectionRefusedError: + can_connect = False + + return can_connect + + +def in_correct_network(bitcoin_cli, network): + mainnet_genesis_block_hash = "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f" + testnet3_genesis_block_hash = "000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943" + correct_network = False + + genesis_block_hash = bitcoin_cli.getblockhash(0) + + if network == 'mainnet' and genesis_block_hash == mainnet_genesis_block_hash: + correct_network = True + elif network == 'testnet' and genesis_block_hash == testnet3_genesis_block_hash: + correct_network = True + 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/pisa-btc/sample_conf.py b/pisa-btc/sample_conf.py index 88ad86a..eb107a2 100644 --- a/pisa-btc/sample_conf.py +++ b/pisa-btc/sample_conf.py @@ -3,6 +3,7 @@ BTC_RPC_USER = None BTC_RPC_PASSWD = None BTC_RPC_HOST = None BTC_RPC_PORT = None +BTC_NETWORK = None # ZMQ @@ -22,4 +23,3 @@ CLIENT_LOG_FILE = 'pisa.log' # CRYPTO SUPPORTED_HASH_FUNCTIONS = ["SHA256"] SUPPORTED_CIPHERS = ["AES-GCM-128"] -SALT = "lightningwatcher"