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