| 41 | #define CHECKERS 6 |
| 42 | |
| 43 | class VersionBitsTester |
| 44 | { |
| 45 | // A fake blockchain |
| 46 | std::vector<CBlockIndex*> vpblock; |
| 47 | |
| 48 | // 6 independent checkers for the same bit. |
| 49 | // The first one performs all checks, the second only 50%, the third only 25%, etc... |
| 50 | // This is to test whether lack of cached information leads to the same results. |
| 51 | TestConditionChecker checker[CHECKERS]; |
| 52 | // Another 6 that assume always active activation |
| 53 | TestAlwaysActiveConditionChecker checker_always[CHECKERS]; |
| 54 | |
| 55 | // Test counter (to identify failures) |
| 56 | int num; |
| 57 | |
| 58 | public: |
| 59 | VersionBitsTester() : num(0) {} |
| 60 | |
| 61 | VersionBitsTester& Reset() { |
| 62 | for (unsigned int i = 0; i < vpblock.size(); i++) { |
| 63 | delete vpblock[i]; |
| 64 | } |
| 65 | for (unsigned int i = 0; i < CHECKERS; i++) { |
| 66 | checker[i] = TestConditionChecker(); |
| 67 | checker_always[i] = TestAlwaysActiveConditionChecker(); |
| 68 | } |
| 69 | vpblock.clear(); |
| 70 | return *this; |
| 71 | } |
| 72 | |
| 73 | ~VersionBitsTester() { |
| 74 | Reset(); |
| 75 | } |
| 76 | |
| 77 | VersionBitsTester& Mine(unsigned int height, int32_t nTime, int32_t nVersion) { |
| 78 | while (vpblock.size() < height) { |
| 79 | CBlockIndex* pindex = new CBlockIndex(); |
| 80 | pindex->nHeight = vpblock.size(); |
| 81 | pindex->pprev = vpblock.size() > 0 ? vpblock.back() : nullptr; |
| 82 | pindex->nTime = nTime; |
| 83 | pindex->nVersion = nVersion; |
| 84 | pindex->BuildSkip(); |
| 85 | vpblock.push_back(pindex); |
| 86 | } |
| 87 | return *this; |
| 88 | } |
| 89 | |
| 90 | VersionBitsTester& TestStateSinceHeight(int height) { |
| 91 | for (int i = 0; i < CHECKERS; i++) { |
| 92 | if (InsecureRandBits(i) == 0) { |
| 93 | BOOST_CHECK_MESSAGE(checker[i].GetStateSinceHeightFor(vpblock.empty() ? nullptr : vpblock.back()) == height, strprintf("Test %i for StateSinceHeight", num)); |
| 94 | BOOST_CHECK_MESSAGE(checker_always[i].GetStateSinceHeightFor(vpblock.empty() ? nullptr : vpblock.back()) == 0, strprintf("Test %i for StateSinceHeight (always active)", num)); |
| 95 | } |
| 96 | } |
| 97 | num++; |
| 98 | return *this; |
| 99 | } |
| 100 | |