Fix claimtriecache test

Signed-off-by: Anthony Fieroni <bvbfan@abv.bg>
This commit is contained in:
Anthony Fieroni 2019-11-26 19:02:09 +02:00 committed by Brannon King
parent 0bc44cac0e
commit 02091d564b
3 changed files with 21 additions and 20 deletions

View file

@ -38,7 +38,7 @@ CUint256 getValueHash(const CTxOutPoint& outPoint, int nHeightOfLastTakeover)
} }
static const sqlite::sqlite_config sharedConfig { static const sqlite::sqlite_config sharedConfig {
sqlite::OpenFlags::READWRITE | sqlite::OpenFlags::CREATE, // TODO: test with this: | sqlite::OpenFlags::SHAREDCACHE, sqlite::OpenFlags::READWRITE | sqlite::OpenFlags::CREATE,
nullptr, sqlite::Encoding::UTF8 nullptr, sqlite::Encoding::UTF8
}; };
@ -51,7 +51,7 @@ CClaimTrie::CClaimTrie(bool fWipe, int height,
int64_t nAllClaimsInMerkleForkHeight, int64_t nAllClaimsInMerkleForkHeight,
int proportionalDelayFactor) : int proportionalDelayFactor) :
nNextHeight(height), nNextHeight(height),
db(dataDir + "/claims.sqlite", sharedConfig), dbFile(dataDir + "/claims.sqlite"), db(dbFile, sharedConfig),
nProportionalDelayFactor(proportionalDelayFactor), nProportionalDelayFactor(proportionalDelayFactor),
nNormalizedNameForkHeight(nNormalizedNameForkHeight), nNormalizedNameForkHeight(nNormalizedNameForkHeight),
nOriginalClaimExpirationTime(nOriginalClaimExpirationTime), nOriginalClaimExpirationTime(nOriginalClaimExpirationTime),
@ -493,7 +493,7 @@ bool CClaimTrieCacheBase::flush()
} }
CClaimTrieCacheBase::CClaimTrieCacheBase(CClaimTrie* base) CClaimTrieCacheBase::CClaimTrieCacheBase(CClaimTrie* base)
: base(base), db(base->db), transacting(false), : base(base), db(base->dbFile, sharedConfig), transacting(false),
childHashQuery(db << "SELECT name, hash, IFNULL(takeoverHeight, 0) FROM nodes WHERE parent = ? ORDER BY name"), childHashQuery(db << "SELECT name, hash, IFNULL(takeoverHeight, 0) FROM nodes WHERE parent = ? ORDER BY name"),
claimHashQuery(db << "SELECT c.txID, c.txN, c.claimID, c.blockHeight, c.validHeight, c.amount, " claimHashQuery(db << "SELECT c.txID, c.txN, c.claimID, c.blockHeight, c.validHeight, c.amount, "
"(SELECT IFNULL(SUM(s.amount),0)+c.amount FROM supports s WHERE s.supportedClaimID = c.claimID " "(SELECT IFNULL(SUM(s.amount),0)+c.amount FROM supports s WHERE s.supportedClaimID = c.claimID "

View file

@ -45,6 +45,7 @@ public:
protected: protected:
int nNextHeight; int nNextHeight;
const std::string dbFile;
sqlite::database db; sqlite::database db;
const int nProportionalDelayFactor; const int nProportionalDelayFactor;

View file

@ -16,6 +16,7 @@ class CClaimTrieCacheTest : public CClaimTrieCacheBase
public: public:
explicit CClaimTrieCacheTest(CClaimTrie* base): CClaimTrieCacheBase(base) explicit CClaimTrieCacheTest(CClaimTrie* base): CClaimTrieCacheBase(base)
{ {
nNextHeight = 2;
} }
bool insertClaimIntoTrie(const std::string& key, const CClaimValue& value) bool insertClaimIntoTrie(const std::string& key, const CClaimValue& value)
@ -108,13 +109,12 @@ BOOST_AUTO_TEST_CASE(merkle_hash_multiple_test)
CUint256 hash4; CUint256 hash4;
hash4.SetHex("c73232a755bf015f22eaa611b283ff38100f2a23fb6222e86eca363452ba0c51"); hash4.SetHex("c73232a755bf015f22eaa611b283ff38100f2a23fb6222e86eca363452ba0c51");
CClaimTrie master(false, 2, 1);
{ {
CClaimTrieCacheTest ntState(&master); CClaimTrieCacheTest ntState(pclaimTrie);
ntState.insertClaimIntoTrie(std::string("test"), CClaimValue(tx1OutPoint, {}, 50, 0, 0)); ntState.insertClaimIntoTrie(std::string("test"), CClaimValue(tx1OutPoint, {}, 50, 0, 0));
ntState.insertClaimIntoTrie(std::string("test2"), CClaimValue(tx2OutPoint, {}, 50, 0, 0)); ntState.insertClaimIntoTrie(std::string("test2"), CClaimValue(tx2OutPoint, {}, 50, 0, 0));
BOOST_CHECK(master.empty()); BOOST_CHECK(pclaimTrie->empty());
BOOST_CHECK_EQUAL(ntState.getTotalClaimsInTrie(), 2U); BOOST_CHECK_EQUAL(ntState.getTotalClaimsInTrie(), 2U);
BOOST_CHECK_EQUAL(ntState.getMerkleHash(), hash1); BOOST_CHECK_EQUAL(ntState.getMerkleHash(), hash1);
@ -128,12 +128,12 @@ BOOST_AUTO_TEST_CASE(merkle_hash_multiple_test)
BOOST_CHECK_EQUAL(ntState.getMerkleHash(), hash2); BOOST_CHECK_EQUAL(ntState.getMerkleHash(), hash2);
ntState.flush(); ntState.flush();
BOOST_CHECK(!master.empty()); BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK_EQUAL(ntState.getMerkleHash(), hash2); BOOST_CHECK_EQUAL(ntState.getMerkleHash(), hash2);
BOOST_CHECK(ntState.checkConsistency()); BOOST_CHECK(ntState.checkConsistency());
} }
{ {
CClaimTrieCacheTest ntState1(&master); CClaimTrieCacheTest ntState1(pclaimTrie);
ntState1.removeClaimFromTrie(std::string("test"), tx1OutPoint); ntState1.removeClaimFromTrie(std::string("test"), tx1OutPoint);
ntState1.removeClaimFromTrie(std::string("test2"), tx2OutPoint); ntState1.removeClaimFromTrie(std::string("test2"), tx2OutPoint);
ntState1.removeClaimFromTrie(std::string("test"), tx3OutPoint); ntState1.removeClaimFromTrie(std::string("test"), tx3OutPoint);
@ -142,7 +142,7 @@ BOOST_AUTO_TEST_CASE(merkle_hash_multiple_test)
BOOST_CHECK_EQUAL(ntState1.getMerkleHash(), hash0); BOOST_CHECK_EQUAL(ntState1.getMerkleHash(), hash0);
} }
{ {
CClaimTrieCacheTest ntState2(&master); CClaimTrieCacheTest ntState2(pclaimTrie);
ntState2.insertClaimIntoTrie(std::string("abab"), CClaimValue(tx6OutPoint, {}, 50, 0, 200)); ntState2.insertClaimIntoTrie(std::string("abab"), CClaimValue(tx6OutPoint, {}, 50, 0, 200));
ntState2.removeClaimFromTrie(std::string("test"), tx1OutPoint); ntState2.removeClaimFromTrie(std::string("test"), tx1OutPoint);
@ -150,50 +150,50 @@ BOOST_AUTO_TEST_CASE(merkle_hash_multiple_test)
ntState2.flush(); ntState2.flush();
BOOST_CHECK(!master.empty()); BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK_EQUAL(ntState2.getMerkleHash(), hash3); BOOST_CHECK_EQUAL(ntState2.getMerkleHash(), hash3);
BOOST_CHECK(ntState2.checkConsistency()); BOOST_CHECK(ntState2.checkConsistency());
} }
{ {
CClaimTrieCacheTest ntState3(&master); CClaimTrieCacheTest ntState3(pclaimTrie);
ntState3.insertClaimIntoTrie(std::string("test"), CClaimValue(tx1OutPoint, {}, 50, 0, 0)); ntState3.insertClaimIntoTrie(std::string("test"), CClaimValue(tx1OutPoint, {}, 50, 0, 0));
BOOST_CHECK_EQUAL(ntState3.getMerkleHash(), hash4); BOOST_CHECK_EQUAL(ntState3.getMerkleHash(), hash4);
ntState3.flush(); ntState3.flush();
BOOST_CHECK(!master.empty()); BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK_EQUAL(ntState3.getMerkleHash(), hash4); BOOST_CHECK_EQUAL(ntState3.getMerkleHash(), hash4);
BOOST_CHECK(ntState3.checkConsistency()); BOOST_CHECK(ntState3.checkConsistency());
} }
{ {
CClaimTrieCacheTest ntState4(&master); CClaimTrieCacheTest ntState4(pclaimTrie);
ntState4.removeClaimFromTrie(std::string("abab"), tx6OutPoint); ntState4.removeClaimFromTrie(std::string("abab"), tx6OutPoint);
BOOST_CHECK_EQUAL(ntState4.getMerkleHash(), hash2); BOOST_CHECK_EQUAL(ntState4.getMerkleHash(), hash2);
ntState4.flush(); ntState4.flush();
BOOST_CHECK(!master.empty()); BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK_EQUAL(ntState4.getMerkleHash(), hash2); BOOST_CHECK_EQUAL(ntState4.getMerkleHash(), hash2);
BOOST_CHECK(ntState4.checkConsistency()); BOOST_CHECK(ntState4.checkConsistency());
} }
{ {
CClaimTrieCacheTest ntState5(&master); CClaimTrieCacheTest ntState5(pclaimTrie);
ntState5.removeClaimFromTrie(std::string("test"), tx3OutPoint); ntState5.removeClaimFromTrie(std::string("test"), tx3OutPoint);
BOOST_CHECK_EQUAL(ntState5.getMerkleHash(), hash2); BOOST_CHECK_EQUAL(ntState5.getMerkleHash(), hash2);
ntState5.flush(); ntState5.flush();
BOOST_CHECK(!master.empty()); BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK_EQUAL(ntState5.getMerkleHash(), hash2); BOOST_CHECK_EQUAL(ntState5.getMerkleHash(), hash2);
BOOST_CHECK(ntState5.checkConsistency()); BOOST_CHECK(ntState5.checkConsistency());
} }
{ {
CClaimTrieCacheTest ntState6(&master); CClaimTrieCacheTest ntState6(pclaimTrie);
ntState6.insertClaimIntoTrie(std::string("test"), CClaimValue(tx3OutPoint, {}, 50, 1, 1)); ntState6.insertClaimIntoTrie(std::string("test"), CClaimValue(tx3OutPoint, {}, 50, 1, 1));
BOOST_CHECK_EQUAL(ntState6.getMerkleHash(), hash2); BOOST_CHECK_EQUAL(ntState6.getMerkleHash(), hash2);
ntState6.flush(); ntState6.flush();
BOOST_CHECK(!master.empty()); BOOST_CHECK(!pclaimTrie->empty());
BOOST_CHECK_EQUAL(ntState6.getMerkleHash(), hash2); BOOST_CHECK_EQUAL(ntState6.getMerkleHash(), hash2);
BOOST_CHECK(ntState6.checkConsistency()); BOOST_CHECK(ntState6.checkConsistency());
} }
{ {
CClaimTrieCacheTest ntState7(&master); CClaimTrieCacheTest ntState7(pclaimTrie);
ntState7.removeClaimFromTrie(std::string("test"), tx3OutPoint); ntState7.removeClaimFromTrie(std::string("test"), tx3OutPoint);
ntState7.removeClaimFromTrie(std::string("test"), tx1OutPoint); ntState7.removeClaimFromTrie(std::string("test"), tx1OutPoint);
ntState7.removeClaimFromTrie(std::string("tes"), tx4OutPoint); ntState7.removeClaimFromTrie(std::string("tes"), tx4OutPoint);
@ -201,7 +201,7 @@ BOOST_AUTO_TEST_CASE(merkle_hash_multiple_test)
BOOST_CHECK_EQUAL(ntState7.getMerkleHash(), hash0); BOOST_CHECK_EQUAL(ntState7.getMerkleHash(), hash0);
ntState7.flush(); ntState7.flush();
BOOST_CHECK(master.empty()); BOOST_CHECK(pclaimTrie->empty());
BOOST_CHECK_EQUAL(ntState7.getMerkleHash(), hash0); BOOST_CHECK_EQUAL(ntState7.getMerkleHash(), hash0);
BOOST_CHECK(ntState7.checkConsistency()); BOOST_CHECK(ntState7.checkConsistency());
} }