channeldb/channel: write boolean to indicate presence of ChannelCloseSummary fields

This commit is contained in:
Johan T. Halseth 2018-11-20 15:09:45 +01:00
parent 149a8ce94f
commit 28b15dcbbb
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26

@ -2059,7 +2059,12 @@ func serializeChannelCloseSummary(w io.Writer, cs *ChannelCloseSummary) error {
// If this is a close channel summary created before the addition of // If this is a close channel summary created before the addition of
// the new fields, then we can exit here. // the new fields, then we can exit here.
if cs.RemoteCurrentRevocation == nil { if cs.RemoteCurrentRevocation == nil {
return nil return WriteElements(w, false)
}
// If fields are present, write boolean to indicate this, and continue.
if err := WriteElements(w, true); err != nil {
return err
} }
if err := WriteElements(w, cs.RemoteCurrentRevocation); err != nil { if err := WriteElements(w, cs.RemoteCurrentRevocation); err != nil {
@ -2070,14 +2075,22 @@ func serializeChannelCloseSummary(w io.Writer, cs *ChannelCloseSummary) error {
return err return err
} }
// We'll write this field last, as it's possible for a channel to be // The RemoteNextRevocation field is optional, as it's possible for a
// closed before we learn of the next unrevoked revocation point for // channel to be closed before we learn of the next unrevoked
// the remote party. // revocation point for the remote party. Write a boolen indicating
if cs.RemoteNextRevocation == nil { // whether this field is present or not.
return nil if err := WriteElements(w, cs.RemoteNextRevocation != nil); err != nil {
return err
} }
return WriteElements(w, cs.RemoteNextRevocation) // Write the field, if present.
if cs.RemoteNextRevocation != nil {
if err = WriteElements(w, cs.RemoteNextRevocation); err != nil {
return err
}
}
return nil
} }
func fetchChannelCloseSummary(tx *bolt.Tx, func fetchChannelCloseSummary(tx *bolt.Tx,
@ -2111,15 +2124,19 @@ func deserializeCloseChannelSummary(r io.Reader) (*ChannelCloseSummary, error) {
// We'll now check to see if the channel close summary was encoded with // We'll now check to see if the channel close summary was encoded with
// any of the additional optional fields. // any of the additional optional fields.
err = ReadElements(r, &c.RemoteCurrentRevocation) var hasNewFields bool
switch { err = ReadElements(r, &hasNewFields)
case err == io.EOF: if err != nil {
return c, nil return nil, err
}
// If we got a non-eof error, then we know there's an actually issue. // If fields are not present, we can return.
// Otherwise, it may have been the case that this summary didn't have if !hasNewFields {
// the set of optional fields. return c, nil
case err != nil: }
// Otherwise read the new fields.
if err := ReadElements(r, &c.RemoteCurrentRevocation); err != nil {
return nil, err return nil, err
} }
@ -2129,17 +2146,22 @@ func deserializeCloseChannelSummary(r io.Reader) (*ChannelCloseSummary, error) {
// Finally, we'll attempt to read the next unrevoked commitment point // Finally, we'll attempt to read the next unrevoked commitment point
// for the remote party. If we closed the channel before receiving a // for the remote party. If we closed the channel before receiving a
// funding locked message, then this can be nil. As a result, we'll use // funding locked message then this might not be present. A boolean
// the same technique to read the field, only if there's still data // indicating whether the field is present will come first.
// left in the buffer. var hasRemoteNextRevocation bool
err = ReadElements(r, &c.RemoteNextRevocation) err = ReadElements(r, &hasRemoteNextRevocation)
if err != nil && err != io.EOF { if err != nil {
// If we got a non-eof error, then we know there's an actually
// issue. Otherwise, it may have been the case that this
// summary didn't have the set of optional fields.
return nil, err return nil, err
} }
// If this field was written, read it.
if hasRemoteNextRevocation {
err = ReadElements(r, &c.RemoteNextRevocation)
if err != nil {
return nil, err
}
}
return c, nil return c, nil
} }