aboutsummaryrefslogtreecommitdiffstats
path: root/ethutil/trie_test.go
blob: d8db8a0d6a1e3b3615a8d2437222035a85197a01 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package ethutil

import (
    "bytes"
    "encoding/hex"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/http"
    "reflect"
    "testing"
)

const LONG_WORD = "1234567890abcdefghijklmnopqrstuvwxxzABCEFGHIJKLMNOPQRSTUVWXYZ"

type MemDatabase struct {
    db map[string][]byte
}

func NewMemDatabase() (*MemDatabase, error) {
    db := &MemDatabase{db: make(map[string][]byte)}
    return db, nil
}
func (db *MemDatabase) Put(key []byte, value []byte) {
    db.db[string(key)] = value
}
func (db *MemDatabase) Get(key []byte) ([]byte, error) {
    return db.db[string(key)], nil
}
func (db *MemDatabase) Delete(key []byte) error {
    delete(db.db, string(key))
    return nil
}
func (db *MemDatabase) Print()              {}
func (db *MemDatabase) Close()              {}
func (db *MemDatabase) LastKnownTD() []byte { return nil }

func New() (*MemDatabase, *Trie) {
    db, _ := NewMemDatabase()
    return db, NewTrie(db, "")
}

func TestTrieSync(t *testing.T) {
    db, trie := New()

    trie.Update("dog", LONG_WORD)
    if len(db.db) != 0 {
        t.Error("Expected no data in database")
    }

    trie.Sync()
    if len(db.db) == 0 {
        t.Error("Expected data to be persisted")
    }
}

func TestTrieDirtyTracking(t *testing.T) {
    _, trie := New()
    trie.Update("dog", LONG_WORD)
    if !trie.cache.IsDirty {
        t.Error("Expected trie to be dirty")
    }

    trie.Sync()
    if trie.cache.IsDirty {
        t.Error("Expected trie not to be dirty")
    }

    trie.Update("test", LONG_WORD)
    trie.cache.Undo()
    if trie.cache.IsDirty {
        t.Error("Expected trie not to be dirty")
    }

}

func TestTrieReset(t *testing.T) {
    _, trie := New()

    trie.Update("cat", LONG_WORD)
    if len(trie.cache.nodes) == 0 {
        t.Error("Expected cached nodes")
    }

    trie.cache.Undo()

    if len(trie.cache.nodes) != 0 {
        t.Error("Expected no nodes after undo")
    }
}

func TestTrieGet(t *testing.T) {
    _, trie := New()

    trie.Update("cat", LONG_WORD)
    x := trie.Get("cat")
    if x != LONG_WORD {
        t.Error("expected %s, got %s", LONG_WORD, x)
    }
}

func TestTrieUpdating(t *testing.T) {
    _, trie := New()
    trie.Update("cat", LONG_WORD)
    trie.Update("cat", LONG_WORD+"1")
    x := trie.Get("cat")
    if x != LONG_WORD+"1" {
        t.Error("expected %S, got %s", LONG_WORD+"1", x)
    }
}

func TestTrieCmp(t *testing.T) {
    _, trie1 := New()
    _, trie2 := New()

    trie1.Update("doge", LONG_WORD)
    trie2.Update("doge", LONG_WORD)
    if !trie1.Cmp(trie2) {
        t.Error("Expected tries to be equal")
    }

    trie1.Update("dog", LONG_WORD)
    trie2.Update("cat", LONG_WORD)
    if trie1.Cmp(trie2) {
        t.Errorf("Expected tries not to be equal %x %x", trie1.Root, trie2.Root)
    }
}

func TestTrieDelete(t *testing.T) {
    _, trie := New()
    trie.Update("cat", LONG_WORD)
    exp := trie.Root
    trie.Update("dog", LONG_WORD)
    trie.Delete("dog")
    if !reflect.DeepEqual(exp, trie.Root) {
        t.Errorf("Expected tries to be equal %x : %x", exp, trie.Root)
    }

    trie.Update("dog", LONG_WORD)
    exp = trie.Root
    trie.Update("dude", LONG_WORD)
    trie.Delete("dude")
    if !reflect.DeepEqual(exp, trie.Root) {
        t.Errorf("Expected tries to be equal %x : %x", exp, trie.Root)
    }
}

func TestTrieDeleteWithValue(t *testing.T) {
    _, trie := New()
    trie.Update("c", LONG_WORD)
    exp := trie.Root
    trie.Update("ca", LONG_WORD)
    trie.Update("cat", LONG_WORD)
    trie.Delete("ca")
    trie.Delete("cat")
    if !reflect.DeepEqual(exp, trie.Root) {
        t.Errorf("Expected tries to be equal %x : %x", exp, trie.Root)
    }

}

func TestTriePurge(t *testing.T) {
    _, trie := New()
    trie.Update("c", LONG_WORD)
    trie.Update("ca", LONG_WORD)
    trie.Update("cat", LONG_WORD)

    lenBefore := len(trie.cache.nodes)
    it := trie.NewIterator()
    if num := it.Purge(); num != 3 {
        t.Errorf("Expected purge to return 3, got %d", num)
    }

    if lenBefore == len(trie.cache.nodes) {
        t.Errorf("Expected cached nodes to be deleted")
    }
}

func h(str string) string {
    d, err := hex.DecodeString(str)
    if err != nil {
        panic(err)
    }

    return string(d)
}

func get(in string) (out string) {
    if len(in) > 2 && in[:2] == "0x" {
        out = h(in[2:])
    } else {
        out = in
    }

    return
}

type Test struct {
    Name string
    In   map[string]string
    Root string
}

func CreateTest(name string, data []byte) (Test, error) {
    t := Test{Name: name}
    err := json.Unmarshal(data, &t)
    if err != nil {
        return Test{}, fmt.Errorf("%v", err)
    }

    return t, nil
}

func CreateTests(uri string, cb func(Test)) {
    resp, err := http.Get(uri)
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()

    data, err := ioutil.ReadAll(resp.Body)

    var objmap map[string]*json.RawMessage
    err = json.Unmarshal(data, &objmap)
    if err != nil {
        panic(err)
    }

    for name, testData := range objmap {
        test, err := CreateTest(name, *testData)
        if err != nil {
            panic(err)
        }

        cb(test)
    }
}

func TestRemote(t *testing.T) {
    CreateTests("https://raw.githubusercontent.com/ethereum/tests/develop/trietest.json", func(test Test) {
        _, trie := New()
        for key, value := range test.In {
            trie.Update(get(key), get(value))
        }
        fmt.Printf("%-15s: %x\n", test.Name, trie.Root)

        a := NewValue(h(test.Root)).Bytes()
        b := NewValue(trie.Root).Bytes()
        if bytes.Compare(a, b) != 0 {
            t.Errorf("%-10s: %x %x", test.Name, a, b)
        }
    })
}

func TestTrieReplay(t *testing.T) {
    CreateTests("https://raw.githubusercontent.com/ethereum/tests/develop/trietest.json", func(test Test) {
        _, trie := New()
        for key, value := range test.In {
            trie.Update(get(key), get(value))
        }

        _, trie2 := New()
        trie.NewIterator().Each(func(key string, v *Value) {
            trie2.Update(key, v.Str())
        })

        a := NewValue(trie.Root).Bytes()
        b := NewValue(trie2.Root).Bytes()
        if bytes.Compare(a, b) != 0 {
            t.Errorf("%s %x %x\n", test.Name, trie.Root, trie2.Root)
        }
    })
}