lnwire: morph CommitSignature into CommitSig

This commit renames and modifies the CommitSignature message to more
closely match the CommitSig message defined within the current set of
draft specifications.

The major change within the new message is that we now longer
explicitly specify the update log index of the remote node that this
signature covers. This is due to the fact the revocation message now
also double as acknowledgements of the remote parties recevied
commitment update messages.
This commit is contained in:
Olaoluwa Osuntokun 2017-02-16 20:04:58 +08:00
parent 9a61bb3ae1
commit 8a63c83283
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2
4 changed files with 124 additions and 134 deletions

115
lnwire/commit_sig.go Normal file

@ -0,0 +1,115 @@
package lnwire
import (
"fmt"
"io"
"github.com/roasbeef/btcd/btcec"
"github.com/roasbeef/btcd/wire"
)
// CommitSig is sent by either side to stage any pending HTLC's in the
// receiver's pending set into a new commitment state. Implicitly, the new
// commitment transaction constructed which has been signed by CommitSig
// includes all HTLC's in the remote node's pending set. A CommitSig message
// may be sent after a series of UpdateAddHTLC/UpdateFufillHTLC messages in
// order to batch add several HTLC's with a single signature covering all
// implicitly accepted HTLC's.
type CommitSig struct {
// ChannelPoint uniquely identifies to which currently active channel
// this CommitSig applies to.
ChannelPoint wire.OutPoint
// CommitSig is Alice's signature for Bob's new commitment transaction.
// Alice is able to send this signature without requesting any
// additional data due to the piggybacking of Bob's next revocation
// hash in his prior RevokeAndAck message, as well as the canonical
// ordering used for all inputs/outputs within commitment transactions.
CommitSig *btcec.Signature
// TODO(roasbeef): add HTLC sigs after state machine is updated to
// support that
}
// NewCommitSig creates a new empty CommitSig message.
func NewCommitSig() *CommitSig {
return &CommitSig{}
}
// A compile time check to ensure CommitSig implements the lnwire.Message
// interface.
var _ Message = (*CommitSig)(nil)
// Decode deserializes a serialized CommitSig message stored in the
// passed io.Reader observing the specified protocol version.
//
// This is part of the lnwire.Message interface.
func (c *CommitSig) Decode(r io.Reader, pver uint32) error {
// ChannelPoint(8)
// RequesterCommitSig(73max+2)
err := readElements(r,
&c.ChannelPoint,
&c.CommitSig,
)
if err != nil {
return err
}
return nil
}
// Encode serializes the target CommitSig into the passed io.Writer
// observing the protocol version specified.
//
// This is part of the lnwire.Message interface.
func (c *CommitSig) Encode(w io.Writer, pver uint32) error {
err := writeElements(w,
c.ChannelPoint,
c.CommitSig,
)
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 *CommitSig) Command() uint32 {
return CmdCommitSig
}
// MaxPayloadLength returns the maximum allowed payload size for a
// CommitSig complete message observing the specified protocol version.
//
// This is part of the lnwire.Message interface.
func (c *CommitSig) MaxPayloadLength(uint32) uint32 {
// 36 + 73
return 109
}
// Validate performs any necessary sanity checks to ensure all fields present
// on the CommitSig are valid.
//
// This is part of the lnwire.Message interface.
func (c *CommitSig) Validate() error {
// We're good!
return nil
}
// String returns the string representation of the target CommitSig.
//
// This is part of the lnwire.Message interface.
func (c *CommitSig) String() string {
var serializedSig []byte
if c.CommitSig != nil {
serializedSig = c.CommitSig.Serialize()
}
return fmt.Sprintf("\n--- Begin CommitSig ---\n") +
fmt.Sprintf("ChannelPoint:\t%v\n", c.ChannelPoint) +
fmt.Sprintf("CommitSig:\t\t%x\n", serializedSig) +
fmt.Sprintf("--- End CommitSig ---\n")
}

