forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdual_open_control.c
4186 lines (3647 loc) · 126 KB
/
dual_open_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
/* This is the lightningd handler for messages to/from various
* dualopend subdaemons. It manages the callbacks and database
* saves and funding tx watching for a channel open */
#include "config.h"
#include <bitcoin/short_channel_id.h>
#include <ccan/array_size/array_size.h>
#include <ccan/cast/cast.h>
#include <ccan/mem/mem.h>
#include <ccan/tal/str/str.h>
#include <common/blockheight_states.h>
#include <common/json_channel_type.h>
#include <common/json_command.h>
#include <common/json_param.h>
#include <common/psbt_open.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 <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/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/opening_common.h>
#include <lightningd/peer_control.h>
#include <lightningd/peer_fd.h>
#include <lightningd/plugin_hook.h>
#include <openingd/dualopend_wiregen.h>
struct commit_rcvd {
struct channel *channel;
struct channel_id cid;
struct uncommitted_channel *uc;
};
static void channel_disconnect(struct channel *channel,
enum log_level level,
bool reconnect,
const char *desc)
{
log_(channel->log, level, NULL, false, "%s", desc);
channel_cleanup_commands(channel, desc);
channel_fail_transient(channel, true, "%s: %s",
channel->owner ?
channel->owner->name :
"dualopend-dead",
desc);
}
void channel_unsaved_close_conn(struct channel *channel, const char *why)
{
/* Gotta be unsaved */
assert(channel_state_uncommitted(channel->state));
log_info(channel->log, "Unsaved peer failed."
" Disconnecting and deleting channel. Reason: %s",
why);
channel_cleanup_commands(channel, why);
assert(channel->owner);
channel_set_owner(channel, NULL);
delete_channel(channel);
}
static void channel_saved_err_broken_reconn(struct channel *channel,
const char *fmt, ...)
{
va_list ap;
const char *errmsg;
/* We only reconnect to 'saved' channel peers */
assert(!channel_state_uncommitted(channel->state));
va_start(ap, fmt);
errmsg = tal_vfmt(tmpctx, fmt, ap);
va_end(ap);
log_broken(channel->log, "%s", errmsg);
channel_disconnect(channel, LOG_INFORM, true, errmsg);
}
static void channel_err_broken(struct channel *channel,
const char *fmt, ...)
{
va_list ap;
const char *errmsg;
va_start(ap, fmt);
errmsg = tal_vfmt(tmpctx, fmt, ap);
va_end(ap);
if (channel_state_uncommitted(channel->state)) {
log_broken(channel->log, "%s", errmsg);
channel_unsaved_close_conn(channel, errmsg);
} else
channel_disconnect(channel, LOG_BROKEN, false, errmsg);
}
void json_add_unsaved_channel(struct json_stream *response,
const struct channel *channel,
/* Only set for listpeerchannels */
const struct peer *peer)
{
struct amount_msat total;
struct open_attempt *oa;
if (!channel)
return;
/* If we're chatting but no channel, that's shown by connected: True */
if (!channel->open_attempt)
return;
/* If we're calling out to connectd to activate peer to start the
* process, this will be NULL */
if (!channel->owner)
return;
oa = channel->open_attempt;
json_object_start(response, NULL);
/* listpeerchannels only */
if (peer) {
json_add_node_id(response, "peer_id", &peer->id);
json_add_bool(response, "peer_connected", peer->connected == PEER_CONNECTED);
json_add_channel_type(response, "channel_type", channel->type);
}
json_add_string(response, "state", channel_state_name(channel));
json_add_string(response, "owner", channel->owner->name);
json_add_string(response, "opener", channel->opener == LOCAL ?
"local" : "remote");
json_add_bool(response, "lost_state", channel->future_per_commitment_point ? true : false);
json_array_start(response, "status");
for (size_t i = 0; i < ARRAY_SIZE(channel->billboard.permanent); i++) {
if (!channel->billboard.permanent[i])
continue;
json_add_string(response, NULL,
channel->billboard.permanent[i]);
}
if (channel->billboard.transient)
json_add_string(response, NULL, channel->billboard.transient);
json_array_end(response);
/* funding + our_upfront_shutdown only available if we're initiator */
if (oa->role == TX_INITIATOR) {
if (amount_sat_to_msat(&total, oa->funding)) {
json_add_amount_msat(response, "to_us_msat", total);
/* This will change if peer adds funds */
json_add_amount_msat(response, "total_msat", total);
}
}
json_array_start(response, "features");
/* v2 channels assume static_remotekey */
json_add_string(response, NULL, "option_static_remotekey");
if (feature_negotiated(channel->peer->ld->our_features,
channel->peer->their_features,
OPT_ANCHOR_OUTPUTS))
json_add_string(response, NULL, "option_anchor_outputs");
if (feature_negotiated(channel->peer->ld->our_features,
channel->peer->their_features,
OPT_ANCHORS_ZERO_FEE_HTLC_TX))
json_add_string(response, NULL, "option_anchors_zero_fee_htlc_tx");
json_array_end(response);
json_object_end(response);
}
struct rbf_channel_payload {
struct subd *dualopend;
struct channel *channel;
struct node_id peer_id;
/* Info specific to this RBF */
struct channel_id channel_id;
struct amount_sat their_last_funding;
struct amount_sat their_proposed_funding;
struct amount_sat our_last_funding;
u32 funding_feerate_per_kw;
u32 locktime;
bool req_confirmed_ins_remote;
/* General info */
u32 feerate_our_max;
u32 feerate_our_min;
/* What's the maximum amount of funding
* this channel can hold */
struct amount_sat channel_max;
/* If they've requested funds, this is their request */
struct amount_sat *requested_lease_amt;
/* Returned from hook */
struct amount_sat our_funding;
struct wally_psbt *psbt;
char *err_msg;
};
static void rbf_channel_hook_serialize(struct rbf_channel_payload *payload,
struct json_stream *stream,
struct plugin *plugin)
{
json_object_start(stream, "rbf_channel");
json_add_node_id(stream, "id", &payload->peer_id);
json_add_channel_id(stream, "channel_id", &payload->channel_id);
json_add_amount_sat_msat(stream, "their_last_funding_msat",
payload->their_last_funding);
json_add_amount_sat_msat(stream, "their_funding_msat",
payload->their_proposed_funding);
json_add_amount_sat_msat(stream, "our_last_funding_msat",
payload->our_last_funding);
json_add_num(stream, "locktime", payload->locktime);
json_add_num(stream, "feerate_our_max",
payload->feerate_our_max);
json_add_num(stream, "feerate_our_min",
payload->feerate_our_min);
json_add_num(stream, "funding_feerate_per_kw",
payload->funding_feerate_per_kw);
json_add_amount_sat_msat(stream, "channel_max_msat",
payload->channel_max);
if (payload->requested_lease_amt)
json_add_amount_sat_msat(stream, "requested_lease_msat",
*payload->requested_lease_amt);
json_add_bool(stream, "require_confirmed_inputs",
payload->req_confirmed_ins_remote);
json_object_end(stream);
}
/* ~Map of the Territory~
*
* openchannel hook
- reserveinputs feerate [{"amt": amt, "script": ""}] excludecommon=true -> psbt
-> psbt_set
*
* openchannel_changed hook
- psbt --> most recent
-> psbt_set (if same as orig) | complete flag
*
* openchannel_sign hook
- signpsbt psbt -> partially_signed_psbt
-> partially_signed_psbt
*/
struct openchannel2_payload {
struct subd *dualopend;
struct channel *channel;
struct node_id peer_id;
struct channel_id channel_id;
struct amount_sat their_funding;
struct amount_sat dust_limit_satoshis;
struct amount_msat max_htlc_value_in_flight_msat;
struct amount_msat htlc_minimum_msat;
u32 funding_feerate_per_kw;
u32 feerate_our_max;
u32 feerate_our_min;
u32 commitment_feerate_per_kw;
u16 to_self_delay;
u16 max_accepted_htlcs;
u8 channel_flags;
u32 locktime;
u8 *shutdown_scriptpubkey;
/* What's the maximum amount of funding
* this channel can hold */
struct amount_sat channel_max;
/* If they've requested funds, this is their request */
struct amount_sat *requested_lease_amt;
u32 lease_blockheight_start;
u32 node_blockheight;
bool req_confirmed_ins_remote;
struct amount_sat accepter_funding;
struct wally_psbt *psbt;
const u8 *our_shutdown_scriptpubkey;
struct lease_rates *rates;
char *err_msg;
};
static void openchannel2_hook_serialize(struct openchannel2_payload *payload,
struct json_stream *stream,
struct plugin *plugin)
{
json_object_start(stream, "openchannel2");
json_add_node_id(stream, "id", &payload->peer_id);
json_add_channel_id(stream, "channel_id", &payload->channel_id);
json_add_amount_sat_msat(stream,
"their_funding_msat", payload->their_funding);
json_add_amount_sat_msat(stream,
"dust_limit_msat", payload->dust_limit_satoshis);
json_add_amount_msat(stream, "max_htlc_value_in_flight_msat",
payload->max_htlc_value_in_flight_msat);
json_add_amount_msat(stream, "htlc_minimum_msat",
payload->htlc_minimum_msat);
json_add_num(stream, "funding_feerate_per_kw",
payload->funding_feerate_per_kw);
json_add_num(stream, "commitment_feerate_per_kw",
payload->commitment_feerate_per_kw);
json_add_num(stream, "feerate_our_max",
payload->feerate_our_max);
json_add_num(stream, "feerate_our_min",
payload->feerate_our_min);
json_add_num(stream, "to_self_delay", payload->to_self_delay);
json_add_num(stream, "max_accepted_htlcs", payload->max_accepted_htlcs);
json_add_num(stream, "channel_flags", payload->channel_flags);
json_add_num(stream, "locktime", payload->locktime);
if (tal_bytelen(payload->shutdown_scriptpubkey) != 0)
json_add_hex_talarr(stream, "shutdown_scriptpubkey",
payload->shutdown_scriptpubkey);
json_add_amount_sat_msat(stream, "channel_max_msat",
payload->channel_max);
if (payload->requested_lease_amt) {
json_add_amount_sat_msat(stream, "requested_lease_msat",
*payload->requested_lease_amt);
json_add_num(stream, "lease_blockheight_start",
payload->lease_blockheight_start);
json_add_num(stream, "node_blockheight",
payload->node_blockheight);
}
json_add_bool(stream, "require_confirmed_inputs",
payload->req_confirmed_ins_remote);
json_object_end(stream);
}
struct openchannel2_psbt_payload {
struct subd *dualopend;
struct wally_psbt *psbt;
struct channel *channel;
struct lightningd *ld;
};
static void
openchannel2_changed_hook_serialize(struct openchannel2_psbt_payload *payload,
struct json_stream *stream,
struct plugin *plugin)
{
json_object_start(stream, "openchannel2_changed");
json_add_psbt(stream, "psbt", payload->psbt);
json_add_string(stream, "channel_id",
type_to_string(tmpctx, struct channel_id,
&payload->channel->cid));
json_add_bool(stream, "require_confirmed_inputs",
payload->channel->req_confirmed_ins[REMOTE]);
json_object_end(stream);
}
static void
openchannel2_sign_hook_serialize(struct openchannel2_psbt_payload *payload,
struct json_stream *stream,
struct plugin *plugin)
{
json_object_start(stream, "openchannel2_sign");
json_add_psbt(stream, "psbt", payload->psbt);
json_add_string(stream, "channel_id",
type_to_string(tmpctx, struct channel_id,
&payload->channel->cid));
json_object_end(stream);
}
static const u8 *hook_extract_shutdown_script(struct subd* dualopend,
const char *buffer,
const jsmntok_t *toks)
{
const u8 *close_to_script;
enum address_parse_result parse_res;
if (!buffer)
return NULL;
const jsmntok_t *t = json_get_member(buffer, toks, "result");
if (!t)
fatal("Plugin must return a 'result'"
"%.*s", toks[0].end - toks[0].start,
buffer + toks[0].start);
if (!json_tok_streq(buffer, t, "continue")) {
char *errmsg = "Client error. Unable to continue";
subd_send_msg(dualopend,
take(towire_dualopend_fail(NULL, errmsg)));
return NULL;
}
const jsmntok_t *close_to_tok = json_get_member(buffer, toks, "close_to");
if (!close_to_tok)
return NULL;
parse_res = json_to_address_scriptpubkey(tmpctx, chainparams, buffer,
close_to_tok, &close_to_script);
switch (parse_res) {
case ADDRESS_PARSE_UNRECOGNIZED:
fatal("Plugin returned an invalid response to the"
" openchannel2.close_to hook: %.*s",
t->end - t->start, buffer + t->start);
case ADDRESS_PARSE_WRONG_NETWORK:
fatal("Plugin returned invalid response to the"
" openchannel2.close_to hook: address %s is"
" not on network %s",
tal_hex(NULL, close_to_script),
chainparams->network_name);
case ADDRESS_PARSE_SUCCESS:
return close_to_script;
}
return NULL;
}
static bool
hook_extract_psbt(const tal_t *ctx, struct subd *dualopend, const char *buffer,
const jsmntok_t *toks, char *hook_name,
bool allow_empty,
struct wally_psbt **out)
{
if (!buffer)
fatal("Plugin must return a valid response to %s", hook_name);
const jsmntok_t *t = json_get_member(buffer, toks, "result");
if (!t)
fatal("Plugin must return a 'result' to %s"
"%.*s", hook_name, toks[0].end - toks[0].start,
buffer + toks[0].start);
if (!json_tok_streq(buffer, t, "continue")) {
/* dualopend might have closed if we're on the signed round */
if (dualopend) {
char *errmsg = "Client error. Unable to continue";
subd_send_msg(dualopend,
take(towire_dualopend_fail(NULL, errmsg)));
}
return false;
}
const jsmntok_t *psbt_tok = json_get_member(buffer, toks, "psbt");
if (!psbt_tok) {
if (!allow_empty)
fatal("Plugin must return a 'psbt' to a 'continue'd"
"%s %.*s", hook_name,
toks[0].end - toks[0].start,
buffer + toks[0].start);
*out = NULL;
return true;
}
*out = json_to_psbt(ctx, buffer, psbt_tok);
if (!*out)
fatal("Plugin must return a valid 'psbt' to a 'continue'd"
"%s %.*s", hook_name,
toks[0].end - toks[0].start,
buffer + toks[0].start);
return true;
}
/* The field is *always* assumed msats, as that's the unit
* amount we're transitioning our API over to. A 'xxxsat'
* unit will be interpreted correctly, but a value given
* without a unit will always be interpreted as msats */
static bool
hook_extract_amount(struct subd *dualopend,
const char *buffer,
const jsmntok_t *toks,
char *field_name,
struct amount_sat *amount)
{
struct amount_msat msats;
if (!buffer)
return false;
const jsmntok_t *t = json_get_member(buffer, toks, "result");
if (!t)
fatal("Plugin must return a 'result' "
" %.*s", toks[0].end - toks[0].start,
buffer + toks[0].start);
if (!json_tok_streq(buffer, t, "continue")) {
char *errmsg = "Client error. Unable to continue";
subd_send_msg(dualopend,
take(towire_dualopend_fail(NULL, errmsg)));
return false;
}
/* If there's no amount_sat field, that's ok */
const jsmntok_t *amt_tok = json_get_member(buffer, toks, field_name);
if (!amt_tok) {
*amount = AMOUNT_SAT(0);
return true;
}
if (!json_to_msat(buffer, amt_tok, &msats))
fatal("Plugin must return a valid '%s' to a 'continue'd"
" %.*s", field_name,
toks[0].end - toks[0].start,
buffer + toks[0].start);
*amount = amount_msat_to_sat_round_down(msats);
return true;
}
static void rbf_channel_remove_dualopend(struct subd *dualopend,
struct rbf_channel_payload *payload)
{
assert(payload->dualopend == dualopend);
payload->dualopend = NULL;
}
static void rbf_channel_hook_cb(struct rbf_channel_payload *payload STEALS)
{
struct subd *dualopend = payload->dualopend;
struct channel *channel = payload->channel;
u8 *msg;
tal_steal(tmpctx, payload);
if (!dualopend)
return;
tal_del_destructor2(dualopend, rbf_channel_remove_dualopend, payload);
if (channel->state != DUALOPEND_AWAITING_LOCKIN) {
log_debug(channel->log,
"rbf_channel hook returned, but channel in state"
" %s", channel_state_name(channel));
msg = towire_dualopend_fail(NULL, "Peer error. Channel"
" not ready for RBF attempt.");
return subd_send_msg(dualopend, take(msg));
}
if (payload->err_msg) {
log_debug(channel->log,
"rbf_channel hook rejects and says '%s'",
payload->err_msg);
msg = towire_dualopend_fail(NULL, payload->err_msg);
return subd_send_msg(dualopend, take(msg));
}
/* Update the remote's require confirmed preferences */
if (payload->req_confirmed_ins_remote != channel->req_confirmed_ins[REMOTE]) {
channel->req_confirmed_ins[REMOTE] =
payload->req_confirmed_ins_remote;
wallet_channel_save(dualopend->ld->wallet, channel);
}
/* Update channel with new open attempt. */
channel->open_attempt = new_channel_open_attempt(channel);
msg = towire_dualopend_got_rbf_offer_reply(NULL,
payload->our_funding,
payload->psbt);
subd_send_msg(dualopend, take(msg));
}
static bool
rbf_channel_hook_deserialize(struct rbf_channel_payload *payload,
const char *buffer,
const jsmntok_t *toks)
{
struct subd *dualopend = payload->dualopend;
struct channel *channel = payload->channel;
if (!dualopend) {
rbf_channel_hook_cb(payload);
return false;
}
/* FIXME: move to new json extraction */
const jsmntok_t *t_result = json_get_member(buffer, toks, "result");
if (!t_result)
fatal("Plugin returned an invalid response to the"
" rbf_channel hook: %.*s",
json_tok_full_len(toks),
json_tok_full(buffer, toks));
if (json_tok_streq(buffer, t_result, "reject")) {
if (json_get_member(buffer, toks, "psbt"))
fatal("Plugin rejected rbf_channel but"
" also set `psbt`");
if (json_get_member(buffer, toks, "our_funding_msat"))
fatal("Plugin rejected rbf_channel but"
" also set `our_funding_msat`");
const jsmntok_t *t_errmsg = json_get_member(buffer, toks,
"error_message");
if (t_errmsg)
payload->err_msg = json_strdup(payload, buffer,
t_errmsg);
else
payload->err_msg = "";
rbf_channel_hook_cb(payload);
return false;
} else if (!json_tok_streq(buffer, t_result, "continue"))
fatal("Plugin returned invalid response to rbf_channel hook:"
" %.*s", json_tok_full_len(toks),
json_tok_full(buffer, toks));
if (!hook_extract_psbt(payload, dualopend, buffer, toks,
"rbf_channel", true, &payload->psbt))
return false;
if (payload->psbt) {
enum tx_role our_role = channel->opener == LOCAL ?
TX_INITIATOR : TX_ACCEPTER;
psbt_add_serials(payload->psbt, our_role);
}
/* We require the PSBT to meet certain criteria such as
* extra, proprietary fields (`serial_id`s) or
* to have a `redeemscripts` iff the inputs are P2SH.
*
* Since this is externally provided, we confirm that
* they've done the right thing / haven't lost any required info.
*/
if (payload->psbt && !psbt_has_required_fields(payload->psbt))
fatal("Plugin supplied PSBT that's missing"
" required fields: %s",
type_to_string(tmpctx, struct wally_psbt,
payload->psbt));
if (!hook_extract_amount(dualopend, buffer, toks,
"our_funding_msat", &payload->our_funding))
fatal("Plugin failed to supply our_funding_msat field");
if (payload->psbt
&& amount_sat_zero(payload->our_funding))
fatal("Plugin failed to supply our_funding_msat field");
if (!payload->psbt &&
!amount_sat_zero(payload->our_funding)) {
log_broken(channel->log, "`our_funding_msat` returned"
" but no `psbt` present. %.*s",
json_tok_full_len(toks),
json_tok_full(buffer, toks));
payload->err_msg = "Client error. Unable to continue";
rbf_channel_hook_cb(payload);
return false;
}
return true;
}
/* dualopend dies? Remove dualopend ptr from payload */
static void openchannel2_remove_dualopend(struct subd *dualopend,
struct openchannel2_payload *payload)
{
assert(payload->dualopend == dualopend);
payload->dualopend = NULL;
}
static void
openchannel2_hook_cb(struct openchannel2_payload *payload STEALS)
{
struct subd *dualopend = payload->dualopend;
struct channel *channel = payload->channel;
u32 *our_shutdown_script_wallet_index;
u8 *msg;
/* Our daemon died! */
if (!dualopend)
return;
/* Free payload regardless of what happens next */
tal_steal(tmpctx, payload);
channel = dualopend->channel;
/* Channel open is currently in progress elsewhere! */
if (channel->open_attempt) {
msg = towire_dualopend_fail(NULL, "Already initiated channel"
" open");
log_debug(dualopend->ld->log,
"Our open in progress, denying their offer");
return subd_send_msg(dualopend, take(msg));
}
tal_del_destructor2(dualopend, openchannel2_remove_dualopend, payload);
if (payload->err_msg) {
log_debug(dualopend->ld->log,
"openchannel2 hook rejects and says '%s'",
payload->err_msg);
msg = towire_dualopend_fail(NULL, payload->err_msg);
return subd_send_msg(dualopend, take(msg));
}
/* Determine the wallet index for our_shutdown_scriptpubkey,
* NULL if not found. */
u32 found_wallet_index;
bool is_p2sh;
if (wallet_can_spend(dualopend->ld->wallet,
payload->our_shutdown_scriptpubkey,
&found_wallet_index,
&is_p2sh)) {
our_shutdown_script_wallet_index = tal(tmpctx, u32);
*our_shutdown_script_wallet_index = found_wallet_index;
} else
our_shutdown_script_wallet_index = NULL;
channel->cid = payload->channel_id;
channel->opener = REMOTE;
channel->open_attempt = new_channel_open_attempt(channel);
channel->req_confirmed_ins[REMOTE] =
payload->req_confirmed_ins_remote;
msg = towire_dualopend_got_offer_reply(NULL,
payload->accepter_funding,
payload->psbt,
payload->our_shutdown_scriptpubkey,
our_shutdown_script_wallet_index,
payload->rates);
subd_send_msg(dualopend, take(msg));
}
static bool
openchannel2_hook_deserialize(struct openchannel2_payload *payload,
const char *buffer,
const jsmntok_t *toks)
{
const u8 *shutdown_script;
const char *err;
struct subd *dualopend = payload->dualopend;
/* If our daemon died, we're done */
if (!dualopend) {
openchannel2_hook_cb(payload);
return false;
}
const jsmntok_t *t_result = json_get_member(buffer, toks, "result");
if (!t_result)
fatal("Plugin returned an invalid response to the"
" openchannel2 hook: %.*s",
json_tok_full_len(toks),
json_tok_full(buffer, toks));
if (json_tok_streq(buffer, t_result, "reject")) {
/* Should not have set any other fields if 'reject'ing */
if (json_get_member(buffer, toks, "close_to"))
fatal("Plugin rejected openchannel2 but"
" also set close_to");
if (json_get_member(buffer, toks, "psbt"))
fatal("Plugin rejected openchannel2 but"
" also set `psbt`");
if (json_get_member(buffer, toks, "our_funding_msat"))
fatal("Plugin rejected openchannel2 but"
" also set `our_funding_psbt`");
const jsmntok_t *t_errmsg = json_get_member(buffer, toks,
"error_message");
if (t_errmsg)
payload->err_msg = json_strdup(payload,
buffer, t_errmsg);
else
payload->err_msg = "";
openchannel2_hook_cb(payload);
return false;
} else if (!json_tok_streq(buffer, t_result, "continue"))
fatal("Plugin returned an invalid response to the"
" openchannel2 hook: %.*s",
json_tok_full_len(toks),
json_tok_full(buffer, toks));
if (!hook_extract_psbt(payload, dualopend, buffer, toks,
"openchannel2", true, &payload->psbt))
return false;
shutdown_script =
hook_extract_shutdown_script(dualopend, buffer, toks);
if (shutdown_script && payload->our_shutdown_scriptpubkey)
log_broken(dualopend->ld->log,
"openchannel2 hook close_to address was"
" already set by other plugin. Ignoring!");
else
payload->our_shutdown_scriptpubkey = shutdown_script;
struct amount_msat fee_base, fee_max_base;
/* deserialized may be called multiple times */
if (!payload->rates)
payload->rates = tal(payload, struct lease_rates);
err = json_scan(payload, buffer, toks,
"{lease_fee_base_msat:%"
",lease_fee_basis:%"
",channel_fee_max_base_msat:%"
",channel_fee_max_proportional_thousandths:%"
",funding_weight:%}",
JSON_SCAN(json_to_msat, &fee_base),
JSON_SCAN(json_to_u16,
&payload->rates->lease_fee_basis),
JSON_SCAN(json_to_msat, &fee_max_base),
JSON_SCAN(json_to_u16,
&payload->rates->channel_fee_max_proportional_thousandths),
JSON_SCAN(json_to_u16,
&payload->rates->funding_weight));
/* It's possible they didn't send these back! */
if (err)
payload->rates = tal_free(payload->rates);
/* Convert to u32s */
if (payload->rates &&
!lease_rates_set_lease_fee_msat(payload->rates, fee_base))
fatal("Plugin sent overflowing/non-sat `lease_fee_base_msat`");
if (payload->rates &&
!lease_rates_set_chan_fee_base_msat(payload->rates, fee_max_base))
fatal("Plugin sent overflowing `channel_fee_max_base_msat`");
/* Add a serial_id to everything that doesn't have one yet */
if (payload->psbt)
psbt_add_serials(payload->psbt, TX_ACCEPTER);
/* We require the PSBT to meet certain criteria such as
* extra, proprietary fields (`serial_id`s) or
* to have a `redeemscripts` iff the inputs are P2SH.
*
* Since this is externally provided, we confirm that
* they've done the right thing / haven't lost any required info.
*/
if (payload->psbt && !psbt_has_required_fields(payload->psbt))
fatal("Plugin supplied PSBT that's missing required fields. %s",
type_to_string(tmpctx, struct wally_psbt, payload->psbt));
if (!hook_extract_amount(dualopend, buffer, toks,
"our_funding_msat",
&payload->accepter_funding))
fatal("Plugin failed to supply our_funding_msat field");
if (payload->psbt
&& amount_sat_zero(payload->accepter_funding))
fatal("Plugin failed to supply our_funding_msat field");
if (!payload->psbt
&& !amount_sat_zero(payload->accepter_funding)) {
/* Gotta give a PSBT if you set the accepter_funding amount */
/* Let dualopend know we've failed */
payload->err_msg = "Client error. Unable to continue";
openchannel2_hook_cb(payload);
return false;
}
return true;
}
/* dualopend dies? Remove dualopend ptr from payload */
static void
openchannel2_psbt_remove_dualopend(struct subd *dualopend,
struct openchannel2_psbt_payload *payload)
{
assert(payload->dualopend == dualopend);
payload->dualopend = NULL;
}
static bool
openchannel2_changed_deserialize(struct openchannel2_psbt_payload *payload,
const char *buffer, const jsmntok_t *toks)
{
struct subd *dualopend = payload->dualopend;
struct wally_psbt *psbt;
if (!hook_extract_psbt(NULL, dualopend, buffer,
toks, "openchannel2_changed",
false, &psbt))
return false;
/* Add serials to PSBT, before checking for required fields */
psbt_add_serials(psbt, TX_ACCEPTER);
/* We require the PSBT to meet certain criteria such as
* extra, proprietary fields (`serial_id`s) or
* to have a `redeemscripts` iff the inputs are P2SH.
*
* Since this is externally provided, we confirm that
* they've done the right thing / haven't lost any required info.
*/
if (!psbt_has_required_fields(psbt))
fatal("Plugin supplied PSBT that's missing required fields. %s",
type_to_string(tmpctx, struct wally_psbt, psbt));
if (payload->psbt)
tal_free(payload->psbt);
payload->psbt = tal_steal(payload, psbt);
return true;
}
static void
openchannel2_changed_hook_cb(struct openchannel2_psbt_payload *payload STEALS)
{
struct subd *dualopend = payload->dualopend;
/* Free payload regardless of what happens next */
tal_steal(tmpctx, payload);
/* If our daemon died, we're done */
if (!dualopend)
return;
tal_del_destructor2(dualopend,
openchannel2_psbt_remove_dualopend,
payload);
subd_send_msg(dualopend,
take(towire_dualopend_psbt_updated(NULL,
payload->psbt)));
}
static bool
openchannel2_signed_deserialize(struct openchannel2_psbt_payload *payload,
const char *buffer, const jsmntok_t *toks)
{
struct subd *dualopend = payload->dualopend;
struct wally_psbt *psbt;
if (!hook_extract_psbt(NULL, dualopend, buffer,
toks, "openchannel2_sign",
false, &psbt))
return false;
/* We require the PSBT to meet certain criteria such as
* extra, proprietary fields (`serial_id`s) or
* to have a `redeemscripts` iff the inputs are P2SH.
*
* Since this is externally provided, we confirm that
* they've done the right thing / haven't lost any required info.
*/
if (!psbt_has_required_fields(psbt))
fatal("Plugin supplied PSBT that's missing required fields. %s",
type_to_string(tmpctx, struct wally_psbt, psbt));
/* NOTE - The psbt_contribs_changed function nulls lots of
* fields in place to compare the PSBTs. This removes the
* witness stack held in final_witness. Give it a clone of
* the PSBT to hack on instead ... */
struct wally_psbt *psbt_clone;
psbt_clone = clone_psbt(tmpctx, psbt);
/* Verify that inputs/outputs are the same. Note that this is a
* 'de minimus' check -- we just look at serial_ids. If you've
* totally managled the data here but left the serial_ids intact,
* you'll get a failure back from the peer when you send
* commitment sigs */
if (psbt_contribs_changed(payload->psbt, psbt_clone))
fatal("Plugin must not change psbt input/output set. "
"orig: %s. updated: %s",
type_to_string(tmpctx, struct wally_psbt,
payload->psbt),
type_to_string(tmpctx, struct wally_psbt,
psbt));
if (payload->psbt)
tal_free(payload->psbt);
payload->psbt = tal_steal(payload, psbt);
return true;
}
static void dualopend_tell_depth(struct channel *channel,
const struct bitcoin_txid *txid,
u32 depth)
{
const u8 *msg;
u32 to_go;
if (!channel->owner) {
log_debug(channel->log,
"Funding tx %s confirmed, but peer disconnected",
type_to_string(tmpctx, struct bitcoin_txid, txid));
return;
}
log_debug(channel->log,
"Funding tx %s confirmed, telling peer",
type_to_string(tmpctx, struct bitcoin_txid, txid));
if (depth < channel->minimum_depth) {
to_go = channel->minimum_depth - depth;
} else
to_go = 0;
/* Are we there yet? */
if (to_go == 0) {
assert(channel->scid);
assert(bitcoin_txid_eq(&channel->funding.txid, txid));
channel_set_billboard(channel, false,