Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch testing framework from MAIN to new UNITTEST network #4845

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Makefile.test.include
Expand Up @@ -50,6 +50,7 @@ BITCOIN_TESTS =\
test/key_tests.cpp \
test/main_tests.cpp \
test/miner_tests.cpp \
test/blockv2_tests.cpp \
test/mruset_tests.cpp \
test/multisig_tests.cpp \
test/netbase_tests.cpp \
Expand Down
45 changes: 45 additions & 0 deletions src/chainparams.cpp
Expand Up @@ -214,8 +214,50 @@ class CRegTestParams : public CTestNetParams {
};
static CRegTestParams regTestParams;

//
// Regression test
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to s/Reg/Unit/ here to keep things distinct. Regtest is for programs that drive the app externally over the wire, unit tests is for, well, unit tests.

//
class CUnitTestParams : public CMainParams, public CModifiableParams {
public:
CUnitTestParams() {
networkID = CBaseChainParams::UNITTEST;
strNetworkID = "unittest";
nDefaultPort = 18445;
vFixedSeeds.clear();
vSeeds.clear(); // Regtest mode doesn't have any DNS seeds.

fRequireRPCPassword = false;
fMiningRequiresPeers = false;
fDefaultCheckMemPool = true;
fAllowMinDifficultyBlocks = false;
fMineBlocksOnDemand = true;
fSkipProofOfWorkCheck = false;
}
virtual bool SkipProofOfWorkCheck() const { return fSkipProofOfWorkCheck; }
protected:
bool fSkipProofOfWorkCheck;
public:
// Published setters to allow changing values in unit test cases
virtual void setSubsidyHalvingInterval(int anSubsidyHalvingInterval) { nSubsidyHalvingInterval=anSubsidyHalvingInterval; }
virtual void setEnforceBlockUpgradeMajority(int anEnforceBlockUpgradeMajority) { nEnforceBlockUpgradeMajority=anEnforceBlockUpgradeMajority; }
virtual void setRejectBlockOutdatedMajority(int anRejectBlockOutdatedMajority) { nRejectBlockOutdatedMajority=anRejectBlockOutdatedMajority; }
virtual void setToCheckBlockUpgradeMajority(int anToCheckBlockUpgradeMajority) { nToCheckBlockUpgradeMajority=anToCheckBlockUpgradeMajority; }
virtual void setDefaultCheckMemPool(bool aDefaultCheckMemPool) { fDefaultCheckMemPool=aDefaultCheckMemPool; }
virtual void setAllowMinDifficultyBlocks(bool aAllowMinDifficultyBlocks) { fAllowMinDifficultyBlocks=aAllowMinDifficultyBlocks; }
virtual void setSkipProofOfWorkCheck(bool aSkipProofOfWorkCheck) { fSkipProofOfWorkCheck = aSkipProofOfWorkCheck; }
};
static CUnitTestParams unitTestParams;


static CChainParams *pCurrentParams = 0;

CModifiableParams *ModifiableParams()
{
assert(pCurrentParams);
assert(pCurrentParams==&unitTestParams);
return (CModifiableParams*)&unitTestParams;
}

