mirror of
https://github.com/Retropex/bitcoin.git
synced 2025-05-13 03:30:42 +02:00
rpc: getblocktemplate getTipHash() via Miner interface
This commit is contained in:
parent
d8a3496b5a
commit
404b01c436
@ -5,6 +5,8 @@
|
|||||||
#ifndef BITCOIN_INTERFACES_MINING_H
|
#ifndef BITCOIN_INTERFACES_MINING_H
|
||||||
#define BITCOIN_INTERFACES_MINING_H
|
#define BITCOIN_INTERFACES_MINING_H
|
||||||
|
|
||||||
|
#include <uint256.h>
|
||||||
|
|
||||||
namespace node {
|
namespace node {
|
||||||
struct NodeContext;
|
struct NodeContext;
|
||||||
} // namespace node
|
} // namespace node
|
||||||
@ -25,6 +27,9 @@ public:
|
|||||||
//! If this chain is exclusively used for testing
|
//! If this chain is exclusively used for testing
|
||||||
virtual bool isTestChain() = 0;
|
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.
|
* Check a block is completely valid from start to finish.
|
||||||
* Only works on top of our current best block.
|
* Only works on top of our current best block.
|
||||||
|
@ -845,6 +845,14 @@ public:
|
|||||||
return chainman().GetParams().IsTestChain();
|
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
|
bool testBlockValidity(BlockValidationState& state, const CBlock& block, bool check_merkle_root) override
|
||||||
{
|
{
|
||||||
LOCK(::cs_main);
|
LOCK(::cs_main);
|
||||||
|
@ -672,7 +672,6 @@ static RPCHelpMan getblocktemplate()
|
|||||||
UniValue lpval = NullUniValue;
|
UniValue lpval = NullUniValue;
|
||||||
std::set<std::string> setClientRules;
|
std::set<std::string> setClientRules;
|
||||||
Chainstate& active_chainstate = chainman.ActiveChainstate();
|
Chainstate& active_chainstate = chainman.ActiveChainstate();
|
||||||
CChain& active_chain = active_chainstate.m_chain;
|
|
||||||
if (!request.params[0].isNull())
|
if (!request.params[0].isNull())
|
||||||
{
|
{
|
||||||
const UniValue& oparam = request.params[0].get_obj();
|
const UniValue& oparam = request.params[0].get_obj();
|
||||||
@ -707,9 +706,8 @@ static RPCHelpMan getblocktemplate()
|
|||||||
return "duplicate-inconclusive";
|
return "duplicate-inconclusive";
|
||||||
}
|
}
|
||||||
|
|
||||||
CBlockIndex* const pindexPrev = active_chain.Tip();
|
|
||||||
// testBlockValidity only supports blocks built on the current 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";
|
return "inconclusive-not-best-prevblk";
|
||||||
}
|
}
|
||||||
BlockValidationState state;
|
BlockValidationState state;
|
||||||
@ -761,7 +759,7 @@ static RPCHelpMan getblocktemplate()
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// NOTE: Spec does not specify behaviour for non-string longpollid, but this makes testing easier
|
// 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;
|
nTransactionsUpdatedLastLP = nTransactionsUpdatedLast;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -806,7 +804,7 @@ static RPCHelpMan getblocktemplate()
|
|||||||
static CBlockIndex* pindexPrev;
|
static CBlockIndex* pindexPrev;
|
||||||
static int64_t time_start;
|
static int64_t time_start;
|
||||||
static std::unique_ptr<CBlockTemplate> pblocktemplate;
|
static std::unique_ptr<CBlockTemplate> pblocktemplate;
|
||||||
if (pindexPrev != active_chain.Tip() ||
|
if (!pindexPrev || pindexPrev->GetBlockHash() != miner.getTipHash() ||
|
||||||
(mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - time_start > 5))
|
(mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - time_start > 5))
|
||||||
{
|
{
|
||||||
// Clear pindexPrev so future calls make a new block, despite any failures from here on
|
// Clear pindexPrev so future calls make a new block, despite any failures from here on
|
||||||
@ -814,7 +812,7 @@ static RPCHelpMan getblocktemplate()
|
|||||||
|
|
||||||
// Store the pindexBest used before CreateNewBlock, to avoid races
|
// Store the pindexBest used before CreateNewBlock, to avoid races
|
||||||
nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
|
nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
|
||||||
CBlockIndex* pindexPrevNew = active_chain.Tip();
|
CBlockIndex* pindexPrevNew = chainman.m_blockman.LookupBlockIndex(miner.getTipHash());
|
||||||
time_start = GetTime();
|
time_start = GetTime();
|
||||||
|
|
||||||
// Create new block
|
// Create new block
|
||||||
@ -946,7 +944,7 @@ static RPCHelpMan getblocktemplate()
|
|||||||
result.pushKV("transactions", std::move(transactions));
|
result.pushKV("transactions", std::move(transactions));
|
||||||
result.pushKV("coinbaseaux", std::move(aux));
|
result.pushKV("coinbaseaux", std::move(aux));
|
||||||
result.pushKV("coinbasevalue", (int64_t)pblock->vtx[0]->vout[0].nValue);
|
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("target", hashTarget.GetHex());
|
||||||
result.pushKV("mintime", (int64_t)pindexPrev->GetMedianTimePast()+1);
|
result.pushKV("mintime", (int64_t)pindexPrev->GetMedianTimePast()+1);
|
||||||
result.pushKV("mutable", std::move(aMutable));
|
result.pushKV("mutable", std::move(aMutable));
|
||||||
|
Loading…
Reference in New Issue
Block a user