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 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 // Decode deserializes a serialized CloseComplete message stored in the passed
// io.Reader observing the specified protocol version. // io.Reader observing the specified protocol version.
// //
@ -44,16 +54,6 @@ func (c *CloseComplete) Decode(r io.Reader, pver uint32) error {
return nil 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 // Encode serializes the target CloseComplete into the passed io.Writer observing
// the protocol version specified. // the protocol version specified.
// //

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

@ -30,14 +30,25 @@ type CommitRevocation struct {
NextRevocationHash [20]byte 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 // Decode deserializes a serialized CommitRevocation message stored in the
// passed io.Reader observing the specified protocol version. // passed io.Reader observing the specified protocol version.
// //
// This is part of the lnwire.Message interface. // This is part of the lnwire.Message interface.
func (c *CommitRevocation) Decode(r io.Reader, pver uint32) error { func (c *CommitRevocation) Decode(r io.Reader, pver uint32) error {
// ChannelID (8)
// NextRevocationHash (20) // NextRevocationHash (20)
// Revocation (20) // Revocation (20)
err := readElements(r, err := readElements(r,
&c.ChannelID,
&c.NextRevocationHash, &c.NextRevocationHash,
&c.Revocation, &c.Revocation,
) )
@ -48,21 +59,13 @@ func (c *CommitRevocation) Decode(r io.Reader, pver uint32) error {
return nil 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 // Encode serializes the target CommitRevocation into the passed io.Writer
// observing the protocol version specified. // observing the protocol version specified.
// //
// This is part of the lnwire.Message interface. // This is part of the lnwire.Message interface.
func (c *CommitRevocation) Encode(w io.Writer, pver uint32) error { func (c *CommitRevocation) Encode(w io.Writer, pver uint32) error {
err := writeElements(w, err := writeElements(w,
c.ChannelID,
c.NextRevocationHash, c.NextRevocationHash,
c.Revocation, c.Revocation,
) )

@ -1,37 +1,36 @@
package lnwire package lnwire
import ( import (
"bytes"
"reflect"
"testing" "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) { func TestCommitRevocationEncodeDecode(t *testing.T) {
// All of these types being passed are of the message interface type copy(revocationHash[:], revocationHashBytes)
// Test serialization, runs: message.Encode(b, 0) copy(nextHop[:], nextHopBytes)
// Returns bytes
// Compares the expected serialized string from the original
s := SerializeTest(t, commitRevocation, commitRevocationSerializedString, filename)
// Test deserialization, runs: message.Decode(s, 0) cr := &CommitRevocation{
// Makes sure the deserialized struct is the same as the original ChannelID: uint64(12345678),
newMessage := NewCommitRevocation() Revocation: revocationHash,
DeserializeTest(t, s, newMessage, commitRevocation) NextRevocationHash: nextHop,
}
// Test message using Message interface
// Serializes into buf: WriteMessage(buf, message, uint32(1), wire.TestNet3) // Next encode the CR message into an empty bytes buffer.
// Deserializes into msg: _, msg, _ , err := ReadMessage(buf, uint32(1), wire.TestNet3) var b bytes.Buffer
MessageSerializeDeserializeTest(t, commitRevocation, commitRevocationSerializedMessage) 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 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 // Decode deserializes a serialized CommitSignature message stored in the
// passed io.Reader observing the specified protocol version. // passed io.Reader observing the specified protocol version.
// //
@ -53,15 +62,6 @@ func (c *CommitSignature) Decode(r io.Reader, pver uint32) error {
return nil 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 // Encode serializes the target CommitSignature into the passed io.Writer
// observing the protocol version specified. // observing the protocol version specified.
// //

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

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

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

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

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

@ -5,6 +5,20 @@ import (
"io" "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 // 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 // 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 // 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. // length of this slice should be N.
RedemptionHashes [][20]byte 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 // OnionBlob is the raw serialized mix header used to route an HTLC in
// a privacy-preserving manner. The mix header is defined currently to // a privacy-preserving manner. The mix header is defined currently to
// be parsed as a 4-tuple: (groupElement, routingInfo, headerMAC, body). // be parsed as a 4-tuple: (groupElement, routingInfo, headerMAC, body).
@ -62,6 +58,15 @@ type HTLCAddRequest struct {
OnionBlob []byte 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 // Decode deserializes a serialized HTLCAddRequest message stored in the passed
// io.Reader observing the specified protocol version. // io.Reader observing the specified protocol version.
// //
@ -72,7 +77,6 @@ func (c *HTLCAddRequest) Decode(r io.Reader, pver uint32) error {
// Amount(4) // Amount(4)
// ContractType(1) // ContractType(1)
// RedemptionHashes (numOfHashes * 20 + numOfHashes) // RedemptionHashes (numOfHashes * 20 + numOfHashes)
// Blob(2+blobsize)
// OnionBlog // OnionBlog
err := readElements(r, err := readElements(r,
&c.ChannelID, &c.ChannelID,
@ -80,7 +84,6 @@ func (c *HTLCAddRequest) Decode(r io.Reader, pver uint32) error {
&c.Amount, &c.Amount,
&c.ContractType, &c.ContractType,
&c.RedemptionHashes, &c.RedemptionHashes,
&c.Blob,
&c.OnionBlob, &c.OnionBlob,
) )
if err != nil { if err != nil {
@ -90,15 +93,6 @@ func (c *HTLCAddRequest) Decode(r io.Reader, pver uint32) error {
return nil 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 // Encode serializes the target HTLCAddRequest into the passed io.Writer observing
// the protocol version specified. // the protocol version specified.
// //
@ -110,7 +104,6 @@ func (c *HTLCAddRequest) Encode(w io.Writer, pver uint32) error {
c.Amount, c.Amount,
c.ContractType, c.ContractType,
c.RedemptionHashes, c.RedemptionHashes,
c.Blob,
c.OnionBlob, c.OnionBlob,
) )
if err != nil { 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("ContractType:\t%d (%b)\n", c.ContractType, c.ContractType) +
fmt.Sprintf("RedemptionHashes:") + fmt.Sprintf("RedemptionHashes:") +
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("OnionBlob:\t\t\t\t%x\n", c.OnionBlob) +
fmt.Sprintf("--- End HTLCAddRequest ---\n") fmt.Sprintf("--- End HTLCAddRequest ---\n")
} }

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

@ -27,6 +27,21 @@ type HTLCSettleRequest struct {
RedemptionProofs [][20]byte 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 // Decode deserializes a serialized HTLCSettleRequest message stored in the passed
// io.Reader observing the specified protocol version. // io.Reader observing the specified protocol version.
// //
@ -34,12 +49,7 @@ type HTLCSettleRequest struct {
func (c *HTLCSettleRequest) Decode(r io.Reader, pver uint32) error { func (c *HTLCSettleRequest) Decode(r io.Reader, pver uint32) error {
// ChannelID(8) // ChannelID(8)
// HTLCKey(8) // HTLCKey(8)
// Expiry(4) // RedemptionProofs(N*20)
// Amount(4)
// NextHop(20)
// ContractType(1)
// RedemptionHashes (numOfHashes * 20 + numOfHashes)
// Blob(2+blobsize)
err := readElements(r, err := readElements(r,
&c.ChannelID, &c.ChannelID,
&c.HTLCKey, &c.HTLCKey,
@ -52,17 +62,10 @@ func (c *HTLCSettleRequest) Decode(r io.Reader, pver uint32) error {
return nil return nil
} }
// NewHTLCSettleRequest returns a new empty HTLCSettleRequest. // Encode serializes the target HTLCSettleRequest into the passed io.Writer
func NewHTLCSettleRequest() *HTLCSettleRequest { // observing the protocol version specified.
return &HTLCSettleRequest{} //
} // This is part of the lnwire.Message interface.
// 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
func (c *HTLCSettleRequest) Encode(w io.Writer, pver uint32) error { func (c *HTLCSettleRequest) Encode(w io.Writer, pver uint32) error {
err := writeElements(w, err := writeElements(w,
c.ChannelID, c.ChannelID,

@ -1,38 +1,33 @@
package lnwire package lnwire
import ( import (
"bytes"
"reflect"
"testing" "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) { func TestHTLCSettleRequestEncodeDecode(t *testing.T) {
// All of these types being passed are of the message interface type redemptionProofs := make([][20]byte, 1)
// Test serialization, runs: message.Encode(b, 0) copy(redemptionProofs[0][:], bytes.Repeat([]byte{0x09}, 20))
// Returns bytes
// Compares the expected serialized string from the original
s := SerializeTest(t, htlcSettleRequest, htlcSettleRequestSerializedString, filename)
// Test deserialization, runs: message.Decode(s, 0) // First create a new HTLCSR message.
// Makes sure the deserialized struct is the same as the original settleReq := NewHTLCSettleRequest(22, HTLCKey(23), redemptionProofs)
newMessage := NewHTLCSettleRequest()
DeserializeTest(t, s, newMessage, htlcSettleRequest)
// Test message using Message interface // Next encode the HTLCSR message into an empty bytes buffer.
// Serializes into buf: WriteMessage(buf, message, uint32(1), wire.TestNet3) var b bytes.Buffer
// Deserializes into msg: _, msg, _ , err := ReadMessage(buf, uint32(1), wire.TestNet3) if err := settleReq.Encode(&b, 0); err != nil {
MessageSerializeDeserializeTest(t, htlcSettleRequest, htlcSettleRequestSerializedMessage) 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)
}
} }