const CChainParams &Params() {
assert(pCurrentParams);
return *pCurrentParams;
Expand All @@ -233,6 +275,9 @@ void SelectParams(CBaseChainParams::Network network) {
case CBaseChainParams::REGTEST:
pCurrentParams = &regTestParams;
break;
case CBaseChainParams::UNITTEST:
pCurrentParams = &unitTestParams;
break;
default:
assert(false && "Unimplemented network");
return;
Expand Down
22 changes: 22 additions & 0 deletions src/chainparams.h
Expand Up @@ -61,6 +61,8 @@ class CChainParams
bool DefaultCheckMemPool() const { return fDefaultCheckMemPool; }
/* Allow mining of a min-difficulty block */
bool AllowMinDifficultyBlocks() const { return fAllowMinDifficultyBlocks; }
/* Skip proof-of-work check: allow mining of any difficulty block */
virtual bool SkipProofOfWorkCheck() const { return false; }
/* Make standard checks */
bool RequireStandard() const { return fRequireStandard; }
int64_t TargetTimespan() const { return nTargetTimespan; }
Expand Down Expand Up @@ -105,12 +107,32 @@ class CChainParams
bool fMineBlocksOnDemand;
};

/** Modifiable parameters interface is used by test cases to adapt the parameters in order
*** to test specific features more easily. Test cases should always restore the previous
*** values after finalization.
**/

class CModifiableParams {
public:
// Published setters to allow changing values in unit test cases
virtual void setSubsidyHalvingInterval(int anSubsidyHalvingInterval) =0;
virtual void setEnforceBlockUpgradeMajority(int anEnforceBlockUpgradeMajority)=0;
virtual void setRejectBlockOutdatedMajority(int anRejectBlockOutdatedMajority)=0;
virtual void setToCheckBlockUpgradeMajority(int anToCheckBlockUpgradeMajority)=0;
virtual void setDefaultCheckMemPool(bool aDefaultCheckMemPool)=0;
virtual void setAllowMinDifficultyBlocks(bool aAllowMinDifficultyBlocks)=0;
virtual void setSkipProofOfWorkCheck(bool aSkipProofOfWorkCheck)=0;
};


/**
* Return the currently selected parameters. This won't change after app startup
* outside of the unit tests.
*/
const CChainParams &Params();

CModifiableParams *ModifiableParams();

/** Sets the params returned by Params() to those for the given network. */
void SelectParams(CBaseChainParams::Network network);

Expand Down
15 changes: 15 additions & 0 deletions src/chainparamsbase.cpp
Expand Up @@ -51,6 +51,18 @@ class CBaseRegTestParams : public CBaseTestNetParams {
};
static CBaseRegTestParams regTestParams;

//
// Unit test
//
class CBaseUnitTestParams : public CBaseMainParams {
public:
CBaseUnitTestParams() {
networkID = CBaseChainParams::UNITTEST;
strDataDir = "unittest";
}
};
static CBaseUnitTestParams unitTestParams;

static CBaseChainParams *pCurrentBaseParams = 0;

const CBaseChainParams &BaseParams() {
Expand All @@ -69,6 +81,9 @@ void SelectBaseParams(CBaseChainParams::Network network) {
case CBaseChainParams::REGTEST:
pCurrentBaseParams = &regTestParams;
break;
case CBaseChainParams::UNITTEST:
pCurrentBaseParams = &unitTestParams;
break;
default:
assert(false && "Unimplemented network");
return;
Expand Down
1 change: 1 addition & 0 deletions src/chainparamsbase.h
Expand Up @@ -19,6 +19,7 @@ class CBaseChainParams
MAIN,
TESTNET,
REGTEST,
UNITTEST,

MAX_NETWORK_TYPES
};
Expand Down
2 changes: 2 additions & 0 deletions src/checkpoints.cpp
Expand Up @@ -88,6 +88,8 @@ namespace Checkpoints {
return dataTestnet;
else if (Params().NetworkID() == CBaseChainParams::MAIN)
return data;
else if (Params().NetworkID() == CBaseChainParams::UNITTEST) // UnitTest share the same checkpoints as MAIN
return data;
else
return dataRegtest;
}
Expand Down
1 change: 1 addition & 0 deletions src/main.cpp
Expand Up @@ -2330,6 +2330,7 @@ bool AcceptBlockHeader(CBlockHeader& block, CValidationState& state, CBlockIndex
nHeight = pindexPrev->nHeight+1;

// Check proof of work
if (!Params().SkipProofOfWorkCheck())
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks weird, is it missing an indentation below or some brackets?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or just fold it into the expression with &&

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's missing indentation below.
But this is on purpose: I want critical consensus code not even look as modified so everyone can evaluate security at eyesight.

if (block.nBits != GetNextWorkRequired(pindexPrev, &block))
return state.DoS(100, error("AcceptBlock() : incorrect proof of work"),
REJECT_INVALID, "bad-diffbits");
Expand Down
4 changes: 4 additions & 0 deletions src/pow.cpp
Expand Up @@ -81,6 +81,10 @@ bool CheckProofOfWork(uint256 hash, unsigned int nBits)
bool fNegative;
bool fOverflow;
uint256 bnTarget;

if (Params().SkipProofOfWorkCheck())
return true;

bnTarget.SetCompact(nBits, &fNegative, &fOverflow);

// Check range
Expand Down
4 changes: 2 additions & 2 deletions src/test/base58_tests.cpp
Expand Up @@ -175,7 +175,7 @@ BOOST_AUTO_TEST_CASE(base58_keys_valid_parse)
BOOST_CHECK_MESSAGE(!secret.IsValid(), "IsValid pubkey as privkey:" + strTest);
}
}
SelectParams(CBaseChainParams::MAIN);
SelectParams(CBaseChainParams::UNITTEST);
}

// Goal: check that generated keys match test vectors
Expand Down Expand Up @@ -243,7 +243,7 @@ BOOST_AUTO_TEST_CASE(base58_keys_valid_gen)
CTxDestination nodest = CNoDestination();
BOOST_CHECK(!dummyAddr.Set(nodest));

SelectParams(CBaseChainParams::MAIN);
SelectParams(CBaseChainParams::UNITTEST);
}

// Goal: check that base58 parsing code is robust against a variety of corrupted data
Expand Down