@@ -296,7 +296,8 @@ void FinalizeNode(NodeId nodeid) {
296296}
297297
298298// Requires cs_main.
299- void MarkBlockAsReceived (const uint256& hash) {
299+ // Returns a bool indicating whether we requested this block.
300+ bool MarkBlockAsReceived (const uint256& hash) {
300301 map<uint256, pair<NodeId, list<QueuedBlock>::iterator> >::iterator itInFlight = mapBlocksInFlight.find (hash);
301302 if (itInFlight != mapBlocksInFlight.end ()) {
302303 CNodeState *state = State (itInFlight->second .first );
@@ -306,7 +307,9 @@ void MarkBlockAsReceived(const uint256& hash) {
306307 state->nBlocksInFlight --;
307308 state->nStallingSince = 0 ;
308309 mapBlocksInFlight.erase (itInFlight);
310+ return true ;
309311 }
312+ return false ;
310313}
311314
312315// Requires cs_main.
@@ -2826,7 +2829,7 @@ bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, CBloc
28262829 return true ;
28272830}
28282831
2829- bool AcceptBlock (CBlock& block, CValidationState& state, CBlockIndex** ppindex, CDiskBlockPos* dbp)
2832+ bool AcceptBlock (CBlock& block, CValidationState& state, CBlockIndex** ppindex, bool fRequested , CDiskBlockPos* dbp)
28302833{
28312834 const CChainParams& chainparams = Params ();
28322835 AssertLockHeld (cs_main);
@@ -2836,13 +2839,18 @@ bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex,
28362839 if (!AcceptBlockHeader (block, state, &pindex))
28372840 return false ;
28382841
2839- // If we're pruning, ensure that we don't allow a peer to dump a copy
2840- // of old blocks. But we might need blocks that are not on the main chain
2841- // to handle a reorg, even if we've processed once.
2842- if (pindex->nStatus & BLOCK_HAVE_DATA || chainActive.Contains (pindex)) {
2843- // TODO: deal better with duplicate blocks.
2844- // return state.DoS(20, error("AcceptBlock(): already have block %d %s", pindex->nHeight, pindex->GetBlockHash().ToString()), REJECT_DUPLICATE, "duplicate");
2845- return true ;
2842+ // Try to process all requested blocks that we don't have, but only
2843+ // process an unrequested block if it's new and has enough work to
2844+ // advance our tip.
2845+ bool fAlreadyHave = pindex->nStatus & BLOCK_HAVE_DATA;
2846+ bool fHasMoreWork = (chainActive.Tip () ? pindex->nChainWork > chainActive.Tip ()->nChainWork : true );
2847+
2848+ // TODO: deal better with return value and error conditions for duplicate
2849+ // and unrequested blocks.
2850+ if (fAlreadyHave ) return true ;
2851+ if (!fRequested ) { // If we didn't ask for it:
2852+ if (pindex->nTx != 0 ) return true ; // This is a previously-processed block that was pruned
2853+ if (!fHasMoreWork ) return true ; // Don't process less-work chains
28462854 }
28472855
28482856 if ((!CheckBlock (block, state)) || !ContextualCheckBlock (block, state, pindex->pprev )) {
@@ -2891,21 +2899,22 @@ static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned
28912899}
28922900
28932901
2894- bool ProcessNewBlock (CValidationState &state, CNode* pfrom, CBlock* pblock, CDiskBlockPos *dbp)
2902+ bool ProcessNewBlock (CValidationState &state, CNode* pfrom, CBlock* pblock, bool fForceProcessing , CDiskBlockPos *dbp)
28952903{
28962904 // Preliminary checks
28972905 bool checked = CheckBlock (*pblock, state);
28982906
28992907 {
29002908 LOCK (cs_main);
2901- MarkBlockAsReceived (pblock->GetHash ());
2909+ bool fRequested = MarkBlockAsReceived (pblock->GetHash ());
2910+ fRequested |= fForceProcessing ;
29022911 if (!checked) {
29032912 return error (" %s: CheckBlock FAILED" , __func__);
29042913 }
29052914
29062915 // Store to disk
29072916 CBlockIndex *pindex = NULL ;
2908- bool ret = AcceptBlock (*pblock, state, &pindex, dbp);
2917+ bool ret = AcceptBlock (*pblock, state, &pindex, fRequested , dbp);
29092918 if (pindex && pfrom) {
29102919 mapBlockSource[pindex->GetBlockHash ()] = pfrom->GetId ();
29112920 }
@@ -3453,7 +3462,7 @@ bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp)
34533462 // process in case the block isn't known yet
34543463 if (mapBlockIndex.count (hash) == 0 || (mapBlockIndex[hash]->nStatus & BLOCK_HAVE_DATA) == 0 ) {
34553464 CValidationState state;
3456- if (ProcessNewBlock (state, NULL , &block, dbp))
3465+ if (ProcessNewBlock (state, NULL , &block, true , dbp))
34573466 nLoaded++;
34583467 if (state.IsError ())
34593468 break ;
@@ -3475,7 +3484,7 @@ bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp)
34753484 LogPrintf (" %s: Processing out of order child %s of %s\n " , __func__, block.GetHash ().ToString (),
34763485 head.ToString ());
34773486 CValidationState dummy;
3478- if (ProcessNewBlock (dummy, NULL , &block, &it->second ))
3487+ if (ProcessNewBlock (dummy, NULL , &block, true , &it->second ))
34793488 {
34803489 nLoaded++;
34813490 queue.push_back (block.GetHash ());
@@ -4462,7 +4471,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
44624471 pfrom->AddInventoryKnown (inv);
44634472
44644473 CValidationState state;
4465- ProcessNewBlock (state, pfrom, &block);
4474+ // Process all blocks from whitelisted peers, even if not requested.
4475+ ProcessNewBlock (state, pfrom, &block, pfrom->fWhitelisted , NULL );
44664476 int nDoS;
44674477 if (state.IsInvalid (nDoS)) {
44684478 pfrom->PushMessage (" reject" , strCommand, state.GetRejectCode (),
0 commit comments