Skip to content

Commit

Permalink
rpc: getblocktemplate getTipHash() via Miner interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Sjors committed Jun 18, 2024
1 parent d8a3496 commit 404b01c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/interfaces/mining.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#ifndef BITCOIN_INTERFACES_MINING_H
#define BITCOIN_INTERFACES_MINING_H

#include <uint256.h>

namespace node {
struct NodeContext;
} // namespace node
Expand All @@ -25,6 +27,9 @@ class Mining
//! If this chain is exclusively used for testing
virtual bool isTestChain() = 0;

//! Returns the hash for the tip of this chain, 0 if none
virtual uint256 getTipHash() = 0;

/**
* Check a block is completely valid from start to finish.
* Only works on top of our current best block.
Expand Down
8 changes: 8 additions & 0 deletions src/node/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,14 @@ class MinerImpl : public Mining
return chainman().GetParams().IsTestChain();
}

uint256 getTipHash() override
{
LOCK(::cs_main);
CBlockIndex* tip{chainman().ActiveChain().Tip()};
if (!tip) return uint256{0};
return tip->GetBlockHash();
}

bool testBlockValidity(BlockValidationState& state, const CBlock& block, bool check_merkle_root) override
{
LOCK(::cs_main);
Expand Down
12 changes: 5 additions & 7 deletions src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,6 @@ static RPCHelpMan getblocktemplate()
UniValue lpval = NullUniValue;
std::set<std::string> setClientRules;
Chainstate& active_chainstate = chainman.ActiveChainstate();
CChain& active_chain = active_chainstate.m_chain;
if (!request.params[0].isNull())
{
const UniValue& oparam = request.params[0].get_obj();
Expand Down Expand Up @@ -707,9 +706,8 @@ static RPCHelpMan getblocktemplate()
return "duplicate-inconclusive";
}

CBlockIndex* const pindexPrev = active_chain.Tip();
// testBlockValidity only supports blocks built on the current Tip
if (block.hashPrevBlock != pindexPrev->GetBlockHash()) {
if (block.hashPrevBlock != miner.getTipHash()) {
return "inconclusive-not-best-prevblk";
}
BlockValidationState state;
Expand Down Expand Up @@ -761,7 +759,7 @@ static RPCHelpMan getblocktemplate()
else
{
// NOTE: Spec does not specify behaviour for non-string longpollid, but this makes testing easier
hashWatchedChain = active_chain.Tip()->GetBlockHash();
hashWatchedChain = miner.getTipHash();
nTransactionsUpdatedLastLP = nTransactionsUpdatedLast;
}

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

// Store the pindexBest used before CreateNewBlock, to avoid races
nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
CBlockIndex* pindexPrevNew = active_chain.Tip();
CBlockIndex* pindexPrevNew = chainman.m_blockman.LookupBlockIndex(miner.getTipHash());
time_start = GetTime();

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

0 comments on commit 404b01c

Please sign in to comment.