From 0b9a323fcbb972dffe2166fa345ff17b3350df38 Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Tue, 20 Nov 2018 15:09:45 +0100 Subject: [PATCH] channeldb/channel: add LastChanSync field to CloseChannelSummary This commit adds an optional field LastChanSyncMsg to the CloseChannelSummary, which will be used to save the ChannelReestablish message for the channel at the point of channel close. --- channeldb/channel.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/channeldb/channel.go b/channeldb/channel.go index 99df5258..5a6245b8 100644 --- a/channeldb/channel.go +++ b/channeldb/channel.go @@ -3,6 +3,7 @@ package channeldb import ( "bytes" "encoding/binary" + "errors" "fmt" "io" "net" @@ -1832,6 +1833,10 @@ type ChannelCloseSummary struct { // LocalChanCfg is the channel configuration for the local node. LocalChanConfig ChannelConfig + + // LastChanSyncMsg is the ChannelReestablish message for this channel + // for the state at the point where it was closed. + LastChanSyncMsg *lnwire.ChannelReestablish } // CloseChannel closes a previously active Lightning channel. Closing a channel @@ -2090,6 +2095,18 @@ func serializeChannelCloseSummary(w io.Writer, cs *ChannelCloseSummary) error { } } + // Write whether the channel sync message is present. + if err := WriteElements(w, cs.LastChanSyncMsg != nil); err != nil { + return err + } + + // Write the channel sync message, if present. + if cs.LastChanSyncMsg != nil { + if err := WriteElements(w, cs.LastChanSyncMsg); err != nil { + return err + } + } + return nil } @@ -2162,6 +2179,32 @@ func deserializeCloseChannelSummary(r io.Reader) (*ChannelCloseSummary, error) { } } + // Check if we have a channel sync message to read. + var hasChanSyncMsg bool + err = ReadElements(r, &hasChanSyncMsg) + if err == io.EOF { + return c, nil + } else if err != nil { + return nil, err + } + + // If a chan sync message is present, read it. + if hasChanSyncMsg { + // We must pass in reference to a lnwire.Message for the codec + // to support it. + var msg lnwire.Message + if err := ReadElements(r, &msg); err != nil { + return nil, err + } + + chanSync, ok := msg.(*lnwire.ChannelReestablish) + if !ok { + return nil, errors.New("unable cast db Message to " + + "ChannelReestablish") + } + c.LastChanSyncMsg = chanSync + } + return c, nil }