@ -4,28 +4,24 @@ import (
"bytes"
"reflect"
"testing"
"github.com/roasbeef/btcutil"
)
func TestCommitSignatureEncodeDecode(t *testing.T) {
commitSignature := &CommitSignature{
ChannelPoint: outpoint1,
Fee: btcutil.Amount(10000),
LogIndex: 5,
func TestCommitSigEncodeDecode(t *testing.T) {
commitSignature := &CommitSig{
ChannelPoint: *outpoint1,
CommitSig: commitSig,
}
// Next encode the CS message into an empty bytes buffer.
var b bytes.Buffer
if err := commitSignature.Encode(&b, 0); err != nil {
t.Fatalf("unable to encode CommitSignature: %v", err)
t.Fatalf("unable to encode CommitSig: %v", err)
}
// Deserialize the encoded EG message into a new empty struct.
commitSignature2 := &CommitSignature{}
commitSignature2 := &CommitSig{}
if err := commitSignature2.Decode(&b, 0); err != nil {
t.Fatalf("unable to decode CommitSignature: %v", err)
t.Fatalf("unable to decode CommitSig: %v", err)
}
// Assert equality of the two instances.

@ -1,121 +0,0 @@
package lnwire
import (
"fmt"
"io"
"github.com/roasbeef/btcd/btcec"
"github.com/roasbeef/btcd/wire"
"github.com/roasbeef/btcutil"
)
// CommitSignature is sent by either side to stage any pending HTLCs in the
// receiver's pending set which has not explicitly been rejected via an
// HTLCAddReject message. Implicitly, the new commitment transaction constructed
// which has been signed by CommitSig includes all HTLCs in the remote node's
// pending set. A CommitSignature message may be sent after a series of HTLCAdd
// messages in order to batch add several HTLCs with a single signature
// covering all implicitly accepted HTLCs.
type CommitSignature struct {
// ChannelPoint uniquely identifies to which currently active channel this
// CommitSignature applies to.
ChannelPoint *wire.OutPoint
// LogIndex is the index into the receiver's HTLC log to which this
// commitment signature covers. In order to properly verify this
// signature, the receiver should include all the HTLCs within their
// log with an index less-than-or-equal to the listed log-index.
LogIndex uint64
// Fee represents the total miner's fee that was used when constructing
// the new commitment transaction.
// TODO(roasbeef): is the above comment correct?
Fee btcutil.Amount
// CommitSig is Alice's signature for Bob's new commitment transaction.
// Alice is able to send this signature without requesting any additional
// data due to the piggybacking of Bob's next revocation hash in his
// prior CommitRevocation message, as well as the canonical ordering
// used for all inputs/outputs within commitment transactions.
CommitSig *btcec.Signature
}
// NewCommitSignature creates a new empty CommitSignature message.
func NewCommitSignature() *CommitSignature {
return &CommitSignature{}
}
// A compile time check to ensure CommitSignature implements the lnwire.Message
// interface.
var _ Message = (*CommitSignature)(nil)
// Decode deserializes a serialized CommitSignature message stored in the
// passed io.Reader observing the specified protocol version.
//
// This is part of the lnwire.Message interface.
func (c *CommitSignature) Decode(r io.Reader, pver uint32) error {
// ChannelPoint(8)
// LogIndex(8)
// Fee(8)
// RequesterCommitSig(73max+2)
err := readElements(r,
&c.ChannelPoint,
&c.LogIndex,
&c.Fee,
&c.CommitSig,
)
if err != nil {
return err
}
return nil
}
// Encode serializes the target CommitSignature into the passed io.Writer
// observing the protocol version specified.
//
// This is part of the lnwire.Message interface.
func (c *CommitSignature) Encode(w io.Writer, pver uint32) error {
err := writeElements(w,
c.ChannelPoint,
c.LogIndex,
c.Fee,
c.CommitSig,
)
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 *CommitSignature) Command() uint32 {
return CmdCommitSignature
}
// MaxPayloadLength returns the maximum allowed payload size for a
// CommitSignature complete message observing the specified protocol version.
//
// This is part of the lnwire.Message interface.
func (c *CommitSignature) MaxPayloadLength(uint32) uint32 {
// 36 + 8 + 8 + 73
return 125
}
// Validate performs any necessary sanity checks to ensure all fields present
// on the CommitSignature are valid.
//
// This is part of the lnwire.Message interface.
func (c *CommitSignature) Validate() error {
if c.Fee < 0 {
// While fees can be negative, it's too confusing to allow
// negative payments. Maybe for some wallets, but not this one!
return fmt.Errorf("Amount paid cannot be negative.")
}
// We're good!
return nil
}

@ -44,7 +44,7 @@ const (
CmdCancelHTLC = uint32(1300)
// Commands for modifying commitment transactions.
CmdCommitSignature = uint32(2000)
CmdCommitSig = uint32(2000)
CmdRevokeAndAck = uint32(2010)
// Commands for reporting protocol errors.
@ -115,8 +115,8 @@ func makeEmptyMessage(command uint32) (Message, error) {
msg = &HTLCSettleRequest{}
case CmdCancelHTLC:
msg = &CancelHTLC{}
case CmdCommitSignature:
msg = &CommitSignature{}
case CmdCommitSig:
msg = &CommitSig{}
case CmdRevokeAndAck:
msg = &RevokeAndAck{}
case CmdErrorGeneric: