Changeset View
Changeset View
Standalone View
Standalone View
src/net_processing.cpp
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
Lint: Code style violation: '/home/jason/projects/bitcoin-abc/src/net_processing.cpp' has code style errors. | |||||
// Copyright (c) 2009-2016 The Bitcoin Core developers | // Copyright (c) 2009-2016 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include "net_processing.h" | #include "net_processing.h" | ||||
#include "addrman.h" | #include "addrman.h" | ||||
#include "arith_uint256.h" | #include "arith_uint256.h" | ||||
#include "blockencodings.h" | #include "blockencodings.h" | ||||
#include "chain.h" | |||||
#include "chainparams.h" | #include "chainparams.h" | ||||
#include "config.h" | #include "config.h" | ||||
#include "consensus/validation.h" | #include "consensus/validation.h" | ||||
#include "hash.h" | #include "hash.h" | ||||
#include "init.h" | #include "init.h" | ||||
#include "merkleblock.h" | #include "merkleblock.h" | ||||
#include "net.h" | #include "net.h" | ||||
#include "netbase.h" | #include "netbase.h" | ||||
▲ Show 20 Lines • Show All 893 Lines • ▼ Show 20 Lines | void PeerLogicValidation::NewPoWValidBlock( | ||||
const CBlockIndex *pindex, const std::shared_ptr<const CBlock> &pblock) { | const CBlockIndex *pindex, const std::shared_ptr<const CBlock> &pblock) { | ||||
std::shared_ptr<const CBlockHeaderAndShortTxIDs> pcmpctblock = | std::shared_ptr<const CBlockHeaderAndShortTxIDs> pcmpctblock = | ||||
std::make_shared<const CBlockHeaderAndShortTxIDs>(*pblock); | std::make_shared<const CBlockHeaderAndShortTxIDs>(*pblock); | ||||
const CNetMsgMaker msgMaker(PROTOCOL_VERSION); | const CNetMsgMaker msgMaker(PROTOCOL_VERSION); | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
static int nHighestFastAnnounce = 0; | static int nHighestFastAnnounce = 0; | ||||
if (pindex->nHeight <= nHighestFastAnnounce) { | // pindex->nHeight == nHighestFastAnnounce is expected when a more honest-looking | ||||
// block replaces a selfishly-mined block. | |||||
if (pindex->nHeight < nHighestFastAnnounce) { | |||||
return; | return; | ||||
} | } | ||||
nHighestFastAnnounce = pindex->nHeight; | nHighestFastAnnounce = pindex->nHeight; | ||||
uint256 hashBlock(pblock->GetHash()); | uint256 hashBlock(pblock->GetHash()); | ||||
{ | { | ||||
LOCK(cs_most_recent_block); | LOCK(cs_most_recent_block); | ||||
▲ Show 20 Lines • Show All 1,321 Lines • ▼ Show 20 Lines | else if (strCommand == NetMsgType::TX) { | ||||
} | } | ||||
} | } | ||||
// Ignore blocks received while importing | // Ignore blocks received while importing | ||||
else if (strCommand == NetMsgType::CMPCTBLOCK && !fImporting && !fReindex) { | else if (strCommand == NetMsgType::CMPCTBLOCK && !fImporting && !fReindex) { | ||||
CBlockHeaderAndShortTxIDs cmpctblock; | CBlockHeaderAndShortTxIDs cmpctblock; | ||||
vRecv >> cmpctblock; | vRecv >> cmpctblock; | ||||
int64_t receiveTime = GetTime(); | |||||
cmpctblock.header.nTimeReceived = receiveTime; | |||||
{ | { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
if (mapBlockIndex.find(cmpctblock.header.hashPrevBlock) == | if (mapBlockIndex.find(cmpctblock.header.hashPrevBlock) == | ||||
mapBlockIndex.end()) { | mapBlockIndex.end()) { | ||||
// Doesn't connect (or is genesis), instead of DoSing in | // Doesn't connect (or is genesis), instead of DoSing in | ||||
// AcceptBlockHeader, request deeper headers | // AcceptBlockHeader, request deeper headers | ||||
if (!IsInitialBlockDownload()) { | if (!IsInitialBlockDownload()) { | ||||
▲ Show 20 Lines • Show All 345 Lines • ▼ Show 20 Lines | else if (strCommand == NetMsgType::HEADERS && !fImporting && !fReindex) { | ||||
ReadCompactSize(vRecv); | ReadCompactSize(vRecv); | ||||
} | } | ||||
if (nCount == 0) { | if (nCount == 0) { | ||||
// Nothing interesting. Stop asking this peers for more headers. | // Nothing interesting. Stop asking this peers for more headers. | ||||
return true; | return true; | ||||
} | } | ||||
int64_t receiveTime = GetTime(); | |||||
for (CBlockHeader &header : headers) { | |||||
header.nTimeReceived = receiveTime; | |||||
} | |||||
const CBlockIndex *pindexLast = nullptr; | const CBlockIndex *pindexLast = nullptr; | ||||
{ | { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
CNodeState *nodestate = State(pfrom->GetId()); | CNodeState *nodestate = State(pfrom->GetId()); | ||||
// If this looks like it could be a block announcement (nCount < | // If this looks like it could be a block announcement (nCount < | ||||
// MAX_BLOCKS_TO_ANNOUNCE), use special logic for handling headers | // MAX_BLOCKS_TO_ANNOUNCE), use special logic for handling headers | ||||
// that | // that | ||||
▲ Show 20 Lines • Show All 87 Lines • ▼ Show 20 Lines | else if (strCommand == NetMsgType::HEADERS && !fImporting && !fReindex) { | ||||
msgMaker.Make(NetMsgType::GETHEADERS, | msgMaker.Make(NetMsgType::GETHEADERS, | ||||
chainActive.GetLocator(pindexLast), | chainActive.GetLocator(pindexLast), | ||||
uint256())); | uint256())); | ||||
} | } | ||||
bool fCanDirectFetch = CanDirectFetch(chainparams.GetConsensus()); | bool fCanDirectFetch = CanDirectFetch(chainparams.GetConsensus()); | ||||
// If this set of headers is valid and ends in a block with at least | // If this set of headers is valid and ends in a block with at least | ||||
// as much work as our tip, download as much as possible. | // as much work as our tip, download as much as possible. | ||||
if (fCanDirectFetch && pindexLast->IsValid(BLOCK_VALID_TREE) && | if (fCanDirectFetch && | ||||
pindexLast->IsValid(BLOCK_VALID_TREE) && | |||||
chainActive.Tip()->nChainWork <= pindexLast->nChainWork) { | chainActive.Tip()->nChainWork <= pindexLast->nChainWork) { | ||||
std::vector<const CBlockIndex *> vToFetch; | std::vector<const CBlockIndex *> vToFetch; | ||||
const CBlockIndex *pindexWalk = pindexLast; | const CBlockIndex *pindexWalk = pindexLast; | ||||
// Calculate all the blocks we'd need to switch to pindexLast, | // Calculate all the blocks we'd need to switch to pindexLast, | ||||
// up to a limit. | // up to a limit. | ||||
while (pindexWalk && !chainActive.Contains(pindexWalk) && | while (pindexWalk && !chainActive.Contains(pindexWalk) && | ||||
vToFetch.size() <= MAX_BLOCKS_IN_TRANSIT_PER_PEER) { | vToFetch.size() <= MAX_BLOCKS_IN_TRANSIT_PER_PEER) { | ||||
if (!(pindexWalk->nStatus & BLOCK_HAVE_DATA) && | if (!(pindexWalk->nStatus & BLOCK_HAVE_DATA) && | ||||
!mapBlocksInFlight.count(pindexWalk->GetBlockHash())) { | !mapBlocksInFlight.count(pindexWalk->GetBlockHash())) { | ||||
▲ Show 20 Lines • Show All 59 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
else if (strCommand == NetMsgType::BLOCK && !fImporting && | else if (strCommand == NetMsgType::BLOCK && !fImporting && | ||||
!fReindex) // Ignore blocks received while importing | !fReindex) // Ignore blocks received while importing | ||||
{ | { | ||||
std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>(); | std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>(); | ||||
vRecv >> *pblock; | vRecv >> *pblock; | ||||
int64_t receiveTime = GetTime(); | |||||
pblock->nTimeReceived = receiveTime; | |||||
LogPrint(BCLog::NET, "received block %s peer=%d\n", | LogPrint(BCLog::NET, "received block %s peer=%d\n", | ||||
pblock->GetHash().ToString(), pfrom->id); | pblock->GetHash().ToString(), pfrom->id); | ||||
// Process all blocks from whitelisted peers, even if not requested, | // Process all blocks from whitelisted peers, even if not requested, | ||||
// unless we're still syncing with the network. Such an unrequested | // unless we're still syncing with the network. Such an unrequested | ||||
// block may still be processed, subject to the conditions in | // block may still be processed, subject to the conditions in | ||||
// AcceptBlock(). | // AcceptBlock(). | ||||
bool forceProcessing = pfrom->fWhitelisted && !IsInitialBlockDownload(); | bool forceProcessing = pfrom->fWhitelisted && !IsInitialBlockDownload(); | ||||
▲ Show 20 Lines • Show All 1,026 Lines • Show Last 20 Lines |
'/home/jason/projects/bitcoin-abc/src/net_processing.cpp' has code style errors.