Merge pull request #5030

fbd36d8 Avoid introducing a virtual into CChainParams (Wladimir J. van der Laan)
f0fd00c Switch testing framework from MAIN to new UNITTEST network (SergioDemianLerner)
This commit is contained in:
Wladimir J. van der Laan 2014-10-02 15:27:54 +02:00
commit 45c41c05a0
No known key found for this signature in database
GPG Key ID: 74810B012346C9A6
10 changed files with 95 additions and 4 deletions

View File

@ -115,6 +115,7 @@ public:
fAllowMinDifficultyBlocks = false; fAllowMinDifficultyBlocks = false;
fRequireStandard = true; fRequireStandard = true;
fMineBlocksOnDemand = false; fMineBlocksOnDemand = false;
fSkipProofOfWorkCheck = false;
} }
}; };
static CMainParams mainParams; static CMainParams mainParams;
@ -214,8 +215,46 @@ public:
}; };
static CRegTestParams regTestParams; static CRegTestParams regTestParams;
//
// Unit test
//
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;
}
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 afDefaultCheckMemPool) { fDefaultCheckMemPool=afDefaultCheckMemPool; }
virtual void setAllowMinDifficultyBlocks(bool afAllowMinDifficultyBlocks) { fAllowMinDifficultyBlocks=afAllowMinDifficultyBlocks; }
virtual void setSkipProofOfWorkCheck(bool afSkipProofOfWorkCheck) { fSkipProofOfWorkCheck = afSkipProofOfWorkCheck; }
};
static CUnitTestParams unitTestParams;
static CChainParams *pCurrentParams = 0; static CChainParams *pCurrentParams = 0;
CModifiableParams *ModifiableParams()
{
assert(pCurrentParams);
assert(pCurrentParams==&unitTestParams);
return (CModifiableParams*)&unitTestParams;
}
const CChainParams &Params() { const CChainParams &Params() {
assert(pCurrentParams); assert(pCurrentParams);
return *pCurrentParams; return *pCurrentParams;
@ -229,6 +268,8 @@ CChainParams &Params(CBaseChainParams::Network network) {
return testNetParams; return testNetParams;
case CBaseChainParams::REGTEST: case CBaseChainParams::REGTEST:
return regTestParams; return regTestParams;
case CBaseChainParams::UNITTEST:
return unitTestParams;
default: default:
assert(false && "Unimplemented network"); assert(false && "Unimplemented network");
return mainParams; return mainParams;

View File

@ -61,6 +61,8 @@ public:
bool DefaultCheckMemPool() const { return fDefaultCheckMemPool; } bool DefaultCheckMemPool() const { return fDefaultCheckMemPool; }
/* Allow mining of a min-difficulty block */ /* Allow mining of a min-difficulty block */
bool AllowMinDifficultyBlocks() const { return fAllowMinDifficultyBlocks; } bool AllowMinDifficultyBlocks() const { return fAllowMinDifficultyBlocks; }
/* Skip proof-of-work check: allow mining of any difficulty block */
bool SkipProofOfWorkCheck() const { return fSkipProofOfWorkCheck; }
/* Make standard checks */ /* Make standard checks */
bool RequireStandard() const { return fRequireStandard; } bool RequireStandard() const { return fRequireStandard; }
int64_t TargetTimespan() const { return nTargetTimespan; } int64_t TargetTimespan() const { return nTargetTimespan; }
@ -103,8 +105,27 @@ protected:
bool fAllowMinDifficultyBlocks; bool fAllowMinDifficultyBlocks;
bool fRequireStandard; bool fRequireStandard;
bool fMineBlocksOnDemand; bool fMineBlocksOnDemand;
bool fSkipProofOfWorkCheck;
}; };
/** 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 * Return the currently selected parameters. This won't change after app startup
* outside of the unit tests. * outside of the unit tests.
@ -114,6 +135,9 @@ const CChainParams &Params();
/** Return parameters for the given network. */ /** Return parameters for the given network. */
CChainParams &Params(CBaseChainParams::Network network); CChainParams &Params(CBaseChainParams::Network network);
/** Get modifyable network parameters (UNITTEST only) */
CModifiableParams *ModifiableParams();
/** Sets the params returned by Params() to those for the given network. */ /** Sets the params returned by Params() to those for the given network. */
void SelectParams(CBaseChainParams::Network network); void SelectParams(CBaseChainParams::Network network);

View File

@ -57,6 +57,20 @@ public:
}; };
static CBaseRegTestParams regTestParams; static CBaseRegTestParams regTestParams;
//
// Unit test
//
class CBaseUnitTestParams : public CBaseMainParams
{
public:
CBaseUnitTestParams()
{
networkID = CBaseChainParams::UNITTEST;
strDataDir = "unittest";
}
};
static CBaseUnitTestParams unitTestParams;
static CBaseChainParams* pCurrentBaseParams = 0; static CBaseChainParams* pCurrentBaseParams = 0;
const CBaseChainParams& BaseParams() const CBaseChainParams& BaseParams()
@ -77,6 +91,9 @@ void SelectBaseParams(CBaseChainParams::Network network)
case CBaseChainParams::REGTEST: case CBaseChainParams::REGTEST:
pCurrentBaseParams = &regTestParams; pCurrentBaseParams = &regTestParams;
break; break;
case CBaseChainParams::UNITTEST:
pCurrentBaseParams = &unitTestParams;
break;
default: default:
assert(false && "Unimplemented network"); assert(false && "Unimplemented network");
return; return;

