forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_protocol.c
1428 lines (1206 loc) · 37.1 KB
/
test_protocol.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
/* Simple simulator for protocol. */
#include "config.h"
#include <assert.h>
#include <ccan/array_size/array_size.h>
#include <ccan/err/err.h>
#include <ccan/opt/opt.h>
#include <ccan/read_write_all/read_write_all.h>
#include <ccan/short_types/short_types.h>
#include <ccan/str/str.h>
#include <ccan/structeq/structeq.h>
#include <ccan/tal/tal.h>
#include <ccan/tal/str/str.h>
#include <common/utils.h>
#include <inttypes.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/uio.h>
#include <unistd.h>
#define A_LINEX 100
#define B_LINEX 245
#define A_TEXTX 95
#define B_TEXTX 250
#define LINE_HEIGHT 5
#define TEXT_HEIGHT 4
#define STEP_HEIGHT 10
#define LETTER_WIDTH 3
#define TEXT_STYLE "style=\"font-size:4;\""
static bool verbose = false;
struct commit_tx {
/* inhtlcs = htlcs they offered, outhtlcs = htlcs we offered */
u32 inhtlcs, outhtlcs;
/* This is a simple counter, reflecting fee updates. */
u32 fee;
};
/* We keep one for them, one for us. */
struct commit_info {
struct commit_info *prev;
/* How deep we are */
unsigned int number;
/* Have sent/received revocation secret. */
bool revoked;
/* Have their signature, ie. can be broadcast */
bool counterparty_signed;
u16 pad;
/* num_commit_or_revoke when we sent/received this. */
size_t order;
};
/* A "signature" is a copy of the commit tx state, for easy diagnosis. */
struct signature {
struct commit_tx f;
};
/* What are we doing: adding or removing? */
#define ADDING 0x1000
#define REMOVING 0x2000
#define PENDING 0x001 /* Change is pending. */
#define COMMITTED 0x002 /* HTLC is in commit_tx */
#define REVOKED 0x004 /* Old pre-change tx revoked */
#define OWNER 0x020 /* This side owns it */
#define OURS LOCAL(OWNER)
#define THEIRS REMOTE(OWNER)
#define LOCAL_ 0
#define REMOTE_ 6
#define SIDE(flag,local_or_remote) ((flag) << local_or_remote)
#define OTHER_SIDE(flag,local_or_remote) ((flag) << (6 - local_or_remote))
#define LOCAL(flag) SIDE(flag, LOCAL_)
#define REMOTE(flag) SIDE(flag, REMOTE_)
enum htlc_state {
NONEXISTENT = 0,
/* When we add a new htlc, it goes in this order. */
SENT_ADD_HTLC = ADDING + OURS + REMOTE(PENDING),
SENT_ADD_COMMIT = SENT_ADD_HTLC - REMOTE(PENDING) + REMOTE(COMMITTED),
RECV_ADD_REVOCATION = SENT_ADD_COMMIT + REMOTE(REVOKED),
RECV_ADD_ACK_COMMIT = RECV_ADD_REVOCATION + LOCAL(COMMITTED),
SENT_ADD_ACK_REVOCATION = RECV_ADD_ACK_COMMIT + LOCAL(REVOKED) - ADDING,
/* When they remove an htlc, it goes from SENT_ADD_ACK_REVOCATION: */
RECV_REMOVE_HTLC = REMOVING + OURS + LOCAL(PENDING)
+ LOCAL(COMMITTED) + REMOTE(COMMITTED),
RECV_REMOVE_COMMIT = RECV_REMOVE_HTLC - LOCAL(PENDING) - LOCAL(COMMITTED),
SENT_REMOVE_REVOCATION = RECV_REMOVE_COMMIT + LOCAL(REVOKED),
SENT_REMOVE_ACK_COMMIT = SENT_REMOVE_REVOCATION - REMOTE(COMMITTED),
RECV_REMOVE_ACK_REVOCATION = SENT_REMOVE_ACK_COMMIT - REMOVING + REMOTE(REVOKED),
/* When they add a new htlc, it goes in this order. */
RECV_ADD_HTLC = ADDING + THEIRS + LOCAL(PENDING),
RECV_ADD_COMMIT = RECV_ADD_HTLC - LOCAL(PENDING) + LOCAL(COMMITTED),
SENT_ADD_REVOCATION = RECV_ADD_COMMIT + LOCAL(REVOKED),
SENT_ADD_ACK_COMMIT = SENT_ADD_REVOCATION + REMOTE(COMMITTED),
RECV_ADD_ACK_REVOCATION = SENT_ADD_ACK_COMMIT + REMOTE(REVOKED),
/* When we remove an htlc, it goes from RECV_ADD_ACK_REVOCATION: */
SENT_REMOVE_HTLC = REMOVING + THEIRS + REMOTE(PENDING)
+ LOCAL(COMMITTED) + REMOTE(COMMITTED),
SENT_REMOVE_COMMIT = SENT_REMOVE_HTLC - REMOTE(PENDING) - REMOTE(COMMITTED),
RECV_REMOVE_REVOCATION = SENT_REMOVE_COMMIT + REMOTE(REVOKED),
RECV_REMOVE_ACK_COMMIT = RECV_REMOVE_REVOCATION - LOCAL(COMMITTED),
SENT_REMOVE_ACK_REVOCATION = RECV_REMOVE_ACK_COMMIT + LOCAL(REVOKED) - REMOVING
};
static const char *htlc_statename(enum htlc_state state)
{
switch (state) {
case NONEXISTENT: return "NONEXISTENT";
case SENT_ADD_HTLC: return "SENT_ADD_HTLC";
case SENT_ADD_COMMIT: return "SENT_ADD_COMMIT";
case RECV_ADD_REVOCATION: return "RECV_ADD_REVOCATION";
case RECV_ADD_ACK_COMMIT: return "RECV_ADD_ACK_COMMIT";
case SENT_ADD_ACK_REVOCATION: return "SENT_ADD_ACK_REVOCATION";
case RECV_REMOVE_HTLC: return "RECV_REMOVE_HTLC";
case RECV_REMOVE_COMMIT: return "RECV_REMOVE_COMMIT";
case SENT_REMOVE_REVOCATION: return "SENT_REMOVE_REVOCATION";
case SENT_REMOVE_ACK_COMMIT: return "SENT_REMOVE_ACK_COMMIT";
case RECV_REMOVE_ACK_REVOCATION: return "RECV_REMOVE_ACK_REVOCATION";
case RECV_ADD_HTLC: return "RECV_ADD_HTLC";
case RECV_ADD_COMMIT: return "RECV_ADD_COMMIT";
case SENT_ADD_REVOCATION: return "SENT_ADD_REVOCATION";
case SENT_ADD_ACK_COMMIT: return "SENT_ADD_ACK_COMMIT";
case RECV_ADD_ACK_REVOCATION: return "RECV_ADD_ACK_REVOCATION";
case SENT_REMOVE_HTLC: return "SENT_REMOVE_HTLC";
case SENT_REMOVE_COMMIT: return "SENT_REMOVE_COMMIT";
case RECV_REMOVE_REVOCATION: return "RECV_REMOVE_REVOCATION";
case RECV_REMOVE_ACK_COMMIT: return "RECV_REMOVE_ACK_COMMIT";
case SENT_REMOVE_ACK_REVOCATION: return "SENT_REMOVE_ACK_REVOCATION";
}
return tal_fmt(NULL, "UNKNOWN STATE %i", state);
}
static const char *htlc_stateflags(const tal_t *ctx, enum htlc_state state)
{
char *flags = tal_strdup(ctx, "");
#define ADD_STATE(flags, flag) \
if (state & flag) \
tal_append_fmt(&flags, #flag ",");
ADD_STATE(flags, ADDING);
ADD_STATE(flags, REMOVING);
ADD_STATE(flags, OURS);
ADD_STATE(flags, THEIRS);
ADD_STATE(flags, LOCAL(PENDING));
ADD_STATE(flags, LOCAL(COMMITTED));
ADD_STATE(flags, LOCAL(REVOKED));
ADD_STATE(flags, REMOTE(PENDING));
ADD_STATE(flags, REMOTE(COMMITTED));
ADD_STATE(flags, REMOTE(REVOKED));
if (strends(flags, ","))
flags[strlen(flags)-1] = '\0';
return flags;
}
struct htlc {
enum htlc_state state;
/* 0 means this is actually a new fee, not a HTLC. */
unsigned int id;
};
static u32 htlc_mask(unsigned int htlc)
{
if (htlc > 32)
errx(1, "HTLC number %u too large", htlc);
if (!htlc)
errx(1, "HTLC number can't be zero");
return (1U << (htlc-1));
}
/* Make commit tx for local/remote */
static struct commit_tx make_commit_tx(struct htlc **htlcs, int local_or_remote)
{
size_t i, n = tal_count(htlcs);
int committed_flag = SIDE(COMMITTED, local_or_remote);
struct commit_tx tx = { 0, 0, 0 };
for (i = 0; i < n; i++) {
if (!(htlcs[i]->state & committed_flag))
continue;
if (!(htlcs[i]->state & SIDE(OWNER, local_or_remote))) {
/* We don't apply fee changes to each other. */
if (htlcs[i]->id)
tx.outhtlcs |= htlc_mask(htlcs[i]->id);
} else {
if (!htlcs[i]->id)
tx.fee++;
else
tx.inhtlcs |= htlc_mask(htlcs[i]->id);
}
}
return tx;
}
struct database {
/* This keeps *all* our HTLCs, including expired ones. */
size_t num_htlcs;
struct htlc htlcs[100];
/* This counts the number of received commit and revocation pkts. */
size_t last_recv;
size_t last_sent;
/* We keep remote_prev because it might not be revoked, and this
* makes our receive_revoke logic simpler. */
struct commit_info local, remote, remote_prev;
};
struct peer {
const char *name;
int infd, outfd, cmdfd, cmddonefd;
/* For drawing svg */
char *info;
/* What we save on disk. */
struct database db;
/* All htlcs. */
struct htlc **htlcs;
/* Last one is the one we're changing. */
struct commit_info *local, *remote;
};
static void db_update_htlc(struct database *db, const struct htlc *htlc)
{
size_t i;
for (i = 0; i < db->num_htlcs; i++) {
if ((db->htlcs[i].state & (OURS|THEIRS))
!= (htlc->state & (OURS|THEIRS)))
continue;
/* FIXME: This isn't quite right for multiple fee changes. */
if (db->htlcs[i].id == htlc->id)
break;
}
if (i == db->num_htlcs) {
db->num_htlcs++;
if (db->num_htlcs > ARRAY_SIZE(db->htlcs))
errx(1, "Too many htlcs");
}
db->htlcs[i] = *htlc;
}
static void db_recv_local_commit(struct database *db,
const struct commit_info *ci)
{
db->last_recv++;
db->local = *ci;
}
static void db_send_remote_commit(struct peer *peer,
struct database *db,
const struct commit_info *ci,
struct signature sig)
{
if (ci->prev)
db->remote_prev = *ci->prev;
db->remote = *ci;
db->remote.order = ++db->last_sent;
}
static void db_send_local_revoke(struct database *db,
const struct commit_info *ci)
{
db->last_sent++;
}
static void db_recv_remote_revoke(struct database *db,
const struct commit_info *ci)
{
assert(ci->revoked);
db->last_recv++;
db->remote_prev.revoked = true;
/* A real db would save the previous revocation hash here too */
}
static struct htlc *find_htlc(struct peer *peer, unsigned int htlc_id, int side)
{
size_t i, n = tal_count(peer->htlcs);
for (i = 0; i < n; i++) {
if ((peer->htlcs[i]->state & side)
&& peer->htlcs[i]->id == htlc_id)
return peer->htlcs[i];
}
return NULL;
}
static struct htlc *new_htlc(struct peer *peer, unsigned int htlc_id, int side)
{
size_t n = tal_count(peer->htlcs);
/* Fee changes don't have to be unique. */
if (htlc_id && find_htlc(peer, htlc_id, side))
errx(1, "%s: %s duplicate new htlc %u", peer->name,
side == OURS ? "Our" : "Their", htlc_id);
tal_resize(&peer->htlcs, n+1);
peer->htlcs[n] = tal(peer, struct htlc);
peer->htlcs[n]->state = NONEXISTENT;
peer->htlcs[n]->id = htlc_id;
return peer->htlcs[n];
}
static void htlc_changestate(struct peer *peer,
struct htlc *htlc,
bool commit,
enum htlc_state old,
enum htlc_state new)
{
if (htlc->state != old)
errx(1, "%s: htlc was in state %s not %s", peer->name,
htlc_statename(htlc->state), htlc_statename(old));
if (htlc->id) {
if (verbose)
printf("%s: HTLC %u -> %s\n",
peer->name, htlc->id, htlc_statename(new));
tal_append_fmt(&peer->info, "%u:%s\n",
htlc->id, htlc_statename(new));
} else {
if (verbose)
printf("%s: FEE -> %s\n",
peer->name, htlc_statename(new));
tal_append_fmt(&peer->info, "FEE:%s\n",
htlc_statename(new));
}
htlc->state = new;
if (commit)
db_update_htlc(&peer->db, htlc);
}
struct state_table {
enum htlc_state from, to;
};
static bool change_htlcs_(struct peer *peer, bool commit,
const struct state_table *table,
size_t n_table)
{
size_t i, n = tal_count(peer->htlcs);
bool changed = false;
for (i = 0; i < n; i++) {
size_t t;
for (t = 0; t < n_table; t++) {
if (peer->htlcs[i]->state == table[t].from) {
htlc_changestate(peer, peer->htlcs[i], commit,
table[t].from, table[t].to);
changed = true;
break;
}
}
}
return changed;
}
#define change_htlcs(peer, table, commit) \
change_htlcs_((peer), (commit), (table), ARRAY_SIZE(table))
static struct commit_info *new_commit_info(const struct peer *peer,
struct commit_info *prev)
{
struct commit_info *ci = tal(peer, struct commit_info);
ci->prev = prev;
ci->revoked = false;
ci->counterparty_signed = false;
ci->pad = 0;
ci->order = 0;
if (prev)
ci->number = prev->number + 1;
else
ci->number = 0;
return ci;
}
static struct signature commit_sig(const struct commit_tx *commit_tx)
{
struct signature sig;
sig.f = *commit_tx;
return sig;
}
static void write_out(int fd, const void *p, size_t len)
{
if (!write_all(fd, p, len))
err(1, "Writing to peer");
}
static void dump_htlcs(struct htlc **htlcs,
const char *prefix,
bool verbose,
int flags_inc, int flags_exc)
{
size_t i, n = tal_count(htlcs);
const tal_t *ctx = tal_tmpctx(htlcs);
bool printed = false;
for (i = 0; i < n; i++) {
if ((htlcs[i]->state & flags_inc) != flags_inc)
continue;
if (htlcs[i]->state & flags_exc)
continue;
if (!htlcs[i]->id && !verbose)
continue;
if (!printed) {
printf("%s", prefix);
printed = true;
}
if (!htlcs[i]->id)
printf(" FEE");
else
printf(" %u", htlcs[i]->id);
if (verbose) {
printf(" (%s - %s)",
htlc_statename(htlcs[i]->state),
htlc_stateflags(ctx, htlcs[i]->state));
}
}
if (printed)
printf("\n");
tal_free(ctx);
}
static void dump_commit_info(const struct peer *peer,
const struct commit_info *ci,
int local_or_remote)
{
struct commit_tx tx;
int committed_flag = SIDE(COMMITTED, local_or_remote);
tx = make_commit_tx(peer->htlcs, local_or_remote);
printf(" Commit %u:\n", ci->number);
dump_htlcs(peer->htlcs, " Our htlcs:", false,
OURS|committed_flag, 0);
dump_htlcs(peer->htlcs, " Their htlcs:", false,
THEIRS|committed_flag, 0);
/* Don't clutter output if fee level untouched. */
if (tx.fee)
printf(" Fee level %u\n", tx.fee);
dump_htlcs(peer->htlcs, "Pending unacked:", true,
SIDE(PENDING, local_or_remote), committed_flag);
dump_htlcs(peer->htlcs, "Pending acked:", true,
OTHER_SIDE(COMMITTED, local_or_remote), committed_flag);
if (ci->counterparty_signed)
printf(" SIGNED\n");
if (ci->revoked)
printf(" REVOKED\n");
fflush(stdout);
}
static void dump_peer(const struct peer *peer, bool all)
{
printf("LOCAL COMMIT:\n");
dump_commit_info(peer, peer->local, LOCAL_);
printf("REMOTE COMMIT:\n");
dump_commit_info(peer, peer->remote, REMOTE_);
if (all)
dump_htlcs(peer->htlcs, "OLD HTLCs:", true,
0, LOCAL(COMMITTED)|REMOTE(COMMITTED));
}
static void read_in(int fd, void *p, size_t len)
{
alarm(5);
if (!read_all(fd, p, len))
err(1, "Reading from peer");
alarm(0);
}
static void read_peer(struct peer *peer, const char *str, const char *cmd)
{
char *p = tal_arr(peer, char, strlen(str)+1);
read_in(peer->infd, p, strlen(str));
p[strlen(str)] = '\0';
if (!streq(p, str))
errx(1, "%s: %s: Expected %s from peer, got %s",
peer->name, cmd, str, p);
tal_free(p);
}
static void PRINTF_FMT(2,3) record_send(struct peer *peer, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
tal_append_fmt(&peer->info, ">");
tal_append_vfmt(&peer->info, fmt, ap);
tal_append_fmt(&peer->info, "\n");
va_end(ap);
if (verbose) {
va_start(ap, fmt);
printf("%s: SEND ", peer->name);
vprintf(fmt, ap);
printf("\n");
va_end(ap);
}
}
static void PRINTF_FMT(2,3) record_recv(struct peer *peer, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
tal_append_fmt(&peer->info, "<");
tal_append_vfmt(&peer->info, fmt, ap);
tal_append_fmt(&peer->info, "\n");
va_end(ap);
if (verbose) {
va_start(ap, fmt);
printf("%s: RECEIVE ", peer->name);
vprintf(fmt, ap);
printf("\n");
va_end(ap);
}
}
static void xmit_add_htlc(struct peer *peer, const struct htlc *h)
{
record_send(peer, "add_htlc %u", h->id);
write_out(peer->outfd, "+", 1);
write_out(peer->outfd, &h->id, sizeof(h->id));
}
static void xmit_remove_htlc(struct peer *peer, const struct htlc *h)
{
record_send(peer, "fulfill_htlc %u", h->id);
write_out(peer->outfd, "-", 1);
write_out(peer->outfd, &h->id, sizeof(h->id));
}
static void xmit_feechange(struct peer *peer)
{
record_send(peer, "update_fee");
write_out(peer->outfd, "F", 1);
}
static void xmit_commit(struct peer *peer, struct signature sig)
{
record_send(peer, "update_commit");
write_out(peer->outfd, "C", 1);
write_out(peer->outfd, &sig, sizeof(sig));
}
static void xmit_revoke(struct peer *peer, unsigned int number)
{
record_send(peer, "update_revocation");
write_out(peer->outfd, "R", 1);
write_out(peer->outfd, &number, sizeof(number));
}
static void send_offer(struct peer *peer, unsigned int htlc)
{
struct htlc *h = new_htlc(peer, htlc, OURS);
htlc_changestate(peer, h, false, NONEXISTENT, SENT_ADD_HTLC);
xmit_add_htlc(peer, h);
}
static void send_remove(struct peer *peer, unsigned int htlc)
{
struct htlc *h = find_htlc(peer, htlc, THEIRS);
if (!h)
errx(1, "%s: send_remove: htlc %u does not exist",
peer->name, htlc);
htlc_changestate(peer, h, false, RECV_ADD_ACK_REVOCATION, SENT_REMOVE_HTLC);
xmit_remove_htlc(peer, h);
}
static void send_feechange(struct peer *peer)
{
struct htlc *fee = new_htlc(peer, 0, OURS);
htlc_changestate(peer, fee, false, NONEXISTENT, SENT_ADD_HTLC);
xmit_feechange(peer);
}
/*
* We don't enforce the rule that commits have to wait for revoke response
* before the next one.
*/
static struct commit_info *last_unrevoked(struct commit_info *ci)
{
struct commit_info *next = NULL;
/* If this is already revoked, all are. */
if (ci->revoked)
return NULL;
/* Find revoked commit; one we hit before that was last unrevoked. */
for (; ci; next = ci, ci = ci->prev) {
if (ci->revoked)
break;
}
return next;
}
static void send_commit(struct peer *peer)
{
struct commit_tx tx;
struct signature sig;
static const struct state_table changes[] = {
{ SENT_ADD_HTLC, SENT_ADD_COMMIT },
{ SENT_REMOVE_REVOCATION, SENT_REMOVE_ACK_COMMIT },
{ SENT_ADD_REVOCATION, SENT_ADD_ACK_COMMIT},
{ SENT_REMOVE_HTLC, SENT_REMOVE_COMMIT}
};
/* FIXME-OLD #2:
*
* An implementation MAY choose not to send an `update_commit`
* until it receives the `update_revocation` response to the
* previous `update_commit`, so there is only ever one
* unrevoked local commitment. */
if (peer->remote->prev && !peer->remote->prev->revoked)
errx(1, "%s: commit: must wait for previous commit", peer->name);
/* FIXME-OLD #2:
*
* ...a sending node MUST apply all remote acked and unacked
* changes except unacked fee changes to the remote commitment
* before generating `sig`.
*/
if (!change_htlcs(peer, changes, true)) {
/* FIXME-OLD #2:
*
* A node MUST NOT send an `update_commit` message which does
* not include any updates.
*/
errx(1, "%s: commit: no changes to commit", peer->name);
}
tx = make_commit_tx(peer->htlcs, REMOTE_);
sig = commit_sig(&tx);
peer->remote = new_commit_info(peer, peer->remote);
peer->remote->counterparty_signed = true;
db_send_remote_commit(peer, &peer->db, peer->remote, sig);
/* Tell other side about commit and result (it should agree!) */
xmit_commit(peer, sig);
}
static void receive_revoke(struct peer *peer, u32 number)
{
static const struct state_table changes[] = {
{ SENT_ADD_COMMIT, RECV_ADD_REVOCATION },
{ SENT_REMOVE_ACK_COMMIT, RECV_REMOVE_ACK_REVOCATION },
{ SENT_ADD_ACK_COMMIT, RECV_ADD_ACK_REVOCATION },
{ SENT_REMOVE_COMMIT, RECV_REMOVE_REVOCATION }
};
struct commit_info *ci = last_unrevoked(peer->remote);
if (!ci)
errx(1, "%s: receive_revoke: no commit to revoke", peer->name);
if (ci->number != number)
errx(1, "%s: receive_revoke: revoked %u but %u is next",
peer->name, number, ci->number);
/* This shouldn't happen if we don't allow multiple commits. */
if (ci != peer->remote->prev)
errx(1, "%s: receive_revoke: always revoke previous?",
peer->name);
if (ci->revoked)
errx(1, "%s: receive_revoke: already revoked?", peer->name);
record_recv(peer, "update_revocation");
ci->revoked = true;
if (!ci->counterparty_signed)
errx(1, "%s: receive_revoke: revoked unsigned commit?",
peer->name);
if (!change_htlcs(peer, changes, true))
errx(1, "%s: receive_revoke: no changes?", peer->name);
db_recv_remote_revoke(&peer->db, ci);
}
/* FIXME-OLD #2:
*
* the receiving node MUST add the HTLC addition to the unacked
* changeset for its local commitment.
*/
static void receive_offer(struct peer *peer, unsigned int htlc)
{
struct htlc *h = new_htlc(peer, htlc, THEIRS);
htlc_changestate(peer, h, false, NONEXISTENT, RECV_ADD_HTLC);
record_recv(peer, "add_htlc %u", h->id);
}
/* FIXME-OLD #2:
*
* the receiving node MUST add the HTLC fulfill/fail to the unacked
* changeset for its local commitment.
*/
static void receive_remove(struct peer *peer, unsigned int htlc)
{
struct htlc *h = find_htlc(peer, htlc, OURS);
if (!h)
errx(1, "%s: recv_remove: htlc %u does not exist",
peer->name, htlc);
htlc_changestate(peer, h, false, SENT_ADD_ACK_REVOCATION, RECV_REMOVE_HTLC);
record_recv(peer, "fulfill_htlc %u", h->id);
}
/* FIXME-OLD #2:
*
* the receiving node MUST add the fee change to the unacked changeset
* for its local commitment.
*/
static void receive_feechange(struct peer *peer)
{
struct htlc *fee = new_htlc(peer, 0, THEIRS);
htlc_changestate(peer, fee, false, NONEXISTENT, RECV_ADD_HTLC);
record_recv(peer, "update_fee");
}
/* Send revoke.
* - Queue changes to them.
*/
static void send_revoke(struct peer *peer, struct commit_info *ci)
{
static const struct state_table changes[] = {
{ RECV_ADD_ACK_COMMIT, SENT_ADD_ACK_REVOCATION },
{ RECV_REMOVE_COMMIT, SENT_REMOVE_REVOCATION },
{ RECV_ADD_COMMIT, SENT_ADD_REVOCATION },
{ RECV_REMOVE_ACK_COMMIT, SENT_REMOVE_ACK_REVOCATION }
};
/* We always revoke in order. */
assert(!ci->prev || ci->prev->revoked);
assert(ci->counterparty_signed);
assert(!ci->revoked);
ci->revoked = true;
if (!change_htlcs(peer, changes, true))
errx(1, "%s: update_revocation: no changes?", peer->name);
db_send_local_revoke(&peer->db, ci);
xmit_revoke(peer, ci->number);
}
/* Receive commit:
* - Apply changes to us.
*/
static void receive_commit(struct peer *peer, const struct signature *sig)
{
struct commit_tx commit_tx;
struct signature oursig;
static const struct state_table changes[] = {
{ RECV_ADD_REVOCATION, RECV_ADD_ACK_COMMIT },
{ RECV_REMOVE_HTLC, RECV_REMOVE_COMMIT },
{ RECV_ADD_HTLC, RECV_ADD_COMMIT },
{ RECV_REMOVE_REVOCATION, RECV_REMOVE_ACK_COMMIT }
};
record_recv(peer, "update_commit");
/* FIXME-OLD #2:
*
* A node MUST NOT send an `update_commit` message which does
* not include any updates.
*/
if (!change_htlcs(peer, changes, true))
errx(1, "%s: receive_commit: no changes to commit", peer->name);
commit_tx = make_commit_tx(peer->htlcs, LOCAL_);
oursig = commit_sig(&commit_tx);
if (!structeq(sig, &oursig))
errx(1, "%s: Commit state %#x/%#x/%u, they gave %#x/%#x/%u",
peer->name,
sig->f.inhtlcs, sig->f.outhtlcs, sig->f.fee,
oursig.f.inhtlcs, oursig.f.outhtlcs, oursig.f.fee);
peer->local = new_commit_info(peer, peer->local);
peer->local->counterparty_signed = true;
db_recv_local_commit(&peer->db, peer->local);
send_revoke(peer, peer->local->prev);
}
static void resend_updates(struct peer *peer)
{
size_t i;
/* Re-transmit our add, removes and fee changes. */
for (i = 0; i < tal_count(peer->htlcs); i++) {
switch (peer->htlcs[i]->state) {
case SENT_ADD_COMMIT:
if (peer->htlcs[i]->id)
xmit_add_htlc(peer, peer->htlcs[i]);
else
xmit_feechange(peer);
break;
case SENT_REMOVE_COMMIT:
xmit_remove_htlc(peer, peer->htlcs[i]);
break;
default:
break;
}
}
}
static void restore_state(struct peer *peer)
{
size_t i, sent, num_revokes, revoke_idx;
peer->htlcs = tal_arr(peer, struct htlc *, peer->db.num_htlcs);
for (i = 0; i < peer->db.num_htlcs; i++) {
peer->htlcs[i] = tal_dup(peer->htlcs, struct htlc,
&peer->db.htlcs[i]);
if (verbose)
printf("%s: HTLC %u %s\n",
peer->name, peer->htlcs[i]->id,
htlc_statename(peer->htlcs[i]->state));
}
*peer->local = peer->db.local;
peer->local->prev = NULL;
*peer->remote = peer->db.remote;
if (peer->remote->number != 0) {
peer->remote->prev = tal(peer, struct commit_info);
*peer->remote->prev = peer->db.remote_prev;
peer->remote->prev->prev = NULL;
} else
peer->remote->prev = NULL;
/* Tell peer where we've received. */
write_out(peer->outfd, "!", 1);
write_out(peer->outfd, &peer->db.last_recv, sizeof(peer->db.last_recv));
/* Find out where peer is up to. */
read_peer(peer, "!", "restore");
read_in(peer->infd, &sent, sizeof(sent));
if (verbose)
printf("%s: peer is up to %zu/%zu: last commit at %zu\n",
peer->name, sent, peer->db.last_sent,peer->remote->order);
if (sent > peer->db.last_sent)
errx(1, "%s: peer said up to %zu, but we only sent %zu",
peer->name, sent, peer->db.last_sent);
/* All up to date? Nothing to do. */
if (sent == peer->db.last_sent)
return;
/* Since we wait for revocation replies, only one of the missing
* could be our update; the rest must be revocations. */
num_revokes = peer->db.last_sent - sent - (sent < peer->remote->order);
if (num_revokes > peer->local->number)
errx(1, "%s: can't rexmit %zu revoke txs at %u",
peer->name, num_revokes, peer->local->number);
revoke_idx = peer->local->number - num_revokes;
/* If we sent a revocation before the commit. */
if (sent + 1 < peer->remote->order) {
xmit_revoke(peer, revoke_idx++);
num_revokes--;
sent++;
}
/* If they didn't get the last commit, re-send all. */
if (sent + 1 == peer->remote->order) {
struct commit_tx tx;
struct signature sig;
resend_updates(peer);
tx = make_commit_tx(peer->htlcs, REMOTE_);
sig = commit_sig(&tx);
xmit_commit(peer, sig);
sent++;
}
/* Now send any revocations after the commit. */
if (sent + 1 == peer->db.last_sent) {
num_revokes--;
xmit_revoke(peer, revoke_idx++);
sent++;
}
if (sent != peer->db.last_sent)
errx(1, "%s: could not catch up %zu to %zu",
peer->name, sent, peer->db.last_sent);
assert(num_revokes == 0);
}
static void do_cmd(struct peer *peer)
{
char cmd[80];
int i;
unsigned int htlc;
struct commit_info *ci;
i = read(peer->cmdfd, cmd, sizeof(cmd)-1);
if (i <= 0)
err(1, "%s: reading command", peer->name);
if (cmd[i-1] != '\0')
errx(1, "%s: Unterminated command", peer->name);
if (i == 1) {
fflush(stdout);
exit(0);
}
peer->info = tal_strdup(peer, "");
if (sscanf(cmd, "offer %u", &htlc) == 1)
send_offer(peer, htlc);
else if (sscanf(cmd, "remove %u", &htlc) == 1)
send_remove(peer, htlc);
else if (streq(cmd, "feechange"))
send_feechange(peer);
else if (streq(cmd, "commit"))
send_commit(peer);
else if (streq(cmd, "recvrevoke")) {
u32 number;
read_peer(peer, "R", cmd);
read_in(peer->infd, &number, sizeof(number));
receive_revoke(peer, number);
} else if (streq(cmd, "recvoffer")) {
read_peer(peer, "+", cmd);
read_in(peer->infd, &htlc, sizeof(htlc));
receive_offer(peer, htlc);
} else if (streq(cmd, "recvremove")) {
read_peer(peer, "-", cmd);
read_in(peer->infd, &htlc, sizeof(htlc));
receive_remove(peer, htlc);
} else if (streq(cmd, "recvfeechange")) {
read_peer(peer, "F", cmd);
receive_feechange(peer);
} else if (streq(cmd, "recvcommit")) {
struct signature sig;
read_peer(peer, "C", cmd);
read_in(peer->infd, &sig, sizeof(sig));
receive_commit(peer, &sig);
} else if (streq(cmd, "save")) {
write_all(peer->cmddonefd, &peer->db, sizeof(peer->db));
return;
} else if (streq(cmd, "restore")) {
write_all(peer->cmddonefd, "", 1);
/* Ack, then read in blob */
read_all(peer->cmdfd, &peer->db, sizeof(peer->db));
restore_state(peer);
} else if (streq(cmd, "checksync")) {
struct commit_tx ours, theirs;
ours = make_commit_tx(peer->htlcs, LOCAL_);
theirs = make_commit_tx(peer->htlcs, REMOTE_);
write_all(peer->cmddonefd, &ours, sizeof(ours));
write_all(peer->cmddonefd, &theirs, sizeof(theirs));
return;
} else if (streq(cmd, "dump")) {
dump_peer(peer, false);
} else if (streq(cmd, "dumpall")) {
dump_peer(peer, true);
} else
errx(1, "%s: Unknown command %s", peer->name, cmd);