From 5330513c7b33de0569f2c38e8d5089be2e2e49d8 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Thu, 16 Feb 2017 20:34:09 +0800 Subject: [PATCH] lnwire: morph HTLCSettleRequest into UpdateFufillHTLC MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit modifies the prior HTLCSettleRequest to more closely match the UpdateFufillHTLC defined within the specification. The only semantic change is the move from a slice of pre-images (for “multi-sig” LN) to a single payment preimage. --- lnwire/htlc_settlerequest.go | 108 ---------------- lnwire/message.go | 6 +- lnwire/update_fulfill_htlc.go | 116 ++++++++++++++++++ ...st_test.go => update_fulfill_htlc_test.go} | 13 +- 4 files changed, 124 insertions(+), 119 deletions(-) delete mode 100644 lnwire/htlc_settlerequest.go create mode 100644 lnwire/update_fulfill_htlc.go rename lnwire/{htlc_settlerequest_test.go => update_fulfill_htlc_test.go} (60%) diff --git a/lnwire/htlc_settlerequest.go b/lnwire/htlc_settlerequest.go deleted file mode 100644 index 6a086db46..000000000 --- a/lnwire/htlc_settlerequest.go +++ /dev/null @@ -1,108 +0,0 @@ -package lnwire - -import ( - "io" - - "github.com/roasbeef/btcd/wire" -) - -// HTLCSettleRequest is sent by Alice to Bob when she wishes to settle a -// particular HTLC referenced by its HTLCKey within a specific active channel -// referenced by ChannelPoint. The message allows multiple hash preimages to be -// presented in order to support N-of-M HTLC contracts. A subsequent -// CommitSignature message will be sent by Alice to "lock-in" the removal of the -// specified HTLC, possible containing a batch signature covering several settled -// HTLCs. -type HTLCSettleRequest struct { - // ChannelPoint references an active channel which holds the HTLC to be - // settled. - ChannelPoint *wire.OutPoint - - // HTLCKey denotes the exact HTLC stage within the receiving node's - // commitment transaction to be removed. - // TODO(roasbeef): rename to LogIndex - HTLCKey HTLCKey - - // RedemptionProofs are the R-value preimages required to fully settle - // an HTLC. The number of preimages in the slice will depend on the - // specific ContractType of the referenced HTLC. - RedemptionProofs [][32]byte -} - -// NewHTLCSettleRequest returns a new empty HTLCSettleRequest. -func NewHTLCSettleRequest(chanPoint *wire.OutPoint, key HTLCKey, - redemptionProofs [][32]byte) *HTLCSettleRequest { - - return &HTLCSettleRequest{ - ChannelPoint: chanPoint, - HTLCKey: key, - RedemptionProofs: redemptionProofs, - } -} - -// A compile time check to ensure HTLCSettleRequest implements the lnwire.Message -// interface. -var _ Message = (*HTLCSettleRequest)(nil) - -// Decode deserializes a serialized HTLCSettleRequest message stored in the passed -// io.Reader observing the specified protocol version. -// -// This is part of the lnwire.Message interface. -func (c *HTLCSettleRequest) Decode(r io.Reader, pver uint32) error { - // ChannelPoint(8) - // HTLCKey(8) - // RedemptionProofs(N*32) - err := readElements(r, - &c.ChannelPoint, - &c.HTLCKey, - &c.RedemptionProofs, - ) - if err != nil { - return err - } - - return nil -} - -// Encode serializes the target HTLCSettleRequest into the passed io.Writer -// observing the protocol version specified. -// -// This is part of the lnwire.Message interface. -func (c *HTLCSettleRequest) Encode(w io.Writer, pver uint32) error { - err := writeElements(w, - c.ChannelPoint, - c.HTLCKey, - c.RedemptionProofs, - ) - 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 *HTLCSettleRequest) Command() uint32 { - return CmdHTLCSettleRequest -} - -// MaxPayloadLength returns the maximum allowed payload size for a HTLCSettleRequest -// complete message observing the specified protocol version. -// -// This is part of the lnwire.Message interface. -func (c *HTLCSettleRequest) MaxPayloadLength(uint32) uint32 { - // 36 + 8 + (32 * 15) - return 524 -} - -// Validate performs any necessary sanity checks to ensure all fields present -// on the HTLCSettleRequest are valid. -// -// This is part of the lnwire.Message interface. -func (c *HTLCSettleRequest) Validate() error { - // We're good! - return nil -} diff --git a/lnwire/message.go b/lnwire/message.go index 02ff5aa45..dbadf2f8b 100644 --- a/lnwire/message.go +++ b/lnwire/message.go @@ -37,8 +37,8 @@ const ( CmdCloseComplete = uint32(310) // Commands for negotiating HTLCs. - CmdHTLCSettleRequest = uint32(1100) CmdUpdateAddHTLC = uint32(1000) + CmdUpdateFufillHTLC = uint32(1010) CmdUpdateFailHTLC = uint32(1020) // Commands for modifying commitment transactions. @@ -105,12 +105,12 @@ func makeEmptyMessage(command uint32) (Message, error) { msg = &CloseRequest{} case CmdCloseComplete: msg = &CloseComplete{} - case CmdHTLCSettleRequest: - msg = &HTLCSettleRequest{} case CmdUpdateAddHTLC: msg = &UpdateAddHTLC{} case CmdUpdateFailHTLC: msg = &UpdateFailHTLC{} + case CmdUpdateFufillHTLC: + msg = &UpdateFufillHTLC{} case CmdCommitSig: msg = &CommitSig{} case CmdRevokeAndAck: diff --git a/lnwire/update_fulfill_htlc.go b/lnwire/update_fulfill_htlc.go new file mode 100644 index 000000000..ebf5d5018 --- /dev/null +++ b/lnwire/update_fulfill_htlc.go @@ -0,0 +1,116 @@ +package lnwire + +import ( + "fmt" + "io" + + "github.com/roasbeef/btcd/wire" +) + +// UpdateFufillHTLC is sent by Alice to Bob when she wishes to settle a +// particular HTLC referenced by its HTLCKey within a specific active channel +// referenced by ChannelPoint. A subsequent CommitSig message will be sent by +// Alice to "lock-in" the removal of the specified HTLC, possible containing a +// batch signature covering several settled HTLC's. +type UpdateFufillHTLC struct { + // ChannelPoint references an active channel which holds the HTLC to be + // settled. + ChannelPoint wire.OutPoint + + // ID denotes the exact HTLC stage within the receiving node's + // commitment transaction to be removed. + ID uint64 + + // PaymentPreimage is the R-value preimage required to fully settle an + // HTLC. + PaymentPreimage [32]byte +} + +// NewUpdateFufillHTLC returns a new empty UpdateFufillHTLC. +func NewUpdateFufillHTLC(chanPoint wire.OutPoint, id uint64, + preimage [32]byte) *UpdateFufillHTLC { + + return &UpdateFufillHTLC{ + ChannelPoint: chanPoint, + ID: id, + PaymentPreimage: preimage, + } +} + +// A compile time check to ensure UpdateFufillHTLC implements the lnwire.Message +// interface. +var _ Message = (*UpdateFufillHTLC)(nil) + +// Decode deserializes a serialized UpdateFufillHTLC message stored in the passed +// io.Reader observing the specified protocol version. +// +// This is part of the lnwire.Message interface. +func (c *UpdateFufillHTLC) Decode(r io.Reader, pver uint32) error { + // ChannelPoint(8) + // ID(8) + // PaymentPreimage(32) + err := readElements(r, + &c.ChannelPoint, + &c.ID, + c.PaymentPreimage[:], + ) + if err != nil { + return err + } + + return nil +} + +// Encode serializes the target UpdateFufillHTLC into the passed io.Writer +// observing the protocol version specified. +// +// This is part of the lnwire.Message interface. +func (c *UpdateFufillHTLC) Encode(w io.Writer, pver uint32) error { + err := writeElements(w, + c.ChannelPoint, + c.ID, + c.PaymentPreimage[:], + ) + 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 *UpdateFufillHTLC) Command() uint32 { + return CmdUpdateFufillHTLC +} + +// MaxPayloadLength returns the maximum allowed payload size for a UpdateFufillHTLC +// complete message observing the specified protocol version. +// +// This is part of the lnwire.Message interface. +func (c *UpdateFufillHTLC) MaxPayloadLength(uint32) uint32 { + // 36 + 8 + (32 * 15) + return 524 +} + +// Validate performs any necessary sanity checks to ensure all fields present +// on the UpdateFufillHTLC are valid. +// +// This is part of the lnwire.Message interface. +func (c *UpdateFufillHTLC) Validate() error { + // We're good! + return nil +} + +// String returns the string representation of the target UpdateFufillHTLC. +// +// This is part of the lnwire.Message interface. +func (c *UpdateFufillHTLC) String() string { + return fmt.Sprintf("\n--- Begin UpdateFufillHTLC ---\n") + + fmt.Sprintf("ChannelPoint:\t%v\n", c.ChannelPoint) + + fmt.Sprintf("ID:\t%d\n", c.ID) + + fmt.Sprintf("RedemptionHashes:\t\t%x\n", c.PaymentPreimage) + + fmt.Sprintf("--- End UpdateFufillHTLC ---\n") +} diff --git a/lnwire/htlc_settlerequest_test.go b/lnwire/update_fulfill_htlc_test.go similarity index 60% rename from lnwire/htlc_settlerequest_test.go rename to lnwire/update_fulfill_htlc_test.go index 566c06d10..51d41307d 100644 --- a/lnwire/htlc_settlerequest_test.go +++ b/lnwire/update_fulfill_htlc_test.go @@ -6,23 +6,20 @@ import ( "testing" ) -func TestHTLCSettleRequestEncodeDecode(t *testing.T) { - redemptionProofs := make([][32]byte, 1) - redemptionProofs[0] = revHash - +func TestUpdateFufillHTLCEncodeDecode(t *testing.T) { // First create a new HTLCSR message. - settleReq := NewHTLCSettleRequest(outpoint1, HTLCKey(23), redemptionProofs) + settleReq := NewUpdateFufillHTLC(*outpoint1, 23, revHash) // Next encode the HTLCSR message into an empty bytes buffer. var b bytes.Buffer if err := settleReq.Encode(&b, 0); err != nil { - t.Fatalf("unable to encode HTLCSettleRequest: %v", err) + t.Fatalf("unable to encode UpdateFufillHTLC: %v", err) } // Deserialize the encoded SFOP message into a new empty struct. - settleReq2 := &HTLCSettleRequest{} + settleReq2 := &UpdateFufillHTLC{} if err := settleReq2.Decode(&b, 0); err != nil { - t.Fatalf("unable to decode HTLCSettleRequest: %v", err) + t.Fatalf("unable to decode UpdateFufillHTLC: %v", err) } // Assert equality of the two instances.