Skip to content

Commit

Permalink
lndmanaged+lndmanage: add main logic
Browse files Browse the repository at this point in the history
  • Loading branch information
bitromortac committed Apr 25, 2022
1 parent 1ad718b commit a7466c5
Show file tree
Hide file tree
Showing 19 changed files with 103 additions and 73 deletions.
5 changes: 0 additions & 5 deletions lndmanage.py

This file was deleted.

1 change: 0 additions & 1 deletion lndmanage/lib/recommend_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lndmanage/lndmanage.py → lndmanage/main_lndmanage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
21 changes: 21 additions & 0 deletions lndmanage/main_lndmanaged.py
Original file line number Diff line number Diff line change
@@ -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()
52 changes: 44 additions & 8 deletions lndmanage/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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': {
Expand All @@ -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',
},
},
Expand Down
5 changes: 5 additions & 0 deletions lndmanage/templates/lndmanaged_config.ini
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions run_lndmanage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env python3

from lndmanage.main_lndmanage import main

main()
5 changes: 5 additions & 0 deletions run_lndmanaged.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env python3

from lndmanage.main_lndmanaged import main

main()
12 changes: 2 additions & 10 deletions test/test_chanacceptor.py
Original file line number Diff line number Diff line change
@@ -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.")
Expand Down
13 changes: 2 additions & 11 deletions test/test_circle.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""
Tests for circular self-payments.
"""
"""Tests for circular self-payments."""
import math
import time
from typing import List
Expand All @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions test/test_fee_setting.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
1 change: 1 addition & 0 deletions test/test_graph_definitions.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down
1 change: 1 addition & 0 deletions test/test_ln_utilities.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from unittest import TestCase

from lndmanage.lib.ln_utilities import (
local_balance_to_unbalancedness,
unbalancedness_to_local_balance,
Expand Down
4 changes: 1 addition & 3 deletions test/test_itest.py → test/test_lndmanage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""
Integration tests for lndmanage.
"""
""" Integration tests for lndmanage."""
from test.testing_common import test_graphs_paths, TestNetwork


Expand Down
14 changes: 1 addition & 13 deletions test/test_openchannels.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
4 changes: 1 addition & 3 deletions test/test_pathfinding.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
3 changes: 2 additions & 1 deletion test/test_psbt.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
13 changes: 1 addition & 12 deletions test/test_rebalance.py
Original file line number Diff line number Diff line change
@@ -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):
"""
Expand Down
6 changes: 5 additions & 1 deletion test/testing_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a7466c5

Please sign in to comment.