lnwire: morph HTLCSettleRequest into UpdateFufillHTLC

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.
This commit is contained in:
Olaoluwa Osuntokun 2017-02-16 20:34:09 +08:00
parent f1357e96b3
commit 5330513c7b
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2
4 changed files with 124 additions and 119 deletions

View File

@ -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
}

View File

@ -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:

View File

@ -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")
}

View File

@ -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.