Updates teos to work with the new conf file format and redefines how objects are built

Configuration parameters are now load from a .conf file (see template.conf as an example).

The code has 3 config levels: default (teos/__init__.py), config file (<data_dir>/teos.conf) and command line.
Most of the config parameters are only modificable trough the config file for now.
The priority order is: command line, config file, default.

Objects that need config parameters are now built in teosd instead of inside other classes. Therefore teosd acts like a factory and config parameters don't have to be passed around between objects. This also implies that a lot of the object creation logic has been changed.

This should simplify unit testing.
This commit is contained in:
Sergi Delgado Segura
2020-03-20 21:16:18 +01:00
parent 6499820540
commit b43731397c
14 changed files with 223 additions and 207 deletions

View File

@@ -1,7 +1,6 @@
from http.client import HTTPException
from socket import timeout
from http.client import HTTPException
import teos.conf as conf
from teos.utils.auth_proxy import AuthServiceProxy, JSONRPCException
"""
@@ -10,25 +9,38 @@ Tools is a module with general methods that can used by different entities in th
# NOTCOVERED
def bitcoin_cli():
def bitcoin_cli(btc_connect_params):
"""
An ``http`` connection with ``bitcoind`` using the ``json-rpc`` interface.
Args:
btc_connect_params (:obj:`dict`): a dictionary with the parameters to connect to bitcoind
(rpc user, rpc passwd, host and port)
Returns:
:obj:`AuthServiceProxy <teos.utils.auth_proxy.AuthServiceProxy>`: An authenticated service proxy to ``bitcoind``
that can be used to send ``json-rpc`` commands.
"""
return AuthServiceProxy(
"http://%s:%s@%s:%d" % (conf.BTC_RPC_USER, conf.BTC_RPC_PASSWD, conf.BTC_RPC_HOST, conf.BTC_RPC_PORT)
"http://%s:%s@%s:%d"
% (
btc_connect_params.get("BTC_RPC_USER"),
btc_connect_params.get("BTC_RPC_PASSWD"),
btc_connect_params.get("BTC_RPC_CONNECT"),
btc_connect_params.get("BTC_RPC_PORT"),
)
)
# NOTCOVERED
def can_connect_to_bitcoind():
def can_connect_to_bitcoind(btc_connect_params):
"""
Checks if the tower has connection to ``bitcoind``.
Args:
btc_connect_params (:obj:`dict`): a dictionary with the parameters to connect to bitcoind
(rpc user, rpc passwd, host and port)
Returns:
:obj:`bool`: ``True`` if the connection can be established. ``False`` otherwise.
"""
@@ -36,18 +48,23 @@ def can_connect_to_bitcoind():
can_connect = True
try:
bitcoin_cli().help()
bitcoin_cli(btc_connect_params).help()
except (timeout, ConnectionRefusedError, JSONRPCException, HTTPException, OSError):
can_connect = False
return can_connect
def in_correct_network(network):
def in_correct_network(btc_connect_params, network):
"""
Checks if ``bitcoind`` and the tower are configured to run in the same network (``mainnet``, ``testnet`` or
``regtest``)
Args:
btc_connect_params (:obj:`dict`): a dictionary with the parameters to connect to bitcoind
(rpc user, rpc passwd, host and port)
network (:obj:`str`): the network the tower is connected to.
Returns:
:obj:`bool`: ``True`` if the network configuration matches. ``False`` otherwise.
"""
@@ -56,7 +73,7 @@ def in_correct_network(network):
testnet3_genesis_block_hash = "000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943"
correct_network = False
genesis_block_hash = bitcoin_cli().getblockhash(0)
genesis_block_hash = bitcoin_cli(btc_connect_params).getblockhash(0)
if network == "mainnet" and genesis_block_hash == mainnet_genesis_block_hash:
correct_network = True