mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-08-08 20:22:14 +02:00
tests: add deterministic chain generation unittest fixture
This commit is contained in:
@@ -199,14 +199,43 @@ TestingSetup::TestingSetup(const std::string& chainName, const std::vector<const
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TestChain100Setup::TestChain100Setup()
|
TestChain100Setup::TestChain100Setup(bool deterministic)
|
||||||
{
|
{
|
||||||
|
m_deterministic = deterministic;
|
||||||
|
|
||||||
|
if (m_deterministic) {
|
||||||
|
SetMockTime(1598887952);
|
||||||
|
constexpr std::array<unsigned char, 32> vchKey = {
|
||||||
|
{
|
||||||
|
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
|
||||||
|
}
|
||||||
|
};
|
||||||
|
coinbaseKey.Set(vchKey.begin(), vchKey.end(), false);
|
||||||
|
} else {
|
||||||
|
coinbaseKey.MakeNewKey(true);
|
||||||
|
}
|
||||||
|
|
||||||
// Generate a 100-block chain:
|
// Generate a 100-block chain:
|
||||||
coinbaseKey.MakeNewKey(true);
|
this->mineBlocks(COINBASE_MATURITY);
|
||||||
|
|
||||||
|
if (m_deterministic) {
|
||||||
|
LOCK(::cs_main);
|
||||||
|
assert(
|
||||||
|
m_node.chainman->ActiveChain().Tip()->GetBlockHash().ToString() ==
|
||||||
|
"49c95db1e470fed04496d801c9d8fbb78155d2c7f855232c918823d2c17d0cf6");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void TestChain100Setup::mineBlocks(int num_blocks)
|
||||||
|
{
|
||||||
CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
|
CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
|
||||||
for (int i = 0; i < COINBASE_MATURITY; i++) {
|
for (int i = 0; i < num_blocks; i++)
|
||||||
|
{
|
||||||
std::vector<CMutableTransaction> noTxns;
|
std::vector<CMutableTransaction> noTxns;
|
||||||
CBlock b = CreateAndProcessBlock(noTxns, scriptPubKey);
|
CBlock b = CreateAndProcessBlock(noTxns, scriptPubKey);
|
||||||
|
if (m_deterministic) {
|
||||||
|
SetMockTime(GetTime() + 1);
|
||||||
|
}
|
||||||
m_coinbase_txns.push_back(b.vtx[0]);
|
m_coinbase_txns.push_back(b.vtx[0]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -234,6 +263,9 @@ CBlock TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransa
|
|||||||
TestChain100Setup::~TestChain100Setup()
|
TestChain100Setup::~TestChain100Setup()
|
||||||
{
|
{
|
||||||
gArgs.ForceSetArg("-segwitheight", "0");
|
gArgs.ForceSetArg("-segwitheight", "0");
|
||||||
|
if (m_deterministic) {
|
||||||
|
SetMockTime(0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(const CMutableTransaction& tx) const
|
CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(const CMutableTransaction& tx) const
|
||||||
|
@@ -78,7 +78,6 @@ struct BasicTestingSetup {
|
|||||||
explicit BasicTestingSetup(const std::string& chainName = CBaseChainParams::MAIN, const std::vector<const char*>& extra_args = {});
|
explicit BasicTestingSetup(const std::string& chainName = CBaseChainParams::MAIN, const std::vector<const char*>& extra_args = {});
|
||||||
~BasicTestingSetup();
|
~BasicTestingSetup();
|
||||||
|
|
||||||
private:
|
|
||||||
const fs::path m_path_root;
|
const fs::path m_path_root;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -112,7 +111,7 @@ class CScript;
|
|||||||
* Testing fixture that pre-creates a 100-block REGTEST-mode block chain
|
* Testing fixture that pre-creates a 100-block REGTEST-mode block chain
|
||||||
*/
|
*/
|
||||||
struct TestChain100Setup : public RegTestingSetup {
|
struct TestChain100Setup : public RegTestingSetup {
|
||||||
TestChain100Setup();
|
TestChain100Setup(bool deterministic = false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new block with just given transactions, coinbase paying to
|
* Create a new block with just given transactions, coinbase paying to
|
||||||
@@ -121,12 +120,21 @@ struct TestChain100Setup : public RegTestingSetup {
|
|||||||
CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
|
CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
|
||||||
const CScript& scriptPubKey);
|
const CScript& scriptPubKey);
|
||||||
|
|
||||||
|
//! Mine a series of new blocks on the active chain.
|
||||||
|
void mineBlocks(int num_blocks);
|
||||||
|
|
||||||
~TestChain100Setup();
|
~TestChain100Setup();
|
||||||
|
|
||||||
|
bool m_deterministic;
|
||||||
std::vector<CTransactionRef> m_coinbase_txns; // For convenience, coinbase transactions
|
std::vector<CTransactionRef> m_coinbase_txns; // For convenience, coinbase transactions
|
||||||
CKey coinbaseKey; // private/public key needed to spend coinbase transactions
|
CKey coinbaseKey; // private/public key needed to spend coinbase transactions
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
struct TestChain100DeterministicSetup : public TestChain100Setup {
|
||||||
|
TestChain100DeterministicSetup() : TestChain100Setup(true) { }
|
||||||
|
};
|
||||||
|
|
||||||
class CTxMemPoolEntry;
|
class CTxMemPoolEntry;
|
||||||
|
|
||||||
struct TestMemPoolEntryHelper
|
struct TestMemPoolEntryHelper
|
||||||
|
Reference in New Issue
Block a user