github.com/core-coin/go-core/v2@v2.1.9/light/trie_test.go (about)

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