@@ -223,47 +223,84 @@ BOOST_AUTO_TEST_CASE(peer_discouragement)
223223{
224224 const CChainParams& chainparams = Params ();
225225 auto banman = MakeUnique<BanMan>(GetDataDir () / " banlist.dat" , nullptr , DEFAULT_MISBEHAVING_BANTIME);
226- auto connman = MakeUnique<CConnman >(0x1337 , 0x1337 );
226+ auto connman = MakeUnique<CConnmanTest >(0x1337 , 0x1337 );
227227 auto peerLogic = std::make_unique<PeerManager>(chainparams, *connman, banman.get (), *m_node.scheduler ,
228228 *m_node.chainman , *m_node.mempool , false );
229229
230+ std::vector<CNode*> nodes (3 );
231+
230232 banman->ClearBanned ();
231233 CAddress addr1 (ip (0xa0b0c001 ), NODE_NONE);
232- CNode dummyNode1 (id++, NODE_NETWORK, INVALID_SOCKET, addr1, 0 , 0 , CAddress (), " " , ConnectionType::INBOUND);
233- dummyNode1.SetCommonVersion (PROTOCOL_VERSION);
234- peerLogic->InitializeNode (&dummyNode1);
235- dummyNode1.fSuccessfullyConnected = true ;
236- peerLogic->Misbehaving (dummyNode1.GetId (), DISCOURAGEMENT_THRESHOLD, /* message */ " " ); // Should be discouraged
234+ nodes[0 ] = new CNode (id++, NODE_NETWORK, INVALID_SOCKET, addr1, 0 , 0 , CAddress (), " " , ConnectionType::INBOUND);
235+ nodes[0 ]->SetCommonVersion (PROTOCOL_VERSION);
236+ peerLogic->InitializeNode (nodes[0 ]);
237+ nodes[0 ]->fSuccessfullyConnected = true ;
238+ connman->AddNode (*nodes[0 ]);
239+ peerLogic->Misbehaving (nodes[0 ]->GetId (), DISCOURAGEMENT_THRESHOLD, /* message */ " " ); // Should be discouraged
237240 {
238- LOCK (dummyNode1. cs_sendProcessing );
239- BOOST_CHECK (peerLogic->SendMessages (&dummyNode1 ));
241+ LOCK (nodes[ 0 ]-> cs_sendProcessing );
242+ BOOST_CHECK (peerLogic->SendMessages (nodes[ 0 ] ));
240243 }
241244 BOOST_CHECK (banman->IsDiscouraged (addr1));
245+ BOOST_CHECK (nodes[0 ]->fDisconnect );
242246 BOOST_CHECK (!banman->IsDiscouraged (ip (0xa0b0c001 |0x0000ff00 ))); // Different IP, not discouraged
243247
244248 CAddress addr2 (ip (0xa0b0c002 ), NODE_NONE);
245- CNode dummyNode2 (id++, NODE_NETWORK, INVALID_SOCKET, addr2, 1 , 1 , CAddress (), " " , ConnectionType::INBOUND);
246- dummyNode2.SetCommonVersion (PROTOCOL_VERSION);
247- peerLogic->InitializeNode (&dummyNode2);
248- dummyNode2.fSuccessfullyConnected = true ;
249- peerLogic->Misbehaving (dummyNode2.GetId (), DISCOURAGEMENT_THRESHOLD - 1 , /* message */ " " );
249+ nodes[1 ] = new CNode (id++, NODE_NETWORK, INVALID_SOCKET, addr2, 1 , 1 , CAddress (), " " , ConnectionType::INBOUND);
250+ nodes[1 ]->SetCommonVersion (PROTOCOL_VERSION);
251+ peerLogic->InitializeNode (nodes[1 ]);
252+ nodes[1 ]->fSuccessfullyConnected = true ;
253+ connman->AddNode (*nodes[1 ]);
254+ peerLogic->Misbehaving (nodes[1 ]->GetId (), DISCOURAGEMENT_THRESHOLD - 1 , /* message */ " " );
250255 {
251- LOCK (dummyNode2. cs_sendProcessing );
252- BOOST_CHECK (peerLogic->SendMessages (&dummyNode2 ));
256+ LOCK (nodes[ 1 ]-> cs_sendProcessing );
257+ BOOST_CHECK (peerLogic->SendMessages (nodes[ 1 ] ));
253258 }
254259 BOOST_CHECK (!banman->IsDiscouraged (addr2)); // 2 not discouraged yet...
260+ BOOST_CHECK (!nodes[1 ]->fDisconnect );
255261 BOOST_CHECK (banman->IsDiscouraged (addr1)); // ... but 1 still should be
256- peerLogic->Misbehaving (dummyNode2.GetId (), 1 , /* message */ " " ); // 2 reaches discouragement threshold
262+ BOOST_CHECK (nodes[0 ]->fDisconnect );
263+ peerLogic->Misbehaving (nodes[1 ]->GetId (), 1 , /* message */ " " ); // 2 reaches discouragement threshold
264+ {
265+ LOCK (nodes[1 ]->cs_sendProcessing );
266+ BOOST_CHECK (peerLogic->SendMessages (nodes[1 ]));
267+ }
268+ // Expect both 1 and 2 to be discouraged now.
269+ BOOST_CHECK (banman->IsDiscouraged (addr1));
270+ BOOST_CHECK (nodes[0 ]->fDisconnect );
271+ BOOST_CHECK (banman->IsDiscouraged (addr2));
272+ BOOST_CHECK (nodes[1 ]->fDisconnect );
273+
274+ // Make sure non-IP peers get discouraged and disconnected properly.
275+
276+ CNetAddr tor_netaddr;
277+ BOOST_REQUIRE (
278+ tor_netaddr.SetSpecial (" pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion" ));
279+
280+ const CAddress addr3 (CService (tor_netaddr, Params ().GetDefaultPort ()), NODE_NONE);
281+ nodes[2 ] = new CNode (id++, NODE_NETWORK, INVALID_SOCKET, addr3, 1 , 1 , CAddress (), " " ,
282+ ConnectionType::OUTBOUND_FULL_RELAY);
283+ nodes[2 ]->SetCommonVersion (PROTOCOL_VERSION);
284+ peerLogic->InitializeNode (nodes[2 ]);
285+ connman->AddNode (*nodes[2 ]);
286+ nodes[2 ]->fSuccessfullyConnected = true ;
287+ peerLogic->Misbehaving (nodes[2 ]->GetId (), DISCOURAGEMENT_THRESHOLD, /* message */ " " );
257288 {
258- LOCK (dummyNode2.cs_sendProcessing );
259- BOOST_CHECK (peerLogic->SendMessages (&dummyNode2));
289+ LOCK (nodes[2 ]->cs_sendProcessing );
290+ BOOST_CHECK (peerLogic->SendMessages (nodes[2 ]));
291+ }
292+ BOOST_CHECK (banman->IsDiscouraged (addr1));
293+ BOOST_CHECK (banman->IsDiscouraged (addr2));
294+ BOOST_CHECK (banman->IsDiscouraged (addr3));
295+ for (CNode* node : nodes) {
296+ BOOST_CHECK (node->fDisconnect );
260297 }
261- BOOST_CHECK (banman->IsDiscouraged (addr1)); // Expect both 1 and 2
262- BOOST_CHECK (banman->IsDiscouraged (addr2)); // to be discouraged now
263298
264299 bool dummy;
265- peerLogic->FinalizeNode (dummyNode1, dummy);
266- peerLogic->FinalizeNode (dummyNode2, dummy);
300+ for (CNode* node : nodes) {
301+ peerLogic->FinalizeNode (*node, dummy);
302+ }
303+ connman->ClearNodes ();
267304}
268305
269306BOOST_AUTO_TEST_CASE (DoS_bantime)
0 commit comments