iterator_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. package trie
  17. import (
  18. "bytes"
  19. "encoding/binary"
  20. "fmt"
  21. "math/rand"
  22. "testing"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/core/rawdb"
  25. "github.com/ethereum/go-ethereum/crypto"
  26. "github.com/ethereum/go-ethereum/ethdb"
  27. "github.com/ethereum/go-ethereum/ethdb/memorydb"
  28. )
  29. func TestEmptyIterator(t *testing.T) {
  30. trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase()))
  31. iter := trie.NodeIterator(nil)
  32. seen := make(map[string]struct{})
  33. for iter.Next(true) {
  34. seen[string(iter.Path())] = struct{}{}
  35. }
  36. if len(seen) != 0 {
  37. t.Fatal("Unexpected trie node iterated")
  38. }
  39. }
  40. func TestIterator(t *testing.T) {
  41. db := NewDatabase(rawdb.NewMemoryDatabase())
  42. trie := NewEmpty(db)
  43. vals := []struct{ k, v string }{
  44. {"do", "verb"},
  45. {"ether", "wookiedoo"},
  46. {"horse", "stallion"},
  47. {"shaman", "horse"},
  48. {"doge", "coin"},
  49. {"dog", "puppy"},
  50. {"somethingveryoddindeedthis is", "myothernodedata"},
  51. }
  52. all := make(map[string]string)
  53. for _, val := range vals {
  54. all[val.k] = val.v
  55. trie.Update([]byte(val.k), []byte(val.v))
  56. }
  57. root, nodes, err := trie.Commit(false)
  58. if err != nil {
  59. t.Fatalf("Failed to commit trie %v", err)
  60. }
  61. db.Update(NewWithNodeSet(nodes))
  62. trie, _ = New(common.Hash{}, root, db)
  63. found := make(map[string]string)
  64. it := NewIterator(trie.NodeIterator(nil))
  65. for it.Next() {
  66. found[string(it.Key)] = string(it.Value)
  67. }
  68. for k, v := range all {
  69. if found[k] != v {
  70. t.Errorf("iterator value mismatch for %s: got %q want %q", k, found[k], v)
  71. }
  72. }
  73. }
  74. type kv struct {
  75. k, v []byte
  76. t bool
  77. }
  78. func TestIteratorLargeData(t *testing.T) {
  79. trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase()))
  80. vals := make(map[string]*kv)
  81. for i := byte(0); i < 255; i++ {
  82. value := &kv{common.LeftPadBytes([]byte{i}, 32), []byte{i}, false}
  83. value2 := &kv{common.LeftPadBytes([]byte{10, i}, 32), []byte{i}, false}
  84. trie.Update(value.k, value.v)
  85. trie.Update(value2.k, value2.v)
  86. vals[string(value.k)] = value
  87. vals[string(value2.k)] = value2
  88. }
  89. it := NewIterator(trie.NodeIterator(nil))
  90. for it.Next() {
  91. vals[string(it.Key)].t = true
  92. }
  93. var untouched []*kv
  94. for _, value := range vals {
  95. if !value.t {
  96. untouched = append(untouched, value)
  97. }
  98. }
  99. if len(untouched) > 0 {
  100. t.Errorf("Missed %d nodes", len(untouched))
  101. for _, value := range untouched {
  102. t.Error(value)
  103. }
  104. }
  105. }
  106. // Tests that the node iterator indeed walks over the entire database contents.
  107. func TestNodeIteratorCoverage(t *testing.T) {
  108. // Create some arbitrary test trie to iterate
  109. db, trie, _ := makeTestTrie()
  110. // Gather all the node hashes found by the iterator
  111. hashes := make(map[common.Hash]struct{})
  112. for it := trie.NodeIterator(nil); it.Next(true); {
  113. if it.Hash() != (common.Hash{}) {
  114. hashes[it.Hash()] = struct{}{}
  115. }
  116. }
  117. // Cross check the hashes and the database itself
  118. for hash := range hashes {
  119. if _, err := db.Node(hash); err != nil {
  120. t.Errorf("failed to retrieve reported node %x: %v", hash, err)
  121. }
  122. }
  123. for hash, obj := range db.dirties {
  124. if obj != nil && hash != (common.Hash{}) {
  125. if _, ok := hashes[hash]; !ok {
  126. t.Errorf("state entry not reported %x", hash)
  127. }
  128. }
  129. }
  130. it := db.diskdb.NewIterator(nil, nil)
  131. for it.Next() {
  132. key := it.Key()
  133. if _, ok := hashes[common.BytesToHash(key)]; !ok {
  134. t.Errorf("state entry not reported %x", key)
  135. }
  136. }
  137. it.Release()
  138. }
  139. type kvs struct{ k, v string }
  140. var testdata1 = []kvs{
  141. {"barb", "ba"},
  142. {"bard", "bc"},
  143. {"bars", "bb"},
  144. {"bar", "b"},
  145. {"fab", "z"},
  146. {"food", "ab"},
  147. {"foos", "aa"},
  148. {"foo", "a"},
  149. }
  150. var testdata2 = []kvs{
  151. {"aardvark", "c"},
  152. {"bar", "b"},
  153. {"barb", "bd"},
  154. {"bars", "be"},
  155. {"fab", "z"},
  156. {"foo", "a"},
  157. {"foos", "aa"},
  158. {"food", "ab"},
  159. {"jars", "d"},
  160. }
  161. func TestIteratorSeek(t *testing.T) {
  162. trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase()))
  163. for _, val := range testdata1 {
  164. trie.Update([]byte(val.k), []byte(val.v))
  165. }
  166. // Seek to the middle.
  167. it := NewIterator(trie.NodeIterator([]byte("fab")))
  168. if err := checkIteratorOrder(testdata1[4:], it); err != nil {
  169. t.Fatal(err)
  170. }
  171. // Seek to a non-existent key.
  172. it = NewIterator(trie.NodeIterator([]byte("barc")))
  173. if err := checkIteratorOrder(testdata1[1:], it); err != nil {
  174. t.Fatal(err)
  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. func checkIteratorOrder(want []kvs, it *Iterator) error {
  183. for it.Next() {
  184. if len(want) == 0 {
  185. return fmt.Errorf("didn't expect any more values, got key %q", it.Key)
  186. }
  187. if !bytes.Equal(it.Key, []byte(want[0].k)) {
  188. return fmt.Errorf("wrong key: got %q, want %q", it.Key, want[0].k)
  189. }
  190. want = want[1:]
  191. }
  192. if len(want) > 0 {
  193. return fmt.Errorf("iterator ended early, want key %q", want[0])
  194. }
  195. return nil
  196. }
  197. func TestDifferenceIterator(t *testing.T) {
  198. dba := NewDatabase(rawdb.NewMemoryDatabase())
  199. triea := NewEmpty(dba)
  200. for _, val := range testdata1 {
  201. triea.Update([]byte(val.k), []byte(val.v))
  202. }
  203. rootA, nodesA, _ := triea.Commit(false)
  204. dba.Update(NewWithNodeSet(nodesA))
  205. triea, _ = New(common.Hash{}, rootA, dba)
  206. dbb := NewDatabase(rawdb.NewMemoryDatabase())
  207. trieb := NewEmpty(dbb)
  208. for _, val := range testdata2 {
  209. trieb.Update([]byte(val.k), []byte(val.v))
  210. }
  211. rootB, nodesB, _ := trieb.Commit(false)
  212. dbb.Update(NewWithNodeSet(nodesB))
  213. trieb, _ = New(common.Hash{}, rootB, dbb)
  214. found := make(map[string]string)
  215. di, _ := NewDifferenceIterator(triea.NodeIterator(nil), trieb.NodeIterator(nil))
  216. it := NewIterator(di)
  217. for it.Next() {
  218. found[string(it.Key)] = string(it.Value)
  219. }
  220. all := []struct{ k, v string }{
  221. {"aardvark", "c"},
  222. {"barb", "bd"},
  223. {"bars", "be"},
  224. {"jars", "d"},
  225. }
  226. for _, item := range all {
  227. if found[item.k] != item.v {
  228. t.Errorf("iterator value mismatch for %s: got %v want %v", item.k, found[item.k], item.v)
  229. }
  230. }
  231. if len(found) != len(all) {
  232. t.Errorf("iterator count mismatch: got %d values, want %d", len(found), len(all))
  233. }
  234. }
  235. func TestUnionIterator(t *testing.T) {
  236. dba := NewDatabase(rawdb.NewMemoryDatabase())
  237. triea := NewEmpty(dba)
  238. for _, val := range testdata1 {
  239. triea.Update([]byte(val.k), []byte(val.v))
  240. }
  241. rootA, nodesA, _ := triea.Commit(false)
  242. dba.Update(NewWithNodeSet(nodesA))
  243. triea, _ = New(common.Hash{}, rootA, dba)
  244. dbb := NewDatabase(rawdb.NewMemoryDatabase())
  245. trieb := NewEmpty(dbb)
  246. for _, val := range testdata2 {
  247. trieb.Update([]byte(val.k), []byte(val.v))
  248. }
  249. rootB, nodesB, _ := trieb.Commit(false)
  250. dbb.Update(NewWithNodeSet(nodesB))
  251. trieb, _ = New(common.Hash{}, rootB, dbb)
  252. di, _ := NewUnionIterator([]NodeIterator{triea.NodeIterator(nil), trieb.NodeIterator(nil)})
  253. it := NewIterator(di)
  254. all := []struct{ k, v string }{
  255. {"aardvark", "c"},
  256. {"barb", "ba"},
  257. {"barb", "bd"},
  258. {"bard", "bc"},
  259. {"bars", "bb"},
  260. {"bars", "be"},
  261. {"bar", "b"},
  262. {"fab", "z"},
  263. {"food", "ab"},
  264. {"foos", "aa"},
  265. {"foo", "a"},
  266. {"jars", "d"},
  267. }
  268. for i, kv := range all {
  269. if !it.Next() {
  270. t.Errorf("Iterator ends prematurely at element %d", i)
  271. }
  272. if kv.k != string(it.Key) {
  273. t.Errorf("iterator value mismatch for element %d: got key %s want %s", i, it.Key, kv.k)
  274. }
  275. if kv.v != string(it.Value) {
  276. t.Errorf("iterator value mismatch for element %d: got value %s want %s", i, it.Value, kv.v)
  277. }
  278. }
  279. if it.Next() {
  280. t.Errorf("Iterator returned extra values.")
  281. }
  282. }
  283. func TestIteratorNoDups(t *testing.T) {
  284. tr := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase()))
  285. for _, val := range testdata1 {
  286. tr.Update([]byte(val.k), []byte(val.v))
  287. }
  288. checkIteratorNoDups(t, tr.NodeIterator(nil), nil)
  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. func testIteratorContinueAfterError(t *testing.T, memonly bool) {
  294. diskdb := memorydb.New()
  295. triedb := NewDatabase(diskdb)
  296. tr := NewEmpty(triedb)
  297. for _, val := range testdata1 {
  298. tr.Update([]byte(val.k), []byte(val.v))
  299. }
  300. _, nodes, _ := tr.Commit(false)
  301. triedb.Update(NewWithNodeSet(nodes))
  302. if !memonly {
  303. triedb.Commit(tr.Hash(), true, nil)
  304. }
  305. wantNodeCount := checkIteratorNoDups(t, tr.NodeIterator(nil), nil)
  306. var (
  307. diskKeys [][]byte
  308. memKeys []common.Hash
  309. )
  310. if memonly {
  311. memKeys = triedb.Nodes()
  312. } else {
  313. it := diskdb.NewIterator(nil, nil)
  314. for it.Next() {
  315. diskKeys = append(diskKeys, it.Key())
  316. }
  317. it.Release()
  318. }
  319. for i := 0; i < 20; i++ {
  320. // Create trie that will load all nodes from DB.
  321. tr, _ := New(common.Hash{}, tr.Hash(), triedb)
  322. // Remove a random node from the database. It can't be the root node
  323. // because that one is already loaded.
  324. var (
  325. rkey common.Hash
  326. rval []byte
  327. robj *cachedNode
  328. )
  329. for {
  330. if memonly {
  331. rkey = memKeys[rand.Intn(len(memKeys))]
  332. } else {
  333. copy(rkey[:], diskKeys[rand.Intn(len(diskKeys))])
  334. }
  335. if rkey != tr.Hash() {
  336. break
  337. }
  338. }
  339. if memonly {
  340. robj = triedb.dirties[rkey]
  341. delete(triedb.dirties, rkey)
  342. } else {
  343. rval, _ = diskdb.Get(rkey[:])
  344. diskdb.Delete(rkey[:])
  345. }
  346. // Iterate until the error is hit.
  347. seen := make(map[string]bool)
  348. it := tr.NodeIterator(nil)
  349. checkIteratorNoDups(t, it, seen)
  350. missing, ok := it.Error().(*MissingNodeError)
  351. if !ok || missing.NodeHash != rkey {
  352. t.Fatal("didn't hit missing node, got", it.Error())
  353. }
  354. // Add the node back and continue iteration.
  355. if memonly {
  356. triedb.dirties[rkey] = robj
  357. } else {
  358. diskdb.Put(rkey[:], 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. // Similar to the test above, this one checks that failure to create nodeIterator at a
  370. // certain key prefix behaves correctly when Next is called. The expectation is that Next
  371. // should retry seeking before returning true for the first time.
  372. func TestIteratorContinueAfterSeekErrorDisk(t *testing.T) {
  373. testIteratorContinueAfterSeekError(t, false)
  374. }
  375. func TestIteratorContinueAfterSeekErrorMemonly(t *testing.T) {
  376. testIteratorContinueAfterSeekError(t, true)
  377. }
  378. func testIteratorContinueAfterSeekError(t *testing.T, memonly bool) {
  379. // Commit test trie to db, then remove the node containing "bars".
  380. diskdb := memorydb.New()
  381. triedb := NewDatabase(diskdb)
  382. ctr := NewEmpty(triedb)
  383. for _, val := range testdata1 {
  384. ctr.Update([]byte(val.k), []byte(val.v))
  385. }
  386. root, nodes, _ := ctr.Commit(false)
  387. triedb.Update(NewWithNodeSet(nodes))
  388. if !memonly {
  389. triedb.Commit(root, true, nil)
  390. }
  391. barNodeHash := common.HexToHash("05041990364eb72fcb1127652ce40d8bab765f2bfe53225b1170d276cc101c2e")
  392. var (
  393. barNodeBlob []byte
  394. barNodeObj *cachedNode
  395. )
  396. if memonly {
  397. barNodeObj = triedb.dirties[barNodeHash]
  398. delete(triedb.dirties, barNodeHash)
  399. } else {
  400. barNodeBlob, _ = diskdb.Get(barNodeHash[:])
  401. diskdb.Delete(barNodeHash[:])
  402. }
  403. // Create a new iterator that seeks to "bars". Seeking can't proceed because
  404. // the node is missing.
  405. tr, _ := New(common.Hash{}, root, triedb)
  406. it := tr.NodeIterator([]byte("bars"))
  407. missing, ok := it.Error().(*MissingNodeError)
  408. if !ok {
  409. t.Fatal("want MissingNodeError, got", it.Error())
  410. } else if missing.NodeHash != barNodeHash {
  411. t.Fatal("wrong node missing")
  412. }
  413. // Reinsert the missing node.
  414. if memonly {
  415. triedb.dirties[barNodeHash] = barNodeObj
  416. } else {
  417. diskdb.Put(barNodeHash[:], barNodeBlob)
  418. }
  419. // Check that iteration produces the right set of values.
  420. if err := checkIteratorOrder(testdata1[2:], NewIterator(it)); err != nil {
  421. t.Fatal(err)
  422. }
  423. }
  424. func checkIteratorNoDups(t *testing.T, it NodeIterator, seen map[string]bool) int {
  425. if seen == nil {
  426. seen = make(map[string]bool)
  427. }
  428. for it.Next(true) {
  429. if seen[string(it.Path())] {
  430. t.Fatalf("iterator visited node path %x twice", it.Path())
  431. }
  432. seen[string(it.Path())] = true
  433. }
  434. return len(seen)
  435. }
  436. type loggingDb struct {
  437. getCount uint64
  438. backend ethdb.KeyValueStore
  439. }
  440. func (l *loggingDb) Has(key []byte) (bool, error) {
  441. return l.backend.Has(key)
  442. }
  443. func (l *loggingDb) Get(key []byte) ([]byte, error) {
  444. l.getCount++
  445. return l.backend.Get(key)
  446. }
  447. func (l *loggingDb) Put(key []byte, value []byte) error {
  448. return l.backend.Put(key, value)
  449. }
  450. func (l *loggingDb) Delete(key []byte) error {
  451. return l.backend.Delete(key)
  452. }
  453. func (l *loggingDb) NewBatch() ethdb.Batch {
  454. return l.backend.NewBatch()
  455. }
  456. func (l *loggingDb) NewBatchWithSize(size int) ethdb.Batch {
  457. return l.backend.NewBatchWithSize(size)
  458. }
  459. func (l *loggingDb) NewIterator(prefix []byte, start []byte) ethdb.Iterator {
  460. return l.backend.NewIterator(prefix, start)
  461. }
  462. func (l *loggingDb) NewSnapshot() (ethdb.Snapshot, error) {
  463. return l.backend.NewSnapshot()
  464. }
  465. func (l *loggingDb) Stat(property string) (string, error) {
  466. return l.backend.Stat(property)
  467. }
  468. func (l *loggingDb) Compact(start []byte, limit []byte) error {
  469. return l.backend.Compact(start, limit)
  470. }
  471. func (l *loggingDb) Close() error {
  472. return l.backend.Close()
  473. }
  474. // makeLargeTestTrie create a sample test trie
  475. func makeLargeTestTrie() (*Database, *StateTrie, *loggingDb) {
  476. // Create an empty trie
  477. logDb := &loggingDb{0, memorydb.New()}
  478. triedb := NewDatabase(logDb)
  479. trie, _ := NewStateTrie(common.Hash{}, common.Hash{}, triedb)
  480. // Fill it with some arbitrary data
  481. for i := 0; i < 10000; i++ {
  482. key := make([]byte, 32)
  483. val := make([]byte, 32)
  484. binary.BigEndian.PutUint64(key, uint64(i))
  485. binary.BigEndian.PutUint64(val, uint64(i))
  486. key = crypto.Keccak256(key)
  487. val = crypto.Keccak256(val)
  488. trie.Update(key, val)
  489. }
  490. _, nodes, _ := trie.Commit(false)
  491. triedb.Update(NewWithNodeSet(nodes))
  492. // Return the generated trie
  493. return triedb, trie, logDb
  494. }
  495. // Tests that the node iterator indeed walks over the entire database contents.
  496. func TestNodeIteratorLargeTrie(t *testing.T) {
  497. // Create some arbitrary test trie to iterate
  498. db, trie, logDb := makeLargeTestTrie()
  499. db.Cap(0) // flush everything
  500. // Do a seek operation
  501. trie.NodeIterator(common.FromHex("0x77667766776677766778855885885885"))
  502. // master: 24 get operations
  503. // this pr: 5 get operations
  504. if have, want := logDb.getCount, uint64(5); have != want {
  505. t.Fatalf("Too many lookups during seek, have %d want %d", have, want)
  506. }
  507. }
  508. func TestIteratorNodeBlob(t *testing.T) {
  509. var (
  510. db = memorydb.New()
  511. triedb = NewDatabase(db)
  512. trie = NewEmpty(triedb)
  513. )
  514. vals := []struct{ k, v string }{
  515. {"do", "verb"},
  516. {"ether", "wookiedoo"},
  517. {"horse", "stallion"},
  518. {"shaman", "horse"},
  519. {"doge", "coin"},
  520. {"dog", "puppy"},
  521. {"somethingveryoddindeedthis is", "myothernodedata"},
  522. }
  523. all := make(map[string]string)
  524. for _, val := range vals {
  525. all[val.k] = val.v
  526. trie.Update([]byte(val.k), []byte(val.v))
  527. }
  528. _, nodes, _ := trie.Commit(false)
  529. triedb.Update(NewWithNodeSet(nodes))
  530. triedb.Cap(0)
  531. found := make(map[common.Hash][]byte)
  532. it := trie.NodeIterator(nil)
  533. for it.Next(true) {
  534. if it.Hash() == (common.Hash{}) {
  535. continue
  536. }
  537. found[it.Hash()] = it.NodeBlob()
  538. }
  539. dbIter := db.NewIterator(nil, nil)
  540. defer dbIter.Release()
  541. var count int
  542. for dbIter.Next() {
  543. got, present := found[common.BytesToHash(dbIter.Key())]
  544. if !present {
  545. t.Fatalf("Miss trie node %v", dbIter.Key())
  546. }
  547. if !bytes.Equal(got, dbIter.Value()) {
  548. t.Fatalf("Unexpected trie node want %v got %v", dbIter.Value(), got)
  549. }
  550. count += 1
  551. }
  552. if count != len(found) {
  553. t.Fatal("Find extra trie node via iterator")
  554. }
  555. }