| 57 | #define CHECKERS 6 |
| 58 | |
| 59 | class VersionBitsTester |
| 60 | { |
| 61 | FastRandomContext& m_rng; |
| 62 | // A fake blockchain |
| 63 | std::vector<CBlockIndex*> vpblock; |
| 64 | |
| 65 | // Used to automatically set the top bits for manual calls to Mine() |
| 66 | const int32_t nVersionBase{0}; |
| 67 | |
| 68 | // Setup BIP9Deployment structs for the checkers |
| 69 | const Deployments test_deployments; |
| 70 | |
| 71 | // 6 independent checkers for the same bit. |
| 72 | // The first one performs all checks, the second only 50%, the third only 25%, etc... |
| 73 | // This is to test whether lack of cached information leads to the same results. |
| 74 | std::vector<TestConditionChecker> checker{CHECKERS, {test_deployments.normal}}; |
| 75 | // Another 6 that assume delayed activation |
| 76 | std::vector<TestConditionChecker> checker_delayed{CHECKERS, {test_deployments.delayed}}; |
| 77 | // Another 6 that assume always active activation |
| 78 | std::vector<TestConditionChecker> checker_always{CHECKERS, {test_deployments.always}}; |
| 79 | // Another 6 that assume never active activation |
| 80 | std::vector<TestConditionChecker> checker_never{CHECKERS, {test_deployments.never}}; |
| 81 | |
| 82 | // Test counter (to identify failures) |
| 83 | int num{1000}; |
| 84 | |
| 85 | public: |
| 86 | explicit VersionBitsTester(FastRandomContext& rng, int32_t nVersionBase=0) : m_rng{rng}, nVersionBase{nVersionBase} { } |
| 87 | |
| 88 | VersionBitsTester& Reset() { |
| 89 | // Have each group of tests be counted by the 1000s part, starting at 1000 |
| 90 | num = num - (num % 1000) + 1000; |
| 91 | |
| 92 | for (unsigned int i = 0; i < vpblock.size(); i++) { |
| 93 | delete vpblock[i]; |
| 94 | } |
| 95 | for (unsigned int i = 0; i < CHECKERS; i++) { |
| 96 | checker[i].clear(); |
| 97 | checker_delayed[i].clear(); |
| 98 | checker_always[i].clear(); |
| 99 | checker_never[i].clear(); |
| 100 | } |
| 101 | vpblock.clear(); |
| 102 | return *this; |
| 103 | } |
| 104 | |
| 105 | ~VersionBitsTester() { |
| 106 | Reset(); |
| 107 | } |
| 108 | |
| 109 | VersionBitsTester& Mine(unsigned int height, int32_t nTime, int32_t nVersion) { |
| 110 | while (vpblock.size() < height) { |
| 111 | CBlockIndex* pindex = new CBlockIndex(); |
| 112 | pindex->nHeight = vpblock.size(); |
| 113 | pindex->pprev = Tip(); |
| 114 | pindex->nTime = nTime; |
| 115 | pindex->nVersion = (nVersionBase | nVersion); |
| 116 | pindex->BuildSkip(); |