2015-10-05 16:37:56 +00:00
|
|
|
// Copyright 2015 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 state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-09-24 12:57:49 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2021-09-28 08:48:07 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2015-12-28 13:20:37 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2015-10-05 16:37:56 +00:00
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
2020-08-28 07:50:37 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2015-10-05 16:37:56 +00:00
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
2024-02-13 13:49:53 +00:00
|
|
|
"github.com/ethereum/go-ethereum/triedb"
|
|
|
|
"github.com/ethereum/go-ethereum/triedb/hashdb"
|
|
|
|
"github.com/ethereum/go-ethereum/triedb/pathdb"
|
2024-01-23 13:51:58 +00:00
|
|
|
"github.com/holiman/uint256"
|
2015-10-05 16:37:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// testAccount is the data associated with an account used by the state tests.
|
|
|
|
type testAccount struct {
|
|
|
|
address common.Address
|
2024-01-23 13:51:58 +00:00
|
|
|
balance *uint256.Int
|
2015-10-05 16:37:56 +00:00
|
|
|
nonce uint64
|
|
|
|
code []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeTestState create a sample test state to test node-wise reconstruction.
|
2024-02-13 13:49:53 +00:00
|
|
|
func makeTestState(scheme string) (ethdb.Database, Database, *triedb.Database, common.Hash, []*testAccount) {
|
2015-10-05 16:37:56 +00:00
|
|
|
// Create an empty state
|
2024-02-13 13:49:53 +00:00
|
|
|
config := &triedb.Config{Preimages: true}
|
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 19:21:36 +00:00
|
|
|
if scheme == rawdb.PathScheme {
|
|
|
|
config.PathDB = pathdb.Defaults
|
|
|
|
} else {
|
|
|
|
config.HashDB = hashdb.Defaults
|
|
|
|
}
|
2022-11-28 13:31:28 +00:00
|
|
|
db := rawdb.NewMemoryDatabase()
|
2024-02-13 13:49:53 +00:00
|
|
|
nodeDb := triedb.NewDatabase(db, config)
|
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 19:21:36 +00:00
|
|
|
sdb := NewDatabaseWithNodeDB(db, nodeDb)
|
2023-05-11 07:19:42 +00:00
|
|
|
state, _ := New(types.EmptyRootHash, sdb, nil)
|
2015-10-05 16:37:56 +00:00
|
|
|
|
|
|
|
// Fill it with some arbitrary data
|
2020-08-28 07:50:37 +00:00
|
|
|
var accounts []*testAccount
|
2015-12-28 13:20:37 +00:00
|
|
|
for i := byte(0); i < 96; i++ {
|
2024-01-14 11:32:23 +00:00
|
|
|
obj := state.getOrNewStateObject(common.BytesToAddress([]byte{i}))
|
2015-10-05 16:37:56 +00:00
|
|
|
acc := &testAccount{address: common.BytesToAddress([]byte{i})}
|
|
|
|
|
2024-01-23 13:51:58 +00:00
|
|
|
obj.AddBalance(uint256.NewInt(uint64(11 * i)))
|
|
|
|
acc.balance = uint256.NewInt(uint64(11 * i))
|
2015-10-05 16:37:56 +00:00
|
|
|
|
|
|
|
obj.SetNonce(uint64(42 * i))
|
|
|
|
acc.nonce = uint64(42 * i)
|
|
|
|
|
|
|
|
if i%3 == 0 {
|
2016-10-01 12:44:53 +00:00
|
|
|
obj.SetCode(crypto.Keccak256Hash([]byte{i, i, i, i, i}), []byte{i, i, i, i, i})
|
2015-10-05 16:37:56 +00:00
|
|
|
acc.code = []byte{i, i, i, i, i}
|
|
|
|
}
|
2020-08-28 07:50:37 +00:00
|
|
|
if i%5 == 0 {
|
|
|
|
for j := byte(0); j < 5; j++ {
|
2020-12-10 13:48:32 +00:00
|
|
|
hash := crypto.Keccak256Hash([]byte{i, i, i, i, i, j, j})
|
2023-07-24 10:22:09 +00:00
|
|
|
obj.SetState(hash, hash)
|
2020-08-28 07:50:37 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-05 16:37:56 +00:00
|
|
|
accounts = append(accounts, acc)
|
|
|
|
}
|
2023-07-24 10:22:09 +00:00
|
|
|
root, _ := state.Commit(0, false)
|
2015-10-05 16:37:56 +00:00
|
|
|
|
|
|
|
// Return the generated state
|
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 19:21:36 +00:00
|
|
|
return db, sdb, nodeDb, root, accounts
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// checkStateAccounts cross references a reconstructed state with an expected
|
|
|
|
// account array.
|
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 19:21:36 +00:00
|
|
|
func checkStateAccounts(t *testing.T, db ethdb.Database, scheme string, root common.Hash, accounts []*testAccount) {
|
2024-02-13 13:49:53 +00:00
|
|
|
var config triedb.Config
|
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 19:21:36 +00:00
|
|
|
if scheme == rawdb.PathScheme {
|
|
|
|
config.PathDB = pathdb.Defaults
|
|
|
|
}
|
2015-12-28 13:20:37 +00:00
|
|
|
// Check root availability and state contents
|
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 19:21:36 +00:00
|
|
|
state, err := New(root, NewDatabaseWithConfig(db, &config), nil)
|
2015-12-28 13:20:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create state trie at %x: %v", root, 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 19:21:36 +00:00
|
|
|
if err := checkStateConsistency(db, scheme, root); err != nil {
|
2015-12-28 13:20:37 +00:00
|
|
|
t.Fatalf("inconsistent state trie at %x: %v", root, err)
|
|
|
|
}
|
|
|
|
for i, acc := range accounts {
|
2015-10-05 16:37:56 +00:00
|
|
|
if balance := state.GetBalance(acc.address); balance.Cmp(acc.balance) != 0 {
|
|
|
|
t.Errorf("account %d: balance mismatch: have %v, want %v", i, balance, acc.balance)
|
|
|
|
}
|
|
|
|
if nonce := state.GetNonce(acc.address); nonce != acc.nonce {
|
|
|
|
t.Errorf("account %d: nonce mismatch: have %v, want %v", i, nonce, acc.nonce)
|
|
|
|
}
|
2017-01-06 15:44:20 +00:00
|
|
|
if code := state.GetCode(acc.address); !bytes.Equal(code, acc.code) {
|
2015-10-05 16:37:56 +00:00
|
|
|
t.Errorf("account %d: code mismatch: have %x, want %x", i, code, acc.code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-27 13:57:06 +00:00
|
|
|
// checkStateConsistency checks that all data of a state root is present.
|
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 19:21:36 +00:00
|
|
|
func checkStateConsistency(db ethdb.Database, scheme string, root common.Hash) error {
|
2024-02-13 13:49:53 +00:00
|
|
|
config := &triedb.Config{Preimages: true}
|
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 19:21:36 +00:00
|
|
|
if scheme == rawdb.PathScheme {
|
|
|
|
config.PathDB = pathdb.Defaults
|
|
|
|
}
|
|
|
|
state, err := New(root, NewDatabaseWithConfig(db, config), nil)
|
2015-12-28 13:20:37 +00:00
|
|
|
if err != nil {
|
2016-02-16 10:37:00 +00:00
|
|
|
return err
|
2015-12-28 13:20:37 +00:00
|
|
|
}
|
2023-05-11 07:15:44 +00:00
|
|
|
it := newNodeIterator(state)
|
2016-02-16 10:37:00 +00:00
|
|
|
for it.Next() {
|
2015-12-28 13:20:37 +00:00
|
|
|
}
|
2016-02-16 10:37:00 +00:00
|
|
|
return it.Error
|
2015-12-28 13:20:37 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 16:37:56 +00:00
|
|
|
// Tests that an empty state is not scheduled for syncing.
|
|
|
|
func TestEmptyStateSync(t *testing.T) {
|
2024-02-13 13:49:53 +00:00
|
|
|
dbA := triedb.NewDatabase(rawdb.NewMemoryDatabase(), nil)
|
|
|
|
dbB := triedb.NewDatabase(rawdb.NewMemoryDatabase(), &triedb.Config{PathDB: pathdb.Defaults})
|
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 19:21:36 +00:00
|
|
|
|
|
|
|
sync := NewStateSync(types.EmptyRootHash, rawdb.NewMemoryDatabase(), nil, dbA.Scheme())
|
|
|
|
if paths, nodes, codes := sync.Missing(1); len(paths) != 0 || len(nodes) != 0 || len(codes) != 0 {
|
|
|
|
t.Errorf("content requested for empty state: %v, %v, %v", nodes, paths, codes)
|
|
|
|
}
|
|
|
|
sync = NewStateSync(types.EmptyRootHash, rawdb.NewMemoryDatabase(), nil, dbB.Scheme())
|
2022-07-15 11:55:51 +00:00
|
|
|
if paths, nodes, codes := sync.Missing(1); len(paths) != 0 || len(nodes) != 0 || len(codes) != 0 {
|
|
|
|
t.Errorf("content requested for empty state: %v, %v, %v", nodes, paths, codes)
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that given a root hash, a state can sync iteratively on a single thread,
|
|
|
|
// requesting retrieval tasks and returning all of them in one go.
|
2020-08-28 07:50:37 +00:00
|
|
|
func TestIterativeStateSyncIndividual(t *testing.T) {
|
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 19:21:36 +00:00
|
|
|
testIterativeStateSync(t, 1, false, false, rawdb.HashScheme)
|
|
|
|
testIterativeStateSync(t, 1, false, false, rawdb.PathScheme)
|
2020-08-28 07:50:37 +00:00
|
|
|
}
|
|
|
|
func TestIterativeStateSyncBatched(t *testing.T) {
|
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 19:21:36 +00:00
|
|
|
testIterativeStateSync(t, 100, false, false, rawdb.HashScheme)
|
|
|
|
testIterativeStateSync(t, 100, false, false, rawdb.PathScheme)
|
2020-08-28 07:50:37 +00:00
|
|
|
}
|
|
|
|
func TestIterativeStateSyncIndividualFromDisk(t *testing.T) {
|
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 19:21:36 +00:00
|
|
|
testIterativeStateSync(t, 1, true, false, rawdb.HashScheme)
|
|
|
|
testIterativeStateSync(t, 1, true, false, rawdb.PathScheme)
|
2020-08-28 07:50:37 +00:00
|
|
|
}
|
|
|
|
func TestIterativeStateSyncBatchedFromDisk(t *testing.T) {
|
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 19:21:36 +00:00
|
|
|
testIterativeStateSync(t, 100, true, false, rawdb.HashScheme)
|
|
|
|
testIterativeStateSync(t, 100, true, false, rawdb.PathScheme)
|
2020-08-28 07:50:37 +00:00
|
|
|
}
|
|
|
|
func TestIterativeStateSyncIndividualByPath(t *testing.T) {
|
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 19:21:36 +00:00
|
|
|
testIterativeStateSync(t, 1, false, true, rawdb.HashScheme)
|
|
|
|
testIterativeStateSync(t, 1, false, true, rawdb.PathScheme)
|
2020-08-28 07:50:37 +00:00
|
|
|
}
|
|
|
|
func TestIterativeStateSyncBatchedByPath(t *testing.T) {
|
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 19:21:36 +00:00
|
|
|
testIterativeStateSync(t, 100, false, true, rawdb.HashScheme)
|
|
|
|
testIterativeStateSync(t, 100, false, true, rawdb.PathScheme)
|
2020-08-28 07:50:37 +00:00
|
|
|
}
|
2015-10-05 16:37:56 +00:00
|
|
|
|
2022-07-15 11:55:51 +00:00
|
|
|
// stateElement represents the element in the state trie(bytecode or trie node).
|
|
|
|
type stateElement struct {
|
|
|
|
path string
|
|
|
|
hash common.Hash
|
|
|
|
code common.Hash
|
|
|
|
syncPath trie.SyncPath
|
|
|
|
}
|
|
|
|
|
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 19:21:36 +00:00
|
|
|
func testIterativeStateSync(t *testing.T, count int, commit bool, bypath bool, scheme string) {
|
2015-10-05 16:37:56 +00:00
|
|
|
// Create a random state to copy
|
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 19:21:36 +00:00
|
|
|
srcDisk, srcDb, ndb, srcRoot, srcAccounts := makeTestState(scheme)
|
2020-08-21 12:10:40 +00:00
|
|
|
if commit {
|
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 19:21:36 +00:00
|
|
|
ndb.Commit(srcRoot, false)
|
2020-08-21 12:10:40 +00:00
|
|
|
}
|
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 19:21:36 +00:00
|
|
|
srcTrie, _ := trie.New(trie.StateTrieID(srcRoot), ndb)
|
2020-08-28 07:50:37 +00:00
|
|
|
|
2015-10-05 16:37:56 +00:00
|
|
|
// Create a destination state and sync with the scheduler
|
2018-09-24 12:57:49 +00:00
|
|
|
dstDb := rawdb.NewMemoryDatabase()
|
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 19:21:36 +00:00
|
|
|
sched := NewStateSync(srcRoot, dstDb, nil, ndb.Scheme())
|
2015-10-05 16:37:56 +00:00
|
|
|
|
2020-08-28 07:50:37 +00:00
|
|
|
var (
|
2022-07-15 11:55:51 +00:00
|
|
|
nodeElements []stateElement
|
|
|
|
codeElements []stateElement
|
2020-08-28 07:50:37 +00:00
|
|
|
)
|
2022-07-15 11:55:51 +00:00
|
|
|
paths, nodes, codes := sched.Missing(count)
|
|
|
|
for i := 0; i < len(paths); i++ {
|
|
|
|
nodeElements = append(nodeElements, stateElement{
|
|
|
|
path: paths[i],
|
|
|
|
hash: nodes[i],
|
|
|
|
syncPath: trie.NewSyncPath([]byte(paths[i])),
|
|
|
|
})
|
2020-08-28 07:50:37 +00:00
|
|
|
}
|
2022-07-15 11:55:51 +00:00
|
|
|
for i := 0; i < len(codes); i++ {
|
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 19:21:36 +00:00
|
|
|
codeElements = append(codeElements, stateElement{code: codes[i]})
|
|
|
|
}
|
|
|
|
reader, err := ndb.Reader(srcRoot)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("state is not existent, %#x", srcRoot)
|
2022-07-15 11:55:51 +00:00
|
|
|
}
|
|
|
|
for len(nodeElements)+len(codeElements) > 0 {
|
|
|
|
var (
|
|
|
|
nodeResults = make([]trie.NodeSyncResult, len(nodeElements))
|
|
|
|
codeResults = make([]trie.CodeSyncResult, len(codeElements))
|
|
|
|
)
|
|
|
|
for i, element := range codeElements {
|
core/state, light, les: make signature of ContractCode hash-independent (#27209)
* core/state, light, les: make signature of ContractCode hash-independent
* push current state for feedback
* les: fix unit test
* core, les, light: fix les unittests
* core/state, trie, les, light: fix state iterator
* core, les: address comments
* les: fix lint
---------
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
2023-06-28 09:11:02 +00:00
|
|
|
data, err := srcDb.ContractCode(common.Address{}, element.code)
|
2015-10-05 16:37:56 +00:00
|
|
|
if err != nil {
|
2022-07-15 11:55:51 +00:00
|
|
|
t.Fatalf("failed to retrieve contract bytecode for hash %x", element.code)
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
2022-07-15 11:55:51 +00:00
|
|
|
codeResults[i] = trie.CodeSyncResult{Hash: element.code, Data: data}
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
2022-07-15 11:55:51 +00:00
|
|
|
for i, node := range nodeElements {
|
|
|
|
if bypath {
|
|
|
|
if len(node.syncPath) == 1 {
|
2023-04-20 10:57:24 +00:00
|
|
|
data, _, err := srcTrie.GetNode(node.syncPath[0])
|
2022-07-15 11:55:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve node data for path %x: %v", node.syncPath[0], err)
|
|
|
|
}
|
|
|
|
nodeResults[i] = trie.NodeSyncResult{Path: node.path, Data: data}
|
|
|
|
} else {
|
|
|
|
var acc types.StateAccount
|
2023-04-20 10:57:24 +00:00
|
|
|
if err := rlp.DecodeBytes(srcTrie.MustGet(node.syncPath[0]), &acc); err != nil {
|
2022-07-15 11:55:51 +00:00
|
|
|
t.Fatalf("failed to decode account on path %x: %v", node.syncPath[0], err)
|
|
|
|
}
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 08:01:02 +00:00
|
|
|
id := trie.StorageTrieID(srcRoot, common.BytesToHash(node.syncPath[0]), acc.Root)
|
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 19:21:36 +00:00
|
|
|
stTrie, err := trie.New(id, ndb)
|
2022-07-15 11:55:51 +00:00
|
|
|
if err != nil {
|
2024-02-05 21:16:32 +00:00
|
|
|
t.Fatalf("failed to retrieve storage trie for path %x: %v", node.syncPath[1], err)
|
2022-07-15 11:55:51 +00:00
|
|
|
}
|
2023-04-20 10:57:24 +00:00
|
|
|
data, _, err := stTrie.GetNode(node.syncPath[1])
|
2022-07-15 11:55:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve node data for path %x: %v", node.syncPath[1], err)
|
|
|
|
}
|
|
|
|
nodeResults[i] = trie.NodeSyncResult{Path: node.path, Data: data}
|
2020-08-28 07:50:37 +00:00
|
|
|
}
|
|
|
|
} else {
|
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 19:21:36 +00:00
|
|
|
owner, inner := trie.ResolvePath([]byte(node.path))
|
|
|
|
data, err := reader.Node(owner, inner, node.hash)
|
2020-08-28 07:50:37 +00:00
|
|
|
if err != nil {
|
2022-07-15 11:55:51 +00:00
|
|
|
t.Fatalf("failed to retrieve node data for key %v", []byte(node.path))
|
2020-08-28 07:50:37 +00:00
|
|
|
}
|
2022-07-15 11:55:51 +00:00
|
|
|
nodeResults[i] = trie.NodeSyncResult{Path: node.path, Data: data}
|
2020-08-28 07:50:37 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-15 11:55:51 +00:00
|
|
|
for _, result := range codeResults {
|
|
|
|
if err := sched.ProcessCode(result); err != nil {
|
|
|
|
t.Errorf("failed to process result %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, result := range nodeResults {
|
|
|
|
if err := sched.ProcessNode(result); err != nil {
|
2020-08-28 07:50:37 +00:00
|
|
|
t.Errorf("failed to process result %v", err)
|
2020-08-21 12:10:40 +00:00
|
|
|
}
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
2019-10-28 17:50:11 +00:00
|
|
|
batch := dstDb.NewBatch()
|
|
|
|
if err := sched.Commit(batch); err != nil {
|
|
|
|
t.Fatalf("failed to commit data: %v", err)
|
eth/downloader: separate state sync from queue (#14460)
* eth/downloader: separate state sync from queue
Scheduling of state node downloads hogged the downloader queue lock when
new requests were scheduled. This caused timeouts for other requests.
With this change, state sync is fully independent of all other downloads
and doesn't involve the queue at all.
State sync is started and checked on in processContent. This is slightly
awkward because processContent doesn't have a select loop. Instead, the
queue is closed by an auxiliary goroutine when state sync fails. We
tried several alternatives to this but settled on the current approach
because it's the least amount of change overall.
Handling of the pivot block has changed slightly: the queue previously
prevented import of pivot block receipts before the state of the pivot
block was available. In this commit, the receipt will be imported before
the state. This causes an annoyance where the pivot block is committed
as fast block head even when state downloads fail. Stay tuned for more
updates in this area ;)
* eth/downloader: remove cancelTimeout channel
* eth/downloader: retry state requests on timeout
* eth/downloader: improve comment
* eth/downloader: mark peers idle when state sync is done
* eth/downloader: move pivot block splitting to processContent
This change also ensures that pivot block receipts aren't imported
before the pivot block itself.
* eth/downloader: limit state node retries
* eth/downloader: improve state node error handling and retry check
* eth/downloader: remove maxStateNodeRetries
It fails the sync too much.
* eth/downloader: remove last use of cancelCh in statesync.go
Fixes TestDeliverHeadersHang*Fast and (hopefully)
the weird cancellation behaviour at the end of fast sync.
* eth/downloader: fix leak in runStateSync
* eth/downloader: don't run processFullSyncContent in LightSync mode
* eth/downloader: improve comments
* eth/downloader: fix vet, megacheck
* eth/downloader: remove unrequested tasks anyway
* eth/downloader, trie: various polishes around duplicate items
This commit explicitly tracks duplicate and unexpected state
delieveries done against a trie Sync structure, also adding
there to import info logs.
The commit moves the db batch used to commit trie changes one
level deeper so its flushed after every node insertion. This
is needed to avoid a lot of duplicate retrievals caused by
inconsistencies between Sync internals and database. A better
approach is to track not-yet-written states in trie.Sync and
flush on commit, but I'm focuing on correctness first now.
The commit fixes a regression around pivot block fail count.
The counter previously was reset to 1 if and only if a sync
cycle progressed (inserted at least 1 entry to the database).
The current code reset it already if a node was delivered,
which is not stong enough, because unless it ends up written
to disk, an attacker can just loop and attack ad infinitum.
The commit also fixes a regression around state deliveries
and timeouts. The old downloader tracked if a delivery is
stale (none of the deliveries were requestedt), in which
case it didn't mark the node idle and did not send further
requests, since it signals a past timeout. The current code
did mark it idle even on stale deliveries, which eventually
caused two requests to be in flight at the same time, making
the deliveries always stale and mass duplicating retrievals
between multiple peers.
* eth/downloader: fix state request leak
This commit fixes the hang seen sometimes while doing the state
sync. The cause of the hang was a rare combination of events:
request state data from peer, peer drops and reconnects almost
immediately. This caused a new download task to be assigned to
the peer, overwriting the old one still waiting for a timeout,
which in turned leaked the requests out, never to be retried.
The fix is to ensure that a task assignment moves any pending
one back into the retry queue.
The commit also fixes a regression with peer dropping due to
stalls. The current code considered a peer stalling if they
timed out delivering 1 item. However, the downloader never
requests only one, the minimum is 2 (attempt to fine tune
estimated latency/bandwidth). The fix is simply to drop if
a timeout is detected at 2 items.
Apart from the above bugfixes, the commit contains some code
polishes I made while debugging the hang.
* core, eth, trie: support batched trie sync db writes
* trie: rename SyncMemCache to syncMemBatch
2017-06-22 12:26:03 +00:00
|
|
|
}
|
2019-10-28 17:50:11 +00:00
|
|
|
batch.Write()
|
2020-08-28 07:50:37 +00:00
|
|
|
|
2022-07-15 11:55:51 +00:00
|
|
|
paths, nodes, codes = sched.Missing(count)
|
|
|
|
nodeElements = nodeElements[:0]
|
|
|
|
for i := 0; i < len(paths); i++ {
|
|
|
|
nodeElements = append(nodeElements, stateElement{
|
|
|
|
path: paths[i],
|
|
|
|
hash: nodes[i],
|
|
|
|
syncPath: trie.NewSyncPath([]byte(paths[i])),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
codeElements = codeElements[:0]
|
|
|
|
for i := 0; i < len(codes); i++ {
|
|
|
|
codeElements = append(codeElements, stateElement{
|
|
|
|
code: codes[i],
|
|
|
|
})
|
2020-08-28 07:50:37 +00:00
|
|
|
}
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
core/state, light, les: make signature of ContractCode hash-independent (#27209)
* core/state, light, les: make signature of ContractCode hash-independent
* push current state for feedback
* les: fix unit test
* core, les, light: fix les unittests
* core/state, trie, les, light: fix state iterator
* core, les: address comments
* les: fix lint
---------
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
2023-06-28 09:11:02 +00:00
|
|
|
// Copy the preimages from source db in order to traverse the state.
|
|
|
|
srcDb.TrieDB().WritePreimages()
|
|
|
|
copyPreimages(srcDisk, dstDb)
|
|
|
|
|
2015-10-05 16:37:56 +00:00
|
|
|
// Cross check that the two states are in sync
|
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 19:21:36 +00:00
|
|
|
checkStateAccounts(t, dstDb, ndb.Scheme(), srcRoot, srcAccounts)
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that the trie scheduler can correctly reconstruct the state even if only
|
|
|
|
// partial results are returned, and the others sent only later.
|
|
|
|
func TestIterativeDelayedStateSync(t *testing.T) {
|
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 19:21:36 +00:00
|
|
|
testIterativeDelayedStateSync(t, rawdb.HashScheme)
|
|
|
|
testIterativeDelayedStateSync(t, rawdb.PathScheme)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testIterativeDelayedStateSync(t *testing.T, scheme string) {
|
2015-10-05 16:37:56 +00:00
|
|
|
// Create a random state to copy
|
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 19:21:36 +00:00
|
|
|
srcDisk, srcDb, ndb, srcRoot, srcAccounts := makeTestState(scheme)
|
2015-10-05 16:37:56 +00:00
|
|
|
|
|
|
|
// Create a destination state and sync with the scheduler
|
2018-09-24 12:57:49 +00:00
|
|
|
dstDb := rawdb.NewMemoryDatabase()
|
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 19:21:36 +00:00
|
|
|
sched := NewStateSync(srcRoot, dstDb, nil, ndb.Scheme())
|
2015-10-05 16:37:56 +00:00
|
|
|
|
2022-07-15 11:55:51 +00:00
|
|
|
var (
|
|
|
|
nodeElements []stateElement
|
|
|
|
codeElements []stateElement
|
|
|
|
)
|
|
|
|
paths, nodes, codes := sched.Missing(0)
|
|
|
|
for i := 0; i < len(paths); i++ {
|
|
|
|
nodeElements = append(nodeElements, stateElement{
|
|
|
|
path: paths[i],
|
|
|
|
hash: nodes[i],
|
|
|
|
syncPath: trie.NewSyncPath([]byte(paths[i])),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for i := 0; i < len(codes); i++ {
|
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 19:21:36 +00:00
|
|
|
codeElements = append(codeElements, stateElement{code: codes[i]})
|
|
|
|
}
|
|
|
|
reader, err := ndb.Reader(srcRoot)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("state is not existent, %#x", srcRoot)
|
2022-07-15 11:55:51 +00:00
|
|
|
}
|
|
|
|
for len(nodeElements)+len(codeElements) > 0 {
|
2015-10-05 16:37:56 +00:00
|
|
|
// Sync only half of the scheduled nodes
|
2022-08-19 06:00:21 +00:00
|
|
|
var nodeProcessed int
|
|
|
|
var codeProcessed int
|
2022-07-15 11:55:51 +00:00
|
|
|
if len(codeElements) > 0 {
|
|
|
|
codeResults := make([]trie.CodeSyncResult, len(codeElements)/2+1)
|
|
|
|
for i, element := range codeElements[:len(codeResults)] {
|
core/state, light, les: make signature of ContractCode hash-independent (#27209)
* core/state, light, les: make signature of ContractCode hash-independent
* push current state for feedback
* les: fix unit test
* core, les, light: fix les unittests
* core/state, trie, les, light: fix state iterator
* core, les: address comments
* les: fix lint
---------
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
2023-06-28 09:11:02 +00:00
|
|
|
data, err := srcDb.ContractCode(common.Address{}, element.code)
|
2022-07-15 11:55:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve contract bytecode for %x", element.code)
|
|
|
|
}
|
|
|
|
codeResults[i] = trie.CodeSyncResult{Hash: element.code, Data: data}
|
2020-08-21 12:10:40 +00:00
|
|
|
}
|
2022-07-15 11:55:51 +00:00
|
|
|
for _, result := range codeResults {
|
|
|
|
if err := sched.ProcessCode(result); err != nil {
|
|
|
|
t.Fatalf("failed to process result %v", err)
|
|
|
|
}
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
2022-08-19 06:00:21 +00:00
|
|
|
codeProcessed = len(codeResults)
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
2022-07-15 11:55:51 +00:00
|
|
|
if len(nodeElements) > 0 {
|
|
|
|
nodeResults := make([]trie.NodeSyncResult, len(nodeElements)/2+1)
|
|
|
|
for i, element := range nodeElements[:len(nodeResults)] {
|
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 19:21:36 +00:00
|
|
|
owner, inner := trie.ResolvePath([]byte(element.path))
|
|
|
|
data, err := reader.Node(owner, inner, element.hash)
|
2022-07-15 11:55:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve contract bytecode for %x", element.code)
|
|
|
|
}
|
|
|
|
nodeResults[i] = trie.NodeSyncResult{Path: element.path, Data: data}
|
|
|
|
}
|
|
|
|
for _, result := range nodeResults {
|
|
|
|
if err := sched.ProcessNode(result); err != nil {
|
|
|
|
t.Fatalf("failed to process result %v", err)
|
|
|
|
}
|
2020-08-21 12:10:40 +00:00
|
|
|
}
|
2022-08-19 06:00:21 +00:00
|
|
|
nodeProcessed = len(nodeResults)
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
2019-10-28 17:50:11 +00:00
|
|
|
batch := dstDb.NewBatch()
|
|
|
|
if err := sched.Commit(batch); err != nil {
|
|
|
|
t.Fatalf("failed to commit data: %v", err)
|
eth/downloader: separate state sync from queue (#14460)
* eth/downloader: separate state sync from queue
Scheduling of state node downloads hogged the downloader queue lock when
new requests were scheduled. This caused timeouts for other requests.
With this change, state sync is fully independent of all other downloads
and doesn't involve the queue at all.
State sync is started and checked on in processContent. This is slightly
awkward because processContent doesn't have a select loop. Instead, the
queue is closed by an auxiliary goroutine when state sync fails. We
tried several alternatives to this but settled on the current approach
because it's the least amount of change overall.
Handling of the pivot block has changed slightly: the queue previously
prevented import of pivot block receipts before the state of the pivot
block was available. In this commit, the receipt will be imported before
the state. This causes an annoyance where the pivot block is committed
as fast block head even when state downloads fail. Stay tuned for more
updates in this area ;)
* eth/downloader: remove cancelTimeout channel
* eth/downloader: retry state requests on timeout
* eth/downloader: improve comment
* eth/downloader: mark peers idle when state sync is done
* eth/downloader: move pivot block splitting to processContent
This change also ensures that pivot block receipts aren't imported
before the pivot block itself.
* eth/downloader: limit state node retries
* eth/downloader: improve state node error handling and retry check
* eth/downloader: remove maxStateNodeRetries
It fails the sync too much.
* eth/downloader: remove last use of cancelCh in statesync.go
Fixes TestDeliverHeadersHang*Fast and (hopefully)
the weird cancellation behaviour at the end of fast sync.
* eth/downloader: fix leak in runStateSync
* eth/downloader: don't run processFullSyncContent in LightSync mode
* eth/downloader: improve comments
* eth/downloader: fix vet, megacheck
* eth/downloader: remove unrequested tasks anyway
* eth/downloader, trie: various polishes around duplicate items
This commit explicitly tracks duplicate and unexpected state
delieveries done against a trie Sync structure, also adding
there to import info logs.
The commit moves the db batch used to commit trie changes one
level deeper so its flushed after every node insertion. This
is needed to avoid a lot of duplicate retrievals caused by
inconsistencies between Sync internals and database. A better
approach is to track not-yet-written states in trie.Sync and
flush on commit, but I'm focuing on correctness first now.
The commit fixes a regression around pivot block fail count.
The counter previously was reset to 1 if and only if a sync
cycle progressed (inserted at least 1 entry to the database).
The current code reset it already if a node was delivered,
which is not stong enough, because unless it ends up written
to disk, an attacker can just loop and attack ad infinitum.
The commit also fixes a regression around state deliveries
and timeouts. The old downloader tracked if a delivery is
stale (none of the deliveries were requestedt), in which
case it didn't mark the node idle and did not send further
requests, since it signals a past timeout. The current code
did mark it idle even on stale deliveries, which eventually
caused two requests to be in flight at the same time, making
the deliveries always stale and mass duplicating retrievals
between multiple peers.
* eth/downloader: fix state request leak
This commit fixes the hang seen sometimes while doing the state
sync. The cause of the hang was a rare combination of events:
request state data from peer, peer drops and reconnects almost
immediately. This caused a new download task to be assigned to
the peer, overwriting the old one still waiting for a timeout,
which in turned leaked the requests out, never to be retried.
The fix is to ensure that a task assignment moves any pending
one back into the retry queue.
The commit also fixes a regression with peer dropping due to
stalls. The current code considered a peer stalling if they
timed out delivering 1 item. However, the downloader never
requests only one, the minimum is 2 (attempt to fine tune
estimated latency/bandwidth). The fix is simply to drop if
a timeout is detected at 2 items.
Apart from the above bugfixes, the commit contains some code
polishes I made while debugging the hang.
* core, eth, trie: support batched trie sync db writes
* trie: rename SyncMemCache to syncMemBatch
2017-06-22 12:26:03 +00:00
|
|
|
}
|
2019-10-28 17:50:11 +00:00
|
|
|
batch.Write()
|
2020-08-28 07:50:37 +00:00
|
|
|
|
2022-07-15 11:55:51 +00:00
|
|
|
paths, nodes, codes = sched.Missing(0)
|
2022-08-19 06:00:21 +00:00
|
|
|
nodeElements = nodeElements[nodeProcessed:]
|
2022-07-15 11:55:51 +00:00
|
|
|
for i := 0; i < len(paths); i++ {
|
|
|
|
nodeElements = append(nodeElements, stateElement{
|
|
|
|
path: paths[i],
|
|
|
|
hash: nodes[i],
|
|
|
|
syncPath: trie.NewSyncPath([]byte(paths[i])),
|
|
|
|
})
|
|
|
|
}
|
2022-08-19 06:00:21 +00:00
|
|
|
codeElements = codeElements[codeProcessed:]
|
2022-07-15 11:55:51 +00:00
|
|
|
for i := 0; i < len(codes); i++ {
|
|
|
|
codeElements = append(codeElements, stateElement{
|
|
|
|
code: codes[i],
|
|
|
|
})
|
|
|
|
}
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
core/state, light, les: make signature of ContractCode hash-independent (#27209)
* core/state, light, les: make signature of ContractCode hash-independent
* push current state for feedback
* les: fix unit test
* core, les, light: fix les unittests
* core/state, trie, les, light: fix state iterator
* core, les: address comments
* les: fix lint
---------
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
2023-06-28 09:11:02 +00:00
|
|
|
// Copy the preimages from source db in order to traverse the state.
|
|
|
|
srcDb.TrieDB().WritePreimages()
|
|
|
|
copyPreimages(srcDisk, dstDb)
|
|
|
|
|
2015-10-05 16:37:56 +00:00
|
|
|
// Cross check that the two states are in sync
|
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 19:21:36 +00:00
|
|
|
checkStateAccounts(t, dstDb, ndb.Scheme(), srcRoot, srcAccounts)
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that given a root hash, a trie can sync iteratively on a single thread,
|
|
|
|
// requesting retrieval tasks and returning all of them in one go, however in a
|
|
|
|
// random order.
|
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 19:21:36 +00:00
|
|
|
func TestIterativeRandomStateSyncIndividual(t *testing.T) {
|
|
|
|
testIterativeRandomStateSync(t, 1, rawdb.HashScheme)
|
|
|
|
testIterativeRandomStateSync(t, 1, rawdb.PathScheme)
|
|
|
|
}
|
|
|
|
func TestIterativeRandomStateSyncBatched(t *testing.T) {
|
|
|
|
testIterativeRandomStateSync(t, 100, rawdb.HashScheme)
|
|
|
|
testIterativeRandomStateSync(t, 100, rawdb.PathScheme)
|
|
|
|
}
|
2015-10-05 16:37:56 +00:00
|
|
|
|
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 19:21:36 +00:00
|
|
|
func testIterativeRandomStateSync(t *testing.T, count int, scheme string) {
|
2015-10-05 16:37:56 +00:00
|
|
|
// Create a random state to copy
|
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 19:21:36 +00:00
|
|
|
srcDisk, srcDb, ndb, srcRoot, srcAccounts := makeTestState(scheme)
|
2015-10-05 16:37:56 +00:00
|
|
|
|
|
|
|
// Create a destination state and sync with the scheduler
|
2018-09-24 12:57:49 +00:00
|
|
|
dstDb := rawdb.NewMemoryDatabase()
|
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 19:21:36 +00:00
|
|
|
sched := NewStateSync(srcRoot, dstDb, nil, ndb.Scheme())
|
2015-10-05 16:37:56 +00:00
|
|
|
|
2022-07-15 11:55:51 +00:00
|
|
|
nodeQueue := make(map[string]stateElement)
|
|
|
|
codeQueue := make(map[common.Hash]struct{})
|
|
|
|
paths, nodes, codes := sched.Missing(count)
|
|
|
|
for i, path := range paths {
|
|
|
|
nodeQueue[path] = stateElement{
|
|
|
|
path: path,
|
|
|
|
hash: nodes[i],
|
|
|
|
syncPath: trie.NewSyncPath([]byte(path)),
|
|
|
|
}
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
2022-07-15 11:55:51 +00:00
|
|
|
for _, hash := range codes {
|
|
|
|
codeQueue[hash] = struct{}{}
|
|
|
|
}
|
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 19:21:36 +00:00
|
|
|
reader, err := ndb.Reader(srcRoot)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("state is not existent, %#x", srcRoot)
|
|
|
|
}
|
2022-07-15 11:55:51 +00:00
|
|
|
for len(nodeQueue)+len(codeQueue) > 0 {
|
2015-10-05 16:37:56 +00:00
|
|
|
// Fetch all the queued nodes in a random order
|
2022-07-15 11:55:51 +00:00
|
|
|
if len(codeQueue) > 0 {
|
|
|
|
results := make([]trie.CodeSyncResult, 0, len(codeQueue))
|
|
|
|
for hash := range codeQueue {
|
core/state, light, les: make signature of ContractCode hash-independent (#27209)
* core/state, light, les: make signature of ContractCode hash-independent
* push current state for feedback
* les: fix unit test
* core, les, light: fix les unittests
* core/state, trie, les, light: fix state iterator
* core, les: address comments
* les: fix lint
---------
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
2023-06-28 09:11:02 +00:00
|
|
|
data, err := srcDb.ContractCode(common.Address{}, hash)
|
2022-07-15 11:55:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve node data for %x", hash)
|
|
|
|
}
|
|
|
|
results = append(results, trie.CodeSyncResult{Hash: hash, Data: data})
|
2020-08-21 12:10:40 +00:00
|
|
|
}
|
2022-07-15 11:55:51 +00:00
|
|
|
for _, result := range results {
|
|
|
|
if err := sched.ProcessCode(result); err != nil {
|
|
|
|
t.Fatalf("failed to process result %v", err)
|
|
|
|
}
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-15 11:55:51 +00:00
|
|
|
if len(nodeQueue) > 0 {
|
|
|
|
results := make([]trie.NodeSyncResult, 0, len(nodeQueue))
|
|
|
|
for path, element := range nodeQueue {
|
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 19:21:36 +00:00
|
|
|
owner, inner := trie.ResolvePath([]byte(element.path))
|
|
|
|
data, err := reader.Node(owner, inner, element.hash)
|
2022-07-15 11:55:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve node data for %x %v %v", element.hash, []byte(element.path), element.path)
|
|
|
|
}
|
|
|
|
results = append(results, trie.NodeSyncResult{Path: path, Data: data})
|
|
|
|
}
|
|
|
|
for _, result := range results {
|
|
|
|
if err := sched.ProcessNode(result); err != nil {
|
|
|
|
t.Fatalf("failed to process result %v", err)
|
|
|
|
}
|
2020-08-21 12:10:40 +00:00
|
|
|
}
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
2019-10-28 17:50:11 +00:00
|
|
|
batch := dstDb.NewBatch()
|
|
|
|
if err := sched.Commit(batch); err != nil {
|
|
|
|
t.Fatalf("failed to commit data: %v", err)
|
eth/downloader: separate state sync from queue (#14460)
* eth/downloader: separate state sync from queue
Scheduling of state node downloads hogged the downloader queue lock when
new requests were scheduled. This caused timeouts for other requests.
With this change, state sync is fully independent of all other downloads
and doesn't involve the queue at all.
State sync is started and checked on in processContent. This is slightly
awkward because processContent doesn't have a select loop. Instead, the
queue is closed by an auxiliary goroutine when state sync fails. We
tried several alternatives to this but settled on the current approach
because it's the least amount of change overall.
Handling of the pivot block has changed slightly: the queue previously
prevented import of pivot block receipts before the state of the pivot
block was available. In this commit, the receipt will be imported before
the state. This causes an annoyance where the pivot block is committed
as fast block head even when state downloads fail. Stay tuned for more
updates in this area ;)
* eth/downloader: remove cancelTimeout channel
* eth/downloader: retry state requests on timeout
* eth/downloader: improve comment
* eth/downloader: mark peers idle when state sync is done
* eth/downloader: move pivot block splitting to processContent
This change also ensures that pivot block receipts aren't imported
before the pivot block itself.
* eth/downloader: limit state node retries
* eth/downloader: improve state node error handling and retry check
* eth/downloader: remove maxStateNodeRetries
It fails the sync too much.
* eth/downloader: remove last use of cancelCh in statesync.go
Fixes TestDeliverHeadersHang*Fast and (hopefully)
the weird cancellation behaviour at the end of fast sync.
* eth/downloader: fix leak in runStateSync
* eth/downloader: don't run processFullSyncContent in LightSync mode
* eth/downloader: improve comments
* eth/downloader: fix vet, megacheck
* eth/downloader: remove unrequested tasks anyway
* eth/downloader, trie: various polishes around duplicate items
This commit explicitly tracks duplicate and unexpected state
delieveries done against a trie Sync structure, also adding
there to import info logs.
The commit moves the db batch used to commit trie changes one
level deeper so its flushed after every node insertion. This
is needed to avoid a lot of duplicate retrievals caused by
inconsistencies between Sync internals and database. A better
approach is to track not-yet-written states in trie.Sync and
flush on commit, but I'm focuing on correctness first now.
The commit fixes a regression around pivot block fail count.
The counter previously was reset to 1 if and only if a sync
cycle progressed (inserted at least 1 entry to the database).
The current code reset it already if a node was delivered,
which is not stong enough, because unless it ends up written
to disk, an attacker can just loop and attack ad infinitum.
The commit also fixes a regression around state deliveries
and timeouts. The old downloader tracked if a delivery is
stale (none of the deliveries were requestedt), in which
case it didn't mark the node idle and did not send further
requests, since it signals a past timeout. The current code
did mark it idle even on stale deliveries, which eventually
caused two requests to be in flight at the same time, making
the deliveries always stale and mass duplicating retrievals
between multiple peers.
* eth/downloader: fix state request leak
This commit fixes the hang seen sometimes while doing the state
sync. The cause of the hang was a rare combination of events:
request state data from peer, peer drops and reconnects almost
immediately. This caused a new download task to be assigned to
the peer, overwriting the old one still waiting for a timeout,
which in turned leaked the requests out, never to be retried.
The fix is to ensure that a task assignment moves any pending
one back into the retry queue.
The commit also fixes a regression with peer dropping due to
stalls. The current code considered a peer stalling if they
timed out delivering 1 item. However, the downloader never
requests only one, the minimum is 2 (attempt to fine tune
estimated latency/bandwidth). The fix is simply to drop if
a timeout is detected at 2 items.
Apart from the above bugfixes, the commit contains some code
polishes I made while debugging the hang.
* core, eth, trie: support batched trie sync db writes
* trie: rename SyncMemCache to syncMemBatch
2017-06-22 12:26:03 +00:00
|
|
|
}
|
2019-10-28 17:50:11 +00:00
|
|
|
batch.Write()
|
2020-08-28 07:50:37 +00:00
|
|
|
|
2022-07-15 11:55:51 +00:00
|
|
|
nodeQueue = make(map[string]stateElement)
|
|
|
|
codeQueue = make(map[common.Hash]struct{})
|
|
|
|
paths, nodes, codes := sched.Missing(count)
|
|
|
|
for i, path := range paths {
|
|
|
|
nodeQueue[path] = stateElement{
|
|
|
|
path: path,
|
|
|
|
hash: nodes[i],
|
|
|
|
syncPath: trie.NewSyncPath([]byte(path)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, hash := range codes {
|
|
|
|
codeQueue[hash] = struct{}{}
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
|
|
|
}
|
core/state, light, les: make signature of ContractCode hash-independent (#27209)
* core/state, light, les: make signature of ContractCode hash-independent
* push current state for feedback
* les: fix unit test
* core, les, light: fix les unittests
* core/state, trie, les, light: fix state iterator
* core, les: address comments
* les: fix lint
---------
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
2023-06-28 09:11:02 +00:00
|
|
|
// Copy the preimages from source db in order to traverse the state.
|
|
|
|
srcDb.TrieDB().WritePreimages()
|
|
|
|
copyPreimages(srcDisk, dstDb)
|
|
|
|
|
2015-10-05 16:37:56 +00:00
|
|
|
// Cross check that the two states are in sync
|
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 19:21:36 +00:00
|
|
|
checkStateAccounts(t, dstDb, ndb.Scheme(), srcRoot, srcAccounts)
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that the trie scheduler can correctly reconstruct the state even if only
|
|
|
|
// partial results are returned (Even those randomly), others sent only later.
|
|
|
|
func TestIterativeRandomDelayedStateSync(t *testing.T) {
|
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 19:21:36 +00:00
|
|
|
testIterativeRandomDelayedStateSync(t, rawdb.HashScheme)
|
|
|
|
testIterativeRandomDelayedStateSync(t, rawdb.PathScheme)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testIterativeRandomDelayedStateSync(t *testing.T, scheme string) {
|
2015-10-05 16:37:56 +00:00
|
|
|
// Create a random state to copy
|
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 19:21:36 +00:00
|
|
|
srcDisk, srcDb, ndb, srcRoot, srcAccounts := makeTestState(scheme)
|
2015-10-05 16:37:56 +00:00
|
|
|
|
|
|
|
// Create a destination state and sync with the scheduler
|
2018-09-24 12:57:49 +00:00
|
|
|
dstDb := rawdb.NewMemoryDatabase()
|
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 19:21:36 +00:00
|
|
|
sched := NewStateSync(srcRoot, dstDb, nil, ndb.Scheme())
|
2015-10-05 16:37:56 +00:00
|
|
|
|
2022-07-15 11:55:51 +00:00
|
|
|
nodeQueue := make(map[string]stateElement)
|
|
|
|
codeQueue := make(map[common.Hash]struct{})
|
|
|
|
paths, nodes, codes := sched.Missing(0)
|
|
|
|
for i, path := range paths {
|
|
|
|
nodeQueue[path] = stateElement{
|
|
|
|
path: path,
|
|
|
|
hash: nodes[i],
|
|
|
|
syncPath: trie.NewSyncPath([]byte(path)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, hash := range codes {
|
|
|
|
codeQueue[hash] = struct{}{}
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
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 19:21:36 +00:00
|
|
|
reader, err := ndb.Reader(srcRoot)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("state is not existent, %#x", srcRoot)
|
|
|
|
}
|
2022-07-15 11:55:51 +00:00
|
|
|
for len(nodeQueue)+len(codeQueue) > 0 {
|
2015-10-05 16:37:56 +00:00
|
|
|
// Sync only half of the scheduled nodes, even those in random order
|
2022-07-15 11:55:51 +00:00
|
|
|
if len(codeQueue) > 0 {
|
|
|
|
results := make([]trie.CodeSyncResult, 0, len(codeQueue)/2+1)
|
|
|
|
for hash := range codeQueue {
|
|
|
|
delete(codeQueue, hash)
|
2015-10-05 16:37:56 +00:00
|
|
|
|
core/state, light, les: make signature of ContractCode hash-independent (#27209)
* core/state, light, les: make signature of ContractCode hash-independent
* push current state for feedback
* les: fix unit test
* core, les, light: fix les unittests
* core/state, trie, les, light: fix state iterator
* core, les: address comments
* les: fix lint
---------
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
2023-06-28 09:11:02 +00:00
|
|
|
data, err := srcDb.ContractCode(common.Address{}, hash)
|
2022-07-15 11:55:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve node data for %x", hash)
|
|
|
|
}
|
|
|
|
results = append(results, trie.CodeSyncResult{Hash: hash, Data: data})
|
|
|
|
|
|
|
|
if len(results) >= cap(results) {
|
|
|
|
break
|
|
|
|
}
|
2020-08-21 12:10:40 +00:00
|
|
|
}
|
2022-07-15 11:55:51 +00:00
|
|
|
for _, result := range results {
|
|
|
|
if err := sched.ProcessCode(result); err != nil {
|
|
|
|
t.Fatalf("failed to process result %v", err)
|
|
|
|
}
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
2022-07-15 11:55:51 +00:00
|
|
|
}
|
|
|
|
if len(nodeQueue) > 0 {
|
|
|
|
results := make([]trie.NodeSyncResult, 0, len(nodeQueue)/2+1)
|
|
|
|
for path, element := range nodeQueue {
|
|
|
|
delete(nodeQueue, path)
|
2015-10-05 16:37:56 +00:00
|
|
|
|
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 19:21:36 +00:00
|
|
|
owner, inner := trie.ResolvePath([]byte(element.path))
|
|
|
|
data, err := reader.Node(owner, inner, element.hash)
|
2022-07-15 11:55:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve node data for %x", element.hash)
|
|
|
|
}
|
|
|
|
results = append(results, trie.NodeSyncResult{Path: path, Data: data})
|
|
|
|
|
|
|
|
if len(results) >= cap(results) {
|
|
|
|
break
|
|
|
|
}
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
2022-07-15 11:55:51 +00:00
|
|
|
// Feed the retrieved results back and queue new tasks
|
|
|
|
for _, result := range results {
|
|
|
|
if err := sched.ProcessNode(result); err != nil {
|
|
|
|
t.Fatalf("failed to process result %v", err)
|
|
|
|
}
|
2020-08-21 12:10:40 +00:00
|
|
|
}
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
2019-10-28 17:50:11 +00:00
|
|
|
batch := dstDb.NewBatch()
|
|
|
|
if err := sched.Commit(batch); err != nil {
|
|
|
|
t.Fatalf("failed to commit data: %v", err)
|
eth/downloader: separate state sync from queue (#14460)
* eth/downloader: separate state sync from queue
Scheduling of state node downloads hogged the downloader queue lock when
new requests were scheduled. This caused timeouts for other requests.
With this change, state sync is fully independent of all other downloads
and doesn't involve the queue at all.
State sync is started and checked on in processContent. This is slightly
awkward because processContent doesn't have a select loop. Instead, the
queue is closed by an auxiliary goroutine when state sync fails. We
tried several alternatives to this but settled on the current approach
because it's the least amount of change overall.
Handling of the pivot block has changed slightly: the queue previously
prevented import of pivot block receipts before the state of the pivot
block was available. In this commit, the receipt will be imported before
the state. This causes an annoyance where the pivot block is committed
as fast block head even when state downloads fail. Stay tuned for more
updates in this area ;)
* eth/downloader: remove cancelTimeout channel
* eth/downloader: retry state requests on timeout
* eth/downloader: improve comment
* eth/downloader: mark peers idle when state sync is done
* eth/downloader: move pivot block splitting to processContent
This change also ensures that pivot block receipts aren't imported
before the pivot block itself.
* eth/downloader: limit state node retries
* eth/downloader: improve state node error handling and retry check
* eth/downloader: remove maxStateNodeRetries
It fails the sync too much.
* eth/downloader: remove last use of cancelCh in statesync.go
Fixes TestDeliverHeadersHang*Fast and (hopefully)
the weird cancellation behaviour at the end of fast sync.
* eth/downloader: fix leak in runStateSync
* eth/downloader: don't run processFullSyncContent in LightSync mode
* eth/downloader: improve comments
* eth/downloader: fix vet, megacheck
* eth/downloader: remove unrequested tasks anyway
* eth/downloader, trie: various polishes around duplicate items
This commit explicitly tracks duplicate and unexpected state
delieveries done against a trie Sync structure, also adding
there to import info logs.
The commit moves the db batch used to commit trie changes one
level deeper so its flushed after every node insertion. This
is needed to avoid a lot of duplicate retrievals caused by
inconsistencies between Sync internals and database. A better
approach is to track not-yet-written states in trie.Sync and
flush on commit, but I'm focuing on correctness first now.
The commit fixes a regression around pivot block fail count.
The counter previously was reset to 1 if and only if a sync
cycle progressed (inserted at least 1 entry to the database).
The current code reset it already if a node was delivered,
which is not stong enough, because unless it ends up written
to disk, an attacker can just loop and attack ad infinitum.
The commit also fixes a regression around state deliveries
and timeouts. The old downloader tracked if a delivery is
stale (none of the deliveries were requestedt), in which
case it didn't mark the node idle and did not send further
requests, since it signals a past timeout. The current code
did mark it idle even on stale deliveries, which eventually
caused two requests to be in flight at the same time, making
the deliveries always stale and mass duplicating retrievals
between multiple peers.
* eth/downloader: fix state request leak
This commit fixes the hang seen sometimes while doing the state
sync. The cause of the hang was a rare combination of events:
request state data from peer, peer drops and reconnects almost
immediately. This caused a new download task to be assigned to
the peer, overwriting the old one still waiting for a timeout,
which in turned leaked the requests out, never to be retried.
The fix is to ensure that a task assignment moves any pending
one back into the retry queue.
The commit also fixes a regression with peer dropping due to
stalls. The current code considered a peer stalling if they
timed out delivering 1 item. However, the downloader never
requests only one, the minimum is 2 (attempt to fine tune
estimated latency/bandwidth). The fix is simply to drop if
a timeout is detected at 2 items.
Apart from the above bugfixes, the commit contains some code
polishes I made while debugging the hang.
* core, eth, trie: support batched trie sync db writes
* trie: rename SyncMemCache to syncMemBatch
2017-06-22 12:26:03 +00:00
|
|
|
}
|
2019-10-28 17:50:11 +00:00
|
|
|
batch.Write()
|
2022-07-15 11:55:51 +00:00
|
|
|
|
|
|
|
paths, nodes, codes := sched.Missing(0)
|
|
|
|
for i, path := range paths {
|
|
|
|
nodeQueue[path] = stateElement{
|
|
|
|
path: path,
|
|
|
|
hash: nodes[i],
|
|
|
|
syncPath: trie.NewSyncPath([]byte(path)),
|
|
|
|
}
|
2020-08-28 07:50:37 +00:00
|
|
|
}
|
2022-07-15 11:55:51 +00:00
|
|
|
for _, hash := range codes {
|
|
|
|
codeQueue[hash] = struct{}{}
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
|
|
|
}
|
core/state, light, les: make signature of ContractCode hash-independent (#27209)
* core/state, light, les: make signature of ContractCode hash-independent
* push current state for feedback
* les: fix unit test
* core, les, light: fix les unittests
* core/state, trie, les, light: fix state iterator
* core, les: address comments
* les: fix lint
---------
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
2023-06-28 09:11:02 +00:00
|
|
|
// Copy the preimages from source db in order to traverse the state.
|
|
|
|
srcDb.TrieDB().WritePreimages()
|
|
|
|
copyPreimages(srcDisk, dstDb)
|
|
|
|
|
2015-10-05 16:37:56 +00:00
|
|
|
// Cross check that the two states are in sync
|
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 19:21:36 +00:00
|
|
|
checkStateAccounts(t, dstDb, ndb.Scheme(), srcRoot, srcAccounts)
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
2015-12-28 13:20:37 +00:00
|
|
|
|
|
|
|
// Tests that at any point in time during a sync, only complete sub-tries are in
|
|
|
|
// the database.
|
|
|
|
func TestIncompleteStateSync(t *testing.T) {
|
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 19:21:36 +00:00
|
|
|
testIncompleteStateSync(t, rawdb.HashScheme)
|
|
|
|
testIncompleteStateSync(t, rawdb.PathScheme)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testIncompleteStateSync(t *testing.T, scheme string) {
|
2015-12-28 13:20:37 +00:00
|
|
|
// Create a random state to copy
|
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 19:21:36 +00:00
|
|
|
db, srcDb, ndb, srcRoot, srcAccounts := makeTestState(scheme)
|
2017-06-27 13:57:06 +00:00
|
|
|
|
2020-12-10 13:48:32 +00:00
|
|
|
// isCodeLookup to save some hashing
|
|
|
|
var isCode = make(map[common.Hash]struct{})
|
|
|
|
for _, acc := range srcAccounts {
|
|
|
|
if len(acc.code) > 0 {
|
|
|
|
isCode[crypto.Keccak256Hash(acc.code)] = struct{}{}
|
2020-08-21 12:10:40 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-21 11:12:27 +00:00
|
|
|
isCode[types.EmptyCodeHash] = struct{}{}
|
2015-12-28 13:20:37 +00:00
|
|
|
|
|
|
|
// Create a destination state and sync with the scheduler
|
2018-09-24 12:57:49 +00:00
|
|
|
dstDb := rawdb.NewMemoryDatabase()
|
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 19:21:36 +00:00
|
|
|
sched := NewStateSync(srcRoot, dstDb, nil, ndb.Scheme())
|
2015-12-28 13:20:37 +00:00
|
|
|
|
2022-07-15 11:55:51 +00:00
|
|
|
var (
|
2022-11-28 13:31:28 +00:00
|
|
|
addedCodes []common.Hash
|
|
|
|
addedPaths []string
|
|
|
|
addedHashes []common.Hash
|
2022-07-15 11:55:51 +00:00
|
|
|
)
|
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 19:21:36 +00:00
|
|
|
reader, err := ndb.Reader(srcRoot)
|
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 19:31:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("state is not available %x", srcRoot)
|
|
|
|
}
|
2022-07-15 11:55:51 +00:00
|
|
|
nodeQueue := make(map[string]stateElement)
|
|
|
|
codeQueue := make(map[common.Hash]struct{})
|
|
|
|
paths, nodes, codes := sched.Missing(1)
|
|
|
|
for i, path := range paths {
|
|
|
|
nodeQueue[path] = stateElement{
|
|
|
|
path: path,
|
|
|
|
hash: nodes[i],
|
|
|
|
syncPath: trie.NewSyncPath([]byte(path)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, hash := range codes {
|
|
|
|
codeQueue[hash] = struct{}{}
|
|
|
|
}
|
|
|
|
for len(nodeQueue)+len(codeQueue) > 0 {
|
2015-12-28 13:20:37 +00:00
|
|
|
// Fetch a batch of state nodes
|
2022-07-15 11:55:51 +00:00
|
|
|
if len(codeQueue) > 0 {
|
|
|
|
results := make([]trie.CodeSyncResult, 0, len(codeQueue))
|
|
|
|
for hash := range codeQueue {
|
core/state, light, les: make signature of ContractCode hash-independent (#27209)
* core/state, light, les: make signature of ContractCode hash-independent
* push current state for feedback
* les: fix unit test
* core, les, light: fix les unittests
* core/state, trie, les, light: fix state iterator
* core, les: address comments
* les: fix lint
---------
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
2023-06-28 09:11:02 +00:00
|
|
|
data, err := srcDb.ContractCode(common.Address{}, hash)
|
2022-07-15 11:55:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve node data for %x", hash)
|
|
|
|
}
|
|
|
|
results = append(results, trie.CodeSyncResult{Hash: hash, Data: data})
|
|
|
|
addedCodes = append(addedCodes, hash)
|
2020-08-21 12:10:40 +00:00
|
|
|
}
|
2022-07-15 11:55:51 +00:00
|
|
|
// Process each of the state nodes
|
|
|
|
for _, result := range results {
|
|
|
|
if err := sched.ProcessCode(result); err != nil {
|
|
|
|
t.Fatalf("failed to process result %v", err)
|
|
|
|
}
|
2015-12-28 13:20:37 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-15 11:55:51 +00:00
|
|
|
if len(nodeQueue) > 0 {
|
|
|
|
results := make([]trie.NodeSyncResult, 0, len(nodeQueue))
|
2022-11-28 13:31:28 +00:00
|
|
|
for path, element := range nodeQueue {
|
2023-05-09 07:11:04 +00:00
|
|
|
owner, inner := trie.ResolvePath([]byte(element.path))
|
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 19:31:45 +00:00
|
|
|
data, err := reader.Node(owner, inner, element.hash)
|
2022-07-15 11:55:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve node data for %x", element.hash)
|
|
|
|
}
|
2022-11-28 13:31:28 +00:00
|
|
|
results = append(results, trie.NodeSyncResult{Path: path, Data: data})
|
2022-07-15 11:55:51 +00:00
|
|
|
|
|
|
|
if element.hash != srcRoot {
|
2022-11-28 13:31:28 +00:00
|
|
|
addedPaths = append(addedPaths, element.path)
|
|
|
|
addedHashes = append(addedHashes, element.hash)
|
2022-07-15 11:55:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Process each of the state nodes
|
|
|
|
for _, result := range results {
|
|
|
|
if err := sched.ProcessNode(result); err != nil {
|
|
|
|
t.Fatalf("failed to process result %v", err)
|
|
|
|
}
|
2020-08-21 12:10:40 +00:00
|
|
|
}
|
2015-12-28 13:20:37 +00:00
|
|
|
}
|
2019-10-28 17:50:11 +00:00
|
|
|
batch := dstDb.NewBatch()
|
|
|
|
if err := sched.Commit(batch); err != nil {
|
|
|
|
t.Fatalf("failed to commit data: %v", err)
|
eth/downloader: separate state sync from queue (#14460)
* eth/downloader: separate state sync from queue
Scheduling of state node downloads hogged the downloader queue lock when
new requests were scheduled. This caused timeouts for other requests.
With this change, state sync is fully independent of all other downloads
and doesn't involve the queue at all.
State sync is started and checked on in processContent. This is slightly
awkward because processContent doesn't have a select loop. Instead, the
queue is closed by an auxiliary goroutine when state sync fails. We
tried several alternatives to this but settled on the current approach
because it's the least amount of change overall.
Handling of the pivot block has changed slightly: the queue previously
prevented import of pivot block receipts before the state of the pivot
block was available. In this commit, the receipt will be imported before
the state. This causes an annoyance where the pivot block is committed
as fast block head even when state downloads fail. Stay tuned for more
updates in this area ;)
* eth/downloader: remove cancelTimeout channel
* eth/downloader: retry state requests on timeout
* eth/downloader: improve comment
* eth/downloader: mark peers idle when state sync is done
* eth/downloader: move pivot block splitting to processContent
This change also ensures that pivot block receipts aren't imported
before the pivot block itself.
* eth/downloader: limit state node retries
* eth/downloader: improve state node error handling and retry check
* eth/downloader: remove maxStateNodeRetries
It fails the sync too much.
* eth/downloader: remove last use of cancelCh in statesync.go
Fixes TestDeliverHeadersHang*Fast and (hopefully)
the weird cancellation behaviour at the end of fast sync.
* eth/downloader: fix leak in runStateSync
* eth/downloader: don't run processFullSyncContent in LightSync mode
* eth/downloader: improve comments
* eth/downloader: fix vet, megacheck
* eth/downloader: remove unrequested tasks anyway
* eth/downloader, trie: various polishes around duplicate items
This commit explicitly tracks duplicate and unexpected state
delieveries done against a trie Sync structure, also adding
there to import info logs.
The commit moves the db batch used to commit trie changes one
level deeper so its flushed after every node insertion. This
is needed to avoid a lot of duplicate retrievals caused by
inconsistencies between Sync internals and database. A better
approach is to track not-yet-written states in trie.Sync and
flush on commit, but I'm focuing on correctness first now.
The commit fixes a regression around pivot block fail count.
The counter previously was reset to 1 if and only if a sync
cycle progressed (inserted at least 1 entry to the database).
The current code reset it already if a node was delivered,
which is not stong enough, because unless it ends up written
to disk, an attacker can just loop and attack ad infinitum.
The commit also fixes a regression around state deliveries
and timeouts. The old downloader tracked if a delivery is
stale (none of the deliveries were requestedt), in which
case it didn't mark the node idle and did not send further
requests, since it signals a past timeout. The current code
did mark it idle even on stale deliveries, which eventually
caused two requests to be in flight at the same time, making
the deliveries always stale and mass duplicating retrievals
between multiple peers.
* eth/downloader: fix state request leak
This commit fixes the hang seen sometimes while doing the state
sync. The cause of the hang was a rare combination of events:
request state data from peer, peer drops and reconnects almost
immediately. This caused a new download task to be assigned to
the peer, overwriting the old one still waiting for a timeout,
which in turned leaked the requests out, never to be retried.
The fix is to ensure that a task assignment moves any pending
one back into the retry queue.
The commit also fixes a regression with peer dropping due to
stalls. The current code considered a peer stalling if they
timed out delivering 1 item. However, the downloader never
requests only one, the minimum is 2 (attempt to fine tune
estimated latency/bandwidth). The fix is simply to drop if
a timeout is detected at 2 items.
Apart from the above bugfixes, the commit contains some code
polishes I made while debugging the hang.
* core, eth, trie: support batched trie sync db writes
* trie: rename SyncMemCache to syncMemBatch
2017-06-22 12:26:03 +00:00
|
|
|
}
|
2019-10-28 17:50:11 +00:00
|
|
|
batch.Write()
|
2022-07-15 11:55:51 +00:00
|
|
|
|
2015-12-28 13:20:37 +00:00
|
|
|
// Fetch the next batch to retrieve
|
2022-07-15 11:55:51 +00:00
|
|
|
nodeQueue = make(map[string]stateElement)
|
|
|
|
codeQueue = make(map[common.Hash]struct{})
|
|
|
|
paths, nodes, codes := sched.Missing(1)
|
|
|
|
for i, path := range paths {
|
|
|
|
nodeQueue[path] = stateElement{
|
|
|
|
path: path,
|
|
|
|
hash: nodes[i],
|
|
|
|
syncPath: trie.NewSyncPath([]byte(path)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, hash := range codes {
|
|
|
|
codeQueue[hash] = struct{}{}
|
|
|
|
}
|
2015-12-28 13:20:37 +00:00
|
|
|
}
|
core/state, light, les: make signature of ContractCode hash-independent (#27209)
* core/state, light, les: make signature of ContractCode hash-independent
* push current state for feedback
* les: fix unit test
* core, les, light: fix les unittests
* core/state, trie, les, light: fix state iterator
* core, les: address comments
* les: fix lint
---------
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
2023-06-28 09:11:02 +00:00
|
|
|
// Copy the preimages from source db in order to traverse the state.
|
|
|
|
srcDb.TrieDB().WritePreimages()
|
|
|
|
copyPreimages(db, dstDb)
|
|
|
|
|
2015-12-28 13:20:37 +00:00
|
|
|
// Sanity check that removing any node from the database is detected
|
2022-07-15 11:55:51 +00:00
|
|
|
for _, node := range addedCodes {
|
|
|
|
val := rawdb.ReadCode(dstDb, node)
|
|
|
|
rawdb.DeleteCode(dstDb, node)
|
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 19:21:36 +00:00
|
|
|
if err := checkStateConsistency(dstDb, ndb.Scheme(), srcRoot); err == nil {
|
2022-07-15 11:55:51 +00:00
|
|
|
t.Errorf("trie inconsistency not caught, missing: %x", node)
|
2020-08-21 12:10:40 +00:00
|
|
|
}
|
2022-07-15 11:55:51 +00:00
|
|
|
rawdb.WriteCode(dstDb, node, val)
|
|
|
|
}
|
2022-11-28 13:31:28 +00:00
|
|
|
for i, path := range addedPaths {
|
|
|
|
owner, inner := trie.ResolvePath([]byte(path))
|
|
|
|
hash := addedHashes[i]
|
2023-02-06 15:28:40 +00:00
|
|
|
val := rawdb.ReadTrieNode(dstDb, owner, inner, hash, scheme)
|
2022-11-28 13:31:28 +00:00
|
|
|
if val == nil {
|
|
|
|
t.Error("missing trie node")
|
|
|
|
}
|
2023-02-06 15:28:40 +00:00
|
|
|
rawdb.DeleteTrieNode(dstDb, owner, inner, hash, scheme)
|
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 19:21:36 +00:00
|
|
|
if err := checkStateConsistency(dstDb, scheme, srcRoot); err == nil {
|
2022-11-28 13:31:28 +00:00
|
|
|
t.Errorf("trie inconsistency not caught, missing: %v", path)
|
2020-08-21 12:10:40 +00:00
|
|
|
}
|
2023-02-06 15:28:40 +00:00
|
|
|
rawdb.WriteTrieNode(dstDb, owner, inner, hash, val, scheme)
|
2015-12-28 13:20:37 +00:00
|
|
|
}
|
|
|
|
}
|
core/state, light, les: make signature of ContractCode hash-independent (#27209)
* core/state, light, les: make signature of ContractCode hash-independent
* push current state for feedback
* les: fix unit test
* core, les, light: fix les unittests
* core/state, trie, les, light: fix state iterator
* core, les: address comments
* les: fix lint
---------
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
2023-06-28 09:11:02 +00:00
|
|
|
|
|
|
|
func copyPreimages(srcDb, dstDb ethdb.Database) {
|
|
|
|
it := srcDb.NewIterator(rawdb.PreimagePrefix, nil)
|
|
|
|
defer it.Release()
|
|
|
|
|
|
|
|
preimages := make(map[common.Hash][]byte)
|
|
|
|
for it.Next() {
|
|
|
|
hash := it.Key()[len(rawdb.PreimagePrefix):]
|
|
|
|
preimages[common.BytesToHash(hash)] = common.CopyBytes(it.Value())
|
|
|
|
}
|
|
|
|
rawdb.WritePreimages(dstDb, preimages)
|
|
|
|
}
|