lnwire: revamp previous encode/decode tests to passing state

This commit is contained in:
Olaoluwa Osuntokun 2016-05-30 16:49:48 -07:00
parent 83b11c5efe
commit 3b6e456371
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2
15 changed files with 269 additions and 266 deletions

@ -27,6 +27,16 @@ type CloseComplete struct {
ResponderCloseSig *btcec.Signature
}
// NewCloseComplete creates a new empty CloseComplete message.
// TODO(roasbeef): add params to all constructors...
func NewCloseComplete() *CloseComplete {
return &CloseComplete{}
}
// A compile time check to ensure CloseComplete implements the lnwire.Message
// interface.
var _ Message = (*CloseComplete)(nil)
// Decode deserializes a serialized CloseComplete message stored in the passed
// io.Reader observing the specified protocol version.
//
@ -44,16 +54,6 @@ func (c *CloseComplete) Decode(r io.Reader, pver uint32) error {
return nil
}
// NewCloseComplete creates a new empty CloseComplete message.
// TODO(roasbeef): add params to all constructors...
func NewCloseComplete() *CloseComplete {
return &CloseComplete{}
}
// A compile time check to ensure CloseComplete implements the lnwire.Message
// interface.
var _ Message = (*CloseComplete)(nil)
// Encode serializes the target CloseComplete into the passed io.Writer observing
// the protocol version specified.
//

