Skip to content

Commit b19bee9

Browse files
committed
[Refactoring][BUG] Never change chain-params after setup in unit tests
Otherwise the block index would be no longer valid, should be discarded, and a new genesis block reloaded.
1 parent 810a880 commit b19bee9

File tree

8 files changed

+35
-20
lines changed

8 files changed

+35
-20
lines changed

src/test/librust/sapling_test_fixture.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "test/librust/sapling_test_fixture.h"
66
#include "sapling/sapling_util.h"
77

8-
SaplingTestingSetup::SaplingTestingSetup() : TestingSetup()
8+
SaplingTestingSetup::SaplingTestingSetup(const std::string& chainName) : TestingSetup(chainName)
99
{
1010
initZKSNARKS(); // init zk-snarks lib
1111
}

src/test/librust/sapling_test_fixture.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
/**
1111
* Testing setup that configures a complete environment for Sapling testing.
1212
*/
13-
struct SaplingTestingSetup : public TestingSetup {
14-
SaplingTestingSetup();
13+
struct SaplingTestingSetup : public TestingSetup
14+
{
15+
SaplingTestingSetup(const std::string& chainName = CBaseChainParams::MAIN);
1516
~SaplingTestingSetup();
1617
};
1718

src/test/miner_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717

1818
#include <boost/test/unit_test.hpp>
1919

20-
BOOST_FIXTURE_TEST_SUITE(miner_tests, WalletTestingSetup)
20+
// future: this should be MAINNET.
21+
BOOST_FIXTURE_TEST_SUITE(miner_tests, WalletRegTestingSetup)
2122

2223
// BOOST_CHECK_EXCEPTION predicates to check the specific validation error
2324
class HasReason {
@@ -73,7 +74,6 @@ struct {
7374
BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
7475
{
7576
// Note that by default, these tests run with size accounting enabled.
76-
SelectParams(CBaseChainParams::REGTEST); // future: this should be MAINNET.
7777
const CChainParams& chainparams = Params();
7878
CScript scriptPubKey = CScript() << OP_DUP << OP_HASH160 << ParseHex("8d5b4f83212214d6ef693e02e6d71969fddad976") << OP_EQUALVERIFY << OP_CHECKSIG;
7979

src/test/test_pivx.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ struct BasicTestingSetup {
4545
* and wallet (if enabled) setup.
4646
*/
4747
class CConnman;
48-
struct TestingSetup: public BasicTestingSetup {
48+
struct TestingSetup: public BasicTestingSetup
49+
{
4950
CCoinsViewDB *pcoinsdbview;
5051
fs::path pathTemp;
5152
boost::thread_group threadGroup;
@@ -56,6 +57,11 @@ struct TestingSetup: public BasicTestingSetup {
5657
~TestingSetup();
5758
};
5859

60+
struct RegTestingSetup : public TestingSetup
61+
{
62+
RegTestingSetup() : TestingSetup(CBaseChainParams::REGTEST) {}
63+
};
64+
5965
class CBlock;
6066
struct CMutableTransaction;
6167
class CScript;
@@ -78,13 +84,15 @@ struct TestChainSetup : public TestingSetup
7884
};
7985

8086
// Testing fixture that pre-creates a 100-block REGTEST-mode blockchain
81-
struct TestChain100Setup : public TestChainSetup {
87+
struct TestChain100Setup : public TestChainSetup
88+
{
8289
TestChain100Setup() : TestChainSetup(100) {}
8390
};
8491

8592
// Testing fixture that pre-creates a 400-block REGTEST-mode blockchain
8693
// all 400 blocks are PoW. PoS starts at height 500
87-
struct TestChain400Setup : public TestChainSetup {
94+
struct TestChain400Setup : public TestChainSetup
95+
{
8896
TestChain400Setup() : TestChainSetup(400) {}
8997
};
9098

src/test/validation_block_tests.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@
1414
#include "validation.h"
1515
#include "validationinterface.h"
1616

17+
1718
#define ASSERT_WITH_MSG(cond, msg) if (!cond) { BOOST_ERROR(msg); }
1819

19-
struct RegtestingSetup : public TestingSetup {
20-
RegtestingSetup() : TestingSetup(CBaseChainParams::REGTEST) {}
21-
};
2220

23-
BOOST_FIXTURE_TEST_SUITE(validation_block_tests, RegtestingSetup)
21+
BOOST_FIXTURE_TEST_SUITE(validation_block_tests, RegTestingSetup)
2422

2523
struct TestSubscriber : public CValidationInterface {
2624
uint256 m_expected_tip;

src/test/validation_tests.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
#include <boost/test/unit_test.hpp>
1313

14-
BOOST_FIXTURE_TEST_SUITE(validation_tests, TestingSetup)
14+
BOOST_AUTO_TEST_SUITE(validation_tests)
1515

16-
BOOST_AUTO_TEST_CASE(test_simple_shielded_invalid)
16+
BOOST_FIXTURE_TEST_CASE(test_simple_shielded_invalid, TestingSetup)
1717
{
1818
CMutableTransaction tx;
1919
tx.nVersion = CTransaction::TxVersion::SAPLING;
@@ -113,10 +113,11 @@ void CheckMempoolZcRejection(CMutableTransaction& mtx)
113113
BOOST_CHECK_EQUAL(state.GetRejectReason(), "bad-tx-with-zc");
114114
}
115115

116-
BOOST_AUTO_TEST_CASE(zerocoin_rejection_tests)
116+
/*
117+
* Running on regtest to have v5 upgrade enforced at block 1 and test in-block zc rejection
118+
*/
119+
BOOST_FIXTURE_TEST_CASE(zerocoin_rejection_tests, RegTestingSetup)
117120
{
118-
// !TODO: fix me
119-
SelectParams(CBaseChainParams::REGTEST);
120121
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_V5_0, Consensus::NetworkUpgrade::ALWAYS_ACTIVE);
121122
const CChainParams& chainparams = Params();
122123

src/wallet/test/wallet_test_fixture.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ void clean()
1919
bitdb.Reset();
2020
}
2121

22-
WalletTestingSetup::WalletTestingSetup(): SaplingTestingSetup()
22+
WalletTestingSetup::WalletTestingSetup(const std::string& chainName):
23+
SaplingTestingSetup(chainName)
2324
{
2425
clean(); // todo: research why we have an initialized bitdb here.
2526
bitdb.MakeMock();

src/wallet/test/wallet_test_fixture.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@
99

1010
/** Testing setup and teardown for wallet.
1111
*/
12-
struct WalletTestingSetup : public SaplingTestingSetup {
13-
WalletTestingSetup();
12+
struct WalletTestingSetup : public SaplingTestingSetup
13+
{
14+
WalletTestingSetup(const std::string& chainName = CBaseChainParams::MAIN);
1415
~WalletTestingSetup();
1516
};
1617

18+
struct WalletRegTestingSetup : public WalletTestingSetup
19+
{
20+
WalletRegTestingSetup() : WalletTestingSetup(CBaseChainParams::REGTEST) {}
21+
};
22+
1723
#endif
1824

0 commit comments

Comments
 (0)