trie_test.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2017 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 light
  17. import (
  18. "bytes"
  19. "context"
  20. "fmt"
  21. "testing"
  22. "github.com/davecgh/go-spew/spew"
  23. "github.com/ethereum/go-ethereum/consensus/ethash"
  24. "github.com/ethereum/go-ethereum/core"
  25. "github.com/ethereum/go-ethereum/core/rawdb"
  26. "github.com/ethereum/go-ethereum/core/state"
  27. "github.com/ethereum/go-ethereum/core/vm"
  28. "github.com/ethereum/go-ethereum/params"
  29. "github.com/ethereum/go-ethereum/trie"
  30. )
  31. func TestNodeIterator(t *testing.T) {
  32. var (
  33. fulldb = rawdb.NewMemoryDatabase()
  34. lightdb = rawdb.NewMemoryDatabase()
  35. gspec = core.Genesis{Alloc: core.GenesisAlloc{testBankAddress: {Balance: testBankFunds}}}
  36. genesis = gspec.MustCommit(fulldb)
  37. )
  38. gspec.MustCommit(lightdb)
  39. blockchain, _ := core.NewBlockChain(fulldb, nil, params.TestChainConfig, ethash.NewFullFaker(), vm.Config{}, nil, nil, nil)
  40. gchain, _ := core.GenerateChain(params.TestChainConfig, genesis, ethash.NewFaker(), fulldb, 4, testChainGen)
  41. if _, err := blockchain.InsertChain(gchain); err != nil {
  42. panic(err)
  43. }
  44. ctx := context.Background()
  45. odr := &testOdr{sdb: fulldb, ldb: lightdb, indexerConfig: TestClientIndexerConfig}
  46. head := blockchain.CurrentHeader()
  47. lightTrie, _ := NewStateDatabase(ctx, head, odr).OpenTrie(head.Root)
  48. fullTrie, _ := state.NewDatabase(fulldb).OpenTrie(head.Root)
  49. if err := diffTries(fullTrie, lightTrie); err != nil {
  50. t.Fatal(err)
  51. }
  52. }
  53. func diffTries(t1, t2 state.Trie) error {
  54. i1 := trie.NewIterator(t1.NodeIterator(nil))
  55. i2 := trie.NewIterator(t2.NodeIterator(nil))
  56. for i1.Next() && i2.Next() {
  57. if !bytes.Equal(i1.Key, i2.Key) {
  58. spew.Dump(i2)
  59. return fmt.Errorf("tries have different keys %x, %x", i1.Key, i2.Key)
  60. }
  61. if !bytes.Equal(i1.Value, i2.Value) {
  62. return fmt.Errorf("tries differ at key %x", i1.Key)
  63. }
  64. }
  65. switch {
  66. case i1.Err != nil:
  67. return fmt.Errorf("full trie iterator error: %v", i1.Err)
  68. case i2.Err != nil:
  69. return fmt.Errorf("light trie iterator error: %v", i1.Err)
  70. case i1.Next():
  71. return fmt.Errorf("full trie iterator has more k/v pairs")
  72. case i2.Next():
  73. return fmt.Errorf("light trie iterator has more k/v pairs")
  74. }
  75. return nil
  76. }