lnwire: add basic encode/decode tests for single funder workflow

This commit is contained in:
Olaoluwa Osuntokun 2016-05-30 15:45:44 -07:00
parent 9978d889b7
commit 4d763e07f7
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2
10 changed files with 240 additions and 41 deletions

@ -24,9 +24,21 @@ type SingleFundingComplete struct {
// signature for Alice's version of the commitment transaction.
FundingOutPoint wire.OutPoint
// CommitmentSignature is Alice's signature for Bob's version of the
// CommitSignature is Alice's signature for Bob's version of the
// commitment transaction.
CommitmentSignature *btcec.Signature
CommitSignature *btcec.Signature
}
// NewSingleFundingComplete creates, and returns a new empty
// SingleFundingResponse.
func NewSingleFundingComplete(chanID uint64, fundingPoint wire.OutPoint,
commitSig *btcec.Signature) *SingleFundingComplete {
return &SingleFundingComplete{
ChannelID: chanID,
FundingOutPoint: fundingPoint,
CommitSignature: commitSig,
}
}
// Decode deserializes the serialized SingleFundingComplete stored in the passed
@ -41,7 +53,7 @@ func (s *SingleFundingComplete) Decode(r io.Reader, pver uint32) error {
err := readElements(r,
&s.ChannelID,
&s.FundingOutPoint,
&s.CommitmentSignature)
&s.CommitSignature)
if err != nil {
return err
}
@ -61,7 +73,7 @@ func (s *SingleFundingComplete) Encode(w io.Writer, pver uint32) error {
err := writeElements(w,
s.ChannelID,
s.FundingOutPoint,
s.CommitmentSignature)
s.CommitSignature)
if err != nil {
return err
}
@ -69,12 +81,6 @@ func (s *SingleFundingComplete) Encode(w io.Writer, pver uint32) error {
return nil
}
// NewSingleFundingComplete creates, and returns a new empty
// SingleFundingResponse.
func NewSingleFundingComplete() *SingleFundingComplete {
return &SingleFundingComplete{}
}
// Command returns the uint32 code which uniquely identifies this message as a
// SingleFundingRequest on the wire.
//
@ -104,7 +110,7 @@ func (s *SingleFundingComplete) Validate() error {
return fmt.Errorf("funding outpoint hash must be non-zero")
}
if s.CommitmentSignature == nil {
if s.CommitSignature == nil {
return fmt.Errorf("commitment signature must be non-nil")
}
@ -119,6 +125,6 @@ func (s *SingleFundingComplete) String() string {
return fmt.Sprintf("\n--- Begin SingleFundingComplete ---\n") +
fmt.Sprintf("ChannelID:\t\t\t%d\n", s.ChannelID) +
fmt.Sprintf("FundingOutPoint:\t\t\t%x\n", s.FundingOutPoint) +
fmt.Sprintf("CommitmentSignature\t\t\t\t%x\n", s.CommitmentSignature) +
fmt.Sprintf("CommitSignature\t\t\t\t%x\n", s.CommitSignature) +
fmt.Sprintf("--- End SingleFundingComplete ---\n")
}

@ -0,0 +1,30 @@
package lnwire
import (
"bytes"
"reflect"
"testing"
)
func TestSingleFundingCompleteWire(t *testing.T) {
// First create a new SFC message.
sfc := NewSingleFundingComplete(22, *outpoint1, commitSig1)
// Next encode the SFC message into an empty bytes buffer.
var b bytes.Buffer
if err := sfc.Encode(&b, 0); err != nil {
t.Fatalf("unable to encode SingleFundingComplete: %v", err)
}
// Deserialize the encoded SFC message into a new empty struct.
sfc2 := &SingleFundingComplete{}
if err := sfc2.Decode(&b, 0); err != nil {
t.Fatalf("unable to decode SingleFundingComplete: %v", err)
}
// Assert equality of the two instances.
if !reflect.DeepEqual(sfc, sfc2) {
t.Fatalf("encode/decode error messages don't match %#v vs %#v",
sfc, sfc2)
}
}

@ -25,6 +25,15 @@ type SingleFundingOpenProof struct {
SpvProof []byte
}
// NewSingleFundingSignComplete creates a new empty SingleFundingOpenProof
// message.
func NewSingleFundingOpenProof(chanID uint64, spvProof []byte) *SingleFundingOpenProof {
return &SingleFundingOpenProof{
ChannelID: chanID,
SpvProof: spvProof,
}
}
// Decode deserializes the serialized SingleFundingOpenProof stored in the
// passed io.Reader into the target SingleFundingOpenProof using the
// deserialization rules defined by the passed protocol version.
@ -43,12 +52,6 @@ func (s *SingleFundingOpenProof) Decode(r io.Reader, pver uint32) error {
return nil
}
// NewSingleFundingSignComplete creates a new empty SingleFundingOpenProof
// message.
func NewSingleFundingOpenProof() *SingleFundingOpenProof {
return &SingleFundingOpenProof{}
}
// Encode serializes the target SingleFundingOpenProof into the passed
// io.Writer implementation. Serialization will observe the rules defined by
// the passed protocol version.

@ -0,0 +1,31 @@
package lnwire
import (
"bytes"
"reflect"
"testing"
)
func TestSingleFundingOpenProofWire(t *testing.T) {
// First create a new SFOP message.
spvProof := bytes.Repeat([]byte{0x9}, 500)
sfop := NewSingleFundingOpenProof(22, spvProof)
// Next encode the SFOP message into an empty bytes buffer.
var b bytes.Buffer
if err := sfop.Encode(&b, 0); err != nil {
t.Fatalf("unable to encode SingleFundingSignComplete: %v", err)
}
// Deserialize the encoded SFOP message into a new empty struct.
sfop2 := &SingleFundingOpenProof{}
if err := sfop2.Decode(&b, 0); err != nil {
t.Fatalf("unable to decode SingleFundingOpenProof: %v", err)
}
// Assert equality of the two instances.
if !reflect.DeepEqual(sfop, sfop2) {
t.Fatalf("encode/decode error messages don't match %#v vs %#v",
sfop, sfop2)
}
}

@ -74,6 +74,23 @@ type SingleFundingRequest struct {
// TODO(roasbeef): confirmation depth
}
// NewSingleFundingRequest creates, and returns a new empty SingleFundingRequest.
func NewSingleFundingRequest(chanID uint64, chanType uint8, coinType uint64,
fee btcutil.Amount, delay uint32, cdp *btcec.PublicKey, revocation [20]byte,
deliveryScript PkScript) *SingleFundingRequest {
return &SingleFundingRequest{
ChannelID: chanID,
ChannelType: chanType,
CoinType: coinType,
FeePerKb: fee,
CsvDelay: delay,
ChannelDerivationPoint: cdp,
RevocationHash: revocation,
DeliveryPkScript: deliveryScript,
}
}
// Decode deserializes the serialized SingleFundingRequest stored in the passed
// io.Reader into the target SingleFundingRequest using the deserialization
// rules defined by the passed protocol version.
@ -106,11 +123,6 @@ func (c *SingleFundingRequest) Decode(r io.Reader, pver uint32) error {
return nil
}
// NewSingleFundingRequest creates, and returns a new empty SingleFundingRequest.
func NewSingleFundingRequest() *SingleFundingRequest {
return &SingleFundingRequest{}
}
// Encode serializes the target SingleFundingRequest into the passed io.Writer
// implementation. Serialization will observe the rules defined by the passed
// protocol version.

@ -0,0 +1,33 @@
package lnwire
import (
"bytes"
"reflect"
"testing"
)
func TestSingleFundingRequestWire(t *testing.T) {
// First create a new SFR message.
var rev [20]byte
cdp := pubKey
delivery := PkScript(bytes.Repeat([]byte{0x02}, 25))
sfr := NewSingleFundingRequest(20, 21, 22, 23, 5, cdp, rev, delivery)
// Next encode the SFR message into an empty bytes buffer.
var b bytes.Buffer
if err := sfr.Encode(&b, 0); err != nil {
t.Fatalf("unable to encode SingleFundingSignComplete: %v", err)
}
// Deserialize the encoded SFR message into a new empty struct.
sfr2 := &SingleFundingRequest{}
if err := sfr2.Decode(&b, 0); err != nil {
t.Fatalf("unable to decode SingleFundingRequest: %v", err)
}
// Assert equality of the two instances.
if !reflect.DeepEqual(sfr, sfr2) {
t.Fatalf("encode/decode error messages don't match %#v vs %#v",
sfr, sfr2)
}
}

@ -38,6 +38,19 @@ type SingleFundingResponse struct {
DeliveryPkScript PkScript
}
// NewSingleFundingResponse creates, and returns a new empty
// SingleFundingResponse.
func NewSingleFundingResponse(chanID uint64, revocation [20]byte,
cdp *btcec.PublicKey, deliveryScript PkScript) *SingleFundingResponse {
return &SingleFundingResponse{
ChannelID: chanID,
RevocationHash: revocation,
ChannelDerivationPoint: cdp,
DeliveryPkScript: deliveryScript,
}
}
// Decode deserializes the serialized SingleFundingResponse stored in the passed
// io.Reader into the target SingleFundingResponse using the deserialization
// rules defined by the passed protocol version.
@ -60,12 +73,6 @@ func (c *SingleFundingResponse) Decode(r io.Reader, pver uint32) error {
return nil
}
// NewSingleFundingResponse creates, and returns a new empty
// SingleFundingResponse.
func NewSingleFundingResponse() *SingleFundingResponse {
return &SingleFundingResponse{}
}
// Encode serializes the target SingleFundingResponse into the passed io.Writer
// implementation. Serialization will observe the rules defined by the passed
// protocol version.

@ -0,0 +1,33 @@
package lnwire
import (
"bytes"
"reflect"
"testing"
)
func TestSingleFundingResponseWire(t *testing.T) {
// First create a new SFR message.
var rev [20]byte
cdp := pubKey
delivery := PkScript(bytes.Repeat([]byte{0x02}, 25))
sfr := NewSingleFundingResponse(22, rev, cdp, delivery)
// Next encode the SFR message into an empty bytes buffer.
var b bytes.Buffer
if err := sfr.Encode(&b, 0); err != nil {
t.Fatalf("unable to encode SingleFundingSignComplete: %v", err)
}
// Deserialize the encoded SFR message into a new empty struct.
sfr2 := &SingleFundingResponse{}
if err := sfr2.Decode(&b, 0); err != nil {
t.Fatalf("unable to decode SingleFundingResponse: %v", err)
}
// Assert equality of the two instances.
if !reflect.DeepEqual(sfr, sfr2) {
t.Fatalf("encode/decode error messages don't match %#v vs %#v",
sfr, sfr2)
}
}

@ -16,9 +16,20 @@ type SingleFundingSignComplete struct {
// the initiated single funder workflow.
ChannelID uint64
// CommitmentSignature is Bobs's signature for Alice's version of the
// CommitSignature is Bobs's signature for Alice's version of the
// commitment transaction.
CommitmentSignature *btcec.Signature
CommitSignature *btcec.Signature
}
// NewSingleFundingSignComplete creates a new empty SingleFundingSignComplete
// message.
func NewSingleFundingSignComplete(chanID uint64,
sig *btcec.Signature) *SingleFundingSignComplete {
return &SingleFundingSignComplete{
ChannelID: chanID,
CommitSignature: sig,
}
}
// Decode deserializes the serialized SingleFundingSignComplete stored in the
@ -31,7 +42,7 @@ func (c *SingleFundingSignComplete) Decode(r io.Reader, pver uint32) error {
// CommitmentSignature (73)
err := readElements(r,
&c.ChannelID,
&c.CommitmentSignature)
&c.CommitSignature)
if err != nil {
return err
}
@ -39,12 +50,6 @@ func (c *SingleFundingSignComplete) Decode(r io.Reader, pver uint32) error {
return nil
}
// NewSingleFundingSignComplete creates a new empty SingleFundingSignComplete
// message.
func NewSingleFundingSignComplete() *SingleFundingSignComplete {
return &SingleFundingSignComplete{}
}
// Encode serializes the target SingleFundingSignComplete into the passed
// io.Writer implementation. Serialization will observe the rules defined by
// the passed protocol version.
@ -53,7 +58,7 @@ func NewSingleFundingSignComplete() *SingleFundingSignComplete {
func (c *SingleFundingSignComplete) Encode(w io.Writer, pver uint32) error {
err := writeElements(w,
c.ChannelID,
c.CommitmentSignature)
c.CommitSignature)
if err != nil {
return err
}
@ -84,7 +89,7 @@ func (c *SingleFundingSignComplete) MaxPayloadLength(uint32) uint32 {
//
// This is part of the lnwire.Message interface.
func (s *SingleFundingSignComplete) Validate() error {
if s.CommitmentSignature == nil {
if s.CommitSignature == nil {
return fmt.Errorf("commitment signature must be non-nil")
}
@ -98,6 +103,6 @@ func (s *SingleFundingSignComplete) Validate() error {
func (c *SingleFundingSignComplete) String() string {
return fmt.Sprintf("\n--- Begin FundingSignComplete ---\n") +
fmt.Sprintf("ChannelID:\t\t%d\n", c.ChannelID) +
fmt.Sprintf("CommitmentSignature\t\t%s\n", c.CommitmentSignature) +
fmt.Sprintf("CommitSignature\t\t%s\n", c.CommitSignature) +
fmt.Sprintf("--- End FundingSignComplete ---\n")
}

@ -0,0 +1,39 @@
package lnwire
import (
"bytes"
"math/big"
"reflect"
"testing"
"github.com/roasbeef/btcd/btcec"
)
func TestSingleFundingSignCompleteWire(t *testing.T) {
// First create a new SFSC message.
sfsc := NewSingleFundingSignComplete(
22,
&btcec.Signature{
R: new(big.Int).SetInt64(9),
S: new(big.Int).SetInt64(11),
},
)
// Next encode the SFSC message into an empty bytes buffer.
var b bytes.Buffer
if err := sfsc.Encode(&b, 0); err != nil {
t.Fatalf("unable to encode SingleFundingSignComplete: %v", err)
}
// Deserialize the encoded SFSC message into a new empty struct.
sfsc2 := &SingleFundingSignComplete{}
if err := sfsc2.Decode(&b, 0); err != nil {
t.Fatalf("unable to decode SingleFundingSignComplete: %v", err)
}
// Assert equality of the two instances.
if !reflect.DeepEqual(sfsc, sfsc2) {
t.Fatalf("encode/decode error messages don't match %#v vs %#v",
sfsc, sfsc2)
}
}