From abbbfac8e23318dd30e72ae8a81b48cc451578fa Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 11 Apr 2018 08:33:35 +0930 Subject: [PATCH] gossipd: return bool from message announce routines. Now we can tell if they fail, so we can respond appropriately if we're loading from the store. Signed-off-by: Rusty Russell --- gossipd/routing.c | 34 +++++++++++++++++++++++----------- gossipd/routing.h | 4 ++-- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/gossipd/routing.c b/gossipd/routing.c index 47a7ca6de..b9a304202 100644 --- a/gossipd/routing.c +++ b/gossipd/routing.c @@ -591,7 +591,7 @@ static void destroy_pending_cannouncement(struct pending_cannouncement *pending, list_del_from(&rstate->pending_cannouncement, &pending->list); } -void routing_add_channel_announcement(struct routing_state *rstate, +bool routing_add_channel_announcement(struct routing_state *rstate, const u8 *msg TAKES, u64 satoshis) { struct chan *chan; @@ -627,13 +627,15 @@ void routing_add_channel_announcement(struct routing_state *rstate, chan->satoshis = satoshis; if (replace_broadcast(chan, rstate->broadcasts, - &chan->channel_announce_msgidx, take(msg))) - status_failed(STATUS_FAIL_INTERNAL_ERROR, - "Announcement %s was replaced: %s, %s, msgidx was %"PRIu64" now %"PRIu64"?", + &chan->channel_announce_msgidx, take(msg))) { + status_broken("Announcement %s was replaced: %s, %s, msgidx was %"PRIu64" now %"PRIu64"?", tal_hex(tmpctx, msg), old_chan ? "preexisting" : "new channel", old_public ? "public" : "not public", old_msgidx, chan->channel_announce_msgidx); + return false; + } + return true; } u8 *handle_channel_announcement(struct routing_state *rstate, @@ -837,7 +839,9 @@ bool handle_pending_cannouncement(struct routing_state *rstate, } gossip_store_add_channel_announcement(rstate->store, pending->announce, satoshis); - routing_add_channel_announcement(rstate, pending->announce, satoshis); + if (!routing_add_channel_announcement(rstate, pending->announce, satoshis)) + status_failed(STATUS_FAIL_INTERNAL_ERROR, + "Could not add channel_announcement"); local = pubkey_eq(&pending->node_id_1, &rstate->local_id) || pubkey_eq(&pending->node_id_2, &rstate->local_id); @@ -908,7 +912,7 @@ void set_connection_values(struct chan *chan, } } -void routing_add_channel_update(struct routing_state *rstate, +bool routing_add_channel_update(struct routing_state *rstate, const u8 *update TAKES) { secp256k1_ecdsa_signature signature; @@ -923,11 +927,15 @@ void routing_add_channel_update(struct routing_state *rstate, struct chan *chan; u8 direction; - fromwire_channel_update(update, &signature, &chain_hash, - &short_channel_id, ×tamp, &flags, &expiry, - &htlc_minimum_msat, &fee_base_msat, - &fee_proportional_millionths); + if (!fromwire_channel_update(update, &signature, &chain_hash, + &short_channel_id, ×tamp, &flags, + &expiry, &htlc_minimum_msat, &fee_base_msat, + &fee_proportional_millionths)) + return false; chan = get_channel(rstate, &short_channel_id); + if (!chan) + return false; + direction = flags & 0x1; set_connection_values(chan, direction, fee_base_msat, fee_proportional_millionths, expiry, @@ -937,6 +945,7 @@ void routing_add_channel_update(struct routing_state *rstate, replace_broadcast(chan, rstate->broadcasts, &chan->half[direction].channel_update_msgidx, take(update)); + return true; } u8 *handle_channel_update(struct routing_state *rstate, const u8 *update) @@ -1034,7 +1043,10 @@ u8 *handle_channel_update(struct routing_state *rstate, const u8 *update) /* Only store updates for public channels */ if (chan->public) gossip_store_add_channel_update(rstate->store, serialized); - routing_add_channel_update(rstate, serialized); + if (!routing_add_channel_update(rstate, serialized)) + status_failed(STATUS_FAIL_INTERNAL_ERROR, + "Failed adding channel_update"); + return NULL; } diff --git a/gossipd/routing.h b/gossipd/routing.h index 7f99b2aab..715c21b2f 100644 --- a/gossipd/routing.h +++ b/gossipd/routing.h @@ -265,7 +265,7 @@ void route_prune(struct routing_state *rstate); * this only for messages from trusted sources. Untrusted sources should use the * @see{handle_channel_announcement} entrypoint to check before adding. */ -void routing_add_channel_announcement(struct routing_state *rstate, +bool routing_add_channel_announcement(struct routing_state *rstate, const u8 *msg TAKES, u64 satoshis); /** @@ -276,7 +276,7 @@ void routing_add_channel_announcement(struct routing_state *rstate, * untrusted source, requiring verification please use * @see{handle_channel_update} */ -void routing_add_channel_update(struct routing_state *rstate, +bool routing_add_channel_update(struct routing_state *rstate, const u8 *update TAKES); /**