github.com/annchain/OG@v0.0.9/trie/iterator_test.go (about) 1 // Copyright 2014 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package trie 18 19 import ( 20 "bytes" 21 "fmt" 22 "github.com/annchain/OG/arefactor/og/types" 23 "math/rand" 24 "testing" 25 26 "github.com/annchain/OG/common" 27 "github.com/annchain/OG/ogdb" 28 // "github.com/annchain/OG/common" 29 // ethdb "github.com/annchain/OG/ogdb" 30 ) 31 32 func TestIterator(t *testing.T) { 33 trie := newEmpty() 34 vals := []struct{ k, v string }{ 35 {"do", "verb"}, 36 {"ether", "wookiedoo"}, 37 {"horse", "stallion"}, 38 {"shaman", "horse"}, 39 {"doge", "coin"}, 40 {"dog", "puppy"}, 41 {"somethingveryoddindeedthis is", "myothernodedata"}, 42 } 43 all := make(map[string]string) 44 for _, val := range vals { 45 all[val.k] = val.v 46 trie.Update([]byte(val.k), []byte(val.v)) 47 } 48 trie.Commit(nil) 49 50 found := make(map[string]string) 51 it := NewIterator(trie.NodeIterator(nil)) 52 for it.Next() { 53 found[string(it.Key)] = string(it.Value) 54 } 55 56 for k, v := range all { 57 if found[k] != v { 58 t.Errorf("iterator value mismatch for %s: got %q want %q", k, found[k], v) 59 } 60 } 61 } 62 63 type kv struct { 64 k, v []byte 65 t bool 66 } 67 68 func TestIteratorLargeData(t *testing.T) { 69 trie := newEmpty() 70 vals := make(map[string]*kv) 71 72 for i := byte(0); i < 255; i++ { 73 value := &kv{common.LeftPadBytes([]byte{i}, 32), []byte{i}, false} 74 value2 := &kv{common.LeftPadBytes([]byte{10, i}, 32), []byte{i}, false} 75 trie.Update(value.k, value.v) 76 trie.Update(value2.k, value2.v) 77 vals[string(value.k)] = value 78 vals[string(value2.k)] = value2 79 } 80 81 it := NewIterator(trie.NodeIterator(nil)) 82 for it.Next() { 83 vals[string(it.Key)].t = true 84 } 85 86 var untouched []*kv 87 for _, value := range vals { 88 if !value.t { 89 untouched = append(untouched, value) 90 } 91 } 92 93 if len(untouched) > 0 { 94 t.Errorf("Missed %d nodes", len(untouched)) 95 for _, value := range untouched { 96 t.Error(value) 97 } 98 } 99 } 100 101 // Tests that the node iterator indeed walks over the entire database contents. 102 func TestNodeIteratorCoverage(t *testing.T) { 103 // Create some arbitrary test trie to iterate 104 db, trie, _ := makeTestTrie() 105 106 // Gather all the node hashes found by the iterator 107 hashes := make(map[types.Hash]struct{}) 108 for it := trie.NodeIterator(nil); it.Next(true); { 109 if it.Hash() != (types.Hash{}) { 110 hashes[it.Hash()] = struct{}{} 111 } 112 } 113 // Cross check the hashes and the database itself 114 for hash := range hashes { 115 if _, err := db.Node(hash); err != nil { 116 t.Errorf("failed to retrieve reported node %x: %v", hash, err) 117 } 118 } 119 for hash, obj := range db.nodes { 120 if obj != nil && hash != (types.Hash{}) { 121 if _, ok := hashes[hash]; !ok { 122 t.Errorf("state entry not reported %x", hash) 123 } 124 } 125 } 126 for _, key := range db.diskdb.(*ogdb.MemDatabase).Keys() { 127 if _, ok := hashes[types.BytesToHash(key)]; !ok { 128 t.Errorf("state entry not reported %x", key) 129 } 130 } 131 } 132 133 type kvs struct{ k, v string } 134 135 var testdata1 = []kvs{ 136 {"barb", "ba"}, 137 {"bard", "bc"}, 138 {"bars", "bb"}, 139 {"bar", "b"}, 140 {"fab", "z"}, 141 {"food", "ab"}, 142 {"foos", "aa"}, 143 {"foo", "a"}, 144 } 145 146 var testdata2 = []kvs{ 147 {"aardvark", "c"}, 148 {"bar", "b"}, 149 {"barb", "bd"}, 150 {"bars", "be"}, 151 {"fab", "z"}, 152 {"foo", "a"}, 153 {"foos", "aa"}, 154 {"food", "ab"}, 155 {"jars", "d"}, 156 } 157 158 func TestIteratorSeek(t *testing.T) { 159 trie := newEmpty() 160 for _, val := range testdata1 { 161 trie.Update([]byte(val.k), []byte(val.v)) 162 } 163 164 // Seek to the middle. 165 it := NewIterator(trie.NodeIterator([]byte("fab"))) 166 if err := checkIteratorOrder(testdata1[4:], it); err != nil { 167 t.Fatal(err) 168 } 169 170 // Seek to a non-existent key. 171 it = NewIterator(trie.NodeIterator([]byte("barc"))) 172 if err := checkIteratorOrder(testdata1[1:], it); err != nil { 173 t.Fatal(err) 174 } 175 176 // Seek beyond the end. 177 it = NewIterator(trie.NodeIterator([]byte("z"))) 178 if err := checkIteratorOrder(nil, it); err != nil { 179 t.Fatal(err) 180 } 181 } 182 183 func checkIteratorOrder(want []kvs, it *Iterator) error { 184 for it.Next() { 185 if len(want) == 0 { 186 return fmt.Errorf("didn't expect any more values, got key %q", it.Key) 187 } 188 if !bytes.Equal(it.Key, []byte(want[0].k)) { 189 return fmt.Errorf("wrong key: got %q, want %q", it.Key, want[0].k) 190 } 191 want = want[1:] 192 } 193 if len(want) > 0 { 194 return fmt.Errorf("iterator ended early, want key %q", want[0]) 195 } 196 return nil 197 } 198 199 func TestDifferenceIterator(t *testing.T) { 200 triea := newEmpty() 201 for _, val := range testdata1 { 202 triea.Update([]byte(val.k), []byte(val.v)) 203 } 204 triea.Commit(nil) 205 206 trieb := newEmpty() 207 for _, val := range testdata2 { 208 trieb.Update([]byte(val.k), []byte(val.v)) 209 } 210 trieb.Commit(nil) 211 212 found := make(map[string]string) 213 di, _ := NewDifferenceIterator(triea.NodeIterator(nil), trieb.NodeIterator(nil)) 214 it := NewIterator(di) 215 for it.Next() { 216 found[string(it.Key)] = string(it.Value) 217 } 218 219 all := []struct{ k, v string }{ 220 {"aardvark", "c"}, 221 {"barb", "bd"}, 222 {"bars", "be"}, 223 {"jars", "d"}, 224 } 225 for _, item := range all { 226 if found[item.k] != item.v { 227 t.Errorf("iterator value mismatch for %s: got %v want %v", item.k, found[item.k], item.v) 228 } 229 } 230 if len(found) != len(all) { 231 t.Errorf("iterator count mismatch: got %d values, want %d", len(found), len(all)) 232 } 233 } 234 235 func TestUnionIterator(t *testing.T) { 236 triea := newEmpty() 237 for _, val := range testdata1 { 238 triea.Update([]byte(val.k), []byte(val.v)) 239 } 240 triea.Commit(nil) 241 242 trieb := newEmpty() 243 for _, val := range testdata2 { 244 trieb.Update([]byte(val.k), []byte(val.v)) 245 } 246 trieb.Commit(nil) 247 248 di, _ := NewUnionIterator([]NodeIterator{triea.NodeIterator(nil), trieb.NodeIterator(nil)}) 249 it := NewIterator(di) 250 251 all := []struct{ k, v string }{ 252 {"aardvark", "c"}, 253 {"barb", "ba"}, 254 {"barb", "bd"}, 255 {"bard", "bc"}, 256 {"bars", "bb"}, 257 {"bars", "be"}, 258 {"bar", "b"}, 259 {"fab", "z"}, 260 {"food", "ab"}, 261 {"foos", "aa"}, 262 {"foo", "a"}, 263 {"jars", "d"}, 264 } 265 266 for i, kv := range all { 267 if !it.Next() { 268 t.Errorf("Iterator ends prematurely at element %d", i) 269 } 270 if kv.k != string(it.Key) { 271 t.Errorf("iterator value mismatch for element %d: got key %s want %s", i, it.Key, kv.k) 272 } 273 if kv.v != string(it.Value) { 274 t.Errorf("iterator value mismatch for element %d: got value %s want %s", i, it.Value, kv.v) 275 } 276 } 277 if it.Next() { 278 t.Errorf("Iterator returned extra values.") 279 } 280 } 281 282 func TestIteratorNoDups(t *testing.T) { 283 var tr Trie 284 for _, val := range testdata1 { 285 tr.Update([]byte(val.k), []byte(val.v)) 286 } 287 checkIteratorNoDups(t, tr.NodeIterator(nil), nil) 288 } 289 290 // This test checks that nodeIterator.Next can be retried after inserting missing trie nodes. 291 func TestIteratorContinueAfterErrorDisk(t *testing.T) { testIteratorContinueAfterError(t, false) } 292 func TestIteratorContinueAfterErrorMemonly(t *testing.T) { testIteratorContinueAfterError(t, true) } 293 294 func testIteratorContinueAfterError(t *testing.T, memonly bool) { 295 diskdb := ogdb.NewMemDatabase() 296 triedb := NewDatabase(diskdb) 297 298 tr, _ := New(types.Hash{}, triedb) 299 for _, val := range testdata1 { 300 tr.Update([]byte(val.k), []byte(val.v)) 301 } 302 tr.Commit(nil) 303 if !memonly { 304 triedb.Commit(tr.Hash(), true) 305 } 306 wantNodeCount := checkIteratorNoDups(t, tr.NodeIterator(nil), nil) 307 308 var ( 309 diskKeys [][]byte 310 memKeys types.Hashes 311 ) 312 if memonly { 313 memKeys = triedb.Nodes() 314 } else { 315 diskKeys = diskdb.Keys() 316 } 317 for i := 0; i < 20; i++ { 318 // Create trie that will load all nodes from DB. 319 tr, _ := New(tr.Hash(), triedb) 320 321 // Remove a random node from the database. It can't be the root node 322 // because that one is already loaded. 323 var ( 324 rkey types.Hash 325 rval []byte 326 robj *cachedNode 327 ) 328 for { 329 if memonly { 330 rkey = memKeys[rand.Intn(len(memKeys))] 331 } else { 332 copy(rkey.ToBytes(), diskKeys[rand.Intn(len(diskKeys))]) 333 } 334 if rkey != tr.Hash() { 335 break 336 } 337 } 338 if memonly { 339 robj = triedb.nodes[rkey] 340 delete(triedb.nodes, rkey) 341 } else { 342 rval, _ = diskdb.Get(rkey.ToBytes()) 343 diskdb.Delete(rkey.ToBytes()) 344 } 345 // Iterate until the error is hit. 346 seen := make(map[string]bool) 347 it := tr.NodeIterator(nil) 348 checkIteratorNoDups(t, it, seen) 349 missing, ok := it.Error().(*MissingNodeError) 350 if !ok || missing.NodeHash != rkey { 351 t.Fatal("didn't hit missing node, got", it.Error()) 352 } 353 354 // Add the node back and continue iteration. 355 if memonly { 356 triedb.nodes[rkey] = robj 357 } else { 358 diskdb.Put(rkey.ToBytes(), rval) 359 } 360 checkIteratorNoDups(t, it, seen) 361 if it.Error() != nil { 362 t.Fatal("unexpected error", it.Error()) 363 } 364 if len(seen) != wantNodeCount { 365 t.Fatal("wrong node iteration count, got", len(seen), "want", wantNodeCount) 366 } 367 } 368 } 369 370 // Similar to the test above, this one checks that failure to create nodeIterator at a 371 // certain key prefix behaves correctly when Next is called. The expectation is that Next 372 // should retry seeking before returning true for the first time. 373 func TestIteratorContinueAfterSeekErrorDisk(t *testing.T) { 374 testIteratorContinueAfterSeekError(t, false) 375 } 376 func TestIteratorContinueAfterSeekErrorMemonly(t *testing.T) { 377 testIteratorContinueAfterSeekError(t, true) 378 } 379 380 func testIteratorContinueAfterSeekError(t *testing.T, memonly bool) { 381 // Commit test trie to db, then remove the node containing "bars". 382 diskdb := ogdb.NewMemDatabase() 383 triedb := NewDatabase(diskdb) 384 385 ctr, _ := New(types.Hash{}, triedb) 386 for _, val := range testdata1 { 387 ctr.Update([]byte(val.k), []byte(val.v)) 388 } 389 root, _ := ctr.Commit(nil) 390 if !memonly { 391 triedb.Commit(root, true) 392 } 393 barNodeHash := types.HexToHash("05041990364eb72fcb1127652ce40d8bab765f2bfe53225b1170d276cc101c2e") 394 var ( 395 barNodeBlob []byte 396 barNodeObj *cachedNode 397 ) 398 if memonly { 399 barNodeObj = triedb.nodes[barNodeHash] 400 delete(triedb.nodes, barNodeHash) 401 } else { 402 barNodeBlob, _ = diskdb.Get(barNodeHash.ToBytes()) 403 diskdb.Delete(barNodeHash.ToBytes()) 404 } 405 // Create a new iterator that seeks to "bars". Seeking can't proceed because 406 // the node is missing. 407 tr, _ := New(root, triedb) 408 it := tr.NodeIterator([]byte("bars")) 409 missing, ok := it.Error().(*MissingNodeError) 410 if !ok { 411 t.Fatal("want MissingNodeError, got", it.Error()) 412 } else if missing.NodeHash != barNodeHash { 413 t.Fatal("wrong node missing") 414 } 415 // Reinsert the missing node. 416 if memonly { 417 triedb.nodes[barNodeHash] = barNodeObj 418 } else { 419 diskdb.Put(barNodeHash.ToBytes(), barNodeBlob) 420 } 421 // Check that iteration produces the right set of values. 422 if err := checkIteratorOrder(testdata1[2:], NewIterator(it)); err != nil { 423 t.Fatal(err) 424 } 425 } 426 427 func checkIteratorNoDups(t *testing.T, it NodeIterator, seen map[string]bool) int { 428 if seen == nil { 429 seen = make(map[string]bool) 430 } 431 for it.Next(true) { 432 if seen[string(it.Path())] { 433 t.Fatalf("iterator visited node path %x twice", it.Path()) 434 } 435 seen[string(it.Path())] = true 436 } 437 return len(seen) 438 }