Skip to content

Commit 4b2e6b3

Browse files
committed
Various updates
1 parent f34f8ee commit 4b2e6b3

13 files changed

+247
-106
lines changed

auto_policy.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1414
#
1515

16-
from PyQt5 import QtCore
16+
from datetime import datetime
17+
from scheduler.update_scheduler import UpdateScheduler
1718
from config.config import SystemConfiguration
1819
from lightning import lightning_channel
1920

@@ -25,15 +26,17 @@ def __init__(self):
2526
self.policy2 = sc.policy2
2627
self.policy3 = sc.policy3
2728

28-
self.update()
29-
30-
self.timer = QtCore.QTimer()
31-
self.timer.timeout.connect(self.update)
32-
self.timer.start(1000 * 60 * 60) # update once every hour
29+
# register the auto policy update function, run it evey 2 hours if auto_policy flag is True in config
30+
st = sc.auto_policy
31+
UpdateScheduler.register('auto_policy',
32+
self.update,
33+
interval=2 * 60 * 60 * 1000,
34+
start=sc.auto_policy,
35+
immediate=sc.auto_policy)
3336

3437
def update(self):
38+
print('auto_policy entry: ' + str(datetime.now()))
3539
sc = SystemConfiguration()
36-
sc.channel_info_update_needed = True
3740
for c in lightning_channel.Channels.channel_index:
3841
channel = lightning_channel.Channels.channel_index[c][0]
3942
if channel.channel_type == "open_channel":
@@ -59,3 +62,5 @@ def update(self):
5962
base_fee_msat=int(self.policy3['base_fee']),
6063
fee_rate=float(self.policy3['fee_rate']),
6164
time_lock_delta=int(sc.default_time_lock_delta))
65+
UpdateScheduler.trigger('channel_info_widget')
66+
print('auto_policy exit: ' + str(datetime.now()))

balance_info_widget.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#
1515

1616
from lightning import wallet_balance, channel_balance, pending_channels, lightning_channel
17+
from scheduler.update_scheduler import UpdateScheduler
1718
from PyQt5 import QtCore, QtWidgets, QtGui
1819

1920

@@ -162,10 +163,8 @@ def __init__(self):
162163
self.ratio_label.setText(str(ratio))
163164
self.formLayout_5.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.ratio_label)
164165

165-
self.timer = QtCore.QTimer()
166-
self.timer.timeout.connect(self.update)
167-
self.timer.start(30000)
168-
166+
# register the balance info widget update function, don't start a timer, but run it once
167+
UpdateScheduler.register('balance_info_widget', self.update, start=False, immediate=True)
169168
self.show()
170169

171170
def update(self):

channel_graph_widget.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,26 @@
1414
#
1515

1616
from PyQt5 import QtCore, QtWidgets, QtGui
17-
from config.config import SystemConfiguration
1817
from instruction import CenterNode, CenterNodeInstruction, ChannelGraphPicture, Node, NodeInstruction
1918
from lightning import lightning_node, lightning_channel
19+
from scheduler.update_scheduler import UpdateScheduler
2020

2121

2222
class ChannelGraphWidget(QtWidgets.QWidget):
2323
channels = lightning_channel.Channels()
2424

25-
def __init__(self, channel_info_widget):
25+
def __init__(self):
2626
super().__init__()
2727
self._stop_repaint = False
2828
self.pen_width = 2
29-
self.channel_info_widget = channel_info_widget
30-
self.update()
31-
self.timer = QtCore.QTimer()
32-
self.timer.timeout.connect(self.update)
33-
self.timer.start(1000 * 60 * 60) # update once every hour
29+
30+
# register the channel graph widget update function, but don't start an automatic update, but run it once
31+
UpdateScheduler.register('channel_graph_widget', self.update, start=False, immediate=True)
3432

3533
self.show()
3634

