From 1b6bd3fded8d8bbfc679aedd78eedaa9efedb01d Mon Sep 17 00:00:00 2001 From: lisa neigut Date: Thu, 4 Oct 2018 13:34:53 -0700 Subject: [PATCH] wire: add test for parsing optional version of channel_update --- gossipd/routing.c | 16 ++++++++-- wire/test/run-peer-wire.c | 65 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 3 deletions(-) diff --git a/gossipd/routing.c b/gossipd/routing.c index df37ba2fb..8bb3a97b7 100644 --- a/gossipd/routing.c +++ b/gossipd/routing.c @@ -1063,12 +1063,22 @@ bool routing_add_channel_update(struct routing_state *rstate, struct chan *chan; u8 direction; - if (!fromwire_channel_update_option_channel_htlc_max(update, &signature, &chain_hash, + if (!fromwire_channel_update(update, &signature, &chain_hash, &short_channel_id, ×tamp, &message_flags, &channel_flags, &expiry, &htlc_minimum_msat, &fee_base_msat, - &fee_proportional_millionths, - &htlc_maximum_msat)) + &fee_proportional_millionths)) + return false; + /* If it's flagged as containing the optional field, reparse for + * the optional field */ + if ((message_flags & ROUTING_OPT_HTLC_MAX_MSAT) && + !fromwire_channel_update_option_channel_htlc_max( + update, &signature, &chain_hash, + &short_channel_id, ×tamp, + &message_flags, &channel_flags, + &expiry, &htlc_minimum_msat, &fee_base_msat, + &fee_proportional_millionths, + &htlc_maximum_msat)) return false; chan = get_channel(rstate, &short_channel_id); if (!chan) diff --git a/wire/test/run-peer-wire.c b/wire/test/run-peer-wire.c index d21778a30..5e156ed1f 100644 --- a/wire/test/run-peer-wire.c +++ b/wire/test/run-peer-wire.c @@ -132,6 +132,19 @@ struct msg_channel_update { struct bitcoin_blkid chain_hash; struct short_channel_id short_channel_id; }; +struct msg_channel_update_opt_htlc_max { + secp256k1_ecdsa_signature signature; + u32 timestamp; + u8 message_flags; + u8 channel_flags; + u16 expiry; + u64 htlc_minimum_msat; + u32 fee_base_msat; + u32 fee_proportional_millionths; + u64 htlc_maximum_msat; + struct bitcoin_blkid chain_hash; + struct short_channel_id short_channel_id; +}; struct msg_funding_locked { struct channel_id channel_id; struct pubkey next_per_commitment_point; @@ -385,6 +398,22 @@ static void *towire_struct_channel_update(const tal_t *ctx, s->fee_proportional_millionths); } +static void *towire_struct_channel_update_opt_htlc_max(const tal_t *ctx, + const struct msg_channel_update_opt_htlc_max *s) +{ + return towire_channel_update_option_channel_htlc_max(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); @@ -404,6 +433,27 @@ static struct msg_channel_update *fromwire_struct_channel_update(const tal_t *ct return tal_free(s); } +static struct msg_channel_update_opt_htlc_max +*fromwire_struct_channel_update_opt_htlc_max(const tal_t *ctx, const void *p) +{ + struct msg_channel_update_opt_htlc_max *s = tal(ctx, struct msg_channel_update_opt_htlc_max); + + if (fromwire_channel_update_option_channel_htlc_max(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_funding_locked(const tal_t *ctx, const struct msg_funding_locked *s) { @@ -810,6 +860,13 @@ static bool channel_update_eq(const struct msg_channel_update *a, short_channel_id_eq(&a->short_channel_id, &b->short_channel_id); } +static bool channel_update_opt_htlc_max_eq(const struct msg_channel_update_opt_htlc_max *a, + const struct msg_channel_update_opt_htlc_max *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) { @@ -865,6 +922,7 @@ int main(void) struct msg_revoke_and_ack raa, *raa2; struct msg_open_channel oc, *oc2; struct msg_channel_update cu, *cu2; + struct msg_channel_update_opt_htlc_max cu_opt_htlc_max, *cu_opt_htlc_max2; struct msg_accept_channel ac, *ac2; struct msg_update_add_htlc uah, *uah2; struct msg_node_announcement na, *na2; @@ -1013,6 +1071,13 @@ int main(void) assert(channel_update_eq(&cu, cu2)); test_corruption(&cu, cu2, channel_update); + memset(&cu_opt_htlc_max, 2, sizeof(cu_opt_htlc_max)); + + msg = towire_struct_channel_update_opt_htlc_max(ctx, &cu_opt_htlc_max); + cu_opt_htlc_max2 = fromwire_struct_channel_update_opt_htlc_max(ctx, msg); + assert(channel_update_opt_htlc_max_eq(&cu_opt_htlc_max, cu_opt_htlc_max2)); + test_corruption(&cu_opt_htlc_max, cu_opt_htlc_max2, channel_update_opt_htlc_max); + memset(&ac, 2, sizeof(ac)); set_pubkey(&ac.funding_pubkey); set_pubkey(&ac.revocation_basepoint);