diff --git a/src/Makefile.test.include b/src/Makefile.test.include index 5ae3077fc..0d918f082 100644 --- a/src/Makefile.test.include +++ b/src/Makefile.test.include @@ -61,6 +61,7 @@ BITCOIN_TESTS =\ test/miner_tests.cpp \ test/multisig_tests.cpp \ test/claimtrie_tests.cpp \ + test/claimtriecache_tests.cpp \ test/claimtriebranching_tests.cpp \ test/netbase_tests.cpp \ test/pmt_tests.cpp \ diff --git a/src/claimtrie.h b/src/claimtrie.h index 7b6ab254d..f41f5e769 100644 --- a/src/claimtrie.h +++ b/src/claimtrie.h @@ -493,7 +493,7 @@ public: CClaimTrieProof getProofForName(const std::string& name) const; bool finalizeDecrement() const; -private: +protected: CClaimTrie* base; bool fRequireTakeoverHeights; diff --git a/src/test/claimtriecache_tests.cpp b/src/test/claimtriecache_tests.cpp new file mode 100644 index 000000000..15ff033f3 --- /dev/null +++ b/src/test/claimtriecache_tests.cpp @@ -0,0 +1,92 @@ +#include "claimtrie.h" +#include "main.h" +#include "uint256.h" + +#include "test/test_bitcoin.h" +#include +using namespace std; + + +class CClaimTrieCacheTest : public CClaimTrieCache { +public: + CClaimTrieCacheTest(CClaimTrie* base): + CClaimTrieCache(base, false){} + + bool recursiveComputeMerkleHash(CClaimTrieNode* tnCurrent, + std::string sPos) const + { + return CClaimTrieCache::recursiveComputeMerkleHash(tnCurrent, sPos); + } + + bool recursivePruneName(CClaimTrieNode* tnCurrent, unsigned int nPos, std::string sName, bool* pfNullified) const + { + return CClaimTrieCache::recursivePruneName(tnCurrent,nPos,sName, pfNullified); + } + int cacheSize() + { + return cache.size(); + } + + nodeCacheType::iterator getCache(std::string key) + { + return cache.find(key); + } + +}; + +BOOST_FIXTURE_TEST_SUITE(claimtriecache_tests, RegTestingSetup) + + +BOOST_AUTO_TEST_CASE(merklehash_test) +{ + // check empty trie + uint256 one(uint256S("0000000000000000000000000000000000000000000000000000000000000001")); + CClaimTrieCacheTest cc(pclaimTrie); + BOOST_CHECK(one == cc.getMerkleHash()); + + // check trie with only root node + CClaimTrieNode base_node; + cc.recursiveComputeMerkleHash(&base_node, ""); + BOOST_CHECK(one == cc.getMerkleHash()); +} + +BOOST_AUTO_TEST_CASE(recursiveprune_test) +{ + CClaimTrieCacheTest cc(pclaimTrie); + BOOST_CHECK_EQUAL(0, cc.cacheSize()); + + COutPoint outpoint; + uint160 claimId; + CAmount amount(20); + int height = 0; + int validAtHeight = 0; + CClaimValue test_claim(outpoint, claimId, amount, height, validAtHeight); + + CClaimTrieNode base_node; + // base node has a claim, so it should not be pruned + base_node.insertClaim(test_claim); + + // node 1 has a claim so it should not be pruned + CClaimTrieNode node_1; + const char c = 't'; + base_node.children[c] = &node_1; + node_1.insertClaim(test_claim); + // set this just to make sure we get the right CClaimTrieNode back + node_1.nHeightOfLastTakeover = 10; + + //node 2 does not have a claim so it should be pruned + // thus we should find pruned node 1 in cache + CClaimTrieNode node_2; + const char c_2 = 'e'; + node_1.children[c_2] = &node_2; + + cc.recursivePruneName(&base_node, 0, std::string("te"), NULL); + BOOST_CHECK_EQUAL(1, cc.cacheSize()); + nodeCacheType::iterator it = cc.getCache(std::string("t")); + BOOST_CHECK_EQUAL(10, it->second->nHeightOfLastTakeover); + BOOST_CHECK_EQUAL(1, it->second->claims.size()); + BOOST_CHECK_EQUAL(0, it->second->children.size()); +} + + +BOOST_AUTO_TEST_SUITE_END()