Changeset View
Changeset View
Standalone View
Standalone View
src/net_processing.cpp
Show First 20 Lines • Show All 986 Lines • ▼ Show 20 Lines | return pindex->IsValid(BlockValidity::SCRIPTS) && | ||||
(pindexBestHeader != nullptr) && | (pindexBestHeader != nullptr) && | ||||
(pindexBestHeader->GetBlockTime() - pindex->GetBlockTime() < | (pindexBestHeader->GetBlockTime() - pindex->GetBlockTime() < | ||||
STALE_RELAY_AGE_LIMIT) && | STALE_RELAY_AGE_LIMIT) && | ||||
(GetBlockProofEquivalentTime(*pindexBestHeader, *pindex, | (GetBlockProofEquivalentTime(*pindexBestHeader, *pindex, | ||||
*pindexBestHeader, consensusParams) < | *pindexBestHeader, consensusParams) < | ||||
STALE_RELAY_AGE_LIMIT); | STALE_RELAY_AGE_LIMIT); | ||||
} | } | ||||
PeerLogicValidation::PeerLogicValidation(CConnman *connmanIn, | PeerLogicValidation::PeerLogicValidation(CConnman *connmanIn, BanMan *banman, | ||||
CScheduler &scheduler, | CScheduler &scheduler, | ||||
bool enable_bip61) | bool enable_bip61) | ||||
: connman(connmanIn), m_stale_tip_check_time(0), | : connman(connmanIn), m_banman(banman), m_stale_tip_check_time(0), | ||||
m_enable_bip61(enable_bip61) { | m_enable_bip61(enable_bip61) { | ||||
// Initialize global variables that cannot be constructed at startup. | // Initialize global variables that cannot be constructed at startup. | ||||
recentRejects.reset(new CRollingBloomFilter(120000, 0.000001)); | recentRejects.reset(new CRollingBloomFilter(120000, 0.000001)); | ||||
const Consensus::Params &consensusParams = Params().GetConsensus(); | const Consensus::Params &consensusParams = Params().GetConsensus(); | ||||
// Stale tip checking and peer eviction are on two different timers, but we | // Stale tip checking and peer eviction are on two different timers, but we | ||||
// don't want them to get out of sync due to drift in the scheduler, so we | // don't want them to get out of sync due to drift in the scheduler, so we | ||||
// combine them in one function and schedule at the quicker (peer-eviction) | // combine them in one function and schedule at the quicker (peer-eviction) | ||||
// timer. | // timer. | ||||
▲ Show 20 Lines • Show All 2,366 Lines • ▼ Show 20 Lines | else { | ||||
// Ignore unknown commands for extensibility | // Ignore unknown commands for extensibility | ||||
LogPrint(BCLog::NET, "Unknown command \"%s\" from peer=%d\n", | LogPrint(BCLog::NET, "Unknown command \"%s\" from peer=%d\n", | ||||
SanitizeString(strCommand), pfrom->GetId()); | SanitizeString(strCommand), pfrom->GetId()); | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
static bool SendRejectsAndCheckIfBanned(CNode *pnode, CConnman *connman, | bool PeerLogicValidation::SendRejectsAndCheckIfBanned(CNode *pnode, | ||||
bool enable_bip61) | bool enable_bip61) | ||||
EXCLUSIVE_LOCKS_REQUIRED(cs_main) { | EXCLUSIVE_LOCKS_REQUIRED(cs_main) { | ||||
AssertLockHeld(cs_main); | AssertLockHeld(cs_main); | ||||
CNodeState &state = *State(pnode->GetId()); | CNodeState &state = *State(pnode->GetId()); | ||||
if (enable_bip61) { | if (enable_bip61) { | ||||
for (const CBlockReject &reject : state.rejects) { | for (const CBlockReject &reject : state.rejects) { | ||||
connman->PushMessage( | connman->PushMessage( | ||||
pnode, | pnode, | ||||
Show All 15 Lines | if (state.fShouldBan) { | ||||
pnode->addr.ToString()); | pnode->addr.ToString()); | ||||
} else if (pnode->addr.IsLocal()) { | } else if (pnode->addr.IsLocal()) { | ||||
// Disconnect but don't ban _this_ local node | // Disconnect but don't ban _this_ local node | ||||
LogPrintf("Warning: disconnecting but not banning local peer %s!\n", | LogPrintf("Warning: disconnecting but not banning local peer %s!\n", | ||||
pnode->addr.ToString()); | pnode->addr.ToString()); | ||||
pnode->fDisconnect = true; | pnode->fDisconnect = true; | ||||
} else { | } else { | ||||
// Disconnect and ban all nodes sharing the address | // Disconnect and ban all nodes sharing the address | ||||
connman->Ban(pnode->addr, BanReasonNodeMisbehaving); | if (m_banman) { | ||||
m_banman->Ban(pnode->addr, BanReasonNodeMisbehaving); | |||||
} | |||||
connman->DisconnectNode(pnode->addr); | connman->DisconnectNode(pnode->addr); | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
return false; | return false; | ||||
} | } | ||||
bool PeerLogicValidation::ProcessMessages(const Config &config, CNode *pfrom, | bool PeerLogicValidation::ProcessMessages(const Config &config, CNode *pfrom, | ||||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | bool PeerLogicValidation::ProcessMessages(const Config &config, CNode *pfrom, | ||||
if (memcmp(std::begin(msg.hdr.pchMessageStart), | if (memcmp(std::begin(msg.hdr.pchMessageStart), | ||||
std::begin(chainparams.NetMagic()), | std::begin(chainparams.NetMagic()), | ||||
CMessageHeader::MESSAGE_START_SIZE) != 0) { | CMessageHeader::MESSAGE_START_SIZE) != 0) { | ||||
LogPrint(BCLog::NET, | LogPrint(BCLog::NET, | ||||
"PROCESSMESSAGE: INVALID MESSAGESTART %s peer=%d\n", | "PROCESSMESSAGE: INVALID MESSAGESTART %s peer=%d\n", | ||||
SanitizeString(msg.hdr.GetCommand()), pfrom->GetId()); | SanitizeString(msg.hdr.GetCommand()), pfrom->GetId()); | ||||
// Make sure we ban where that come from for some time. | // Make sure we ban where that come from for some time. | ||||
connman->Ban(pfrom->addr, BanReasonNodeMisbehaving); | if (m_banman) { | ||||
m_banman->Ban(pfrom->addr, BanReasonNodeMisbehaving); | |||||
} | |||||
connman->DisconnectNode(pfrom->addr); | connman->DisconnectNode(pfrom->addr); | ||||
pfrom->fDisconnect = true; | pfrom->fDisconnect = true; | ||||
return false; | return false; | ||||
} | } | ||||
// Read header | // Read header | ||||
CMessageHeader &hdr = msg.hdr; | CMessageHeader &hdr = msg.hdr; | ||||
Show All 16 Lines | if (memcmp(hash.begin(), hdr.pchChecksum, CMessageHeader::CHECKSUM_SIZE) != | ||||
BCLog::NET, | BCLog::NET, | ||||
"%s(%s, %u bytes): CHECKSUM ERROR expected %s was %s from " | "%s(%s, %u bytes): CHECKSUM ERROR expected %s was %s from " | ||||
"peer=%d\n", | "peer=%d\n", | ||||
__func__, SanitizeString(strCommand), nMessageSize, | __func__, SanitizeString(strCommand), nMessageSize, | ||||
HexStr(hash.begin(), hash.begin() + CMessageHeader::CHECKSUM_SIZE), | HexStr(hash.begin(), hash.begin() + CMessageHeader::CHECKSUM_SIZE), | ||||
HexStr(hdr.pchChecksum, | HexStr(hdr.pchChecksum, | ||||
hdr.pchChecksum + CMessageHeader::CHECKSUM_SIZE), | hdr.pchChecksum + CMessageHeader::CHECKSUM_SIZE), | ||||
pfrom->GetId()); | pfrom->GetId()); | ||||
connman->Ban(pfrom->addr, BanReasonNodeMisbehaving); | if (m_banman) { | ||||
m_banman->Ban(pfrom->addr, BanReasonNodeMisbehaving); | |||||
} | |||||
connman->DisconnectNode(pfrom->addr); | connman->DisconnectNode(pfrom->addr); | ||||
return fMoreWork; | return fMoreWork; | ||||
} | } | ||||
// Process message | // Process message | ||||
bool fRet = false; | bool fRet = false; | ||||
try { | try { | ||||
fRet = ProcessMessage(config, pfrom, strCommand, vRecv, msg.nTime, | fRet = ProcessMessage(config, pfrom, strCommand, vRecv, msg.nTime, | ||||
Show All 40 Lines | bool PeerLogicValidation::ProcessMessages(const Config &config, CNode *pfrom, | ||||
} | } | ||||
if (!fRet) { | if (!fRet) { | ||||
LogPrint(BCLog::NET, "%s(%s, %u bytes) FAILED peer=%d\n", __func__, | LogPrint(BCLog::NET, "%s(%s, %u bytes) FAILED peer=%d\n", __func__, | ||||
SanitizeString(strCommand), nMessageSize, pfrom->GetId()); | SanitizeString(strCommand), nMessageSize, pfrom->GetId()); | ||||
} | } | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
SendRejectsAndCheckIfBanned(pfrom, connman, m_enable_bip61); | SendRejectsAndCheckIfBanned(pfrom, m_enable_bip61); | ||||
return fMoreWork; | return fMoreWork; | ||||
} | } | ||||
void PeerLogicValidation::ConsiderEviction(CNode *pto, | void PeerLogicValidation::ConsiderEviction(CNode *pto, | ||||
int64_t time_in_seconds) { | int64_t time_in_seconds) { | ||||
AssertLockHeld(cs_main); | AssertLockHeld(cs_main); | ||||
▲ Show 20 Lines • Show All 248 Lines • ▼ Show 20 Lines | bool PeerLogicValidation::SendMessages(const Config &config, CNode *pto, | ||||
} | } | ||||
// Acquire cs_main for IsInitialBlockDownload() and CNodeState() | // Acquire cs_main for IsInitialBlockDownload() and CNodeState() | ||||
TRY_LOCK(cs_main, lockMain); | TRY_LOCK(cs_main, lockMain); | ||||
if (!lockMain) { | if (!lockMain) { | ||||
return true; | return true; | ||||
} | } | ||||
if (SendRejectsAndCheckIfBanned(pto, connman, m_enable_bip61)) { | if (SendRejectsAndCheckIfBanned(pto, m_enable_bip61)) { | ||||
return true; | return true; | ||||
} | } | ||||
CNodeState &state = *State(pto->GetId()); | CNodeState &state = *State(pto->GetId()); | ||||
// Address refresh broadcast | // Address refresh broadcast | ||||
int64_t nNow = GetTimeMicros(); | int64_t nNow = GetTimeMicros(); | ||||
if (!IsInitialBlockDownload() && pto->nNextLocalAddrSend < nNow) { | if (!IsInitialBlockDownload() && pto->nNextLocalAddrSend < nNow) { | ||||
AdvertiseLocal(pto); | AdvertiseLocal(pto); | ||||
▲ Show 20 Lines • Show All 595 Lines • Show Last 20 Lines |