From 1e06f6db6e1d6185a61692288467aec05e211329 Mon Sep 17 00:00:00 2001 From: Jonathan Chappelow Date: Tue, 18 Jul 2023 17:31:53 -0500 Subject: [PATCH] [release-v1.8] rpc: Add PoWHash to getblock/getblockheader (verbose) results. --- internal/rpcserver/rpcserver.go | 24 +++++++++++++++++++- internal/rpcserver/rpcserverhandlers_test.go | 6 +++++ internal/rpcserver/rpcserverhelp.go | 4 +++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/internal/rpcserver/rpcserver.go b/internal/rpcserver/rpcserver.go index 8de7bb7167..f44d4ff2ba 100644 --- a/internal/rpcserver/rpcserver.go +++ b/internal/rpcserver/rpcserver.go @@ -61,7 +61,7 @@ import ( // API version constants const ( jsonrpcSemverMajor = 8 - jsonrpcSemverMinor = 0 + jsonrpcSemverMinor = 1 jsonrpcSemverPatch = 0 ) @@ -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(), @@ -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(), diff --git a/internal/rpcserver/rpcserverhandlers_test.go b/internal/rpcserver/rpcserverhandlers_test.go index 7e7ac83068..1fc559c549 100644 --- a/internal/rpcserver/rpcserverhandlers_test.go +++ b/internal/rpcserver/rpcserverhandlers_test.go @@ -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) @@ -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(), @@ -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(), @@ -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() @@ -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(), diff --git a/internal/rpcserver/rpcserverhelp.go b/internal/rpcserver/rpcserverhelp.go index 158b68eee0..807219f689 100644 --- a/internal/rpcserver/rpcserverhelp.go +++ b/internal/rpcserver/rpcserverhelp.go @@ -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. @@ -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", @@ -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",