2017-07-09 00:12:36 +02:00
|
|
|
package lnwire
|
|
|
|
|
2017-11-14 07:28:46 +01:00
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
2018-06-05 03:34:16 +02:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
2017-11-14 07:28:46 +01:00
|
|
|
)
|
2017-07-09 00:12:36 +02:00
|
|
|
|
2017-11-10 06:03:09 +01:00
|
|
|
// ChannelReestablish is a message sent between peers that have an existing
|
|
|
|
// open channel upon connection reestablishment. This message allows both sides
|
|
|
|
// to report their local state, and their current knowledge of the state of the
|
|
|
|
// remote commitment chain. If a deviation is detected and can be recovered
|
|
|
|
// from, then the necessary messages will be retransmitted. If the level of
|
|
|
|
// desynchronization if irreconcilable, then the channel will be force closed.
|
2017-07-09 00:12:36 +02:00
|
|
|
type ChannelReestablish struct {
|
2017-11-10 06:03:09 +01:00
|
|
|
// ChanID is the channel ID of the channel state we're attempting
|
|
|
|
// synchronize with the remote party.
|
2017-07-09 00:12:36 +02:00
|
|
|
ChanID ChannelID
|
|
|
|
|
2017-11-10 06:03:09 +01:00
|
|
|
// NextLocalCommitHeight is the next local commitment height of the
|
|
|
|
// sending party. If the height of the sender's commitment chain from
|
|
|
|
// the receiver's Pov is one less that this number, then the sender
|
|
|
|
// should re-send the *exact* same proposed commitment.
|
|
|
|
//
|
|
|
|
// In other words, the receiver should re-send their last sent
|
|
|
|
// commitment iff:
|
|
|
|
//
|
|
|
|
// * NextLocalCommitHeight == remoteCommitChain.Height
|
|
|
|
//
|
|
|
|
// This covers the case of a lost commitment which was sent by the
|
|
|
|
// sender of this message, but never received by the receiver of this
|
|
|
|
// message.
|
|
|
|
NextLocalCommitHeight uint64
|
2017-07-09 00:12:36 +02:00
|
|
|
|
2017-11-10 06:03:09 +01:00
|
|
|
// RemoteCommitTailHeight is the height of the receiving party's
|
|
|
|
// unrevoked commitment from the PoV of the sender of this message. If
|
|
|
|
// the height of the receiver's commitment is *one more* than this
|
|
|
|
// value, then their prior RevokeAndAck message should be
|
|
|
|
// retransmitted.
|
|
|
|
//
|
|
|
|
// In other words, the receiver should re-send their last sent
|
|
|
|
// RevokeAndAck message iff:
|
|
|
|
//
|
|
|
|
// * localCommitChain.tail().Height == RemoteCommitTailHeight + 1
|
|
|
|
//
|
|
|
|
// This covers the case of a lost revocation, wherein the receiver of
|
|
|
|
// the message sent a revocation for a prior state, but the sender of
|
|
|
|
// the message never fully processed it.
|
|
|
|
RemoteCommitTailHeight uint64
|
2017-11-14 07:28:46 +01:00
|
|
|
|
|
|
|
// LastRemoteCommitSecret is the last commitment secret that the
|
|
|
|
// receiving node has sent to the sending party. This will be the
|
|
|
|
// secret of the last revoked commitment transaction. Including this
|
|
|
|
// provides proof that the sending node at least knows of this state,
|
|
|
|
// as they couldn't have produced it if it wasn't sent, as the value
|
|
|
|
// can be authenticated by querying the shachain or the receiving
|
|
|
|
// party.
|
|
|
|
LastRemoteCommitSecret [32]byte
|
|
|
|
|
|
|
|
// LocalUnrevokedCommitPoint is the commitment point used in the
|
|
|
|
// current un-revoked commitment transaction of the sending party.
|
|
|
|
LocalUnrevokedCommitPoint *btcec.PublicKey
|
2017-07-09 00:12:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// A compile time check to ensure ChannelReestablish implements the
|
|
|
|
// lnwire.Message interface.
|
|
|
|
var _ Message = (*ChannelReestablish)(nil)
|
|
|
|
|
|
|
|
// Encode serializes the target ChannelReestablish into the passed io.Writer
|
|
|
|
// observing the protocol version specified.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
|
|
|
func (a *ChannelReestablish) Encode(w io.Writer, pver uint32) error {
|
2018-12-10 03:27:41 +01:00
|
|
|
err := WriteElements(w,
|
2017-07-09 00:12:36 +02:00
|
|
|
a.ChanID,
|
2017-11-10 06:03:09 +01:00
|
|
|
a.NextLocalCommitHeight,
|
|
|
|
a.RemoteCommitTailHeight,
|
|
|
|
)
|
2017-11-14 07:28:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the commit point wasn't sent, then we won't write out any of the
|
|
|
|
// remaining fields as they're optional.
|
|
|
|
if a.LocalUnrevokedCommitPoint == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we'll write out the remaining elements.
|
2018-12-10 03:27:41 +01:00
|
|
|
return WriteElements(w, a.LastRemoteCommitSecret[:],
|
2017-11-14 07:28:46 +01:00
|
|
|
a.LocalUnrevokedCommitPoint)
|
2017-11-10 06:03:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Decode deserializes a serialized ChannelReestablish stored in the passed
|
|
|
|
// io.Reader observing the specified protocol version.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
|
|
|
func (a *ChannelReestablish) Decode(r io.Reader, pver uint32) error {
|
2018-12-10 03:27:41 +01:00
|
|
|
err := ReadElements(r,
|
2017-11-10 06:03:09 +01:00
|
|
|
&a.ChanID,
|
|
|
|
&a.NextLocalCommitHeight,
|
|
|
|
&a.RemoteCommitTailHeight,
|
2017-07-09 00:12:36 +02:00
|
|
|
)
|
2017-11-14 07:28:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// This message has to currently defined optional fields. As a result,
|
|
|
|
// we'll only proceed if there's still bytes remaining within the
|
|
|
|
// reader.
|
|
|
|
//
|
|
|
|
// We'll manually parse out the optional fields in order to be able to
|
|
|
|
// still utilize the io.Reader interface.
|
|
|
|
|
|
|
|
// We'll first attempt to read the optional commit secret, if we're at
|
|
|
|
// the EOF, then this means the field wasn't included so we can exit
|
|
|
|
// early.
|
|
|
|
var buf [32]byte
|
|
|
|
_, err = io.ReadFull(r, buf[:32])
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the field is present, then we'll copy it over and proceed.
|
|
|
|
copy(a.LastRemoteCommitSecret[:], buf[:])
|
|
|
|
|
|
|
|
// We'll conclude by parsing out the commitment point. We don't check
|
|
|
|
// the error in this case, as it hey included the commit secret, then
|
|
|
|
// they MUST also include the commit point.
|
2018-12-10 03:27:41 +01:00
|
|
|
return ReadElement(r, &a.LocalUnrevokedCommitPoint)
|
2017-07-09 00:12:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MsgType returns the integer uniquely identifying this message type on the
|
|
|
|
// wire.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
|
|
|
func (a *ChannelReestablish) MsgType() MessageType {
|
|
|
|
return MsgChannelReestablish
|
|
|
|
}
|
|
|
|
|
|
|
|
// MaxPayloadLength returns the maximum allowed payload size for this message
|
|
|
|
// observing the specified protocol version.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
|
|
|
func (a *ChannelReestablish) MaxPayloadLength(pver uint32) uint32 {
|
|
|
|
var length uint32
|
|
|
|
|
|
|
|
// ChanID - 32 bytes
|
|
|
|
length += 32
|
|
|
|
|
2017-11-10 06:03:09 +01:00
|
|
|
// NextLocalCommitHeight - 8 bytes
|
2017-07-09 00:12:36 +02:00
|
|
|
length += 8
|
|
|
|
|
2017-11-10 06:03:09 +01:00
|
|
|
// RemoteCommitTailHeight - 8 bytes
|
2017-07-09 00:12:36 +02:00
|
|
|
length += 8
|
|
|
|
|
2017-11-14 07:28:46 +01:00
|
|
|
// LastRemoteCommitSecret - 32 bytes
|
|
|
|
length += 32
|
|
|
|
|
|
|
|
// LocalUnrevokedCommitPoint - 33 bytes
|
|
|
|
length += 33
|
|
|
|
|
2017-07-09 00:12:36 +02:00
|
|
|
return length
|
|
|
|
}
|