3735
def update(self):
3836
ChannelGraphPicture.reset()
39-
ChannelGraphWidget.channels.read_channels()
4037
self._stop_repaint = True
4138
# draw center node (HomeNode - your node)
4239
home_node = None
@@ -64,7 +61,7 @@ def update(self):
6461
instruction = NodeInstruction(node, pen, channel.chan_id)
6562
ChannelGraphPicture.instructions.append(instruction)
6663
if ChannelGraphPicture.instructions[1]:
67-
self.channel_info_widget.update(ChannelGraphPicture.instructions[1].chan_id)
64+
UpdateScheduler.trigger('channel_info_widget', ChannelGraphPicture.instructions[1].chan_id)
6865
self._stop_repaint = False
6966

7067
def paintEvent(self, event):
@@ -73,15 +70,11 @@ def paintEvent(self, event):
7370
instruction.paint(self, qp)
7471

7572
def mousePressEvent(self, event):
76-
sc = SystemConfiguration()
77-
temp_update_needed = sc.channel_info_update_needed
78-
sc.channel_info_update_needed = False
7973
for i in ChannelGraphPicture.instructions:
8074
if isinstance(i, NodeInstruction):
8175
try:
8276
if i.get_window_position().contains(event.pos()):
83-
self.channel_info_widget.update(i.chan_id)
77+
UpdateScheduler.trigger('channel_info_widget', i.chan_id)
8478
break
8579
except AttributeError:
86-
sc.channel_info_update_needed = temp_update_needed
87-
sc.channel_info_update_needed = temp_update_needed
80+
pass

channel_info_widget.py

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
#
1515

1616
from datetime import date
17+
import urllib.error
1718
from lightning import lightning_channel
1819
from PyQt5 import QtCore, QtWidgets, QtGui
1920
from stylesheets.dark_theme import DarkTheme
2021
from lightning.fwding_event import FwdingEvents
22+
from scheduler.update_scheduler import UpdateScheduler
2123
from utils.block_explorer import open_block_explorer
2224
from config.config import SystemConfiguration
2325

@@ -156,7 +158,7 @@ def reject(self):
156158
self.parent.fee_rate = -1
157159
self.hide()
158160

159-
def __init__(self, channel_id=0):
161+
def __init__(self):
160162
super().__init__()
161163

162164
sc = SystemConfiguration()
@@ -170,7 +172,7 @@ def __init__(self, channel_id=0):
170172
self.fee_rate = sc.default_fee_rate
171173
self.time_lock_delta = sc.default_time_lock_delta
172174

173-
self.channel_id = channel_id
175+
# self.channel_id = channel_id
174176
self.channel_name_label = QtWidgets.QLabel(self)
175177
self.channel_name_label.setGeometry(QtCore.QRect(200, 0, 700, 60))
176178
font = QtGui.QFont()
@@ -398,6 +400,14 @@ def __init__(self, channel_id=0):
398400
self.date_label.setTextInteractionFlags(QtCore.Qt.TextSelectableByMouse)
399401
self.formLayout_3.setWidget(3, QtWidgets.QFormLayout.FieldRole, self.date_label)
400402

403+
self.label_304 = QtWidgets.QLabel(self.formLayoutWidget_3)
404+
self.label_304.setObjectName("label_304")
405+
self.formLayout_3.setWidget(4, QtWidgets.QFormLayout.LabelRole, self.label_304)
406+
self.date_created_label = QtWidgets.QLabel(self.formLayoutWidget_3)
407+
self.date_created_label.setObjectName("date_created_label")
408+
self.date_created_label.setTextInteractionFlags(QtCore.Qt.TextSelectableByMouse)
409+
self.formLayout_3.setWidget(4, QtWidgets.QFormLayout.FieldRole, self.date_created_label)
410+
401411
self.formLayoutWidget_4 = QtWidgets.QWidget(self)
402412
self.formLayoutWidget_4.setGeometry(QtCore.QRect(0, 280, 900, 150))
403413
self.formLayoutWidget_4.setObjectName("formLayoutWidget_3")
@@ -498,26 +508,26 @@ def __init__(self, channel_id=0):
498508
self.label_301.setText('Total forwarded amount out:')
499509
self.label_302.setText('Total fees received:')
500510
self.label_303.setText('Date last forward (UTC):')
511+
self.label_304.setText('Date channel creation:')
501512

