16
16
/* Define a virtual block time, one block per 10 minutes after Nov 14 2014, 0:55:36am */
17
17
static int32_t TestTime (int nHeight) { return 1415926536 + 600 * nHeight; }
18
18
19
- class TestConditionChecker : public AbstractThresholdConditionChecker
19
+ class TestConditionChecker final : public VersionBitsConditionChecker
20
20
{
21
21
private:
22
22
mutable ThresholdConditionCache cache;
23
23
24
24
public:
25
- int64_t BeginTime () const override { return TestTime (10000 ); }
26
- int64_t EndTime () const override { return TestTime (20000 ); }
27
- int Period () const override { return 1000 ; }
28
- int Threshold () const override { return 900 ; }
29
- bool Condition (const CBlockIndex* pindex) const override { return (pindex->nVersion & 0x100 ); }
30
-
31
- ThresholdState GetStateFor (const CBlockIndex* pindexPrev) const { return AbstractThresholdConditionChecker::GetStateFor (pindexPrev, cache); }
32
- int GetStateSinceHeightFor (const CBlockIndex* pindexPrev) const { return AbstractThresholdConditionChecker::GetStateSinceHeightFor (pindexPrev, cache); }
33
- };
25
+ // constructor is implicit to allow for easier initialization of vector<TestConditionChecker>
26
+ explicit (false ) TestConditionChecker(const Consensus::BIP9Deployment& dep) : VersionBitsConditionChecker{dep} { }
27
+ ~TestConditionChecker () override = default ;
34
28
35
- class TestDelayedActivationConditionChecker : public TestConditionChecker
36
- {
37
- public:
38
- int MinActivationHeight () const override { return 15000 ; }
39
- };
40
-
41
- class TestAlwaysActiveConditionChecker : public TestConditionChecker
42
- {
43
- public:
44
- int64_t BeginTime () const override { return Consensus::BIP9Deployment::ALWAYS_ACTIVE; }
29
+ ThresholdState StateFor (const CBlockIndex* pindexPrev) const { return AbstractThresholdConditionChecker::GetStateFor (pindexPrev, cache); }
30
+ int StateSinceHeightFor (const CBlockIndex* pindexPrev) const { return AbstractThresholdConditionChecker::GetStateSinceHeightFor (pindexPrev, cache); }
31
+ void clear () { cache.clear (); }
45
32
};
46
33
47
- class TestNeverActiveConditionChecker : public TestConditionChecker
34
+ namespace {
35
+ struct Deployments
48
36
{
49
- public:
50
- int64_t BeginTime () const override { return Consensus::BIP9Deployment::NEVER_ACTIVE; }
37
+ const Consensus::BIP9Deployment normal{
38
+ .bit = 8 ,
39
+ .nStartTime = TestTime (10000 ),
40
+ .nTimeout = TestTime (20000 ),
41
+ .min_activation_height = 0 ,
42
+ .period = 1000 ,
43
+ .threshold = 900 ,
44
+ };
45
+ Consensus::BIP9Deployment always, never, delayed;
46
+ Deployments ()
47
+ {
48
+ delayed = normal; delayed.min_activation_height = 15000 ;
49
+ always = normal; always.nStartTime = Consensus::BIP9Deployment::ALWAYS_ACTIVE;
50
+ never = normal; never.nStartTime = Consensus::BIP9Deployment::NEVER_ACTIVE;
51
+ }
51
52
};
53
+ }
52
54
53
55
#define CHECKERS 6
54
56
@@ -58,22 +60,28 @@ class VersionBitsTester
58
60
// A fake blockchain
59
61
std::vector<CBlockIndex*> vpblock;
60
62
63
+ // Used to automatically set the top bits for manual calls to Mine()
64
+ const int32_t nVersionBase{0 };
65
+
66
+ // Setup BIP9Deployment structs for the checkers
67
+ const Deployments test_deployments;
68
+
61
69
// 6 independent checkers for the same bit.
62
70
// The first one performs all checks, the second only 50%, the third only 25%, etc...
63
71
// This is to test whether lack of cached information leads to the same results.
64
- TestConditionChecker checker[ CHECKERS] ;
72
+ std::vector< TestConditionChecker> checker{ CHECKERS, {test_deployments. normal }} ;
65
73
// Another 6 that assume delayed activation
66
- TestDelayedActivationConditionChecker checker_delayed[ CHECKERS] ;
74
+ std::vector<TestConditionChecker> checker_delayed{ CHECKERS, {test_deployments. delayed }} ;
67
75
// Another 6 that assume always active activation
68
- TestAlwaysActiveConditionChecker checker_always[ CHECKERS] ;
76
+ std::vector<TestConditionChecker> checker_always{ CHECKERS, {test_deployments. always }} ;
69
77
// Another 6 that assume never active activation
70
- TestNeverActiveConditionChecker checker_never[ CHECKERS] ;
78
+ std::vector<TestConditionChecker> checker_never{ CHECKERS, {test_deployments. never }} ;
71
79
72
80
// Test counter (to identify failures)
73
81
int num{1000 };
74
82
75
83
public:
76
- VersionBitsTester (FastRandomContext& rng) : m_rng{rng} { }
84
+ explicit VersionBitsTester (FastRandomContext& rng, int32_t nVersionBase= 0 ) : m_rng{rng}, nVersionBase{nVersionBase} { }
77
85
78
86
VersionBitsTester& Reset () {
79
87
// Have each group of tests be counted by the 1000s part, starting at 1000
@@ -83,10 +91,10 @@ class VersionBitsTester
83
91
delete vpblock[i];
84
92
}
85
93
for (unsigned int i = 0 ; i < CHECKERS; i++) {
86
- checker[i] = TestConditionChecker ();
87
- checker_delayed[i] = TestDelayedActivationConditionChecker ();
88
- checker_always[i] = TestAlwaysActiveConditionChecker ();
89
- checker_never[i] = TestNeverActiveConditionChecker ();
94
+ checker[i]. clear ();
95
+ checker_delayed[i]. clear ();
96
+ checker_always[i]. clear ();
97
+ checker_never[i]. clear ();
90
98
}
91
99
vpblock.clear ();
92
100
return *this ;
@@ -102,7 +110,7 @@ class VersionBitsTester
102
110
pindex->nHeight = vpblock.size ();
103
111
pindex->pprev = Tip ();
104
112
pindex->nTime = nTime;
105
- pindex->nVersion = nVersion;
113
+ pindex->nVersion = (nVersionBase | nVersion) ;
106
114
pindex->BuildSkip ();
107
115
vpblock.push_back (pindex);
108
116
}
@@ -119,10 +127,10 @@ class VersionBitsTester
119
127
const CBlockIndex* tip = Tip ();
120
128
for (int i = 0 ; i < CHECKERS; i++) {
121
129
if (m_rng.randbits (i) == 0 ) {
122
- BOOST_CHECK_MESSAGE (checker[i].GetStateSinceHeightFor (tip) == height, strprintf (" Test %i for StateSinceHeight" , num));
123
- BOOST_CHECK_MESSAGE (checker_delayed[i].GetStateSinceHeightFor (tip) == height_delayed, strprintf (" Test %i for StateSinceHeight (delayed)" , num));
124
- BOOST_CHECK_MESSAGE (checker_always[i].GetStateSinceHeightFor (tip) == 0 , strprintf (" Test %i for StateSinceHeight (always active)" , num));
125
- BOOST_CHECK_MESSAGE (checker_never[i].GetStateSinceHeightFor (tip) == 0 , strprintf (" Test %i for StateSinceHeight (never active)" , num));
130
+ BOOST_CHECK_MESSAGE (checker[i].StateSinceHeightFor (tip) == height, strprintf (" Test %i for StateSinceHeight" , num));
131
+ BOOST_CHECK_MESSAGE (checker_delayed[i].StateSinceHeightFor (tip) == height_delayed, strprintf (" Test %i for StateSinceHeight (delayed)" , num));
132
+ BOOST_CHECK_MESSAGE (checker_always[i].StateSinceHeightFor (tip) == 0 , strprintf (" Test %i for StateSinceHeight (always active)" , num));
133
+ BOOST_CHECK_MESSAGE (checker_never[i].StateSinceHeightFor (tip) == 0 , strprintf (" Test %i for StateSinceHeight (never active)" , num));
126
134
}
127
135
}
128
136
num++;
@@ -145,10 +153,10 @@ class VersionBitsTester
145
153
const CBlockIndex* pindex = Tip ();
146
154
for (int i = 0 ; i < CHECKERS; i++) {
147
155
if (m_rng.randbits (i) == 0 ) {
148
- ThresholdState got = checker[i].GetStateFor (pindex);
149
- ThresholdState got_delayed = checker_delayed[i].GetStateFor (pindex);
150
- ThresholdState got_always = checker_always[i].GetStateFor (pindex);
151
- ThresholdState got_never = checker_never[i].GetStateFor (pindex);
156
+ ThresholdState got = checker[i].StateFor (pindex);
157
+ ThresholdState got_delayed = checker_delayed[i].StateFor (pindex);
158
+ ThresholdState got_always = checker_always[i].StateFor (pindex);
159
+ ThresholdState got_never = checker_never[i].StateFor (pindex);
152
160
// nHeight of the next block. If vpblock is empty, the next (ie first)
153
161
// block should be the genesis block with nHeight == 0.
154
162
int height = pindex == nullptr ? 0 : pindex->nHeight + 1 ;
@@ -180,7 +188,7 @@ BOOST_AUTO_TEST_CASE(versionbits_test)
180
188
{
181
189
for (int i = 0 ; i < 64 ; i++) {
182
190
// DEFINED -> STARTED after timeout reached -> FAILED
183
- VersionBitsTester (m_rng).TestDefined ().TestStateSinceHeight (0 )
191
+ VersionBitsTester (m_rng, VERSIONBITS_TOP_BITS ).TestDefined ().TestStateSinceHeight (0 )
184
192
.Mine (1 , TestTime (1 ), 0x100 ).TestDefined ().TestStateSinceHeight (0 )
185
193
.Mine (11 , TestTime (11 ), 0x100 ).TestDefined ().TestStateSinceHeight (0 )
186
194
.Mine (989 , TestTime (989 ), 0x100 ).TestDefined ().TestStateSinceHeight (0 )
0 commit comments