-
Notifications
You must be signed in to change notification settings - Fork 906
/
run-peer-wire.c
1172 lines (1026 loc) · 32.1 KB
/
run-peer-wire.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "config.h"
#include "../towire.c"
#include "../fromwire.c"
#include "../peer_wire.c"
#include "bitcoin/pubkey.c"
#include "bitcoin/chainparams.c"
#include "common/amount.c"
#include "common/channel_id.c"
#include "common/node_id.c"
#include "wire/tlvstream.h"
#include <stdio.h>
#include <common/channel_type.h>
#include <common/setup.h>
#include <common/sphinx.h>
#include <common/wireaddr.h>
#include <wire/tlvstream.c>
extern secp256k1_context *secp256k1_ctx;
/* AUTOGENERATED MOCKS START */
/* AUTOGENERATED MOCKS END */
/* memsetting pubkeys doesn't work */
static void set_pubkey(struct pubkey *key)
{
u8 der[PUBKEY_CMPR_LEN];
memset(der, 2, sizeof(der));
assert(pubkey_from_der(der, sizeof(der), key));
}
static void set_node_id(struct node_id *id)
{
memset(id->k, 2, sizeof(id->k));
}
static void set_scid(struct short_channel_id *scid)
{
memset(scid, 2, sizeof(struct short_channel_id));
}
static void set_cid(struct channel_id *cid)
{
memset(cid, 2, sizeof(struct channel_id));
}
/* Size up to field. */
#define upto_field(p, field) \
((char *)&(p)->field - (char *)(p))
/* Size including field. */
#define with_field(p, field) \
(upto_field((p), field) + sizeof((p)->field))
/* Equal up to this field */
#define eq_upto(p1, p2, field) \
(memcmp((p1), (p2), upto_field(p1, field)) == 0)
/* Equal up to and including this field */
#define eq_with(p1, p2, field) \
(memcmp((p1), (p2), with_field(p1, field)) == 0)
/* Equal from fields first to last inclusive. */
#define eq_between(p1, p2, first, last) \
(memcmp((char *)(p1) + upto_field((p1), first), \
(char *)(p2) + upto_field((p1), first), \
with_field(p1, last) - upto_field(p1, first)) == 0)
/* Equal in one field. */
#define eq_field(p1, p2, field) \
(memcmp((char *)(p1) + upto_field((p1), field), \
(char *)(p2) + upto_field((p1), field), \
sizeof((p1)->field)) == 0)
#define eq_var(p1, p2, field) \
(tal_count((p1)->field) == tal_count((p2)->field) \
&& (tal_count((p1)->field) == 0 || memcmp((p1)->field, (p2)->field, tal_bytelen((p1)->field)) == 0))
/* TLV field comparison: both unset, or both set and eqfn ok */
#define eq_tlv(p1, p2, tlvname, eqfn) \
(((p1)->tlvs == NULL && (p2)->tlvs == NULL) \
|| ((p1)->tlvs && (p2)->tlvs \
&& (((p1)->tlvs->tlvname == NULL && (p2)->tlvs->tlvname == NULL) \
|| ((p1)->tlvs->tlvname && (p2)->tlvs->tlvname && \
eqfn((p1)->tlvs->tlvname, (p2)->tlvs->tlvname)))))
/* Convenience structs for everyone! */
struct msg_error {
struct channel_id channel_id;
u8 *data;
};
struct msg_closing_signed {
struct channel_id channel_id;
struct amount_sat fee_satoshis;
secp256k1_ecdsa_signature signature;
};
struct msg_funding_created {
struct channel_id temporary_channel_id;
struct bitcoin_txid txid;
u16 output_index;
secp256k1_ecdsa_signature signature;
};
struct msg_accept_channel {
struct channel_id temporary_channel_id;
struct amount_sat dust_limit_satoshis;
struct amount_msat max_htlc_value_in_flight_msat;
struct amount_sat channel_reserve_satoshis;
struct amount_msat htlc_minimum_msat;
u32 minimum_depth;
u16 to_self_delay;
u16 max_accepted_htlcs;
struct pubkey funding_pubkey;
struct pubkey revocation_basepoint;
struct pubkey payment_basepoint;
struct pubkey delayed_payment_basepoint;
struct pubkey htlc_basepoint;
struct pubkey first_per_commitment_point;
struct tlv_accept_channel_tlvs *tlvs;
};
struct msg_update_fulfill_htlc {
struct channel_id channel_id;
u64 id;
struct preimage payment_preimage;
};
struct msg_shutdown {
struct channel_id channel_id;
u8 *scriptpubkey;
struct tlv_shutdown_tlvs *tlvs;
};
struct msg_funding_signed {
struct channel_id temporary_channel_id;
secp256k1_ecdsa_signature signature;
};
struct msg_revoke_and_ack {
struct channel_id channel_id;
struct secret per_commitment_secret;
struct pubkey next_per_commitment_point;
};
struct msg_channel_update {
secp256k1_ecdsa_signature signature;
u32 timestamp;
u8 message_flags;
u8 channel_flags;
u16 expiry;
struct amount_msat htlc_minimum_msat;
u32 fee_base_msat;
u32 fee_proportional_millionths;
struct amount_msat htlc_maximum_msat;
struct bitcoin_blkid chain_hash;
struct short_channel_id short_channel_id;
};
struct msg_channel_ready {
struct channel_id channel_id;
struct pubkey next_per_commitment_point;
struct tlv_channel_ready_tlvs *tlvs;
};
struct msg_announcement_signatures {
struct channel_id channel_id;
secp256k1_ecdsa_signature announcement_node_signature;
secp256k1_ecdsa_signature announcement_bitcoin_signature;
struct short_channel_id short_channel_id;
};
struct msg_commitment_signed {
struct channel_id channel_id;
secp256k1_ecdsa_signature signature;
secp256k1_ecdsa_signature *htlc_signature;
struct tlv_commitment_signed_tlvs *tlvs;
};
struct msg_node_announcement {
secp256k1_ecdsa_signature signature;
u32 timestamp;
struct node_id node_id;
u8 rgb_color[3];
u8 alias[32];
u8 *features;
u8 *addresses;
struct tlv_node_ann_tlvs *tlvs;
};
struct msg_open_channel {
struct bitcoin_blkid chain_hash;
struct channel_id temporary_channel_id;
struct amount_sat funding_satoshis;
struct amount_msat push_msat;
struct amount_sat dust_limit_satoshis;
struct amount_msat max_htlc_value_in_flight_msat;
struct amount_sat channel_reserve_satoshis;
struct amount_msat htlc_minimum_msat;
u32 feerate_per_kw;
u16 to_self_delay;
u16 max_accepted_htlcs;
struct pubkey funding_pubkey;
struct pubkey revocation_basepoint;
struct pubkey payment_basepoint;
struct pubkey delayed_payment_basepoint;
struct pubkey htlc_basepoint;
struct pubkey first_per_commitment_point;
u8 channel_flags;
struct tlv_open_channel_tlvs *tlvs;
};
struct msg_update_fail_htlc {
struct channel_id channel_id;
u64 id;
u8 *reason;
};
struct msg_channel_announcement {
secp256k1_ecdsa_signature node_signature_1;
secp256k1_ecdsa_signature node_signature_2;
secp256k1_ecdsa_signature bitcoin_signature_1;
secp256k1_ecdsa_signature bitcoin_signature_2;
u8 *features;
struct bitcoin_blkid chain_hash;
struct short_channel_id short_channel_id;
struct node_id node_id_1;
struct node_id node_id_2;
struct pubkey bitcoin_key_1;
struct pubkey bitcoin_key_2;
};
struct msg_init {
u8 *globalfeatures;
u8 *localfeatures;
struct tlv_init_tlvs *tlvs;
};
struct msg_update_add_htlc {
struct channel_id channel_id;
u64 id;
struct amount_msat amount_msat;
u32 expiry;
struct sha256 payment_hash;
u8 onion_routing_packet[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)];
struct tlv_update_add_htlc_tlvs *tlvs;
};
struct msg_update_fee {
struct channel_id channel_id;
u32 feerate_per_kw;
};
static void *towire_struct_channel_announcement(const tal_t *ctx,
const struct msg_channel_announcement *s)
{
return towire_channel_announcement(ctx,
&s->node_signature_1,
&s->node_signature_2,
&s->bitcoin_signature_1,
&s->bitcoin_signature_2,
s->features,
&s->chain_hash,
s->short_channel_id,
&s->node_id_1,
&s->node_id_2,
&s->bitcoin_key_1,
&s->bitcoin_key_2);
}
static struct msg_channel_announcement *fromwire_struct_channel_announcement(const tal_t *ctx, const void *p)
{
struct msg_channel_announcement *s = tal(ctx, struct msg_channel_announcement);
if (!fromwire_channel_announcement(s, p,
&s->node_signature_1,
&s->node_signature_2,
&s->bitcoin_signature_1,
&s->bitcoin_signature_2,
&s->features,
&s->chain_hash,
&s->short_channel_id,
&s->node_id_1,
&s->node_id_2,
&s->bitcoin_key_1,
&s->bitcoin_key_2))
return tal_free(s);
return s;
}
static void *towire_struct_open_channel(const tal_t *ctx,
const struct msg_open_channel *s)
{
return towire_open_channel(ctx,
&s->chain_hash,
&s->temporary_channel_id,
s->funding_satoshis,
s->push_msat,
s->dust_limit_satoshis,
s->max_htlc_value_in_flight_msat,
s->channel_reserve_satoshis,
s->htlc_minimum_msat,
s->feerate_per_kw,
s->to_self_delay,
s->max_accepted_htlcs,
&s->funding_pubkey,
&s->revocation_basepoint,
&s->payment_basepoint,
&s->delayed_payment_basepoint,
&s->htlc_basepoint,
&s->first_per_commitment_point,
s->channel_flags,
NULL);
}
static struct msg_open_channel *fromwire_struct_open_channel(const tal_t *ctx, const void *p)
{
struct msg_open_channel *s = tal(ctx, struct msg_open_channel);
if (fromwire_open_channel(s, p,
&s->chain_hash,
&s->temporary_channel_id,
&s->funding_satoshis,
&s->push_msat,
&s->dust_limit_satoshis,
&s->max_htlc_value_in_flight_msat,
&s->channel_reserve_satoshis,
&s->htlc_minimum_msat,
&s->feerate_per_kw,
&s->to_self_delay,
&s->max_accepted_htlcs,
&s->funding_pubkey,
&s->revocation_basepoint,
&s->payment_basepoint,
&s->delayed_payment_basepoint,
&s->htlc_basepoint,
&s->first_per_commitment_point,
&s->channel_flags,
&s->tlvs))
return s;
return tal_free(s);
}
static void *towire_struct_accept_channel(const tal_t *ctx,
const struct msg_accept_channel *s)
{
return towire_accept_channel(ctx,
&s->temporary_channel_id,
s->dust_limit_satoshis,
s->max_htlc_value_in_flight_msat,
s->channel_reserve_satoshis,
s->htlc_minimum_msat,
s->minimum_depth,
s->to_self_delay,
s->max_accepted_htlcs,
&s->funding_pubkey,
&s->revocation_basepoint,
&s->payment_basepoint,
&s->delayed_payment_basepoint,
&s->htlc_basepoint,
&s->first_per_commitment_point,
s->tlvs);
}
static struct msg_accept_channel *fromwire_struct_accept_channel(const tal_t *ctx, const void *p)
{
struct msg_accept_channel *s = tal(ctx, struct msg_accept_channel);
if (fromwire_accept_channel(s, p,
&s->temporary_channel_id,
&s->dust_limit_satoshis,
&s->max_htlc_value_in_flight_msat,
&s->channel_reserve_satoshis,
&s->htlc_minimum_msat,
&s->minimum_depth,
&s->to_self_delay,
&s->max_accepted_htlcs,
&s->funding_pubkey,
&s->revocation_basepoint,
&s->payment_basepoint,
&s->delayed_payment_basepoint,
&s->htlc_basepoint,
&s->first_per_commitment_point,
&s->tlvs))
return s;
return tal_free(s);
}
static void *towire_struct_node_announcement(const tal_t *ctx,
const struct msg_node_announcement *s)
{
return towire_node_announcement(ctx,
&s->signature,
s->features,
s->timestamp,
&s->node_id,
s->rgb_color,
s->alias,
s->addresses,
s->tlvs);
}
static struct msg_node_announcement *fromwire_struct_node_announcement(const tal_t *ctx, const void *p)
{
struct msg_node_announcement *s = tal(ctx, struct msg_node_announcement);
if (!fromwire_node_announcement(s, p,
&s->signature,
&s->features,
&s->timestamp,
&s->node_id,
s->rgb_color,
s->alias,
&s->addresses,
&s->tlvs))
return tal_free(s);
return s;
}
static void *towire_struct_channel_update(const tal_t *ctx,
const struct msg_channel_update *s)
{
return towire_channel_update(ctx,
&s->signature,
&s->chain_hash,
s->short_channel_id,
s->timestamp,
s->message_flags,
s->channel_flags,
s->expiry,
s->htlc_minimum_msat,
s->fee_base_msat,
s->fee_proportional_millionths,
s->htlc_maximum_msat);
}
static struct msg_channel_update
*fromwire_struct_channel_update(const tal_t *ctx, const void *p)
{
struct msg_channel_update *s = tal(ctx, struct msg_channel_update);
if (fromwire_channel_update(p,
&s->signature,
&s->chain_hash,
&s->short_channel_id,
&s->timestamp,
&s->message_flags,
&s->channel_flags,
&s->expiry,
&s->htlc_minimum_msat,
&s->fee_base_msat,
&s->fee_proportional_millionths,
&s->htlc_maximum_msat))
return s;
return tal_free(s);
}
static void *towire_struct_channel_ready(const tal_t *ctx,
const struct msg_channel_ready *s)
{
return towire_channel_ready(ctx,
&s->channel_id,
&s->next_per_commitment_point,
s->tlvs);
}
static struct msg_channel_ready *fromwire_struct_channel_ready(const tal_t *ctx, const void *p)
{
struct msg_channel_ready *s = tal(ctx, struct msg_channel_ready);
if (fromwire_channel_ready(ctx,
p,
&s->channel_id,
&s->next_per_commitment_point,
&s->tlvs))
return s;
return tal_free(s);
}
static void *towire_struct_announcement_signatures(const tal_t *ctx,
const struct msg_announcement_signatures *s)
{
return towire_announcement_signatures(ctx,
&s->channel_id,
s->short_channel_id,
&s->announcement_node_signature,
&s->announcement_bitcoin_signature);
}
static struct msg_announcement_signatures *fromwire_struct_announcement_signatures(const tal_t *ctx, const void *p)
{
struct msg_announcement_signatures *s = tal(ctx, struct msg_announcement_signatures);
if (fromwire_announcement_signatures(p,
&s->channel_id,
&s->short_channel_id,
&s->announcement_node_signature,
&s->announcement_bitcoin_signature))
return s;
return tal_free(s);
}
static void *towire_struct_update_fail_htlc(const tal_t *ctx,
const struct msg_update_fail_htlc *s)
{
return towire_update_fail_htlc(ctx,
&s->channel_id,
s->id,
s->reason);
}
static struct msg_update_fail_htlc *fromwire_struct_update_fail_htlc(const tal_t *ctx, const void *p)
{
struct msg_update_fail_htlc *s = tal(ctx, struct msg_update_fail_htlc);
if (!fromwire_update_fail_htlc(ctx, p,
&s->channel_id,
&s->id,
&s->reason))
return tal_free(s);
return s;
}
static void *towire_struct_update_fulfill_htlc(const tal_t *ctx,
const struct msg_update_fulfill_htlc *s)
{
return towire_update_fulfill_htlc(ctx,
&s->channel_id,
s->id,
&s->payment_preimage);
}
static struct msg_update_fulfill_htlc *fromwire_struct_update_fulfill_htlc(const tal_t *ctx, const void *p)
{
struct msg_update_fulfill_htlc *s = tal(ctx, struct msg_update_fulfill_htlc);
if (fromwire_update_fulfill_htlc(p,
&s->channel_id,
&s->id,
&s->payment_preimage))
return s;
return tal_free(s);
}
static void *towire_struct_commitment_signed(const tal_t *ctx,
const struct msg_commitment_signed *s)
{
return towire_commitment_signed(ctx,
&s->channel_id,
&s->signature,
s->htlc_signature,
s->tlvs);
}
static struct msg_commitment_signed *fromwire_struct_commitment_signed(const tal_t *ctx, const void *p)
{
struct msg_commitment_signed *s = tal(ctx, struct msg_commitment_signed);
s->tlvs = tlv_commitment_signed_tlvs_new(ctx);
if (!fromwire_commitment_signed(s, p,
&s->channel_id,
&s->signature,
&s->htlc_signature,
&s->tlvs))
return tal_free(s);
return s;
}
static void *towire_struct_revoke_and_ack(const tal_t *ctx,
const struct msg_revoke_and_ack *s)
{
return towire_revoke_and_ack(ctx,
&s->channel_id,
&s->per_commitment_secret,
&s->next_per_commitment_point);
}
static struct msg_revoke_and_ack *fromwire_struct_revoke_and_ack(const tal_t *ctx, const void *p)
{
struct msg_revoke_and_ack *s = tal(ctx, struct msg_revoke_and_ack);
if (!fromwire_revoke_and_ack(p,
&s->channel_id,
&s->per_commitment_secret,
&s->next_per_commitment_point))
return tal_free(s);
return s;
}
static void *towire_struct_funding_signed(const tal_t *ctx,
const struct msg_funding_signed *s)
{
return towire_funding_signed(ctx,
&s->temporary_channel_id,
&s->signature);
}
static struct msg_funding_signed *fromwire_struct_funding_signed(const tal_t *ctx, const void *p)
{
struct msg_funding_signed *s = tal(ctx, struct msg_funding_signed);
if (fromwire_funding_signed(p,
&s->temporary_channel_id,
&s->signature))
return s;
return tal_free(s);
}
static void *towire_struct_closing_signed(const tal_t *ctx,
const struct msg_closing_signed *s)
{
struct tlv_closing_signed_tlvs *close_tlvs;
close_tlvs = tlv_closing_signed_tlvs_new(ctx);
return towire_closing_signed(ctx,
&s->channel_id,
s->fee_satoshis,
&s->signature,
close_tlvs);
}
static struct msg_closing_signed *fromwire_struct_closing_signed(const tal_t *ctx, const void *p)
{
struct msg_closing_signed *s = tal(ctx, struct msg_closing_signed);
struct tlv_closing_signed_tlvs *close_tlvs;
if (fromwire_closing_signed(ctx, p,
&s->channel_id,
&s->fee_satoshis,
&s->signature,
&close_tlvs))
return s;
return tal_free(s);
}
static void *towire_struct_shutdown(const tal_t *ctx,
const struct msg_shutdown *s)
{
return towire_shutdown(ctx,
&s->channel_id,
s->scriptpubkey,
NULL);
}
static struct msg_shutdown *fromwire_struct_shutdown(const tal_t *ctx, const void *p)
{
struct msg_shutdown *s = tal(ctx, struct msg_shutdown);
if (!fromwire_shutdown(s, p,
&s->channel_id,
&s->scriptpubkey,
&s->tlvs))
return tal_free(s);
return s;
}
static void *towire_struct_funding_created(const tal_t *ctx,
const struct msg_funding_created *s)
{
return towire_funding_created(ctx,
&s->temporary_channel_id,
&s->txid,
s->output_index,
&s->signature);
}
static struct msg_funding_created *fromwire_struct_funding_created(const tal_t *ctx, const void *p)
{
struct msg_funding_created *s = tal(ctx, struct msg_funding_created);
if (fromwire_funding_created(p,
&s->temporary_channel_id,
&s->txid,
&s->output_index,
&s->signature))
return s;
return tal_free(s);
}
static void *towire_struct_error(const tal_t *ctx,
const struct msg_error *s)
{
return towire_error(ctx,
&s->channel_id,
s->data);
}
static struct msg_error *fromwire_struct_error(const tal_t *ctx, const void *p)
{
struct msg_error *s = tal(ctx, struct msg_error);
if (!fromwire_error(s, p,
&s->channel_id,
&s->data))
return tal_free(s);
return s;
}
static void *towire_struct_update_add_htlc(const tal_t *ctx,
const struct msg_update_add_htlc *s)
{
return towire_update_add_htlc(ctx,
&s->channel_id,
s->id,
s->amount_msat,
&s->payment_hash,
s->expiry,
s->onion_routing_packet, NULL);
}
static struct msg_update_add_htlc *fromwire_struct_update_add_htlc(const tal_t *ctx, const void *p)
{
struct msg_update_add_htlc *s = tal(ctx, struct msg_update_add_htlc);
if (fromwire_update_add_htlc(s, p,
&s->channel_id,
&s->id,
&s->amount_msat,
&s->payment_hash,
&s->expiry,
s->onion_routing_packet,
&s->tlvs))
return s;
return tal_free(s);
}
static void *towire_struct_update_fee(const tal_t *ctx,
const struct msg_update_fee *s)
{
return towire_update_fee(ctx,
&s->channel_id,
s->feerate_per_kw);
}
static struct msg_update_fee *fromwire_struct_update_fee(const tal_t *ctx, const void *p)
{
struct msg_update_fee *s = tal(ctx, struct msg_update_fee);
if (fromwire_update_fee(p,
&s->channel_id,
&s->feerate_per_kw))
return s;
return tal_free(s);
}
static void *towire_struct_init(const tal_t *ctx,
const struct msg_init *s)
{
return towire_init(ctx,
s->globalfeatures,
s->localfeatures,
s->tlvs);
}
static struct msg_init *fromwire_struct_init(const tal_t *ctx, const void *p)
{
struct msg_init *s = tal(ctx, struct msg_init);
if (!fromwire_init(s, p,
&s->globalfeatures,
&s->localfeatures,
&s->tlvs))
return tal_free(s);
return s;
}
static bool channel_announcement_eq(const struct msg_channel_announcement *a,
const struct msg_channel_announcement *b)
{
return eq_upto(a, b, features)
&& eq_var(a, b, features)
&& eq_field(a, b, chain_hash)
&& short_channel_id_eq(a->short_channel_id,
b->short_channel_id)
&& eq_field(a, b, node_id_1)
&& eq_field(a, b, node_id_2)
&& eq_between(a, b, bitcoin_key_1, bitcoin_key_2);
}
static bool short_channel_id_ptr_eq(const struct short_channel_id *ap,
const struct short_channel_id *bp)
{
return short_channel_id_eq(*ap, *bp);
}
static bool channel_ready_eq(const struct msg_channel_ready *a,
const struct msg_channel_ready *b)
{
if (!eq_upto(a, b, tlvs))
return false;
return eq_tlv(a, b, short_channel_id, short_channel_id_ptr_eq);
}
static bool announcement_signatures_eq(const struct msg_announcement_signatures *a,
const struct msg_announcement_signatures *b)
{
return eq_upto(a, b, short_channel_id) &&
short_channel_id_eq(a->short_channel_id, b->short_channel_id);
}
static bool update_fail_htlc_eq(const struct msg_update_fail_htlc *a,
const struct msg_update_fail_htlc *b)
{
return eq_with(a, b, id)
&& eq_var(a, b, reason);
}
static bool commitment_signed_eq(const struct msg_commitment_signed *a,
const struct msg_commitment_signed *b)
{
return eq_upto(a, b, htlc_signature)
&& eq_var(a, b, htlc_signature)
&& eq_tlv(a, b, splice_info, channel_id_eq);
}
static bool funding_signed_eq(const struct msg_funding_signed *a,
const struct msg_funding_signed *b)
{
return memcmp(a, b, sizeof(*a)) == 0;
}
static bool closing_signed_eq(const struct msg_closing_signed *a,
const struct msg_closing_signed *b)
{
return memcmp(a, b, sizeof(*a)) == 0;
}
static bool update_fulfill_htlc_eq(const struct msg_update_fulfill_htlc *a,
const struct msg_update_fulfill_htlc *b)
{
return memcmp(a, b, sizeof(*a)) == 0;
}
static bool error_eq(const struct msg_error *a,
const struct msg_error *b)
{
return eq_upto(a, b, data)
&& eq_var(a, b, data);
}
static bool tlv_networks_eq(const struct bitcoin_blkid *a,
const struct bitcoin_blkid *b)
{
if (tal_count(a) != tal_count(b))
return false;
for (size_t i = 0; i < tal_count(a); i++)
if (!bitcoin_blkid_eq(&a[i], &b[i]))
return false;
return true;
}
static bool init_eq(const struct msg_init *a,
const struct msg_init *b)
{
if (!eq_var(a, b, globalfeatures) || !eq_var(a, b, localfeatures))
return false;
return eq_tlv(a, b, networks, tlv_networks_eq)
&& eq_tlv(a, b, remote_addr, tal_arr_eq);
}
static bool update_fee_eq(const struct msg_update_fee *a,
const struct msg_update_fee *b)
{
return memcmp(a, b, sizeof(*a)) == 0;
}
static bool shutdown_eq(const struct msg_shutdown *a,
const struct msg_shutdown *b)
{
return eq_upto(a, b, scriptpubkey)
&& eq_var(a, b, scriptpubkey);
}
static bool funding_created_eq(const struct msg_funding_created *a,
const struct msg_funding_created *b)
{
return eq_with(a, b, output_index)
&& eq_field(a, b, signature);
}
static bool revoke_and_ack_eq(const struct msg_revoke_and_ack *a,
const struct msg_revoke_and_ack *b)
{
return memcmp(a, b, sizeof(*a)) == 0;
}
static bool open_channel_eq(const struct msg_open_channel *a,
const struct msg_open_channel *b)
{
return eq_with(a, b, max_accepted_htlcs)
&& eq_between(a, b, funding_pubkey, channel_flags);
}
static bool channel_update_eq(const struct msg_channel_update *a,
const struct msg_channel_update *b)
{
return eq_upto(a, b, short_channel_id) &&
short_channel_id_eq(a->short_channel_id, b->short_channel_id);
}
static bool accept_channel_eq(const struct msg_accept_channel *a,
const struct msg_accept_channel *b)
{
return eq_with(a, b, max_accepted_htlcs)
&& eq_between(a, b, funding_pubkey, first_per_commitment_point);
}
static bool update_add_htlc_eq(const struct msg_update_add_htlc *a,
const struct msg_update_add_htlc *b)
{
return eq_with(a, b, onion_routing_packet);
}
static bool
lease_rates_eq(const struct lease_rates *a,
const struct lease_rates *b)
{
return eq_field(a, b, channel_fee_max_base_msat)
&& eq_field(a, b, channel_fee_max_proportional_thousandths)
&& eq_field(a, b, funding_weight)
&& eq_field(a, b, lease_fee_base_sat)
&& eq_field(a, b, lease_fee_basis);
}
static bool node_announcement_eq(const struct msg_node_announcement *a,
const struct msg_node_announcement *b)
{
if (!eq_with(a, b, node_id)
|| !eq_field(a, b, rgb_color)
|| !eq_field(a, b, alias)
|| !eq_var(a, b, features)
|| !eq_var(a, b, addresses))
return false;
return eq_tlv(a, b, option_will_fund, lease_rates_eq);
}
/* Try flipping each bit, try running short. */
#define test_bitflip_and_short(a, b, type, short_decodefail) \
for (i = 0; i < tal_count(msg) * 8; i++) { \
msg[i / 8] ^= (1 << (i%8)); \
b = fromwire_struct_##type(ctx, msg); \
assert(!b || !type##_eq(a, b)); \
msg[i / 8] ^= (1 << (i%8)); \
} \
for (i = 0; i < tal_count(msg); i++) { \
u8 *trunc = tal_dup_arr(ctx, u8, msg, i, 0); \
b = fromwire_struct_##type(ctx, trunc); \
if (short_decodefail) \
assert(!b); \
else \
assert(!b || !type##_eq(a, b)); \
}
#define test_corruption(a, b, type) \
test_bitflip_and_short(a, b, type, true)
/* If it has a tlv at the end, truncated may still parse! */
#define test_corruption_tlv(a, b, type) \
test_bitflip_and_short(a, b, type, false)
int main(int argc, char *argv[])
{
struct msg_channel_announcement ca, *ca2;
struct msg_channel_ready fl, *fl2;
struct msg_announcement_signatures as, *as2;
struct msg_update_fail_htlc ufh, *ufh2;
struct msg_commitment_signed cs, *cs2;
struct msg_funding_signed fs, *fs2;
struct msg_closing_signed cls, *cls2;
struct msg_update_fulfill_htlc uflh, *uflh2;
struct msg_error e, *e2;
struct msg_init init, *init2;
struct msg_update_fee uf, *uf2;
struct msg_shutdown shutdown, *shutdown2;
struct msg_funding_created fc, *fc2;
struct msg_revoke_and_ack raa, *raa2;
struct msg_open_channel oc, *oc2;
struct msg_channel_update cu, *cu2;
struct msg_accept_channel ac, *ac2;
struct msg_update_add_htlc uah, *uah2;
struct msg_node_announcement na, *na2;
void *ctx = tal(NULL, char);
size_t i;
u8 *msg;
common_setup(argv[0]);
memset(&ca, 2, sizeof(ca));
set_node_id(&ca.node_id_1);
set_node_id(&ca.node_id_2);
set_pubkey(&ca.bitcoin_key_1);
set_pubkey(&ca.bitcoin_key_2);
ca.features = tal_arr(ctx, u8, 2);
memset(ca.features, 2, 2);
msg = towire_struct_channel_announcement(ctx, &ca);
ca2 = fromwire_struct_channel_announcement(ctx, msg);
assert(channel_announcement_eq(&ca, ca2));
test_corruption(&ca, ca2, channel_announcement);
memset(&fl, 2, sizeof(fl));
fl.tlvs = tlv_channel_ready_tlvs_new(ctx);
fl.tlvs->short_channel_id = tal(ctx, struct short_channel_id);
set_scid(fl.tlvs->short_channel_id);
set_pubkey(&fl.next_per_commitment_point);
msg = towire_struct_channel_ready(ctx, &fl);
fl2 = fromwire_struct_channel_ready(ctx, msg);
assert(channel_ready_eq(&fl, fl2));
test_corruption_tlv(&fl, fl2, channel_ready);