From 2578371a2ad769ee10d45bc51c0fe1d5ae794d1f Mon Sep 17 00:00:00 2001 From: tecnovert Date: Sat, 28 Oct 2017 18:28:41 +0200 Subject: [PATCH] Improved unittests. Clear mocktime, optimise versionbits_tests. --- .travis.yml | 2 +- src/test/DoS_tests.cpp | 2 ++ src/test/test_particl.h | 3 +-- src/test/versionbits_tests.cpp | 23 ++++++++++++++++------- src/util.cpp | 23 +++++++++++------------ src/wallet/test/stake_tests.cpp | 12 +++++------- 6 files changed, 36 insertions(+), 29 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7a07ce74801e5..60a3adafc41da 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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) diff --git a/src/test/DoS_tests.cpp b/src/test/DoS_tests.cpp index 4e785dac62b9d..e96a1d0c9c0aa 100644 --- a/src/test/DoS_tests.cpp +++ b/src/test/DoS_tests.cpp @@ -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() diff --git a/src/test/test_particl.h b/src/test/test_particl.h index 20ccaa41270c1..d0de1391598b1 100644 --- a/src/test/test_particl.h +++ b/src/test/test_particl.h @@ -18,7 +18,6 @@ - #if BOOST_VERSION > 105300 #ifndef BOOST_MESSAGE #define BOOST_MESSAGE(msg) BOOST_TEST_MESSAGE(msg) @@ -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; diff --git a/src/test/versionbits_tests.cpp b/src/test/versionbits_tests.cpp index 0249b133161ac..00c2c3f4f6f69 100644 --- a/src/test/versionbits_tests.cpp +++ b/src/test/versionbits_tests.cpp @@ -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 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(); } @@ -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; @@ -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) @@ -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; diff --git a/src/util.cpp b/src/util.cpp index 0ad83bbff4f64..bafe1a3e56b5c 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -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; }; @@ -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; }; @@ -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; }; @@ -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); }; @@ -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 @@ -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 }; @@ -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; @@ -713,7 +713,6 @@ static CCriticalSection csPathCached; const fs::path &GetDataDir(bool fNetSpecific) { - LOCK(csPathCached); fs::path &path = fNetSpecific ? pathCachedNetSpecific : pathCached; diff --git a/src/wallet/test/stake_tests.cpp b/src/wallet/test/stake_tests.cpp index fa7a6b6fb4309..03853abae135a 100644 --- a/src/wallet/test/stake_tests.cpp +++ b/src/wallet/test/stake_tests.cpp @@ -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; @@ -85,10 +88,6 @@ void StakeNBlocks(CHDWallet *pwallet, size_t nBlocks) nBestHeight = chainActive.Height(); } - boost::shared_ptr coinbaseScript; - boost::shared_ptr rKey(new CReserveKey(pwallet)); - coinbaseScript = rKey; - int64_t nSearchTime = GetAdjustedTime() & ~Params().GetStakeTimestampMask(nBestHeight+1); if (nSearchTime <= pwallet->nLastCoinStakeSearchTime) { @@ -96,7 +95,8 @@ void StakeNBlocks(CHDWallet *pwallet, size_t nBlocks) continue; }; - std::unique_ptr pblocktemplate(BlockAssembler(Params()).CreateNewBlock(coinbaseScript->reserveScript)); + CScript coinbaseScript; + std::unique_ptr pblocktemplate(BlockAssembler(Params()).CreateNewBlock(coinbaseScript)); BOOST_REQUIRE(pblocktemplate.get()); if (pwallet->SignBlock(pblocktemplate.get(), nBestHeight+1, nSearchTime)) @@ -104,9 +104,7 @@ void StakeNBlocks(CHDWallet *pwallet, size_t nBlocks) CBlock *pblock = &pblocktemplate->block; if (CheckStake(pblock)) - { nStaked++; - }; }; if (nStaked >= nBlocks)