Skip to content

Commit 85424d7

Browse files
committed
Merge #12603: [docs] PeerLogicValidation interface
b7cd08b Add documentation to PeerLogicValidation interface and related functions (James O'Beirne) Pull request description: Adds docs for PeerLogicValidation's public interface and two related functions. Tree-SHA512: b4c2f47e9baa9396d2b6faf3792e46b371c50cd91b9ac890f263f4d14eb24a71e7b40ceb4cbb41e254f5008eff357f417b842618e7ebece9039802ab2a5dd728
2 parents a36834f + b7cd08b commit 85424d7

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

src/net_processing.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,12 @@ void UpdateBlockAvailability(NodeId nodeid, const uint256 &hash) {
401401
}
402402
}
403403

404+
/**
405+
* When a peer sends us a valid block, instruct it to announce blocks to us
406+
* using CMPCTBLOCK if possible by adding its nodeid to the end of
407+
* lNodesAnnouncingHeaderAndIDs, and keeping that list under a certain size by
408+
* removing the first element if necessary.
409+
*/
404410
void MaybeSetPeerAsAnnouncingHeaderAndIDs(NodeId nodeid, CConnman* connman) {
405411
AssertLockHeld(cs_main);
406412
CNodeState* nodestate = State(nodeid);
@@ -749,7 +755,11 @@ unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans)
749755
return nEvicted;
750756
}
751757

752-
// Requires cs_main.
758+
/**
759+
* Mark a misbehaving peer to be banned depending upon the value of `-banscore`.
760+
*
761+
* Requires cs_main.
762+
*/
753763
void Misbehaving(NodeId pnode, int howmuch, const std::string& message)
754764
{
755765
if (howmuch == 0)
@@ -808,6 +818,10 @@ PeerLogicValidation::PeerLogicValidation(CConnman* connmanIn, CScheduler &schedu
808818
scheduler.scheduleEvery(std::bind(&PeerLogicValidation::CheckForStaleTipAndEvictPeers, this, consensusParams), EXTRA_PEER_CHECK_INTERVAL * 1000);
809819
}
810820

821+
/**
822+
* Evict orphan txn pool entries (EraseOrphanTx) based on a newly connected
823+
* block. Also save the time of the last tip update.
824+
*/
811825
void PeerLogicValidation::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex, const std::vector<CTransactionRef>& vtxConflicted) {
812826
LOCK(g_cs_orphans);
813827

@@ -828,7 +842,7 @@ void PeerLogicValidation::BlockConnected(const std::shared_ptr<const CBlock>& pb
828842
}
829843
}
830844

831-
// Erase orphan transactions include or precluded by this block
845+
// Erase orphan transactions included or precluded by this block
832846
if (vOrphanErase.size()) {
833847
int nErased = 0;
834848
for (uint256 &orphanHash : vOrphanErase) {
@@ -847,6 +861,10 @@ static std::shared_ptr<const CBlockHeaderAndShortTxIDs> most_recent_compact_bloc
847861
static uint256 most_recent_block_hash;
848862
static bool fWitnessesPresentInMostRecentCompactBlock;
849863

864+
/**
865+
* Maintain state about the best-seen block and fast-announce a compact block
866+
* to compatible peers.
867+
*/
850868
void PeerLogicValidation::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& pblock) {
851869
std::shared_ptr<const CBlockHeaderAndShortTxIDs> pcmpctblock = std::make_shared<const CBlockHeaderAndShortTxIDs> (*pblock, true);
852870
const CNetMsgMaker msgMaker(PROTOCOL_VERSION);
@@ -888,6 +906,10 @@ void PeerLogicValidation::NewPoWValidBlock(const CBlockIndex *pindex, const std:
888906
});
889907
}
890908

909+
/**
910+
* Update our best height and announce any block hashes which weren't previously
911+
* in chainActive to our peers.
912+
*/
891913
void PeerLogicValidation::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {
892914
const int nNewHeight = pindexNew->nHeight;
893915
connman->SetBestHeight(nNewHeight);
@@ -920,6 +942,10 @@ void PeerLogicValidation::UpdatedBlockTip(const CBlockIndex *pindexNew, const CB
920942
nTimeBestReceived = GetTime();
921943
}
922944

945+
/**
946+
* Handle invalid block rejection and consequent peer banning, maintain which
947+
* peers announce compact blocks.
948+
*/
923949
void PeerLogicValidation::BlockChecked(const CBlock& block, const CValidationState& state) {
924950
LOCK(cs_main);
925951

src/net_processing.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,26 @@ class PeerLogicValidation : public CValidationInterface, public NetEventsInterfa
4242
public:
4343
explicit PeerLogicValidation(CConnman* connman, CScheduler &scheduler);
4444

45+
/**
46+
* Overridden from CValidationInterface.
47+
*/
4548
void BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexConnected, const std::vector<CTransactionRef>& vtxConflicted) override;
49+
/**
50+
* Overridden from CValidationInterface.
51+
*/
4652
void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override;
53+
/**
54+
* Overridden from CValidationInterface.
55+
*/
4756
void BlockChecked(const CBlock& block, const CValidationState& state) override;
57+
/**
58+
* Overridden from CValidationInterface.
59+
*/
4860
void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& pblock) override;
4961

50-
62+
/** Initialize a peer by adding it to mapNodeState and pushing a message requesting its version */
5163
void InitializeNode(CNode* pnode) override;
64+
/** Handle removal of a peer by updating various state and removing it from mapNodeState */
5265
void FinalizeNode(NodeId nodeid, bool& fUpdateConnectionTime) override;
5366
/** Process protocol messages received from a given node */
5467
bool ProcessMessages(CNode* pfrom, std::atomic<bool>& interrupt) override;
@@ -61,8 +74,11 @@ class PeerLogicValidation : public CValidationInterface, public NetEventsInterfa
6174
*/
6275
bool SendMessages(CNode* pto, std::atomic<bool>& interrupt) override;
6376

77+
/** Consider evicting an outbound peer based on the amount of time they've been behind our tip */
6478
void ConsiderEviction(CNode *pto, int64_t time_in_seconds);
79+
/** Evict extra outbound peers. If we think our tip may be stale, connect to an extra outbound */
6580
void CheckForStaleTipAndEvictPeers(const Consensus::Params &consensusParams);
81+
/** If we have extra outbound peers, try to disconnect the one with the oldest block announcement */
6682
void EvictExtraOutboundPeers(int64_t time_in_seconds);
6783

6884
private:

0 commit comments

Comments
 (0)