From 6fcc76fc68fb4ac7f5476ce6ddbe622ac1fca899 Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Sat, 12 Jan 2019 18:59:44 +0100 Subject: [PATCH] channeldb/channel: ignore ChannelEdgePolicy with missing max_htlc If the max_htlc field is not found when fetching a ChannelEdgePolicy from the DB, we treat this as an unknown policy. This is done to ensure we won't propagate invalid data further. The data will be overwritten with a valid one when we receive an update for this channel. It shouldn't be very common, but old data could be lingering in the DB added before this field was validated. --- channeldb/graph.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/channeldb/graph.go b/channeldb/graph.go index 085d46e7..19a19439 100644 --- a/channeldb/graph.go +++ b/channeldb/graph.go @@ -3204,11 +3204,15 @@ func putChanEdgePolicy(edges, nodes *bbolt.Bucket, edge *ChannelEdgePolicy, // *prior* update time in order to delete it. To do this, we'll // need to deserialize the existing policy within the database // (now outdated by the new one), and delete its corresponding - // entry within the update index. + // entry within the update index. We'll ignore any + // ErrEdgePolicyOptionalFieldNotFound error, as we only need + // the channel ID and update time to delete the entry. + // TODO(halseth): get rid of these invalid policies in a + // migration. oldEdgePolicy, err := deserializeChanEdgePolicy( bytes.NewReader(edgeBytes), nodes, ) - if err != nil { + if err != nil && err != ErrEdgePolicyOptionalFieldNotFound { return err } @@ -3266,7 +3270,18 @@ func fetchChanEdgePolicy(edges *bbolt.Bucket, chanID []byte, edgeReader := bytes.NewReader(edgeBytes) - return deserializeChanEdgePolicy(edgeReader, nodes) + ep, err := deserializeChanEdgePolicy(edgeReader, nodes) + switch { + // If the db policy was missing an expected optional field, we return + // nil as if the policy was unknown. + case err == ErrEdgePolicyOptionalFieldNotFound: + return nil, nil + + case err != nil: + return nil, err + } + + return ep, nil } func fetchChanEdgePolicies(edgeIndex *bbolt.Bucket, edges *bbolt.Bucket,