Consistantly create empty bytes.Buffers.

This commit is contained in:
Josh Rickmar 2014-06-04 22:23:32 -05:00
parent d863c75be7
commit 99c986e21f
6 changed files with 18 additions and 18 deletions

View file

@ -414,17 +414,17 @@ func (a *Account) ExportWatchingWallet() (*Account, error) {
// exportBase64 exports an account's serialized wallet, tx, and utxo // exportBase64 exports an account's serialized wallet, tx, and utxo
// stores as base64-encoded values in a map. // stores as base64-encoded values in a map.
func (a *Account) exportBase64() (map[string]string, error) { func (a *Account) exportBase64() (map[string]string, error) {
buf := &bytes.Buffer{} buf := bytes.Buffer{}
m := make(map[string]string) m := make(map[string]string)
_, err := a.Wallet.WriteTo(buf) _, err := a.Wallet.WriteTo(&buf)
if err != nil { if err != nil {
return nil, err return nil, err
} }
m["wallet"] = base64.StdEncoding.EncodeToString(buf.Bytes()) m["wallet"] = base64.StdEncoding.EncodeToString(buf.Bytes())
buf.Reset() buf.Reset()
if _, err = a.TxStore.WriteTo(buf); err != nil { if _, err = a.TxStore.WriteTo(&buf); err != nil {
return nil, err return nil, err
} }
m["tx"] = base64.StdEncoding.EncodeToString(buf.Bytes()) 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. // to send each to the chain server for relay.
func (a *Account) ResendUnminedTxs() { func (a *Account) ResendUnminedTxs() {
txs := a.TxStore.UnminedDebitTxs() txs := a.TxStore.UnminedDebitTxs()
txbuf := new(bytes.Buffer) txBuf := bytes.Buffer{}
for _, tx := range txs { 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 // Writing to a bytes.Buffer panics for OOM, and should
// not return any other errors. // not return any other errors.
panic(err) panic(err)
} }
hextx := hex.EncodeToString(txbuf.Bytes()) hextx := hex.EncodeToString(txBuf.Bytes())
txsha, err := SendRawTransaction(CurrentServerConn(), hextx) txsha, err := SendRawTransaction(CurrentServerConn(), hextx)
if err != nil { if err != nil {
// TODO(jrick): Check error for if this tx is a double spend, // TODO(jrick): Check error for if this tx is a double spend,
@ -510,7 +510,7 @@ func (a *Account) ResendUnminedTxs() {
} else { } else {
log.Debugf("Resent unmined transaction %v", txsha) log.Debugf("Resent unmined transaction %v", txsha)
} }
txbuf.Reset() txBuf.Reset()
} }
} }

View file

@ -296,9 +296,9 @@ func (a *Account) txToPairs(pairs map[string]btcutil.Amount,
} }
} }
buf := bytes.NewBuffer(nil) buf := bytes.Buffer{}
buf.Grow(msgtx.SerializeSize()) 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 // Hitting OOM by growing or writing to a bytes.Buffer already
// panics, and all returned errors are unexpected. // panics, and all returned errors are unexpected.
panic(err) panic(err)

View file

@ -1170,9 +1170,9 @@ func sendPairs(icmd btcjson.Cmd, account string, amounts map[string]btcutil.Amou
a.ReqNewTxsForAddress(createdTx.changeAddr) a.ReqNewTxsForAddress(createdTx.changeAddr)
} }
serializedTx := bytes.NewBuffer(nil) serializedTx := bytes.Buffer{}
serializedTx.Grow(createdTx.tx.MsgTx().SerializeSize()) 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 // Hitting OOM writing to a bytes.Buffer already panics, and
// all other errors are unexpected. // all other errors are unexpected.
panic(err) panic(err)

View file

@ -745,8 +745,8 @@ type msgTx btcwire.MsgTx
func (tx *msgTx) ReadFrom(r io.Reader) (int64, error) { func (tx *msgTx) ReadFrom(r io.Reader) (int64, error) {
// Read from a TeeReader to return the number of read bytes. // Read from a TeeReader to return the number of read bytes.
buf := new(bytes.Buffer) buf := bytes.Buffer{}
tr := io.TeeReader(r, buf) tr := io.TeeReader(r, &buf)
if err := (*btcwire.MsgTx)(tx).Deserialize(tr); err != nil { if err := (*btcwire.MsgTx)(tx).Deserialize(tr); err != nil {
if buf.Len() != 0 && err == io.EOF { if buf.Len() != 0 && err == io.EOF {
err = io.ErrUnexpectedEOF 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 // written can be returned to the caller. Writing to a to a
// bytes.Buffer never fails except for OOM panics, so check and panic // bytes.Buffer never fails except for OOM panics, so check and panic
// on any unexpected non-nil returned errors. // on any unexpected non-nil returned errors.
buf := new(bytes.Buffer) buf := bytes.Buffer{}
if err := (*btcwire.MsgTx)(tx).Serialize(buf); err != nil { if err := (*btcwire.MsgTx)(tx).Serialize(&buf); err != nil {
panic(err) panic(err)
} }
return io.Copy(w, buf) return io.Copy(w, &buf)
} }
// ReadFrom reads a mined transaction output lookup key from r. The total // ReadFrom reads a mined transaction output lookup key from r. The total

View file

@ -74,7 +74,7 @@ func version() string {
// version and build metadata strings. In particular they MUST only contain // version and build metadata strings. In particular they MUST only contain
// characters in semanticAlphabet. // characters in semanticAlphabet.
func normalizeVerString(str string) string { func normalizeVerString(str string) string {
var result bytes.Buffer result := bytes.Buffer{}
for _, r := range str { for _, r := range str {
if strings.ContainsRune(semanticAlphabet, r) { if strings.ContainsRune(semanticAlphabet, r) {
_, err := result.WriteRune(r) _, err := result.WriteRune(r)

View file

@ -96,7 +96,7 @@ func binaryRead(r io.Reader, order binary.ByteOrder, data interface{}) (n int64,
// See comment for binaryRead(). // See comment for binaryRead().
func binaryWrite(w io.Writer, order binary.ByteOrder, data interface{}) (n int64, err error) { 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 { if err = binary.Write(&buf, order, data); err != nil {
return 0, err return 0, err
} }