Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ require (
github.com/decred/dcrd/rpc/jsonrpc/types/v4 v4.4.0
github.com/decred/dcrd/rpcclient/v8 v8.1.0
github.com/decred/dcrd/txscript/v4 v4.1.2
github.com/decred/dcrd/wire v1.7.2
github.com/decred/dcrd/wire v1.7.5
github.com/decred/dcrtest/dcrdtest v1.0.1-0.20240404170936-a2529e936df1
github.com/decred/go-socks v1.1.0
github.com/decred/slog v1.2.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ github.com/decred/dcrd/rpcclient/v8 v8.1.0 h1:FLZ1j4ub7+O9oCIcKf+frYCrZW++G3FSzk
github.com/decred/dcrd/rpcclient/v8 v8.1.0/go.mod h1:iTHqLrHnS2VLJPHQk7guy0BP3jKvMew9STDqWWhFNA4=
github.com/decred/dcrd/txscript/v4 v4.1.2 h1:1EP7ZmBDl2LBeAMTEygxY8rVNN3+lkGqrsb4u64x+II=
github.com/decred/dcrd/txscript/v4 v4.1.2/go.mod h1:r5/8qfCnl6TFrE369gggUayVIryM1oC7BLoRfa27Ckw=
github.com/decred/dcrd/wire v1.7.2 h1:04vpHHE3t78rDztjZx82JV2EEOMDUtUUB1347H32kho=
github.com/decred/dcrd/wire v1.7.2/go.mod h1:eP9XRsMloy+phlntkTAaAm611JgLv8NqY1YJoRxkNKU=
github.com/decred/dcrd/wire v1.7.5 h1:fRaaB5CrwYWGI3YVv50XHm54lsU1TB40WnnIJ4W6aGM=
github.com/decred/dcrd/wire v1.7.5/go.mod h1:NZK8QD5W2ObX6p+Q0TUzYNpQtk4Ov3pBIvc6ZUK88FU=
github.com/decred/dcrtest/dcrdtest v1.0.1-0.20240404170936-a2529e936df1 h1:RbUvO7dsxdNgb2DvP2/h34eS2Ej1T4a7opzvOzz+YFI=
github.com/decred/dcrtest/dcrdtest v1.0.1-0.20240404170936-a2529e936df1/go.mod h1:kbRQzyWu1IfukYZqCioVyJokzu1ifvIXzVDrXReeOsQ=
github.com/decred/go-socks v1.1.0 h1:dnENcc0KIqQo3HSXdgboXAHgqsCIutkqq6ntQjYtm2U=
Expand Down
47 changes: 46 additions & 1 deletion wire/bench_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2013-2016 The btcsuite developers
// Copyright (c) 2015-2025 The Decred developers
// Copyright (c) 2015-2026 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand All @@ -14,6 +14,7 @@ import (
"time"

"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrd/crypto/blake256"
)

// genesisCoinbaseTx is the coinbase transaction for the genesis blocks for
Expand Down Expand Up @@ -634,6 +635,28 @@ func BenchmarkTxHash(b *testing.B) {
}
}

// BenchmarkTxHashReuseHasher performs a benchmark on how long it takes to hash a
// transaction by reusing the *blake256.Hasher256 object.
func BenchmarkTxHashReuseHasher(b *testing.B) {
h := blake256.NewHasher256()

txHash := func(h *blake256.Hasher256, tx *MsgTx) chainhash.Hash {
txCopy := *tx
txCopy.SerType = TxSerializeNoWitness
err := txCopy.Serialize(h)
if err != nil {
panic(err)
}
return h.Sum256()
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
h.Reset()
_ = txHash(h, &genesisCoinbaseTx)
}
}

// BenchmarkHashB performs a benchmark on how long it takes to perform a hash
// returning a byte slice.
func BenchmarkHashB(b *testing.B) {
Expand Down Expand Up @@ -676,3 +699,25 @@ func BenchmarkWriteMessageN(b *testing.B) {
}
}
}

// BenchmarkReadMessageN benchmarks the genesis coinbase deserialization using
// the ReadMessageN function.
func BenchmarkReadMessageN(b *testing.B) {
var buf bytes.Buffer
_, err := WriteMessageN(&buf, &genesisCoinbaseTx, ProtocolVersion, MainNet)
if err != nil {
b.Fatal(err)
}
msgBytes := buf.Bytes()

r := new(bytes.Reader)

b.ResetTimer()
for i := 0; i < b.N; i++ {
r.Reset(msgBytes)
_, _, _, err := ReadMessageN(r, ProtocolVersion, MainNet)
if err != nil {
b.Fatalf("ReadMessageN: unexpected error: %v", err)
}
}
}
30 changes: 16 additions & 14 deletions wire/blockheader.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2013-2016 The btcsuite developers
// Copyright (c) 2015-2023 The Decred developers
// Copyright (c) 2015-2026 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand All @@ -11,6 +11,7 @@ import (
"time"

"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrd/crypto/blake256"
"lukechampine.com/blake3"
)

Expand Down Expand Up @@ -88,36 +89,37 @@ type BlockHeader struct {
// header.
const blockHeaderLen = 180

// BlockHash computes the block identifier hash for the given block header.
// BlockHash computes the BLAKE-256 block identifier hash for the given block
// header.
func (h *BlockHeader) BlockHash() chainhash.Hash {
// Encode the header and hash 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.
buf := bytes.NewBuffer(make([]byte, 0, MaxBlockHeaderPayload))
_ = writeBlockHeader(buf, 0, h)

return chainhash.HashH(buf.Bytes())
hasher := blake256.NewHasher256()
_ = writeBlockHeader(hasher, 0, h)
return hasher.Sum256()
}

// PowHashV1 calculates and returns the version 1 proof of work hash for the
// block header.
// PowHashV1 calculates and returns the version 1 proof of work BLAKE-256 hash
// for the block header.
//
// NOTE: This is the original proof of work hash function used at Decred launch
// and applies to all blocks prior to the activation of DCP0011.
func (h *BlockHeader) PowHashV1() chainhash.Hash {
return h.BlockHash()
}

// PowHashV2 calculates and returns the version 2 proof of work hash as defined
// in DCP0011 for the block header.
// PowHashV2 calculates and returns the version 2 proof of work BLAKE3 hash as
// defined in DCP0011 for the block header.
func (h *BlockHeader) PowHashV2() chainhash.Hash {
// Encode the header and hash 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.
buf := bytes.NewBuffer(make([]byte, 0, MaxBlockHeaderPayload))
_ = writeBlockHeader(buf, 0, h)

return blake3.Sum256(buf.Bytes())
var digest chainhash.Hash
hasher := blake3.New(len(digest), nil)
_ = writeBlockHeader(hasher, 0, h)
hasher.Sum(digest[:0])
return digest
}

// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
Expand Down
Loading
Loading