forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel_control.c
2264 lines (1952 loc) · 65.7 KB
/
channel_control.c
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
#include "config.h"
#include <ccan/cast/cast.h>
#include <ccan/mem/mem.h>
#include <ccan/tal/str/str.h>
#include <channeld/channeld_wiregen.h>
#include <common/json_command.h>
#include <common/json_param.h>
#include <common/json_stream.h>
#include <common/memleak.h>
#include <common/shutdown_scriptpubkey.h>
#include <common/type_to_string.h>
#include <common/wire_error.h>
#include <connectd/connectd_wiregen.h>
#include <errno.h>
#include <fcntl.h>
#include <hsmd/permissions.h>
#include <lightningd/chaintopology.h>
#include <lightningd/channel.h>
#include <lightningd/channel_control.h>
#include <lightningd/channel_gossip.h>
#include <lightningd/closing_control.h>
#include <lightningd/coin_mvts.h>
#include <lightningd/connect_control.h>
#include <lightningd/dual_open_control.h>
#include <lightningd/gossip_control.h>
#include <lightningd/hsm_control.h>
#include <lightningd/notification.h>
#include <lightningd/peer_control.h>
#include <lightningd/peer_fd.h>
#include <lightningd/peer_htlcs.h>
#include <wally_bip32.h>
#include <wally_psbt.h>
struct splice_command {
/* Inside struct lightningd splice_commands. */
struct list_node list;
/* Command structure. This is the parent of the splice command. */
struct command *cmd;
/* Channel being spliced. */
struct channel *channel;
};
void channel_update_feerates(struct lightningd *ld, const struct channel *channel)
{
u8 *msg;
u32 min_feerate, max_feerate;
bool anchors = channel_type_has_anchors(channel->type);
u32 feerate = unilateral_feerate(ld->topology, anchors);
/* Nothing to do if we don't know feerate. */
if (!feerate)
return;
/* For anchors, we just need the commitment tx to relay. */
if (anchors)
min_feerate = get_feerate_floor(ld->topology);
else
min_feerate = feerate_min(ld, NULL);
max_feerate = feerate_max(ld, NULL);
/* The channel opener should use a slightly higher than minimal feerate
* in order to avoid excessive feerate disagreements */
if (channel->opener == LOCAL) {
feerate += ld->config.feerate_offset;
if (feerate > max_feerate)
feerate = max_feerate;
}
if (channel->ignore_fee_limits || ld->config.ignore_fee_limits) {
min_feerate = 1;
max_feerate = 0xFFFFFFFF;
}
log_debug(ld->log,
"update_feerates: feerate = %u, min=%u, max=%u, penalty=%u",
feerate,
min_feerate,
feerate_max(ld, NULL),
penalty_feerate(ld->topology));
msg = towire_channeld_feerates(NULL, feerate,
min_feerate,
max_feerate,
penalty_feerate(ld->topology));
subd_send_msg(channel->owner, take(msg));
}
static void try_update_feerates(struct lightningd *ld, struct channel *channel)
{
/* No point until funding locked in */
if (!channel_state_fees_can_change(channel->state))
return;
/* Can't if no daemon listening. */
if (!channel->owner)
return;
/* The feerate message is only understood by `channeld` so
* don't attempt to send it to other subds*/
if (!streq(channel->owner->name, "channeld"))
return;
channel_update_feerates(ld, channel);
}
static void try_update_blockheight(struct lightningd *ld,
struct channel *channel,
u32 blockheight)
{
u8 *msg;
/* We don't update the blockheight for non-leased chans */
if (channel->lease_expiry == 0)
return;
log_debug(channel->log, "attempting update blockheight %s",
type_to_string(tmpctx, struct channel_id, &channel->cid));
if (!topology_synced(ld->topology)) {
log_debug(channel->log, "chain not synced,"
" not updating blockheight");
return;
}
/* If they're offline, check that we're not too far behind anyway */
if (!channel->owner) {
if (channel->opener == REMOTE) {
u32 peer_height
= get_blockheight(channel->blockheight_states,
channel->opener, REMOTE);
/* Lease no longer active, we don't (really) care */
if (peer_height >= channel->lease_expiry)
return;
assert(peer_height + 1008 > peer_height);
if (peer_height + 1008 < blockheight)
channel_fail_permanent(channel,
REASON_PROTOCOL,
"Offline peer is too"
" far behind,"
" terminating leased"
" channel. Our current"
" %u, theirs %u",
blockheight,
peer_height);
}
return;
}
/* If we're not opened/locked in yet, don't send update */
if (!channel_state_can_add_htlc(channel->state))
return;
log_debug(ld->log, "update_blockheight: height = %u", blockheight);
msg = towire_channeld_blockheight(NULL, blockheight);
subd_send_msg(channel->owner, take(msg));
}
void notify_feerate_change(struct lightningd *ld)
{
struct peer *peer;
struct peer_node_id_map_iter it;
for (peer = peer_node_id_map_first(ld->peers, &it);
peer;
peer = peer_node_id_map_next(ld->peers, &it)) {
struct channel *channel;
list_for_each(&peer->channels, channel, list)
try_update_feerates(ld, channel);
}
/* FIXME: We choose not to drop to chain if we can't contact
* peer. We *could* do so, however. */
}
static struct splice_command *splice_command_for_chan(struct lightningd *ld,
struct channel *channel)
{
struct splice_command *cc;
list_for_each(&ld->splice_commands, cc, list)
if (channel == cc->channel)
return cc;
return NULL;
}
static void handle_splice_funding_error(struct lightningd *ld,
struct channel *channel,
const u8 *msg)
{
struct splice_command *cc;
struct amount_msat funding, req_funding;
bool opener_error;
if (!fromwire_channeld_splice_funding_error(msg, &funding,
&req_funding,
&opener_error)) {
channel_internal_error(channel,
"bad channeld_splice_feerate_error %s",
tal_hex(channel, msg));
return;
}
cc = splice_command_for_chan(ld, channel);
if (cc) {
was_pending(command_fail(cc->cmd, SPLICE_FUNDING_LOW,
"%s provided %s but committed to %s.",
opener_error ? "You" : "Peer",
fmt_amount_msat(tmpctx, funding),
fmt_amount_msat(tmpctx, req_funding)));
}
else {
log_peer_unusual(ld->log, &channel->peer->id,
"Splice funding too low. %s provided but %s"
" commited to %s",
opener_error ? "peer" : "you",
fmt_amount_msat(tmpctx, funding),
fmt_amount_msat(tmpctx, req_funding));
}
}
static void handle_splice_state_error(struct lightningd *ld,
struct channel *channel,
const u8 *msg)
{
struct splice_command *cc;
char *error_msg;
if (!fromwire_channeld_splice_state_error(tmpctx, msg, &error_msg)) {
channel_internal_error(channel,
"bad channeld_splice_state_error %s",
tal_hex(channel, msg));
return;
}
cc = splice_command_for_chan(ld, channel);
if (cc)
was_pending(command_fail(cc->cmd, SPLICE_STATE_ERROR,
"%s", error_msg));
else
log_peer_unusual(ld->log, &channel->peer->id,
"Splice state error: %s", error_msg);
}
static void handle_splice_feerate_error(struct lightningd *ld,
struct channel *channel,
const u8 *msg)
{
struct splice_command *cc;
struct amount_msat fee;
bool too_high;
char *error_msg;
if (!fromwire_channeld_splice_feerate_error(msg, &fee, &too_high)) {
channel_internal_error(channel,
"bad fromwire_channeld_splice_feerate_error %s",
tal_hex(channel, msg));
return;
}
cc = splice_command_for_chan(ld, channel);
if (cc) {
if (too_high)
error_msg = tal_fmt(tmpctx, "Feerate too high. Do you "
"really want to spend %s on fees?",
fmt_amount_msat(tmpctx, fee));
else
error_msg = tal_fmt(tmpctx, "Feerate too low. Your "
"funding only provided %s in fees",
fmt_amount_msat(tmpctx, fee));
was_pending(command_fail(cc->cmd,
too_high ? SPLICE_HIGH_FEE : SPLICE_LOW_FEE,
"%s", error_msg));
}
else {
log_peer_unusual(ld->log, &channel->peer->id, "Peer gave us a"
" splice pkg with too low of feerate (fee was"
" %s), we rejected it.",
fmt_amount_msat(tmpctx, fee));
}
}
/* When channeld finishes processing the `splice_init` command, this is called */
static void handle_splice_confirmed_init(struct lightningd *ld,
struct channel *channel,
const u8 *msg)
{
struct splice_command *cc;
struct wally_psbt *psbt;
if (!fromwire_channeld_splice_confirmed_init(tmpctx, msg, &psbt)) {
channel_internal_error(channel,
"bad splice_confirmed_init %s",
tal_hex(channel, msg));
return;
}
cc = splice_command_for_chan(ld, channel);
if (!cc) {
channel_internal_error(channel, "splice_confirmed_init"
" received without an active command %s",
tal_hex(channel, msg));
return;
}
struct json_stream *response = json_stream_success(cc->cmd);
json_add_string(response, "psbt", psbt_to_b64(tmpctx, psbt));
was_pending(command_success(cc->cmd, response));
}
/* Channeld sends us this in response to a user's `splice_update` request */
static void handle_splice_confirmed_update(struct lightningd *ld,
struct channel *channel,
const u8 *msg)
{
struct splice_command *cc;
struct wally_psbt *psbt;
bool commitments_secured;
if (!fromwire_channeld_splice_confirmed_update(tmpctx,
msg,
&psbt,
&commitments_secured)) {
channel_internal_error(channel,
"bad splice_confirmed_update %s",
tal_hex(channel, msg));
return;
}
cc = splice_command_for_chan(ld, channel);
if (!cc) {
channel_internal_error(channel, "splice_update_confirmed"
" received without an active command %s",
tal_hex(channel, msg));
return;
}
struct json_stream *response = json_stream_success(cc->cmd);
json_add_string(response, "psbt", psbt_to_b64(tmpctx, psbt));
json_add_bool(response, "commitments_secured", commitments_secured);
was_pending(command_success(cc->cmd, response));
}
/* Channeld uses this to request the funding transaction for help building the
* splice tx */
static void handle_splice_lookup_tx(struct lightningd *ld,
struct channel *channel,
const u8 *msg)
{
struct bitcoin_txid txid;
struct bitcoin_tx *tx;
u8 *outmsg;
if (!fromwire_channeld_splice_lookup_tx(msg, &txid)) {
channel_internal_error(channel,
"bad splice_lookup_tx %s",
tal_hex(channel, msg));
return;
}
tx = wallet_transaction_get(tmpctx, ld->wallet, &txid);
if (!tx) {
channel_internal_error(channel,
"channel control unable to find txid %s",
type_to_string(tmpctx,
struct bitcoin_txid,
&txid));
return;
}
outmsg = towire_channeld_splice_lookup_tx_result(NULL, tx);
subd_send_msg(channel->owner, take(outmsg));
}
/* Extra splice data we want to store for bitcoin send tx interface */
struct send_splice_info
{
struct splice_command *cc;
struct channel *channel;
const struct bitcoin_tx *final_tx;
u32 output_index;
const char *err_msg;
};
static void handle_tx_broadcast(struct send_splice_info *info)
{
struct lightningd *ld = info->channel->peer->ld;
struct amount_sat unused;
struct json_stream *response;
struct bitcoin_txid txid;
u8 *tx_bytes;
int num_utxos;
tx_bytes = linearize_tx(tmpctx, info->final_tx);
bitcoin_txid(info->final_tx, &txid);
/* This might have spent UTXOs from our wallet */
num_utxos = wallet_extract_owned_outputs(ld->wallet,
info->final_tx->wtx, false,
NULL, &unused);
if (num_utxos)
wallet_transaction_add(ld->wallet, info->final_tx->wtx, 0, 0);
if (info->cc) {
response = json_stream_success(info->cc->cmd);
json_add_hex(response, "tx", tx_bytes, tal_bytelen(tx_bytes));
json_add_txid(response, "txid", &txid);
was_pending(command_success(info->cc->cmd, response));
}
}
/* Succeeds if the utxo was found in the mempool or in the utxo set. If it's in
* a block and spent it will fail but we're okay with that here. */
static void check_utxo_block(struct bitcoind *bitcoind UNUSED,
const struct bitcoin_tx_output *txout,
void *arg)
{
struct send_splice_info *info = arg;
if(!txout) {
if (info->cc)
was_pending(command_fail(info->cc->cmd,
SPLICE_BROADCAST_FAIL,
"Error broadcasting splice "
"tx: %s. Unsent tx discarded "
"%s.",
info->err_msg,
type_to_string(tmpctx,
struct wally_tx,
info->final_tx->wtx)));
log_unusual(info->channel->log,
"Error broadcasting splice "
"tx: %s. Unsent tx discarded "
"%s.",
info->err_msg,
type_to_string(tmpctx, struct wally_tx,
info->final_tx->wtx));
}
else
handle_tx_broadcast(info);
tal_free(info);
}
/* Callback for after the splice tx is sent to bitcoind */
static void send_splice_tx_done(struct bitcoind *bitcoind UNUSED,
bool success, const char *msg,
struct send_splice_info *info)
{
/* A NULL value of `info->cc` means we got here without user intiation.
* This means we are the ACCEPTER side of the splice */
struct lightningd *ld = info->channel->peer->ld;
struct bitcoin_outpoint outpoint;
bitcoin_txid(info->final_tx, &outpoint.txid);
outpoint.n = info->output_index;
if (!success) {
info->err_msg = tal_strdup(info, msg);
bitcoind_getutxout(ld->topology->bitcoind, &outpoint,
check_utxo_block, info);
} else {
handle_tx_broadcast(info);
tal_free(info);
}
}
/* Where the splice tx gets finally transmitted to the chain */
static void send_splice_tx(struct channel *channel,
const struct bitcoin_tx *tx,
struct splice_command *cc,
u32 output_index)
{
struct lightningd *ld = channel->peer->ld;
u8* tx_bytes = linearize_tx(tmpctx, tx);
log_debug(channel->log,
"Broadcasting splice tx %s for channel %s.",
tal_hex(tmpctx, tx_bytes),
type_to_string(tmpctx, struct channel_id, &channel->cid));
struct send_splice_info *info = tal(NULL, struct send_splice_info);
info->cc = tal_steal(info, cc);
info->channel = channel;
info->final_tx = tal_steal(info, tx);
info->output_index = output_index;
info->err_msg = NULL;
bitcoind_sendrawtx(ld->topology->bitcoind,
ld->topology->bitcoind,
cc ? cc->cmd->id : NULL,
tal_hex(tmpctx, tx_bytes),
false,
send_splice_tx_done, info);
}
/* After channeld have all the signatures it sends the result to us here */
static void handle_splice_confirmed_signed(struct lightningd *ld,
struct channel *channel,
const u8 *msg)
{
struct splice_command *cc;
struct bitcoin_tx *tx;
struct bitcoin_txid txid;
struct channel_inflight *inflight;
u32 output_index;
if (!fromwire_channeld_splice_confirmed_signed(tmpctx, msg, &tx,
&output_index)) {
channel_internal_error(channel,
"bad splice_confirmed_signed %s",
tal_hex(channel, msg));
return;
}
bitcoin_txid(tx, &txid);
inflight = channel_inflight_find(channel, &txid);
if (!inflight)
channel_internal_error(channel, "Unable to load inflight for"
" splice_confirmed_signed txid %s",
type_to_string(tmpctx,
struct bitcoin_txid,
&txid));
inflight->remote_tx_sigs = true;
wallet_inflight_save(ld->wallet, inflight);
if (channel->state != CHANNELD_AWAITING_SPLICE) {
log_debug(channel->log,
"Would broadcast splice, but state %s"
" isn't CHANNELD_AWAITING_SPLICE",
channel_state_name(channel));
return;
}
cc = splice_command_for_chan(ld, channel);
send_splice_tx(channel, tx, cc, output_index);
}
static enum watch_result splice_depth_cb(struct lightningd *ld,
const struct bitcoin_txid *txid,
const struct bitcoin_tx *tx,
unsigned int depth,
void *param)
{
/* find_txwatch triggers a type warning on inflight, so we do this. */
struct channel_inflight *inflight = param;
struct txlocator *loc;
struct short_channel_id scid;
/* What scid is this giving us? */
loc = wallet_transaction_locate(tmpctx, ld->wallet, txid);
if (!mk_short_channel_id(&scid,
loc->blkheight, loc->index,
inflight->funding->outpoint.n)) {
channel_fail_permanent(inflight->channel,
REASON_LOCAL,
"Invalid funding scid %u:%u:%u",
loc->blkheight, loc->index,
inflight->funding->outpoint.n);
return false;
}
/* Usually, we're here because we're awaiting a splice, but
* we could also mutual shutdown, or that weird splice_locked_memonly
* hack... */
if (inflight->channel->state != CHANNELD_AWAITING_SPLICE) {
log_info(inflight->channel->log, "Splice inflight event but not"
" in AWAITING_SPLICE, ending watch of txid %s",
type_to_string(tmpctx, struct bitcoin_txid, txid));
return DELETE_WATCH;
}
/* Reorged out? OK, we're not committed yet. */
if (depth == 0) {
return KEEP_WATCHING;
}
if (inflight->channel->owner) {
log_info(inflight->channel->log, "splice_depth_cb: sending funding depth scid: %s",
type_to_string(tmpctx, struct short_channel_id, &scid));
subd_send_msg(inflight->channel->owner,
take(towire_channeld_funding_depth(
NULL, &scid,
depth, true, txid)));
}
/* channeld will tell us when splice is locked in: we'll clean
* this watch up then. */
return KEEP_WATCHING;
}
void watch_splice_inflight(struct lightningd *ld,
struct channel_inflight *inflight)
{
log_info(inflight->channel->log, "Watching splice inflight %s",
type_to_string(tmpctx, struct bitcoin_txid,
&inflight->funding->outpoint.txid));
watch_txid(inflight, ld->topology,
&inflight->funding->outpoint.txid,
splice_depth_cb, inflight);
}
static struct txwatch *splice_inflight_txwatch(struct channel *channel,
struct channel_inflight *inflight)
{
return find_txwatch(channel->peer->ld->topology,
&inflight->funding->outpoint.txid,
splice_depth_cb, channel);
}
static void handle_splice_sending_sigs(struct lightningd *ld,
struct channel *channel,
const u8 *msg)
{
struct splice_command *cc;
struct bitcoin_txid txid;
struct channel_inflight *inflight;
if (!fromwire_channeld_splice_sending_sigs(msg, &txid)) {
channel_internal_error(channel,
"bad splice_confirmed_signed %s",
tal_hex(channel, msg));
return;
}
inflight = channel_inflight_find(channel, &txid);
if (!inflight)
channel_internal_error(channel, "Unable to load inflight for"
" splice_confirmed_signed txid %s",
type_to_string(tmpctx,
struct bitcoin_txid,
&txid));
/* Signing a splice after it has confirmed is safe and can happen during
* reestablish if one node is late seeing blocks */
if (channel->state == CHANNELD_AWAITING_SPLICE)
return;
cc = splice_command_for_chan(ld, channel);
/* If matching user command found, this was a user intiated splice */
channel_set_state(channel,
CHANNELD_NORMAL,
CHANNELD_AWAITING_SPLICE,
cc ? REASON_USER : REASON_REMOTE,
"Splice signatures sent");
watch_splice_inflight(ld, inflight);
}
bool depthcb_update_scid(struct channel *channel,
const struct bitcoin_txid *txid,
const struct bitcoin_outpoint *outpoint)
{
struct txlocator *loc;
struct lightningd *ld = channel->peer->ld;
struct short_channel_id scid;
/* What scid is this giving us? */
loc = wallet_transaction_locate(tmpctx, ld->wallet, txid);
if (!mk_short_channel_id(&scid,
loc->blkheight, loc->index,
outpoint->n)) {
channel_fail_permanent(channel,
REASON_LOCAL,
"Invalid funding scid %u:%u:%u",
loc->blkheight, loc->index,
outpoint->n);
return false;
}
/* No change? Great. */
if (channel->scid && short_channel_id_eq(channel->scid, &scid))
return true;
if (!channel->scid) {
wallet_annotate_txout(ld->wallet, outpoint,
TX_CHANNEL_FUNDING, channel->dbid);
channel->scid = tal_dup(channel, struct short_channel_id, &scid);
/* If we have a zeroconf channel, i.e., no scid yet
* but have exchange `channel_ready` messages, then we
* need to fire a second time, in order to trigger the
* `coin_movement` event. This is a subset of the
* `lockin_complete` function called from
* AWAITING_LOCKIN->NORMAL otherwise. */
if (channel->minimum_depth == 0)
lockin_has_completed(channel, false);
} else {
/* We freaked out if required when original was
* removed, so just update now */
log_info(channel->log, "Short channel id changed from %s->%s",
type_to_string(tmpctx, struct short_channel_id, channel->scid),
type_to_string(tmpctx, struct short_channel_id, &scid));
*channel->scid = scid;
channel_gossip_scid_changed(channel);
}
wallet_channel_save(ld->wallet, channel);
return true;
}
static void handle_add_inflight(struct lightningd *ld,
struct channel *channel,
const u8 *msg)
{
struct bitcoin_outpoint outpoint;
u32 feerate;
struct amount_sat satoshis;
s64 splice_amnt;
struct wally_psbt *psbt;
struct channel_inflight *inflight;
bool i_am_initiator, force_sign_first;
if (!fromwire_channeld_add_inflight(tmpctx,
msg,
&outpoint.txid,
&outpoint.n,
&feerate,
&satoshis,
&splice_amnt,
&psbt,
&i_am_initiator,
&force_sign_first)) {
channel_internal_error(channel,
"bad channel_add_inflight %s",
tal_hex(channel, msg));
return;
}
inflight = new_inflight(channel,
&outpoint,
feerate,
satoshis,
channel->our_funds,
psbt,
channel->lease_expiry,
channel->lease_commit_sig,
channel->lease_chan_max_msat,
channel->lease_chan_max_ppt,
0,
AMOUNT_MSAT(0),
AMOUNT_SAT(0),
splice_amnt,
i_am_initiator,
force_sign_first);
log_debug(channel->log, "lightningd adding inflight with txid %s",
type_to_string(tmpctx, struct bitcoin_txid,
&inflight->funding->outpoint.txid));
wallet_inflight_add(ld->wallet, inflight);
subd_send_msg(channel->owner, take(towire_channeld_got_inflight(NULL)));
}
static void handle_update_inflight(struct lightningd *ld,
struct channel *channel,
const u8 *msg)
{
struct channel_inflight *inflight;
struct wally_psbt *psbt;
struct bitcoin_txid txid;
struct bitcoin_tx *last_tx;
struct bitcoin_signature *last_sig;
if (!fromwire_channeld_update_inflight(tmpctx, msg, &psbt, &last_tx,
&last_sig)) {
channel_internal_error(channel,
"bad channel_add_inflight %s",
tal_hex(channel, msg));
return;
}
psbt_txid(tmpctx, psbt, &txid, NULL);
inflight = channel_inflight_find(channel, &txid);
if (!inflight)
channel_internal_error(channel, "Unable to load inflight for"
" update_inflight txid %s",
type_to_string(tmpctx,
struct bitcoin_txid,
&txid));
if (!!last_tx != !!last_sig)
channel_internal_error(channel, "Must set last_tx and last_sig"
" together at the same time for"
" update_inflight txid %s",
type_to_string(tmpctx,
struct bitcoin_txid,
&txid));
if (last_tx) {
tal_free(inflight->last_tx);
inflight->last_tx = clone_bitcoin_tx(inflight, last_tx);
}
if (last_sig)
inflight->last_sig = *last_sig;
tal_wally_start();
if (wally_psbt_combine(inflight->funding_psbt, psbt) != WALLY_OK) {
channel_internal_error(channel,
"Unable to combine PSBTs: %s, %s",
type_to_string(tmpctx,
struct wally_psbt,
inflight->funding_psbt),
type_to_string(tmpctx,
struct wally_psbt,
psbt));
tal_wally_end(inflight->funding_psbt);
return;
}
tal_wally_end(inflight->funding_psbt);
psbt_finalize(inflight->funding_psbt);
wallet_inflight_save(ld->wallet, inflight);
}
void channel_record_open(struct channel *channel, u32 blockheight, bool record_push)
{
struct chain_coin_mvt *mvt;
struct amount_msat start_balance;
bool is_pushed = !amount_msat_zero(channel->push);
bool is_leased = channel->lease_expiry > 0;
/* If funds were pushed, add/sub them from the starting balance */
if (channel->opener == LOCAL) {
if (!amount_msat_add(&start_balance,
channel->our_msat, channel->push))
fatal("Unable to add push_msat (%s) + our_msat (%s)",
type_to_string(tmpctx, struct amount_msat,
&channel->push),
type_to_string(tmpctx, struct amount_msat,
&channel->our_msat));
} else {
if (!amount_msat_sub(&start_balance,
channel->our_msat, channel->push))
fatal("Unable to sub our_msat (%s) - push (%s)",
type_to_string(tmpctx, struct amount_msat,
&channel->our_msat),
type_to_string(tmpctx, struct amount_msat,
&channel->push));
}
/* If it's not in a block yet, send a proposal */
if (blockheight > 0)
mvt = new_coin_channel_open(tmpctx,
&channel->cid,
&channel->funding,
&channel->peer->id,
blockheight,
start_balance,
channel->funding_sats,
channel->opener == LOCAL,
is_leased);
else
mvt = new_coin_channel_open_proposed(tmpctx,
&channel->cid,
&channel->funding,
&channel->peer->id,
start_balance,
channel->funding_sats,
channel->opener == LOCAL,
is_leased);
notify_chain_mvt(channel->peer->ld, mvt);
/* If we pushed sats, *now* record them */
if (is_pushed && record_push)
notify_channel_mvt(channel->peer->ld,
new_coin_channel_push(tmpctx, &channel->cid,
channel->push,
is_leased ? LEASE_FEE : PUSHED,
channel->opener == REMOTE));
}
void lockin_has_completed(struct channel *channel, bool record_push)
{
struct lightningd *ld = channel->peer->ld;
/* Fees might have changed (and we use IMMEDIATE once we're funded),
* so update now. */
try_update_feerates(ld, channel);
try_update_blockheight(ld, channel, get_block_height(ld->topology));
/* Emit an event for the channel open (or channel proposal if blockheight
* is zero) */
channel_record_open(channel,
channel->scid ?
short_channel_id_blocknum(channel->scid) : 0,
record_push);
}
void lockin_complete(struct channel *channel,
enum channel_state expected_state)
{
if (!channel->scid &&
(!channel->alias[REMOTE] || !channel->alias[LOCAL])) {
log_debug(channel->log, "Attempted lockin, but neither scid "
"nor aliases are set, ignoring");
return;
}
/* We set this once they're locked in. */
assert(channel->remote_channel_ready);
/* We might have already started shutting down */
if (channel->state != expected_state) {
log_debug(channel->log, "Lockin complete, but state %s",
channel_state_name(channel));
return;
}
channel_set_state(channel,
expected_state,
CHANNELD_NORMAL,
REASON_UNKNOWN,
"Lockin complete");
lockin_has_completed(channel, true);
}
bool channel_on_channel_ready(struct channel *channel,
const struct pubkey *next_per_commitment_point,
const struct short_channel_id *remote_alias)
{
if (channel->remote_channel_ready) {
channel_internal_error(channel,
"channel_got_channel_ready twice");
return false;
}
update_per_commit_point(channel, next_per_commitment_point);
/* FIXME: we should apply this even if it changed! */
if (channel->alias[REMOTE] == NULL) {
channel->alias[REMOTE]
= tal_dup_or_null(channel, struct short_channel_id,
remote_alias);
}
log_debug(channel->log, "Got channel_ready");
channel->remote_channel_ready = true;
return true;
}
static void handle_peer_splice_locked(struct channel *channel, const u8 *msg)
{
struct amount_sat funding_sats;
s64 splice_amnt;
struct channel_inflight *inflight;
struct bitcoin_txid locked_txid;
struct txwatch *txw;
if (!fromwire_channeld_got_splice_locked(msg, &funding_sats,
&splice_amnt,
&locked_txid)) {
channel_internal_error(channel,
"bad channel_got_funding_locked %s",
tal_hex(channel, msg));
return;
}
channel->our_msat.millisatoshis += splice_amnt * 1000; /* Raw: splicing */
channel->msat_to_us_min.millisatoshis += splice_amnt * 1000; /* Raw: splicing */
channel->msat_to_us_max.millisatoshis += splice_amnt * 1000; /* Raw: splicing */
inflight = channel_inflight_find(channel, &locked_txid);
if(!inflight)
channel_internal_error(channel, "Unable to load inflight for"
" locked_txid %s",
type_to_string(tmpctx,
struct bitcoin_txid,
&locked_txid));
wallet_htlcsigs_confirm_inflight(channel->peer->ld->wallet, channel,
&inflight->funding->outpoint);
update_channel_from_inflight(channel->peer->ld, channel, inflight);
/* Remember that we got the lockin */
wallet_channel_save(channel->peer->ld->wallet, channel);
log_debug(channel->log, "lightningd, splice_locked clearing inflights");