Skip to content

Commit

Permalink
rpc: Add PoWHash to getblock/getblockheader (verbose) results.
Browse files Browse the repository at this point in the history
  • Loading branch information
chappjc authored and davecgh committed Jul 19, 2023
1 parent 1f3cb22 commit 060f951
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
24 changes: 23 additions & 1 deletion internal/rpcserver/rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ import (
// API version constants
const (
jsonrpcSemverMajor = 8
jsonrpcSemverMinor = 0
jsonrpcSemverMinor = 1
jsonrpcSemverPatch = 0
)

Expand Down Expand Up @@ -1969,8 +1969,19 @@ func handleGetBlock(_ context.Context, s *Server, cmd interface{}) (interface{},
return nil, rpcInternalError(err.Error(), "Unable to retrieve median block time")
}

isBlake3PowActive, err := s.isBlake3PowAgendaActive(&blockHeader.PrevBlock)
if err != nil {
return nil, err
}
powHashFn := blockHeader.PowHashV1
if isBlake3PowActive {
powHashFn = blockHeader.PowHashV2
}
powHash := powHashFn()

blockReply := types.GetBlockVerboseResult{
Hash: c.Hash,
PoWHash: powHash.String(),
Version: blockHeader.Version,
MerkleRoot: blockHeader.MerkleRoot.String(),
StakeRoot: blockHeader.StakeRoot.String(),
Expand Down Expand Up @@ -2249,8 +2260,19 @@ func handleGetBlockHeader(_ context.Context, s *Server, cmd interface{}) (interf
return nil, rpcInternalError(err.Error(), "Unable to retrieve median block time")
}

isBlake3PowActive, err := s.isBlake3PowAgendaActive(&blockHeader.PrevBlock)
if err != nil {
return nil, err
}
powHashFn := blockHeader.PowHashV1
if isBlake3PowActive {
powHashFn = blockHeader.PowHashV2
}
powHash := powHashFn()

blockHeaderReply := types.GetBlockHeaderVerboseResult{
Hash: c.Hash,
PowHash: powHash.String(),
Confirmations: confirmations,
Version: blockHeader.Version,
MerkleRoot: blockHeader.MerkleRoot.String(),
Expand Down
6 changes: 6 additions & 0 deletions internal/rpcserver/rpcserverhandlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3763,6 +3763,8 @@ func TestHandleGetBlock(t *testing.T) {
blk := dcrutil.NewBlock(&block432100)
blkHash := blk.Hash()
blkHashString := blkHash.String()
powHash := blkHeader.PowHashV1() // pre-DCP0011 activation
powHashString := powHash.String()
blkBytes, err := blk.Bytes()
if err != nil {
t.Fatalf("error serializing block: %+v", err)
Expand Down Expand Up @@ -3827,6 +3829,7 @@ func TestHandleGetBlock(t *testing.T) {
}(),
result: types.GetBlockVerboseResult{
Hash: blkHashString,
PoWHash: powHashString,
Version: blkHeader.Version,
MerkleRoot: blkHeader.MerkleRoot.String(),
StakeRoot: blkHeader.StakeRoot.String(),
Expand Down Expand Up @@ -3882,6 +3885,7 @@ func TestHandleGetBlock(t *testing.T) {
}(),
result: types.GetBlockVerboseResult{
Hash: blkHashString,
PoWHash: powHashString,
Version: blkHeader.Version,
MerkleRoot: blkHeader.MerkleRoot.String(),
StakeRoot: blkHeader.StakeRoot.String(),
Expand Down Expand Up @@ -4032,6 +4036,7 @@ func TestHandleGetBlockHeader(t *testing.T) {
t.Fatalf("error serializing block header: %+v", err)
}
blkHeaderHexString := hex.EncodeToString(blkHeaderBytes)
powHashString := blkHeader.PowHashV1().String() // pre-DCP0011 block
blk := dcrutil.NewBlock(&block432100)
blkHash := blk.Hash()
blkHashString := blkHash.String()
Expand Down Expand Up @@ -4065,6 +4070,7 @@ func TestHandleGetBlockHeader(t *testing.T) {
}(),
result: types.GetBlockHeaderVerboseResult{
Hash: blkHashString,
PowHash: powHashString,
Confirmations: confirmations,
Version: blkHeader.Version,
MerkleRoot: blkHeader.MerkleRoot.String(),
Expand Down
4 changes: 3 additions & 1 deletion internal/rpcserver/rpcserverhelp.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2015 The btcsuite developers
// Copyright (c) 2015-2022 The Decred developers
// Copyright (c) 2015-2023 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -276,6 +276,7 @@ var helpDescsEnUS = map[string]string{

// GetBlockVerboseResult help.
"getblockverboseresult-hash": "The hash of the block (same as provided)",
"getblockverboseresult-powhash": "The Proof-of-Work hash of the block (same as hash prior to DCP0011 activation)",
"getblockverboseresult-confirmations": "The number of confirmations",
"getblockverboseresult-size": "The size of the block",
"getblockverboseresult-height": "The height of the block in the block chain",
Expand Down Expand Up @@ -323,6 +324,7 @@ var helpDescsEnUS = map[string]string{

// GetBlockHeaderVerboseResult help.
"getblockheaderverboseresult-hash": "The hash of the block (same as provided)",
"getblockheaderverboseresult-powhash": "The Proof-of-Work hash of the block (same as hash prior to DCP0011 activation)",
"getblockheaderverboseresult-confirmations": "The number of confirmations",
"getblockheaderverboseresult-height": "The height of the block in the block chain",
"getblockheaderverboseresult-version": "The block version",
Expand Down

0 comments on commit 060f951

Please sign in to comment.