lbcd/blockheader.go
Dave Collins 9ee6a8aeb6 Optimize writeElement.
This commit modifies the writeElement function to have a "fast path" which uses type
assertions for all of the types which btcwire write so the more expensive
reflection-based binary.Write can be avoided.

Also, this changes all cases that were writing raw ShaHash (32-byte) arrays (which
requires a stack copy) instead simply passing the pointer.

The following benchmark results show the results for serializing a block header
after these changes:

Before: BenchmarkWriteBlockHeader         500000              5566 ns/op
After:  BenchmarkWriteBlockHeader        1000000               991 ns/op

This is part of the ongoing effort to optimize serialization as noted in
conformal/btcd#27.
2013-11-07 00:56:20 -06:00

122 lines
3.5 KiB
Go

// Copyright (c) 2013 Conformal Systems LLC.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcwire
import (
"bytes"
"io"
"time"
)
// BlockVersion is the current latest supported block version.
const BlockVersion uint32 = 2
// Version 4 bytes + Timestamp 4 bytes + Bits 4 bytes + Nonce 4 bytes +
// TxnCount (varInt) + PrevBlock and MerkleRoot hashes.
const maxBlockHeaderPayload = 16 + maxVarIntPayload + (HashSize * 2)
// BlockHeader defines information about a block and is used in the bitcoin
// block (MsgBlock) and headers (MsgHeaders) messages.
type BlockHeader struct {
// Version of the block. This is not the same as the protocol version.
Version uint32
// Hash of the previous block in the block chain.
PrevBlock ShaHash
// Merkle tree reference to hash of all transactions for the block.
MerkleRoot ShaHash
// Time the block was created. This is, unfortunately, encoded as a
// uint32 on the wire and therefore is limited to 2106.
Timestamp time.Time
// Difficulty target for the block.
Bits uint32
// Nonce used to generate the block.
Nonce uint32
// Number of transactions in the block. For the bitcoin headers
// (MsgHeaders) message, this must be 0. This is encoded as a variable
// length integer on the wire.
TxnCount uint64
}
// blockHashLen is a constant that represents how much of the block header is
// used when computing the block sha 0:blockHashLen
const blockHashLen = 80
// BlockSha computes the block identifier hash for the given block header.
func (h *BlockHeader) BlockSha() (ShaHash, error) {
// Encode the header and run double sha256 everything prior to the
// number of transactions. Ignore the error returns since there is no
// way the encode could fail except being out of memory which would
// 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
var sha ShaHash
_ = writeBlockHeader(&buf, 0, h)
_ = sha.SetBytes(DoubleSha256(buf.Bytes()[0:blockHashLen]))
// Even though this function can't currently fail, it still returns
// a potential error to help future proof the API should a failure
// become possible.
return sha, nil
}
// NewBlockHeader returns a new BlockHeader using the provided previous block
// hash, merkle root hash, difficulty bits, and nonce used to generate the
// block with defaults for the remaining fields.
func NewBlockHeader(prevHash *ShaHash, merkleRootHash *ShaHash, bits uint32,
nonce uint32) *BlockHeader {
return &BlockHeader{
Version: BlockVersion,
PrevBlock: *prevHash,
MerkleRoot: *merkleRootHash,
Timestamp: time.Now(),
Bits: bits,
Nonce: nonce,
TxnCount: 0,
}
}
// readBlockHeader reads a bitcoin block header from r.
func readBlockHeader(r io.Reader, pver uint32, bh *BlockHeader) error {
var sec uint32
err := readElements(r, &bh.Version, &bh.PrevBlock, &bh.MerkleRoot, &sec,
&bh.Bits, &bh.Nonce)
if err != nil {
return err
}
bh.Timestamp = time.Unix(int64(sec), 0)
count, err := readVarInt(r, pver)
if err != nil {
return err
}
bh.TxnCount = count
return nil
}
// writeBlockHeader writes a bitcoin block header to w.
func writeBlockHeader(w io.Writer, pver uint32, bh *BlockHeader) error {
sec := uint32(bh.Timestamp.Unix())
err := writeElements(w, bh.Version, &bh.PrevBlock, &bh.MerkleRoot,
sec, bh.Bits, bh.Nonce)
if err != nil {
return err
}
err = writeVarInt(w, pver, bh.TxnCount)
if err != nil {
return err
}
return nil
}