502-
if self.channel_id != 0:
503-
self.update(channel_id)
504-
# don't show the object yet, because it is forward created
505-
# in order for the channel list to access this object
506-
# to update info
513+
for c in lightning_channel.Channels.channel_index:
514+
self.update(channel_id=c)
515+
break
507516

508-
self.timer = QtCore.QTimer()
509-
self.timer.timeout.connect(lambda: self.update(self.channel_id))
510-
self.timer.start(1000 * 60 * 5) # update once 5 minutes
517+
self.show()
511518

512-
def update(self, channel_id):
513-
sc = SystemConfiguration()
514-
self.channel_id = channel_id
515-
if sc.channel_info_update_needed:
516-
lightning_channel.Channels.read_channels()
517-
sc.channel_info_update_needed = False
519+
# register the channel info widget update function, but don't start an automatic update
520+
UpdateScheduler.register('channel_info_widget', self.update, start=False, immediate=False)
521+
522+
def update(self, channel_id=None):
523+
if channel_id is not None:
524+
self.channel_id = channel_id
525+
526+
if self.channel_id is None:
527+
return
518528

519529
self.forwarding_events = FwdingEvents()
520-
channel = lightning_channel.Channels.channel_index[channel_id][0]
530+
channel = lightning_channel.Channels.channel_index[self.channel_id][0]
521531
if channel.channel_state == lightning_channel.Channel.ChannelState.ACTIVE:
522532
self.reconnect_push_button.hide()
523533
else:
@@ -564,14 +574,20 @@ def update(self, channel_id):
564574
self.date_label.setText(str(date.fromtimestamp(last_forward)))
565575
else:
566576
self.date_label.setText('---')
577+
try:
578+
self.date_created_label.setText(str(date.fromtimestamp(channel.creation_date)))
579+
except urllib.error.URLError:
580+
self.date_created_label.setText('Unable to determine')
581+
567582

568583
def reconnect_channel(self, event):
569584
try:
570585
channel = lightning_channel.Channels.channel_index[self.channel_id][0]
571586
channel.reconnect()
572-
sc = SystemConfiguration()
573-
sc.channel_info_update_needed = True
574-
self.update(self.channel_id)
587+
588+
lightning_channel.Channels.read_channels()
589+
UpdateScheduler.trigger('channel_info_widget', self.channel_id)
590+
575591
if self.active_label.text() == 'INACTIVE':
576592
mb = QtWidgets.QMessageBox()
577593
mb.about(self, "Reconnect error", "Unable to reconnect with node. Try again later")
@@ -604,6 +620,6 @@ def set_channel_policy(self):
604620
base_fee_msat=self.base_fee_msat,
605621
fee_rate=self.fee_rate,
606622
time_lock_delta=self.time_lock_delta)
607-
sc = SystemConfiguration()
608-
sc.channel_info_update_needed = True
609-
self.update(self.channel_id)
623+
624+
lightning_channel.Channels.read_channels()
625+
UpdateScheduler.trigger('channel_info_widget', self.channel_id)

conductor.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from node_info_widget import NodeInfoWidget
2424
from stylesheets.dark_theme import DarkTheme
2525
from config.config import SystemConfiguration
26-
from lightning import test_lnd_connection
26+
from lightning import test_lnd_connection, lightning_channel
2727
from auto_policy import AutoPolicy
2828

2929
from PyQt5 import QtCore, QtWidgets, QtGui
@@ -57,6 +57,9 @@ def __init__(self):
5757
self.settings_dialog = None
5858
self.node_info_dialog = None
5959

