forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlnchannel.c
927 lines (808 loc) · 27.1 KB
/
lnchannel.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
#include "names.h"
#include "db.h"
#include "log.h"
#include "lnchannel_internal.h"
#include "channel.h"
#include "permute_tx.h"
#include "close_tx.h"
#include "commit_tx.h"
#include "output_to_htlc.h"
#include "pseudorand.h"
#include "remove_dust.h"
#include "secrets.h"
#include "btcnetwork/c/chaintopology.h"
#include "btcnetwork/c/watch.h"
#include "utils/utils.h"
#include "utils/sodium/randombytes.h"
#include <bitcoin/base58.h>
#include <bitcoin/tx.h>
#include <ccan/array_size/array_size.h>
#include <ccan/cast/cast.h>
#include <ccan/list/list.h>
#include <ccan/mem/mem.h>
#include <ccan/noerr/noerr.h>
#include <ccan/ptrint/ptrint.h>
#include <ccan/str/hex/hex.h>
#include <ccan/structeq/structeq.h>
#include <ccan/tal/str/str.h>
#include <ccan/tal/tal.h>
#include <errno.h>
#include <inttypes.h>
#include <stdlib.h>
#include <sys/types.h>
struct bitcoin_tx *lnchn_create_close_tx(const tal_t *ctx,
struct LNchannel *lnchn, u64 fee)
{
struct channel_state cstate;
/* We don't need a deep copy here, just fee levels. */
cstate = *lnchn->local.staging_cstate;
if (!force_fee(&cstate, fee)) {
log_unusual(lnchn->log,
"lnchn_create_close_tx: can't afford fee %"PRIu64,
fee);
return NULL;
}
log_debug(lnchn->log,
"creating close-tx with fee %"PRIu64" amounts %u/%u to ",
fee,
cstate.side[LOCAL].pay_msat / 1000,
cstate.side[REMOTE].pay_msat / 1000);
log_add_struct(lnchn->log, "%s", struct pubkey, &lnchn->local.finalkey);
log_add_struct(lnchn->log, "/%s", struct pubkey, &lnchn->remote.finalkey);
return create_close_tx(ctx,
lnchn->closing.our_script,
lnchn->closing.their_script,
&lnchn->anchor.txid,
lnchn->anchor.index,
lnchn->anchor.satoshis,
cstate.side[LOCAL].pay_msat / 1000,
cstate.side[REMOTE].pay_msat / 1000);
}
/* Create a bitcoin close tx, using last signature they sent. */
static const struct bitcoin_tx *mk_bitcoin_close(const tal_t *ctx,
struct LNchannel *lnchn)
{
struct bitcoin_tx *close_tx;
ecdsa_signature our_close_sig;
close_tx = lnchn_create_close_tx(ctx, lnchn, lnchn->closing.their_fee);
lnchn_sign_mutual_close(lnchn, close_tx, &our_close_sig);
close_tx->input[0].witness
= bitcoin_witness_2of2(close_tx->input,
lnchn->closing.their_sig,
&our_close_sig,
&lnchn->remote.commitkey,
&lnchn->local.commitkey);
return close_tx;
}
void internal_set_lnchn_state(struct LNchannel *lnchn, enum state newstate,
const char *caller, bool db_commit)
{
log_debug(lnchn->log, "%s: %s => %s", caller,
state_name(lnchn->state), state_name(newstate));
lnchn->state = newstate;
lnchn->state_height = get_block_height(lnchn->dstate->topology);
/* We can only route in normal state. */
if (db_commit)
db_update_state(lnchn);
}
void internal_lnchn_fail_on_notify(struct LNchannel *lnchn, const char* msg, ...)
{
va_list ap;
tal_free(lnchn->notify_fail_reason);
va_start(ap, msg);
lnchn->notify_fail_reason = tal_vfmt(lnchn, msg, ap);
va_end(ap);
log_debug(lnchn->log, "Channel fail on a message: %s", lnchn->notify_fail_reason);
}
void internal_lnchn_temp_breakdown(struct LNchannel *lnchn, const char* reason)
{
size_t len = strlen(reason);
assert(!lnchn->rt.temp_errormsg);
lnchn->rt.temp_errormsg = tal_arrz(lnchn, u8, len + 1);
memcpy(lnchn->rt.temp_errormsg, reason, len);
internal_set_lnchn_state(lnchn, STATE_ERR_TEMP, __func__, false);
}
void internal_lnchn_breakdown(struct LNchannel *lnchn)
{
///* If we have a closing tx, use it. */
//if (lnchn->closing.their_sig) {
// const struct bitcoin_tx *close = mk_bitcoin_close(lnchn, lnchn);
// log_unusual(lnchn->log, "lnchn breakdown: sending close tx");
// broadcast_tx(lnchn->dstate->topology, lnchn, close, NULL);
// tal_free(close);
///* If we have a signed commit tx (maybe not if we just offered
// * anchor, or they supplied anchor, or no outputs to us). */
//} else if (lnchn->local.commit && lnchn->local.commit->sig) {
// log_unusual(lnchn->log, "lnchn breakdown: sending commit tx");
// sign_commit_tx(lnchn);
// broadcast_tx(lnchn->dstate->topology, lnchn,
// lnchn->local.commit->tx, NULL);
//} else {
// log_info(lnchn->log, "lnchn breakdown: nothing to do");
// /* We close immediately. */
// set_lnchn_state(lnchn, STATE_CLOSED, __func__, false);
// db_forget_lnchn(lnchn);
//}
}
/* All unrevoked commit txs must have no HTLCs in them. */
static bool committed_to_htlcs(const struct LNchannel *lnchn)
{
struct htlc_map_iter it;
struct htlc *h;
for (h = htlc_map_first(&lnchn->htlcs, &it);
h;
h = htlc_map_next(&lnchn->htlcs, &it)) {
if (htlc_is_dead(h))
continue;
return true;
}
return false;
}
static void lnchn_calculate_close_fee(struct LNchannel *lnchn)
{
/* Use actual worst-case length of close tx: based on FIXME-OLD#02's
* commitment tx numbers, but only 1 byte for output count */
const uint64_t txsize = 41 + 221 + 10 + 32 + 32;
uint64_t maxfee;
lnchn->closing.our_fee
= fee_by_feerate(txsize, get_feerate(lnchn->dstate->topology));
/* FIXME-OLD #2:
* The sender MUST set `close_fee` lower than or equal to the
* fee of the final commitment transaction, and MUST set
* `close_fee` to an even number of satoshis.
*/
maxfee = commit_tx_fee(lnchn->local.commit->tx, lnchn->anchor.satoshis);
if (lnchn->closing.our_fee > maxfee) {
/* This could only happen if the fee rate dramatically */
log_unusual(lnchn->log,
"Closing fee %"PRIu64" exceeded commit fee %"PRIu64", reducing.",
lnchn->closing.our_fee, maxfee);
lnchn->closing.our_fee = maxfee;
/* This can happen if actual commit txfee is odd. */
if (lnchn->closing.our_fee & 1)
lnchn->closing.our_fee--;
}
assert(!(lnchn->closing.our_fee & 1));
}
//static void start_closing_in_transaction(struct LNchannel *lnchn)
//{
// assert(!committed_to_htlcs(lnchn));
//
// set_lnchn_state(lnchn, STATE_MUTUAL_CLOSING, __func__, true);
//
// lnchn_calculate_close_fee(lnchn);
// lnchn->closing.closing_order = lnchn->order_counter++;
// db_update_our_closing(lnchn);
// queue_pkt_close_signature(lnchn);
//}
void lnchn_fail(struct LNchannel *lnchn, const char *caller)
{
/* Don't fail twice. */
if (state_is_error(lnchn->state) || state_is_onchain(lnchn->state))
return;
/* FIXME: Save state here? */
internal_set_lnchn_state(lnchn, STATE_ERR_BREAKDOWN, caller, false);
internal_lnchn_breakdown(lnchn);
}
//
///* This is the io loop while we're negotiating closing tx. */
//static bool closing_pkt_in(struct LNchannel *lnchn, const Pkt *pkt)
//{
// const CloseSignature *c = pkt->close_signature;
// struct bitcoin_tx *close_tx;
// ecdsa_signature theirsig;
//
// assert(lnchn->state == STATE_MUTUAL_CLOSING);
//
// if (pkt->pkt_case != PKT__PKT_CLOSE_SIGNATURE)
// return lnchn_received_unexpected_pkt(lnchn, pkt, __func__);
//
// log_info(lnchn->log, "closing_pkt_in: they offered close fee %"PRIu64,
// c->close_fee);
//
// /* FIXME-OLD #2:
// *
// * The sender MUST set `close_fee` lower than or equal to the fee of the
// * final commitment transaction, and MUST set `close_fee` to an even
// * number of satoshis.
// */
// if ((c->close_fee & 1)
// || c->close_fee > commit_tx_fee(lnchn->remote.commit->tx,
// lnchn->anchor.satoshis)) {
// return lnchn_comms_err(lnchn, pkt_err(lnchn, "Invalid close fee"));
// }
//
// /* FIXME: Don't accept tiny fee at all? */
//
// /* FIXME-OLD #2:
// ... otherwise it SHOULD propose a
// value strictly between the received `close_fee` and its
// previously-sent `close_fee`.
// */
// if (lnchn->closing.their_sig) {
// /* We want more, they should give more. */
// if (lnchn->closing.our_fee > lnchn->closing.their_fee) {
// if (c->close_fee <= lnchn->closing.their_fee)
// return lnchn_comms_err(lnchn,
// pkt_err(lnchn, "Didn't increase close fee"));
// } else {
// if (c->close_fee >= lnchn->closing.their_fee)
// return lnchn_comms_err(lnchn,
// pkt_err(lnchn, "Didn't decrease close fee"));
// }
// }
//
// /* FIXME-OLD #2:
// *
// * The receiver MUST check `sig` is valid for the close
// * transaction with the given `close_fee`, and MUST fail the
// * connection if it is not. */
// if (!proto_to_signature(c->sig, &theirsig))
// return lnchn_comms_err(lnchn,
// pkt_err(lnchn, "Invalid signature format"));
//
// close_tx = lnchn_create_close_tx(c, lnchn, c->close_fee);
// if (!check_tx_sig(close_tx, 0,
// NULL,
// lnchn->anchor.witnessscript,
// &lnchn->remote.commitkey, &theirsig))
// return lnchn_comms_err(lnchn,
// pkt_err(lnchn, "Invalid signature"));
//
// tal_free(lnchn->closing.their_sig);
// lnchn->closing.their_sig = tal_dup(lnchn,
// ecdsa_signature, &theirsig);
// lnchn->closing.their_fee = c->close_fee;
// lnchn->closing.sigs_in++;
//
// if (!db_update_their_closing(lnchn))
// return lnchn_database_err(lnchn);
//
// if (lnchn->closing.our_fee != lnchn->closing.their_fee) {
// /* FIXME-OLD #2:
// *
// * If the receiver agrees with the fee, it SHOULD reply with a
// * `close_signature` with the same `close_fee` value,
// * otherwise it SHOULD propose a value strictly between the
// * received `close_fee` and its previously-sent `close_fee`.
// */
//
// /* Adjust our fee to close on their fee. */
// u64 sum;
//
// /* Beware overflow! */
// sum = (u64)lnchn->closing.our_fee + lnchn->closing.their_fee;
//
// lnchn->closing.our_fee = sum / 2;
// if (lnchn->closing.our_fee & 1)
// lnchn->closing.our_fee++;
//
// log_info(lnchn->log, "accept_pkt_close_sig: we change to %"PRIu64,
// lnchn->closing.our_fee);
//
// lnchn->closing.closing_order = lnchn->order_counter++;
//
// db_start_transaction(lnchn);
// db_update_our_closing(lnchn);
// if (db_commit_transaction(lnchn) != NULL)
// return lnchn_database_err(lnchn);
//
// queue_pkt_close_signature(lnchn);
// }
//
// /* Note corner case: we may *now* agree with them! */
// if (lnchn->closing.our_fee == lnchn->closing.their_fee) {
// const struct bitcoin_tx *close;
// log_info(lnchn->log, "accept_pkt_close_sig: we agree");
// /* FIXME-OLD #2:
// *
// * Once a node has sent or received a `close_signature` with
// * matching `close_fee` it SHOULD close the connection and
// * SHOULD sign and broadcast the final closing transaction.
// */
// close = mk_bitcoin_close(lnchn, lnchn);
// broadcast_tx(lnchn->dstate->topology, lnchn, close, NULL);
// tal_free(close);
// return false;
// }
//
// return true;
//}
//static void set_feechange(struct LNchannel *lnchn, u64 fee_rate,
// enum feechange_state state)
//{
// /* If we already have a feechange for this commit, simply update it. */
// if (lnchn->feechanges[state]) {
// log_debug(lnchn->log, "Feechange: fee %"PRIu64" to %"PRIu64,
// lnchn->feechanges[state]->fee_rate,
// fee_rate);
// lnchn->feechanges[state]->fee_rate = fee_rate;
// } else {
// log_debug(lnchn->log, "Feechange: New fee %"PRIu64, fee_rate);
// lnchn->feechanges[state] = new_feechange(lnchn, fee_rate, state);
// }
//}
//
//static Pkt *handle_pkt_feechange(struct LNchannel *lnchn, const Pkt *pkt)
//{
// u64 feerate;
// Pkt *err;
//
// err = accept_pkt_update_fee(lnchn, pkt, &feerate);
// if (err)
// return err;
//
// /* FIXME-OLD #2:
// *
// * The sending node MUST NOT send a `fee_rate` which it could not
// * afford (see "Fee Calculation), were it applied to the receiving
// * node's commitment transaction. The receiving node SHOULD fail the
// * connection if this occurs.
// */
// if (!can_afford_feerate(lnchn->local.staging_cstate, feerate, REMOTE))
// return pkt_err(lnchn, "Cannot afford feerate %"PRIu64,
// feerate);
//
// set_feechange(lnchn, feerate, RCVD_FEECHANGE);
// return NULL;
//}
static bool lnchn_start_shutdown(struct LNchannel *lnchn)
{
enum state newstate;
//u8 *redeemscript;
/* We might have uncommited changes; if so, commit them now. */
// if (!do_commit(lnchn, NULL))
// return false;
db_start_transaction(lnchn);
db_begin_shutdown(lnchn);
/* If they started close, we might not have sent ours. */
assert(!lnchn->closing.our_script);
//redeemscript = bitcoin_redeem_single(lnchn, &lnchn->local.finalkey);
// lnchn->closing.our_script = lnchn->final_redeemscript;//scriptpubkey_p2sh(lnchn, redeemscript);
//tal_free(redeemscript);
/* FIXME-OLD #2:
*
* A node SHOULD send a `close_shutdown` (if it has
* not already) after receiving `close_shutdown`.
*/
db_set_our_closing_script(lnchn);
//queue_pkt_close_shutdown(lnchn);
if (lnchn->state == STATE_NORMAL_COMMITTING) {
newstate = STATE_SHUTDOWN_COMMITTING;
} else {
newstate = STATE_SHUTDOWN;
}
internal_set_lnchn_state(lnchn, newstate, __func__, true);
/* Catch case where we've exchanged and had no HTLCs anyway. */
// if (lnchn->closing.their_script && !committed_to_htlcs(lnchn))
// start_closing_in_transaction(lnchn);
return db_commit_transaction(lnchn) == NULL;
}
static bool want_feechange(const struct LNchannel *lnchn)
{
if (!state_is_normal(lnchn->state) && !state_is_shutdown(lnchn->state))
return false;
log_debug(lnchn->log, "Current fee_rate: %"PRIu64" want %"PRIu64,
lnchn->local.staging_cstate->fee_rate,
desired_commit_feerate(lnchn->dstate));
/* FIXME: Send fee changes when we want it */
return false;
}
struct commit_info *internal_new_commit_info(const tal_t *ctx, u64 commit_num)
{
struct commit_info *ci = tal(ctx, struct commit_info);
ci->commit_num = commit_num;
ci->tx = NULL;
ci->cstate = NULL;
ci->sig = NULL;
ci->order = (s64)-1LL;
return ci;
}
struct LNchannel *new_LNChannel(struct lightningd_state *dstate,
struct log *log)
{
struct LNchannel *lnchn = tal(dstate, struct LNchannel);
lnchn->state = STATE_INIT;
lnchn->state_height = 0;
lnchn->id = NULL;
lnchn->dstate = dstate;
lnchn->secrets = NULL;
lnchn->anchor.ok_depth = -1;
// lnchn->order_counter = 0;
lnchn->their_commitsigs = 0;
lnchn->closing.their_sig = NULL;
lnchn->closing.our_script = NULL;
lnchn->closing.their_script = NULL;
lnchn->closing.shutdown_order = (s64)-1LL;
lnchn->closing.closing_order = (s64)-1LL;
lnchn->closing.sigs_in = 0;
lnchn->onchain.tx = NULL;
lnchn->onchain.resolved = NULL;
lnchn->onchain.htlcs = NULL;
lnchn->commit_timer = NULL;
lnchn->fake_close = false;
lnchn->output_enabled = true;
lnchn->local.offer_anchor = false;
lnchn->broadcast_index = 0;
if (!blocks_to_rel_locktime(dstate->config.locktime_blocks,
&lnchn->local.locktime))
fatal("Could not convert locktime_blocks");
lnchn->local.mindepth = dstate->config.anchor_confirms;
lnchn->local.commit = lnchn->remote.commit = NULL;
lnchn->local.staging_cstate = lnchn->remote.staging_cstate = NULL;
lnchn->log = tal_steal(lnchn, log);
log_debug(lnchn->log, "New lnchn %p", lnchn);
lnchn->notify_fail_reason = NULL;
lnchn->remote.offer_anchor = false;
htlc_map_init(&lnchn->htlcs);
// memset(lnchn->feechanges, 0, sizeof(lnchn->feechanges));
shachain_init(&lnchn->their_preimages);
/* init runtime */
lnchn->rt.outsourcing_counter = 0;
lnchn->rt.outsourcing_lock = false;
lnchn->rt.prev_call = NULL;
lnchn->rt.changed_htlc_cache = NULL;
lnchn->rt.their_last_commit = NULL;
lnchn->rt.temp_errormsg = NULL;
memset(lnchn->rt.feechanges, 0, sizeof(lnchn->rt.feechanges));
// tal_add_destructor(lnchn, destroy_lnchn);
return lnchn;
}
//static void htlc_destroy(struct htlc *htlc)
//{
// if (!htlc_map_del(&htlc->lnchn->htlcs, htlc))
// fatal("Could not find htlc to destroy");
//}
struct htlc *internal_new_htlc(struct LNchannel *lnchn,
u64 msatoshi,
const struct sha256 *rhash,
u32 expiry, u32 routing,
enum htlc_state state)
{
struct htlc *h = tal(lnchn, struct htlc);
h->state = state;
h->msatoshi = msatoshi;
h->rhash = *rhash;
h->routing = routing;
h->r = NULL;
h->fail = NULL;
if (!blocks_to_abs_locktime(expiry, &h->expiry))
fatal("Invalid HTLC expiry %u", expiry);
h->src_expiry = NULL;
if (htlc_owner(h) == LOCAL) {
/* If we're paying, give it a little longer. will be adjust later if
we are in chain
*/
h->deadline = expiry
+ lnchn->dstate->config.min_htlc_expiry;
}
else
h->deadline = 0;
h->history[0] = lnchn->local.commit ? lnchn->local.commit->commit_num : 0;
h->history[1] = h->history[0] + 1;
h->in_commit_output[0] = h->in_commit_output[1]
= h->in_commit_output[2] = -1;
//htlc_map_add(&lnchn->htlcs, h);
//tal_add_destructor(h, htlc_destroy);
return h;
}
/*
rebuild all data related with other channel (currently only src expiry in htlcs)
and make some verification
*/
void reopen_LNChannel(struct LNchannel *lnchn)
{
struct htlc_map_iter it;
struct htlc *h, *srchtlc;
/* enum feechange_state i; */
//verify signature from remote
if (lnchn->remote.commit && lnchn->remote.commit->sig &&
!check_tx_sig(lnchn->remote.commit->tx, 0,
NULL,
lnchn->anchor.witnessscript,
&lnchn->remote.commitkey,
lnchn->remote.commit->sig))
{
log_broken(lnchn->log, "reopen check signature fail");
lnchn_fail(lnchn, __func__);
return;
}
//update htlc's state
for (h = htlc_map_first(&lnchn->htlcs, &it);
h;
h = htlc_map_next(&lnchn->htlcs, &it)) {
if (!htlc_route_is_chain(h))continue;
internal_htlc_update(lnchn, h);
}
}
//
///* Have any of our HTLCs passed their deadline? */
//static bool any_deadline_past(struct LNchannel *lnchn)
//{
// u32 height = get_block_height(lnchn->dstate->topology);
// struct htlc_map_iter it;
// struct htlc *h;
//
// for (h = htlc_map_first(&lnchn->htlcs, &it);
// h;
// h = htlc_map_next(&lnchn->htlcs, &it)) {
// if (htlc_is_dead(h))
// continue;
// if (htlc_owner(h) != LOCAL)
// continue;
// if (height >= h->deadline) {
// log_unusual_struct(lnchn->log,
// "HTLC %s deadline has passed",
// struct htlc, h);
// return true;
// }
// }
// return false;
//}
//
//static void check_htlc_expiry(struct LNchannel *lnchn)
//{
// u32 height = get_block_height(lnchn->dstate->topology);
// struct htlc_map_iter it;
// struct htlc *h;
//
// /* Check their currently still-existing htlcs for expiry */
// for (h = htlc_map_first(&lnchn->htlcs, &it);
// h;
// h = htlc_map_next(&lnchn->htlcs, &it)) {
// assert(!abs_locktime_is_seconds(&h->expiry));
//
// /* Only their consider HTLCs which are completely locked in. */
// if (h->state != RCVD_ADD_ACK_REVOCATION)
// continue;
//
// /* We give it an extra block, to avoid the worst of the
// * inter-node timing issues. */
// if (height <= abs_locktime_to_blocks(&h->expiry))
// continue;
//
// db_start_transaction(lnchn);
// /* This can fail only if we're in an error state. */
// command_htlc_set_fail(lnchn, h,
// REQUEST_TIMEOUT_408, "timed out");
// if (db_commit_transaction(lnchn) != NULL) {
// lnchn_fail(lnchn, __func__);
// return;
// }
// }
//
// /* FIXME-OLD #2:
// *
// * A node MUST NOT offer a HTLC after this deadline, and MUST
// * fail the connection if an HTLC which it offered is in
// * either node's current commitment transaction past this
// * deadline.
// */
//
// /* To save logic elsewhere (ie. to avoid signing a new commit with a
// * past-deadline HTLC) we also check staged HTLCs.
// */
// if (!state_is_normal(lnchn->state))
// return;
//
// if (any_deadline_past(lnchn))
// lnchn_fail(lnchn, __func__);
//}
//void notify_new_block(struct chain_topology *topo, unsigned int height)
//{
// struct lightningd_state *dstate = tal_parent(topo);
// /* This is where we check for anchor timeouts. */
// struct LNchannel *lnchn;
//
// list_for_each(&dstate->lnchns, lnchn, list) {
// if (!state_is_waiting_for_anchor(lnchn->state))
// continue;
//
// /* If we haven't seen anchor yet, we can timeout. */
// if (height >= lnchn->anchor.min_depth
// + dstate->config.anchor_onchain_wait
// + dstate->config.anchor_confirms) {
// queue_pkt_err(lnchn, pkt_err(lnchn, "Funding timeout"));
// set_lnchn_state(lnchn, STATE_ERR_ANCHOR_TIMEOUT, __func__,
// false);
// lnchn_breakdown(lnchn);
// }
// }
//}
///* Return earliest block we're interested in, or 0 for none. */
//u32 get_lnchn_min_block(struct lightningd_state *dstate)
//{
// u32 min_block = 0;
// struct LNchannel *lnchn;
//
// /* If loaded from database, go back to earliest possible lnchn anchor. */
// list_for_each(&dstate->lnchns, lnchn, list) {
// if (!lnchn->anchor.min_depth)
// continue;
// if (min_block == 0 || lnchn->anchor.min_depth < min_block)
// min_block = lnchn->anchor.min_depth;
// }
// return min_block;
//}
//static void json_close(struct command *cmd,
// const char *buffer, const jsmntok_t *params)
//{
// struct LNchannel *lnchn;
// jsmntok_t *lnchnidtok;
//
// if (!json_get_params(buffer, params,
// "lnchnid", &lnchnidtok,
// NULL)) {
// command_fail(cmd, "Need lnchnid");
// return;
// }
//
// lnchn = find_lnchn_json(cmd->dstate, buffer, lnchnidtok);
// if (!lnchn) {
// command_fail(cmd, "Could not find lnchn with that lnchnid");
// return;
// }
//
// if (!state_is_normal(lnchn->state) && !state_is_opening(lnchn->state)) {
// command_fail(cmd, "lnchn is already closing: state %s",
// state_name(lnchn->state));
// return;
// }
//
// if (!lnchn_start_shutdown(lnchn)) {
// command_fail(cmd, "Database error");
// return;
// }
// /* FIXME: Block until closed! */
// command_success(cmd, null_response(cmd));
//}
//static void json_feerate(struct command *cmd,
// const char *buffer, const jsmntok_t *params)
//{
// jsmntok_t *feeratetok;
// u64 feerate;
//
// if (!json_get_params(buffer, params,
// "feerate", &feeratetok,
// NULL)) {
// command_fail(cmd, "Need feerate");
// return;
// }
//
// if (!json_tok_u64(buffer, feeratetok, &feerate)) {
// command_fail(cmd, "Invalid feerate");
// return;
// }
// log_debug(cmd->jcon->log, "Fee rate changed to %"PRIu64, feerate);
// cmd->dstate->topology->default_fee_rate = feerate;
//
// command_success(cmd, null_response(cmd));
//}
void lnchn_object_release(const void * p)
{
tal_free(p);
}
const struct pubkey* lnchn_channel_pubkey(const struct LNchannel* chn)
{
return chn->id;
}
void lnchn_channel_commits(const struct LNchannel* chn, const struct sha256_double* commitids[3])
{
commitids[0] = chn->local.commit ? &chn->local.commit->txid : NULL;
commitids[1] = chn->remote.commit ? &chn->remote.commit->txid : NULL;
commitids[2] = chn->rt.their_last_commit ? &chn->rt.their_last_commit->txid : NULL;
}
int lnchn_channel_state(const struct LNchannel* chn)
{
return chn->state;
}
const struct htlc* lnchn_channel_htlc(const struct LNchannel* chn, const struct sha256* key)
{
return htlc_get_any(&chn->htlcs, key);
}
struct LNchannel* lnchn_channel_copy(const struct LNchannel* chn,
unsigned int copy_mask, void *tal_ctx)
{
struct LNchannel *lnchn = talz((tal_t*)tal_ctx, struct LNchannel);
htlc_map_init(&lnchn->htlcs);
shachain_init(&lnchn->their_preimages);
lnchn_channel_update(lnchn, chn, copy_mask);
return lnchn;
}
void lnchn_channel_update(struct LNchannel* dst, const struct LNchannel* src, unsigned int copy_mask)
{
#define SIMPLE_CREATE(NAME, TYPE) if(!dst->NAME){dst->NAME = tal_dup(dst, TYPE, src->NAME);}\
else memcpy(dst->NAME, src->NAME, sizeof(TYPE))
#define SIMPLE_REPLACE(NAME, TYPE) tal_free(dst->NAME),dst->NAME = tal_dup(dst, TYPE, src->NAME)
#define SIMPLE_REPLACEARR(NAME, TYPE) tal_free(dst->NAME),dst->NAME = tal_dup_arr(dst, TYPE, src->NAME, (tal_len(src->NAME)), 0)
#define SIMPLE_COPY(NAME) memcpy(&dst->NAME, &src->NAME, sizeof(dst->NAME))
//trivial
dst->state = src->state;
dst->state_height = src->state_height;
SIMPLE_CREATE(id, struct pubkey);
if (src->notify_fail_reason)SIMPLE_REPLACEARR(notify_fail_reason, char);
if (src->rt.temp_errormsg)SIMPLE_REPLACEARR(rt.temp_errormsg, u8);
if (copy_mask & LNchn_copy_anchor)
{
dst->anchor.txid = src->anchor.txid;
dst->anchor.index = src->anchor.index;
dst->anchor.satoshis = src->anchor.satoshis;
dst->anchor.min_depth = src->anchor.min_depth;
dst->anchor.ok_depth = src->anchor.ok_depth;
dst->anchor.ours = src->anchor.ours;
if (src->anchor.input)SIMPLE_REPLACE(anchor.input, struct anchor_input);
SIMPLE_REPLACEARR(anchor.witnessscript, u8);
/*TODO: need bitcoin tx copy*/
dst->anchor.tx = NULL;
}
if (copy_mask & LNchn_copy_ourcommit)
{
SIMPLE_COPY(local);
if (src->local.commit) {
SIMPLE_REPLACE(local.commit, struct commit_info);
SIMPLE_REPLACE(local.commit->cstate, struct channel_state);
SIMPLE_REPLACE(local.commit->sig, ecdsa_signature);
SIMPLE_REPLACE(local.staging_cstate, struct channel_state);
dst->local.commit->tx = NULL;
}
}
if (copy_mask & LNchn_copy_theircommit)
{
SIMPLE_COPY(remote);
if (src->remote.commit) {
SIMPLE_REPLACE(remote.commit, struct commit_info);
SIMPLE_REPLACE(remote.commit->cstate, struct channel_state);
SIMPLE_REPLACE(remote.commit->sig, ecdsa_signature);
SIMPLE_REPLACE(remote.staging_cstate, struct channel_state);
dst->remote.commit->tx = NULL;
}
if (src->rt.their_last_commit) {
SIMPLE_REPLACE(rt.their_last_commit, struct commit_info);
SIMPLE_REPLACE(rt.their_last_commit->cstate, struct channel_state);
SIMPLE_REPLACE(rt.their_last_commit->sig, ecdsa_signature);
dst->rt.their_last_commit->tx = NULL;
}
}
if (copy_mask & LNchn_copy_closing)
{
SIMPLE_COPY(closing);
if (src->closing.their_sig) {
SIMPLE_REPLACE(closing.their_sig, ecdsa_signature);
SIMPLE_REPLACEARR(closing.our_script, u8);
SIMPLE_REPLACEARR(closing.our_script, u8);
}
}
}
struct htlc* lnchn_htlc_copy(const struct htlc* src, void *tal_ctx)
{
struct htlc *dst = talz((tal_t*)tal_ctx, struct htlc);
*dst = *src;
if (src->r) { SIMPLE_CREATE(r, struct preimage); }
if (src->fail) { SIMPLE_REPLACEARR(fail, u8); }
if (src->src_expiry) { SIMPLE_REPLACE(src_expiry, struct abs_locktime); }
return dst;
}
int lnchn_htlc_route_is_upstream(const struct htlc *h) { return h->routing & 1; }
struct msg_htlc_entry* lnchn_htlc_entry_create(const struct msg_htlc_entry* from, unsigned int size, void *tal_ctx)
{
struct msg_htlc_entry* ret = tal_arrz(tal_ctx, struct msg_htlc_entry, size);
if (from) {
size_t i;
for (i = 0; i < size; ++i) {
ret[i].rhash = tal_dup(tal_ctx, struct sha256, from[i].rhash);
ret[i].action_type = from[i].action_type;
if (from[i].action_type == 1 /*add*/) {
ret[i].action.add = from[i].action.add;
}
else if (from[i].action.del.r){
ret[i].action.del.r = tal_dup(tal_ctx, struct preimage, from[i].action.del.r);
}
else {
ret[i].action.del.failflag = from[i].action.del.failflag;
ret[i].action.del.fail = tal_dup_arr(tal_ctx, u8, ret[i].action.del.fail,
tal_len(ret[i].action.del.fail), 0);
}
}
}
return ret;
}