forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_state_coverage.c
2473 lines (2119 loc) · 64.7 KB
/
test_state_coverage.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
/* Test for state machine. */
#include <stdbool.h>
#include <ccan/cast/cast.h>
#include <ccan/array_size/array_size.h>
#include <ccan/tal/tal.h>
#include <ccan/tal/str/str.h>
#include <ccan/err/err.h>
#include <ccan/structeq/structeq.h>
#include <ccan/htable/htable_type.h>
#include <ccan/hash/hash.h>
#include <ccan/opt/opt.h>
#include <ccan/asort/asort.h>
#include <ccan/list/list.h>
#include "version.h"
static bool record_input_mapping(int b);
#define MAPPING_INPUTS(b) \
do { if (record_input_mapping(b)) return false; } while(0)
#include "state.h"
#include "names.c"
static bool quick = false;
static bool dot_simplify = false;
static bool dot_enable = false;
static bool dot_include_abnormal = false;
static bool dot_include_errors = false;
static bool include_nops = false;
static enum state_input *mapping_inputs;
enum failure {
FAIL_NONE,
FAIL_DECLINE_HTLC,
FAIL_STEAL,
FAIL_ACCEPT_OPEN,
FAIL_ACCEPT_ANCHOR,
FAIL_ACCEPT_OPEN_COMMIT_SIG,
FAIL_ACCEPT_OPEN_COMPLETE,
FAIL_ACCEPT_HTLC_ADD,
FAIL_ACCEPT_HTLC_FAIL,
FAIL_ACCEPT_HTLC_FULFILL,
FAIL_ACCEPT_UPDATE_ACCEPT,
FAIL_ACCEPT_UPDATE_COMPLETE,
FAIL_ACCEPT_UPDATE_SIGNATURE,
FAIL_ACCEPT_CLOSE,
FAIL_ACCEPT_CLOSE_COMPLETE,
FAIL_ACCEPT_CLOSE_ACK,
FAIL_ACCEPT_SIMULTANEOUS_CLOSE
};
struct htlc {
bool to_them;
unsigned int id;
};
struct htlc_progress {
struct htlc htlc; /* id == -1 if none in progress. */
bool adding; /* otherwise, removing. */
};
struct htlc_spend_watch {
unsigned int id;
enum state_input done;
};
/* Beyond this we consider cases equal for traverse loop detection. */
#define CAP_HTLCS 1
/* How many HTLCs to negotiate. */
#define MAX_HTLCS 2
/* But we can have many different malleated commit txs. */
#define HTLC_ARRSIZE 20
/*
* Worst case:
* Low priority: PKT_UPDATE_ADD_HTLC
* Receive update, which we decline: PKT_UPDATE_DECLINE_HTLC
* Send new update: PKT_UPDATE_ADD_HTLC
* Start close: PKT_CLOSE
* Some error: PKT_ERROR
*/
#define MAX_OUTQ 5
/* No padding, for fast compare and hashing. */
struct core_state {
/* What bitcoin/timeout notifications are we subscribed to? */
uint64_t event_notifies;
enum state_input current_command;
enum state_input outputs[MAX_OUTQ];
uint8_t num_outputs;
/* Here down need to be generated from other fields */
uint8_t state;
uint8_t peercond;
bool has_current_htlc;
uint8_t capped_htlcs_to_them;
uint8_t capped_htlcs_to_us;
uint8_t capped_htlc_spends_to_them;
uint8_t capped_htlc_spends_to_us;
uint8_t capped_live_htlcs_to_them;
uint8_t capped_live_htlcs_to_us;
bool valid;
bool pad[5];
};
struct peer {
struct core_state core;
enum state state;
enum state_peercond cond;
/* To store HTLC numbers. */
unsigned int pkt_data[MAX_OUTQ];
/* id == -1 if none currently. */
struct htlc_progress current_htlc;
/* Transitory: True if we just declined an HTLC. */
bool htlc_declined;
/* Have we created an anchor tx? */
bool anchor;
/* Have we broadcast anchor? */
bool anchor_broadcast;
/* Have we spent anchor (or seen them do it?) */
bool anchor_spent;
unsigned int num_htlcs_to_them, num_htlcs_to_us;
struct htlc htlcs_to_them[MAX_HTLCS], htlcs_to_us[MAX_HTLCS];
unsigned int num_live_htlcs_to_them, num_live_htlcs_to_us;
struct htlc live_htlcs_to_them[HTLC_ARRSIZE], live_htlcs_to_us[HTLC_ARRSIZE];
unsigned int num_htlc_spends_to_them, num_htlc_spends_to_us;
struct htlc htlc_spends_to_us[HTLC_ARRSIZE],
htlc_spends_to_them[HTLC_ARRSIZE];
unsigned int num_rvals_known;
unsigned int rvals_known[HTLC_ARRSIZE];
/* Where we came from. */
const struct trail *trail;
/* Current input and idata (for fail()) */
enum state_input current_input;
const union input *current_idata;
const char *error;
/* ID. */
const char *name;
/* The other peer's data. */
struct peer *other;
};
/* To recontruct errors. */
struct trail {
const struct trail *prev;
const char *name;
enum state_input input;
const struct peer *before, *after;
int htlc_id;
unsigned int num_peer_outputs;
unsigned int depth;
const char *pkt_sent;
};
struct situation {
union {
struct core_state s;
uint32_t u32[sizeof(struct core_state)/sizeof(uint32_t)];
} a, b;
};
static const struct situation *situation_keyof(const struct situation *situation)
{
return situation;
}
/* After 2, we stop looping. */
static unsigned int cap(unsigned int val)
{
return val > CAP_HTLCS ? CAP_HTLCS : val;
}
static size_t situation_hash(const struct situation *situation)
{
BUILD_ASSERT(sizeof(situation->a.u32) == sizeof(situation->a.s));
return hash(situation->a.u32, ARRAY_SIZE(situation->a.u32), 0);
}
static bool situation_eq(const struct situation *a, const struct situation *b)
{
/* No padding */
BUILD_ASSERT(sizeof(a->a.s)
== (sizeof(a->a.s.event_notifies)
+ sizeof(a->a.s.state)
+ sizeof(a->a.s.current_command)
+ sizeof(a->a.s.outputs)
+ sizeof(a->a.s.num_outputs)
+ sizeof(a->a.s.peercond)
+ sizeof(a->a.s.has_current_htlc)
+ sizeof(a->a.s.capped_htlcs_to_us)
+ sizeof(a->a.s.capped_htlcs_to_them)
+ sizeof(a->a.s.capped_htlc_spends_to_us)
+ sizeof(a->a.s.capped_htlc_spends_to_them)
+ sizeof(a->a.s.capped_live_htlcs_to_us)
+ sizeof(a->a.s.capped_live_htlcs_to_them)
+ sizeof(a->a.s.valid)
+ sizeof(a->a.s.pad)));
return structeq(&a->a.s, &b->a.s) && structeq(&a->b.s, &b->b.s);
}
struct dot_edge {
const char *oldstate, *newstate;
enum state_input i;
const char *pkt;
};
static const struct dot_edge *dot_edge_keyof(const struct dot_edge *dot_edge)
{
return dot_edge;
}
static size_t dot_edge_hash(const struct dot_edge *d)
{
uint32_t pkthash;
if (d->pkt)
pkthash = hash(d->pkt, strlen(d->pkt), d->i);
else
pkthash = d->i;
return hash_pointer(d->oldstate, hash_pointer(d->newstate, pkthash));
}
static bool dot_edge_eq(const struct dot_edge *a, const struct dot_edge *b)
{
return a->oldstate == b->oldstate
&& a->newstate == b->newstate
&& a->i == b->i
&& ((a->pkt == NULL && b->pkt == NULL)
|| streq(a->pkt, b->pkt));
}
HTABLE_DEFINE_TYPE(struct dot_edge,
dot_edge_keyof, dot_edge_hash, dot_edge_eq,
edge_hash);
HTABLE_DEFINE_TYPE(struct situation,
situation_keyof, situation_hash, situation_eq,
sithash);
struct hist {
/* All the different state combinations. */
struct sithash sithash;
/* The different inputs. */
enum state_input **inputs_per_state;
/* The different outputs. */
enum state_input *outputs;
/* Edges for the dot graph, if any. */
struct edge_hash edges;
/* For dumping states. */
struct state_dump {
enum state_input input;
enum state next;
enum state_input pkt;
} **state_dump;
};
struct fail_details {
/* The universe state at the time. */
struct peer us, other;
enum state_input input;
union input idata;
/* Previous history. */
const struct trail *prev_trail;
};
struct failpoint {
/* Hash key (with which_fail) */
struct situation sit;
/* Which failure */
enum failure which_fail;
/* If we haven't tried failing yet, this is set */
struct fail_details *details;
};
static const struct failpoint *
failpoint_keyof(const struct failpoint *f)
{
return f;
}
static size_t failpoint_hash(const struct failpoint *f)
{
return situation_hash(&f->sit) + f->which_fail;
}
static bool failpoint_eq(const struct failpoint *a,
const struct failpoint *b)
{
return a->which_fail == b->which_fail
&& situation_eq(&a->sit, &b->sit);
}
HTABLE_DEFINE_TYPE(struct failpoint,
failpoint_keyof, failpoint_hash, failpoint_eq,
failhash);
static struct failhash failhash;
static void update_core(struct core_state *core, const struct peer *peer)
{
size_t i;
for (i = core->num_outputs; i < ARRAY_SIZE(core->outputs); i++)
assert(core->outputs[i] == 0);
core->has_current_htlc = peer->current_htlc.htlc.id != -1;
core->state = peer->state;
BUILD_ASSERT(STATE_MAX < 256);
core->peercond = peer->cond;
core->capped_htlcs_to_us = cap(peer->num_htlcs_to_us);
core->capped_htlcs_to_them = cap(peer->num_htlcs_to_them);
core->capped_live_htlcs_to_us = cap(peer->num_live_htlcs_to_us);
core->capped_live_htlcs_to_them = cap(peer->num_live_htlcs_to_them);
core->capped_htlc_spends_to_us = cap(peer->num_htlc_spends_to_us);
core->capped_htlc_spends_to_them = cap(peer->num_htlc_spends_to_them);
core->valid = true;
}
static void situation_init(struct situation *sit,
const struct peer *peer)
{
if (streq(peer->name, "A")) {
sit->a.s = peer->core;
update_core(&sit->a.s, peer);
/* If we're still talking to peer, their state matters. */
if (peer->cond != PEER_CLOSED
|| peer->other->cond != PEER_CLOSED) {
sit->b.s = peer->other->core;
update_core(&sit->b.s, peer->other);
} else
memset(&sit->b.s, 0, sizeof(sit->b.s));
} else {
sit->b.s = peer->core;
update_core(&sit->b.s, peer);
/* If we're still talking to peer, their state matters. */
if (peer->cond != PEER_CLOSED
|| peer->other->cond != PEER_CLOSED) {
sit->a.s = peer->other->core;
update_core(&sit->a.s, peer->other);
} else
memset(&sit->a.s, 0, sizeof(sit->a.s));
}
}
/* Returns false if we've been here before. */
static bool sithash_update(struct sithash *sithash,
const struct peer *peer)
{
struct situation sit;
situation_init(&sit, peer);
if (sithash_get(sithash, &sit))
return false;
sithash_add(sithash, tal_dup(NULL, struct situation, &sit));
return true;
}
static void copy_peers(struct peer *dst, struct peer *other,
const struct peer *src)
{
*dst = *src;
*other = *src->other;
dst->other = other;
other->other = dst;
}
static const struct trail *clone_trail(const tal_t *ctx,
const struct trail *trail)
{
struct trail *t;
if (!trail)
return NULL;
t = tal_dup(ctx, struct trail, trail);
t->before = tal_dup(t, struct peer, trail->before);
t->after = trail->after ? tal_dup(t, struct peer, trail->after)
: NULL;
t->pkt_sent = trail->pkt_sent ? tal_strdup(t, trail->pkt_sent) : NULL;
t->prev = clone_trail(t, trail->prev);
return t;
}
static const union input dup_idata(const tal_t *ctx,
enum state_input input,
const union input *idata)
{
union input i;
if (input_is_pkt(input))
i.pkt = (Pkt *)tal_strdup(ctx, (const char *)idata->pkt);
else if (input == CMD_SEND_HTLC_ADD
|| input == CMD_SEND_HTLC_FULFILL
|| input == CMD_SEND_HTLC_FAIL) {
i.htlc_prog = tal_dup(ctx, struct htlc_progress,
idata->htlc_prog);
} else {
if (idata->htlc)
i.htlc = tal_dup(ctx, struct htlc, idata->htlc);
else
i.htlc = NULL;
}
return i;
}
static bool fail(const struct peer *peer, enum failure which_fail)
{
struct failpoint *f = tal(NULL, struct failpoint), *old;
situation_init(&f->sit, peer);
f->which_fail = which_fail;
/* If we've been here before... */
old = failhash_get(&failhash, f);
if (old) {
tal_free(f);
/* If we haven't tried failing, try that now. */
if (old->details) {
old->details = tal_free(old->details);
return true;
}
return false;
}
/* First time here, save details, don't fail yet. */
f->details = tal(f, struct fail_details);
/* Copy old peer, in case it has been changed since. */
copy_peers(&f->details->us, &f->details->other, peer->trail->before);
f->details->us.trail = clone_trail(f->details, peer->trail);
f->details->input = peer->current_input;
f->details->idata = dup_idata(f->details,
f->details->input, peer->current_idata);
failhash_add(&failhash, f);
return false;
}
static enum state_input input_by_name(const char *name)
{
size_t i;
for (i = 0; enum_state_input_names[i].name; i++) {
if (!strstarts(name, enum_state_input_names[i].name))
continue;
if (name[strlen(enum_state_input_names[i].name)] == '\0'
|| name[strlen(enum_state_input_names[i].name)] == ':')
return enum_state_input_names[i].v;
}
abort();
}
static Pkt *new_pkt(const tal_t *ctx, enum state_input i)
{
return (Pkt *)input_name(i);
}
static unsigned int htlc_id_from_pkt(const Pkt *pkt)
{
const char *s = strstr((const char *)pkt, ": HTLC #");
return s ? atoi(s + strlen(": HTLC #")) : -1U;
}
static Pkt *htlc_pkt(const tal_t *ctx, const char *prefix, unsigned int id)
{
assert(id != -1);
return (Pkt *)tal_fmt(ctx, "%s: HTLC #%u", prefix, id);
}
static unsigned int htlc_id_from_tx(const struct bitcoin_tx *tx)
{
const char *s = strstr((const char *)tx, "HTLC #");
return atoi(s + strlen("HTLC #"));
}
static struct bitcoin_tx *htlc_tx(const tal_t *ctx,
const char *prefix, unsigned int id)
{
return (struct bitcoin_tx *)tal_fmt(ctx, "%s HTLC #%u", prefix, id);
}
static struct htlc *find_any_htlc(const struct htlc *htlcs, size_t num,
unsigned id)
{
unsigned int i;
for (i = 0; i < num; i++)
if (htlcs[i].id == id)
return (struct htlc *)htlcs + i;
return NULL;
}
static struct htlc *find_htlc(const struct peer *peer, unsigned id)
{
const struct htlc *h;
h = find_any_htlc(peer->htlcs_to_us, peer->num_htlcs_to_us, id);
if (!h)
h = find_any_htlc(peer->htlcs_to_them,
peer->num_htlcs_to_them, id);
return (struct htlc *)h;
}
static struct htlc *find_live_htlc(const struct peer *peer,
unsigned id)
{
const struct htlc *h;
h = find_any_htlc(peer->live_htlcs_to_us, peer->num_live_htlcs_to_us,
id);
if (!h)
h = find_any_htlc(peer->live_htlcs_to_them,
peer->num_live_htlcs_to_them, id);
return (struct htlc *)h;
}
static struct htlc *find_htlc_spend(const struct peer *peer,
unsigned id)
{
const struct htlc *h;
h = find_any_htlc(peer->htlc_spends_to_us,
peer->num_htlc_spends_to_us,
id);
if (!h)
h = find_any_htlc(peer->htlc_spends_to_them,
peer->num_htlc_spends_to_them, id);
return (struct htlc *)h;
}
/* FIXME: order functions correctly. */
static void report_trail(const struct trail *t, const char *problem);
static void set_current_htlc(struct peer *peer,
unsigned int id,
bool to_them, bool adding)
{
if (peer->current_htlc.htlc.id != -1)
report_trail(peer->trail, "Already have current htlc");
assert(id != -1);
peer->current_htlc.htlc.id = id;
peer->current_htlc.htlc.to_them = to_them;
peer->current_htlc.adding = adding;
}
static void clear_current_htlc(struct peer *peer)
{
if (peer->current_htlc.htlc.id == -1)
report_trail(peer->trail, "No current htlc");
peer->current_htlc.htlc.id = -1;
}
static bool rval_known(const struct peer *peer, unsigned int id)
{
unsigned int i;
for (i = 0; i < peer->num_rvals_known; i++)
if (peer->rvals_known[i] == id)
return true;
return false;
}
static void add_rval(struct peer *peer, unsigned int id)
{
if (!rval_known(peer, id)) {
assert(peer->num_rvals_known < ARRAY_SIZE(peer->rvals_known));
peer->rvals_known[peer->num_rvals_known++] = id;
}
}
Pkt *pkt_open(const tal_t *ctx, const struct peer *peer,
OpenChannel__AnchorOffer anchor)
{
return new_pkt(ctx, PKT_OPEN);
}
Pkt *pkt_anchor(const tal_t *ctx, const struct peer *peer)
{
return new_pkt(ctx, PKT_OPEN_ANCHOR);
}
Pkt *pkt_open_commit_sig(const tal_t *ctx, const struct peer *peer)
{
return new_pkt(ctx, PKT_OPEN_COMMIT_SIG);
}
Pkt *pkt_open_complete(const tal_t *ctx, const struct peer *peer)
{
return new_pkt(ctx, PKT_OPEN_COMPLETE);
}
Pkt *pkt_htlc_add(const tal_t *ctx, const struct peer *peer,
const struct htlc_progress *htlc_prog)
{
return htlc_pkt(ctx, "PKT_UPDATE_ADD_HTLC", htlc_prog->htlc.id);
}
Pkt *pkt_htlc_fulfill(const tal_t *ctx, const struct peer *peer,
const struct htlc_progress *htlc_prog)
{
return htlc_pkt(ctx, "PKT_UPDATE_FULFILL_HTLC", htlc_prog->htlc.id);
}
Pkt *pkt_htlc_fail(const tal_t *ctx, const struct peer *peer,
const struct htlc_progress *htlc_prog)
{
return htlc_pkt(ctx, "PKT_UPDATE_FAIL_HTLC", htlc_prog->htlc.id);
}
Pkt *pkt_update_accept(const tal_t *ctx, const struct peer *peer)
{
return htlc_pkt(ctx, "PKT_UPDATE_ACCEPT", peer->current_htlc.htlc.id);
}
Pkt *pkt_update_signature(const tal_t *ctx, const struct peer *peer)
{
return htlc_pkt(ctx, "PKT_UPDATE_SIGNATURE",
peer->current_htlc.htlc.id);
}
Pkt *pkt_update_complete(const tal_t *ctx, const struct peer *peer)
{
return htlc_pkt(ctx, "PKT_UPDATE_COMPLETE",
peer->current_htlc.htlc.id);
}
Pkt *pkt_err(const tal_t *ctx, const char *fmt, ...)
{
char *str;
va_list ap;
str = tal_strdup(ctx, "PKT_ERROR: ");
va_start(ap, fmt);
tal_append_vfmt(&str, fmt, ap);
va_end(ap);
return (Pkt *)str;
}
Pkt *pkt_close(const tal_t *ctx, const struct peer *peer)
{
return new_pkt(ctx, PKT_CLOSE);
}
Pkt *pkt_close_complete(const tal_t *ctx, const struct peer *peer)
{
return new_pkt(ctx, PKT_CLOSE_COMPLETE);
}
Pkt *pkt_close_ack(const tal_t *ctx, const struct peer *peer)
{
return new_pkt(ctx, PKT_CLOSE_ACK);
}
Pkt *pkt_err_unexpected(const tal_t *ctx, const Pkt *pkt)
{
return pkt_err("Unexpected pkt %s", (const char *)pkt);
}
Pkt *accept_pkt_open(const tal_t *ctx,
struct peer *peer,
const Pkt *pkt)
{
if (fail(peer, FAIL_ACCEPT_OPEN))
return pkt_err(ctx, "Error inject");
return NULL;
}
Pkt *accept_pkt_anchor(const tal_t *ctx, struct peer *peer, const Pkt *pkt)
{
if (fail(peer, FAIL_ACCEPT_ANCHOR))
return pkt_err(ctx, "Error inject");
return NULL;
}
Pkt *accept_pkt_open_commit_sig(const tal_t *ctx,
struct peer *peer,
const Pkt *pkt)
{
if (fail(peer, FAIL_ACCEPT_OPEN_COMMIT_SIG))
return pkt_err(ctx, "Error inject");
return NULL;
}
Pkt *accept_pkt_open_complete(const tal_t *ctx,
struct peer *peer,
const Pkt *pkt)
{
if (fail(peer, FAIL_ACCEPT_OPEN_COMPLETE))
return pkt_err(ctx, "Error inject");
return NULL;
}
Pkt *accept_pkt_htlc_add(const tal_t *ctx,
struct peer *peer, const Pkt *pkt,
Pkt **decline)
{
if (fail(peer, FAIL_ACCEPT_HTLC_ADD))
return pkt_err(ctx, "Error inject");
/* This is the current htlc: If they propose it, it's to us. */
set_current_htlc(peer, htlc_id_from_pkt(pkt), false, true);
if (fail(peer, FAIL_DECLINE_HTLC))
*decline = new_pkt(ctx, PKT_UPDATE_DECLINE_HTLC);
else
*decline = NULL;
return NULL;
}
Pkt *accept_pkt_htlc_fail(const tal_t *ctx,
struct peer *peer, const Pkt *pkt)
{
unsigned int id = htlc_id_from_pkt(pkt);
const struct htlc *h = find_htlc(peer, id);
if (fail(peer, FAIL_ACCEPT_HTLC_FAIL))
return pkt_err(ctx, "Error inject");
/* The shouldn't fail unless it's to them */
assert(h->to_them);
/* This is the current htlc */
set_current_htlc(peer, h->id, h->to_them, false);
return NULL;
}
Pkt *accept_pkt_htlc_fulfill(const tal_t *ctx,
struct peer *peer, const Pkt *pkt)
{
unsigned int id = htlc_id_from_pkt(pkt);
const struct htlc *h = find_htlc(peer, id);
if (fail(peer, FAIL_ACCEPT_HTLC_FULFILL))
return pkt_err(ctx, "Error inject");
/* The shouldn't complete unless it's to them */
assert(h->to_them);
/* This gives us the r value. */
add_rval(peer, htlc_id_from_pkt(pkt));
/* This is the current htlc */
set_current_htlc(peer, h->id, h->to_them, false);
return NULL;
}
Pkt *accept_pkt_update_accept(const tal_t *ctx,
struct peer *peer, const Pkt *pkt)
{
unsigned int id = htlc_id_from_pkt(pkt);
assert(id == peer->current_htlc.htlc.id);
if (fail(peer, FAIL_ACCEPT_UPDATE_ACCEPT))
return pkt_err(ctx, "Error inject");
return NULL;
}
Pkt *accept_pkt_update_complete(const tal_t *ctx,
struct peer *peer, const Pkt *pkt)
{
unsigned int id = htlc_id_from_pkt(pkt);
assert(id == peer->current_htlc.htlc.id);
if (fail(peer, FAIL_ACCEPT_UPDATE_COMPLETE))
return pkt_err(ctx, "Error inject");
return NULL;
}
Pkt *accept_pkt_update_signature(const tal_t *ctx,
struct peer *peer, const Pkt *pkt)
{
unsigned int id = htlc_id_from_pkt(pkt);
assert(id == peer->current_htlc.htlc.id);
if (fail(peer, FAIL_ACCEPT_UPDATE_SIGNATURE))
return pkt_err(ctx, "Error inject");
return NULL;
}
Pkt *accept_pkt_close(const tal_t *ctx, struct peer *peer, const Pkt *pkt)
{
if (fail(peer, FAIL_ACCEPT_CLOSE))
return pkt_err(ctx, "Error inject");
return NULL;
}
Pkt *accept_pkt_close_complete(const tal_t *ctx,
struct peer *peer, const Pkt *pkt)
{
if (fail(peer, FAIL_ACCEPT_CLOSE_COMPLETE))
return pkt_err(ctx, "Error inject");
return NULL;
}
Pkt *accept_pkt_simultaneous_close(const tal_t *ctx,
struct peer *peer,
const Pkt *pkt)
{
if (fail(peer, FAIL_ACCEPT_SIMULTANEOUS_CLOSE))
return pkt_err(ctx, "Error inject");
return NULL;
}
Pkt *accept_pkt_close_ack(const tal_t *ctx,
struct peer *peer, const Pkt *pkt)
{
if (fail(peer, FAIL_ACCEPT_CLOSE_ACK))
return pkt_err(ctx, "Error inject");
return NULL;
}
static struct bitcoin_tx *bitcoin_tx(const char *str)
{
return (struct bitcoin_tx *)str;
}
static bool bitcoin_tx_is(const struct bitcoin_tx *btx, const char *str)
{
return streq((const char *)btx, str);
}
const struct bitcoin_tx *bitcoin_anchor(const tal_t *ctx, struct peer *peer)
{
if (!peer->anchor)
report_trail(peer->trail, "Can't create anchor tx: no anchor!");
peer->anchor = false;
peer->anchor_broadcast = true;
return bitcoin_tx("anchor");
}
const struct bitcoin_tx *bitcoin_close(const tal_t *ctx,
struct peer *peer)
{
peer->anchor_spent = true;
return bitcoin_tx("close");
}
const struct bitcoin_tx *bitcoin_spend_ours(const tal_t *ctx,
const struct peer *peer)
{
return bitcoin_tx("spend our commit");
}
const struct bitcoin_tx *bitcoin_spend_theirs(const tal_t *ctx,
const struct peer *peer,
const struct bitcoin_event *btc)
{
return bitcoin_tx("spend their commit");
}
const struct bitcoin_tx *bitcoin_steal(const tal_t *ctx,
const struct peer *peer,
struct bitcoin_event *btc)
{
if (fail(peer, FAIL_STEAL))
return NULL;
return bitcoin_tx("steal");
}
const struct bitcoin_tx *bitcoin_commit(const tal_t *ctx, struct peer *peer)
{
peer->anchor_spent = true;
return bitcoin_tx("our commit");
}
/* Create a HTLC refund collection */
const struct bitcoin_tx *bitcoin_htlc_timeout(const tal_t *ctx,
const struct peer *peer,
const struct htlc *htlc)
{
return htlc_tx(ctx, "htlc timeout", htlc->id);
}
/* Create a HTLC collection */
const struct bitcoin_tx *bitcoin_htlc_spend(const tal_t *ctx,
const struct peer *peer,
const struct htlc *htlc)
{
return htlc_tx(ctx, "htlc fulfill", htlc->id);
}
bool committed_to_htlcs(const struct peer *peer)
{
return peer->num_htlcs_to_them != 0 || peer->num_htlcs_to_us != 0;
}
#define TEST_STATE_COVERAGE 1
#include "state.c"
#include <ccan/tal/tal.h>
#include <stdio.h>
static void peer_init(struct peer *peer,
struct peer *other,
const char *name)
{
peer->cond = PEER_CMD_OK;
peer->state = STATE_INIT;
peer->core.num_outputs = 0;
peer->current_htlc.htlc.id = -1;
peer->num_htlcs_to_us = 0;
peer->num_htlcs_to_them = 0;
peer->num_live_htlcs_to_us = 0;
peer->num_live_htlcs_to_them = 0;
peer->num_htlc_spends_to_us = 0;
peer->num_htlc_spends_to_them = 0;
peer->num_rvals_known = 0;
peer->error = NULL;
peer->htlc_declined = false;
peer->anchor = false;
peer->anchor_broadcast = false;
peer->anchor_spent = false;
memset(peer->core.outputs, 0, sizeof(peer->core.outputs));
peer->pkt_data[0] = -1;
peer->core.current_command = INPUT_NONE;
peer->core.event_notifies = 0;
peer->name = name;
peer->other = other;
peer->trail = NULL;
memset(peer->core.pad, 0, sizeof(peer->core.pad));
}
/* Recursion! */
static void run_peer(const struct peer *peer,
bool normalpath, bool errorpath,
const struct trail *prev_trail,
struct hist *hist);
static void init_trail(struct trail *t,
enum state_input input,
const union input *idata,
const struct peer *before,
const struct trail *prev)
{
t->name = before->name;
t->prev = prev;
t->depth = prev ? prev->depth + 1 : 0;
t->input = input;
t->before = before;
t->after = NULL;
t->num_peer_outputs = -1;
t->pkt_sent = NULL;
if (input == CMD_SEND_HTLC_FULFILL
|| input == INPUT_RVALUE
|| input == BITCOIN_HTLC_TOTHEM_TIMEOUT
|| input == BITCOIN_HTLC_TOTHEM_SPENT
|| input == BITCOIN_HTLC_TOUS_TIMEOUT
|| input == BITCOIN_HTLC_FULFILL_SPEND_DONE
|| input == BITCOIN_HTLC_RETURN_SPEND_DONE)
t->htlc_id = idata->htlc->id;
else if (input == PKT_UPDATE_ADD_HTLC)
t->htlc_id = htlc_id_from_pkt(idata->pkt);
else
t->htlc_id = -1;
}
static void update_trail(struct trail *t,
const struct peer *after,
const Pkt *output)
{
t->after = after;
t->num_peer_outputs = after->other->core.num_outputs;
t->pkt_sent = (const char *)output;
}
static void report_trail_rev(const struct trail *t)