60+
# read the channels for this node, to be used by the other widgets
61+
lightning_channel.Channels.read_channels()
62+
6063
self.resize(50, 50)
6164
centralwidget = QtWidgets.QWidget(self)
6265
centralwidget.setObjectName("centralwidget")
@@ -87,14 +90,10 @@ def __init__(self):
8790
action_menu = menubar.addMenu('&Action')
8891
action_menu.addAction(open_channel_action)
8992

90-
# Channel List and Channel Graph need access to the ChannelInfoWidget to display info
91-
# create the channel info widget to be used by the channel list and graph
92-
self.channelInfoWidget = ChannelInfoWidget()
93-
9493
self.dockGraphWidget = QtWidgets.QDockWidget("Lightning Channel Graph", self)
9594
self.dockGraphWidget.setMinimumSize(QtCore.QSize(1035, 440))
9695
self.dockGraphWidget.setObjectName("dockGraphWidget")
97-
self.dockGraphWidget.setWidget(ChannelGraphWidget(self.channelInfoWidget))
96+
self.dockGraphWidget.setWidget(ChannelGraphWidget())
9897
self.addDockWidget(QtCore.Qt.DockWidgetArea(1), self.dockGraphWidget)
9998

10099
self.dockPendingChannelsWidget = QtWidgets.QDockWidget("Pending Channels", self)
@@ -106,7 +105,7 @@ def __init__(self):
106105
self.dockInfoWidget = QtWidgets.QDockWidget("Lightning Channel Info", self)
107106
self.dockInfoWidget.setMinimumSize(QtCore.QSize(800, 440))
108107
self.dockInfoWidget.setObjectName("dockInfoWidget")
109-
self.dockInfoWidget.setWidget(self.channelInfoWidget)
108+
self.dockInfoWidget.setWidget(ChannelInfoWidget())
110109
self.addDockWidget(QtCore.Qt.DockWidgetArea(QtCore.Qt.LeftDockWidgetArea), self.dockInfoWidget)
111110

112111
self.dockNodeInfoWidget = QtWidgets.QDockWidget("Node Info", self)
@@ -123,10 +122,6 @@ def __init__(self):
123122

124123
self.auto_policy = AutoPolicy()
125124

126-
# because the containing view for ChannelInfoWidget did not exist on time of creation
127-
# of the object, the object can not show itself
128-
self.channelInfoWidget.show()
129-
130125
def settings(self):
131126
self.settings_dialog = SettingsDialog()
132127
self.settings_dialog.setModal(True)
@@ -141,10 +136,6 @@ def settings(self):
141136
def open_channel(self):
142137
self.dockNodeInfoWidget.show()
143138

144-
@staticmethod
145-
def open_block_explorer(link_str):
146-
QtGui.QDesktopServices.openUrl(QtCore.QUrl(link_str))
147-
148139

149140
if __name__ == "__main__":
150141
app = QtWidgets.QApplication(sys.argv)

config/conductor.conf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ tls_cert = /home/coen/data
66
lnd_rpc_address = 192.168.0.110
77
lnd_rpc_port = 10009
88

9+
#section [SETTING] is optional, when present all the option below must be defined
910
[SETTINGS]
1011
default_sat_per_byte = 1
1112
default_base_fee_msat = 1000
1213
default_fee_rate = 0.000001
1314
default_time_lock_delta = 144
1415

16+
# section [POLICY] is optional, if present all the below options must be defined
1517
[POLICY]
1618
# if local balance divided by remote balance >= policy1_perc
1719
# in the case below if the local balance is at least 4 times
@@ -33,3 +35,6 @@ policy2_fee_rate = 0.000001
3335
policy3_perc = 0
3436
policy3_base_fee = 5000
3537
policy3_fee_rate = 0.000001
38+
39+
# the automatic policy update is run from the start and run every 2 hours when option is set to True
40+
auto_policy = True

0 commit comments

Comments
 (0)