From 99c986e21f7b26fbb4efe617c73d6731a4c151d4 Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Wed, 4 Jun 2014 22:23:32 -0500 Subject: [PATCH] Consistantly create empty bytes.Buffers. --- account.go | 14 +++++++------- createtx.go | 4 ++-- rpcserver.go | 4 ++-- txstore/serialization.go | 10 +++++----- version.go | 2 +- wallet/wallet.go | 2 +- 6 files changed, 18 insertions(+), 18 deletions(-) diff --git a/account.go b/account.go index 986cdb4..179017b 100644 --- a/account.go +++ b/account.go @@ -414,17 +414,17 @@ func (a *Account) ExportWatchingWallet() (*Account, error) { // exportBase64 exports an account's serialized wallet, tx, and utxo // stores as base64-encoded values in a map. func (a *Account) exportBase64() (map[string]string, error) { - buf := &bytes.Buffer{} + buf := bytes.Buffer{} m := make(map[string]string) - _, err := a.Wallet.WriteTo(buf) + _, err := a.Wallet.WriteTo(&buf) if err != nil { return nil, err } m["wallet"] = base64.StdEncoding.EncodeToString(buf.Bytes()) buf.Reset() - if _, err = a.TxStore.WriteTo(buf); err != nil { + if _, err = a.TxStore.WriteTo(&buf); err != nil { return nil, err } m["tx"] = base64.StdEncoding.EncodeToString(buf.Bytes()) @@ -493,14 +493,14 @@ func (a *Account) RescanActiveJob() (*RescanJob, error) { // to send each to the chain server for relay. func (a *Account) ResendUnminedTxs() { txs := a.TxStore.UnminedDebitTxs() - txbuf := new(bytes.Buffer) + txBuf := bytes.Buffer{} for _, tx := range txs { - if err := tx.MsgTx().Serialize(txbuf); err != nil { + if err := tx.MsgTx().Serialize(&txBuf); err != nil { // Writing to a bytes.Buffer panics for OOM, and should // not return any other errors. panic(err) } - hextx := hex.EncodeToString(txbuf.Bytes()) + hextx := hex.EncodeToString(txBuf.Bytes()) txsha, err := SendRawTransaction(CurrentServerConn(), hextx) if err != nil { // TODO(jrick): Check error for if this tx is a double spend, @@ -510,7 +510,7 @@ func (a *Account) ResendUnminedTxs() { } else { log.Debugf("Resent unmined transaction %v", txsha) } - txbuf.Reset() + txBuf.Reset() } } diff --git a/createtx.go b/createtx.go index 7e8c4f0..d0ee8aa 100644 --- a/createtx.go +++ b/createtx.go @@ -296,9 +296,9 @@ func (a *Account) txToPairs(pairs map[string]btcutil.Amount, } } - buf := bytes.NewBuffer(nil) + buf := bytes.Buffer{} buf.Grow(msgtx.SerializeSize()) - if err := msgtx.BtcEncode(buf, btcwire.ProtocolVersion); err != nil { + if err := msgtx.BtcEncode(&buf, btcwire.ProtocolVersion); err != nil { // Hitting OOM by growing or writing to a bytes.Buffer already // panics, and all returned errors are unexpected. panic(err) diff --git a/rpcserver.go b/rpcserver.go index 3309fe4..4bde428 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -1170,9 +1170,9 @@ func sendPairs(icmd btcjson.Cmd, account string, amounts map[string]btcutil.Amou a.ReqNewTxsForAddress(createdTx.changeAddr) } - serializedTx := bytes.NewBuffer(nil) + serializedTx := bytes.Buffer{} serializedTx.Grow(createdTx.tx.MsgTx().SerializeSize()) - if err := createdTx.tx.MsgTx().Serialize(serializedTx); err != nil { + if err := createdTx.tx.MsgTx().Serialize(&serializedTx); err != nil { // Hitting OOM writing to a bytes.Buffer already panics, and // all other errors are unexpected. panic(err) diff --git a/txstore/serialization.go b/txstore/serialization.go index 8691721..c39cc3e 100644 --- a/txstore/serialization.go +++ b/txstore/serialization.go @@ -745,8 +745,8 @@ type msgTx btcwire.MsgTx func (tx *msgTx) ReadFrom(r io.Reader) (int64, error) { // Read from a TeeReader to return the number of read bytes. - buf := new(bytes.Buffer) - tr := io.TeeReader(r, buf) + buf := bytes.Buffer{} + tr := io.TeeReader(r, &buf) if err := (*btcwire.MsgTx)(tx).Deserialize(tr); err != nil { if buf.Len() != 0 && err == io.EOF { err = io.ErrUnexpectedEOF @@ -762,11 +762,11 @@ func (tx *msgTx) WriteTo(w io.Writer) (int64, error) { // written can be returned to the caller. Writing to a to a // bytes.Buffer never fails except for OOM panics, so check and panic // on any unexpected non-nil returned errors. - buf := new(bytes.Buffer) - if err := (*btcwire.MsgTx)(tx).Serialize(buf); err != nil { + buf := bytes.Buffer{} + if err := (*btcwire.MsgTx)(tx).Serialize(&buf); err != nil { panic(err) } - return io.Copy(w, buf) + return io.Copy(w, &buf) } // ReadFrom reads a mined transaction output lookup key from r. The total diff --git a/version.go b/version.go index 1154238..c188d66 100644 --- a/version.go +++ b/version.go @@ -74,7 +74,7 @@ func version() string { // version and build metadata strings. In particular they MUST only contain // characters in semanticAlphabet. func normalizeVerString(str string) string { - var result bytes.Buffer + result := bytes.Buffer{} for _, r := range str { if strings.ContainsRune(semanticAlphabet, r) { _, err := result.WriteRune(r) diff --git a/wallet/wallet.go b/wallet/wallet.go index 58e0ce7..b5674b1 100644 --- a/wallet/wallet.go +++ b/wallet/wallet.go @@ -96,7 +96,7 @@ func binaryRead(r io.Reader, order binary.ByteOrder, data interface{}) (n int64, // See comment for binaryRead(). func binaryWrite(w io.Writer, order binary.ByteOrder, data interface{}) (n int64, err error) { - var buf bytes.Buffer + buf := bytes.Buffer{} if err = binary.Write(&buf, order, data); err != nil { return 0, err }