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