diff --git a/message.go b/message.go index 98561734..83f51e6f 100644 --- a/message.go +++ b/message.go @@ -218,8 +218,8 @@ func WriteMessageN(w io.Writer, msg Message, pver uint32, btcnet BitcoinNet) (in // Encode the header for the message. This is done to a buffer // rather than directly to the writer since writeElements doesn't // return the number of bytes written. - var hw bytes.Buffer - writeElements(&hw, hdr.magic, command, hdr.length, hdr.checksum) + hw := bytes.NewBuffer(make([]byte, 0, MessageHeaderSize)) + writeElements(hw, hdr.magic, command, hdr.length, hdr.checksum) // Write header. n, err := w.Write(hw.Bytes()) diff --git a/msgtx.go b/msgtx.go index f7cd87d0..61b77c99 100644 --- a/msgtx.go +++ b/msgtx.go @@ -157,9 +157,9 @@ func (msg *MsgTx) TxSha() (ShaHash, error) { // cause a run-time panic. Also, SetBytes can't fail here due to the // fact DoubleSha256 always returns a []byte of the right size // regardless of input. - var buf bytes.Buffer + buf := bytes.NewBuffer(make([]byte, 0, msg.SerializeSize())) + _ = msg.Serialize(buf) var sha ShaHash - _ = msg.Serialize(&buf) _ = sha.SetBytes(DoubleSha256(buf.Bytes())) // Even though this function can't currently fail, it still returns