trie_test.go 3.02 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package light

import (
	"bytes"
	"context"
22
	"errors"
23
	"fmt"
24
	"math/big"
25 26 27 28 29
	"testing"

	"github.com/davecgh/go-spew/spew"
	"github.com/ethereum/go-ethereum/consensus/ethash"
	"github.com/ethereum/go-ethereum/core"
30
	"github.com/ethereum/go-ethereum/core/rawdb"
31 32 33 34 35 36 37 38
	"github.com/ethereum/go-ethereum/core/state"
	"github.com/ethereum/go-ethereum/core/vm"
	"github.com/ethereum/go-ethereum/params"
	"github.com/ethereum/go-ethereum/trie"
)

func TestNodeIterator(t *testing.T) {
	var (
39 40
		fulldb  = rawdb.NewMemoryDatabase()
		lightdb = rawdb.NewMemoryDatabase()
41 42
		gspec   = &core.Genesis{
			Config:  params.TestChainConfig,
43 44 45
			Alloc:   core.GenesisAlloc{testBankAddress: {Balance: testBankFunds}},
			BaseFee: big.NewInt(params.InitialBaseFee),
		}
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 50 51 52
	if _, err := blockchain.InsertChain(gchain); err != nil {
		panic(err)
	}

53
	gspec.MustCommit(lightdb)
54
	ctx := context.Background()
55
	odr := &testOdr{sdb: fulldb, ldb: lightdb, serverState: blockchain.StateCache(), indexerConfig: TestClientIndexerConfig}
56 57
	head := blockchain.CurrentHeader()
	lightTrie, _ := NewStateDatabase(ctx, head, odr).OpenTrie(head.Root)
58
	fullTrie, _ := blockchain.StateCache().OpenTrie(head.Root)
59 60 61 62 63 64
	if err := diffTries(fullTrie, lightTrie); err != nil {
		t.Fatal(err)
	}
}

func diffTries(t1, t2 state.Trie) error {
65 66 67 68 69 70 71 72 73 74
	trieIt1, err := t1.NodeIterator(nil)
	if err != nil {
		return err
	}
	trieIt2, err := t2.NodeIterator(nil)
	if err != nil {
		return err
	}
	i1 := trie.NewIterator(trieIt1)
	i2 := trie.NewIterator(trieIt2)
75 76 77 78 79
	for i1.Next() && i2.Next() {
		if !bytes.Equal(i1.Key, i2.Key) {
			spew.Dump(i2)
			return fmt.Errorf("tries have different keys %x, %x", i1.Key, i2.Key)
		}
80
		if !bytes.Equal(i1.Value, i2.Value) {
81 82 83 84 85 86 87
			return fmt.Errorf("tries differ at key %x", i1.Key)
		}
	}
	switch {
	case i1.Err != nil:
		return fmt.Errorf("full trie iterator error: %v", i1.Err)
	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 95
	}
	return nil
}