Skip to content

Commit 31d2252

Browse files
committed
tests: add deterministic chain generation unittest fixture
1 parent 6606a4f commit 31d2252

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

src/test/util/setup_common.cpp

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,43 @@ TestingSetup::TestingSetup(const std::string& chainName, const std::vector<const
199199
}
200200
}
201201

202-
TestChain100Setup::TestChain100Setup()
202+
TestChain100Setup::TestChain100Setup(bool deterministic)
203203
{
204+
m_deterministic = deterministic;
205+
206+
if (m_deterministic) {
207+
SetMockTime(1598887952);
208+
constexpr std::array<unsigned char, 32> vchKey = {
209+
{
210+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
211+
}
212+
};
213+
coinbaseKey.Set(vchKey.begin(), vchKey.end(), false);
214+
} else {
215+
coinbaseKey.MakeNewKey(true);
216+
}
217+
204218
// Generate a 100-block chain:
205-
coinbaseKey.MakeNewKey(true);
219+
this->mineBlocks(COINBASE_MATURITY);
220+
221+
if (m_deterministic) {
222+
LOCK(::cs_main);
223+
assert(
224+
m_node.chainman->ActiveChain().Tip()->GetBlockHash().ToString() ==
225+
"49c95db1e470fed04496d801c9d8fbb78155d2c7f855232c918823d2c17d0cf6");
226+
}
227+
}
228+
229+
void TestChain100Setup::mineBlocks(int num_blocks)
230+
{
206231
CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
207-
for (int i = 0; i < COINBASE_MATURITY; i++) {
232+
for (int i = 0; i < num_blocks; i++)
233+
{
208234
std::vector<CMutableTransaction> noTxns;
209235
CBlock b = CreateAndProcessBlock(noTxns, scriptPubKey);
236+
if (m_deterministic) {
237+
SetMockTime(GetTime() + 1);
238+
}
210239
m_coinbase_txns.push_back(b.vtx[0]);
211240
}
212241
}
@@ -234,6 +263,9 @@ CBlock TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransa
234263
TestChain100Setup::~TestChain100Setup()
235264
{
236265
gArgs.ForceSetArg("-segwitheight", "0");
266+
if (m_deterministic) {
267+
SetMockTime(0);
268+
}
237269
}
238270

239271
CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(const CMutableTransaction& tx) const

src/test/util/setup_common.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ struct BasicTestingSetup {
7878
explicit BasicTestingSetup(const std::string& chainName = CBaseChainParams::MAIN, const std::vector<const char*>& extra_args = {});
7979
~BasicTestingSetup();
8080

81-
private:
8281
const fs::path m_path_root;
8382
};
8483

@@ -112,7 +111,7 @@ class CScript;
112111
* Testing fixture that pre-creates a 100-block REGTEST-mode block chain
113112
*/
114113
struct TestChain100Setup : public RegTestingSetup {
115-
TestChain100Setup();
114+
TestChain100Setup(bool deterministic = false);
116115

117116
/**
118117
* Create a new block with just given transactions, coinbase paying to
@@ -121,12 +120,21 @@ struct TestChain100Setup : public RegTestingSetup {
121120
CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
122121
const CScript& scriptPubKey);
123122

123+
//! Mine a series of new blocks on the active chain.
124+
void mineBlocks(int num_blocks);
125+
124126
~TestChain100Setup();
125127

128+
bool m_deterministic;
126129
std::vector<CTransactionRef> m_coinbase_txns; // For convenience, coinbase transactions
127130
CKey coinbaseKey; // private/public key needed to spend coinbase transactions
128131
};
129132

133+
134+
struct TestChain100DeterministicSetup : public TestChain100Setup {
135+
TestChain100DeterministicSetup() : TestChain100Setup(true) { }
136+
};
137+
130138
class CTxMemPoolEntry;
131139

132140
struct TestMemPoolEntryHelper

0 commit comments

Comments
 (0)