diff --git a/src/avalanche/processor.h b/src/avalanche/processor.h --- a/src/avalanche/processor.h +++ b/src/avalanche/processor.h @@ -29,6 +29,9 @@ class Config; class CBlockIndex; class CScheduler; +class PeerManager; + +using NodePeerManager = PeerManager; /** * Is avalanche enabled by default. @@ -191,6 +194,7 @@ class Processor { CConnman *connman; + NodePeerManager *nodePeerManager; std::chrono::milliseconds queryTimeoutDuration; /** @@ -252,7 +256,8 @@ std::unique_ptr chainNotificationsHandler; public: - Processor(interfaces::Chain &chain, CConnman *connmanIn); + Processor(interfaces::Chain &chain, CConnman *connmanIn, + NodePeerManager *nodePeerManagerIn); ~Processor(); void setQueryTimeoutDuration(std::chrono::milliseconds d) { diff --git a/src/avalanche/processor.cpp b/src/avalanche/processor.cpp --- a/src/avalanche/processor.cpp +++ b/src/avalanche/processor.cpp @@ -8,7 +8,7 @@ #include #include #include // For DecodeSecret -#include // For Misbehaving +#include // For ::PeerManager #include #include #include @@ -153,9 +153,11 @@ } }; -Processor::Processor(interfaces::Chain &chain, CConnman *connmanIn) - : connman(connmanIn), queryTimeoutDuration(AVALANCHE_DEFAULT_QUERY_TIMEOUT), - round(0), peerManager(std::make_unique()) { +Processor::Processor(interfaces::Chain &chain, CConnman *connmanIn, + NodePeerManager *nodePeerManagerIn) + : connman(connmanIn), nodePeerManager(nodePeerManagerIn), + queryTimeoutDuration(AVALANCHE_DEFAULT_QUERY_TIMEOUT), round(0), + peerManager(std::make_unique()) { if (gArgs.IsArgSet("-avasessionkey")) { sessionKey = DecodeSecret(gArgs.GetArg("-avasessionkey", "")); } else { @@ -299,7 +301,7 @@ auto w = queries.getWriteView(); auto it = w->find(std::make_tuple(nodeid, response.getRound())); if (it == w.end()) { - Misbehaving(nodeid, 2, "unexpcted-ava-response"); + nodePeerManager->Misbehaving(nodeid, 2, "unexpcted-ava-response"); return false; } @@ -311,13 +313,14 @@ const std::vector &votes = response.GetVotes(); size_t size = invs.size(); if (votes.size() != size) { - Misbehaving(nodeid, 100, "invalid-ava-response-size"); + nodePeerManager->Misbehaving(nodeid, 100, "invalid-ava-response-size"); return false; } for (size_t i = 0; i < size; i++) { if (invs[i].hash != votes[i].GetHash()) { - Misbehaving(nodeid, 100, "invalid-ava-response-content"); + nodePeerManager->Misbehaving(nodeid, 100, + "invalid-ava-response-content"); return false; } } diff --git a/src/avalanche/test/processor_tests.cpp b/src/avalanche/test/processor_tests.cpp --- a/src/avalanche/test/processor_tests.cpp +++ b/src/avalanche/test/processor_tests.cpp @@ -86,8 +86,8 @@ m_node.chain = interfaces::MakeChain(m_node, config.GetChainParams()); // Get the processor ready. - m_processor = - std::make_unique(*m_node.chain, m_node.connman.get()); + m_processor = std::make_unique( + *m_node.chain, m_node.connman.get(), m_node.peerman.get()); // The master private key we delegate to. masterpriv.MakeNewKey(true); diff --git a/src/init.cpp b/src/init.cpp --- a/src/init.cpp +++ b/src/init.cpp @@ -2429,8 +2429,8 @@ } // Step 6.5 (I guess ?): Initialize Avalanche. - g_avalanche = - std::make_unique(*node.chain, node.connman.get()); + g_avalanche = std::make_unique( + *node.chain, node.connman.get(), node.peerman.get()); // Step 7: load block chain diff --git a/src/net_processing.h b/src/net_processing.h --- a/src/net_processing.h +++ b/src/net_processing.h @@ -120,6 +120,15 @@ int64_t nTimeReceived, const std::atomic &interruptMsgProc); + /** + * Increment peer's misbehavior score. If the new value >= + * DISCOURAGEMENT_THRESHOLD, mark the node to be discouraged, meaning the + * peer might be disconnected and added to the discouragement filter. Public + * for unit testing. + */ + void Misbehaving(const NodeId pnode, const int howmuch, + const std::string &message); + /** * Retrieve unbroadcast transactions from the mempool and reattempt * sending to peers @@ -127,6 +136,12 @@ void ReattemptInitialBroadcast(CScheduler &scheduler) const; private: + // overloaded variant of above to operate on CNode*s + void Misbehaving(const CNode &node, int howmuch, + const std::string &message) { + Misbehaving(node.GetId(), howmuch, message); + } + /** * Potentially mark a node discouraged based on the contents of a * BlockValidationState object @@ -196,13 +211,6 @@ /** Get statistics from node state */ bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats); -/** - * Increment peer's misbehavior score. If the new value >= - * DISCOURAGEMENT_THRESHOLD, mark the node to be discouraged, meaning the peer - * might be disconnected and added to the discouragement filter. - */ -void Misbehaving(const NodeId nodeid, const int howmuch, - const std::string &message = ""); /** Relay transaction to every node */ void RelayTransaction(const TxId &txid, const CConnman &connman); diff --git a/src/net_processing.cpp b/src/net_processing.cpp --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -1314,13 +1314,8 @@ return nEvicted; } -/** - * Increment peer's misbehavior score. If the new value >= - * DISCOURAGEMENT_THRESHOLD, mark the node to be discouraged, meaning the peer - * might be disconnected and added to the discouragement filter. - */ -void Misbehaving(const NodeId pnode, const int howmuch, - const std::string &message) { +void PeerManager::Misbehaving(const NodeId pnode, const int howmuch, + const std::string &message) { assert(howmuch > 0); PeerRef peer = GetPeerRef(pnode); @@ -1347,12 +1342,6 @@ } } -// overloaded variant of above to operate on CNode*s -static void Misbehaving(const CNode &node, int howmuch, - const std::string &message) { - Misbehaving(node.GetId(), howmuch, message); -} - /** * Returns true if the given validation state result may result in a peer * banning/disconnecting us. We use this to determine which unaccepted diff --git a/src/test/denialofservice_tests.cpp b/src/test/denialofservice_tests.cpp --- a/src/test/denialofservice_tests.cpp +++ b/src/test/denialofservice_tests.cpp @@ -255,7 +255,8 @@ dummyNode1.nVersion = 1; dummyNode1.fSuccessfullyConnected = true; // Should be discouraged - Misbehaving(dummyNode1.GetId(), DISCOURAGEMENT_THRESHOLD); + peerLogic->Misbehaving(dummyNode1.GetId(), DISCOURAGEMENT_THRESHOLD, + /* message */ ""); { LOCK2(cs_main, dummyNode1.cs_sendProcessing); BOOST_CHECK( @@ -272,7 +273,8 @@ peerLogic->InitializeNode(config, &dummyNode2); dummyNode2.nVersion = 1; dummyNode2.fSuccessfullyConnected = true; - Misbehaving(dummyNode2.GetId(), DISCOURAGEMENT_THRESHOLD - 1); + peerLogic->Misbehaving(dummyNode2.GetId(), DISCOURAGEMENT_THRESHOLD - 1, + /* message */ ""); { LOCK2(cs_main, dummyNode2.cs_sendProcessing); BOOST_CHECK( @@ -283,7 +285,7 @@ // ... but 1 still should be BOOST_CHECK(banman->IsDiscouraged(addr1)); // 2 reaches discouragement threshold - Misbehaving(dummyNode2.GetId(), 1); + peerLogic->Misbehaving(dummyNode2.GetId(), 1, /* message */ ""); { LOCK2(cs_main, dummyNode2.cs_sendProcessing); BOOST_CHECK( @@ -322,7 +324,8 @@ dummyNode.nVersion = 1; dummyNode.fSuccessfullyConnected = true; - Misbehaving(dummyNode.GetId(), DISCOURAGEMENT_THRESHOLD); + peerLogic->Misbehaving(dummyNode.GetId(), DISCOURAGEMENT_THRESHOLD, + /* message */ ""); { LOCK2(cs_main, dummyNode.cs_sendProcessing); BOOST_CHECK(