Skip to content

Commit 404b01c

Browse files
committed
rpc: getblocktemplate getTipHash() via Miner interface
1 parent d8a3496 commit 404b01c

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

src/interfaces/mining.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#ifndef BITCOIN_INTERFACES_MINING_H
66
#define BITCOIN_INTERFACES_MINING_H
77

8+
#include <uint256.h>
9+
810
namespace node {
911
struct NodeContext;
1012
} // namespace node
@@ -25,6 +27,9 @@ class Mining
2527
//! If this chain is exclusively used for testing
2628
virtual bool isTestChain() = 0;
2729

30+
//! Returns the hash for the tip of this chain, 0 if none
31+
virtual uint256 getTipHash() = 0;
32+
2833
/**
2934
* Check a block is completely valid from start to finish.
3035
* Only works on top of our current best block.

src/node/interfaces.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,14 @@ class MinerImpl : public Mining
845845
return chainman().GetParams().IsTestChain();
846846
}
847847

848+
uint256 getTipHash() override
849+
{
850+
LOCK(::cs_main);
851+
CBlockIndex* tip{chainman().ActiveChain().Tip()};
852+
if (!tip) return uint256{0};
853+
return tip->GetBlockHash();
854+
}
855+
848856
bool testBlockValidity(BlockValidationState& state, const CBlock& block, bool check_merkle_root) override
849857
{
850858
LOCK(::cs_main);

src/rpc/mining.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,6 @@ static RPCHelpMan getblocktemplate()
672672
UniValue lpval = NullUniValue;
673673
std::set<std::string> setClientRules;
674674
Chainstate& active_chainstate = chainman.ActiveChainstate();
675-
CChain& active_chain = active_chainstate.m_chain;
676675
if (!request.params[0].isNull())
677676
{
678677
const UniValue& oparam = request.params[0].get_obj();
@@ -707,9 +706,8 @@ static RPCHelpMan getblocktemplate()
707706
return "duplicate-inconclusive";
708707
}
709708

710-
CBlockIndex* const pindexPrev = active_chain.Tip();
711709
// testBlockValidity only supports blocks built on the current Tip
712-
if (block.hashPrevBlock != pindexPrev->GetBlockHash()) {
710+
if (block.hashPrevBlock != miner.getTipHash()) {
713711
return "inconclusive-not-best-prevblk";
714712
}
715713
BlockValidationState state;
@@ -761,7 +759,7 @@ static RPCHelpMan getblocktemplate()
761759
else
762760
{
763761
// NOTE: Spec does not specify behaviour for non-string longpollid, but this makes testing easier
764-
hashWatchedChain = active_chain.Tip()->GetBlockHash();
762+
hashWatchedChain = miner.getTipHash();
765763
nTransactionsUpdatedLastLP = nTransactionsUpdatedLast;
766764
}
767765

@@ -806,15 +804,15 @@ static RPCHelpMan getblocktemplate()
806804
static CBlockIndex* pindexPrev;
807805
static int64_t time_start;
808806
static std::unique_ptr<CBlockTemplate> pblocktemplate;
809-
if (pindexPrev != active_chain.Tip() ||
807+
if (!pindexPrev || pindexPrev->GetBlockHash() != miner.getTipHash() ||
810808
(mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - time_start > 5))
811809
{
812810
// Clear pindexPrev so future calls make a new block, despite any failures from here on
813811
pindexPrev = nullptr;
814812

815813
// Store the pindexBest used before CreateNewBlock, to avoid races
816814
nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
817-
CBlockIndex* pindexPrevNew = active_chain.Tip();
815+
CBlockIndex* pindexPrevNew = chainman.m_blockman.LookupBlockIndex(miner.getTipHash());
818816
time_start = GetTime();
819817

820818
// Create new block
@@ -946,7 +944,7 @@ static RPCHelpMan getblocktemplate()
946944
result.pushKV("transactions", std::move(transactions));
947945
result.pushKV("coinbaseaux", std::move(aux));
948946
result.pushKV("coinbasevalue", (int64_t)pblock->vtx[0]->vout[0].nValue);
949-
result.pushKV("longpollid", active_chain.Tip()->GetBlockHash().GetHex() + ToString(nTransactionsUpdatedLast));
947+
result.pushKV("longpollid", miner.getTipHash().GetHex() + ToString(nTransactionsUpdatedLast));
950948
result.pushKV("target", hashTarget.GetHex());
951949
result.pushKV("mintime", (int64_t)pindexPrev->GetMedianTimePast()+1);
952950
result.pushKV("mutable", std::move(aMutable));

0 commit comments

Comments
 (0)