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