forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_plugin.py
2958 lines (2412 loc) · 119 KB
/
test_plugin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from collections import OrderedDict
from datetime import datetime
from fixtures import * # noqa: F401,F403
from hashlib import sha256
from pyln.client import RpcError, Millisatoshi
from pyln.proto import Invoice
from utils import (
DEVELOPER, only_one, sync_blockheight, TIMEOUT, wait_for, TEST_NETWORK,
DEPRECATED_APIS, expected_peer_features, expected_node_features,
expected_channel_features, account_balance,
check_coin_moves, first_channel_id, EXPERIMENTAL_DUAL_FUND,
mine_funding_to_announce
)
import ast
import base64
import concurrent.futures
import json
import os
import pytest
import random
import re
import signal
import sqlite3
import stat
import subprocess
import time
import unittest
def test_option_passthrough(node_factory, directory):
""" Ensure that registering options works.
First attempts without the plugin and then with the plugin.
Then a plugin tries to register the same option "name" again, fails startup.
"""
plugin_path = os.path.join(os.getcwd(), 'contrib/plugins/helloworld.py')
plugin_path2 = os.path.join(os.getcwd(), 'tests/plugins/options.py')
help_out = subprocess.check_output([
'lightningd/lightningd',
'--lightning-dir={}'.format(directory),
'--help'
]).decode('utf-8')
assert('--greeting' not in help_out)
help_out = subprocess.check_output([
'lightningd/lightningd',
'--lightning-dir={}'.format(directory),
'--plugin={}'.format(plugin_path),
'--help'
]).decode('utf-8')
assert('--greeting' in help_out)
# Now try to see if it gets accepted, would fail to start if the
# option didn't exist
n = node_factory.get_node(options={'plugin': plugin_path, 'greeting': 'Ciao'})
n.stop()
with pytest.raises(subprocess.CalledProcessError):
err_out = subprocess.run([
'lightningd/lightningd',
'--lightning-dir={}'.format(directory),
'--plugin={}'.format(plugin_path),
'--plugin={}'.format(plugin_path2),
'--help'
], capture_output=True, check=True).stderr.decode('utf-8')
# first come first serve
assert("error starting plugin '{}': option name '--greeting' is already taken".format(plugin_path2) in err_out)
def test_option_types(node_factory):
"""Ensure that desired types of options are
respected in output """
plugin_path = os.path.join(os.getcwd(), 'tests/plugins/options.py')
n = node_factory.get_node(options={
'plugin': plugin_path,
'str_opt': 'ok',
'int_opt': 22,
'bool_opt': True,
})
assert n.daemon.is_in_log(r"option str_opt ok <class 'str'>")
assert n.daemon.is_in_log(r"option int_opt 22 <class 'int'>")
assert n.daemon.is_in_log(r"option bool_opt True <class 'bool'>")
# flag options aren't passed through if not flagged on
assert not n.daemon.is_in_log(r"option flag_opt")
n.stop()
# A blank bool_opt should default to false
n = node_factory.get_node(options={
'plugin': plugin_path, 'str_opt': 'ok',
'int_opt': 22,
'bool_opt': 'true',
'flag_opt': None,
})
assert n.daemon.is_in_log(r"option bool_opt True <class 'bool'>")
assert n.daemon.is_in_log(r"option flag_opt True <class 'bool'>")
n.stop()
# What happens if we give it a bad bool-option?
n = node_factory.get_node(options={
'plugin': plugin_path,
'str_opt': 'ok',
'int_opt': 22,
'bool_opt': '!',
}, may_fail=True, start=False)
# the node should fail after start, and we get a stderr msg
n.daemon.start(wait_for_initialized=False, stderr_redir=True)
assert n.daemon.wait() == 1
wait_for(lambda: n.daemon.is_in_stderr('bool_opt: ! does not parse as type bool'))
# What happens if we give it a bad int-option?
n = node_factory.get_node(options={
'plugin': plugin_path,
'str_opt': 'ok',
'int_opt': 'notok',
'bool_opt': 1,
}, may_fail=True, start=False)
# the node should fail after start, and we get a stderr msg
n.daemon.start(wait_for_initialized=False, stderr_redir=True)
assert n.daemon.wait() == 1
assert n.daemon.is_in_stderr('--int_opt: notok does not parse as type int')
# Flag opts shouldn't allow any input
n = node_factory.get_node(options={
'plugin': plugin_path,
'str_opt': 'ok',
'int_opt': 11,
'bool_opt': 1,
'flag_opt': True,
}, may_fail=True, start=False)
# the node should fail after start, and we get a stderr msg
n.daemon.start(wait_for_initialized=False, stderr_redir=True)
assert n.daemon.wait() == 1
assert n.daemon.is_in_stderr("--flag_opt: doesn't allow an argument")
n = node_factory.get_node(options={
'plugin': plugin_path,
'str_optm': ['ok', 'ok2'],
'int_optm': [11, 12, 13],
})
assert n.daemon.is_in_log(r"option str_optm \['ok', 'ok2'\] <class 'list'>")
assert n.daemon.is_in_log(r"option int_optm \[11, 12, 13\] <class 'list'>")
n.stop()
def test_millisatoshi_passthrough(node_factory):
""" Ensure that Millisatoshi arguments and return work.
"""
plugin_path = os.path.join(os.getcwd(), 'tests/plugins/millisatoshis.py')
n = node_factory.get_node(options={'plugin': plugin_path, 'log-level': 'io'})
# By keyword
ret = n.rpc.call('echo', {'msat': Millisatoshi(17), 'not_an_msat': '22msat'})['echo_msat']
assert type(ret) == Millisatoshi
assert ret == Millisatoshi(17)
# By position
ret = n.rpc.call('echo', [Millisatoshi(18), '22msat'])['echo_msat']
assert type(ret) == Millisatoshi
assert ret == Millisatoshi(18)
def test_rpc_passthrough(node_factory):
"""Starting with a plugin exposes its RPC methods.
First check that the RPC method appears in the help output and
then try to call it.
"""
plugin_path = os.path.join(os.getcwd(), 'contrib/plugins/helloworld.py')
n = node_factory.get_node(options={'plugin': plugin_path, 'greeting': 'Ciao'})
# Make sure that the 'hello' command that the helloworld.py plugin
# has registered is available.
cmd = [hlp for hlp in n.rpc.help()['help'] if 'hello' in hlp['command']]
assert(len(cmd) == 1)
# Make sure usage message is present.
assert only_one(n.rpc.help('hello')['help'])['command'] == 'hello [name]'
# While we're at it, let's check that helloworld.py is logging
# correctly via the notifications plugin->lightningd
assert n.daemon.is_in_log('Plugin helloworld.py initialized')
# Now try to call it and see what it returns:
greet = n.rpc.hello(name='World')
assert(greet == "Ciao World")
with pytest.raises(RpcError):
n.rpc.fail()
# Try to call a method without enough arguments
with pytest.raises(RpcError, match="processing bye: missing a required"
" argument"):
n.rpc.bye()
def test_plugin_dir(node_factory):
"""--plugin-dir works"""
plugin_dir = os.path.join(os.getcwd(), 'contrib/plugins')
node_factory.get_node(options={'plugin-dir': plugin_dir, 'greeting': 'Mars'})
def test_plugin_slowinit(node_factory):
"""Tests that the 'plugin' RPC command times out if plugin doesnt respond"""
os.environ['SLOWINIT_TIME'] = '61'
n = node_factory.get_node()
with pytest.raises(RpcError, match=': timed out before replying to init'):
n.rpc.plugin_start(os.path.join(os.getcwd(), "tests/plugins/slow_init.py"))
# It's not actually configured yet, see what happens;
# make sure 'rescan' and 'list' controls dont crash
n.rpc.plugin_rescan()
n.rpc.plugin_list()
def test_plugin_command(node_factory):
"""Tests the 'plugin' RPC command"""
n = node_factory.get_node()
# Make sure that the 'hello' command from the helloworld.py plugin
# is not available.
cmd = [hlp for hlp in n.rpc.help()["help"] if "hello" in hlp["command"]]
assert(len(cmd) == 0)
# Add the 'contrib/plugins' test dir
n.rpc.plugin_startdir(directory=os.path.join(os.getcwd(), "contrib/plugins"))
# Make sure that the 'hello' command from the helloworld.py plugin
# is now available.
cmd = [hlp for hlp in n.rpc.help()["help"] if "hello" in hlp["command"]]
assert(len(cmd) == 1)
# Make sure 'rescan' and 'list' subcommands dont crash
n.rpc.plugin_rescan()
n.rpc.plugin_list()
# Make sure the plugin behaves normally after stop and restart
assert("Successfully stopped helloworld.py."
== n.rpc.plugin_stop(plugin="helloworld.py")["result"])
n.daemon.wait_for_log(r"Killing plugin: stopped by lightningd via RPC")
n.rpc.plugin_start(plugin=os.path.join(os.getcwd(), "contrib/plugins/helloworld.py"))
n.daemon.wait_for_log(r"Plugin helloworld.py initialized")
assert("Hello world" == n.rpc.call(method="hello"))
# Now stop the helloworld plugin
assert("Successfully stopped helloworld.py."
== n.rpc.plugin_stop(plugin="helloworld.py")["result"])
n.daemon.wait_for_log(r"Killing plugin: stopped by lightningd via RPC")
# Make sure that the 'hello' command from the helloworld.py plugin
# is not available anymore.
cmd = [hlp for hlp in n.rpc.help()["help"] if "hello" in hlp["command"]]
assert(len(cmd) == 0)
# Test that we cannot start a plugin with 'dynamic' set to False in
# getmanifest
with pytest.raises(RpcError, match=r"Not a dynamic plugin"):
n.rpc.plugin_start(plugin=os.path.join(os.getcwd(), "tests/plugins/static.py"))
# Test that we cannot stop a started plugin with 'dynamic' flag set to
# False
n2 = node_factory.get_node(options={
"plugin": os.path.join(os.getcwd(), "tests/plugins/static.py")
})
with pytest.raises(RpcError, match=r"static.py cannot be managed when lightningd is up"):
n2.rpc.plugin_stop(plugin="static.py")
# Test that we don't crash when starting a broken plugin
with pytest.raises(RpcError, match=r": exited before replying to getmanifest"):
n2.rpc.plugin_start(plugin=os.path.join(os.getcwd(), "tests/plugins/broken.py"))
with pytest.raises(RpcError, match=r': timed out before replying to getmanifest'):
n2.rpc.plugin_start(os.path.join(os.getcwd(), 'contrib/plugins/fail/failtimeout.py'))
# Test that we can add a directory with more than one new plugin in it.
try:
n.rpc.plugin_startdir(os.path.join(os.getcwd(), "contrib/plugins"))
except RpcError:
pass
# Usually, it crashes after the above return.
n.rpc.stop()
def test_plugin_disable(node_factory):
"""--disable-plugin works"""
plugin_dir = os.path.join(os.getcwd(), 'contrib/plugins')
# We used to need plugin-dir before disable-plugin!
n = node_factory.get_node(options=OrderedDict([('plugin-dir', plugin_dir),
('disable-plugin',
'{}/helloworld.py'
.format(plugin_dir))]))
with pytest.raises(RpcError):
n.rpc.hello(name='Sun')
assert n.daemon.is_in_log('helloworld.py: disabled via disable-plugin')
n.stop()
# Also works by basename.
n = node_factory.get_node(options=OrderedDict([('plugin-dir', plugin_dir),
('disable-plugin',
'helloworld.py')]))
with pytest.raises(RpcError):
n.rpc.hello(name='Sun')
assert n.daemon.is_in_log('helloworld.py: disabled via disable-plugin')
n.stop()
# Other order also works!
n = node_factory.get_node(options=OrderedDict([('disable-plugin',
'helloworld.py'),
('plugin-dir', plugin_dir)]))
with pytest.raises(RpcError):
n.rpc.hello(name='Sun')
assert n.daemon.is_in_log('helloworld.py: disabled via disable-plugin')
n.stop()
# Both orders of explicit specification work.
n = node_factory.get_node(options=OrderedDict([('disable-plugin',
'helloworld.py'),
('plugin',
'{}/helloworld.py'
.format(plugin_dir))]))
with pytest.raises(RpcError):
n.rpc.hello(name='Sun')
assert n.daemon.is_in_log('helloworld.py: disabled via disable-plugin')
n.stop()
# Both orders of explicit specification work.
n = node_factory.get_node(options=OrderedDict([('plugin',
'{}/helloworld.py'
.format(plugin_dir)),
('disable-plugin',
'helloworld.py')]))
with pytest.raises(RpcError):
n.rpc.hello(name='Sun')
assert n.daemon.is_in_log('helloworld.py: disabled via disable-plugin')
# Still disabled if we load directory.
n.rpc.plugin_startdir(directory=os.path.join(os.getcwd(), "contrib/plugins"))
n.daemon.wait_for_log('helloworld.py: disabled via disable-plugin')
n.stop()
# Check that list works
n = node_factory.get_node(options={'disable-plugin':
['something-else.py', 'helloworld.py']})
assert n.rpc.listconfigs()['disable-plugin'] == ['something-else.py', 'helloworld.py']
def test_plugin_hook(node_factory, executor):
"""The helloworld plugin registers a htlc_accepted hook.
The hook will sleep for a few seconds and log a
message. `lightningd` should wait for the response and only then
complete the payment.
"""
l1, l2 = node_factory.line_graph(2, opts={'plugin': os.path.join(os.getcwd(), 'contrib/plugins/helloworld.py')})
start_time = time.time()
f = executor.submit(l1.pay, l2, 100000)
l2.daemon.wait_for_log(r'on_htlc_accepted called')
# The hook will sleep for 20 seconds before answering, so `f`
# should take at least that long.
f.result()
end_time = time.time()
assert(end_time >= start_time + 20)
def test_plugin_connect_notifications(node_factory):
""" test 'connect' and 'disconnect' notifications
"""
l1, l2 = node_factory.get_nodes(2, opts={'plugin': os.path.join(os.getcwd(), 'contrib/plugins/helloworld.py')})
l1.connect(l2)
l1.daemon.wait_for_log(r'Received connect event')
l2.daemon.wait_for_log(r'Received connect event')
l2.rpc.disconnect(l1.info['id'])
l1.daemon.wait_for_log(r'Received disconnect event')
l2.daemon.wait_for_log(r'Received disconnect event')
def test_failing_plugins(directory):
fail_plugins = [
os.path.join(os.getcwd(), 'contrib/plugins/fail/failtimeout.py'),
os.path.join(os.getcwd(), 'contrib/plugins/fail/doesnotexist.py'),
]
for p in fail_plugins:
with pytest.raises(subprocess.CalledProcessError):
subprocess.check_output([
'lightningd/lightningd',
'--lightning-dir={}'.format(directory),
'--plugin={}'.format(p),
'--help',
])
def test_pay_plugin(node_factory):
l1, l2 = node_factory.line_graph(2)
inv = l2.rpc.invoice(123000, 'label', 'description', 3700)
res = l1.rpc.pay(bolt11=inv['bolt11'])
assert res['status'] == 'complete'
with pytest.raises(RpcError, match=r'missing required parameter'):
l1.rpc.call('pay')
# Make sure usage messages are present.
msg = 'pay bolt11 [amount_msat] [label] [riskfactor] [maxfeepercent] '\
'[retry_for] [maxdelay] [exemptfee] [localofferid] [exclude] '\
'[maxfee] [description]'
if DEVELOPER:
msg += ' [use_shadow]'
assert only_one(l1.rpc.help('pay')['help'])['command'] == msg
def test_plugin_connected_hook_chaining(node_factory):
""" l1 uses the logger_a, reject and logger_b plugin.
l1 is configured to accept connections from l2, but not from l3.
we check that logger_a is always called and logger_b only for l2.
"""
opts = [{'plugin':
[os.path.join(os.getcwd(),
'tests/plugins/peer_connected_logger_a.py'),
os.path.join(os.getcwd(),
'tests/plugins/reject.py'),
os.path.join(os.getcwd(),
'tests/plugins/peer_connected_logger_b.py')],
'allow_warning': True},
{},
{'allow_warning': True}]
l1, l2, l3 = node_factory.get_nodes(3, opts=opts)
l2id = l2.info['id']
l3id = l3.info['id']
l1.rpc.reject(l3.info['id'])
l2.connect(l1)
l1.daemon.wait_for_logs([
f"peer_connected_logger_a {l2id}",
f"{l2id} is allowed",
f"peer_connected_logger_b {l2id}"
])
assert len(l1.rpc.listpeers(l2id)['peers']) == 1
l3.connect(l1)
l1.daemon.wait_for_logs([
f"peer_connected_logger_a {l3id}",
f"{l3id} is in reject list"
])
# FIXME: this error occurs *after* connection, so we connect then drop.
l3.daemon.wait_for_log(r"-connectd: peer_in WIRE_WARNING")
l3.daemon.wait_for_log(r"You are in reject list")
def check_disconnect():
peers = l1.rpc.listpeers(l3id)['peers']
return peers == [] or not peers[0]['connected']
wait_for(check_disconnect)
assert not l1.daemon.is_in_log(f"peer_connected_logger_b {l3id}")
@pytest.mark.developer("localhost remote_addr will be filtered without DEVELOEPR")
def test_peer_connected_remote_addr(node_factory):
"""This tests the optional tlv `remote_addr` being passed to a plugin.
The optional tlv `remote_addr` should only be visible to the initiator l1.
"""
pluginpath = os.path.join(os.getcwd(), 'tests/plugins/peer_connected_logger_a.py')
l1, l2 = node_factory.get_nodes(2, opts={
'plugin': pluginpath,
'dev-allow-localhost': None})
l1id = l1.info['id']
l2id = l2.info['id']
l1.connect(l2)
l1log = l1.daemon.wait_for_log(f"peer_connected_logger_a {l2id}")
l2log = l2.daemon.wait_for_log(f"peer_connected_logger_a {l1id}")
# the log entries are followed by the peer_connected payload as dict {} like:
# {'id': '022d223...', 'direction': 'out', 'addr': '127.0.0.1:35289',
# 'remote_addr': '127.0.0.1:59582', 'features': '8808226aa2'}
l1payload = eval(l1log[l1log.find('{'):])
l2payload = eval(l2log[l2log.find('{'):])
# check that l1 sees its remote_addr as l2 sees l1
assert(l1payload['remote_addr'] == l2payload['addr'])
assert(not l2payload.get('remote_addr')) # l2 can't see a remote_addr
def test_async_rpcmethod(node_factory, executor):
"""This tests the async rpcmethods.
It works in conjunction with the `asynctest` plugin which stashes
requests and then resolves all of them on the fifth call.
"""
l1 = node_factory.get_node(options={'plugin': os.path.join(os.getcwd(), 'tests/plugins/asynctest.py')})
results = []
for i in range(10):
results.append(executor.submit(l1.rpc.asyncqueue))
time.sleep(3)
# None of these should have returned yet
assert len([r for r in results if r.done()]) == 0
# This last one triggers the release and all results should be 42,
# since the last number is returned for all
l1.rpc.asyncflush(42)
assert [r.result() for r in results] == [42] * len(results)
@unittest.skipIf(os.getenv('TEST_DB_PROVIDER', 'sqlite3') != 'sqlite3', "Only sqlite3 implements the db_write_hook currently")
def test_db_hook(node_factory, executor):
"""This tests the db hook."""
dbfile = os.path.join(node_factory.directory, "dblog.sqlite3")
l1 = node_factory.get_node(options={'plugin': os.path.join(os.getcwd(), 'tests/plugins/dblog.py'),
'dblog-file': dbfile})
# It should see the db being created, and sometime later actually get
# initted.
# This precedes startup, so needle already past
assert l1.daemon.is_in_log(r'plugin-dblog.py: deferring \d+ commands')
l1.daemon.logsearch_start = 0
l1.daemon.wait_for_log('plugin-dblog.py: replaying pre-init data:')
l1.daemon.wait_for_log('plugin-dblog.py: CREATE TABLE version \\(version INTEGER\\)')
l1.daemon.wait_for_log("plugin-dblog.py: initialized.* 'startup': True")
l1.stop()
# Databases should be identical.
db1 = sqlite3.connect(os.path.join(l1.daemon.lightning_dir, TEST_NETWORK, 'lightningd.sqlite3'))
db2 = sqlite3.connect(dbfile)
assert [x for x in db1.iterdump()] == [x for x in db2.iterdump()]
@unittest.skipIf(os.getenv('TEST_DB_PROVIDER', 'sqlite3') != 'sqlite3', "Only sqlite3 implements the db_write_hook currently")
def test_db_hook_multiple(node_factory, executor):
"""This tests the db hook for multiple-plugin case."""
dbfile = os.path.join(node_factory.directory, "dblog.sqlite3")
l1 = node_factory.get_node(options={'plugin': os.path.join(os.getcwd(), 'tests/plugins/dblog.py'),
'important-plugin': os.path.join(os.getcwd(), 'tests/plugins/dbdummy.py'),
'dblog-file': dbfile})
# It should see the db being created, and sometime later actually get
# initted.
# This precedes startup, so needle already past
assert l1.daemon.is_in_log(r'plugin-dblog.py: deferring \d+ commands')
l1.daemon.logsearch_start = 0
l1.daemon.wait_for_log('plugin-dblog.py: replaying pre-init data:')
l1.daemon.wait_for_log('plugin-dblog.py: CREATE TABLE version \\(version INTEGER\\)')
l1.daemon.wait_for_log("plugin-dblog.py: initialized.* 'startup': True")
l1.stop()
# Databases should be identical.
db1 = sqlite3.connect(os.path.join(l1.daemon.lightning_dir, TEST_NETWORK, 'lightningd.sqlite3'))
db2 = sqlite3.connect(dbfile)
assert [x for x in db1.iterdump()] == [x for x in db2.iterdump()]
def test_utf8_passthrough(node_factory, executor):
l1 = node_factory.get_node(options={'plugin': os.path.join(os.getcwd(), 'tests/plugins/utf8.py'),
'log-level': 'io'})
# This works because Python unmangles.
res = l1.rpc.call('utf8', ['ナンセンス 1杯'])
assert '\\u' not in res['utf8']
assert res['utf8'] == 'ナンセンス 1杯'
# Now, try native.
out = subprocess.check_output(['cli/lightning-cli',
'--network={}'.format(TEST_NETWORK),
'--lightning-dir={}'
.format(l1.daemon.lightning_dir),
'utf8', 'ナンセンス 1杯']).decode('utf-8')
assert '\\u' not in out
assert out == '{\n "utf8": "ナンセンス 1杯"\n}\n'
def test_invoice_payment_hook(node_factory):
""" l1 uses the reject-payment plugin to reject invoices with odd preimages.
"""
opts = [{}, {'plugin': os.path.join(os.getcwd(), 'tests/plugins/reject_some_invoices.py')}]
l1, l2 = node_factory.line_graph(2, opts=opts)
# This one works
inv1 = l2.rpc.invoice(1230, 'label', 'description', preimage='1' * 64)
l1.rpc.pay(inv1['bolt11'])
l2.daemon.wait_for_log('label=label')
l2.daemon.wait_for_log('msat=')
l2.daemon.wait_for_log('preimage=' + '1' * 64)
# This one will be rejected.
inv2 = l2.rpc.invoice(1230, 'label2', 'description', preimage='0' * 64)
with pytest.raises(RpcError):
l1.rpc.pay(inv2['bolt11'])
pstatus = l1.rpc.call('paystatus', [inv2['bolt11']])['pay'][0]
assert pstatus['attempts'][-1]['failure']['data']['failcodename'] == 'WIRE_TEMPORARY_NODE_FAILURE'
l2.daemon.wait_for_log('label=label2')
l2.daemon.wait_for_log('msat=')
l2.daemon.wait_for_log('preimage=' + '0' * 64)
def test_invoice_payment_hook_hold(node_factory):
""" l1 uses the hold_invoice plugin to delay invoice payment.
"""
opts = [{}, {'plugin': os.path.join(os.getcwd(), 'tests/plugins/hold_invoice.py'), 'holdtime': TIMEOUT / 2}]
l1, l2 = node_factory.line_graph(2, opts=opts)
inv1 = l2.rpc.invoice(1230, 'label', 'description', preimage='1' * 64)
l1.rpc.pay(inv1['bolt11'])
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
def test_openchannel_hook(node_factory, bitcoind):
""" l2 uses the reject_odd_funding_amounts plugin to reject some openings.
"""
opts = [{}, {'plugin': os.path.join(os.getcwd(), 'tests/plugins/reject_odd_funding_amounts.py')}]
l1, l2 = node_factory.line_graph(2, fundchannel=False, opts=opts)
l1.fundwallet(10**6)
# Even amount: works.
l1.rpc.fundchannel(l2.info['id'], 100000)
# Make sure plugin got all the vars we expect
expected = {
'channel_flags': '1',
'dust_limit_msat': 546000,
'htlc_minimum_msat': 0,
'id': l1.info['id'],
'max_accepted_htlcs': '483',
'max_htlc_value_in_flight_msat': 18446744073709551615,
'to_self_delay': '5',
}
if l2.config('experimental-dual-fund'):
# openchannel2 var checks
expected.update({
'channel_id': '.*',
'commitment_feerate_per_kw': '7500',
'funding_feerate_per_kw': '7500',
'feerate_our_max': '150000',
'feerate_our_min': '1875',
'locktime': '.*',
'their_funding_msat': 100000000,
'channel_max_msat': 16777215000,
})
else:
expected.update({
'channel_reserve_msat': 1000000,
'feerate_per_kw': '7500',
'funding_msat': 100000000,
'push_msat': 0,
})
l2.daemon.wait_for_log('reject_odd_funding_amounts.py: {} VARS'.format(len(expected)))
for k, v in expected.items():
assert l2.daemon.is_in_log('reject_odd_funding_amounts.py: {}={}'.format(k, v))
# Close it.
txid = l1.rpc.close(l2.info['id'])['txid']
bitcoind.generate_block(1, txid)
wait_for(lambda: [c['state'] for c in only_one(l1.rpc.listpeers(l2.info['id'])['peers'])['channels']] == ['ONCHAIN'])
# Odd amount: fails
l1.connect(l2)
with pytest.raises(RpcError, match=r"I don't like odd amounts"):
l1.rpc.fundchannel(l2.info['id'], 100001)
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
def test_openchannel_hook_error_handling(node_factory, bitcoind):
""" l2 uses a plugin that should fatal() crash the node.
This is because the plugin rejects a channel while
also setting a close_to address which isn't allowed.
"""
opts = {'plugin': os.path.join(os.getcwd(), 'tests/plugins/openchannel_hook_accepter.py')}
# openchannel_reject_but_set_close_to.py')}
l1 = node_factory.get_node()
l2 = node_factory.get_node(options=opts,
expect_fail=True,
may_fail=True,
allow_broken_log=True)
l1.connect(l2)
l1.fundwallet(10**6)
# next fundchannel should fail fatal() for l2
with pytest.raises(RpcError, match=r'Owning subdaemon (openingd|dualopend) died'):
l1.rpc.fundchannel(l2.info['id'], 100004)
assert l2.daemon.is_in_log("BROKEN.*Plugin rejected openchannel[2]? but also set close_to")
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
def test_openchannel_hook_chaining(node_factory, bitcoind):
""" l2 uses a set of plugin that all use the openchannel_hook.
We test that chaining works by using multiple plugins in a way
that we check for the first plugin that rejects prevents from evaluating
further plugin responses down the chain.
"""
opts = [{}, {'plugin': [
os.path.join(os.path.dirname(__file__), '..', 'tests/plugins/openchannel_hook_accept.py'),
os.path.join(os.path.dirname(__file__), '..', 'tests/plugins/openchannel_hook_accepter.py'),
os.path.join(os.path.dirname(__file__), '..', 'tests/plugins/openchannel_hook_reject.py')
]}]
l1, l2 = node_factory.line_graph(2, fundchannel=False, opts=opts)
l1.fundwallet(10**6)
hook_msg = "openchannel2? hook rejects and says '"
# 100005sat fundchannel should fail fatal() for l2
# because hook_accepter.py rejects on that amount 'for a reason'
with pytest.raises(RpcError, match=r'reject for a reason'):
l1.rpc.fundchannel(l2.info['id'], 100005)
assert l2.daemon.wait_for_log(hook_msg + "reject for a reason")
# first plugin in the chain was called
assert l2.daemon.is_in_log("accept on principle")
# the third plugin must now not be called anymore
assert not l2.daemon.is_in_log("reject on principle")
wait_for(lambda: l1.rpc.listpeers()['peers'] == [])
l1.rpc.connect(l2.info['id'], 'localhost', l2.port)
# 100000sat is good for hook_accepter, so it should fail 'on principle'
# at third hook openchannel_reject.py
with pytest.raises(RpcError, match=r'reject on principle'):
l1.rpc.fundchannel(l2.info['id'], 100000)
assert l2.daemon.wait_for_log(hook_msg + "reject on principle")
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
def test_channel_state_changed_bilateral(node_factory, bitcoind):
""" We open and close a channel and check notifications both sides.
The misc_notifications.py plugin logs `channel_state_changed` events.
"""
opts = {"plugin": os.path.join(os.getcwd(), "tests/plugins/misc_notifications.py")}
l1, l2 = node_factory.line_graph(2, opts=opts)
l1_id = l1.rpc.getinfo()["id"]
l2_id = l2.rpc.getinfo()["id"]
cid = l1.get_channel_id(l2)
scid = l1.get_channel_scid(l2)
# a helper that gives us the next channel_state_changed log entry
def wait_for_event(node):
msg = node.daemon.wait_for_log("channel_state_changed.*new_state.*")
event = ast.literal_eval(re.findall(".*({.*}).*", msg)[0])
return event
# check channel 'opener' and 'closer' within this testcase ...
assert(l1.rpc.listpeers()['peers'][0]['channels'][0]['opener'] == 'local')
assert(l2.rpc.listpeers()['peers'][0]['channels'][0]['opener'] == 'remote')
# the 'closer' should be missing initially
assert 'closer' not in l1.rpc.listpeers()['peers'][0]['channels'][0]
assert 'closer' not in l2.rpc.listpeers()['peers'][0]['channels'][0]
event1 = wait_for_event(l1)
event2 = wait_for_event(l2)
assert(event1['peer_id'] == l2_id) # we only test these IDs the first time
assert(event1['channel_id'] == cid)
assert(event1['short_channel_id'] is None) # None until locked in
assert(event1['cause'] == "user")
assert(event2['peer_id'] == l1_id) # we only test these IDs the first time
assert(event2['channel_id'] == cid)
assert(event2['short_channel_id'] is None) # None until locked in
assert(event2['cause'] == "remote")
for ev in [event1, event2]:
# Dual funded channels
if l1.config('experimental-dual-fund'):
assert(ev['old_state'] == "DUALOPEND_OPEN_INIT")
assert(ev['new_state'] == "DUALOPEND_AWAITING_LOCKIN")
assert(ev['message'] == "Sigs exchanged, waiting for lock-in")
else:
assert(ev['old_state'] == "unknown")
assert(ev['new_state'] == "CHANNELD_AWAITING_LOCKIN")
assert(ev['message'] == "new channel opened")
event1 = wait_for_event(l1)
event2 = wait_for_event(l2)
assert(event1['short_channel_id'] == scid)
if l1.config('experimental-dual-fund'):
assert(event1['old_state'] == "DUALOPEND_AWAITING_LOCKIN")
else:
assert(event1['old_state'] == "CHANNELD_AWAITING_LOCKIN")
assert(event1['new_state'] == "CHANNELD_NORMAL")
assert(event1['cause'] == "user")
assert(event1['message'] == "Lockin complete")
assert(event2['short_channel_id'] == scid)
if l1.config('experimental-dual-fund'):
assert(event2['old_state'] == "DUALOPEND_AWAITING_LOCKIN")
else:
assert(event2['old_state'] == "CHANNELD_AWAITING_LOCKIN")
assert(event2['new_state'] == "CHANNELD_NORMAL")
assert(event2['cause'] == "remote")
assert(event2['message'] == "Lockin complete")
# also test the correctness of timestamps once
assert(datetime.strptime(event1['timestamp'], '%Y-%m-%dT%H:%M:%S.%fZ'))
assert(datetime.strptime(event2['timestamp'], '%Y-%m-%dT%H:%M:%S.%fZ'))
# close channel and look for stateful events
l1.rpc.close(scid)
event1 = wait_for_event(l1)
assert(event1['old_state'] == "CHANNELD_NORMAL")
assert(event1['new_state'] == "CHANNELD_SHUTTING_DOWN")
assert(event1['cause'] == "user")
assert(event1['message'] == "User or plugin invoked close command")
event2 = wait_for_event(l2)
assert(event2['old_state'] == "CHANNELD_NORMAL")
assert(event2['new_state'] == "CHANNELD_SHUTTING_DOWN")
assert(event2['cause'] == "remote")
assert(event2['message'] == "Peer closes channel")
# 'closer' should now be set accordingly ...
assert(l1.rpc.listpeers()['peers'][0]['channels'][0]['closer'] == 'local')
assert(l2.rpc.listpeers()['peers'][0]['channels'][0]['closer'] == 'remote')
event1 = wait_for_event(l1)
assert(event1['old_state'] == "CHANNELD_SHUTTING_DOWN")
assert(event1['new_state'] == "CLOSINGD_SIGEXCHANGE")
assert(event1['cause'] == "user")
assert(event1['message'] == "Start closingd")
event2 = wait_for_event(l2)
assert(event2['old_state'] == "CHANNELD_SHUTTING_DOWN")
assert(event2['new_state'] == "CLOSINGD_SIGEXCHANGE")
assert(event2['cause'] == "remote")
assert(event2['message'] == "Start closingd")
event1 = wait_for_event(l1)
assert(event1['old_state'] == "CLOSINGD_SIGEXCHANGE")
assert(event1['new_state'] == "CLOSINGD_COMPLETE")
assert(event1['cause'] == "user")
assert(event1['message'] == "Closing complete")
event2 = wait_for_event(l2)
assert(event2['old_state'] == "CLOSINGD_SIGEXCHANGE")
assert(event2['new_state'] == "CLOSINGD_COMPLETE")
assert(event2['cause'] == "remote")
assert(event2['message'] == "Closing complete")
bitcoind.generate_block(100, wait_for_mempool=1) # so it gets settled
event1 = wait_for_event(l1)
assert(event1['old_state'] == "CLOSINGD_COMPLETE")
assert(event1['new_state'] == "FUNDING_SPEND_SEEN")
assert(event1['cause'] == "user")
assert(event1['message'] == "Onchain funding spend")
event2 = wait_for_event(l2)
assert(event2['old_state'] == "CLOSINGD_COMPLETE")
assert(event2['new_state'] == "FUNDING_SPEND_SEEN")
assert(event2['cause'] == "remote")
assert(event2['message'] == "Onchain funding spend")
event1 = wait_for_event(l1)
assert(event1['old_state'] == "FUNDING_SPEND_SEEN")
assert(event1['new_state'] == "ONCHAIN")
assert(event1['cause'] == "user")
assert(event1['message'] == "Onchain init reply")
event2 = wait_for_event(l2)
assert(event2['old_state'] == "FUNDING_SPEND_SEEN")
assert(event2['new_state'] == "ONCHAIN")
assert(event2['cause'] == "remote")
assert(event2['message'] == "Onchain init reply")
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
def test_channel_state_changed_unilateral(node_factory, bitcoind):
""" We open, disconnect, force-close a channel and check for notifications.
The misc_notifications.py plugin logs `channel_state_changed` events.
"""
opts = {"plugin": os.path.join(os.getcwd(), "tests/plugins/misc_notifications.py"),
"allow_warning": True,
'may_reconnect': True}
l1, l2 = node_factory.line_graph(2, opts=opts)
l1_id = l1.rpc.getinfo()["id"]
cid = l1.get_channel_id(l2)
scid = l1.get_channel_scid(l2)
# a helper that gives us the next channel_state_changed log entry
def wait_for_event(node):
msg = node.daemon.wait_for_log("channel_state_changed.*new_state.*")
event = ast.literal_eval(re.findall(".*({.*}).*", msg)[0])
return event
event2 = wait_for_event(l2)
assert(event2['peer_id'] == l1_id)
assert(event2['channel_id'] == cid)
assert(event2['short_channel_id'] is None)
assert(event2['cause'] == "remote")
if l2.config('experimental-dual-fund'):
assert(event2['old_state'] == "DUALOPEND_OPEN_INIT")
assert(event2['new_state'] == "DUALOPEND_AWAITING_LOCKIN")
assert(event2['message'] == "Sigs exchanged, waiting for lock-in")
else:
assert(event2['old_state'] == "unknown")
assert(event2['new_state'] == "CHANNELD_AWAITING_LOCKIN")
assert(event2['message'] == "new channel opened")
event2 = wait_for_event(l2)
assert(event2['short_channel_id'] == scid)
if l2.config('experimental-dual-fund'):
assert(event2['old_state'] == "DUALOPEND_AWAITING_LOCKIN")
else:
assert(event2['old_state'] == "CHANNELD_AWAITING_LOCKIN")
assert(event2['new_state'] == "CHANNELD_NORMAL")
assert(event2['cause'] == "remote")
assert(event2['message'] == "Lockin complete")
# close channel unilaterally and look for stateful events
l1.rpc.stop()
wait_for(lambda: not only_one(l2.rpc.listpeers()['peers'])['connected'])
l2.rpc.close(scid, 1) # force close after 1sec timeout
event2 = wait_for_event(l2)
assert(event2['old_state'] == "CHANNELD_NORMAL")
assert(event2['new_state'] == "CHANNELD_SHUTTING_DOWN")
assert(event2['cause'] == "user")
assert(event2['message'] == "User or plugin invoked close command")
event2 = wait_for_event(l2)
assert(event2['old_state'] == "CHANNELD_SHUTTING_DOWN")
assert(event2['new_state'] == "AWAITING_UNILATERAL")
assert(event2['cause'] == "user")
assert(event2['message'] == "Forcibly closed by `close` command timeout")
# restart l1 now, it will reconnect and l2 will send it an error.
l1.restart()
wait_for(lambda: len(l1.rpc.listpeers()['peers']) == 1)
# check 'closer' on l2 while the peer is not yet forgotten
assert(l2.rpc.listpeers()['peers'][0]['channels'][0]['closer'] == 'local')
if EXPERIMENTAL_DUAL_FUND:
l1.daemon.wait_for_log(r'Peer has reconnected, state')
l2.daemon.wait_for_log(r'Telling connectd to send error')
# l1 will receive error, and go into AWAITING_UNILATERAL
# FIXME: l2 should re-xmit shutdown, but it doesn't until it's mined :(
event1 = wait_for_event(l1)
# Doesn't have closer, since it blames the "protocol"?
assert 'closer' not in l1.rpc.listpeers()['peers'][0]['channels'][0]
assert(event1['old_state'] == "CHANNELD_NORMAL")
assert(event1['new_state'] == "AWAITING_UNILATERAL")
assert(event1['cause'] == "protocol")
assert(event1['message'] == "channeld: received ERROR error channel {}: Forcibly closed by `close` command timeout".format(cid))
# settle the channel closure
bitcoind.generate_block(100)
event2 = wait_for_event(l2)
assert(event2['old_state'] == "AWAITING_UNILATERAL")
assert(event2['new_state'] == "FUNDING_SPEND_SEEN")
assert(event2['cause'] == "user")
assert(event2['message'] == "Onchain funding spend")
event2 = wait_for_event(l2)
assert(event2['old_state'] == "FUNDING_SPEND_SEEN")
assert(event2['new_state'] == "ONCHAIN")
assert(event2['cause'] == "user")
assert(event2['message'] == "Onchain init reply")
# Check 'closer' on l1 while the peer is not yet forgotten
event1 = wait_for_event(l1)
assert(l1.rpc.listpeers()['peers'][0]['channels'][0]['closer'] == 'remote')
assert(event1['old_state'] == "AWAITING_UNILATERAL")
assert(event1['new_state'] == "FUNDING_SPEND_SEEN")
assert(event1['cause'] == "onchain")
assert(event1['message'] == "Onchain funding spend")
event1 = wait_for_event(l1)
assert(event1['old_state'] == "FUNDING_SPEND_SEEN")