Skip to content

Commit

Permalink
Merge pull request #2073 from Roasbeef/wire-opts
Browse files Browse the repository at this point in the history
wire: only borrow/return binaryFreeList buffers at the message level
  • Loading branch information
Roasbeef authored Dec 29, 2023
2 parents 790c570 + b0e9636 commit 16684f6
Show file tree
Hide file tree
Showing 26 changed files with 1,278 additions and 359 deletions.
484 changes: 469 additions & 15 deletions wire/bench_test.go

Large diffs are not rendered by default.

103 changes: 98 additions & 5 deletions wire/blockheader.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,109 @@ func NewBlockHeader(version int32, prevHash, merkleRootHash *chainhash.Hash,
// readBlockHeader reads a bitcoin block header from r. See Deserialize for
// decoding block headers stored to disk, such as in a database, as opposed to
// decoding from the wire.
//
// DEPRECATED: Use readBlockHeaderBuf instead.
func readBlockHeader(r io.Reader, pver uint32, bh *BlockHeader) error {
return readElements(r, &bh.Version, &bh.PrevBlock, &bh.MerkleRoot,
(*uint32Time)(&bh.Timestamp), &bh.Bits, &bh.Nonce)
buf := binarySerializer.Borrow()
err := readBlockHeaderBuf(r, pver, bh, buf)
binarySerializer.Return(buf)
return err
}

// readBlockHeaderBuf reads a bitcoin block header from r. See Deserialize for
// decoding block headers stored to disk, such as in a database, as opposed to
// decoding from the wire.
//
// If b is non-nil, the provided buffer will be used for serializing small
// values. Otherwise a buffer will be drawn from the binarySerializer's pool
// and return when the method finishes.
//
// NOTE: b MUST either be nil or at least an 8-byte slice.
func readBlockHeaderBuf(r io.Reader, pver uint32, bh *BlockHeader,
buf []byte) error {

if _, err := io.ReadFull(r, buf[:4]); err != nil {
return err
}
bh.Version = int32(littleEndian.Uint32(buf[:4]))

if _, err := io.ReadFull(r, bh.PrevBlock[:]); err != nil {
return err
}

if _, err := io.ReadFull(r, bh.MerkleRoot[:]); err != nil {
return err
}

if _, err := io.ReadFull(r, buf[:4]); err != nil {
return err
}
bh.Timestamp = time.Unix(int64(littleEndian.Uint32(buf[:4])), 0)

if _, err := io.ReadFull(r, buf[:4]); err != nil {
return err
}
bh.Bits = littleEndian.Uint32(buf[:4])

if _, err := io.ReadFull(r, buf[:4]); err != nil {
return err
}
bh.Nonce = littleEndian.Uint32(buf[:4])

return nil
}

// writeBlockHeader writes a bitcoin block header to w. See Serialize for
// encoding block headers to be stored to disk, such as in a database, as
// opposed to encoding for the wire.
//
// DEPRECATED: Use writeBlockHeaderBuf instead.
func writeBlockHeader(w io.Writer, pver uint32, bh *BlockHeader) error {
sec := uint32(bh.Timestamp.Unix())
return writeElements(w, bh.Version, &bh.PrevBlock, &bh.MerkleRoot,
sec, bh.Bits, bh.Nonce)
buf := binarySerializer.Borrow()
err := writeBlockHeaderBuf(w, pver, bh, buf)
binarySerializer.Return(buf)
return err
}

// writeBlockHeaderBuf writes a bitcoin block header to w. See Serialize for
// encoding block headers to be stored to disk, such as in a database, as
// opposed to encoding for the wire.
//
// If b is non-nil, the provided buffer will be used for serializing small
// values. Otherwise a buffer will be drawn from the binarySerializer's pool
// and return when the method finishes.
//
// NOTE: b MUST either be nil or at least an 8-byte slice.
func writeBlockHeaderBuf(w io.Writer, pver uint32, bh *BlockHeader,
buf []byte) error {

littleEndian.PutUint32(buf[:4], uint32(bh.Version))
if _, err := w.Write(buf[:4]); err != nil {
return err
}

if _, err := w.Write(bh.PrevBlock[:]); err != nil {
return err
}

if _, err := w.Write(bh.MerkleRoot[:]); err != nil {
return err
}

littleEndian.PutUint32(buf[:4], uint32(bh.Timestamp.Unix()))
if _, err := w.Write(buf[:4]); err != nil {
return err
}

littleEndian.PutUint32(buf[:4], bh.Bits)
if _, err := w.Write(buf[:4]); err != nil {
return err
}

littleEndian.PutUint32(buf[:4], bh.Nonce)
if _, err := w.Write(buf[:4]); err != nil {
return err
}

return nil
}
Loading

0 comments on commit 16684f6

Please sign in to comment.