View File

@ -19,6 +19,7 @@ public:
MAIN, MAIN,
TESTNET, TESTNET,
REGTEST, REGTEST,
UNITTEST,
MAX_NETWORK_TYPES MAX_NETWORK_TYPES
}; };

View File

@ -88,6 +88,8 @@ namespace Checkpoints {
return dataTestnet; return dataTestnet;
else if (Params().NetworkID() == CBaseChainParams::MAIN) else if (Params().NetworkID() == CBaseChainParams::MAIN)
return data; return data;
else if (Params().NetworkID() == CBaseChainParams::UNITTEST) // UnitTest share the same checkpoints as MAIN
return data;
else else
return dataRegtest; return dataRegtest;
} }

View File

@ -2305,7 +2305,8 @@ bool AcceptBlockHeader(CBlockHeader& block, CValidationState& state, CBlockIndex
nHeight = pindexPrev->nHeight+1; nHeight = pindexPrev->nHeight+1;
// Check proof of work // Check proof of work
if (block.nBits != GetNextWorkRequired(pindexPrev, &block)) if ((!Params().SkipProofOfWorkCheck()) &&
(block.nBits != GetNextWorkRequired(pindexPrev, &block)))
return state.DoS(100, error("AcceptBlock() : incorrect proof of work"), return state.DoS(100, error("AcceptBlock() : incorrect proof of work"),
REJECT_INVALID, "bad-diffbits"); REJECT_INVALID, "bad-diffbits");

View File

@ -81,6 +81,10 @@ bool CheckProofOfWork(uint256 hash, unsigned int nBits)
bool fNegative; bool fNegative;
bool fOverflow; bool fOverflow;
uint256 bnTarget; uint256 bnTarget;
if (Params().SkipProofOfWorkCheck())
return true;
bnTarget.SetCompact(nBits, &fNegative, &fOverflow); bnTarget.SetCompact(nBits, &fNegative, &fOverflow);
// Check range // Check range

View File

@ -175,7 +175,7 @@ BOOST_AUTO_TEST_CASE(base58_keys_valid_parse)
BOOST_CHECK_MESSAGE(!secret.IsValid(), "IsValid pubkey as privkey:" + strTest); BOOST_CHECK_MESSAGE(!secret.IsValid(), "IsValid pubkey as privkey:" + strTest);
} }
} }
SelectParams(CBaseChainParams::MAIN); SelectParams(CBaseChainParams::UNITTEST);
} }
// Goal: check that generated keys match test vectors // Goal: check that generated keys match test vectors
@ -243,7 +243,7 @@ BOOST_AUTO_TEST_CASE(base58_keys_valid_gen)
CTxDestination nodest = CNoDestination(); CTxDestination nodest = CNoDestination();
BOOST_CHECK(!dummyAddr.Set(nodest)); 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 // Goal: check that base58 parsing code is robust against a variety of corrupted data

View File

@ -253,6 +253,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
chainActive.Tip()->nHeight--; chainActive.Tip()->nHeight--;
SetMockTime(0); SetMockTime(0);
mempool.clear();
BOOST_FOREACH(CTransaction *tx, txFirst) BOOST_FOREACH(CTransaction *tx, txFirst)
delete tx; delete tx;

View File

@ -31,7 +31,7 @@ struct TestingSetup {
TestingSetup() { TestingSetup() {
fPrintToDebugLog = false; // don't want to write to debug.log file fPrintToDebugLog = false; // don't want to write to debug.log file
SelectParams(CBaseChainParams::MAIN); SelectParams(CBaseChainParams::UNITTEST);
noui_connect(); noui_connect();
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
bitdb.MakeMock(); bitdb.MakeMock();