diff --git a/lnwire/htlc_cancel.go b/lnwire/htlc_cancel.go new file mode 100644 index 000000000..ff896daf7 --- /dev/null +++ b/lnwire/htlc_cancel.go @@ -0,0 +1,100 @@ +package lnwire + +import ( + "fmt" + "io" + + "github.com/roasbeef/btcd/wire" +) + +// CancelHTLC is sent by Alice to Bob in order to remove a previously added +// HTLC. Upon receipt of an CancelHTLC the HTLC should be removed from the next +// commitment transaction, with the CancelHTLC propgated backwards in the route +// to fully un-clear the HTLC. +type CancelHTLC struct { + // ChannelPoint is the particular active channel that this CancelHTLC + // is binded to. + ChannelPoint *wire.OutPoint + + // HTLCKey references which HTLC on the remote node's commitment + // transaction has timed out. + HTLCKey HTLCKey +} + +// Decode deserializes a serialized CancelHTLC message stored in the passed +// io.Reader observing the specified protocol version. +// +// This is part of the lnwire.Message interface. +func (c *CancelHTLC) Decode(r io.Reader, pver uint32) error { + // ChannelPoint(8) + // HTLCKey(8) + err := readElements(r, + &c.ChannelPoint, + &c.HTLCKey, + ) + if err != nil { + return err + } + + return nil +} + +// CancelHTLC creates a new CancelHTLC message. +func NewHTLCTimeoutRequest() *CancelHTLC { + return &CancelHTLC{} +} + +// A compile time check to ensure CancelHTLC implements the lnwire.Message +// interface. +var _ Message = (*CancelHTLC)(nil) + +// Encode serializes the target CancelHTLC into the passed io.Writer observing +// the protocol version specified. +// +// This is part of the lnwire.Message interface. +func (c *CancelHTLC) Encode(w io.Writer, pver uint32) error { + err := writeElements(w, + c.ChannelPoint, + c.HTLCKey, + ) + if err != nil { + return err + } + + return nil +} + +// Command returns the integer uniquely identifying this message type on the +// wire. +// +// This is part of the lnwire.Message interface. +func (c *CancelHTLC) Command() uint32 { + return CmdCancelHTLC +} + +// MaxPayloadLength returns the maximum allowed payload size for a CancelHTLC +// complete message observing the specified protocol version. +// +// This is part of the lnwire.Message interface. +func (c *CancelHTLC) MaxPayloadLength(uint32) uint32 { + // 36 + 8 + return 44 +} + +// Validate performs any necessary sanity checks to ensure all fields present +// on the CancelHTLC are valid. +// +// This is part of the lnwire.Message interface. +func (c *CancelHTLC) Validate() error { + // We're good! + return nil +} + +// String returns the string representation of the target CancelHTLC. This is +// part of the lnwire.Message interface. +func (c *CancelHTLC) String() string { + return fmt.Sprintf("\n--- Begin CancelHTLC ---\n") + + fmt.Sprintf("ChannelPoint:\t%d\n", c.ChannelPoint) + + fmt.Sprintf("HTLCKey:\t%d\n", c.HTLCKey) + + fmt.Sprintf("--- End CancelHTLC ---\n") +} diff --git a/lnwire/htlc_cancel_test.go b/lnwire/htlc_cancel_test.go new file mode 100644 index 000000000..4f2984e9c --- /dev/null +++ b/lnwire/htlc_cancel_test.go @@ -0,0 +1,33 @@ +package lnwire + +import ( + "bytes" + "reflect" + "testing" +) + +func TestCancelHTLCEncodeDecode(t *testing.T) { + // First create a new HTLCTR message. + cancelMsg := &CancelHTLC{ + ChannelPoint: outpoint1, + HTLCKey: 22, + } + + // Next encode the HTLCTR message into an empty bytes buffer. + var b bytes.Buffer + if err := cancelMsg.Encode(&b, 0); err != nil { + t.Fatalf("unable to encode CancelHTLC: %v", err) + } + + // Deserialize the encoded HTLCTR message into a new empty struct. + cancelMsg2 := &CancelHTLC{} + if err := cancelMsg2.Decode(&b, 0); err != nil { + t.Fatalf("unable to decode CancelHTLC: %v", err) + } + + // Assert equality of the two instances. + if !reflect.DeepEqual(cancelMsg, cancelMsg2) { + t.Fatalf("encode/decode error messages don't match %#v vs %#v", + cancelMsg, cancelMsg2) + } +} diff --git a/lnwire/htlc_timeoutrequest.go b/lnwire/htlc_timeoutrequest.go deleted file mode 100644 index 1e38e9c88..000000000 --- a/lnwire/htlc_timeoutrequest.go +++ /dev/null @@ -1,100 +0,0 @@ -package lnwire - -import ( - "fmt" - "io" - - "github.com/roasbeef/btcd/wire" -) - -// HTLCTimeoutRequest is sent by Alice to Bob in order to timeout a previously -// added HTLC. Upon receipt of an HTLCTimeoutRequest the HTLC should be removed -// from the next commitment transaction, with the HTLCTimeoutRequest propgated -// backwards in the route to fully clear the HTLC. -type HTLCTimeoutRequest struct { - // ChannelPoint is the particular active channel that this HTLCTimeoutRequest - // is binded to. - ChannelPoint *wire.OutPoint - - // HTLCKey references which HTLC on the remote node's commitment - // transaction has timed out. - HTLCKey HTLCKey -} - -// Decode deserializes a serialized HTLCTimeoutRequest message stored in the passed -// io.Reader observing the specified protocol version. -// -// This is part of the lnwire.Message interface. -func (c *HTLCTimeoutRequest) Decode(r io.Reader, pver uint32) error { - // ChannelPoint(8) - // HTLCKey(8) - err := readElements(r, - &c.ChannelPoint, - &c.HTLCKey, - ) - if err != nil { - return err - } - - return nil -} - -// NewHTLCTimeoutRequest creates a new HTLCTimeoutRequest message. -func NewHTLCTimeoutRequest() *HTLCTimeoutRequest { - return &HTLCTimeoutRequest{} -} - -// A compile time check to ensure HTLCTimeoutRequest implements the lnwire.Message -// interface. -var _ Message = (*HTLCTimeoutRequest)(nil) - -// Encode serializes the target HTLCTimeoutRequest into the passed io.Writer observing -// the protocol version specified. -// -// This is part of the lnwire.Message interface. -func (c *HTLCTimeoutRequest) Encode(w io.Writer, pver uint32) error { - err := writeElements(w, - c.ChannelPoint, - c.HTLCKey, - ) - if err != nil { - return err - } - - return nil -} - -// Command returns the integer uniquely identifying this message type on the -// wire. -// -// This is part of the lnwire.Message interface. -func (c *HTLCTimeoutRequest) Command() uint32 { - return CmdHTLCTimeoutRequest -} - -// MaxPayloadLength returns the maximum allowed payload size for a HTLCTimeoutRequest -// complete message observing the specified protocol version. -// -// This is part of the lnwire.Message interface. -func (c *HTLCTimeoutRequest) MaxPayloadLength(uint32) uint32 { - // 36 + 8 - return 44 -} - -// Validate performs any necessary sanity checks to ensure all fields present -// on the HTLCTimeoutRequest are valid. -// -// This is part of the lnwire.Message interface. -func (c *HTLCTimeoutRequest) Validate() error { - // We're good! - return nil -} - -// String returns the string representation of the target HTLCTimeoutRequest. // -// This is part of the lnwire.Message interface. -func (c *HTLCTimeoutRequest) String() string { - return fmt.Sprintf("\n--- Begin HTLCTimeoutRequest ---\n") + - fmt.Sprintf("ChannelPoint:\t%d\n", c.ChannelPoint) + - fmt.Sprintf("HTLCKey:\t%d\n", c.HTLCKey) + - fmt.Sprintf("--- End HTLCTimeoutRequest ---\n") -} diff --git a/lnwire/htlc_timeoutrequest_test.go b/lnwire/htlc_timeoutrequest_test.go deleted file mode 100644 index e8f5e065a..000000000 --- a/lnwire/htlc_timeoutrequest_test.go +++ /dev/null @@ -1,33 +0,0 @@ -package lnwire - -import ( - "bytes" - "reflect" - "testing" -) - -func TestHTLCTimeoutRequestEncodeDecode(t *testing.T) { - // First create a new HTLCTR message. - timeoutReq := &HTLCTimeoutRequest{ - ChannelPoint: outpoint1, - HTLCKey: 22, - } - - // Next encode the HTLCTR message into an empty bytes buffer. - var b bytes.Buffer - if err := timeoutReq.Encode(&b, 0); err != nil { - t.Fatalf("unable to encode HTLCTimeoutRequest: %v", err) - } - - // Deserialize the encoded HTLCTR message into a new empty struct. - timeoutReq2 := &HTLCTimeoutRequest{} - if err := timeoutReq2.Decode(&b, 0); err != nil { - t.Fatalf("unable to decode HTLCTimeoutRequest: %v", err) - } - - // Assert equality of the two instances. - if !reflect.DeepEqual(timeoutReq, timeoutReq2) { - t.Fatalf("encode/decode error messages don't match %#v vs %#v", - timeoutReq, timeoutReq2) - } -} diff --git a/lnwire/message.go b/lnwire/message.go index 4c291d7cc..049a6ae10 100644 --- a/lnwire/message.go +++ b/lnwire/message.go @@ -34,11 +34,11 @@ const ( CmdCloseComplete = uint32(310) // Commands for negotiating HTLCs. - CmdHTLCAddRequest = uint32(1000) - CmdHTLCAddAccept = uint32(1010) - CmdHTLCAddReject = uint32(1020) - CmdHTLCSettleRequest = uint32(1100) - CmdHTLCTimeoutRequest = uint32(1300) + CmdHTLCAddRequest = uint32(1000) + CmdHTLCAddAccept = uint32(1010) + CmdHTLCAddReject = uint32(1020) + CmdHTLCSettleRequest = uint32(1100) + CmdCancelHTLC = uint32(1300) // Commands for modifying commitment transactions. CmdCommitSignature = uint32(2000) @@ -94,8 +94,8 @@ func makeEmptyMessage(command uint32) (Message, error) { msg = &HTLCAddReject{} case CmdHTLCSettleRequest: msg = &HTLCSettleRequest{} - case CmdHTLCTimeoutRequest: - msg = &HTLCTimeoutRequest{} + case CmdCancelHTLC: + msg = &CancelHTLC{} case CmdCommitSignature: msg = &CommitSignature{} case CmdCommitRevocation: