@@ -42,6 +42,51 @@ static NodeId id = 0;
4242
4343BOOST_FIXTURE_TEST_SUITE (DoS_tests, TestingSetup)
4444
45+ // Test eviction of an outbound peer whose chain never advances
46+ // Mock a node connection, and use mocktime to simulate a peer
47+ // which never sends any headers messages. PeerLogic should
48+ // decide to evict that outbound peer, after the appropriate timeouts.
49+ // Note that we protect 4 outbound nodes from being subject to
50+ // this logic; this test takes advantage of that protection only
51+ // being applied to nodes which send headers with sufficient
52+ // work.
53+ BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
54+ {
55+ std::atomic<bool > interruptDummy (false );
56+
57+ // Mock an outbound peer
58+ CAddress addr1 (ip (0xa0b0c001 ), NODE_NONE);
59+ CNode dummyNode1 (id++, ServiceFlags (NODE_NETWORK|NODE_WITNESS), 0 , INVALID_SOCKET, addr1, 0 , 0 , CAddress (), " " , /* fInboundIn=*/ false );
60+ dummyNode1.SetSendVersion (PROTOCOL_VERSION);
61+
62+ peerLogic->InitializeNode (&dummyNode1);
63+ dummyNode1.nVersion = 1 ;
64+ dummyNode1.fSuccessfullyConnected = true ;
65+
66+ // This test requires that we have a chain with non-zero work.
67+ BOOST_CHECK (chainActive.Tip () != nullptr );
68+ BOOST_CHECK (chainActive.Tip ()->nChainWork > 0 );
69+
70+ // Test starts here
71+ peerLogic->SendMessages (&dummyNode1, interruptDummy); // should result in getheaders
72+ BOOST_CHECK (dummyNode1.vSendMsg .size () > 0 );
73+ dummyNode1.vSendMsg .clear ();
74+
75+ int64_t nStartTime = GetTime ();
76+ // Wait 21 minutes
77+ SetMockTime (nStartTime+21 *60 );
78+ peerLogic->SendMessages (&dummyNode1, interruptDummy); // should result in getheaders
79+ BOOST_CHECK (dummyNode1.vSendMsg .size () > 0 );
80+ // Wait 3 more minutes
81+ SetMockTime (nStartTime+24 *60 );
82+ peerLogic->SendMessages (&dummyNode1, interruptDummy); // should result in disconnect
83+ BOOST_CHECK (dummyNode1.fDisconnect == true );
84+ SetMockTime (0 );
85+
86+ bool dummy;
87+ peerLogic->FinalizeNode (dummyNode1.GetId (), dummy);
88+ }
89+
4590BOOST_AUTO_TEST_CASE (DoS_banning)
4691{
4792 std::atomic<bool > interruptDummy (false );
@@ -71,6 +116,10 @@ BOOST_AUTO_TEST_CASE(DoS_banning)
71116 Misbehaving (dummyNode2.GetId (), 50 );
72117 peerLogic->SendMessages (&dummyNode2, interruptDummy);
73118 BOOST_CHECK (connman->IsBanned (addr2));
119+
120+ bool dummy;
121+ peerLogic->FinalizeNode (dummyNode1.GetId (), dummy);
122+ peerLogic->FinalizeNode (dummyNode2.GetId (), dummy);
74123}
75124
76125BOOST_AUTO_TEST_CASE (DoS_banscore)
@@ -95,6 +144,9 @@ BOOST_AUTO_TEST_CASE(DoS_banscore)
95144 peerLogic->SendMessages (&dummyNode1, interruptDummy);
96145 BOOST_CHECK (connman->IsBanned (addr1));
97146 gArgs .ForceSetArg (" -banscore" , std::to_string (DEFAULT_BANSCORE_THRESHOLD));
147+
148+ bool dummy;
149+ peerLogic->FinalizeNode (dummyNode1.GetId (), dummy);
98150}
99151
100152BOOST_AUTO_TEST_CASE (DoS_bantime)
@@ -121,6 +173,9 @@ BOOST_AUTO_TEST_CASE(DoS_bantime)
121173
122174 SetMockTime (nStartTime+60 *60 *24 +1 );
123175 BOOST_CHECK (!connman->IsBanned (addr));
176+
177+ bool dummy;
178+ peerLogic->FinalizeNode (dummyNode.GetId (), dummy);
124179}
125180
126181CTransactionRef RandomOrphan ()
0 commit comments