@@ -301,17 +301,10 @@ void CChainLocksHandler::TrySignChainTip()
301301 break ;
302302 }
303303
304- decltype (blockTxs.begin ()->second ) txids;
305- {
306- LOCK (cs);
307- auto it = blockTxs.find (pindexWalk->GetBlockHash ());
308- if (it == blockTxs.end ()) {
309- // this should actually not happen as NewPoWValidBlock should have been called before
310- LogPrint (" chainlocks" , " CChainLocksHandler::%s -- blockTxs for %s not found\n " , __func__,
311- pindexWalk->GetBlockHash ().ToString ());
312- return ;
313- }
314- txids = it->second ;
304+ auto txids = GetBlockTxs (pindexWalk->GetBlockHash ());
305+ if (!txids) {
306+ pindexWalk = pindexWalk->pprev ;
307+ continue ;
315308 }
316309
317310 for (auto & txid : *txids) {
@@ -382,6 +375,55 @@ void CChainLocksHandler::SyncTransaction(const CTransaction& tx, const CBlockInd
382375 }
383376}
384377
378+ CChainLocksHandler::BlockTxs::mapped_type CChainLocksHandler::GetBlockTxs (const uint256& blockHash)
379+ {
380+ AssertLockNotHeld (cs);
381+ AssertLockNotHeld (cs_main);
382+
383+ CChainLocksHandler::BlockTxs::mapped_type ret;
384+
385+ {
386+ LOCK (cs);
387+ auto it = blockTxs.find (blockHash);
388+ if (it != blockTxs.end ()) {
389+ ret = it->second ;
390+ }
391+ }
392+ if (!ret) {
393+ // This should only happen when freshly started.
394+ // If running for some time, SyncTransaction should have been called before which fills blockTxs.
395+ LogPrint (" chainlocks" , " CChainLocksHandler::%s -- blockTxs for %s not found. Trying ReadBlockFromDisk\n " , __func__,
396+ blockHash.ToString ());
397+
398+ uint32_t blockTime;
399+ {
400+ LOCK (cs_main);
401+ auto pindex = mapBlockIndex.at (blockHash);
402+ CBlock block;
403+ if (!ReadBlockFromDisk (block, pindex, Params ().GetConsensus ())) {
404+ return nullptr ;
405+ }
406+
407+ ret = std::make_shared<std::unordered_set<uint256, StaticSaltedHasher>>();
408+ for (auto & tx : block.vtx ) {
409+ if (tx->IsCoinBase () || tx->vin .empty ()) {
410+ continue ;
411+ }
412+ ret->emplace (tx->GetHash ());
413+ }
414+
415+ blockTime = block.nTime ;
416+ }
417+
418+ LOCK (cs);
419+ blockTxs.emplace (blockHash, ret);
420+ for (auto & txid : *ret) {
421+ txFirstSeenTime.emplace (txid, blockTime);
422+ }
423+ }
424+ return ret;
425+ }
426+
385427bool CChainLocksHandler::IsTxSafeForMining (const uint256& txid)
386428{
387429 if (!sporkManager.IsSporkActive (SPORK_3_INSTANTSEND_BLOCK_FILTERING)) {
0 commit comments