github.com/theQRL/go-zond@v0.1.1/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  	"errors"
    23  	"fmt"
    24  	"math/big"
    25  	"testing"
    26  
    27  	"github.com/davecgh/go-spew/spew"
    28  	"github.com/theQRL/go-zond/consensus/ethash"
    29  	"github.com/theQRL/go-zond/core"
    30  	"github.com/theQRL/go-zond/core/rawdb"
    31  	"github.com/theQRL/go-zond/core/state"
    32  	"github.com/theQRL/go-zond/core/vm"
    33  	"github.com/theQRL/go-zond/params"
    34  	"github.com/theQRL/go-zond/trie"
    35  )
    36  
    37  func TestNodeIterator(t *testing.T) {
    38  	var (
    39  		fulldb  = rawdb.NewMemoryDatabase()
    40  		lightdb = rawdb.NewMemoryDatabase()
    41  		gspec   = &core.Genesis{
    42  			Config:  params.TestChainConfig,
    43  			Alloc:   core.GenesisAlloc{testBankAddress: {Balance: testBankFunds}},
    44  			BaseFee: big.NewInt(params.InitialBaseFee),
    45  		}
    46  	)
    47  	blockchain, _ := core.NewBlockChain(fulldb, nil, gspec, nil, ethash.NewFullFaker(), vm.Config{}, nil, nil)
    48  	_, gchain, _ := core.GenerateChainWithGenesis(gspec, ethash.NewFaker(), 4, testChainGen)
    49  	if _, err := blockchain.InsertChain(gchain); err != nil {
    50  		panic(err)
    51  	}
    52  
    53  	gspec.MustCommit(lightdb, trie.NewDatabase(lightdb, trie.HashDefaults))
    54  	ctx := context.Background()
    55  	odr := &testOdr{sdb: fulldb, ldb: lightdb, serverState: blockchain.StateCache(), indexerConfig: TestClientIndexerConfig}
    56  	head := blockchain.CurrentHeader()
    57  	lightTrie, _ := NewStateDatabase(ctx, head, odr).OpenTrie(head.Root)
    58  	fullTrie, _ := blockchain.StateCache().OpenTrie(head.Root)
    59  	if err := diffTries(fullTrie, lightTrie); err != nil {
    60  		t.Fatal(err)
    61  	}
    62  }
    63  
    64  func diffTries(t1, t2 state.Trie) error {
    65  	trieIt1, err := t1.NodeIterator(nil)
    66  	if err != nil {
    67  		return err
    68  	}
    69  	trieIt2, err := t2.NodeIterator(nil)
    70  	if err != nil {
    71  		return err
    72  	}
    73  	i1 := trie.NewIterator(trieIt1)
    74  	i2 := trie.NewIterator(trieIt2)
    75  	for i1.Next() && i2.Next() {
    76  		if !bytes.Equal(i1.Key, i2.Key) {
    77  			spew.Dump(i2)
    78  			return fmt.Errorf("tries have different keys %x, %x", i1.Key, i2.Key)
    79  		}
    80  		if !bytes.Equal(i1.Value, i2.Value) {
    81  			return fmt.Errorf("tries differ at key %x", i1.Key)
    82  		}
    83  	}
    84  	switch {
    85  	case i1.Err != nil:
    86  		return fmt.Errorf("full trie iterator error: %v", i1.Err)
    87  	case i2.Err != nil:
    88  		return fmt.Errorf("light trie iterator error: %v", i2.Err)
    89  	case i1.Next():
    90  		return errors.New("full trie iterator has more k/v pairs")
    91  	case i2.Next():
    92  		return errors.New("light trie iterator has more k/v pairs")
    93  	}
    94  	return nil
    95  }