-
Notifications
You must be signed in to change notification settings - Fork 990
Expand file tree
/
Copy pathtest_splice.py
More file actions
720 lines (552 loc) · 33.2 KB
/
test_splice.py
File metadata and controls
720 lines (552 loc) · 33.2 KB
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
from fixtures import * # noqa: F401,F403
from pathlib import Path
from pyln.client import Millisatoshi
import pytest
import re
import unittest
from utils import (
bkpr_account_balance, check_coin_moves, first_channel_id,
TEST_NETWORK, only_one, wait_for
)
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
def test_script_splice_out(node_factory, bitcoind, chainparams):
fundamt = 1000000
coin_mvt_plugin = Path(__file__).parent / "plugins" / "coin_movements.py"
l1, l2 = node_factory.line_graph(2, fundamount=fundamt, wait_for_announce=True,
opts={'experimental-splicing': None,
'plugin': coin_mvt_plugin})
initial_wallet_balance = Millisatoshi(bkpr_account_balance(l1, 'wallet'))
initial_channel_balance = Millisatoshi(bkpr_account_balance(l1, first_channel_id(l1, l2)))
assert initial_channel_balance == Millisatoshi(fundamt * 1000)
# Splice out 100k from first channel, explicitly putting result less fees into onchain wallet
spliceamt = 100000
l1.rpc.splice(f"*:? -> {spliceamt}; 100%-fee -> wallet", debug_log=True)
p1 = only_one(l1.rpc.listpeerchannels(peer_id=l2.info['id'])['channels'])
p2 = only_one(l2.rpc.listpeerchannels(l1.info['id'])['channels'])
assert p1['inflight'][0]['splice_amount'] == -1 * spliceamt
assert p1['inflight'][0]['total_funding_msat'] == (fundamt - spliceamt) * 1000
assert p1['inflight'][0]['our_funding_msat'] == fundamt * 1000
assert p2['inflight'][0]['splice_amount'] == 0
assert p2['inflight'][0]['total_funding_msat'] == (fundamt - spliceamt) * 1000
assert p2['inflight'][0]['our_funding_msat'] == 0
bitcoind.generate_block(6, wait_for_mempool=1)
l2.daemon.wait_for_log(r'lightningd, splice_locked clearing inflights')
p1 = only_one(l1.rpc.listpeerchannels(peer_id=l2.info['id'])['channels'])
p2 = only_one(l2.rpc.listpeerchannels(l1.info['id'])['channels'])
assert p1['to_us_msat'] == (fundamt - spliceamt) * 1000
assert p1['total_msat'] == (fundamt - spliceamt) * 1000
assert p2['to_us_msat'] == 0
assert p2['total_msat'] == (fundamt - spliceamt) * 1000
assert 'inflight' not in p1
assert 'inflight' not in p2
wait_for(lambda: len(l1.rpc.listfunds()['outputs']) == 2)
wait_for(lambda: len(l1.rpc.listfunds()['channels']) == 1)
# At the end we'd expect the balance of channel 1 to be down by the splice amount
end_channel_balance = Millisatoshi(bkpr_account_balance(l1, first_channel_id(l1, l2)))
end_wallet_balance = Millisatoshi(bkpr_account_balance(l1, 'wallet'))
assert initial_channel_balance - Millisatoshi(spliceamt * 1000) == end_channel_balance
# The fee is assumed to be the difference between the start+end balances?
fee_guess = initial_wallet_balance + initial_channel_balance - end_channel_balance - end_wallet_balance
# We'd expect the following coin movements
starting_wallet_msat = 2000000000
expected_wallet_moves = [
# initial deposit
{'type': 'chain_mvt', 'credit_msat': starting_wallet_msat, 'debit_msat': 0, 'tags': ['deposit']},
# channel open spend
{'type': 'chain_mvt', 'credit_msat': 0, 'debit_msat': starting_wallet_msat, 'tags': ['withdrawal']},
# channel open change
{'type': 'chain_mvt', 'credit_msat': initial_wallet_balance, 'debit_msat': 0, 'tags': ['deposit']},
# deposit of spliceamt - fees
{'type': 'chain_mvt', 'credit_msat': Millisatoshi(spliceamt * 1000) - fee_guess, 'debit_msat': 0, 'tags': ['deposit']},
]
check_coin_moves(l1, 'wallet', expected_wallet_moves, chainparams)
expected_channel_moves = [
# channel_open [utxo created], chain_mvt (fundamt - spliceamt)
{'type': 'chain_mvt', 'credit_msat': fundamt * 1000, 'debit_msat': 0, 'tags': ['channel_open', 'opener']},
# channel_close [utxo spend], chain_mvt (fundamt)
{'type': 'chain_mvt', 'debit_msat': fundamt * 1000, 'credit_msat': 0, 'tags': ['channel_close', 'splice']},
# channel_open [utxo created], chain_mvt (fundamt - spliceamt)
{'type': 'chain_mvt', 'credit_msat': (fundamt - spliceamt) * 1000, 'debit_msat': 0, 'tags': ['channel_open', 'opener']},
]
check_coin_moves(l1, first_channel_id(l1, l2), expected_channel_moves, chainparams)
# Make sure the channel isn't marked as closed in bookkeeper
account_id = first_channel_id(l1, l2)
account_info = only_one([acct for acct in l1.rpc.bkpr_listbalances()['accounts'] if acct['account'] == account_id])
assert not account_info['account_closed']
# We'd also expect the wallet to be up by splice amt - fees
onchain_fees = [fee for fee in l1.rpc.bkpr_listincome()['income_events'] if fee['tag'] == 'onchain_fee']
assert len(onchain_fees) == 2
total_fees = sum([x['debit_msat'] for x in onchain_fees])
assert starting_wallet_msat == end_wallet_balance + total_fees + end_channel_balance
# Now close the channel and check that everything resolves as expected
l1.rpc.close(l2.info['id'])
l1.wait_for_channel_onchain(l2.info['id'])
account_info = only_one([acct for acct in l1.rpc.bkpr_listbalances()['accounts'] if acct['account'] == account_id])
assert not account_info['account_closed']
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
def test_script_splice_in(node_factory, bitcoind, chainparams):
fundamt = 1000000
coin_mvt_plugin = Path(__file__).parent / "plugins" / "coin_movements.py"
l1, l2 = node_factory.line_graph(2, fundamount=fundamt, wait_for_announce=True,
opts={'experimental-splicing': None,
'plugin': coin_mvt_plugin})
initial_wallet_balance = Millisatoshi(bkpr_account_balance(l1, 'wallet'))
initial_channel_balance = Millisatoshi(bkpr_account_balance(l1, first_channel_id(l1, l2)))
assert initial_channel_balance == Millisatoshi(fundamt * 1000)
# Splice in 100k sats into first channel, explicitly taking out 200k sats from wallet
# and letting change go automatically back to wallet (100k less onchain fees)
spliceamt = 100000
withdraw_amt = 200000
starting_wallet_msat = withdraw_amt * 10000
l1.rpc.splice(f"wallet -> {withdraw_amt}; {spliceamt} -> *:?", debug_log=True)
p1 = only_one(l1.rpc.listpeerchannels(peer_id=l2.info['id'])['channels'])
p2 = only_one(l2.rpc.listpeerchannels(l1.info['id'])['channels'])
assert p1['inflight'][0]['splice_amount'] == spliceamt
assert p1['inflight'][0]['total_funding_msat'] == (fundamt + spliceamt) * 1000
assert p1['inflight'][0]['our_funding_msat'] == fundamt * 1000
assert p2['inflight'][0]['splice_amount'] == 0
assert p2['inflight'][0]['total_funding_msat'] == (fundamt + spliceamt) * 1000
assert p2['inflight'][0]['our_funding_msat'] == 0
bitcoind.generate_block(6, wait_for_mempool=1)
l2.daemon.wait_for_log(r'lightningd, splice_locked clearing inflights')
p1 = only_one(l1.rpc.listpeerchannels(peer_id=l2.info['id'])['channels'])
p2 = only_one(l2.rpc.listpeerchannels(l1.info['id'])['channels'])
assert p1['to_us_msat'] == (fundamt + spliceamt) * 1000
assert p1['total_msat'] == (fundamt + spliceamt) * 1000
assert p2['to_us_msat'] == 0
assert p2['total_msat'] == (fundamt + spliceamt) * 1000
assert 'inflight' not in p1
assert 'inflight' not in p2
wait_for(lambda: len(l1.rpc.listfunds()['outputs']) == 1)
wait_for(lambda: len(l1.rpc.listfunds()['channels']) == 1)
# At the end we'd expect the balance of channel 1 to be up by the splice amount
end_channel_balance = Millisatoshi(bkpr_account_balance(l1, first_channel_id(l1, l2)))
end_wallet_balance = Millisatoshi(bkpr_account_balance(l1, 'wallet'))
assert initial_channel_balance + Millisatoshi(spliceamt * 1000) == end_channel_balance
# The fee is assumed to be the difference between the start+end balances?
fee_guess = initial_wallet_balance + initial_channel_balance - end_channel_balance - end_wallet_balance
# We'd expect the following coin movements
expected_wallet_moves = [
# initial deposit
{'type': 'chain_mvt', 'credit_msat': starting_wallet_msat, 'debit_msat': 0, 'tags': ['deposit']},
# channel open spend
{'type': 'chain_mvt', 'credit_msat': 0, 'debit_msat': starting_wallet_msat, 'tags': ['withdrawal']},
# channel open change
{'type': 'chain_mvt', 'credit_msat': initial_wallet_balance, 'debit_msat': 0, 'tags': ['deposit']},
# splice-in spend
{'type': 'chain_mvt', 'debit_msat': initial_wallet_balance, 'credit_msat': 0, 'tags': ['withdrawal']},
# post-splice deposit
{'type': 'chain_mvt', 'credit_msat': initial_wallet_balance - Millisatoshi(spliceamt * 1000) - fee_guess, 'debit_msat': 0, 'tags': ['deposit']},
]
check_coin_moves(l1, 'wallet', expected_wallet_moves, chainparams)
expected_channel_moves = [
# channel_open [utxo created], chain_mvt
{'type': 'chain_mvt', 'credit_msat': fundamt * 1000, 'debit_msat': 0, 'tags': ['channel_open', 'opener']},
# channel_close [utxo spend], chain_mvt (fundamt)
{'type': 'chain_mvt', 'debit_msat': fundamt * 1000, 'credit_msat': 0, 'tags': ['channel_close', 'splice']},
# channel_open [utxo created], chain_mvt (fundamt - spliceamt)
{'type': 'chain_mvt', 'credit_msat': (fundamt + spliceamt) * 1000, 'debit_msat': 0, 'tags': ['channel_open', 'opener']},
]
check_coin_moves(l1, first_channel_id(l1, l2), expected_channel_moves, chainparams)
# Make sure the channel isn't marked as closed in bookkeeper
account_id = first_channel_id(l1, l2)
account_info = only_one([acct for acct in l1.rpc.bkpr_listbalances()['accounts'] if acct['account'] == account_id])
assert not account_info['account_closed']
# We'd also expect the wallet to be down by splice amt + fees
onchain_fees = [fee for fee in l1.rpc.bkpr_listincome()['income_events'] if fee['tag'] == 'onchain_fee']
assert len(onchain_fees) == 2
total_fees = sum([x['debit_msat'] for x in onchain_fees])
assert starting_wallet_msat == end_wallet_balance + total_fees + end_channel_balance
# Now close the channel and check that everything resolves as expected
l1.rpc.close(l2.info['id'])
l1.wait_for_channel_onchain(l2.info['id'])
account_info = only_one([acct for acct in l1.rpc.bkpr_listbalances()['accounts'] if acct['account'] == account_id])
assert not account_info['account_closed']
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
def test_script_two_chan_splice_in(node_factory, bitcoind):
l1, l2, l3 = node_factory.line_graph(3, fundamount=1000000, wait_for_announce=True, opts={'experimental-splicing': None})
chan_id1 = l2.get_channel_id(l1)
chan_id2 = l2.get_channel_id(l3)
# l2 will splice funds into the channels with l1 and l3 at the same time
result = l2.rpc.splice(f"wallet -> 200000+fee; 100000 -> {chan_id1}; 100000 -> {chan_id2}")
l3.daemon.wait_for_log(r'CHANNELD_NORMAL to CHANNELD_AWAITING_SPLICE')
l2.daemon.wait_for_log(r'CHANNELD_NORMAL to CHANNELD_AWAITING_SPLICE')
l1.daemon.wait_for_log(r'CHANNELD_NORMAL to CHANNELD_AWAITING_SPLICE')
wait_for(lambda: len(list(bitcoind.rpc.getrawmempool(True).keys())) == 1)
assert result['txid'] in list(bitcoind.rpc.getrawmempool(True).keys())
bitcoind.generate_block(6, wait_for_mempool=1)
l3.daemon.wait_for_log(r'CHANNELD_AWAITING_SPLICE to CHANNELD_NORMAL')
l2.daemon.wait_for_log(r'CHANNELD_AWAITING_SPLICE to CHANNELD_NORMAL')
l1.daemon.wait_for_log(r'CHANNELD_AWAITING_SPLICE to CHANNELD_NORMAL')
# l2 should now have funds on their side to pay l1
inv = l1.rpc.invoice(10000, '1', 'no_1')
l2.rpc.pay(inv['bolt11'])
# l2 spliced extra funds into chan with l3 (but l3 still has 0 on their side)
# Send a payment l2->l3 just to check for channel stability
inv = l3.rpc.invoice(10000, '2', 'no_2')
l2.rpc.pay(inv['bolt11'])
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
def test_script_two_chan_splice_out(node_factory, bitcoind):
l1, l2, l3 = node_factory.line_graph(3, fundamount=1000000, wait_for_announce=True, opts={'experimental-splicing': None})
# We need to get funds into l1 -> l2 channel so we can splice it out
inv = l2.rpc.invoice(100000000, '1', 'no_1')
l1.rpc.pay(inv['bolt11'])
chan_id1 = l2.get_channel_id(l1)
chan_id2 = l2.get_channel_id(l3)
# l2 will splice funds out of the channels with l1 and l3 at the same time
# By default extra funds get sent to wallet
result = l2.rpc.splice(f"{chan_id1} -> 100000; {chan_id2} -> 100000")
l3.daemon.wait_for_log(r'CHANNELD_NORMAL to CHANNELD_AWAITING_SPLICE')
l2.daemon.wait_for_log(r'CHANNELD_NORMAL to CHANNELD_AWAITING_SPLICE')
l1.daemon.wait_for_log(r'CHANNELD_NORMAL to CHANNELD_AWAITING_SPLICE')
wait_for(lambda: len(list(bitcoind.rpc.getrawmempool(True).keys())) == 1)
assert result['txid'] in list(bitcoind.rpc.getrawmempool(True).keys())
bitcoind.generate_block(6, wait_for_mempool=1)
l3.daemon.wait_for_log(r'CHANNELD_AWAITING_SPLICE to CHANNELD_NORMAL')
l2.daemon.wait_for_log(r'CHANNELD_AWAITING_SPLICE to CHANNELD_NORMAL')
l1.daemon.wait_for_log(r'CHANNELD_AWAITING_SPLICE to CHANNELD_NORMAL')
# no extra funds in channels but do some simple payments to test stability
inv = l2.rpc.invoice(10000, '2', 'no_2')
l1.rpc.pay(inv['bolt11'])
inv = l3.rpc.invoice(10000, '3', 'no_3')
l2.rpc.pay(inv['bolt11'])
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
def test_script_two_chan_splice_inout(node_factory, bitcoind):
l1, l2, l3 = node_factory.line_graph(3, fundamount=1000000, wait_for_announce=True, opts={'experimental-splicing': None})
chan_id1 = l2.get_channel_id(l1)
chan_id2 = l2.get_channel_id(l3)
# move sats from chan 2 into chan 1
# By adding 10000 from wallet, the fee will be taken from this and the
# extra placed back into the wallet by default
result = l2.rpc.splice(f"wallet -> 10000; 100000 -> {chan_id1}; {chan_id2} -> 100000")
l3.daemon.wait_for_log(r'CHANNELD_NORMAL to CHANNELD_AWAITING_SPLICE')
l2.daemon.wait_for_log(r'CHANNELD_NORMAL to CHANNELD_AWAITING_SPLICE')
l1.daemon.wait_for_log(r'CHANNELD_NORMAL to CHANNELD_AWAITING_SPLICE')
wait_for(lambda: len(list(bitcoind.rpc.getrawmempool(True).keys())) == 1)
assert result['txid'] in list(bitcoind.rpc.getrawmempool(True).keys())
bitcoind.generate_block(6, wait_for_mempool=1)
l3.daemon.wait_for_log(r'CHANNELD_AWAITING_SPLICE to CHANNELD_NORMAL')
l2.daemon.wait_for_log(r'CHANNELD_AWAITING_SPLICE to CHANNELD_NORMAL')
l1.daemon.wait_for_log(r'CHANNELD_AWAITING_SPLICE to CHANNELD_NORMAL')
# l2 should now have funds on their side to pay l1
inv = l1.rpc.invoice(10000, '2', 'no_2')
l2.rpc.pay(inv['bolt11'])
# l2 spliced extra funds into chan with l3 (but l3 still has 0 on their side)
# Send a payment l2->l3 just to check for channel stability
inv = l3.rpc.invoice(10000, '3', 'no_3')
l2.rpc.pay(inv['bolt11'])
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
def test_easy_splice_in(node_factory, bitcoind, chainparams):
fundamt = 1000000
coin_mvt_plugin = Path(__file__).parent / "plugins" / "coin_movements.py"
l1, l2 = node_factory.line_graph(2, fundamount=fundamt, wait_for_announce=True,
opts={'experimental-splicing': None,
'plugin': coin_mvt_plugin})
# Splice in 100k sats into first channel
spliceamt = 100000
l1.rpc.splicein("*:?", f"{spliceamt}")
p1 = only_one(l1.rpc.listpeerchannels(peer_id=l2.info['id'])['channels'])
p2 = only_one(l2.rpc.listpeerchannels(l1.info['id'])['channels'])
assert p1['inflight'][0]['splice_amount'] == spliceamt
assert p1['inflight'][0]['total_funding_msat'] == (fundamt + spliceamt) * 1000
assert p1['inflight'][0]['our_funding_msat'] == fundamt * 1000
assert p2['inflight'][0]['splice_amount'] == 0
assert p2['inflight'][0]['total_funding_msat'] == (fundamt + spliceamt) * 1000
assert p2['inflight'][0]['our_funding_msat'] == 0
bitcoind.generate_block(6, wait_for_mempool=1)
l2.daemon.wait_for_log(r'lightningd, splice_locked clearing inflights')
p1 = only_one(l1.rpc.listpeerchannels(peer_id=l2.info['id'])['channels'])
p2 = only_one(l2.rpc.listpeerchannels(l1.info['id'])['channels'])
assert p1['to_us_msat'] == (fundamt + spliceamt) * 1000
assert p1['total_msat'] == (fundamt + spliceamt) * 1000
assert p2['to_us_msat'] == 0
assert p2['total_msat'] == (fundamt + spliceamt) * 1000
assert 'inflight' not in p1
assert 'inflight' not in p2
wait_for(lambda: len(l1.rpc.listfunds()['outputs']) == 1)
wait_for(lambda: len(l1.rpc.listfunds()['channels']) == 1)
# Makes channels going from node 1 -> 2, 2 -> 3, etc up to 'qty' number channels.
# If balanced is True, than each channel will be balanced -- otherwise the lower
# index channel will have funds in the channel to the higher indexed one.
#
# The channels for the second node are returned in chanids
def make_chans(node_factory, qty=2, fundamount=1000000, balanced=True):
nodes = node_factory.line_graph(qty + 1, fundamount=fundamount, opts={'experimental-splicing': None, 'allow_bad_gossip': True})
chanids = []
for i in range(len(nodes) - 1):
nodes[i].daemon.wait_for_log(' to CHANNELD_NORMAL')
if balanced:
inv = nodes[i + 1].rpc.invoice(1000 * fundamount // 2, 'balance', 'balance')
nodes[i].rpc.pay(inv['bolt11'])
chanids.insert(0, nodes[1].get_channel_id(nodes[0]))
if qty > 1:
chanids.insert(0, nodes[1].get_channel_id(nodes[2]))
return [nodes, chanids]
def verify_chans(nodes, bitcoind, txid, chanids, fee, expected_balances=None, fee_multiplier=None):
for node in nodes:
node.daemon.wait_for_log(r'CHANNELD_NORMAL to CHANNELD_AWAITING_SPLICE')
wait_for(lambda: len(list(bitcoind.rpc.getrawmempool(True).keys())) == 1)
bitcoind.generate_block(6, wait_for_mempool=1)
for chanid in chanids:
nodes[1].daemon.wait_for_log(r'CHANNELD_AWAITING_SPLICE to CHANNELD_NORMAL')
if not expected_balances:
return
# Modify expected_balances by fee_multipler and fee
extra_funds = 0
last_index = 0
if fee_multiplier:
for index, multiplier in enumerate(fee_multiplier):
amount, remainder = divmod(multiplier * fee, 1)
expected_balances[index] += int(amount)
extra_funds += remainder
if multiplier != 0:
last_index = index
# If we have extra funds, put them all in the last item
if extra_funds > 0:
expected_balances[last_index] += int(extra_funds)
channel_funds = nodes[1].rpc.listfunds()['channels']
channel_funds.sort(key=lambda info: chanids.index(info['channel_id']))
funds = [info['our_amount_msat'] // 1000 for info in channel_funds]
assert funds == expected_balances
# * expected_balances is an array of sat values
# * fee_multiplier is an array of ints. The coorisponding channel balance expectation is adjusted
# by the fee * fee_multiplier. For example [0, -1] reduces the second channels expected balance
# by the fee value
def execute_script(node_factory, bitcoind, script, expected_balances=None, fee_multiplier=None):
nodes, chanids = make_chans(node_factory, script.count("{}"))
result = nodes[1].rpc.splice(script.format(*chanids), debug_log=True)
# Extract the fee that splice scripts thinks we're using from the logs
nodes[1].daemon.wait_for_log(r'calc_in_ppm_and_fee starting calculations FINALIZING PASS')
# It can either be "fee x" or "x fee" so we match on both
logline = nodes[1].daemon.wait_for_log(r'(fee [0-9]+sat)|([0-9]+sat fee)')
# Try both regex versions
reg_res = re.search(r'fee ([0-9]+)sat', logline) or re.search(r'([0-9]+)sat fee', logline)
# now we should know the fee
fee = reg_res.group(1)
verify_chans(nodes, bitcoind, result['txid'], chanids, int(fee), expected_balances, fee_multiplier)
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
def test_script_two_chan_splice_b(node_factory, bitcoind):
execute_script(node_factory, bitcoind, "wallet -> 10000; {} -> 100000; {} -> 100000",
[500000 - 100000, 500000 - 100000])
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
def test_script_two_chan_splice_c(node_factory, bitcoind):
execute_script(node_factory, bitcoind, "wallet -> 10000; 100000 -> {}; {} -> 100000",
[500000 + 100000, 500000 - 100000])
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
def test_script_two_chan_splice_d(node_factory, bitcoind):
execute_script(node_factory, bitcoind, "wallet -> 250000; 100000 -> {}; 100000 -> {}",
[500000 + 100000, 500000 + 100000])
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
def test_script_two_chan_splice_e(node_factory, bitcoind):
execute_script(node_factory, bitcoind, "{} -> 100000; {} -> 100000",
[500000 - 100000, 500000 - 100000])
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
def test_script_two_chan_splice_f(node_factory, bitcoind):
execute_script(node_factory, bitcoind, "{} -> 200000; 100000 -> {}",
[500000 - 200000, 500000 + 100000])
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
def test_script_two_chan_splice_g(node_factory, bitcoind):
execute_script(node_factory, bitcoind, "{} -> 200000; 100000 -> {}; * -> wallet",
[500000 - 200000, 500000 + 100000])
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
def test_script_two_chan_splice_h(node_factory, bitcoind):
execute_script(node_factory, bitcoind, "wallet -> 200000+fee; 100000 -> {}; 100000 -> {}",
[500000 + 100000, 500000 + 100000])
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
def test_script_two_chan_splice_ii(node_factory, bitcoind):
execute_script(node_factory, bitcoind, "100000 -> {}; {} -> 100000+fee",
[500000 + 100000, 500000 - 100000], [0, -1])
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
def test_script_two_chan_splice_j(node_factory, bitcoind):
execute_script(node_factory, bitcoind, "100000-fee -> {}; {} -> 100000",
[500000 + 100000, 500000 - 100000], [-1, 0])
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
def test_script_two_chan_splice_k(node_factory, bitcoind):
execute_script(node_factory, bitcoind, "{} -> 10000; 1000 -> {}",
[500000 - 10000, 500000 + 1000])
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
def test_script_two_chan_splice_l(node_factory, bitcoind):
execute_script(node_factory, bitcoind, "wallet -> 100000; * -> {}; * -> {}",
[500000 + 50000, 500000 + 50000], [-0.5, -0.5])
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
def test_script_two_chan_splice_m(node_factory, bitcoind):
execute_script(node_factory, bitcoind, "wallet -> *+fee; 100000 -> {}; 100000 -> {}",
[500000 + 100000, 500000 + 100000])
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
def test_script_two_chan_splice_n(node_factory, bitcoind):
execute_script(node_factory, bitcoind, "wallet -> 100%+fee; {} -> 50%; 100000 -> {}",
[500000 // 2, 500000 + 100000])
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
def test_script_two_chan_splice_oo(node_factory, bitcoind):
execute_script(node_factory, bitcoind, "wallet -> *+fee; {} -> 50%; 100000 -> {}",
[500000 // 2, 500000 + 100000])
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
def test_script_two_chan_splice_p(node_factory, bitcoind):
execute_script(node_factory, bitcoind, "wallet -> *; {} -> 50%+fee; 100000 -> {}",
[500000 // 2, 500000 + 100000], [-1, 0])
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
def test_script_two_chan_splice_q(node_factory, bitcoind):
execute_script(node_factory, bitcoind, "wallet -> *; {} -> 50000+fee; 100000 -> {}",
[500000 - 50000, 500000 + 100000], [-1, 0])
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
def test_script_two_chan_splice_r(node_factory, bitcoind):
execute_script(node_factory, bitcoind, "wallet -> 0+fee; {} -> 100000; 100000 -> {}",
[500000 - 100000, 500000 + 100000])
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
def test_script_two_chan_splice_s(node_factory, bitcoind):
execute_script(node_factory, bitcoind, "wallet -> 50000; {} -> 50000+fee; 100000 -> {}",
[500000 - 50000, 500000 + 100000], [-1, 0])
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
def test_script_two_chan_splice_t(node_factory, bitcoind):
execute_script(node_factory, bitcoind, "wallet -> 100%; {} -> 50000+fee; 100000 -> {}",
[500000 - 50000, 500000 + 100000], [-1, 0])
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
def test_script_two_chan_splice_u(node_factory, bitcoind):
execute_script(node_factory, bitcoind, "wallet -> 100%; {} -> 50000; 100000 -> {}",
[500000 - 50000, 500000 + 100000])
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
def test_script_two_chan_splice_v(node_factory, bitcoind):
execute_script(node_factory, bitcoind, "wallet -> 100%; {} -> 100000; 100000 -> {}",
[500000 - 100000, 500000 + 100000])
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
def test_script_two_chan_splice_x(node_factory, bitcoind):
execute_script(node_factory, bitcoind, "* -> wallet; * -> {}; {} -> 100000",
[500000 + 50000, 500000 - 100000], [-0.5, 0])
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
def test_script_two_chan_splice_y(node_factory, bitcoind):
execute_script(node_factory, bitcoind, "wallet -> *; 100000 -> {}; 100000 -> {}",
[500000 + 100000, 500000 + 100000])
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
def test_script_two_chan_splice_z(node_factory, bitcoind):
execute_script(node_factory, bitcoind, "wallet -> 100000; 70% -> {}; 30% -> {}",
[500000 + int(100000 * 0.7), 500000 + int(100000 * 0.3)], [-0.7, -0.3])
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
def test_easy_splice_out(node_factory, bitcoind, chainparams):
fundamt = 1000000
coin_mvt_plugin = Path(__file__).parent / "plugins" / "coin_movements.py"
l1, l2 = node_factory.line_graph(2, fundamount=fundamt, wait_for_announce=True,
opts={'experimental-splicing': None,
'plugin': coin_mvt_plugin})
initial_wallet_balance = Millisatoshi(bkpr_account_balance(l1, 'wallet'))
# Splice out 100k from first channel, putting result less fees into onchain wallet
spliceamt = 100000
l1.rpc.spliceout("*:?", f"{spliceamt}", force_feerate=True)
bitcoind.generate_block(6, wait_for_mempool=1)
l2.daemon.wait_for_log(r'lightningd, splice_locked clearing inflights')
p1 = only_one(l1.rpc.listpeerchannels(peer_id=l2.info['id'])['channels'])
p2 = only_one(l2.rpc.listpeerchannels(l1.info['id'])['channels'])
assert 'inflight' not in p1
assert 'inflight' not in p2
wait_for(lambda: len(l1.rpc.listfunds()['outputs']) == 2)
wait_for(lambda: len(l1.rpc.listfunds()['channels']) == 1)
end_wallet_balance = Millisatoshi(bkpr_account_balance(l1, 'wallet'))
assert initial_wallet_balance + Millisatoshi(spliceamt * 1000) == end_wallet_balance
@pytest.mark.xfail(strict=True)
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
def test_easy_splice_out_address(node_factory, bitcoind, chainparams):
fundamt = 1000000
l1, l2 = node_factory.line_graph(2, fundamount=fundamt, wait_for_announce=True,
opts={'experimental-splicing': None})
initial_wallet_balance = Millisatoshi(bkpr_account_balance(l1, 'wallet'))
addr = l1.rpc.newaddr()['p2tr']
# Splice out 100k from first channel, putting result less fees into onchain wallet via addres
spliceamt = 100000
l1.rpc.spliceout("*:?", f"{spliceamt}", destination=addr, force_feerate=True)
bitcoind.generate_block(6, wait_for_mempool=1)
l2.daemon.wait_for_log(r'lightningd, splice_locked clearing inflights')
p1 = only_one(l1.rpc.listpeerchannels(peer_id=l2.info['id'])['channels'])
p2 = only_one(l2.rpc.listpeerchannels(l1.info['id'])['channels'])
assert 'inflight' not in p1
assert 'inflight' not in p2
wait_for(lambda: len(l1.rpc.listfunds()['outputs']) == 2)
wait_for(lambda: len(l1.rpc.listfunds()['channels']) == 1)
end_wallet_balance = Millisatoshi(bkpr_account_balance(l1, 'wallet'))
assert initial_wallet_balance + Millisatoshi(spliceamt * 1000) == end_wallet_balance
@pytest.mark.openchannel('v1')
@pytest.mark.openchannel('v2')
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
def test_easy_splice_out_into_channel(node_factory, bitcoind, chainparams):
fundamt = 1000000
l1, l2, l3 = node_factory.line_graph(3, fundamount=fundamt, wait_for_announce=True,
opts={'experimental-splicing': None})
chan1 = first_channel_id(l1, l2)
chan2 = first_channel_id(l2, l3)
initial_chan1_balance = Millisatoshi(bkpr_account_balance(l2, chan1))
assert initial_chan1_balance == 0
# Splice out 100k from first channel, putting result into channel
spliceamt = 100000
l2.rpc.spliceout(f"{chan2}", f"{spliceamt}", destination=chan1, force_feerate=True)
bitcoind.generate_block(6, wait_for_mempool=1)
l2.daemon.wait_for_log(r'lightningd, splice_locked clearing inflights')
p1 = only_one(l1.rpc.listpeerchannels()['channels'])
p2 = only_one(l3.rpc.listpeerchannels()['channels'])
assert 'inflight' not in p1
assert 'inflight' not in p2
wait_for(lambda: len(l2.rpc.listfunds()['outputs']) == 1)
wait_for(lambda: len(l2.rpc.listfunds()['channels']) == 2)
end_chan1_balance = Millisatoshi(bkpr_account_balance(l2, chan1))
assert initial_chan1_balance + Millisatoshi(spliceamt * 1000) == end_chan1_balance