@@ -667,6 +667,9 @@ static RPCHelpMan getblocktemplate()
667
667
ChainstateManager& chainman = EnsureChainman (node);
668
668
Mining& miner = EnsureMining (node);
669
669
LOCK (cs_main);
670
+ std::optional<uint256> maybe_tip{miner.getTipHash ()};
671
+ CHECK_NONFATAL (maybe_tip);
672
+ uint256 tip{maybe_tip.value ()};
670
673
671
674
std::string strMode = " template" ;
672
675
UniValue lpval = NullUniValue;
@@ -706,7 +709,7 @@ static RPCHelpMan getblocktemplate()
706
709
}
707
710
708
711
// testBlockValidity only supports blocks built on the current Tip
709
- if (block.hashPrevBlock != miner. getTipHash () ) {
712
+ if (block.hashPrevBlock != tip ) {
710
713
return " inconclusive-not-best-prevblk" ;
711
714
}
712
715
BlockValidationState state;
@@ -757,7 +760,7 @@ static RPCHelpMan getblocktemplate()
757
760
else
758
761
{
759
762
// NOTE: Spec does not specify behaviour for non-string longpollid, but this makes testing easier
760
- hashWatchedChain = miner. getTipHash () ;
763
+ hashWatchedChain = tip ;
761
764
nTransactionsUpdatedLastLP = nTransactionsUpdatedLast;
762
765
}
763
766
@@ -781,6 +784,10 @@ static RPCHelpMan getblocktemplate()
781
784
}
782
785
ENTER_CRITICAL_SECTION (cs_main);
783
786
787
+ std::optional<uint256> maybe_tip{miner.getTipHash ()};
788
+ CHECK_NONFATAL (maybe_tip);
789
+ tip = maybe_tip.value ();
790
+
784
791
if (!IsRPCRunning ())
785
792
throw JSONRPCError (RPC_CLIENT_NOT_CONNECTED, " Shutting down" );
786
793
// TODO: Maybe recheck connections/IBD and (if something wrong) send an expires-immediately template to stop miners?
@@ -802,15 +809,15 @@ static RPCHelpMan getblocktemplate()
802
809
static CBlockIndex* pindexPrev;
803
810
static int64_t time_start;
804
811
static std::unique_ptr<CBlockTemplate> pblocktemplate;
805
- if (!pindexPrev || pindexPrev->GetBlockHash () != miner. getTipHash () ||
812
+ if (!pindexPrev || pindexPrev->GetBlockHash () != tip ||
806
813
(miner.getTransactionsUpdated () != nTransactionsUpdatedLast && GetTime () - time_start > 5 ))
807
814
{
808
815
// Clear pindexPrev so future calls make a new block, despite any failures from here on
809
816
pindexPrev = nullptr ;
810
817
811
818
// Store the pindexBest used before createNewBlock, to avoid races
812
819
nTransactionsUpdatedLast = miner.getTransactionsUpdated ();
813
- CBlockIndex* pindexPrevNew = chainman.m_blockman .LookupBlockIndex (miner. getTipHash () );
820
+ CBlockIndex* pindexPrevNew = chainman.m_blockman .LookupBlockIndex (tip );
814
821
time_start = GetTime ();
815
822
816
823
// Create new block
@@ -943,7 +950,7 @@ static RPCHelpMan getblocktemplate()
943
950
result.pushKV (" transactions" , std::move (transactions));
944
951
result.pushKV (" coinbaseaux" , std::move (aux));
945
952
result.pushKV (" coinbasevalue" , (int64_t )pblock->vtx [0 ]->vout [0 ].nValue );
946
- result.pushKV (" longpollid" , miner. getTipHash () .GetHex () + ToString (nTransactionsUpdatedLast));
953
+ result.pushKV (" longpollid" , tip .GetHex () + ToString (nTransactionsUpdatedLast));
947
954
result.pushKV (" target" , hashTarget.GetHex ());
948
955
result.pushKV (" mintime" , (int64_t )pindexPrev->GetMedianTimePast ()+1 );
949
956
result.pushKV (" mutable" , std::move (aMutable));
0 commit comments