aboutsummaryrefslogtreecommitdiffstats
path: root/trie.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'trie.cpp')
-rw-r--r--trie.cpp126
1 files changed, 124 insertions, 2 deletions
diff --git a/trie.cpp b/trie.cpp
index dd335b4a..9e59dd31 100644
--- a/trie.cpp
+++ b/trie.cpp
@@ -50,8 +50,92 @@ static unsigned fac(unsigned _i)
}
}
+using dev::operator <<;
+
BOOST_AUTO_TEST_SUITE(TrieTests)
+BOOST_AUTO_TEST_CASE(fat_trie)
+{
+ h256 r;
+ MemoryDB fm;
+ {
+ FatGenericTrieDB<MemoryDB> ft(&fm);
+ ft.init();
+ ft.insert(h256("69", h256::FromHex, h256::AlignRight).ref(), h256("414243", h256::FromHex, h256::AlignRight).ref());
+ for (auto i: ft)
+ cnote << i.first << i.second;
+ r = ft.root();
+ }
+ {
+ FatGenericTrieDB<MemoryDB> ft(&fm);
+ ft.setRoot(r);
+ for (auto i: ft)
+ cnote << i.first << i.second;
+ }
+}
+
+BOOST_AUTO_TEST_CASE(hex_encoded_securetrie_test)
+{
+ string testPath = test::getTestPath();
+
+ testPath += "/TrieTests";
+
+ cnote << "Testing Secure Trie...";
+ js::mValue v;
+ string s = asString(contents(testPath + "/hex_encoded_securetrie_test.json"));
+ BOOST_REQUIRE_MESSAGE(s.length() > 0, "Contents of 'hex_encoded_securetrie_test.json' is empty. Have you cloned the 'tests' repo branch develop?");
+ js::read_string(s, v);
+ for (auto& i: v.get_obj())
+ {
+ cnote << i.first;
+ js::mObject& o = i.second.get_obj();
+ vector<pair<string, string>> ss;
+ for (auto i: o["in"].get_obj())
+ {
+ ss.push_back(make_pair(i.first, i.second.get_str()));
+ if (!ss.back().first.find("0x"))
+ ss.back().first = asString(fromHex(ss.back().first.substr(2)));
+ if (!ss.back().second.find("0x"))
+ ss.back().second = asString(fromHex(ss.back().second.substr(2)));
+ }
+ for (unsigned j = 0; j < min(1000000000u, dev::test::fac((unsigned)ss.size())); ++j)
+ {
+ next_permutation(ss.begin(), ss.end());
+ MemoryDB m;
+ GenericTrieDB<MemoryDB> t(&m);
+ MemoryDB hm;
+ HashedGenericTrieDB<MemoryDB> ht(&hm);
+ MemoryDB fm;
+ FatGenericTrieDB<MemoryDB> ft(&fm);
+ t.init();
+ ht.init();
+ ft.init();
+ BOOST_REQUIRE(t.check(true));
+ BOOST_REQUIRE(ht.check(true));
+ BOOST_REQUIRE(ft.check(true));
+ for (auto const& k: ss)
+ {
+ t.insert(k.first, k.second);
+ ht.insert(k.first, k.second);
+ ft.insert(k.first, k.second);
+ BOOST_REQUIRE(t.check(true));
+ BOOST_REQUIRE(ht.check(true));
+ BOOST_REQUIRE(ft.check(true));
+ for (auto i = ft.begin(), j = t.begin(); i != ft.end() && j != t.end(); ++i, ++j)
+ {
+ BOOST_CHECK_EQUAL(i == ft.end(), j == t.end());
+ BOOST_REQUIRE((*i).first.toBytes() == (*j).first.toBytes());
+ BOOST_REQUIRE((*i).second.toBytes() == (*j).second.toBytes());
+ }
+ BOOST_CHECK_EQUAL(ht.root(), ft.root());
+ }
+ BOOST_REQUIRE(!o["root"].is_null());
+ BOOST_CHECK_EQUAL(o["root"].get_str(), "0x" + toHex(ht.root().asArray()));
+ BOOST_CHECK_EQUAL(o["root"].get_str(), "0x" + toHex(ft.root().asArray()));
+ }
+ }
+}
+
BOOST_AUTO_TEST_CASE(trie_test_anyorder)
{
string testPath = test::getTestPath();
@@ -81,15 +165,35 @@ BOOST_AUTO_TEST_CASE(trie_test_anyorder)
next_permutation(ss.begin(), ss.end());
MemoryDB m;
GenericTrieDB<MemoryDB> t(&m);
+ MemoryDB hm;
+ HashedGenericTrieDB<MemoryDB> ht(&hm);
+ MemoryDB fm;
+ FatGenericTrieDB<MemoryDB> ft(&fm);
t.init();
+ ht.init();
+ ft.init();
BOOST_REQUIRE(t.check(true));
+ BOOST_REQUIRE(ht.check(true));
+ BOOST_REQUIRE(ft.check(true));
for (auto const& k: ss)
{
t.insert(k.first, k.second);
+ ht.insert(k.first, k.second);
+ ft.insert(k.first, k.second);
BOOST_REQUIRE(t.check(true));
+ BOOST_REQUIRE(ht.check(true));
+ BOOST_REQUIRE(ft.check(true));
+ for (auto i = ft.begin(), j = t.begin(); i != ft.end() && j != t.end(); ++i, ++j)
+ {
+ BOOST_CHECK_EQUAL(i == ft.end(), j == t.end());
+ BOOST_REQUIRE((*i).first.toBytes() == (*j).first.toBytes());
+ BOOST_REQUIRE((*i).second.toBytes() == (*j).second.toBytes());
+ }
+ BOOST_CHECK_EQUAL(ht.root(), ft.root());
}
BOOST_REQUIRE(!o["root"].is_null());
BOOST_CHECK_EQUAL(o["root"].get_str(), "0x" + toHex(t.root().asArray()));
+ BOOST_CHECK_EQUAL(ht.root(), ft.root());
}
}
}
@@ -141,15 +245,33 @@ BOOST_AUTO_TEST_CASE(trie_tests_ordered)
MemoryDB m;
GenericTrieDB<MemoryDB> t(&m);
+ MemoryDB hm;
+ HashedGenericTrieDB<MemoryDB> ht(&hm);
+ MemoryDB fm;
+ FatGenericTrieDB<MemoryDB> ft(&fm);
t.init();
+ ht.init();
+ ft.init();
BOOST_REQUIRE(t.check(true));
+ BOOST_REQUIRE(ht.check(true));
+ BOOST_REQUIRE(ft.check(true));
+
for (auto const& k: ss)
{
if (find(keysToBeDeleted.begin(), keysToBeDeleted.end(), k.first) != keysToBeDeleted.end() && k.second.empty())
- t.remove(k.first);
+ t.remove(k.first), ht.remove(k.first), ft.remove(k.first);
else
- t.insert(k.first, k.second);
+ t.insert(k.first, k.second), ht.insert(k.first, k.second), ft.insert(k.first, k.second);
BOOST_REQUIRE(t.check(true));
+ BOOST_REQUIRE(ht.check(true));
+ BOOST_REQUIRE(ft.check(true));
+ for (auto i = ft.begin(), j = t.begin(); i != ft.end() && j != t.end(); ++i, ++j)
+ {
+ BOOST_CHECK_EQUAL(i == ft.end(), j == t.end());
+ BOOST_REQUIRE((*i).first.toBytes() == (*j).first.toBytes());
+ BOOST_REQUIRE((*i).second.toBytes() == (*j).second.toBytes());
+ }
+ BOOST_CHECK_EQUAL(ht.root(), ft.root());
}
BOOST_REQUIRE(!o["root"].is_null());