iterator_test.go 13 KB

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