Skip to content

Commit

Permalink
Improved unittests.
Browse files Browse the repository at this point in the history
Clear mocktime, optimise versionbits_tests.
  • Loading branch information
tecnovert committed Oct 28, 2017
1 parent 23ba1ce commit 2578371
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ env:
# Win32
- HOST=i686-w64-mingw32 DPKG_ADD_ARCH="i386" PACKAGES="python3 nsis g++-mingw-w64-i686 wine1.6 bc" DEP_OPTS="NO_QT=1" RUN_UNIT_TESTS=true RUN_TESTS=false GOAL="install" BITCOIN_CONFIG="--enable-reduce-exports"
# 32-bit + dash
- HOST=i686-pc-linux-gnu PACKAGES="g++-multilib bc python3-zmq" DEP_OPTS="NO_QT=1" RUN_TESTS=false GOAL="install" BITCOIN_CONFIG="--enable-zmq --enable-glibc-back-compat --enable-reduce-exports LDFLAGS=-static-libstdc++" USE_SHELL="/bin/dash"
- HOST=i686-pc-linux-gnu PACKAGES="g++-multilib bc python3-zmq" DEP_OPTS="NO_QT=1" RUN_UNIT_TESTS=true RUN_TESTS=false GOAL="install" BITCOIN_CONFIG="--enable-zmq --enable-glibc-back-compat --enable-reduce-exports LDFLAGS=-static-libstdc++" USE_SHELL="/bin/dash"
# Win64
- HOST=x86_64-w64-mingw32 DPKG_ADD_ARCH="i386" DEP_OPTS="NO_QT=1" PACKAGES="python3 nsis g++-mingw-w64-x86-64 wine1.6 bc" RUN_UNIT_TESTS=true RUN_TESTS=false GOAL="install" BITCOIN_CONFIG="--enable-reduce-exports"
# x86_64 Linux (uses qt5 dev package instead of depends Qt to speed up build and avoid timeout)
Expand Down
2 changes: 2 additions & 0 deletions src/test/DoS_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ BOOST_AUTO_TEST_CASE(DoS_bantime)

SetMockTime(nStartTime+60*60*24+1);
BOOST_CHECK(!connman->IsBanned(addr));

SetMockTime(0);
}

CTransactionRef RandomOrphan()
Expand Down
3 changes: 1 addition & 2 deletions src/test/test_particl.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@




