aboutsummaryrefslogtreecommitdiffstats
path: root/trie.cpp
blob: 8fe596a7f60b4ae329df4132bd8faea398863e99 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
    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
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Foobar is distributed in the hope that it will be useful,
    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
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
*/
/** @file trie.cpp
 * @author Gav Wood <i@gavwood.com>
 * @date 2014
 * Trie test functions.
 */

#include <random>
#include <TrieHash.h>
#include <TrieDB.h>
#include <MemTrie.h>
using namespace std;
using namespace eth;

int trieTest()
{
    {
        BasicMap m;
        GenericTrieDB<BasicMap> t(&m);
        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;
        cout << hash256({{"test", "test"}}) << endl;

        t.insert(string("tesa"), string("testy"));
        cout << t;
        cout << m;
        cout << t.root() << endl;
        cout << hash256({{"test", "test"}, {"te", "testy"}}) << endl;
        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;
        cout << hash256({{"test", "test"}}) << endl;

        t.remove(string("test"));
        cout << m;
        cout << t.root() << endl;
        cout << hash256(StringMap()) << endl;
    }
    {
        BasicMap m;
        GenericTrieDB<BasicMap> t(&m);
        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;
        cout << hash256({{"b", "B"}, {"a", "A"}}) << endl;
        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;
    }
    {
        cout << hex << hash256({{"dog", "puppy"}, {"doe", "reindeer"}}) << endl;
        MemTrie t;
        t.insert("dog", "puppy");
        t.insert("doe", "reindeer");
        cout << hex << t.hash256() << endl;
        cout << RLP(t.rlp()) << endl;
        cout << asHex(t.rlp()) << endl;
    }
    {
        BasicMap m;
        GenericTrieDB<BasicMap> d(&m);
        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;

            cout << endl << "-------------------------------" << endl;
            cout << a << " -> " << b << endl;
            cout << d;
            cout << m;
            cout << d.root() << endl;
            cout << hash256(s) << endl;

            assert(t.hash256() == hash256(s));
            assert(d.root() == hash256(s));
            for (auto const& i: s)
            {
                (void)i;
                assert(t.at(i.first) == i.second);
                assert(d.at(i.first) == i.second);
            }
        };

        auto remove = [&](char const* a)
        {
            s.erase(a);
            t.remove(a);
            d.remove(string(a));

            cout << endl << "-------------------------------" << endl;
            cout << "X " << a << endl;
            cout << d;
            cout << m;
            cout << d.root() << endl;
            cout << hash256(s) << endl;

            assert(t.at(a).empty());
            assert(d.at(string(a)).empty());
            assert(t.hash256() == hash256(s));
            assert(d.root() == hash256(s));
            for (auto const& i: s)
            {
                (void)i;
                assert(t.at(i.first) == i.second);
                assert(d.at(i.first) == i.second);
            }
        };

        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");
        for (int a = 0; a < 20; ++a)
        {
            StringMap m;
            for (int i = 0; i < 20; ++i)
            {
                auto k = randomWord();
                auto v = toString(i);
                m.insert(make_pair(k, v));
                t.insert(k, v);
                d.insert(k, v);
                assert(hash256(m) == t.hash256());
                assert(hash256(m) == d.root());
            }
            while (!m.empty())
            {
                auto k = m.begin()->first;
                d.remove(k);
                t.remove(k);
                m.erase(k);
                assert(hash256(m) == t.hash256());
                assert(hash256(m) == d.root());
            }
        }
    }
    return 0;
}