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