Changeset View
Changeset View
Standalone View
Standalone View
src/seeder/test/banning_tests.cpp
- This file was added.
// Copyright (c) 2020 The Bitcoin developers | |||||
// Distributed under the MIT software license, see the accompanying | |||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | |||||
#include <seeder/bitcoin.h> | |||||
#include <boost/test/unit_test.hpp> | |||||
BOOST_AUTO_TEST_SUITE(banning_tests) | |||||
static const int64_t RECENT_SUCCESS_OR_DISCOVERY_TIME = 0; | |||||
static const int64_t OLD_SUCCESS_OR_DISCOVERY_TIME = 2 * 86400 + 1; | |||||
class TestBanCSeederNode : public CSeederNode { | |||||
public: | |||||
TestBanCSeederNode(bool reliable, bool testedBefore, | |||||
int64_t timeSinceLastSuccess) | |||||
: CSeederNode(CService(), reliable, testedBefore, timeSinceLastSuccess, | |||||
nullptr) {} | |||||
void TestDetermineBan(const bool results, const int &expectedBanTime) { | |||||
DetermineBan(results); | |||||
BOOST_CHECK_EQUAL(GetBan(), expectedBanTime); | |||||
} | |||||
}; | |||||
struct TestNodeState { | |||||
TestNodeState(bool reliable, bool triedBefore, int64_t time, bool isGood) | |||||
: isReliable(reliable), hasBeenTriedBefore(triedBefore), | |||||
timeSinceLastSuccess(time), testResults(isGood) {} | |||||
bool isReliable; | |||||
bool hasBeenTriedBefore; | |||||
int64_t timeSinceLastSuccess; | |||||
bool testResults; | |||||
}; | |||||
static void CheckDetermineBan(TestNodeState state, int expectedBanTime) { | |||||
TestBanCSeederNode testNode(state.isReliable, state.hasBeenTriedBefore, | |||||
state.timeSinceLastSuccess); | |||||
BOOST_CHECK_EQUAL(testNode.GetBan(), 0); | |||||
testNode.TestDetermineBan(state.testResults, expectedBanTime); | |||||
} | |||||
BOOST_AUTO_TEST_CASE(ban_test) { | |||||
// Node banning is very srict. Only a node that is unreliable, has been | |||||
// tested before, has no success/was not discovered within the last 2 days, | |||||
// and has failed the current test will be banned. | |||||
// Node is reliable, has been tested before, had a recent success, and | |||||
// passed the current test | |||||
CheckDetermineBan( | |||||
TestNodeState(true, true, RECENT_SUCCESS_OR_DISCOVERY_TIME, true), 0); | |||||
// Node is reliable, has been tested before, had a recent success, and | |||||
// failed the current test | |||||
CheckDetermineBan( | |||||
TestNodeState(true, true, RECENT_SUCCESS_OR_DISCOVERY_TIME, false), 0); | |||||
// Node is reliable, has been tested before, had no recent success, and | |||||
// passed the current test | |||||
CheckDetermineBan( | |||||
TestNodeState(true, true, OLD_SUCCESS_OR_DISCOVERY_TIME, true), 0); | |||||
// Node is reliable, has been tested before, had no recent success, and | |||||
// failed the current test | |||||
CheckDetermineBan( | |||||
TestNodeState(true, true, OLD_SUCCESS_OR_DISCOVERY_TIME, false), 0); | |||||
// Node is reliable, has not been tested before, was discovered recently, | |||||
// and passed the current test | |||||
CheckDetermineBan( | |||||
TestNodeState(true, false, RECENT_SUCCESS_OR_DISCOVERY_TIME, true), 0); | |||||
// Node is reliable, has not been tested before, was discovered recently, | |||||
// and failed the current test | |||||
CheckDetermineBan( | |||||
TestNodeState(true, false, RECENT_SUCCESS_OR_DISCOVERY_TIME, false), 0); | |||||
// Node is reliable, has not been tested before, was not discovered | |||||
// recently, and passed the current test | |||||
CheckDetermineBan( | |||||
TestNodeState(true, false, OLD_SUCCESS_OR_DISCOVERY_TIME, true), 0); | |||||
// Node is reliable, has not been tested before, was not discovered | |||||
// recently, and failed the current test | |||||
CheckDetermineBan( | |||||
TestNodeState(true, false, OLD_SUCCESS_OR_DISCOVERY_TIME, false), 0); | |||||
// Node is not reliable, has been tested before, had a recent success, and | |||||
// passed the current test | |||||
CheckDetermineBan( | |||||
TestNodeState(false, true, RECENT_SUCCESS_OR_DISCOVERY_TIME, true), 0); | |||||
// Node is not reliable, has been tested before, had a recent success, and | |||||
// failed the current test | |||||
CheckDetermineBan( | |||||
TestNodeState(false, true, RECENT_SUCCESS_OR_DISCOVERY_TIME, false), 0); | |||||
// Node is not reliable, has been tested before, had no recent success, and | |||||
// passed the current test | |||||
CheckDetermineBan( | |||||
TestNodeState(false, true, OLD_SUCCESS_OR_DISCOVERY_TIME, true), 0); | |||||
// Node is not reliable, has been tested before, had no recent success, and | |||||
// failed the current test | |||||
// This type of node is not only very likely to be following the wrong chain | |||||
// but is unlikely to be helpful as a bootstrap node to help (at least | |||||
// partially) catch up to the current chain tip. As such, it should be | |||||
// banned | |||||
CheckDetermineBan( | |||||
TestNodeState(false, true, OLD_SUCCESS_OR_DISCOVERY_TIME, false), | |||||
100000); | |||||
// Node is not reliable, has not been tested before, was discovered | |||||
// recently, and passed the current test | |||||
CheckDetermineBan( | |||||
TestNodeState(false, false, RECENT_SUCCESS_OR_DISCOVERY_TIME, true), 0); | |||||
// Node is not reliable, has been not tested before, was discovered | |||||
// recently, and failed the current test | |||||
CheckDetermineBan( | |||||
TestNodeState(false, false, RECENT_SUCCESS_OR_DISCOVERY_TIME, false), | |||||
0); | |||||
// Node is not reliable, has not been tested before, was not discovered | |||||
// recently, and passed the current test | |||||
CheckDetermineBan( | |||||
TestNodeState(false, false, OLD_SUCCESS_OR_DISCOVERY_TIME, true), 0); | |||||
// Node is not reliable, has not been tested before, was not discovered | |||||
// recently, and failed the current test | |||||
CheckDetermineBan( | |||||
TestNodeState(false, false, OLD_SUCCESS_OR_DISCOVERY_TIME, false), 0); | |||||
} | |||||
BOOST_AUTO_TEST_SUITE_END() |