|
| 1 | +// Copyright (c) 2014-2015 The Bitcoin Core developers |
| 2 | +// Distributed under the MIT software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +#include "chain.h" |
| 6 | +#include "random.h" |
| 7 | +#include "versionbits.h" |
| 8 | +#include "test/test_bitcoin.h" |
| 9 | + |
| 10 | +#include <boost/test/unit_test.hpp> |
| 11 | + |
| 12 | +/* Define a virtual block time, one block per 10 minutes after Nov 14 2014, 0:55:36am */ |
| 13 | +int32_t TestTime(int nHeight) { return 1415926536 + 600 * nHeight; } |
| 14 | + |
| 15 | +static const Consensus::Params paramsDummy = Consensus::Params(); |
| 16 | + |
| 17 | +class TestConditionChecker : public AbstractThresholdConditionChecker |
| 18 | +{ |
| 19 | +private: |
| 20 | + mutable ThresholdConditionCache cache; |
| 21 | + |
| 22 | +public: |
| 23 | + int64_t BeginTime(const Consensus::Params& params) const { return TestTime(10000); } |
| 24 | + int64_t EndTime(const Consensus::Params& params) const { return TestTime(20000); } |
| 25 | + int Period(const Consensus::Params& params) const { return 1000; } |
| 26 | + int Threshold(const Consensus::Params& params) const { return 900; } |
| 27 | + bool Condition(const CBlockIndex* pindex, const Consensus::Params& params) const { return (pindex->nVersion & 0x100); } |
| 28 | + |
| 29 | + ThresholdState GetStateFor(const CBlockIndex* pindexPrev) const { return AbstractThresholdConditionChecker::GetStateFor(pindexPrev, paramsDummy, cache); } |
| 30 | +}; |
| 31 | + |
| 32 | +#define CHECKERS 6 |
| 33 | + |
| 34 | +class VersionBitsTester |
| 35 | +{ |
| 36 | + // A fake blockchain |
| 37 | + std::vector<CBlockIndex*> vpblock; |
| 38 | + |
| 39 | + // 6 independent checkers for the same bit. |
| 40 | + // The first one performs all checks, the second only 50%, the third only 25%, etc... |
| 41 | + // This is to test whether lack of cached information leads to the same results. |
| 42 | + TestConditionChecker checker[CHECKERS]; |
| 43 | + |
| 44 | + // Test counter (to identify failures) |
| 45 | + int num; |
| 46 | + |
| 47 | +public: |
| 48 | + VersionBitsTester() : num(0) {} |
| 49 | + |
| 50 | + VersionBitsTester& Reset() { |
| 51 | + for (unsigned int i = 0; i < vpblock.size(); i++) { |
| 52 | + delete vpblock[i]; |
| 53 | + } |
| 54 | + for (unsigned int i = 0; i < CHECKERS; i++) { |
| 55 | + checker[i] = TestConditionChecker(); |
| 56 | + } |
| 57 | + vpblock.clear(); |
| 58 | + return *this; |
| 59 | + } |
| 60 | + |
| 61 | + ~VersionBitsTester() { |
| 62 | + Reset(); |
| 63 | + } |
| 64 | + |
| 65 | + VersionBitsTester& Mine(unsigned int height, int32_t nTime, int32_t nVersion) { |
| 66 | + while (vpblock.size() < height) { |
| 67 | + CBlockIndex* pindex = new CBlockIndex(); |
| 68 | + pindex->nHeight = vpblock.size(); |
| 69 | + pindex->pprev = vpblock.size() > 0 ? vpblock.back() : NULL; |
| 70 | + pindex->nTime = nTime; |
| 71 | + pindex->nVersion = nVersion; |
| 72 | + pindex->BuildSkip(); |
| 73 | + vpblock.push_back(pindex); |
| 74 | + } |
| 75 | + return *this; |
| 76 | + } |
| 77 | + |
| 78 | + VersionBitsTester& TestDefined() { |
| 79 | + for (int i = 0; i < CHECKERS; i++) { |
| 80 | + if ((insecure_rand() & ((1 << i) - 1)) == 0) { |
| 81 | + BOOST_CHECK_MESSAGE(checker[i].GetStateFor(vpblock.empty() ? NULL : vpblock.back()) == THRESHOLD_DEFINED, strprintf("Test %i for DEFINED", num)); |
| 82 | + } |
| 83 | + } |
| 84 | + num++; |
| 85 | + return *this; |
| 86 | + } |
| 87 | + |
| 88 | + VersionBitsTester& TestStarted() { |
| 89 | + for (int i = 0; i < CHECKERS; i++) { |
| 90 | + if ((insecure_rand() & ((1 << i) - 1)) == 0) { |
| 91 | + BOOST_CHECK_MESSAGE(checker[i].GetStateFor(vpblock.empty() ? NULL : vpblock.back()) == THRESHOLD_STARTED, strprintf("Test %i for STARTED", num)); |
| 92 | + } |
| 93 | + } |
| 94 | + num++; |
| 95 | + return *this; |
| 96 | + } |
| 97 | + |
| 98 | + VersionBitsTester& TestLockedIn() { |
| 99 | + for (int i = 0; i < CHECKERS; i++) { |
| 100 | + if ((insecure_rand() & ((1 << i) - 1)) == 0) { |
| 101 | + BOOST_CHECK_MESSAGE(checker[i].GetStateFor(vpblock.empty() ? NULL : vpblock.back()) == THRESHOLD_LOCKED_IN, strprintf("Test %i for LOCKED_IN", num)); |
| 102 | + } |
| 103 | + } |
| 104 | + num++; |
| 105 | + return *this; |
| 106 | + } |
| 107 | + |
| 108 | + VersionBitsTester& TestActive() { |
| 109 | + for (int i = 0; i < CHECKERS; i++) { |
| 110 | + if ((insecure_rand() & ((1 << i) - 1)) == 0) { |
| 111 | + BOOST_CHECK_MESSAGE(checker[i].GetStateFor(vpblock.empty() ? NULL : vpblock.back()) == THRESHOLD_ACTIVE, strprintf("Test %i for ACTIVE", num)); |
| 112 | + } |
| 113 | + } |
| 114 | + num++; |
| 115 | + return *this; |
| 116 | + } |
| 117 | + |
| 118 | + VersionBitsTester& TestFailed() { |
| 119 | + for (int i = 0; i < CHECKERS; i++) { |
| 120 | + if ((insecure_rand() & ((1 << i) - 1)) == 0) { |
| 121 | + BOOST_CHECK_MESSAGE(checker[i].GetStateFor(vpblock.empty() ? NULL : vpblock.back()) == THRESHOLD_FAILED, strprintf("Test %i for FAILED", num)); |
| 122 | + } |
| 123 | + } |
| 124 | + num++; |
| 125 | + return *this; |
| 126 | + } |
| 127 | +}; |
| 128 | + |
| 129 | +BOOST_FIXTURE_TEST_SUITE(versionbits_tests, TestingSetup) |
| 130 | + |
| 131 | +BOOST_AUTO_TEST_CASE(versionbits_test) |
| 132 | +{ |
| 133 | + for (int i = 0; i < 64; i++) { |
| 134 | + // DEFINED -> FAILED |
| 135 | + VersionBitsTester().TestDefined() |
| 136 | + .Mine(1, TestTime(1), 0x100).TestDefined() |
| 137 | + .Mine(11, TestTime(11), 0x100).TestDefined() |
| 138 | + .Mine(989, TestTime(989), 0x100).TestDefined() |
| 139 | + .Mine(999, TestTime(20000), 0x100).TestDefined() |
| 140 | + .Mine(1000, TestTime(20000), 0x100).TestFailed() |
| 141 | + .Mine(1999, TestTime(30001), 0x100).TestFailed() |
| 142 | + .Mine(2000, TestTime(30002), 0x100).TestFailed() |
| 143 | + .Mine(2001, TestTime(30003), 0x100).TestFailed() |
| 144 | + .Mine(2999, TestTime(30004), 0x100).TestFailed() |
| 145 | + .Mine(3000, TestTime(30005), 0x100).TestFailed() |
| 146 | + |
| 147 | + // DEFINED -> STARTED -> FAILED |
| 148 | + .Reset().TestDefined() |
| 149 | + .Mine(1, TestTime(1), 0).TestDefined() |
| 150 | + .Mine(1000, TestTime(10000) - 1, 0x100).TestDefined() // One second more and it would be defined |
| 151 | + .Mine(2000, TestTime(10000), 0x100).TestStarted() // So that's what happens the next period |
| 152 | + .Mine(2051, TestTime(10010), 0).TestStarted() // 51 old blocks |
| 153 | + .Mine(2950, TestTime(10020), 0x100).TestStarted() // 899 new blocks |
| 154 | + .Mine(3000, TestTime(20000), 0).TestFailed() // 50 old blocks (so 899 out of the past 1000) |
| 155 | + .Mine(4000, TestTime(20010), 0x100).TestFailed() |
| 156 | + |
| 157 | + // DEFINED -> STARTED -> FAILED while threshold reached |
| 158 | + .Reset().TestDefined() |
| 159 | + .Mine(1, TestTime(1), 0).TestDefined() |
| 160 | + .Mine(1000, TestTime(10000) - 1, 0x101).TestDefined() // One second more and it would be defined |
| 161 | + .Mine(2000, TestTime(10000), 0x101).TestStarted() // So that's what happens the next period |
| 162 | + .Mine(2999, TestTime(30000), 0x100).TestStarted() // 999 new blocks |
| 163 | + .Mine(3000, TestTime(30000), 0x100).TestFailed() // 1 new block (so 1000 out of the past 1000 are new) |
| 164 | + .Mine(3999, TestTime(30001), 0).TestFailed() |
| 165 | + .Mine(4000, TestTime(30002), 0).TestFailed() |
| 166 | + .Mine(14333, TestTime(30003), 0).TestFailed() |
| 167 | + .Mine(24000, TestTime(40000), 0).TestFailed() |
| 168 | + |
| 169 | + // DEFINED -> STARTED -> LOCKEDIN at the last minute -> ACTIVE |
| 170 | + .Reset().TestDefined() |
| 171 | + .Mine(1, TestTime(1), 0).TestDefined() |
| 172 | + .Mine(1000, TestTime(10000) - 1, 0x101).TestDefined() // One second more and it would be defined |
| 173 | + .Mine(2000, TestTime(10000), 0x101).TestStarted() // So that's what happens the next period |
| 174 | + .Mine(2050, TestTime(10010), 0x200).TestStarted() // 50 old blocks |
| 175 | + .Mine(2950, TestTime(10020), 0x100).TestStarted() // 900 new blocks |
| 176 | + .Mine(2999, TestTime(19999), 0x200).TestStarted() // 49 old blocks |
| 177 | + .Mine(3000, TestTime(29999), 0x200).TestLockedIn() // 1 old block (so 900 out of the past 1000) |
| 178 | + .Mine(3999, TestTime(30001), 0).TestLockedIn() |
| 179 | + .Mine(4000, TestTime(30002), 0).TestActive() |
| 180 | + .Mine(14333, TestTime(30003), 0).TestActive() |
| 181 | + .Mine(24000, TestTime(40000), 0).TestActive(); |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +BOOST_AUTO_TEST_SUITE_END() |
0 commit comments