2017-06-27 15:57:06 +02:00
|
|
|
// 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"
|
2023-05-25 14:51:13 +08:00
|
|
|
"errors"
|
2017-06-27 15:57:06 +02:00
|
|
|
"fmt"
|
2021-06-15 13:56:14 +03:00
|
|
|
"math/big"
|
2017-06-27 15:57:06 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
"github.com/ethereum/go-ethereum/consensus/ethash"
|
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2018-09-24 15:57:49 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2017-06-27 15:57:06 +02:00
|
|
|
"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 (
|
2018-09-24 15:57:49 +03:00
|
|
|
fulldb = rawdb.NewMemoryDatabase()
|
|
|
|
lightdb = rawdb.NewMemoryDatabase()
|
2022-08-31 00:22:28 +08:00
|
|
|
gspec = &core.Genesis{
|
|
|
|
Config: params.TestChainConfig,
|
2021-06-15 13:56:14 +03:00
|
|
|
Alloc: core.GenesisAlloc{testBankAddress: {Balance: testBankFunds}},
|
|
|
|
BaseFee: big.NewInt(params.InitialBaseFee),
|
|
|
|
}
|
2017-06-27 15:57:06 +02:00
|
|
|
)
|
2022-08-31 00:22:28 +08:00
|
|
|
blockchain, _ := core.NewBlockChain(fulldb, nil, gspec, nil, ethash.NewFullFaker(), vm.Config{}, nil, nil)
|
2022-09-08 02:21:59 +08:00
|
|
|
_, gchain, _ := core.GenerateChainWithGenesis(gspec, ethash.NewFaker(), 4, testChainGen)
|
2017-06-27 15:57:06 +02:00
|
|
|
if _, err := blockchain.InsertChain(gchain); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-09-08 02:21:59 +08:00
|
|
|
gspec.MustCommit(lightdb)
|
2017-06-27 15:57:06 +02:00
|
|
|
ctx := context.Background()
|
2022-09-08 02:21:59 +08:00
|
|
|
odr := &testOdr{sdb: fulldb, ldb: lightdb, serverState: blockchain.StateCache(), indexerConfig: TestClientIndexerConfig}
|
2017-06-27 15:57:06 +02:00
|
|
|
head := blockchain.CurrentHeader()
|
|
|
|
lightTrie, _ := NewStateDatabase(ctx, head, odr).OpenTrie(head.Root)
|
2022-09-08 02:21:59 +08:00
|
|
|
fullTrie, _ := blockchain.StateCache().OpenTrie(head.Root)
|
2017-06-27 15:57:06 +02:00
|
|
|
if err := diffTries(fullTrie, lightTrie); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func diffTries(t1, t2 state.Trie) error {
|
cmd, core/state, eth, tests, trie: improve state reader (#27428)
The state availability is checked during the creation of a state reader.
- In hash-based database, if the specified root node does not exist on disk disk, then
the state reader won't be created and an error will be returned.
- In path-based database, if the specified state layer is not available, then the
state reader won't be created and an error will be returned.
This change also contains a stricter semantics regarding the `Commit` operation: once it has been performed, the trie is no longer usable, and certain operations will return an error.
2023-06-21 03:31:45 +08:00
|
|
|
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)
|
2017-06-27 15:57:06 +02:00
|
|
|
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)
|
|
|
|
}
|
2018-12-10 14:29:34 +03:00
|
|
|
if !bytes.Equal(i1.Value, i2.Value) {
|
2017-06-27 15:57:06 +02:00
|
|
|
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:
|
2022-07-25 17:54:31 +08:00
|
|
|
return fmt.Errorf("light trie iterator error: %v", i2.Err)
|
2017-06-27 15:57:06 +02:00
|
|
|
case i1.Next():
|
2023-05-25 14:51:13 +08:00
|
|
|
return errors.New("full trie iterator has more k/v pairs")
|
2017-06-27 15:57:06 +02:00
|
|
|
case i2.Next():
|
2023-05-25 14:51:13 +08:00
|
|
|
return errors.New("light trie iterator has more k/v pairs")
|
2017-06-27 15:57:06 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|