#if BOOST_VERSION > 105300
#ifndef BOOST_MESSAGE
#define BOOST_MESSAGE(msg) BOOST_TEST_MESSAGE(msg)
Expand Down Expand Up @@ -72,7 +71,7 @@ struct BasicTestingSetup {
*/
class CConnman;
struct TestingSetup: public BasicTestingSetup {
//CCoinsViewDB *pcoinsdbview;
CCoinsViewDB *pcoinsdbview;
fs::path pathTemp;
boost::thread_group threadGroup;
CConnman* connman;
Expand Down
23 changes: 16 additions & 7 deletions src/test/versionbits_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,20 @@ class VersionBitsTester
// Test counter (to identify failures)
int num;

// Optimise by removing new/delete, speeds up test in some cases (Travis CI win32)

std::vector<CBlockIndex> vblocks;
size_t nMaxHeight = 0;
size_t n = 0;

public:
VersionBitsTester() : num(0) {}
VersionBitsTester(size_t nMaxHeight_) : num(0), nMaxHeight(nMaxHeight_) {vblocks.resize(nMaxHeight);}

VersionBitsTester& Reset() {
for (unsigned int i = 0; i < vpblock.size(); i++) {
delete vpblock[i];
}
//for (unsigned int i = 0; i < vpblock.size(); i++) {
// delete vpblock[i];
//}
n = 0;
for (unsigned int i = 0; i < CHECKERS; i++) {
checker[i] = TestConditionChecker();
}
Expand All @@ -66,8 +73,10 @@ class VersionBitsTester
}

VersionBitsTester& Mine(unsigned int height, int32_t nTime, int32_t nVersion) {
assert(nMaxHeight >= height);
while (vpblock.size() < height) {
CBlockIndex* pindex = new CBlockIndex();
//CBlockIndex* pindex = new CBlockIndex();
CBlockIndex* pindex = &vblocks[n++];
pindex->nHeight = vpblock.size();
pindex->pprev = vpblock.size() > 0 ? vpblock.back() : nullptr;
pindex->nTime = nTime;
Expand Down Expand Up @@ -147,7 +156,7 @@ BOOST_AUTO_TEST_CASE(versionbits_test)
{
for (int i = 0; i < 64; i++) {
// DEFINED -> FAILED
VersionBitsTester().TestDefined().TestStateSinceHeight(0)
VersionBitsTester(24000).TestDefined().TestStateSinceHeight(0)
.Mine(1, TestTime(1), 0x100).TestDefined().TestStateSinceHeight(0)
.Mine(11, TestTime(11), 0x100).TestDefined().TestStateSinceHeight(0)
.Mine(989, TestTime(989), 0x100).TestDefined().TestStateSinceHeight(0)
Expand Down Expand Up @@ -248,7 +257,7 @@ BOOST_AUTO_TEST_CASE(versionbits_computeblockversion)
// In the first chain, test that the bit is set by CBV until it has failed.
// In the second chain, test the bit is set by CBV while STARTED and
// LOCKED-IN, and then no longer set while ACTIVE.
VersionBitsTester firstChain, secondChain;
VersionBitsTester firstChain(16000), secondChain(16000);

// Start generating blocks before nStartTime
int64_t nTime = nStartTime - 1;
Expand Down
23 changes: 11 additions & 12 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,14 +448,14 @@ void *memrchr(const void *s, int c, size_t n)
{
if (n < 1)
return NULL;

unsigned char *cp = (unsigned char*) s + n;

do {
if (*(--cp) == (unsigned char) c)
return (void*) cp;
} while (--n != 0);

return NULL;
};

Expand All @@ -465,11 +465,11 @@ int memcmp_nta(const void *cs, const void *ct, size_t count)
{
const unsigned char *su1, *su2;
int res = 0;

for (su1 = (unsigned char*)cs, su2 = (unsigned char*)ct;
0 < count; ++su1, ++su2, count--)
res |= (*su1 ^ *su2);

return res;
};

Expand Down Expand Up @@ -500,13 +500,13 @@ bool GetStringBool(const std::string &value, bool &fOut)
fOut = true;
return true;
};

if (IsStringBoolNegative(value))
{
fOut = false;
return true;
};

return false;
};

Expand Down Expand Up @@ -535,7 +535,7 @@ std::string BytesReadable(uint64_t nBytes)
return strprintf("%.2f MB", nBytes/1024.0/1024.0);
if (nBytes >= 1024)
return strprintf("%.2f KB", nBytes/1024.0);

return strprintf("%d B", nBytes);
};

Expand All @@ -546,7 +546,7 @@ static bool icompare_pred(unsigned char a, unsigned char b)
bool stringsMatchI(const std::string &sString, const std::string &sFind, int type)
{
// case insensitive

switch (type)
{
case 0: // full match
Expand All @@ -559,7 +559,7 @@ bool stringsMatchI(const std::string &sString, const std::string &sFind, int typ
return sString.length() >= sFind.length()
&& std::equal(sFind.begin(), sFind.end(), sString.begin(), icompare_pred);
};

return 0; // unknown type
};

Expand All @@ -578,7 +578,7 @@ std::string &TrimQuotes(std::string &s)
return s;
if (s.front() == '"')
s.erase(0, 1);

size_t n = s.size();
if (n < 1)
return s;
Expand Down Expand Up @@ -713,7 +713,6 @@ static CCriticalSection csPathCached;

const fs::path &GetDataDir(bool fNetSpecific)
{

LOCK(csPathCached);

fs::path &path = fNetSpecific ? pathCachedNetSpecific : pathCached;
Expand Down
12 changes: 5 additions & 7 deletions src/wallet/test/stake_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ struct StakeTestingSetup: public TestingSetup {
RegisterHDWalletRPCCommands(tableRPC);
ECC_Start_Stealth();
ECC_Start_Blinding();
::pcoinsdbview = pcoinsdbview;
SetMockTime(0);
}

~StakeTestingSetup()
{
::pcoinsdbview = nullptr;
UnregisterValidationInterface(pwalletMain);
delete pwalletMain;
pwalletMain = nullptr;
Expand Down Expand Up @@ -85,28 +88,23 @@ void StakeNBlocks(CHDWallet *pwallet, size_t nBlocks)
nBestHeight = chainActive.Height();
}

boost::shared_ptr<CReserveScript> coinbaseScript;
boost::shared_ptr<CReserveKey> rKey(new CReserveKey(pwallet));
coinbaseScript = rKey;

int64_t nSearchTime = GetAdjustedTime() & ~Params().GetStakeTimestampMask(nBestHeight+1);
if (nSearchTime <= pwallet->nLastCoinStakeSearchTime)
{
std::this_thread::sleep_for(std::chrono::milliseconds(250));
continue;
};

std::unique_ptr<CBlockTemplate> pblocktemplate(BlockAssembler(Params()).CreateNewBlock(coinbaseScript->reserveScript));
CScript coinbaseScript;
std::unique_ptr<CBlockTemplate> pblocktemplate(BlockAssembler(Params()).CreateNewBlock(coinbaseScript));
BOOST_REQUIRE(pblocktemplate.get());

if (pwallet->SignBlock(pblocktemplate.get(), nBestHeight+1, nSearchTime))
{
CBlock *pblock = &pblocktemplate->block;

if (CheckStake(pblock))
{
nStaked++;
};
};

if (nStaked >= nBlocks)
Expand Down

0 comments on commit 2578371

Please sign in to comment.