mirror of
https://github.com/FlipsideCrypto/dcrd.git
synced 2026-02-06 10:56:47 +00:00
Preallocate the exact number of bytes if known.
This commit is contained in:
parent
7cb68f472a
commit
99d04eea40
@ -1083,6 +1083,7 @@ func winningTickets(voteBlock *wire.MsgBlock, liveTickets []*stakeTicket, numVot
|
||||
// Serialize the parent block header used as the seed to the
|
||||
// deterministic pseudo random number generator for vote selection.
|
||||
var buf bytes.Buffer
|
||||
buf.Grow(wire.MaxBlockHeaderPayload)
|
||||
if err := voteBlock.Header.Serialize(&buf); err != nil {
|
||||
return nil, chainhash.Hash{}, err
|
||||
}
|
||||
|
||||
@ -62,6 +62,7 @@ func TestIsSStx(t *testing.T) {
|
||||
func TestIsSSTxErrors(t *testing.T) {
|
||||
// Initialize the buffer for later manipulation
|
||||
var buf bytes.Buffer
|
||||
buf.Grow(sstxMsgTx.SerializeSize())
|
||||
err := sstxMsgTx.Serialize(&buf)
|
||||
if err != nil {
|
||||
t.Errorf("Error serializing the reference sstx: %v", err)
|
||||
@ -294,6 +295,7 @@ func TestIsSSGen(t *testing.T) {
|
||||
func TestIsSSGenErrors(t *testing.T) {
|
||||
// Initialize the buffer for later manipulation
|
||||
var buf bytes.Buffer
|
||||
buf.Grow(ssgenMsgTx.SerializeSize())
|
||||
err := ssgenMsgTx.Serialize(&buf)
|
||||
if err != nil {
|
||||
t.Errorf("Error serializing the reference sstx: %v", err)
|
||||
@ -626,6 +628,7 @@ func TestIsSSRtx(t *testing.T) {
|
||||
func TestIsSSRtxErrors(t *testing.T) {
|
||||
// Initialize the buffer for later manipulation
|
||||
var buf bytes.Buffer
|
||||
buf.Grow(ssrtxMsgTx.SerializeSize())
|
||||
err := ssrtxMsgTx.Serialize(&buf)
|
||||
if err != nil {
|
||||
t.Errorf("Error serializing the reference sstx: %v", err)
|
||||
|
||||
@ -3364,6 +3364,7 @@ func handleGetHeaders(s *rpcServer, cmd interface{}, closeChan <-chan struct{})
|
||||
|
||||
hexBlockHeaders := make([]string, len(blockHeaders))
|
||||
var buf bytes.Buffer
|
||||
buf.Grow(wire.MaxBlockHeaderPayload)
|
||||
for i, h := range blockHeaders {
|
||||
err := h.Serialize(&buf)
|
||||
if err != nil {
|
||||
|
||||
@ -382,10 +382,11 @@ func calcSignatureHash(script []parsedOpcode, hashType SigHashType,
|
||||
}
|
||||
|
||||
// The final hash (message to sign) is the hash of:
|
||||
// 1) hash of the prefix ||
|
||||
// 2) hash of the witness for signing ||
|
||||
// 3) the hash type (encoded as a 4-byte little-endian value)
|
||||
// 1) the hash type (encoded as a 4-byte little-endian value)
|
||||
// 2) hash of the prefix ||
|
||||
// 3) hash of the witness for signing ||
|
||||
var wbuf bytes.Buffer
|
||||
wbuf.Grow(chainhash.HashSize*2 + 4)
|
||||
binary.Write(&wbuf, binary.LittleEndian, uint32(hashType))
|
||||
|
||||
// Optimization for SIGHASH_ALL. In this case, the prefix hash is
|
||||
|
||||
@ -93,6 +93,7 @@ func (h *BlockHeader) BlockHash() chainhash.Hash {
|
||||
// encode could fail except being out of memory which would cause a
|
||||
// run-time panic.
|
||||
var buf bytes.Buffer
|
||||
buf.Grow(MaxBlockHeaderPayload)
|
||||
_ = writeBlockHeader(&buf, 0, h)
|
||||
|
||||
return chainhash.HashH(buf.Bytes())
|
||||
@ -145,6 +146,7 @@ func (h *BlockHeader) Serialize(w io.Writer) error {
|
||||
func (h *BlockHeader) Bytes() ([]byte, error) {
|
||||
// Serialize the MsgBlock.
|
||||
var w bytes.Buffer
|
||||
w.Grow(MaxBlockHeaderPayload)
|
||||
err := h.Serialize(&w)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@ -196,6 +196,7 @@ func TestBlockHeaderWire(t *testing.T) {
|
||||
// Encode to wire format.
|
||||
// Former test (doesn't work because of capacity error)
|
||||
var buf bytes.Buffer
|
||||
buf.Grow(MaxBlockHeaderPayload)
|
||||
err := writeBlockHeader(&buf, test.pver, test.in)
|
||||
if err != nil {
|
||||
t.Errorf("writeBlockHeader #%d error %v", i, err)
|
||||
@ -319,9 +320,13 @@ func TestBlockHeaderSerialize(t *testing.T) {
|
||||
}
|
||||
|
||||
t.Logf("Running %d tests", len(tests))
|
||||
var buf bytes.Buffer
|
||||
buf.Grow(MaxBlockHeaderPayload)
|
||||
for i, test := range tests {
|
||||
// Clear existing contents.
|
||||
buf.Reset()
|
||||
|
||||
// Serialize the block header.
|
||||
var buf bytes.Buffer
|
||||
err := test.in.Serialize(&buf)
|
||||
if err != nil {
|
||||
t.Errorf("Serialize #%d error %v", i, err)
|
||||
|
||||
@ -303,6 +303,7 @@ func (msg *MsgBlock) Serialize(w io.Writer) error {
|
||||
func (msg *MsgBlock) Bytes() ([]byte, error) {
|
||||
// Serialize the MsgTx.
|
||||
var w bytes.Buffer
|
||||
w.Grow(msg.SerializeSize())
|
||||
err := msg.Serialize(&w)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@ -315,6 +315,7 @@ func TestBlockSerialize(t *testing.T) {
|
||||
for i, test := range tests {
|
||||
// Serialize the block.
|
||||
var buf bytes.Buffer
|
||||
buf.Grow(test.in.SerializeSize())
|
||||
err := test.in.Serialize(&buf)
|
||||
if err != nil {
|
||||
t.Errorf("Serialize #%d error %v", i, err)
|
||||
|
||||
@ -1397,6 +1397,7 @@ func (msg *MsgTx) LegacySerialize(w io.Writer) error {
|
||||
func (msg *MsgTx) Bytes() ([]byte, error) {
|
||||
// Serialize the MsgTx.
|
||||
var w bytes.Buffer
|
||||
w.Grow(msg.SerializeSize())
|
||||
err := msg.Serialize(&w)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -1409,6 +1410,7 @@ func (msg *MsgTx) BytesPrefix() ([]byte, error) {
|
||||
mtxCopy := msg.shallowCopyForSerializing(NoWitnessMsgTxVersion())
|
||||
|
||||
var w bytes.Buffer
|
||||
w.Grow(msg.SerializeSize())
|
||||
err := mtxCopy.Serialize(&w)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -1421,6 +1423,7 @@ func (msg *MsgTx) BytesWitness() ([]byte, error) {
|
||||
mtxCopy := msg.shallowCopyForSerializing(WitnessOnlyMsgTxVersion())
|
||||
|
||||
var w bytes.Buffer
|
||||
w.Grow(msg.SerializeSize())
|
||||
err := mtxCopy.Serialize(&w)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@ -379,6 +379,7 @@ func TestTxSerialize(t *testing.T) {
|
||||
for i, test := range tests {
|
||||
// Serialize the transaction.
|
||||
var buf bytes.Buffer
|
||||
buf.Grow(test.in.SerializeSize())
|
||||
err := test.in.Serialize(&buf)
|
||||
if err != nil {
|
||||
t.Errorf("Serialize #%d error %v", i, err)
|
||||
@ -472,6 +473,7 @@ func TestTxSerializePrefix(t *testing.T) {
|
||||
for i, test := range tests {
|
||||
// Serialize the transaction.
|
||||
var buf bytes.Buffer
|
||||
buf.Grow(test.in.SerializeSize())
|
||||
err := test.in.Serialize(&buf)
|
||||
if err != nil {
|
||||
t.Errorf("Serialize #%d error %v", i, err)
|
||||
@ -562,6 +564,7 @@ func TestTxSerializeWitness(t *testing.T) {
|
||||
for i, test := range tests {
|
||||
// Serialize the transaction.
|
||||
var buf bytes.Buffer
|
||||
buf.Grow(test.in.SerializeSize())
|
||||
err := test.in.Serialize(&buf)
|
||||
if err != nil {
|
||||
t.Errorf("Serialize #%d error %v", i, err)
|
||||
@ -652,6 +655,7 @@ func TestTxSerializeWitnessSigning(t *testing.T) {
|
||||
for i, test := range tests {
|
||||
// Serialize the transaction.
|
||||
var buf bytes.Buffer
|
||||
buf.Grow(test.in.SerializeSize())
|
||||
err := test.in.Serialize(&buf)
|
||||
if err != nil {
|
||||
t.Errorf("Serialize #%d error %v", i, err)
|
||||
@ -742,6 +746,7 @@ func TestTxSerializeWitnessValueSigning(t *testing.T) {
|
||||
for i, test := range tests {
|
||||
// Serialize the transaction.
|
||||
var buf bytes.Buffer
|
||||
buf.Grow(test.in.SerializeSize())
|
||||
err := test.in.Serialize(&buf)
|
||||
if err != nil {
|
||||
t.Errorf("Serialize #%d error %v", i, err)
|
||||
|
||||
@ -136,9 +136,10 @@ func TestNetAddressWire(t *testing.T) {
|
||||
}
|
||||
|
||||
t.Logf("Running %d tests", len(tests))
|
||||
var buf bytes.Buffer
|
||||
for i, test := range tests {
|
||||
buf.Reset()
|
||||
// Encode to wire format.
|
||||
var buf bytes.Buffer
|
||||
err := writeNetAddress(&buf, test.pver, &test.in, test.ts)
|
||||
if err != nil {
|
||||
t.Errorf("writeNetAddress #%d error %v", i, err)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user