From ccbbcf389bb4b715c574331ff5ee12c37a2e65ca Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Thu, 16 Feb 2017 20:34:44 +0800 Subject: [PATCH] lnwire: directly embed the wire.OutPoint in all commitment update msgs --- lnwire/close_complete.go | 2 +- lnwire/close_complete_test.go | 2 +- lnwire/close_request.go | 4 +- lnwire/close_request_test.go | 2 +- lnwire/error_generic.go | 2 +- lnwire/error_generic_test.go | 2 +- lnwire/lnwire.go | 7 ++-- lnwire/lnwire_test.go | 55 ++++---------------------- lnwire/single_funding_complete.go | 8 ++-- lnwire/single_funding_complete_test.go | 2 +- 10 files changed, 24 insertions(+), 62 deletions(-) diff --git a/lnwire/close_complete.go b/lnwire/close_complete.go index c1246198..2c2fa830 100644 --- a/lnwire/close_complete.go +++ b/lnwire/close_complete.go @@ -19,7 +19,7 @@ import ( // inputs/outputs. type CloseComplete struct { // ChannelPoint serves to identify which channel is to be closed. - ChannelPoint *wire.OutPoint + ChannelPoint wire.OutPoint // ResponderCloseSig is the signature of the responder for the // transaction which closes the previously active channel. diff --git a/lnwire/close_complete_test.go b/lnwire/close_complete_test.go index ee49e65c..1ab67d3b 100644 --- a/lnwire/close_complete_test.go +++ b/lnwire/close_complete_test.go @@ -8,7 +8,7 @@ import ( func TestCloseCompleteEncodeDecode(t *testing.T) { cc := &CloseComplete{ - ChannelPoint: outpoint1, + ChannelPoint: *outpoint1, ResponderCloseSig: commitSig, } diff --git a/lnwire/close_request.go b/lnwire/close_request.go index b5f52142..f26ee19c 100644 --- a/lnwire/close_request.go +++ b/lnwire/close_request.go @@ -22,7 +22,7 @@ import ( // know the order of the inputs/outputs. type CloseRequest struct { // ChannelPoint serves to identify which channel is to be closed. - ChannelPoint *wire.OutPoint + ChannelPoint wire.OutPoint // RequesterCloseSig is the signature of the requester for the fully // assembled closing transaction. @@ -36,7 +36,7 @@ type CloseRequest struct { } // NewCloseRequest creates a new CloseRequest. -func NewCloseRequest(cp *wire.OutPoint, sig *btcec.Signature) *CloseRequest { +func NewCloseRequest(cp wire.OutPoint, sig *btcec.Signature) *CloseRequest { // TODO(roasbeef): update once fees aren't hardcoded return &CloseRequest{ ChannelPoint: cp, diff --git a/lnwire/close_request_test.go b/lnwire/close_request_test.go index 48398446..ff92307a 100644 --- a/lnwire/close_request_test.go +++ b/lnwire/close_request_test.go @@ -10,7 +10,7 @@ import ( func TestCloseRequestEncodeDecode(t *testing.T) { cr := &CloseRequest{ - ChannelPoint: outpoint1, + ChannelPoint: *outpoint1, RequesterCloseSig: commitSig, Fee: btcutil.Amount(10000), } diff --git a/lnwire/error_generic.go b/lnwire/error_generic.go index 6d1ff778..d794b106 100644 --- a/lnwire/error_generic.go +++ b/lnwire/error_generic.go @@ -39,7 +39,7 @@ type ErrorGeneric struct { // ChannelPoint references the active channel in which the error // occurred within. A ChannelPoint of zeroHash:0 denotes this error // applies to the entire established connection. - ChannelPoint *wire.OutPoint + ChannelPoint wire.OutPoint // PendingChannelID allows peers communicate errors in the context of a // particular pending channel. With this field, once a peer reads an diff --git a/lnwire/error_generic_test.go b/lnwire/error_generic_test.go index 71cfa574..4ffb941f 100644 --- a/lnwire/error_generic_test.go +++ b/lnwire/error_generic_test.go @@ -8,7 +8,7 @@ import ( func TestErrorGenericEncodeDecode(t *testing.T) { eg := &ErrorGeneric{ - ChannelPoint: outpoint1, + ChannelPoint: *outpoint1, PendingChannelID: 1, Code: 99, Problem: "Hello world!", diff --git a/lnwire/lnwire.go b/lnwire/lnwire.go index f43537e5..a44c1fbb 100644 --- a/lnwire/lnwire.go +++ b/lnwire/lnwire.go @@ -230,7 +230,8 @@ func writeElement(w io.Writer, element interface{}) error { if err := e.Encode(w); err != nil { return err } - case *wire.OutPoint: + + case wire.OutPoint: // TODO(roasbeef): consolidate with above // First write out the previous txid. var h [32]byte @@ -514,7 +515,7 @@ func readElement(r io.Reader, element interface{}) error { } (*e).PreviousOutPoint.Index = binary.BigEndian.Uint32(idxBytes[:]) return nil - case **wire.OutPoint: + case *wire.OutPoint: // TODO(roasbeef): consolidate with above var h [32]byte if _, err = io.ReadFull(r, h[:]); err != nil { @@ -532,7 +533,7 @@ func readElement(r io.Reader, element interface{}) error { } index := binary.BigEndian.Uint32(idxBytes[:]) - *e = wire.NewOutPoint(hash, index) + *e = wire.OutPoint{Hash: *hash, Index: index} case *int64, *float64: err := binary.Read(r, binary.BigEndian, e) if err != nil { diff --git a/lnwire/lnwire_test.go b/lnwire/lnwire_test.go index bec12a0b..c729cf46 100644 --- a/lnwire/lnwire_test.go +++ b/lnwire/lnwire_test.go @@ -2,7 +2,6 @@ package lnwire import ( "encoding/hex" - "net" "github.com/roasbeef/btcd/btcec" @@ -12,7 +11,6 @@ import ( ) // Common variables and functions for the message tests - var ( revHash = [32]byte{ 0xb7, 0x94, 0x38, 0x5f, 0x2d, 0x1e, 0xf7, 0xab, @@ -21,66 +19,25 @@ var ( 0x6a, 0x49, 0x18, 0x83, 0x31, 0x98, 0x47, 0x53, } - maxUint32 uint32 = (1 << 32) - 1 - maxUint24 uint32 = (1 << 24) - 1 - maxUint16 uint16 = (1 << 16) - 1 - - // For debugging, writes to /dev/shm/ - // Maybe in the future do it if you do "go test -v" - WRITE_FILE = false - filename = "/dev/shm/serialized.raw" - - // preimage: 9a2cbd088763db88dd8ba79e5726daa6aba4aa7e - // echo -n | openssl sha256 | openssl ripemd160 | openssl sha256 | openssl ripemd160 - revocationHashBytes, _ = hex.DecodeString("4132b6b48371f7b022a16eacb9b2b0ebee134d41") - revocationHash [20]byte - - // preimage: "hello world" - redemptionHashBytes, _ = hex.DecodeString("5b315ebabb0d8c0d94281caa2dfee69a1a00436e") - redemptionHash [20]byte - - // preimage: "next hop" - nextHopBytes, _ = hex.DecodeString("94a9ded5a30fc5944cb1e2cbcd980f30616a1440") - nextHop [20]byte + shaHash1Bytes, _ = hex.DecodeString("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855") + shaHash1, _ = chainhash.NewHash(shaHash1Bytes) + outpoint1 = wire.NewOutPoint(shaHash1, 0) privKeyBytes, _ = hex.DecodeString("9fa1d55217f57019a3c37f49465896b15836f54cb8ef6963870a52926420a2dd") privKey, pubKey = btcec.PrivKeyFromBytes(btcec.S256(), privKeyBytes) address = pubKey - // Delivery PkScript - // Privkey: f2c00ead9cbcfec63098dc0a5f152c0165aff40a2ab92feb4e24869a284c32a7 - // PKhash: n2fkWVphUzw3zSigzPsv9GuDyg9mohzKpz - deliveryPkScript, _ = hex.DecodeString("76a914e8048c0fb75bdecc91ebfb99c174f4ece29ffbd488ac") - - // Change PkScript - // Privkey: 5b18f5049efd9d3aff1fb9a06506c0b809fb71562b6ecd02f6c5b3ab298f3b0f - // PKhash: miky84cHvLuk6jcT6GsSbgHR8d7eZCu9Qc - changePkScript, _ = hex.DecodeString("76a914238ee44bb5c8c1314dd03974a17ec6c406fdcb8388ac") - - // echo -n | openssl sha256 - // This stuff gets reversed!!! - shaHash1Bytes, _ = hex.DecodeString("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855") - shaHash1, _ = chainhash.NewHash(shaHash1Bytes) - outpoint1 = wire.NewOutPoint(shaHash1, 0) - // echo | openssl sha256 - // This stuff gets reversed!!! - shaHash2Bytes, _ = hex.DecodeString("01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b") - shaHash2, _ = chainhash.NewHash(shaHash2Bytes) - outpoint2 = wire.NewOutPoint(shaHash2, 1) - // create inputs from outpoint1 and outpoint2 - inputs = []*wire.TxIn{wire.NewTxIn(outpoint1, nil, nil), wire.NewTxIn(outpoint2, nil, nil)} - // Commitment Signature tx = wire.NewMsgTx(1) emptybytes = new([]byte) sigStr, _ = txscript.RawTxInSignature(tx, 0, *emptybytes, txscript.SigHashAll, privKey) commitSig, _ = btcec.ParseSignature(sigStr, btcec.S256()) - // Funding TX Sig 1 sig1privKeyBytes, _ = hex.DecodeString("927f5827d75dd2addeb532c0fa5ac9277565f981dd6d0d037b422be5f60bdbef") sig1privKey, _ = btcec.PrivKeyFromBytes(btcec.S256(), sig1privKeyBytes) sigStr1, _ = txscript.RawTxInSignature(tx, 0, *emptybytes, txscript.SigHashAll, sig1privKey) commitSig1, _ = btcec.ParseSignature(sigStr1, btcec.S256()) + // Funding TX Sig 2 sig2privKeyBytes, _ = hex.DecodeString("8a4ad188f6f4000495b765cfb6ffa591133a73019c45428ddd28f53bab551847") sig2privKey, _ = btcec.PrivKeyFromBytes(btcec.S256(), sig2privKeyBytes) @@ -101,6 +58,10 @@ var ( someAddress = &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 8333} + maxUint32 uint32 = (1 << 32) - 1 + maxUint24 uint32 = (1 << 24) - 1 + maxUint16 uint16 = (1 << 16) - 1 + someChannelID = ChannelID{ BlockHeight: maxUint24, TxIndex: maxUint24, diff --git a/lnwire/single_funding_complete.go b/lnwire/single_funding_complete.go index d6441652..323a8557 100644 --- a/lnwire/single_funding_complete.go +++ b/lnwire/single_funding_complete.go @@ -23,7 +23,7 @@ type SingleFundingComplete struct { // FundingOutPoint is the outpoint (txid:index) of the funding // transaction. With this value, Bob will be able to generate a // signature for Alice's version of the commitment transaction. - FundingOutPoint *wire.OutPoint + FundingOutPoint wire.OutPoint // CommitSignature is Alice's signature for Bob's version of the // commitment transaction. @@ -46,7 +46,7 @@ type SingleFundingComplete struct { // NewSingleFundingComplete creates, and returns a new empty // SingleFundingResponse. -func NewSingleFundingComplete(chanID uint64, fundingPoint *wire.OutPoint, +func NewSingleFundingComplete(chanID uint64, fundingPoint wire.OutPoint, commitSig *btcec.Signature, revokeKey *btcec.PublicKey, obsfucator [4]byte) *SingleFundingComplete { @@ -74,7 +74,7 @@ func (s *SingleFundingComplete) Decode(r io.Reader, pver uint32) error { &s.FundingOutPoint, &s.CommitSignature, &s.RevocationKey, - &s.StateHintObsfucator) + s.StateHintObsfucator[:]) if err != nil { return err } @@ -97,7 +97,7 @@ func (s *SingleFundingComplete) Encode(w io.Writer, pver uint32) error { s.FundingOutPoint, s.CommitSignature, s.RevocationKey, - s.StateHintObsfucator) + s.StateHintObsfucator[:]) if err != nil { return err } diff --git a/lnwire/single_funding_complete_test.go b/lnwire/single_funding_complete_test.go index ac052a3a..a340bc6c 100644 --- a/lnwire/single_funding_complete_test.go +++ b/lnwire/single_funding_complete_test.go @@ -11,7 +11,7 @@ func TestSingleFundingCompleteWire(t *testing.T) { copy(obsfucator[:], bytes.Repeat([]byte("k"), 4)) // First create a new SFC message. - sfc := NewSingleFundingComplete(22, outpoint1, commitSig1, pubKey, + sfc := NewSingleFundingComplete(22, *outpoint1, commitSig1, pubKey, obsfucator) // Next encode the SFC message into an empty bytes buffer.