From 3b6e456371c3a1a2a33daeb2f6492d5dbb58757b Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Mon, 30 May 2016 16:49:48 -0700 Subject: [PATCH] lnwire: revamp previous encode/decode tests to passing state --- lnwire/close_complete.go | 20 ++++++------ lnwire/close_complete_test.go | 38 +++++++++++----------- lnwire/commit_revocation.go | 21 ++++++------ lnwire/commit_revocation_test.go | 53 +++++++++++++++---------------- lnwire/commit_signature.go | 18 +++++------ lnwire/commit_signature_test.go | 41 ++++++++++++------------ lnwire/error_generic.go | 46 ++++++++++++++------------- lnwire/error_generic_test.go | 39 ++++++++++++----------- lnwire/funding_request_test.go | 41 ++++++++++++------------ lnwire/funding_response_test.go | 41 ++++++++++++------------ lnwire/htlc_addreject.go | 14 ++++++++ lnwire/htlc_addrequest.go | 26 ++++++--------- lnwire/htlc_addrequest_test.go | 49 ++++++++++++++-------------- lnwire/htlc_settlerequest.go | 37 +++++++++++---------- lnwire/htlc_settlerequest_test.go | 51 ++++++++++++++--------------- 15 files changed, 269 insertions(+), 266 deletions(-) diff --git a/lnwire/close_complete.go b/lnwire/close_complete.go index f235d02e..81a5178e 100644 --- a/lnwire/close_complete.go +++ b/lnwire/close_complete.go @@ -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. // diff --git a/lnwire/close_complete_test.go b/lnwire/close_complete_test.go index 4500bf3b..43dcccdb 100644 --- a/lnwire/close_complete_test.go +++ b/lnwire/close_complete_test.go @@ -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) + // 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) + } - // 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) + // 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) + } - // 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) + // 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) + } } diff --git a/lnwire/commit_revocation.go b/lnwire/commit_revocation.go index 114e4c17..dedf1130 100644 --- a/lnwire/commit_revocation.go +++ b/lnwire/commit_revocation.go @@ -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, ) diff --git a/lnwire/commit_revocation_test.go b/lnwire/commit_revocation_test.go index 0f62c779..537e1d16 100644 --- a/lnwire/commit_revocation_test.go +++ b/lnwire/commit_revocation_test.go @@ -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) + cr := &CommitRevocation{ + ChannelID: uint64(12345678), + Revocation: revocationHash, + NextRevocationHash: nextHop, + } - // 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) + // 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) + } } diff --git a/lnwire/commit_signature.go b/lnwire/commit_signature.go index acf10248..1563004e 100644 --- a/lnwire/commit_signature.go +++ b/lnwire/commit_signature.go @@ -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. // diff --git a/lnwire/commit_signature_test.go b/lnwire/commit_signature_test.go index 20a6fe19..6e29ef09 100644 --- a/lnwire/commit_signature_test.go +++ b/lnwire/commit_signature_test.go @@ -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) + // 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) + } - // 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) + // 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) + } - // 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) + // 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) + } } diff --git a/lnwire/error_generic.go b/lnwire/error_generic.go index c7ab6bfa..efd5b938 100644 --- a/lnwire/error_generic.go +++ b/lnwire/error_generic.go @@ -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") } diff --git a/lnwire/error_generic_test.go b/lnwire/error_generic_test.go index a2ec5972..c15c0b89 100644 --- a/lnwire/error_generic_test.go +++ b/lnwire/error_generic_test.go @@ -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) + // 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) + } - // 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) + // 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) + } - // 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) + // 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) + } } diff --git a/lnwire/funding_request_test.go b/lnwire/funding_request_test.go index e8f50406..c5d3235e 100644 --- a/lnwire/funding_request_test.go +++ b/lnwire/funding_request_test.go @@ -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) + // 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) + } - // 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) + // 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) + } - // 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) + // 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) + } } diff --git a/lnwire/funding_response_test.go b/lnwire/funding_response_test.go index 30fb90da..e3e35713 100644 --- a/lnwire/funding_response_test.go +++ b/lnwire/funding_response_test.go @@ -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) + // 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) + } - // 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) + // 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) + } - // 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) + // 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) + } } diff --git a/lnwire/htlc_addreject.go b/lnwire/htlc_addreject.go index b93c9c77..af57d78f 100644 --- a/lnwire/htlc_addreject.go +++ b/lnwire/htlc_addreject.go @@ -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 diff --git a/lnwire/htlc_addrequest.go b/lnwire/htlc_addrequest.go index e7eca7ae..09d7ff06 100644 --- a/lnwire/htlc_addrequest.go +++ b/lnwire/htlc_addrequest.go @@ -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") } diff --git a/lnwire/htlc_addrequest_test.go b/lnwire/htlc_addrequest_test.go index 74a3d922..cec612af 100644 --- a/lnwire/htlc_addrequest_test.go +++ b/lnwire/htlc_addrequest_test.go @@ -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" -) + // 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) + } -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) + // 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) + } - // 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) + // 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) + } } diff --git a/lnwire/htlc_settlerequest.go b/lnwire/htlc_settlerequest.go index 015d24d1..80d78fd2 100644 --- a/lnwire/htlc_settlerequest.go +++ b/lnwire/htlc_settlerequest.go @@ -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, diff --git a/lnwire/htlc_settlerequest_test.go b/lnwire/htlc_settlerequest_test.go index 51018d8c..443877ab 100644 --- a/lnwire/htlc_settlerequest_test.go +++ b/lnwire/htlc_settlerequest_test.go @@ -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) + } }