github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/light/trie_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:39</date>
    10  //</624450096796274688>
    11  
    12  
    13  package light
    14  
    15  import (
    16  	"bytes"
    17  	"context"
    18  	"fmt"
    19  	"testing"
    20  
    21  	"github.com/davecgh/go-spew/spew"
    22  	"github.com/ethereum/go-ethereum/consensus/ethash"
    23  	"github.com/ethereum/go-ethereum/core"
    24  	"github.com/ethereum/go-ethereum/core/state"
    25  	"github.com/ethereum/go-ethereum/core/vm"
    26  	"github.com/ethereum/go-ethereum/ethdb"
    27  	"github.com/ethereum/go-ethereum/params"
    28  	"github.com/ethereum/go-ethereum/trie"
    29  )
    30  
    31  func TestNodeIterator(t *testing.T) {
    32  	var (
    33  		fulldb  = ethdb.NewMemDatabase()
    34  		lightdb = ethdb.NewMemDatabase()
    35  		gspec   = core.Genesis{Alloc: core.GenesisAlloc{testBankAddress: {Balance: testBankFunds}}}
    36  		genesis = gspec.MustCommit(fulldb)
    37  	)
    38  	gspec.MustCommit(lightdb)
    39  	blockchain, _ := core.NewBlockChain(fulldb, nil, params.TestChainConfig, ethash.NewFullFaker(), vm.Config{}, nil)
    40  	gchain, _ := core.GenerateChain(params.TestChainConfig, genesis, ethash.NewFaker(), fulldb, 4, testChainGen)
    41  	if _, err := blockchain.InsertChain(gchain); err != nil {
    42  		panic(err)
    43  	}
    44  
    45  	ctx := context.Background()
    46  	odr := &testOdr{sdb: fulldb, ldb: lightdb, indexerConfig: TestClientIndexerConfig}
    47  	head := blockchain.CurrentHeader()
    48  	lightTrie, _ := NewStateDatabase(ctx, head, odr).OpenTrie(head.Root)
    49  	fullTrie, _ := state.NewDatabase(fulldb).OpenTrie(head.Root)
    50  	if err := diffTries(fullTrie, lightTrie); err != nil {
    51  		t.Fatal(err)
    52  	}
    53  }
    54  
    55  func diffTries(t1, t2 state.Trie) error {
    56  	i1 := trie.NewIterator(t1.NodeIterator(nil))
    57  	i2 := trie.NewIterator(t2.NodeIterator(nil))
    58  	for i1.Next() && i2.Next() {
    59  		if !bytes.Equal(i1.Key, i2.Key) {
    60  			spew.Dump(i2)
    61  			return fmt.Errorf("tries have different keys %x, %x", i1.Key, i2.Key)
    62  		}
    63  		if !bytes.Equal(i1.Value, i2.Value) {
    64  			return fmt.Errorf("tries differ at key %x", i1.Key)
    65  		}
    66  	}
    67  	switch {
    68  	case i1.Err != nil:
    69  		return fmt.Errorf("full trie iterator error: %v", i1.Err)
    70  	case i2.Err != nil:
    71  		return fmt.Errorf("light trie iterator error: %v", i1.Err)
    72  	case i1.Next():
    73  		return fmt.Errorf("full trie iterator has more k/v pairs")
    74  	case i2.Next():
    75  		return fmt.Errorf("light trie iterator has more k/v pairs")
    76  	}
    77  	return nil
    78  }
    79