From a7466c53a7f5e7c464c89e05156fd93499e1585a Mon Sep 17 00:00:00 2001 From: bitromortac Date: Sat, 23 Apr 2022 11:52:31 +0200 Subject: [PATCH] lndmanaged+lndmanage: add main logic --- lndmanage.py | 5 -- lndmanage/lib/recommend_nodes.py | 1 - lndmanage/{lndmanage.py => main_lndmanage.py} | 3 +- lndmanage/main_lndmanaged.py | 21 ++++++++ lndmanage/settings.py | 52 ++++++++++++++++--- lndmanage/templates/lndmanaged_config.ini | 5 ++ run_lndmanage.py | 5 ++ run_lndmanaged.py | 5 ++ test/test_chanacceptor.py | 12 +---- test/test_circle.py | 13 +---- test/test_fee_setting.py | 8 +-- test/test_graph_definitions.py | 1 + test/test_ln_utilities.py | 1 + test/{test_itest.py => test_lndmanage.py} | 4 +- test/test_openchannels.py | 14 +---- test/test_pathfinding.py | 4 +- test/test_psbt.py | 3 +- test/test_rebalance.py | 13 +---- test/testing_common.py | 6 ++- 19 files changed, 103 insertions(+), 73 deletions(-) delete mode 100755 lndmanage.py rename lndmanage/{lndmanage.py => main_lndmanage.py} (99%) create mode 100644 lndmanage/main_lndmanaged.py create mode 100644 lndmanage/templates/lndmanaged_config.ini create mode 100755 run_lndmanage.py create mode 100755 run_lndmanaged.py rename test/{test_itest.py => test_lndmanage.py} (92%) diff --git a/lndmanage.py b/lndmanage.py deleted file mode 100755 index 46de72e..0000000 --- a/lndmanage.py +++ /dev/null @@ -1,5 +0,0 @@ -#!/usr/bin/env python3 - -from lndmanage.lndmanage import main - -main() diff --git a/lndmanage/lib/recommend_nodes.py b/lndmanage/lib/recommend_nodes.py index ec12a02..0581e40 100644 --- a/lndmanage/lib/recommend_nodes.py +++ b/lndmanage/lib/recommend_nodes.py @@ -10,7 +10,6 @@ from lndmanage import settings import logging.config -logging.config.dictConfig(settings.logger_config) logger = logging.getLogger(__name__) # define printing shortcuts, alignments, and cutoffs diff --git a/lndmanage/lndmanage.py b/lndmanage/main_lndmanage.py similarity index 99% rename from lndmanage/lndmanage.py rename to lndmanage/main_lndmanage.py index d57a98a..8537279 100755 --- a/lndmanage/lndmanage.py +++ b/lndmanage/main_lndmanage.py @@ -23,10 +23,11 @@ from lndmanage.lib.rebalance import Rebalancer, DEFAULT_MAX_FEE_RATE, DEFAULT_AMOUNT_SAT from lndmanage.lib.recommend_nodes import RecommendNodes from lndmanage.lib.report import Report + from lndmanage import settings import logging.config -logging.config.dictConfig(settings.logger_config) +logging.config.dictConfig(settings.lndmanage_log_config) logger = logging.getLogger() diff --git a/lndmanage/main_lndmanaged.py b/lndmanage/main_lndmanaged.py new file mode 100644 index 0000000..a2ae69c --- /dev/null +++ b/lndmanage/main_lndmanaged.py @@ -0,0 +1,21 @@ +import os +from lndmanage import settings + +from lndmanage.lib.managed import LNDManageDaemon + +def main(): + lndm_config_path = os.path.join(settings.home_dir, 'config.ini') + lndmd_config_path = os.path.join(settings.home_dir, 'lndmanaged.ini') + + lndmd = LNDManageDaemon( + lndm_config_path=lndm_config_path, + lndmd_config_path=lndmd_config_path, + ) + lndmd.run_services() + + +if __name__ == '__main__': + lndmd = LNDManageDaemon( + lndm_config_path="/home/user/.lndmanage/config.ini", + lndmd_config_path="/home/user/.lndmanage/lndmanaged.ini") + lndmd.run_services() diff --git a/lndmanage/settings.py b/lndmanage/settings.py index 5baf412..839ba99 100644 --- a/lndmanage/settings.py +++ b/lndmanage/settings.py @@ -44,17 +44,20 @@ def parse_env(key, default, _type: Type = str): CHACC_MAX_CHANNEL_SIZE_PRIVATE = parse_env('LNDMANAGED_CHACC_MAX_CHANNEL_SIZE_PRIVATE', '16777215', int) CHACC_MIN_CHANNEL_SIZE_PUBLIC = parse_env('LNDMANAGED_CHACC_MIN_CHANNEL_SIZE_PUBLIC', '0', int) CHACC_MAX_CHANNEL_SIZE_PUBLIC = parse_env('LNDMANAGED_CHACC_MAX_CHANNEL_SIZE_PUBLIC', '16777215', int) + +# define some globals for logging +lndmanage_log_config = None +lndmanaged_log_config = None home_dir = None def set_lndmanage_home_dir(directory=None): - """ - Sets the correct path to the lndmanage home folder. + """Sets the correct path to the lndmanage home folder. :param directory: home folder, overwrites default :type directory: str """ - global home_dir, logger_config + global home_dir, lndmanage_log_config, lndmanaged_log_config if directory: home_dir = directory @@ -78,15 +81,48 @@ def set_lndmanage_home_dir(directory=None): check_or_create_configuration(home_dir) # logger settings - logfile_path = os.path.join(home_dir, 'lndmanage.log') + lndmanage_log_config = { + 'version': 1, + 'disable_existing_loggers': False, + 'formatters': { + 'file': { + 'format': '[%(asctime)s %(levelname)s %(name)s] %(message)s', + 'datefmt': '%Y-%m-%d %H:%M:%S' + }, + 'standard': { + 'format': '%(message)s', + }, + }, + 'handlers': { + 'default': { + 'level': 'INFO', + 'formatter': 'standard', + 'class': 'logging.StreamHandler', + 'stream': 'ext://sys.stdout', # Default is stderr + }, + 'file': { + 'level': 'DEBUG', + 'formatter': 'file', + 'class': 'logging.FileHandler', + 'filename': os.path.join(home_dir, 'lndmanage.log'), + 'encoding': 'utf-8', + }, + }, + 'loggers': { + '': { # root logger + 'handlers': ['default', 'file'], + 'level': 'DEBUG', + 'propagate': True + }, + } + } - logger_config = { + lndmanaged_log_config = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'file': { - 'format': '[%(asctime)s %(levelname)s] %(message)s', - #'format': '[%(asctime)s %(levelname)s %(name)s] %(message)s', + 'format': '[%(asctime)s %(levelname)s %(name)s] %(message)s', 'datefmt': '%Y-%m-%d %H:%M:%S' }, 'standard': { @@ -104,7 +140,7 @@ def set_lndmanage_home_dir(directory=None): 'level': 'DEBUG', 'formatter': 'file', 'class': 'logging.FileHandler', - 'filename': logfile_path, + 'filename': os.path.join(home_dir, 'lndmanaged.log'), 'encoding': 'utf-8', }, }, diff --git a/lndmanage/templates/lndmanaged_config.ini b/lndmanage/templates/lndmanaged_config.ini new file mode 100644 index 0000000..6281d91 --- /dev/null +++ b/lndmanage/templates/lndmanaged_config.ini @@ -0,0 +1,5 @@ +[channel_acceptor] +min_channel_size_private = 0 +max_channel_size_private = 16777215 +min_channel_size_public = 0 +max_channel_size_public = 16777215 diff --git a/run_lndmanage.py b/run_lndmanage.py new file mode 100755 index 0000000..6ee3414 --- /dev/null +++ b/run_lndmanage.py @@ -0,0 +1,5 @@ +#!/usr/bin/env python3 + +from lndmanage.main_lndmanage import main + +main() diff --git a/run_lndmanaged.py b/run_lndmanaged.py new file mode 100755 index 0000000..0e6b184 --- /dev/null +++ b/run_lndmanaged.py @@ -0,0 +1,5 @@ +#!/usr/bin/env python3 + +from lndmanage.main_lndmanaged import main + +main() \ No newline at end of file diff --git a/test/test_chanacceptor.py b/test/test_chanacceptor.py index 481d1c1..3fc62fe 100644 --- a/test/test_chanacceptor.py +++ b/test/test_chanacceptor.py @@ -1,28 +1,20 @@ -"""Tests lndmanaged, lnd management daemon.""" +"""Tests for channel acceptor.""" import asyncio from typing import TYPE_CHECKING from configparser import ConfigParser from unittest.mock import patch from lndmanage.lib.chan_acceptor import ChanAcceptor -from lndmanage import settings from test.testing_common import ( test_graphs_paths, - lndmanage_home, TestNetwork, + logger, ) if TYPE_CHECKING: from lnregtest.lib.network_components import LND -import logging.config -settings.set_lndmanage_home_dir(lndmanage_home) -logging.config.dictConfig(settings.lndmanage_log_config) -logger = logging.getLogger() -logger.setLevel(logging.DEBUG) -logger.handlers[0].setLevel(logging.DEBUG) - def cancel_all_tasks_callback(future: asyncio.Future): logger.debug(f"TEST: Task {future} is done, cleaning up other tasks.") diff --git a/test/test_circle.py b/test/test_circle.py index 344e97e..49d6d61 100644 --- a/test/test_circle.py +++ b/test/test_circle.py @@ -1,6 +1,4 @@ -""" -Tests for circular self-payments. -""" +"""Tests for circular self-payments.""" import math import time from typing import List @@ -15,21 +13,14 @@ NoRoute, OurNodeFailure, ) -from lndmanage import settings from test.testing_common import ( test_graphs_paths, - lndmanage_home, SLEEP_SEC_AFTER_REBALANCING, TestNetwork, ) -import logging.config -settings.set_lndmanage_home_dir(lndmanage_home) -logging.config.dictConfig(settings.logger_config) -logger = logging.getLogger() -logger.setLevel(logging.DEBUG) -logger.handlers[0].setLevel(logging.DEBUG) +from lndmanage import settings # needed for side effect configuration class CircleTest(TestNetwork): diff --git a/test/test_fee_setting.py b/test/test_fee_setting.py index a184f6f..e183363 100644 --- a/test/test_fee_setting.py +++ b/test/test_fee_setting.py @@ -1,12 +1,12 @@ from unittest import TestCase -import logging import sys +import logging from lndmanage.lib.fee_setting import delta_demand, delta_min, optimization_parameters -logger = logging.getLogger() -logger.level = logging.DEBUG -logger.addHandler(logging.StreamHandler(sys.stdout)) +import testing_common + +testing_common.logger.addHandler(logging.StreamHandler(sys.stdout)) class TestFeeSetter(TestCase): diff --git a/test/test_graph_definitions.py b/test/test_graph_definitions.py index 622fe6f..bfa0bb4 100644 --- a/test/test_graph_definitions.py +++ b/test/test_graph_definitions.py @@ -1,6 +1,7 @@ from unittest import TestCase from lnregtest.lib.graph_testing import graph_test + from test.graph_definitions.star_ring_4_illiquid import nodes diff --git a/test/test_ln_utilities.py b/test/test_ln_utilities.py index 13413fc..b1aaa61 100644 --- a/test/test_ln_utilities.py +++ b/test/test_ln_utilities.py @@ -1,4 +1,5 @@ from unittest import TestCase + from lndmanage.lib.ln_utilities import ( local_balance_to_unbalancedness, unbalancedness_to_local_balance, diff --git a/test/test_itest.py b/test/test_lndmanage.py similarity index 92% rename from test/test_itest.py rename to test/test_lndmanage.py index 8f57842..c547750 100644 --- a/test/test_itest.py +++ b/test/test_lndmanage.py @@ -1,6 +1,4 @@ -""" -Integration tests for lndmanage. -""" +""" Integration tests for lndmanage.""" from test.testing_common import test_graphs_paths, TestNetwork diff --git a/test/test_openchannels.py b/test/test_openchannels.py index f5c7c8f..3ce938b 100644 --- a/test/test_openchannels.py +++ b/test/test_openchannels.py @@ -1,26 +1,14 @@ -""" -Integration tests for batch opening of channels. -""" +"""Integration tests for batch opening of channels.""" import time from unittest import TestCase -from lndmanage import settings from lndmanage.lib import openchannels from test.testing_common import ( - lndmanage_home, test_graphs_paths, TestNetwork, ) -import logging.config - -settings.set_lndmanage_home_dir(lndmanage_home) -logging.config.dictConfig(settings.logger_config) -logger = logging.getLogger() -logger.setLevel(logging.DEBUG) -logger.handlers[0].setLevel(logging.DEBUG) - def confirm_transactions(testnet): for _ in range(6): diff --git a/test/test_pathfinding.py b/test/test_pathfinding.py index 338472c..366210f 100644 --- a/test/test_pathfinding.py +++ b/test/test_pathfinding.py @@ -1,6 +1,4 @@ -""" -Integration tests for batch opening of channels. -""" +"""Integration tests for batch opening of channels.""" from typing import Dict from unittest import TestCase, mock diff --git a/test/test_psbt.py b/test/test_psbt.py index 6fe1777..82f047e 100644 --- a/test/test_psbt.py +++ b/test/test_psbt.py @@ -1,6 +1,7 @@ +from binascii import a2b_base64 from unittest import TestCase + from lndmanage.lib import psbt -from binascii import unhexlify, a2b_base64 class PSBTTest(TestCase): diff --git a/test/test_rebalance.py b/test/test_rebalance.py index 91e7199..bbf5770 100644 --- a/test/test_rebalance.py +++ b/test/test_rebalance.py @@ -1,28 +1,17 @@ -""" -Integration tests for rebalancing of channels. -""" +"""Integration tests for rebalancing of channels.""" import time from typing import Optional -from lndmanage import settings from lndmanage.lib.rebalance import Rebalancer from lndmanage.lib.ln_utilities import local_balance_to_unbalancedness from lndmanage.lib.exceptions import NoRebalanceCandidates from test.testing_common import ( - lndmanage_home, test_graphs_paths, SLEEP_SEC_AFTER_REBALANCING, TestNetwork ) -import logging.config -settings.set_lndmanage_home_dir(lndmanage_home) -logging.config.dictConfig(settings.logger_config) -logger = logging.getLogger() -logger.setLevel(logging.DEBUG) -logger.handlers[0].setLevel(logging.DEBUG) - class RebalanceTest(TestNetwork): """ diff --git a/test/testing_common.py b/test/testing_common.py index bd6b1b9..014ff64 100644 --- a/test/testing_common.py +++ b/test/testing_common.py @@ -5,8 +5,12 @@ from lnregtest.lib.network import Network from lib.node import LndNode -from lndmanage import settings +import logging.config +logger = logging.getLogger() +logger.setLevel(logging.DEBUG) + +from lndmanage import settings settings.CACHING_RETENTION_MINUTES = 0 # constants for testing