diff --git a/src/net_processing.cpp b/src/net_processing.cpp --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -4909,7 +4909,7 @@ { LOCK(cs_main); - CNodeState &state = *State(pto->GetId()); + CNodeState *state = State(pto->GetId()); // Address refresh broadcast int64_t nNow = GetTimeMicros(); @@ -4974,18 +4974,18 @@ // Download if this is a nice peer, or we have no nice peers and this // one might do. - bool fFetch = state.fPreferredDownload || + bool fFetch = state->fPreferredDownload || (nPreferredDownload == 0 && !pto->fClient && !pto->IsAddrFetchConn()); - if (!state.fSyncStarted && !pto->fClient && !fImporting && !fReindex) { + if (!state->fSyncStarted && !pto->fClient && !fImporting && !fReindex) { // Only actively request headers from a single peer, unless we're // close to today. if ((nSyncStarted == 0 && fFetch) || pindexBestHeader->GetBlockTime() > GetAdjustedTime() - 24 * 60 * 60) { - state.fSyncStarted = true; - state.nHeadersSyncTimeout = + state->fSyncStarted = true; + state->nHeadersSyncTimeout = GetTimeMicros() + HEADERS_DOWNLOAD_TIMEOUT_BASE + HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER * (GetAdjustedTime() - pindexBestHeader->GetBlockTime()) / @@ -5028,8 +5028,8 @@ LOCK(pto->cs_inventory); std::vector vHeaders; bool fRevertToInv = - ((!state.fPreferHeaders && - (!state.fPreferHeaderAndIDs || + ((!state->fPreferHeaders && + (!state->fPreferHeaderAndIDs || pto->vBlockHashesToAnnounce.size() > 1)) || pto->vBlockHashesToAnnounce.size() > MAX_BLOCKS_TO_ANNOUNCE); // last header queued for delivery @@ -5068,11 +5068,11 @@ if (fFoundStartingHeader) { // add this to the headers message vHeaders.push_back(pindex->GetBlockHeader()); - } else if (PeerHasHeader(&state, pindex)) { + } else if (PeerHasHeader(state, pindex)) { // Keep looking for the first new block. continue; } else if (pindex->pprev == nullptr || - PeerHasHeader(&state, pindex->pprev)) { + PeerHasHeader(state, pindex->pprev)) { // Peer doesn't have this header but they do have the // prior one. Start sending headers. fFoundStartingHeader = true; @@ -5086,7 +5086,7 @@ } } if (!fRevertToInv && !vHeaders.empty()) { - if (vHeaders.size() == 1 && state.fPreferHeaderAndIDs) { + if (vHeaders.size() == 1 && state->fPreferHeaderAndIDs) { // We only send up to 1 block as header-and-ids, as // otherwise probably means we're doing an initial-ish-sync // or they're slow. @@ -5122,8 +5122,8 @@ msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, cmpctblock)); } - state.pindexBestHeaderSent = pBestIndex; - } else if (state.fPreferHeaders) { + state->pindexBestHeaderSent = pBestIndex; + } else if (state->fPreferHeaders) { if (vHeaders.size() > 1) { LogPrint(BCLog::NET, "%s: %u headers, range (%s, %s), to peer=%d\n", @@ -5139,7 +5139,7 @@ } m_connman.PushMessage( pto, msgMaker.Make(NetMsgType::HEADERS, vHeaders)); - state.pindexBestHeaderSent = pBestIndex; + state->pindexBestHeaderSent = pBestIndex; } else { fRevertToInv = true; } @@ -5167,7 +5167,7 @@ } // If the peer's chain has this block, don't inv it back. - if (!PeerHasHeader(&state, pindex)) { + if (!PeerHasHeader(state, pindex)) { pto->vInventoryBlockToSend.push_back(hashToAnnounce); LogPrint(BCLog::NET, "%s: sending inv peer=%d hash=%s\n", __func__, @@ -5363,8 +5363,8 @@ // nNow is the current system time (GetTimeMicros is not mockable) and // should be replaced by the mockable current_time eventually nNow = GetTimeMicros(); - if (state.nStallingSince && - state.nStallingSince < nNow - 1000000 * BLOCK_STALLING_TIMEOUT) { + if (state->nStallingSince && + state->nStallingSince < nNow - 1000000 * BLOCK_STALLING_TIMEOUT) { // Stalling only triggers when the block download window cannot // move. During normal steady state, the download window should be // much larger than the to-be-downloaded set of blocks, so @@ -5381,12 +5381,12 @@ // own downstream link being saturated. We only count validated // in-flight blocks so peers can't advertise non-existing block hashes // to unreasonably increase our timeout. - if (state.vBlocksInFlight.size() > 0) { - QueuedBlock &queuedBlock = state.vBlocksInFlight.front(); + if (state->vBlocksInFlight.size() > 0) { + QueuedBlock &queuedBlock = state->vBlocksInFlight.front(); int nOtherPeersWithValidatedDownloads = nPeersWithValidatedDownloads - - (state.nBlocksInFlightValidHeaders > 0); - if (nNow > state.nDownloadingSince + + (state->nBlocksInFlightValidHeaders > 0); + if (nNow > state->nDownloadingSince + consensusParams.nPowTargetSpacing * (BLOCK_DOWNLOAD_TIMEOUT_BASE + BLOCK_DOWNLOAD_TIMEOUT_PER_PEER * @@ -5400,13 +5400,13 @@ } // Check for headers sync timeouts - if (state.fSyncStarted && - state.nHeadersSyncTimeout < std::numeric_limits::max()) { + if (state->fSyncStarted && + state->nHeadersSyncTimeout < std::numeric_limits::max()) { // Detect whether this is a stalling initial-headers-sync peer if (pindexBestHeader->GetBlockTime() <= GetAdjustedTime() - 24 * 60 * 60) { - if (nNow > state.nHeadersSyncTimeout && nSyncStarted == 1 && - (nPreferredDownload - state.fPreferredDownload >= 1)) { + if (nNow > state->nHeadersSyncTimeout && nSyncStarted == 1 && + (nPreferredDownload - state->fPreferredDownload >= 1)) { // Disconnect a (non-whitelisted) peer if it is our only // sync peer, and we have others we could be using instead. // Note: If all our peers are inbound, then we won't @@ -5427,15 +5427,16 @@ // to try downloading from a different peer. Note: this // will also result in at least one more getheaders // message to be sent to this peer (eventually). - state.fSyncStarted = false; + state->fSyncStarted = false; nSyncStarted--; - state.nHeadersSyncTimeout = 0; + state->nHeadersSyncTimeout = 0; } } } else { // After we've caught up once, reset the timeout so we can't // trigger disconnect later. - state.nHeadersSyncTimeout = std::numeric_limits::max(); + state->nHeadersSyncTimeout = + std::numeric_limits::max(); } } @@ -5450,12 +5451,12 @@ if (!pto->fClient && ((fFetch && !pto->m_limited_node) || !::ChainstateActive().IsInitialBlockDownload()) && - state.nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) { + state->nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) { std::vector vToDownload; NodeId staller = -1; FindNextBlocksToDownload(pto->GetId(), MAX_BLOCKS_IN_TRANSIT_PER_PEER - - state.nBlocksInFlight, + state->nBlocksInFlight, vToDownload, staller, consensusParams); for (const CBlockIndex *pindex : vToDownload) { vGetData.push_back(CInv(MSG_BLOCK, pindex->GetBlockHash())); @@ -5466,7 +5467,7 @@ pindex->GetBlockHash().ToString(), pindex->nHeight, pto->GetId()); } - if (state.nBlocksInFlight == 0 && staller != -1) { + if (state->nBlocksInFlight == 0 && staller != -1) { if (State(staller)->nStallingSince == 0) { State(staller)->nStallingSince = nNow; LogPrint(BCLog::NET, "Stall started peer=%d\n", staller); @@ -5482,30 +5483,30 @@ // can resume downloading transactions from a peer even if they were // unresponsive in the past. Eventually we should consider disconnecting // peers, but this is conservative. - if (state.m_tx_download.m_check_expiry_timer <= current_time) { - for (auto it = state.m_tx_download.m_tx_in_flight.begin(); - it != state.m_tx_download.m_tx_in_flight.end();) { + if (state->m_tx_download.m_check_expiry_timer <= current_time) { + for (auto it = state->m_tx_download.m_tx_in_flight.begin(); + it != state->m_tx_download.m_tx_in_flight.end();) { if (it->second <= current_time - TX_EXPIRY_INTERVAL) { LogPrint(BCLog::NET, "timeout of inflight tx %s from peer=%d\n", it->first.ToString(), pto->GetId()); - state.m_tx_download.m_tx_announced.erase(it->first); - state.m_tx_download.m_tx_in_flight.erase(it++); + state->m_tx_download.m_tx_announced.erase(it->first); + state->m_tx_download.m_tx_in_flight.erase(it++); } else { ++it; } } // On average, we do this check every TX_EXPIRY_INTERVAL. Randomize // so that we're not doing this for all peers at the same time. - state.m_tx_download.m_check_expiry_timer = + state->m_tx_download.m_check_expiry_timer = current_time + TX_EXPIRY_INTERVAL / 2 + GetRandMicros(TX_EXPIRY_INTERVAL); } - auto &tx_process_time = state.m_tx_download.m_tx_process_time; + auto &tx_process_time = state->m_tx_download.m_tx_process_time; while (!tx_process_time.empty() && tx_process_time.begin()->first <= current_time && - state.m_tx_download.m_tx_in_flight.size() < + state->m_tx_download.m_tx_in_flight.size() < MAX_PEER_TX_IN_FLIGHT) { const TxId txid = tx_process_time.begin()->second; // Erase this entry from tx_process_time (it may be added back for @@ -5526,21 +5527,21 @@ vGetData.clear(); } UpdateTxRequestTime(txid, current_time); - state.m_tx_download.m_tx_in_flight.emplace(txid, - current_time); + state->m_tx_download.m_tx_in_flight.emplace(txid, + current_time); } else { // This transaction is in flight from someone else; queue // up processing to happen after the download times out // (with a slight delay for inbound peers, to prefer // requests to outbound peers). const auto next_process_time = CalculateTxGetDataTime( - txid, current_time, !state.fPreferredDownload); + txid, current_time, !state->fPreferredDownload); tx_process_time.emplace(next_process_time, txid); } } else { // We have already seen this transaction, no need to download. - state.m_tx_download.m_tx_announced.erase(txid); - state.m_tx_download.m_tx_in_flight.erase(txid); + state->m_tx_download.m_tx_announced.erase(txid); + state->m_tx_download.m_tx_in_flight.erase(txid); } }