@ -1,32 +1,32 @@
package lnwire
import (
"bytes"
"reflect"
"testing"
)
var (
closeComplete = &CloseComplete{
func TestCloseCompleteEncodeDecode(t *testing.T) {
cc := &CloseComplete{
ChannelID: uint64(12345678),
ResponderCloseSig: commitSig,
}
closeCompleteSerializedString = "0000000000bc614e4630440220333835e58e958f5e92b4ff4e6fa2470dac88094c97506b4d6d1f4e23e52cb481022057483ac18d6b9c9c14f0c626694c9ccf8b27b3dbbedfdf6b6c9a9fa9f427a1dfe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
closeCompleteSerializedMessage = "0709110b000001360000006f0000000000bc614e4630440220333835e58e958f5e92b4ff4e6fa2470dac88094c97506b4d6d1f4e23e52cb481022057483ac18d6b9c9c14f0c626694c9ccf8b27b3dbbedfdf6b6c9a9fa9f427a1dfe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
)
func TestCloseCompleteEncodeDecode(t *testing.T) {
// All of these types being passed are of the message interface type
// Test serialization, runs: message.Encode(b, 0)
// Returns bytes
// Compares the expected serialized string from the original
s := SerializeTest(t, closeComplete, closeCompleteSerializedString, filename)
// Test deserialization, runs: message.Decode(s, 0)
// Makes sure the deserialized struct is the same as the original
newMessage := NewCloseComplete()
DeserializeTest(t, s, newMessage, closeComplete)
// Test message using Message interface
// Serializes into buf: WriteMessage(buf, message, uint32(1), wire.TestNet3)
// Deserializes into msg: _, msg, _ , err := ReadMessage(buf, uint32(1), wire.TestNet3)
MessageSerializeDeserializeTest(t, closeComplete, closeCompleteSerializedMessage)
// Next encode the CC message into an empty bytes buffer.
var b bytes.Buffer
if err := cc.Encode(&b, 0); err != nil {
t.Fatalf("unable to encode CloseComplete: %v", err)
}
// Deserialize the encoded CC message into a new empty struct.
cc2 := &CloseComplete{}
if err := cc2.Decode(&b, 0); err != nil {
t.Fatalf("unable to decode CloseComplete: %v", err)
}
// Assert equality of the two instances.
if !reflect.DeepEqual(cc, cc2) {
t.Fatalf("encode/decode error messages don't match %#v vs %#v",
cc, cc2)
}
}

@ -30,14 +30,25 @@ type CommitRevocation struct {
NextRevocationHash [20]byte
}
// NewCommitRevocation creates a new CommitRevocation message.
func NewCommitRevocation() *CommitRevocation {
return &CommitRevocation{}
}
// A compile time check to ensure CommitRevocation implements the lnwire.Message
// interface.
var _ Message = (*CommitRevocation)(nil)
// Decode deserializes a serialized CommitRevocation message stored in the
// passed io.Reader observing the specified protocol version.
//
// This is part of the lnwire.Message interface.
func (c *CommitRevocation) Decode(r io.Reader, pver uint32) error {
// ChannelID (8)
// NextRevocationHash (20)
// Revocation (20)
err := readElements(r,
&c.ChannelID,
&c.NextRevocationHash,
&c.Revocation,
)
@ -48,21 +59,13 @@ func (c *CommitRevocation) Decode(r io.Reader, pver uint32) error {
return nil
}
// NewCommitRevocation creates a new CommitRevocation message.
func NewCommitRevocation() *CommitRevocation {
return &CommitRevocation{}
}
// A compile time check to ensure CommitRevocation implements the lnwire.Message
// interface.
var _ Message = (*CommitRevocation)(nil)
// Encode serializes the target CommitRevocation into the passed io.Writer
// observing the protocol version specified.
//
// This is part of the lnwire.Message interface.
func (c *CommitRevocation) Encode(w io.Writer, pver uint32) error {
err := writeElements(w,
c.ChannelID,
c.NextRevocationHash,
c.Revocation,
)

@ -1,37 +1,36 @@
package lnwire
import (
"bytes"
"reflect"
"testing"
)
var (
// Need to to do this here
_ = copy(revocationHash[:], revocationHashBytes)
_ = copy(nextHop[:], nextHopBytes)
commitRevocation = &CommitRevocation{
ChannelID: uint64(12345678),
Revocation: revocationHash, // technically it's not a hash... fix later
NextRevocationHash: nextHop, // technically it's not a hash... fix later
}
commitRevocationSerializedString = "0000000000bc614e4132b6b48371f7b022a16eacb9b2b0ebee134d4194a9ded5a30fc5944cb1e2cbcd980f30616a1440"
commitRevocationSerializedMessage = "0709110b000007da000000300000000000bc614e4132b6b48371f7b022a16eacb9b2b0ebee134d4194a9ded5a30fc5944cb1e2cbcd980f30616a1440"
)
func TestCommitRevocationEncodeDecode(t *testing.T) {
// All of these types being passed are of the message interface type
// Test serialization, runs: message.Encode(b, 0)
// Returns bytes
// Compares the expected serialized string from the original
s := SerializeTest(t, commitRevocation, commitRevocationSerializedString, filename)
copy(revocationHash[:], revocationHashBytes)
copy(nextHop[:], nextHopBytes)
// Test deserialization, runs: message.Decode(s, 0)
// Makes sure the deserialized struct is the same as the original
newMessage := NewCommitRevocation()
DeserializeTest(t, s, newMessage, commitRevocation)
// Test message using Message interface
// Serializes into buf: WriteMessage(buf, message, uint32(1), wire.TestNet3)
// Deserializes into msg: _, msg, _ , err := ReadMessage(buf, uint32(1), wire.TestNet3)
MessageSerializeDeserializeTest(t, commitRevocation, commitRevocationSerializedMessage)
cr := &CommitRevocation{
ChannelID: uint64(12345678),
Revocation: revocationHash,
NextRevocationHash: nextHop,
}
// Next encode the CR message into an empty bytes buffer.
var b bytes.Buffer
if err := cr.Encode(&b, 0); err != nil {
t.Fatalf("unable to encode CommitRevocation: %v", err)
}
// Deserialize the encoded EG message into a new empty struct.
cr2 := &CommitRevocation{}
if err := cr2.Decode(&b, 0); err != nil {
t.Fatalf("unable to decode CommitRevocation: %v", err)
}
// Assert equality of the two instances.
if !reflect.DeepEqual(cr, cr2) {
t.Fatalf("encode/decode error messages don't match %#v vs %#v",
cr, cr2)
}
}

@ -33,6 +33,15 @@ type CommitSignature struct {
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.
//
@ -53,15 +62,6 @@ func (c *CommitSignature) Decode(r io.Reader, pver uint32) error {
return nil
}
// 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)
// Encode serializes the target CommitSignature into the passed io.Writer
// observing the protocol version specified.
//

@ -1,38 +1,37 @@
package lnwire
import (
"bytes"
"reflect"
"testing"
"github.com/roasbeef/btcutil"
)
var (
// Need to to do this here
_ = copy(revocationHash[:], revocationHashBytes)
func TestCommitSignatureEncodeDecode(t *testing.T) {
copy(revocationHash[:], revocationHashBytes)
commitSignature = &CommitSignature{
commitSignature := &CommitSignature{
ChannelID: uint64(12345678),
Fee: btcutil.Amount(10000),
CommitSig: commitSig,
}
commitSignatureSerializedString = "0000000000bc614e00000000000030390000000000003039000000000000d4314132b6b48371f7b022a16eacb9b2b0ebee134d4100000000000027104630440220333835e58e958f5e92b4ff4e6fa2470dac88094c97506b4d6d1f4e23e52cb481022057483ac18d6b9c9c14f0c626694c9ccf8b27b3dbbedfdf6b6c9a9fa9f427a1df"
commitSignatureSerializedMessage = "0709110b000007d0000000830000000000bc614e00000000000030390000000000003039000000000000d4314132b6b48371f7b022a16eacb9b2b0ebee134d4100000000000027104630440220333835e58e958f5e92b4ff4e6fa2470dac88094c97506b4d6d1f4e23e52cb481022057483ac18d6b9c9c14f0c626694c9ccf8b27b3dbbedfdf6b6c9a9fa9f427a1df"
)
func TestCommitSignatureEncodeDecode(t *testing.T) {
// All of these types being passed are of the message interface type
// Test serialization, runs: message.Encode(b, 0)
// Returns bytes
// Compares the expected serialized string from the original
s := SerializeTest(t, commitSignature, commitSignatureSerializedString, filename)
// Test deserialization, runs: message.Decode(s, 0)
// Makes sure the deserialized struct is the same as the original
newMessage := NewCommitSignature()
DeserializeTest(t, s, newMessage, commitSignature)
// Test message using Message interface
// Serializes into buf: WriteMessage(buf, message, uint32(1), wire.TestNet3)
// Deserializes into msg: _, msg, _ , err := ReadMessage(buf, uint32(1), wire.TestNet3)
MessageSerializeDeserializeTest(t, commitSignature, commitSignatureSerializedMessage)
// 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)
}
// Deserialize the encoded EG message into a new empty struct.
commitSignature2 := &CommitSignature{}
if err := commitSignature2.Decode(&b, 0); err != nil {
t.Fatalf("unable to decode CommitSignature: %v", err)
}
// Assert equality of the two instances.
if !reflect.DeepEqual(commitSignature, commitSignature2) {
t.Fatalf("encode/decode error messages don't match %#v vs %#v",
commitSignature, commitSignature2)
}
}

@ -14,33 +14,14 @@ type ErrorGeneric struct {
// within.
ChannelID uint64
// TODO(roasbeef): uint16 for problem type?
// ErrorID uint16
// ErrorID quickly defines the nature of the error according to error
// type.
ErrorID uint16
// Problem is a human-readable string further elaborating upon the
// nature of the exact error. The maxmium allowed length of this
// message is 8192 bytes.
Problem string
// TODO(roasbeef): add SerializeSize?
}
// Decode deserializes a serialized ErrorGeneric message stored in the
// passed io.Reader observing the specified protocol version.
//
// This is part of the lnwire.Message interface.
func (c *ErrorGeneric) Decode(r io.Reader, pver uint32) error {
// ChannelID(8)
// Problem
err := readElements(r,
&c.ChannelID,
&c.Problem,
)
if err != nil {
return err
}
return nil
}
// NewErrorGeneric creates a new ErrorGeneric message.
@ -52,6 +33,25 @@ func NewErrorGeneric() *ErrorGeneric {
// interface.
var _ Message = (*ErrorGeneric)(nil)
// Decode deserializes a serialized ErrorGeneric message stored in the
// passed io.Reader observing the specified protocol version.
//
// This is part of the lnwire.Message interface.
func (c *ErrorGeneric) Decode(r io.Reader, pver uint32) error {
// ChannelID(8)
// Problem
err := readElements(r,
&c.ChannelID,
&c.ErrorID,
&c.Problem,
)
if err != nil {
return err
}
return nil
}
// Encode serializes the target ErrorGeneric into the passed io.Writer
// observing the protocol version specified.
//
@ -59,6 +59,7 @@ var _ Message = (*ErrorGeneric)(nil)
func (c *ErrorGeneric) Encode(w io.Writer, pver uint32) error {
err := writeElements(w,
c.ChannelID,
c.ErrorID,
c.Problem,
)
if err != nil {
@ -104,6 +105,7 @@ func (c *ErrorGeneric) Validate() error {
func (c *ErrorGeneric) String() string {
return fmt.Sprintf("\n--- Begin ErrorGeneric ---\n") +
fmt.Sprintf("ChannelID:\t%d\n", c.ChannelID) +
fmt.Sprintf("ErrorID:\t%d\n", c.ErrorID) +
fmt.Sprintf("Problem:\t%s\n", c.Problem) +
fmt.Sprintf("--- End ErrorGeneric ---\n")
}

@ -1,32 +1,33 @@
package lnwire
import (
"bytes"
"reflect"
"testing"
)
var (
errorGeneric = &ErrorGeneric{
func TestErrorGenericEncodeDecode(t *testing.T) {
eg := &ErrorGeneric{
ChannelID: uint64(12345678),
ErrorID: 99,
Problem: "Hello world!",
}
errorGenericSerializedString = "0000000000bc614e000c48656c6c6f20776f726c6421"
errorGenericSerializedMessage = "0709110b00000fa0000000160000000000bc614e000c48656c6c6f20776f726c6421"
)
func TestErrorGenericEncodeDecode(t *testing.T) {
// All of these types being passed are of the message interface type
// Test serialization, runs: message.Encode(b, 0)
// Returns bytes
// Compares the expected serialized string from the original
s := SerializeTest(t, errorGeneric, errorGenericSerializedString, filename)
// Test deserialization, runs: message.Decode(s, 0)
// Makes sure the deserialized struct is the same as the original
newMessage := NewErrorGeneric()
DeserializeTest(t, s, newMessage, errorGeneric)
// Test message using Message interface
// Serializes into buf: WriteMessage(buf, message, uint32(1), wire.TestNet3)
// Deserializes into msg: _, msg, _ , err := ReadMessage(buf, uint32(1), wire.TestNet3)
MessageSerializeDeserializeTest(t, errorGeneric, errorGenericSerializedMessage)
// Next encode the EG message into an empty bytes buffer.
var b bytes.Buffer
if err := eg.Encode(&b, 0); err != nil {
t.Fatalf("unable to encode ErrorGeneric: %v", err)
}
// Deserialize the encoded EG message into a new empty struct.
eg2 := &ErrorGeneric{}
if err := eg2.Decode(&b, 0); err != nil {
t.Fatalf("unable to decode ErrorGeneric: %v", err)
}
// Assert equality of the two instances.
if !reflect.DeepEqual(eg, eg2) {
t.Fatalf("encode/decode error messages don't match %#v vs %#v",
eg, eg2)
}
}

@ -1,17 +1,18 @@
package lnwire
import (
"bytes"
"reflect"
"testing"
"github.com/roasbeef/btcutil"
)
var (
// Need to do this here
_ = copy(revocationHash[:], revocationHashBytes)
func TestFundingRequestEncodeDecode(t *testing.T) {
copy(revocationHash[:], revocationHashBytes)
// funding request
fundingRequest = &FundingRequest{
fr := &FundingRequest{
ReservationID: uint64(12345678),
ChannelType: uint8(0),
RequesterFundingAmount: btcutil.Amount(100000000),
@ -28,24 +29,22 @@ var (
ChangePkScript: changePkScript,
Inputs: inputs,
}
fundingRequestSerializedString = "0000000000bc614e000000000005f5e1000000000008f0d1804132b6b48371f7b022a16eacb9b2b0ebee134d4102f977808cb9577897582d7524b562691e180953dd0008eb44e09594c539d6daee00000000000200000000000000004e20000000000012d68700000006000010e0001976a914e8048c0fb75bdecc91ebfb99c174f4ece29ffbd488ac1976a914238ee44bb5c8c1314dd03974a17ec6c406fdcb8388ac02e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b8550000000001ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b00000001"
fundingRequestSerializedMessage = "0709110b000000c8000000ec0000000000bc614e000000000005f5e1000000000008f0d1804132b6b48371f7b022a16eacb9b2b0ebee134d4102f977808cb9577897582d7524b562691e180953dd0008eb44e09594c539d6daee00000000000200000000000000004e20000000000012d68700000006000010e0001976a914e8048c0fb75bdecc91ebfb99c174f4ece29ffbd488ac1976a914238ee44bb5c8c1314dd03974a17ec6c406fdcb8388ac02e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b8550000000001ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b00000001"
)
func TestFundingRequestEncodeDecode(t *testing.T) {
// All of these types being passed are of the message interface type
// Test serialization, runs: message.Encode(b, 0)
// Returns bytes
// Compares the expected serialized string from the original
s := SerializeTest(t, fundingRequest, fundingRequestSerializedString, filename)
// Test deserialization, runs: message.Decode(s, 0)
// Makes sure the deserialized struct is the same as the original
newMessage := NewFundingRequest()
DeserializeTest(t, s, newMessage, fundingRequest)
// Test message using Message interface
// Serializes into buf: WriteMessage(buf, message, uint32(1), wire.TestNet3)
// Deserializes into msg: _, msg, _ , err := ReadMessage(buf, uint32(1), wire.TestNet3)
MessageSerializeDeserializeTest(t, fundingRequest, fundingRequestSerializedMessage)
// Next encode the FR message into an empty bytes buffer.
var b bytes.Buffer
if err := fr.Encode(&b, 0); err != nil {
t.Fatalf("unable to encode FundingRequest: %v", err)
}
// Deserialize the encoded FR message into a new empty struct.
fr2 := &FundingRequest{}
if err := fr2.Decode(&b, 0); err != nil {
t.Fatalf("unable to decode FundingRequest: %v", err)
}
// Assert equality of the two instances.
if !reflect.DeepEqual(fr, fr2) {
t.Fatalf("encode/decode error messages don't match %#v vs %#v",
fr, fr2)
}
}

@ -1,17 +1,18 @@
package lnwire
import (
"bytes"
"reflect"
"testing"
"github.com/roasbeef/btcutil"
)
var (
// Need to do this here
_ = copy(revocationHash[:], revocationHashBytes)
func TestFundingResponseEncodeDecode(t *testing.T) {
copy(revocationHash[:], revocationHashBytes)
// funding response
fundingResponse = &FundingResponse{
fr := &FundingResponse{
ChannelType: uint8(1),
ReservationID: uint64(12345678),
ResponderFundingAmount: btcutil.Amount(100000000),
@ -27,24 +28,22 @@ var (
ChangePkScript: changePkScript,
Inputs: inputs,
}
fundingResponseSerializedString = "0000000000bc614e010000000005f5e1004132b6b48371f7b022a16eacb9b2b0ebee134d4102f977808cb9577897582d7524b562691e180953dd0008eb44e09594c539d6daee00000000000200000000000000004e2000000006000010e0011976a914e8048c0fb75bdecc91ebfb99c174f4ece29ffbd488ac1976a914238ee44bb5c8c1314dd03974a17ec6c406fdcb8388ac4630440220333835e58e958f5e92b4ff4e6fa2470dac88094c97506b4d6d1f4e23e52cb481022057483ac18d6b9c9c14f0c626694c9ccf8b27b3dbbedfdf6b6c9a9fa9f427a1df02e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b8550000000001ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b00000001"
fundingResponseSerializedMessage = "0709110b000000d2000001230000000000bc614e010000000005f5e1004132b6b48371f7b022a16eacb9b2b0ebee134d4102f977808cb9577897582d7524b562691e180953dd0008eb44e09594c539d6daee00000000000200000000000000004e2000000006000010e0011976a914e8048c0fb75bdecc91ebfb99c174f4ece29ffbd488ac1976a914238ee44bb5c8c1314dd03974a17ec6c406fdcb8388ac4630440220333835e58e958f5e92b4ff4e6fa2470dac88094c97506b4d6d1f4e23e52cb481022057483ac18d6b9c9c14f0c626694c9ccf8b27b3dbbedfdf6b6c9a9fa9f427a1df02e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b8550000000001ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b00000001"
)
func TestFundingResponseEncodeDecode(t *testing.T) {
// All of these types being passed are of the message interface type
// Test serialization, runs: message.Encode(b, 0)
// Returns bytes
// Compares the expected serialized string from the original
s := SerializeTest(t, fundingResponse, fundingResponseSerializedString, filename)
// Test deserialization, runs: message.Decode(s, 0)
// Makes sure the deserialized struct is the same as the original
newMessage := NewFundingResponse()
DeserializeTest(t, s, newMessage, fundingResponse)
// Test message using Message interface
// Serializes into buf: WriteMessage(buf, message, uint32(1), wire.TestNet3)
// Deserializes into msg: _, msg, _ , err := ReadMessage(buf, uint32(1), wire.TestNet3)
MessageSerializeDeserializeTest(t, fundingResponse, fundingResponseSerializedMessage)
// Next encode the FR message into an empty bytes buffer.
var b bytes.Buffer
if err := fr.Encode(&b, 0); err != nil {
t.Fatalf("unable to encode HTLCAddRequest: %v", err)
}
// Deserialize the encoded FR message into a new empty struct.
fr2 := &FundingResponse{}
if err := fr2.Decode(&b, 0); err != nil {
t.Fatalf("unable to decode FundingResponse: %v", err)
}
// Assert equality of the two instances.
if !reflect.DeepEqual(fr, fr2) {
t.Fatalf("encode/decode error messages don't match %#v vs %#v",
fr, fr2)
}
}

@ -5,6 +5,20 @@ import (
"io"
)
// ChannelPoint represents a unique state update within currently active
// channel. A channel update across all open chnnels can be uniquely identified
// by a two-tuple: (fundingTXID, HTLCKey). All explicit updates to an open
// channel will reference a ChannelPoint to apply the update to.
type ChannelPoint struct {
// ChannelID references the particular active channel to which this
// HTLCAddReject message is binded to.
ChannelID uint64
// HTLCKey is used to identify which HTLC previously attempted to be
// added via an HTLCAddRequest message is being declined.
HTLCKey HTLCKey
}
// HTLCAddReject is sent by Bob when he wishes to reject a particular HTLC that
// Alice attempted to add via an HTLCAddRequest message. The rejected HTLC is
// referenced by its unique HTLCKey ID. An HTLCAddReject message is bound to a

@ -45,10 +45,6 @@ type HTLCAddRequest struct {
// length of this slice should be N.
RedemptionHashes [][20]byte
// Data to parse&pass on to the next node
// Nested HTLCAddRequests with a uint32 in front for the size
Blob []byte
// OnionBlob is the raw serialized mix header used to route an HTLC in
// a privacy-preserving manner. The mix header is defined currently to
// be parsed as a 4-tuple: (groupElement, routingInfo, headerMAC, body).
@ -62,6 +58,15 @@ type HTLCAddRequest struct {
OnionBlob []byte
}
// NewHTLCAddRequest returns a new empty HTLCAddRequest message.
func NewHTLCAddRequest() *HTLCAddRequest {
return &HTLCAddRequest{}
}
// A compile time check to ensure HTLCAddRequest implements the lnwire.Message
// interface.
var _ Message = (*HTLCAddRequest)(nil)
// Decode deserializes a serialized HTLCAddRequest message stored in the passed
// io.Reader observing the specified protocol version.
//
@ -72,7 +77,6 @@ func (c *HTLCAddRequest) Decode(r io.Reader, pver uint32) error {
// Amount(4)
// ContractType(1)
// RedemptionHashes (numOfHashes * 20 + numOfHashes)
// Blob(2+blobsize)
// OnionBlog
err := readElements(r,
&c.ChannelID,
@ -80,7 +84,6 @@ func (c *HTLCAddRequest) Decode(r io.Reader, pver uint32) error {
&c.Amount,
&c.ContractType,
&c.RedemptionHashes,
&c.Blob,
&c.OnionBlob,
)
if err != nil {
@ -90,15 +93,6 @@ func (c *HTLCAddRequest) Decode(r io.Reader, pver uint32) error {
return nil
}
// NewHTLCAddRequest returns a new empty HTLCAddRequest message.
func NewHTLCAddRequest() *HTLCAddRequest {
return &HTLCAddRequest{}
}
// A compile time check to ensure HTLCAddRequest implements the lnwire.Message
// interface.
var _ Message = (*HTLCAddRequest)(nil)
// Encode serializes the target HTLCAddRequest into the passed io.Writer observing
// the protocol version specified.
//
@ -110,7 +104,6 @@ func (c *HTLCAddRequest) Encode(w io.Writer, pver uint32) error {
c.Amount,
c.ContractType,
c.RedemptionHashes,
c.Blob,
c.OnionBlob,
)
if err != nil {
@ -169,7 +162,6 @@ func (c *HTLCAddRequest) String() string {
fmt.Sprintf("ContractType:\t%d (%b)\n", c.ContractType, c.ContractType) +
fmt.Sprintf("RedemptionHashes:") +
redemptionHashes +
fmt.Sprintf("Blob:\t\t\t\t%x\n", c.Blob) +
fmt.Sprintf("OnionBlob:\t\t\t\t%x\n", c.OnionBlob) +
fmt.Sprintf("--- End HTLCAddRequest ---\n")
}

@ -1,43 +1,40 @@
package lnwire
import (
"bytes"
"reflect"
"testing"
)
var (
// Need to to do this here
_ = copy(revocationHash[:], revocationHashBytes)
_ = copy(redemptionHash[:], redemptionHashBytes)
emptyRedemptionHashes = [][20]byte{}
redemptionHashes = append(emptyRedemptionHashes, redemptionHash)
func TestHTLCAddRequestEncodeDecode(t *testing.T) {
redemptionHashes := make([][20]byte, 1)
copy(redemptionHashes[0][:], bytes.Repeat([]byte{0x09}, 20))
htlcAddRequest = &HTLCAddRequest{
// First create a new HTLCAR message.
addReq := &HTLCAddRequest{
ChannelID: uint64(12345678),
Expiry: uint32(144),
Amount: CreditsAmount(123456000),
ContractType: uint8(17),
RedemptionHashes: redemptionHashes,
Blob: []byte{255, 0, 255, 0, 255, 0, 255, 0},
OnionBlob: []byte{255, 0, 255, 0, 255, 0, 255, 0},
}
htlcAddRequestSerializedString = "0000000000bc614e00000000000030390000009000000000075bca001100015b315ebabb0d8c0d94281caa2dfee69a1a00436e0008ff00ff00ff00ff00"
htlcAddRequestSerializedMessage = "0709110b000003e80000003d0000000000bc614e00000000000030390000009000000000075bca001100015b315ebabb0d8c0d94281caa2dfee69a1a00436e0008ff00ff00ff00ff00"
)
func TestHTLCAddRequestEncodeDecode(t *testing.T) {
// All of these types being passed are of the message interface type
// Test serialization, runs: message.Encode(b, 0)
// Returns bytes
// Compares the expected serialized string from the original
s := SerializeTest(t, htlcAddRequest, htlcAddRequestSerializedString, filename)
// Test deserialization, runs: message.Decode(s, 0)
// Makes sure the deserialized struct is the same as the original
newMessage := NewHTLCAddRequest()
DeserializeTest(t, s, newMessage, htlcAddRequest)
// Test message using Message interface
// Serializes into buf: WriteMessage(buf, message, uint32(1), wire.TestNet3)
// Deserializes into msg: _, msg, _ , err := ReadMessage(buf, uint32(1), wire.TestNet3)
MessageSerializeDeserializeTest(t, htlcAddRequest, htlcAddRequestSerializedMessage)
// Next encode the HTLCAR message into an empty bytes buffer.
var b bytes.Buffer
if err := addReq.Encode(&b, 0); err != nil {
t.Fatalf("unable to encode HTLCAddRequest: %v", err)
}
// Deserialize the encoded HTLCAR message into a new empty struct.
addReq2 := &HTLCAddRequest{}
if err := addReq2.Decode(&b, 0); err != nil {
t.Fatalf("unable to decode HTLCAddRequest: %v", err)
}
// Assert equality of the two instances.
if !reflect.DeepEqual(addReq, addReq2) {
t.Fatalf("encode/decode error messages don't match %#v vs %#v",
addReq, addReq2)
}
}

@ -27,6 +27,21 @@ type HTLCSettleRequest struct {
RedemptionProofs [][20]byte
}
// NewHTLCSettleRequest returns a new empty HTLCSettleRequest.
func NewHTLCSettleRequest(chanID uint64, key HTLCKey,
redemptionProofs [][20]byte) *HTLCSettleRequest {
return &HTLCSettleRequest{
ChannelID: chanID,
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.
//
@ -34,12 +49,7 @@ type HTLCSettleRequest struct {
func (c *HTLCSettleRequest) Decode(r io.Reader, pver uint32) error {
// ChannelID(8)
// HTLCKey(8)
// Expiry(4)
// Amount(4)
// NextHop(20)
// ContractType(1)
// RedemptionHashes (numOfHashes * 20 + numOfHashes)
// Blob(2+blobsize)
// RedemptionProofs(N*20)
err := readElements(r,
&c.ChannelID,
&c.HTLCKey,
@ -52,17 +62,10 @@ func (c *HTLCSettleRequest) Decode(r io.Reader, pver uint32) error {
return nil
}
// NewHTLCSettleRequest returns a new empty HTLCSettleRequest.
func NewHTLCSettleRequest() *HTLCSettleRequest {
return &HTLCSettleRequest{}
}
// A compile time check to ensure HTLCSettleRequest implements the lnwire.Message
// interface.
var _ Message = (*HTLCSettleRequest)(nil)
// Serializes the item from the HTLCSettleRequest struct
// Writes the data to w
// 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.ChannelID,

@ -1,38 +1,33 @@
package lnwire
import (
"bytes"
"reflect"
"testing"
)
var (
// Need to to do this here
_ = copy(redemptionHash[:], redemptionHashBytes)
emptyRedemptionProofs = [][20]byte{}
redemptionProofs = append(emptyRedemptionProofs, redemptionHash)
htlcSettleRequest = &HTLCSettleRequest{
ChannelID: uint64(12345678),
HTLCKey: HTLCKey(12345),
RedemptionProofs: redemptionProofs,
}
htlcSettleRequestSerializedString = "0000000000bc614e000000000000303900015b315ebabb0d8c0d94281caa2dfee69a1a00436e"
htlcSettleRequestSerializedMessage = "0709110b0000044c000000260000000000bc614e000000000000303900015b315ebabb0d8c0d94281caa2dfee69a1a00436e"
)
func TestHTLCSettleRequestEncodeDecode(t *testing.T) {
// All of these types being passed are of the message interface type
// Test serialization, runs: message.Encode(b, 0)
// Returns bytes
// Compares the expected serialized string from the original
s := SerializeTest(t, htlcSettleRequest, htlcSettleRequestSerializedString, filename)
redemptionProofs := make([][20]byte, 1)
copy(redemptionProofs[0][:], bytes.Repeat([]byte{0x09}, 20))
// Test deserialization, runs: message.Decode(s, 0)
// Makes sure the deserialized struct is the same as the original
newMessage := NewHTLCSettleRequest()
DeserializeTest(t, s, newMessage, htlcSettleRequest)
// First create a new HTLCSR message.
settleReq := NewHTLCSettleRequest(22, HTLCKey(23), redemptionProofs)
// Test message using Message interface
// Serializes into buf: WriteMessage(buf, message, uint32(1), wire.TestNet3)
// Deserializes into msg: _, msg, _ , err := ReadMessage(buf, uint32(1), wire.TestNet3)
MessageSerializeDeserializeTest(t, htlcSettleRequest, htlcSettleRequestSerializedMessage)
// 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)
}
// Deserialize the encoded SFOP message into a new empty struct.
settleReq2 := &HTLCSettleRequest{}
if err := settleReq2.Decode(&b, 0); err != nil {
t.Fatalf("unable to decode HTLCSettleRequest: %v", err)
}
// Assert equality of the two instances.
if !reflect.DeepEqual(settleReq, settleReq2) {
t.Fatalf("encode/decode error messages don't match %#v vs %#v",
settleReq, settleReq2)
}
}