2017-06-27 08:57:06 -05: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 01:51:13 -05:00
|
|
|
"errors"
|
2017-06-27 08:57:06 -05:00
|
|
|
"fmt"
|
2021-06-15 05:56:14 -05:00
|
|
|
"math/big"
|
2017-06-27 08:57:06 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
"github.com/ethereum/go-ethereum/consensus/ethash"
|
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2018-09-24 07:57:49 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2017-06-27 08:57:06 -05: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 07:57:49 -05:00
|
|
|
fulldb = rawdb.NewMemoryDatabase()
|
|
|
|
lightdb = rawdb.NewMemoryDatabase()
|
2022-08-30 11:22:28 -05:00
|
|
|
gspec = &core.Genesis{
|
|
|
|
Config: params.TestChainConfig,
|
2021-06-15 05:56:14 -05:00
|
|
|
Alloc: core.GenesisAlloc{testBankAddress: {Balance: testBankFunds}},
|
|
|
|
BaseFee: big.NewInt(params.InitialBaseFee),
|
|
|
|
}
|
2017-06-27 08:57:06 -05:00
|
|
|
)
|
2022-08-30 11:22:28 -05:00
|
|
|
blockchain, _ := core.NewBlockChain(fulldb, nil, gspec, nil, ethash.NewFullFaker(), vm.Config{}, nil, nil)
|
2022-09-07 13:21:59 -05:00
|
|
|
_, gchain, _ := core.GenerateChainWithGenesis(gspec, ethash.NewFaker(), 4, testChainGen)
|
2017-06-27 08:57:06 -05:00
|
|
|
if _, err := blockchain.InsertChain(gchain); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 14:21:36 -05:00
|
|
|
gspec.MustCommit(lightdb, trie.NewDatabase(lightdb, trie.HashDefaults))
|
2017-06-27 08:57:06 -05:00
|
|
|
ctx := context.Background()
|
2022-09-07 13:21:59 -05:00
|
|
|
odr := &testOdr{sdb: fulldb, ldb: lightdb, serverState: blockchain.StateCache(), indexerConfig: TestClientIndexerConfig}
|
2017-06-27 08:57:06 -05:00
|
|
|
head := blockchain.CurrentHeader()
|
|
|
|
lightTrie, _ := NewStateDatabase(ctx, head, odr).OpenTrie(head.Root)
|
2022-09-07 13:21:59 -05:00
|
|
|
fullTrie, _ := blockchain.StateCache().OpenTrie(head.Root)
|
2017-06-27 08:57:06 -05: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-20 14:31:45 -05: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 08:57:06 -05: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 05:29:34 -06:00
|
|
|
if !bytes.Equal(i1.Value, i2.Value) {
|
2017-06-27 08:57:06 -05: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 04:54:31 -05:00
|
|
|
return fmt.Errorf("light trie iterator error: %v", i2.Err)
|
2017-06-27 08:57:06 -05:00
|
|
|
case i1.Next():
|
2023-05-25 01:51:13 -05:00
|
|
|
return errors.New("full trie iterator has more k/v pairs")
|
2017-06-27 08:57:06 -05:00
|
|
|
case i2.Next():
|
2023-05-25 01:51:13 -05:00
|
|
|
return errors.New("light trie iterator has more k/v pairs")
|
2017-06-27 08:57:06 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|