From f29b4f60e464bf3b02f4e695e52b265f9577492d Mon Sep 17 00:00:00 2001 From: Andrey Samokhvalov Date: Sun, 9 Jul 2017 01:12:36 +0300 Subject: [PATCH] lnwire: add BOLT#2 channel reestablish message In this commit the reestablish message have been added, which serves as channel state synchronization message. Before exchanging the messages for particular channel peers have to send it to each other as the first message in order to be sure that non of the updates have been lost because of the previous disconnect. --- lnwire/channel_reestablish.go | 73 +++++++++++++++++++++++++++++++++++ lnwire/lnwire_test.go | 6 +++ lnwire/message.go | 5 +++ 3 files changed, 84 insertions(+) create mode 100644 lnwire/channel_reestablish.go diff --git a/lnwire/channel_reestablish.go b/lnwire/channel_reestablish.go new file mode 100644 index 00000000..809e5bc9 --- /dev/null +++ b/lnwire/channel_reestablish.go @@ -0,0 +1,73 @@ +package lnwire + +import "io" + +// ChannelReestablish is sent during node reconnection for every channel +// established in order to synchronize the states on both sides. +type ChannelReestablish struct { + // ChanID serves to identify to which channel this message belongs. + ChanID ChannelID + + // NextLocalCommitmentNumber is the commitment number of the next + // commitment signed message it expects to receive. + NextLocalCommitmentNumber uint64 + + // NextRemoteRevocationNumber is the commitment number of the next + // revoke and ack message it expects to receive. + NextRemoteRevocationNumber uint64 +} + +// A compile time check to ensure ChannelReestablish implements the +// lnwire.Message interface. +var _ Message = (*ChannelReestablish)(nil) + +// 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 { + return readElements(r, + &a.ChanID, + &a.NextLocalCommitmentNumber, + &a.NextRemoteRevocationNumber, + ) +} + +// 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 { + return writeElements(w, + a.ChanID, + a.NextLocalCommitmentNumber, + a.NextRemoteRevocationNumber, + ) +} + +// 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 + + // NextLocalCommitmentNumber - 8 bytes + length += 8 + + // NextRemoteRevocationNumber - 8 bytes + length += 8 + + return length +} diff --git a/lnwire/lnwire_test.go b/lnwire/lnwire_test.go index 43521dd9..191a6697 100644 --- a/lnwire/lnwire_test.go +++ b/lnwire/lnwire_test.go @@ -563,6 +563,12 @@ func TestLightningWireProtocol(t *testing.T) { return mainScenario(&m) }, }, + { + msgType: MsgChannelReestablish, + scenario: func(m ChannelReestablish) bool { + return mainScenario(&m) + }, + }, { msgType: MsgChannelAnnouncement, scenario: func(m ChannelAnnouncement) bool { diff --git a/lnwire/message.go b/lnwire/message.go index f41ca3a5..9dc0b6b5 100644 --- a/lnwire/message.go +++ b/lnwire/message.go @@ -40,6 +40,7 @@ const ( MsgCommitSig = 132 MsgRevokeAndAck = 133 MsgUpdateFailMalformedHTLC = 135 + MsgChannelReestablish = 136 MsgUpdateFee = 137 MsgChannelAnnouncement = 256 MsgNodeAnnouncement = 257 @@ -78,6 +79,8 @@ func (t MessageType) String() string { return "RevokeAndAck" case MsgUpdateFailMalformedHTLC: return "UpdateFailMalformedHTLC" + case MsgChannelReestablish: + return "ChannelReestablish" case MsgError: return "Error" case MsgChannelAnnouncement: @@ -169,6 +172,8 @@ func makeEmptyMessage(msgType MessageType) (Message, error) { msg = &UpdateFee{} case MsgUpdateFailMalformedHTLC: msg = &UpdateFailMalformedHTLC{} + case MsgChannelReestablish: + msg = &ChannelReestablish{} case MsgError: msg = &Error{} case MsgChannelAnnouncement: