From 266c1215104bc39051d1bef3a675782caec6b6f6 Mon Sep 17 00:00:00 2001 From: Joseph Poon Date: Thu, 31 Dec 2015 00:25:00 -0800 Subject: [PATCH] Changed type in Accept/Complete & lnwire refactor * FundingSignAccept and FundingSingComplete had *[]btcec.Signature and instead it's now []*btcec.Signature to match other slice types. * Refactored lnwire's when doing readElement/writeElement on slices --- lnwire/funding_signaccept.go | 6 +-- lnwire/funding_signaccept_test.go | 2 +- lnwire/funding_signcomplete.go | 6 +-- lnwire/funding_signcomplete_test.go | 2 +- lnwire/lnwire.go | 84 +++++++++++++++++------------ lnwire/lnwire_test.go | 2 +- 6 files changed, 58 insertions(+), 44 deletions(-) diff --git a/lnwire/funding_signaccept.go b/lnwire/funding_signaccept.go index e0ce9ec2..52b01949 100644 --- a/lnwire/funding_signaccept.go +++ b/lnwire/funding_signaccept.go @@ -10,7 +10,7 @@ type FundingSignAccept struct { ReservationID uint64 CommitSig *btcec.Signature //Requester's Commitment - FundingTXSigs *[]btcec.Signature + FundingTXSigs []*btcec.Signature } func (c *FundingSignAccept) Decode(r io.Reader, pver uint32) error { @@ -71,9 +71,9 @@ func (c *FundingSignAccept) Validate() error { func (c *FundingSignAccept) String() string { var sigs string - for i, in := range *c.FundingTXSigs { + for i, in := range c.FundingTXSigs { sigs += fmt.Sprintf("\n Slice\t%d\n", i) - if &in != nil && in.R != nil { + if in != nil { sigs += fmt.Sprintf("\tSig\t%x\n", in.Serialize()) } } diff --git a/lnwire/funding_signaccept_test.go b/lnwire/funding_signaccept_test.go index c2a71367..8b3b3a6e 100644 --- a/lnwire/funding_signaccept_test.go +++ b/lnwire/funding_signaccept_test.go @@ -9,7 +9,7 @@ var ( fundingSignAccept = &FundingSignAccept{ ReservationID: uint64(12345678), CommitSig: commitSig, - FundingTXSigs: &fundingTXSigs, + FundingTXSigs: ptrFundingTXSigs, } fundingSignAcceptSerializedString = "0000000000bc614e4630440220333835e58e958f5e92b4ff4e6fa2470dac88094c97506b4d6d1f4e23e52cb481022057483ac18d6b9c9c14f0c626694c9ccf8b27b3dbbedfdf6b6c9a9fa9f427a1df02473045022100e7946d057c0b4cc4d3ea525ba156b429796858ebc543d75a6c6c2cbca732db6902202fea377c1f9fb98cd103cf5a4fba276a074b378d4227d15f5fa6439f1a6685bb4630440220235ee55fed634080089953048c3e3f7dc3a154fd7ad18f31dc08e05b7864608a02203bdd7d4e4d9a8162d4b511faf161f0bb16c45181187125017cd0c620c53876ca" fundingSignAcceptSerializedMessage = "0709110b000000dc000000df0000000000bc614e4630440220333835e58e958f5e92b4ff4e6fa2470dac88094c97506b4d6d1f4e23e52cb481022057483ac18d6b9c9c14f0c626694c9ccf8b27b3dbbedfdf6b6c9a9fa9f427a1df02473045022100e7946d057c0b4cc4d3ea525ba156b429796858ebc543d75a6c6c2cbca732db6902202fea377c1f9fb98cd103cf5a4fba276a074b378d4227d15f5fa6439f1a6685bb4630440220235ee55fed634080089953048c3e3f7dc3a154fd7ad18f31dc08e05b7864608a02203bdd7d4e4d9a8162d4b511faf161f0bb16c45181187125017cd0c620c53876ca" diff --git a/lnwire/funding_signcomplete.go b/lnwire/funding_signcomplete.go index 4fd681d8..86e90c1a 100644 --- a/lnwire/funding_signcomplete.go +++ b/lnwire/funding_signcomplete.go @@ -11,7 +11,7 @@ type FundingSignComplete struct { ReservationID uint64 TxID *wire.ShaHash - FundingTXSigs *[]btcec.Signature + FundingTXSigs []*btcec.Signature } func (c *FundingSignComplete) Decode(r io.Reader, pver uint32) error { @@ -71,9 +71,9 @@ func (c *FundingSignComplete) Validate() error { func (c *FundingSignComplete) String() string { var sigs string - for i, in := range *c.FundingTXSigs { + for i, in := range c.FundingTXSigs { sigs += fmt.Sprintf("\n Slice\t%d\n", i) - if &in != nil && in.R != nil { + if in != nil { sigs += fmt.Sprintf("\tSig\t%x\n", in.Serialize()) } } diff --git a/lnwire/funding_signcomplete_test.go b/lnwire/funding_signcomplete_test.go index 442113fc..de958d34 100644 --- a/lnwire/funding_signcomplete_test.go +++ b/lnwire/funding_signcomplete_test.go @@ -9,7 +9,7 @@ var ( fundingSignComplete = &FundingSignComplete{ ReservationID: uint64(12345678), TxID: txid, - FundingTXSigs: &fundingTXSigs, + FundingTXSigs: ptrFundingTXSigs, } fundingSignCompleteSerializedString = "0000000000bc614efd95c6e5c9d5bcf9cfc7231b6a438e46c518c724d0b04b75cc8fddf84a254e3a02473045022100e7946d057c0b4cc4d3ea525ba156b429796858ebc543d75a6c6c2cbca732db6902202fea377c1f9fb98cd103cf5a4fba276a074b378d4227d15f5fa6439f1a6685bb4630440220235ee55fed634080089953048c3e3f7dc3a154fd7ad18f31dc08e05b7864608a02203bdd7d4e4d9a8162d4b511faf161f0bb16c45181187125017cd0c620c53876ca" fundingSignCompleteSerializedMessage = "0709110b000000e6000000b80000000000bc614efd95c6e5c9d5bcf9cfc7231b6a438e46c518c724d0b04b75cc8fddf84a254e3a02473045022100e7946d057c0b4cc4d3ea525ba156b429796858ebc543d75a6c6c2cbca732db6902202fea377c1f9fb98cd103cf5a4fba276a074b378d4227d15f5fa6439f1a6685bb4630440220235ee55fed634080089953048c3e3f7dc3a154fd7ad18f31dc08e05b7864608a02203bdd7d4e4d9a8162d4b511faf161f0bb16c45181187125017cd0c620c53876ca" diff --git a/lnwire/lnwire.go b/lnwire/lnwire.go index 6446734a..25c6bc86 100644 --- a/lnwire/lnwire.go +++ b/lnwire/lnwire.go @@ -66,8 +66,8 @@ func writeElement(w io.Writer, element interface{}) error { return err } return nil - case *[]btcec.Signature: - numSigs := len(*e) + case []*btcec.Signature: + numSigs := len(e) if numSigs > 127 { return fmt.Errorf("Too many signatures!") } @@ -78,7 +78,7 @@ func writeElement(w io.Writer, element interface{}) error { } //Write the data for i := 0; i < numSigs; i++ { - err = writeElement(w, &(*e)[i]) + err = writeElement(w, e[i]) if err != nil { return err } @@ -151,22 +151,29 @@ func writeElement(w io.Writer, element interface{}) error { //Append the actual TxIns (Size: NumOfTxins * 36) //Do not include the sequence number to eliminate funny business for _, in := range e { - //Hash - var h [32]byte - copy(h[:], in.PreviousOutPoint.Hash.Bytes()) - _, err = w.Write(h[:]) - if err != nil { - return err - } - //Index - var idx [4]byte - binary.BigEndian.PutUint32(idx[:], in.PreviousOutPoint.Index) - _, err = w.Write(idx[:]) + err = writeElement(w, in) if err != nil { return err } } return nil + case *wire.TxIn: + //Hash + var h [32]byte + copy(h[:], e.PreviousOutPoint.Hash.Bytes()) + _, err = w.Write(h[:]) + if err != nil { + return err + } + //Index + var idx [4]byte + binary.BigEndian.PutUint32(idx[:], e.PreviousOutPoint.Index) + _, err = w.Write(idx[:]) + if err != nil { + return err + } + return nil + default: return fmt.Errorf("Unknown type in writeElement: %T", e) } @@ -239,7 +246,7 @@ func readElement(r io.Reader, element interface{}) error { } *e = &*x return nil - case **[]btcec.Signature: + case *[]*btcec.Signature: var numSigs uint8 err = readElement(r, &numSigs) if err != nil { @@ -250,16 +257,16 @@ func readElement(r io.Reader, element interface{}) error { } //Read that number of signatures - var sigs []btcec.Signature + var sigs []*btcec.Signature for i := uint8(0); i < numSigs; i++ { sig := new(btcec.Signature) err = readElement(r, &sig) if err != nil { return err } - sigs = append(sigs, *sig) + sigs = append(sigs, sig) } - *e = &sigs + *e = *&sigs return nil case **btcec.Signature: var sigLength uint8 @@ -337,29 +344,36 @@ func readElement(r io.Reader, element interface{}) error { //Append the actual TxIns var txins []*wire.TxIn for i := uint8(0); i < numScripts; i++ { - //Hash - var h [32]byte - _, err = io.ReadFull(r, h[:]) + outpoint := new(wire.OutPoint) + txin := wire.NewTxIn(outpoint, nil) + err = readElement(r, &txin) if err != nil { return err } - shaHash, err := wire.NewShaHash(h[:]) - if err != nil { - return err - } - //Index - var idxBytes [4]byte - _, err = io.ReadFull(r, idxBytes[:]) - if err != nil { - return err - } - index := binary.BigEndian.Uint32(idxBytes[:]) - outPoint := wire.NewOutPoint(shaHash, index) - txins = append(txins, wire.NewTxIn(outPoint, nil)) - + txins = append(txins, txin) } *e = *&txins return nil + case **wire.TxIn: + //Hash + var h [32]byte + _, err = io.ReadFull(r, h[:]) + if err != nil { + return err + } + hash, err := wire.NewShaHash(h[:]) + (*e).PreviousOutPoint.Hash = *hash + if err != nil { + return err + } + //Index + var idxBytes [4]byte + _, err = io.ReadFull(r, idxBytes[:]) + if err != nil { + return err + } + (*e).PreviousOutPoint.Index = binary.BigEndian.Uint32(idxBytes[:]) + return nil default: return fmt.Errorf("Unknown type in readElement: %T", e) } diff --git a/lnwire/lnwire_test.go b/lnwire/lnwire_test.go index 5b58a731..19ec4e0f 100644 --- a/lnwire/lnwire_test.go +++ b/lnwire/lnwire_test.go @@ -67,7 +67,7 @@ var ( sigStr2, _ = txscript.RawTxInSignature(tx, 0, *emptybytes, txscript.SigHashAll, sig2privKey) commitSig2, _ = btcec.ParseSignature(sigStr2, btcec.S256()) //Slice of Funding TX Sigs - fundingTXSigs = append(*new([]btcec.Signature), *commitSig1, *commitSig2) + ptrFundingTXSigs = append(*new([]*btcec.Signature), commitSig1, commitSig2) //TxID txid = new(wire.ShaHash)