2014-01-19 14:42:02 +00:00
|
|
|
/*
|
|
|
|
This file is part of cpp-ethereum.
|
|
|
|
|
|
|
|
cpp-ethereum is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2014-02-06 18:53:46 +00:00
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
2014-01-19 14:42:02 +00:00
|
|
|
(at your option) any later version.
|
|
|
|
|
2014-02-16 10:20:55 +00:00
|
|
|
cpp-ethereum is distributed in the hope that it will be useful,
|
2014-01-19 14:42:02 +00:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2014-02-16 10:41:15 +00:00
|
|
|
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2014-01-19 14:42:02 +00:00
|
|
|
*/
|
|
|
|
/** @file trie.cpp
|
|
|
|
* @author Gav Wood <i@gavwood.com>
|
|
|
|
* @date 2014
|
|
|
|
* Trie test functions.
|
|
|
|
*/
|
|
|
|
|
2014-02-28 12:55:30 +00:00
|
|
|
#include <fstream>
|
2014-01-19 14:42:02 +00:00
|
|
|
#include <random>
|
2014-03-05 16:32:37 +00:00
|
|
|
#include "JsonSpiritHeaders.h"
|
2014-10-07 21:07:56 +00:00
|
|
|
#include <libdevcore/CommonIO.h>
|
2014-09-05 16:24:29 +00:00
|
|
|
#include <libdevcrypto/TrieDB.h>
|
2014-03-04 18:43:27 +00:00
|
|
|
#include "TrieHash.h"
|
|
|
|
#include "MemTrie.h"
|
2014-04-19 17:52:08 +00:00
|
|
|
#include <boost/test/unit_test.hpp>
|
2014-11-03 15:33:02 +00:00
|
|
|
#include "TestHelper.h"
|
2014-04-19 17:52:08 +00:00
|
|
|
|
2014-01-19 14:42:02 +00:00
|
|
|
using namespace std;
|
2014-09-05 15:09:58 +00:00
|
|
|
using namespace dev;
|
2014-01-19 14:42:02 +00:00
|
|
|
|
2014-02-28 12:55:30 +00:00
|
|
|
namespace js = json_spirit;
|
|
|
|
|
2014-09-05 15:09:58 +00:00
|
|
|
namespace dev
|
|
|
|
{
|
|
|
|
namespace test
|
|
|
|
{
|
2014-04-19 17:52:08 +00:00
|
|
|
|
2014-09-05 15:09:58 +00:00
|
|
|
static unsigned fac(unsigned _i)
|
|
|
|
{
|
|
|
|
return _i > 2 ? _i * fac(_i - 1) : _i;
|
2014-04-19 17:52:08 +00:00
|
|
|
}
|
2014-02-28 12:55:30 +00:00
|
|
|
|
2014-09-05 15:09:58 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-28 12:55:30 +00:00
|
|
|
|
2015-03-01 16:47:27 +00:00
|
|
|
using dev::operator <<;
|
|
|
|
|
2014-11-03 15:33:02 +00:00
|
|
|
BOOST_AUTO_TEST_SUITE(TrieTests)
|
|
|
|
|
2015-03-01 21:54:05 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-03 12:00:09 +00:00
|
|
|
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()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-12 20:09:23 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(trie_test_anyorder)
|
2014-02-28 12:55:30 +00:00
|
|
|
{
|
2014-11-03 15:33:02 +00:00
|
|
|
string testPath = test::getTestPath();
|
2014-10-27 16:04:18 +00:00
|
|
|
|
2014-11-03 15:33:02 +00:00
|
|
|
testPath += "/TrieTests";
|
2014-10-27 16:04:18 +00:00
|
|
|
|
2014-04-19 17:52:08 +00:00
|
|
|
cnote << "Testing Trie...";
|
|
|
|
js::mValue v;
|
2014-12-12 14:31:24 +00:00
|
|
|
string s = asString(contents(testPath + "/trieanyorder.json"));
|
|
|
|
BOOST_REQUIRE_MESSAGE(s.length() > 0, "Contents of 'trieanyorder.json' is empty. Have you cloned the 'tests' repo branch develop?");
|
2014-04-19 17:52:08 +00:00
|
|
|
js::read_string(s, v);
|
|
|
|
for (auto& i: v.get_obj())
|
2014-02-28 12:55:30 +00:00
|
|
|
{
|
2014-06-28 17:23:32 +00:00
|
|
|
cnote << i.first;
|
2014-04-19 17:52:08 +00:00
|
|
|
js::mObject& o = i.second.get_obj();
|
|
|
|
vector<pair<string, string>> ss;
|
2014-06-28 17:23:32 +00:00
|
|
|
for (auto i: o["in"].get_obj())
|
|
|
|
{
|
2014-04-19 17:52:08 +00:00
|
|
|
ss.push_back(make_pair(i.first, i.second.get_str()));
|
2014-06-28 17:23:32 +00:00
|
|
|
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)));
|
|
|
|
}
|
2014-09-05 15:09:58 +00:00
|
|
|
for (unsigned j = 0; j < min(1000u, dev::test::fac((unsigned)ss.size())); ++j)
|
2014-02-28 12:55:30 +00:00
|
|
|
{
|
2014-04-19 17:52:08 +00:00
|
|
|
next_permutation(ss.begin(), ss.end());
|
2014-05-30 13:25:13 +00:00
|
|
|
MemoryDB m;
|
|
|
|
GenericTrieDB<MemoryDB> t(&m);
|
2015-03-01 16:47:27 +00:00
|
|
|
MemoryDB hm;
|
|
|
|
HashedGenericTrieDB<MemoryDB> ht(&hm);
|
|
|
|
MemoryDB fm;
|
|
|
|
FatGenericTrieDB<MemoryDB> ft(&fm);
|
2014-04-19 17:52:08 +00:00
|
|
|
t.init();
|
2015-03-01 16:47:27 +00:00
|
|
|
ht.init();
|
|
|
|
ft.init();
|
2014-05-29 22:29:38 +00:00
|
|
|
BOOST_REQUIRE(t.check(true));
|
2015-03-01 16:47:27 +00:00
|
|
|
BOOST_REQUIRE(ht.check(true));
|
|
|
|
BOOST_REQUIRE(ft.check(true));
|
2014-04-19 17:52:08 +00:00
|
|
|
for (auto const& k: ss)
|
2014-05-29 22:29:38 +00:00
|
|
|
{
|
2014-04-19 17:52:08 +00:00
|
|
|
t.insert(k.first, k.second);
|
2015-03-01 16:47:27 +00:00
|
|
|
ht.insert(k.first, k.second);
|
|
|
|
ft.insert(k.first, k.second);
|
2014-05-29 22:29:38 +00:00
|
|
|
BOOST_REQUIRE(t.check(true));
|
2015-03-01 16:47:27 +00:00
|
|
|
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());
|
2014-05-29 22:29:38 +00:00
|
|
|
}
|
|
|
|
BOOST_REQUIRE(!o["root"].is_null());
|
2014-12-12 14:31:24 +00:00
|
|
|
BOOST_CHECK_EQUAL(o["root"].get_str(), "0x" + toHex(t.root().asArray()));
|
2015-03-01 16:47:27 +00:00
|
|
|
BOOST_CHECK_EQUAL(ht.root(), ft.root());
|
2014-02-28 12:55:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-12 20:09:23 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(trie_tests_ordered)
|
|
|
|
{
|
|
|
|
string testPath = test::getTestPath();
|
|
|
|
|
|
|
|
testPath += "/TrieTests";
|
|
|
|
|
|
|
|
cnote << "Testing Trie...";
|
|
|
|
js::mValue v;
|
|
|
|
string s = asString(contents(testPath + "/trietest.json"));
|
|
|
|
BOOST_REQUIRE_MESSAGE(s.length() > 0, "Contents of 'trietest.json' is empty. Have you cloned the 'tests' repo branch develop?");
|
|
|
|
js::read_string(s, v);
|
2015-01-01 20:36:02 +00:00
|
|
|
|
2014-12-12 20:09:23 +00:00
|
|
|
for (auto& i: v.get_obj())
|
|
|
|
{
|
|
|
|
cnote << i.first;
|
|
|
|
js::mObject& o = i.second.get_obj();
|
|
|
|
vector<pair<string, string>> ss;
|
2015-01-01 20:36:02 +00:00
|
|
|
vector<string> keysToBeDeleted;
|
2014-12-12 20:09:23 +00:00
|
|
|
for (auto& i: o["in"].get_array())
|
|
|
|
{
|
|
|
|
vector<string> values;
|
|
|
|
for (auto& s: i.get_array())
|
2015-01-01 20:36:02 +00:00
|
|
|
{
|
|
|
|
if (s.type() == json_spirit::str_type)
|
|
|
|
values.push_back(s.get_str());
|
|
|
|
else if (s.type() == json_spirit::null_type)
|
|
|
|
{
|
|
|
|
// mark entry for deletion
|
|
|
|
values.push_back("");
|
|
|
|
if (!values[0].find("0x"))
|
|
|
|
values[0] = asString(fromHex(values[0].substr(2)));
|
|
|
|
keysToBeDeleted.push_back(values[0]);
|
|
|
|
}
|
|
|
|
else
|
2015-01-01 20:45:00 +00:00
|
|
|
BOOST_FAIL("Bad type (expected string)");
|
2015-01-01 20:36:02 +00:00
|
|
|
}
|
2014-12-12 20:09:23 +00:00
|
|
|
|
2015-01-01 20:45:00 +00:00
|
|
|
BOOST_REQUIRE(values.size() == 2);
|
2014-12-12 20:09:23 +00:00
|
|
|
ss.push_back(make_pair(values[0], values[1]));
|
|
|
|
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)));
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryDB m;
|
|
|
|
GenericTrieDB<MemoryDB> t(&m);
|
2015-03-01 16:47:27 +00:00
|
|
|
MemoryDB hm;
|
|
|
|
HashedGenericTrieDB<MemoryDB> ht(&hm);
|
|
|
|
MemoryDB fm;
|
|
|
|
FatGenericTrieDB<MemoryDB> ft(&fm);
|
2014-12-12 20:09:23 +00:00
|
|
|
t.init();
|
2015-03-01 16:47:27 +00:00
|
|
|
ht.init();
|
|
|
|
ft.init();
|
2014-12-12 20:09:23 +00:00
|
|
|
BOOST_REQUIRE(t.check(true));
|
2015-03-01 16:47:27 +00:00
|
|
|
BOOST_REQUIRE(ht.check(true));
|
|
|
|
BOOST_REQUIRE(ft.check(true));
|
|
|
|
|
2014-12-12 20:09:23 +00:00
|
|
|
for (auto const& k: ss)
|
|
|
|
{
|
2015-01-01 20:36:02 +00:00
|
|
|
if (find(keysToBeDeleted.begin(), keysToBeDeleted.end(), k.first) != keysToBeDeleted.end() && k.second.empty())
|
2015-03-01 16:47:27 +00:00
|
|
|
t.remove(k.first), ht.remove(k.first), ft.remove(k.first);
|
2015-01-01 20:36:02 +00:00
|
|
|
else
|
2015-03-01 16:47:27 +00:00
|
|
|
t.insert(k.first, k.second), ht.insert(k.first, k.second), ft.insert(k.first, k.second);
|
2014-12-12 20:09:23 +00:00
|
|
|
BOOST_REQUIRE(t.check(true));
|
2015-03-01 16:47:27 +00:00
|
|
|
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());
|
2014-12-12 20:09:23 +00:00
|
|
|
}
|
2015-01-01 20:36:02 +00:00
|
|
|
|
2014-12-12 20:09:23 +00:00
|
|
|
BOOST_REQUIRE(!o["root"].is_null());
|
|
|
|
BOOST_CHECK_EQUAL(o["root"].get_str(), "0x" + toHex(t.root().asArray()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-11 19:13:31 +00:00
|
|
|
inline h256 stringMapHash256(StringMap const& _s)
|
|
|
|
{
|
|
|
|
return hash256(_s);
|
|
|
|
}
|
|
|
|
|
2014-05-29 22:29:38 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(moreTrieTests)
|
2014-01-19 14:42:02 +00:00
|
|
|
{
|
2014-05-29 22:29:38 +00:00
|
|
|
cnote << "Testing Trie more...";
|
2014-05-28 09:52:42 +00:00
|
|
|
#if 0
|
2014-02-28 12:55:30 +00:00
|
|
|
// More tests...
|
2014-01-19 14:42:02 +00:00
|
|
|
{
|
2014-05-30 13:25:13 +00:00
|
|
|
MemoryDB m;
|
|
|
|
GenericTrieDB<MemoryDB> t(&m);
|
2014-01-19 14:42:02 +00:00
|
|
|
t.init(); // initialise as empty tree.
|
|
|
|
cout << t;
|
|
|
|
cout << m;
|
|
|
|
cout << t.root() << endl;
|
|
|
|
cout << hash256(StringMap()) << endl;
|
|
|
|
|
|
|
|
t.insert(string("tesz"), string("test"));
|
|
|
|
cout << t;
|
|
|
|
cout << m;
|
|
|
|
cout << t.root() << endl;
|
2014-02-11 19:13:31 +00:00
|
|
|
cout << stringMapHash256({{"test", "test"}}) << endl;
|
2014-01-19 14:42:02 +00:00
|
|
|
|
|
|
|
t.insert(string("tesa"), string("testy"));
|
|
|
|
cout << t;
|
|
|
|
cout << m;
|
|
|
|
cout << t.root() << endl;
|
2014-02-11 19:13:31 +00:00
|
|
|
cout << stringMapHash256({{"test", "test"}, {"te", "testy"}}) << endl;
|
2014-01-19 14:42:02 +00:00
|
|
|
cout << t.at(string("test")) << endl;
|
|
|
|
cout << t.at(string("te")) << endl;
|
|
|
|
cout << t.at(string("t")) << endl;
|
|
|
|
|
|
|
|
t.remove(string("te"));
|
|
|
|
cout << m;
|
|
|
|
cout << t.root() << endl;
|
2014-02-11 19:13:31 +00:00
|
|
|
cout << stringMapHash256({{"test", "test"}}) << endl;
|
2014-01-19 14:42:02 +00:00
|
|
|
|
|
|
|
t.remove(string("test"));
|
|
|
|
cout << m;
|
|
|
|
cout << t.root() << endl;
|
|
|
|
cout << hash256(StringMap()) << endl;
|
|
|
|
}
|
|
|
|
{
|
2014-05-30 13:25:13 +00:00
|
|
|
MemoryDB m;
|
|
|
|
GenericTrieDB<MemoryDB> t(&m);
|
2014-01-19 14:42:02 +00:00
|
|
|
t.init(); // initialise as empty tree.
|
|
|
|
t.insert(string("a"), string("A"));
|
|
|
|
t.insert(string("b"), string("B"));
|
|
|
|
cout << t;
|
|
|
|
cout << m;
|
|
|
|
cout << t.root() << endl;
|
2014-02-11 19:13:31 +00:00
|
|
|
cout << stringMapHash256({{"b", "B"}, {"a", "A"}}) << endl;
|
2014-01-19 14:42:02 +00:00
|
|
|
cout << RLP(rlp256({{"b", "B"}, {"a", "A"}})) << endl;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
MemTrie t;
|
|
|
|
t.insert("dog", "puppy");
|
|
|
|
cout << hex << t.hash256() << endl;
|
|
|
|
cout << RLP(t.rlp()) << endl;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
MemTrie t;
|
|
|
|
t.insert("bed", "d");
|
|
|
|
t.insert("be", "e");
|
|
|
|
cout << hex << t.hash256() << endl;
|
|
|
|
cout << RLP(t.rlp()) << endl;
|
|
|
|
}
|
|
|
|
{
|
2014-02-11 19:13:31 +00:00
|
|
|
cout << hex << stringMapHash256({{"dog", "puppy"}, {"doe", "reindeer"}}) << endl;
|
2014-01-19 14:42:02 +00:00
|
|
|
MemTrie t;
|
|
|
|
t.insert("dog", "puppy");
|
|
|
|
t.insert("doe", "reindeer");
|
|
|
|
cout << hex << t.hash256() << endl;
|
|
|
|
cout << RLP(t.rlp()) << endl;
|
2014-03-04 17:46:26 +00:00
|
|
|
cout << toHex(t.rlp()) << endl;
|
2014-01-19 14:42:02 +00:00
|
|
|
}
|
2014-05-29 22:29:38 +00:00
|
|
|
#endif
|
2014-01-19 14:42:02 +00:00
|
|
|
{
|
2014-05-30 13:25:13 +00:00
|
|
|
MemoryDB m;
|
|
|
|
GenericTrieDB<MemoryDB> d(&m);
|
2014-01-19 14:42:02 +00:00
|
|
|
d.init(); // initialise as empty tree.
|
|
|
|
MemTrie t;
|
|
|
|
StringMap s;
|
|
|
|
|
|
|
|
auto add = [&](char const* a, char const* b)
|
|
|
|
{
|
|
|
|
d.insert(string(a), string(b));
|
|
|
|
t.insert(a, b);
|
|
|
|
s[a] = b;
|
|
|
|
|
2014-05-29 22:29:38 +00:00
|
|
|
/*cout << endl << "-------------------------------" << endl;
|
2014-01-19 14:42:02 +00:00
|
|
|
cout << a << " -> " << b << endl;
|
|
|
|
cout << d;
|
|
|
|
cout << m;
|
|
|
|
cout << d.root() << endl;
|
2014-05-29 22:29:38 +00:00
|
|
|
cout << hash256(s) << endl;*/
|
2014-01-19 14:42:02 +00:00
|
|
|
|
2014-05-29 22:29:38 +00:00
|
|
|
BOOST_REQUIRE(d.check(true));
|
|
|
|
BOOST_REQUIRE_EQUAL(t.hash256(), hash256(s));
|
|
|
|
BOOST_REQUIRE_EQUAL(d.root(), hash256(s));
|
2014-01-19 14:42:02 +00:00
|
|
|
for (auto const& i: s)
|
|
|
|
{
|
2014-01-22 14:08:18 +00:00
|
|
|
(void)i;
|
2014-05-29 22:29:38 +00:00
|
|
|
BOOST_REQUIRE_EQUAL(t.at(i.first), i.second);
|
|
|
|
BOOST_REQUIRE_EQUAL(d.at(i.first), i.second);
|
2014-01-19 14:42:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
auto remove = [&](char const* a)
|
|
|
|
{
|
|
|
|
s.erase(a);
|
|
|
|
t.remove(a);
|
|
|
|
d.remove(string(a));
|
|
|
|
|
2014-05-29 22:29:38 +00:00
|
|
|
/*cout << endl << "-------------------------------" << endl;
|
2014-01-19 14:42:02 +00:00
|
|
|
cout << "X " << a << endl;
|
|
|
|
cout << d;
|
|
|
|
cout << m;
|
|
|
|
cout << d.root() << endl;
|
2014-05-29 22:29:38 +00:00
|
|
|
cout << hash256(s) << endl;*/
|
2014-01-19 14:42:02 +00:00
|
|
|
|
2014-05-29 22:29:38 +00:00
|
|
|
BOOST_REQUIRE(d.check(true));
|
|
|
|
BOOST_REQUIRE(t.at(a).empty());
|
|
|
|
BOOST_REQUIRE(d.at(string(a)).empty());
|
|
|
|
BOOST_REQUIRE_EQUAL(t.hash256(), hash256(s));
|
|
|
|
BOOST_REQUIRE_EQUAL(d.root(), hash256(s));
|
2014-01-19 14:42:02 +00:00
|
|
|
for (auto const& i: s)
|
|
|
|
{
|
2014-01-22 14:08:18 +00:00
|
|
|
(void)i;
|
2014-05-29 22:29:38 +00:00
|
|
|
BOOST_REQUIRE_EQUAL(t.at(i.first), i.second);
|
|
|
|
BOOST_REQUIRE_EQUAL(d.at(i.first), i.second);
|
2014-01-19 14:42:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
add("dogglesworth", "cat");
|
|
|
|
add("doe", "reindeer");
|
|
|
|
remove("dogglesworth");
|
|
|
|
add("horse", "stallion");
|
|
|
|
add("do", "verb");
|
|
|
|
add("doge", "coin");
|
|
|
|
remove("horse");
|
|
|
|
remove("do");
|
|
|
|
remove("doge");
|
|
|
|
remove("doe");
|
2014-05-28 09:52:42 +00:00
|
|
|
}
|
2014-05-29 22:29:38 +00:00
|
|
|
}
|
|
|
|
|
2014-10-16 16:43:48 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(trieLowerBound)
|
|
|
|
{
|
|
|
|
cnote << "Stress-testing Trie.lower_bound...";
|
2014-12-12 12:33:42 +00:00
|
|
|
if (0)
|
2014-10-16 16:43:48 +00:00
|
|
|
{
|
|
|
|
MemoryDB dm;
|
|
|
|
EnforceRefs e(dm, true);
|
|
|
|
GenericTrieDB<MemoryDB> d(&dm);
|
|
|
|
d.init(); // initialise as empty tree.
|
|
|
|
for (int a = 0; a < 20; ++a)
|
|
|
|
{
|
|
|
|
StringMap m;
|
|
|
|
for (int i = 0; i < 50; ++i)
|
|
|
|
{
|
|
|
|
auto k = randomWord();
|
|
|
|
auto v = toString(i);
|
|
|
|
m[k] = v;
|
|
|
|
d.insert(k, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto i: d)
|
|
|
|
{
|
|
|
|
auto it = d.lower_bound(i.first);
|
|
|
|
for (auto iit = d.begin(); iit != d.end(); ++iit)
|
|
|
|
if ((*iit).first.toString() >= i.first.toString())
|
|
|
|
{
|
|
|
|
BOOST_REQUIRE(it == iit);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (unsigned i = 0; i < 100; ++i)
|
|
|
|
{
|
|
|
|
auto k = randomWord();
|
|
|
|
auto it = d.lower_bound(k);
|
|
|
|
for (auto iit = d.begin(); iit != d.end(); ++iit)
|
|
|
|
if ((*iit).first.toString() >= k)
|
|
|
|
{
|
|
|
|
BOOST_REQUIRE(it == iit);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-29 22:29:38 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(trieStess)
|
|
|
|
{
|
|
|
|
cnote << "Stress-testing Trie...";
|
2014-12-12 12:33:42 +00:00
|
|
|
if (0)
|
2014-05-29 21:11:45 +00:00
|
|
|
{
|
2014-05-30 13:25:13 +00:00
|
|
|
MemoryDB m;
|
|
|
|
MemoryDB dm;
|
2014-05-29 22:29:38 +00:00
|
|
|
EnforceRefs e(dm, true);
|
2014-05-30 13:25:13 +00:00
|
|
|
GenericTrieDB<MemoryDB> d(&dm);
|
2014-05-29 21:11:45 +00:00
|
|
|
d.init(); // initialise as empty tree.
|
|
|
|
MemTrie t;
|
2014-05-29 22:29:38 +00:00
|
|
|
BOOST_REQUIRE(d.check(true));
|
2014-05-29 21:11:45 +00:00
|
|
|
for (int a = 0; a < 20; ++a)
|
|
|
|
{
|
|
|
|
StringMap m;
|
2014-05-29 22:29:38 +00:00
|
|
|
for (int i = 0; i < 50; ++i)
|
2014-05-29 21:11:45 +00:00
|
|
|
{
|
|
|
|
auto k = randomWord();
|
|
|
|
auto v = toString(i);
|
2014-05-29 22:29:38 +00:00
|
|
|
m[k] = v;
|
2014-05-29 21:11:45 +00:00
|
|
|
t.insert(k, v);
|
|
|
|
d.insert(k, v);
|
2014-05-29 22:29:38 +00:00
|
|
|
BOOST_REQUIRE_EQUAL(hash256(m), t.hash256());
|
|
|
|
BOOST_REQUIRE_EQUAL(hash256(m), d.root());
|
|
|
|
BOOST_REQUIRE(d.check(true));
|
2014-05-29 21:11:45 +00:00
|
|
|
}
|
|
|
|
while (!m.empty())
|
|
|
|
{
|
|
|
|
auto k = m.begin()->first;
|
2014-05-29 22:29:38 +00:00
|
|
|
auto v = m.begin()->second;
|
2014-05-29 21:11:45 +00:00
|
|
|
d.remove(k);
|
|
|
|
t.remove(k);
|
|
|
|
m.erase(k);
|
2014-05-29 22:29:38 +00:00
|
|
|
if (!d.check(true))
|
|
|
|
{
|
2014-06-28 23:22:48 +00:00
|
|
|
// cwarn << m;
|
2014-05-29 22:29:38 +00:00
|
|
|
for (auto i: d)
|
|
|
|
cwarn << i.first.toString() << i.second.toString();
|
|
|
|
|
2014-05-30 13:25:13 +00:00
|
|
|
MemoryDB dm2;
|
2014-05-29 22:29:38 +00:00
|
|
|
EnforceRefs e2(dm2, true);
|
2014-05-30 13:25:13 +00:00
|
|
|
GenericTrieDB<MemoryDB> d2(&dm2);
|
2014-05-29 22:29:38 +00:00
|
|
|
d2.init(); // initialise as empty tree.
|
|
|
|
for (auto i: d)
|
|
|
|
d2.insert(i.first, i.second);
|
|
|
|
|
|
|
|
cwarn << "Good:" << d2.root();
|
|
|
|
// for (auto i: dm2.get())
|
|
|
|
// cwarn << i.first.abridged() << ": " << RLP(i.second);
|
|
|
|
d2.debugStructure(cerr);
|
|
|
|
cwarn << "Broken:" << d.root(); // Leaves an extension -> extension (3c1... -> 742...)
|
|
|
|
// for (auto i: dm.get())
|
|
|
|
// cwarn << i.first.abridged() << ": " << RLP(i.second);
|
|
|
|
d.debugStructure(cerr);
|
|
|
|
|
|
|
|
d2.insert(k, v);
|
|
|
|
cwarn << "Pres:" << d2.root();
|
|
|
|
// for (auto i: dm2.get())
|
|
|
|
// cwarn << i.first.abridged() << ": " << RLP(i.second);
|
|
|
|
d2.debugStructure(cerr);
|
|
|
|
g_logVerbosity = 99;
|
|
|
|
d2.remove(k);
|
|
|
|
g_logVerbosity = 4;
|
|
|
|
|
|
|
|
cwarn << "Good?" << d2.root();
|
|
|
|
}
|
|
|
|
BOOST_REQUIRE(d.check(true));
|
|
|
|
BOOST_REQUIRE_EQUAL(hash256(m), t.hash256());
|
|
|
|
BOOST_REQUIRE_EQUAL(hash256(m), d.root());
|
2014-05-29 21:11:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-19 14:42:02 +00:00
|
|
|
}
|
|
|
|
|
2014-11-03 15:33:02 +00:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|
|
|
|
|
|
|
|
|