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 trie
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2020-08-28 07:50:37 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2018-09-24 12:57:49 +00:00
|
|
|
"github.com/ethereum/go-ethereum/ethdb/memorydb"
|
2015-10-05 16:37:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// makeTestTrie create a sample test trie to test node-wise reconstruction.
|
2020-08-28 07:50:37 +00:00
|
|
|
func makeTestTrie() (*Database, *SecureTrie, map[string][]byte) {
|
2015-10-05 16:37:56 +00:00
|
|
|
// Create an empty trie
|
2018-09-24 12:57:49 +00:00
|
|
|
triedb := NewDatabase(memorydb.New())
|
2020-08-28 07:50:37 +00:00
|
|
|
trie, _ := NewSecure(common.Hash{}, triedb)
|
2015-10-05 16:37:56 +00:00
|
|
|
|
|
|
|
// Fill it with some arbitrary data
|
|
|
|
content := make(map[string][]byte)
|
|
|
|
for i := byte(0); i < 255; i++ {
|
2015-12-28 13:20:37 +00:00
|
|
|
// Map the same data under multiple keys
|
2015-10-05 16:37:56 +00:00
|
|
|
key, val := common.LeftPadBytes([]byte{1, i}, 32), []byte{i}
|
|
|
|
content[string(key)] = val
|
|
|
|
trie.Update(key, val)
|
|
|
|
|
|
|
|
key, val = common.LeftPadBytes([]byte{2, i}, 32), []byte{i}
|
|
|
|
content[string(key)] = val
|
|
|
|
trie.Update(key, val)
|
2015-12-28 13:20:37 +00:00
|
|
|
|
2017-06-12 12:45:17 +00:00
|
|
|
// Add some other data to inflate the trie
|
2015-12-28 13:20:37 +00:00
|
|
|
for j := byte(3); j < 13; j++ {
|
|
|
|
key, val = common.LeftPadBytes([]byte{j, i}, 32), []byte{j, i}
|
|
|
|
content[string(key)] = val
|
|
|
|
trie.Update(key, val)
|
|
|
|
}
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
2018-02-05 16:40:32 +00:00
|
|
|
trie.Commit(nil)
|
2015-10-05 16:37:56 +00:00
|
|
|
|
|
|
|
// Return the generated trie
|
2018-02-05 16:40:32 +00:00
|
|
|
return triedb, trie, content
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// checkTrieContents cross references a reconstructed trie with an expected data
|
|
|
|
// content map.
|
2018-02-05 16:40:32 +00:00
|
|
|
func checkTrieContents(t *testing.T, db *Database, root []byte, content map[string][]byte) {
|
2015-12-28 13:20:37 +00:00
|
|
|
// Check root availability and trie contents
|
2020-08-28 07:50:37 +00:00
|
|
|
trie, err := NewSecure(common.BytesToHash(root), db)
|
2015-10-05 16:37:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create trie at %x: %v", root, err)
|
|
|
|
}
|
2015-12-28 13:20:37 +00:00
|
|
|
if err := checkTrieConsistency(db, common.BytesToHash(root)); err != nil {
|
|
|
|
t.Fatalf("inconsistent trie at %x: %v", root, err)
|
|
|
|
}
|
2015-10-05 16:37:56 +00:00
|
|
|
for key, val := range content {
|
2017-01-06 15:44:20 +00:00
|
|
|
if have := trie.Get([]byte(key)); !bytes.Equal(have, val) {
|
2015-10-05 16:37:56 +00:00
|
|
|
t.Errorf("entry %x: content mismatch: have %x, want %x", key, have, val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-16 10:37:00 +00:00
|
|
|
// checkTrieConsistency checks that all nodes in a trie are indeed present.
|
2018-02-05 16:40:32 +00:00
|
|
|
func checkTrieConsistency(db *Database, root common.Hash) error {
|
2015-12-28 13:20:37 +00:00
|
|
|
// Create and iterate a trie rooted in a subnode
|
2020-08-28 07:50:37 +00:00
|
|
|
trie, err := NewSecure(root, db)
|
2015-12-28 13:20:37 +00:00
|
|
|
if err != nil {
|
2017-06-12 12:45:17 +00:00
|
|
|
return nil // Consider a non existent state consistent
|
2015-12-28 13:20:37 +00:00
|
|
|
}
|
2017-04-13 12:41:24 +00:00
|
|
|
it := trie.NodeIterator(nil)
|
2017-02-22 22:49:34 +00:00
|
|
|
for it.Next(true) {
|
2015-12-28 13:20:37 +00:00
|
|
|
}
|
2017-02-22 22:49:34 +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 trie is not scheduled for syncing.
|
2018-05-29 15:48:43 +00:00
|
|
|
func TestEmptySync(t *testing.T) {
|
2018-09-24 12:57:49 +00:00
|
|
|
dbA := NewDatabase(memorydb.New())
|
|
|
|
dbB := NewDatabase(memorydb.New())
|
2018-05-09 12:24:25 +00:00
|
|
|
emptyA, _ := New(common.Hash{}, dbA)
|
|
|
|
emptyB, _ := New(emptyRoot, dbB)
|
2015-10-05 16:37:56 +00:00
|
|
|
|
|
|
|
for i, trie := range []*Trie{emptyA, emptyB} {
|
2020-08-28 07:50:37 +00:00
|
|
|
sync := NewSync(trie.Hash(), memorydb.New(), nil, NewSyncBloom(1, memorydb.New()))
|
|
|
|
if nodes, paths, codes := sync.Missing(1); len(nodes) != 0 || len(paths) != 0 || len(codes) != 0 {
|
|
|
|
t.Errorf("test %d: content requested for empty trie: %v, %v, %v", i, nodes, paths, codes)
|
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.
|
2020-08-28 07:50:37 +00:00
|
|
|
func TestIterativeSyncIndividual(t *testing.T) { testIterativeSync(t, 1, false) }
|
|
|
|
func TestIterativeSyncBatched(t *testing.T) { testIterativeSync(t, 100, false) }
|
|
|
|
func TestIterativeSyncIndividualByPath(t *testing.T) { testIterativeSync(t, 1, true) }
|
|
|
|
func TestIterativeSyncBatchedByPath(t *testing.T) { testIterativeSync(t, 100, true) }
|
2015-10-05 16:37:56 +00:00
|
|
|
|
2020-08-28 07:50:37 +00:00
|
|
|
func testIterativeSync(t *testing.T, count int, bypath bool) {
|
2015-10-05 16:37:56 +00:00
|
|
|
// Create a random trie to copy
|
|
|
|
srcDb, srcTrie, srcData := makeTestTrie()
|
|
|
|
|
|
|
|
// Create a destination trie and sync with the scheduler
|
2018-09-24 12:57:49 +00:00
|
|
|
diskdb := memorydb.New()
|
2018-02-05 16:40:32 +00:00
|
|
|
triedb := NewDatabase(diskdb)
|
2019-05-13 12:28:01 +00:00
|
|
|
sched := NewSync(srcTrie.Hash(), diskdb, nil, NewSyncBloom(1, diskdb))
|
2015-10-05 16:37:56 +00:00
|
|
|
|
2020-08-28 07:50:37 +00:00
|
|
|
nodes, paths, codes := sched.Missing(count)
|
|
|
|
var (
|
|
|
|
hashQueue []common.Hash
|
|
|
|
pathQueue []SyncPath
|
|
|
|
)
|
|
|
|
if !bypath {
|
|
|
|
hashQueue = append(append(hashQueue[:0], nodes...), codes...)
|
|
|
|
} else {
|
|
|
|
hashQueue = append(hashQueue[:0], codes...)
|
|
|
|
pathQueue = append(pathQueue[:0], paths...)
|
|
|
|
}
|
|
|
|
for len(hashQueue)+len(pathQueue) > 0 {
|
|
|
|
results := make([]SyncResult, len(hashQueue)+len(pathQueue))
|
|
|
|
for i, hash := range hashQueue {
|
2018-02-05 16:40:32 +00:00
|
|
|
data, err := srcDb.Node(hash)
|
2015-10-05 16:37:56 +00:00
|
|
|
if err != nil {
|
2020-08-28 07:50:37 +00:00
|
|
|
t.Fatalf("failed to retrieve node data for hash %x: %v", hash, err)
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
|
|
|
results[i] = SyncResult{hash, data}
|
|
|
|
}
|
2020-08-28 07:50:37 +00:00
|
|
|
for i, path := range pathQueue {
|
|
|
|
data, _, err := srcTrie.TryGetNode(path[0])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve node data for path %x: %v", path, err)
|
|
|
|
}
|
|
|
|
results[len(hashQueue)+i] = SyncResult{crypto.Keccak256Hash(data), data}
|
|
|
|
}
|
2020-08-21 12:10:40 +00:00
|
|
|
for _, result := range results {
|
|
|
|
if err := sched.Process(result); err != nil {
|
|
|
|
t.Fatalf("failed to process result %v", err)
|
|
|
|
}
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
2019-10-28 17:50:11 +00:00
|
|
|
batch := diskdb.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
|
|
|
|
|
|
|
nodes, paths, codes = sched.Missing(count)
|
|
|
|
if !bypath {
|
|
|
|
hashQueue = append(append(hashQueue[:0], nodes...), codes...)
|
|
|
|
} else {
|
|
|
|
hashQueue = append(hashQueue[:0], codes...)
|
|
|
|
pathQueue = append(pathQueue[:0], paths...)
|
|
|
|
}
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
2015-12-28 13:20:37 +00:00
|
|
|
// Cross check that the two tries are in sync
|
2019-03-14 13:25:12 +00:00
|
|
|
checkTrieContents(t, triedb, srcTrie.Hash().Bytes(), srcData)
|
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.
|
2018-05-29 15:48:43 +00:00
|
|
|
func TestIterativeDelayedSync(t *testing.T) {
|
2015-10-05 16:37:56 +00:00
|
|
|
// Create a random trie to copy
|
|
|
|
srcDb, srcTrie, srcData := makeTestTrie()
|
|
|
|
|
|
|
|
// Create a destination trie and sync with the scheduler
|
2018-09-24 12:57:49 +00:00
|
|
|
diskdb := memorydb.New()
|
2018-02-05 16:40:32 +00:00
|
|
|
triedb := NewDatabase(diskdb)
|
2019-05-13 12:28:01 +00:00
|
|
|
sched := NewSync(srcTrie.Hash(), diskdb, nil, NewSyncBloom(1, diskdb))
|
2015-10-05 16:37:56 +00:00
|
|
|
|
2020-08-28 07:50:37 +00:00
|
|
|
nodes, _, codes := sched.Missing(10000)
|
|
|
|
queue := append(append([]common.Hash{}, nodes...), codes...)
|
|
|
|
|
2015-10-05 16:37:56 +00:00
|
|
|
for len(queue) > 0 {
|
|
|
|
// Sync only half of the scheduled nodes
|
|
|
|
results := make([]SyncResult, len(queue)/2+1)
|
|
|
|
for i, hash := range queue[:len(results)] {
|
2018-02-05 16:40:32 +00:00
|
|
|
data, err := srcDb.Node(hash)
|
2015-10-05 16:37:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve node data for %x: %v", hash, err)
|
|
|
|
}
|
|
|
|
results[i] = SyncResult{hash, data}
|
|
|
|
}
|
2020-08-21 12:10:40 +00:00
|
|
|
for _, result := range results {
|
|
|
|
if err := sched.Process(result); err != nil {
|
|
|
|
t.Fatalf("failed to process result %v", err)
|
|
|
|
}
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
2019-10-28 17:50:11 +00:00
|
|
|
batch := diskdb.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
|
|
|
|
|
|
|
nodes, _, codes = sched.Missing(10000)
|
|
|
|
queue = append(append(queue[len(results):], nodes...), codes...)
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
2015-12-28 13:20:37 +00:00
|
|
|
// Cross check that the two tries are in sync
|
2019-03-14 13:25:12 +00:00
|
|
|
checkTrieContents(t, triedb, srcTrie.Hash().Bytes(), srcData)
|
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.
|
2018-05-29 15:48:43 +00:00
|
|
|
func TestIterativeRandomSyncIndividual(t *testing.T) { testIterativeRandomSync(t, 1) }
|
|
|
|
func TestIterativeRandomSyncBatched(t *testing.T) { testIterativeRandomSync(t, 100) }
|
2015-10-05 16:37:56 +00:00
|
|
|
|
2019-10-28 17:50:11 +00:00
|
|
|
func testIterativeRandomSync(t *testing.T, count int) {
|
2015-10-05 16:37:56 +00:00
|
|
|
// Create a random trie to copy
|
|
|
|
srcDb, srcTrie, srcData := makeTestTrie()
|
|
|
|
|
|
|
|
// Create a destination trie and sync with the scheduler
|
2018-09-24 12:57:49 +00:00
|
|
|
diskdb := memorydb.New()
|
2018-02-05 16:40:32 +00:00
|
|
|
triedb := NewDatabase(diskdb)
|
2019-05-13 12:28:01 +00:00
|
|
|
sched := NewSync(srcTrie.Hash(), diskdb, nil, NewSyncBloom(1, diskdb))
|
2015-10-05 16:37:56 +00:00
|
|
|
|
|
|
|
queue := make(map[common.Hash]struct{})
|
2020-08-28 07:50:37 +00:00
|
|
|
nodes, _, codes := sched.Missing(count)
|
|
|
|
for _, hash := range append(nodes, codes...) {
|
2015-10-05 16:37:56 +00:00
|
|
|
queue[hash] = struct{}{}
|
|
|
|
}
|
|
|
|
for len(queue) > 0 {
|
|
|
|
// Fetch all the queued nodes in a random order
|
|
|
|
results := make([]SyncResult, 0, len(queue))
|
2017-01-06 14:52:03 +00:00
|
|
|
for hash := range queue {
|
2018-02-05 16:40:32 +00:00
|
|
|
data, err := srcDb.Node(hash)
|
2015-10-05 16:37:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve node data for %x: %v", hash, err)
|
|
|
|
}
|
|
|
|
results = append(results, SyncResult{hash, data})
|
|
|
|
}
|
|
|
|
// Feed the retrieved results back and queue new tasks
|
2020-08-21 12:10:40 +00:00
|
|
|
for _, result := range results {
|
|
|
|
if err := sched.Process(result); err != nil {
|
|
|
|
t.Fatalf("failed to process result %v", err)
|
|
|
|
}
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
2019-10-28 17:50:11 +00:00
|
|
|
batch := diskdb.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
|
|
|
|
2015-10-05 16:37:56 +00:00
|
|
|
queue = make(map[common.Hash]struct{})
|
2020-08-28 07:50:37 +00:00
|
|
|
nodes, _, codes = sched.Missing(count)
|
|
|
|
for _, hash := range append(nodes, codes...) {
|
2015-10-05 16:37:56 +00:00
|
|
|
queue[hash] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
2015-12-28 13:20:37 +00:00
|
|
|
// Cross check that the two tries are in sync
|
2019-03-14 13:25:12 +00:00
|
|
|
checkTrieContents(t, triedb, srcTrie.Hash().Bytes(), srcData)
|
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.
|
2018-05-29 15:48:43 +00:00
|
|
|
func TestIterativeRandomDelayedSync(t *testing.T) {
|
2015-10-05 16:37:56 +00:00
|
|
|
// Create a random trie to copy
|
|
|
|
srcDb, srcTrie, srcData := makeTestTrie()
|
|
|
|
|
|
|
|
// Create a destination trie and sync with the scheduler
|
2018-09-24 12:57:49 +00:00
|
|
|
diskdb := memorydb.New()
|
2018-02-05 16:40:32 +00:00
|
|
|
triedb := NewDatabase(diskdb)
|
2019-05-13 12:28:01 +00:00
|
|
|
sched := NewSync(srcTrie.Hash(), diskdb, nil, NewSyncBloom(1, diskdb))
|
2015-10-05 16:37:56 +00:00
|
|
|
|
|
|
|
queue := make(map[common.Hash]struct{})
|
2020-08-28 07:50:37 +00:00
|
|
|
nodes, _, codes := sched.Missing(10000)
|
|
|
|
for _, hash := range append(nodes, codes...) {
|
2015-10-05 16:37:56 +00:00
|
|
|
queue[hash] = struct{}{}
|
|
|
|
}
|
|
|
|
for len(queue) > 0 {
|
|
|
|
// Sync only half of the scheduled nodes, even those in random order
|
|
|
|
results := make([]SyncResult, 0, len(queue)/2+1)
|
2017-01-06 14:52:03 +00:00
|
|
|
for hash := range queue {
|
2018-02-05 16:40:32 +00:00
|
|
|
data, err := srcDb.Node(hash)
|
2015-10-05 16:37:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve node data for %x: %v", hash, err)
|
|
|
|
}
|
|
|
|
results = append(results, SyncResult{hash, data})
|
|
|
|
|
|
|
|
if len(results) >= cap(results) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Feed the retrieved results back and queue new tasks
|
2020-08-21 12:10:40 +00:00
|
|
|
for _, result := range results {
|
|
|
|
if err := sched.Process(result); err != nil {
|
|
|
|
t.Fatalf("failed to process result %v", err)
|
|
|
|
}
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
2019-10-28 17:50:11 +00:00
|
|
|
batch := diskdb.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()
|
2015-10-05 16:37:56 +00:00
|
|
|
for _, result := range results {
|
|
|
|
delete(queue, result.Hash)
|
|
|
|
}
|
2020-08-28 07:50:37 +00:00
|
|
|
nodes, _, codes = sched.Missing(10000)
|
|
|
|
for _, hash := range append(nodes, codes...) {
|
2015-10-05 16:37:56 +00:00
|
|
|
queue[hash] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
2015-12-28 13:20:37 +00:00
|
|
|
// Cross check that the two tries are in sync
|
2019-03-14 13:25:12 +00:00
|
|
|
checkTrieContents(t, triedb, srcTrie.Hash().Bytes(), srcData)
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that a trie sync will not request nodes multiple times, even if they
|
|
|
|
// have such references.
|
2018-05-29 15:48:43 +00:00
|
|
|
func TestDuplicateAvoidanceSync(t *testing.T) {
|
2015-10-05 16:37:56 +00:00
|
|
|
// Create a random trie to copy
|
|
|
|
srcDb, srcTrie, srcData := makeTestTrie()
|
|
|
|
|
|
|
|
// Create a destination trie and sync with the scheduler
|
2018-09-24 12:57:49 +00:00
|
|
|
diskdb := memorydb.New()
|
2018-02-05 16:40:32 +00:00
|
|
|
triedb := NewDatabase(diskdb)
|
2019-05-13 12:28:01 +00:00
|
|
|
sched := NewSync(srcTrie.Hash(), diskdb, nil, NewSyncBloom(1, diskdb))
|
2015-10-05 16:37:56 +00:00
|
|
|
|
2020-08-28 07:50:37 +00:00
|
|
|
nodes, _, codes := sched.Missing(0)
|
|
|
|
queue := append(append([]common.Hash{}, nodes...), codes...)
|
2015-10-05 16:37:56 +00:00
|
|
|
requested := make(map[common.Hash]struct{})
|
|
|
|
|
|
|
|
for len(queue) > 0 {
|
|
|
|
results := make([]SyncResult, len(queue))
|
|
|
|
for i, hash := range queue {
|
2018-02-05 16:40:32 +00:00
|
|
|
data, err := srcDb.Node(hash)
|
2015-10-05 16:37:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve node data for %x: %v", hash, err)
|
|
|
|
}
|
|
|
|
if _, ok := requested[hash]; ok {
|
|
|
|
t.Errorf("hash %x already requested once", hash)
|
|
|
|
}
|
|
|
|
requested[hash] = struct{}{}
|
|
|
|
|
|
|
|
results[i] = SyncResult{hash, data}
|
|
|
|
}
|
2020-08-21 12:10:40 +00:00
|
|
|
for _, result := range results {
|
|
|
|
if err := sched.Process(result); err != nil {
|
|
|
|
t.Fatalf("failed to process result %v", err)
|
|
|
|
}
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
2019-10-28 17:50:11 +00:00
|
|
|
batch := diskdb.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
|
|
|
|
|
|
|
nodes, _, codes = sched.Missing(0)
|
|
|
|
queue = append(append(queue[:0], nodes...), codes...)
|
2015-10-05 16:37:56 +00:00
|
|
|
}
|
2015-12-28 13:20:37 +00:00
|
|
|
// Cross check that the two tries are in sync
|
2019-03-14 13:25:12 +00:00
|
|
|
checkTrieContents(t, triedb, srcTrie.Hash().Bytes(), srcData)
|
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.
|
2018-05-29 15:48:43 +00:00
|
|
|
func TestIncompleteSync(t *testing.T) {
|
2015-12-28 13:20:37 +00:00
|
|
|
// Create a random trie to copy
|
|
|
|
srcDb, srcTrie, _ := makeTestTrie()
|
|
|
|
|
|
|
|
// Create a destination trie and sync with the scheduler
|
2018-09-24 12:57:49 +00:00
|
|
|
diskdb := memorydb.New()
|
2018-02-05 16:40:32 +00:00
|
|
|
triedb := NewDatabase(diskdb)
|
2019-05-13 12:28:01 +00:00
|
|
|
sched := NewSync(srcTrie.Hash(), diskdb, nil, NewSyncBloom(1, diskdb))
|
2015-12-28 13:20:37 +00:00
|
|
|
|
2019-02-19 13:50:11 +00:00
|
|
|
var added []common.Hash
|
2020-08-28 07:50:37 +00:00
|
|
|
|
|
|
|
nodes, _, codes := sched.Missing(1)
|
|
|
|
queue := append(append([]common.Hash{}, nodes...), codes...)
|
2015-12-28 13:20:37 +00:00
|
|
|
for len(queue) > 0 {
|
|
|
|
// Fetch a batch of trie nodes
|
|
|
|
results := make([]SyncResult, len(queue))
|
|
|
|
for i, hash := range queue {
|
2018-02-05 16:40:32 +00:00
|
|
|
data, err := srcDb.Node(hash)
|
2015-12-28 13:20:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve node data for %x: %v", hash, err)
|
|
|
|
}
|
|
|
|
results[i] = SyncResult{hash, data}
|
|
|
|
}
|
|
|
|
// Process each of the trie nodes
|
2020-08-21 12:10:40 +00:00
|
|
|
for _, result := range results {
|
|
|
|
if err := sched.Process(result); err != nil {
|
|
|
|
t.Fatalf("failed to process result %v", err)
|
|
|
|
}
|
2015-12-28 13:20:37 +00:00
|
|
|
}
|
2019-10-28 17:50:11 +00:00
|
|
|
batch := diskdb.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()
|
2015-12-28 13:20:37 +00:00
|
|
|
for _, result := range results {
|
|
|
|
added = append(added, result.Hash)
|
2020-12-10 13:48:32 +00:00
|
|
|
// Check that all known sub-tries in the synced trie are complete
|
|
|
|
if err := checkTrieConsistency(triedb, result.Hash); err != nil {
|
2015-12-28 13:20:37 +00:00
|
|
|
t.Fatalf("trie inconsistent: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Fetch the next batch to retrieve
|
2020-08-28 07:50:37 +00:00
|
|
|
nodes, _, codes = sched.Missing(1)
|
|
|
|
queue = append(append(queue[:0], nodes...), codes...)
|
2015-12-28 13:20:37 +00:00
|
|
|
}
|
|
|
|
// Sanity check that removing any node from the database is detected
|
|
|
|
for _, node := range added[1:] {
|
|
|
|
key := node.Bytes()
|
2018-02-05 16:40:32 +00:00
|
|
|
value, _ := diskdb.Get(key)
|
2015-12-28 13:20:37 +00:00
|
|
|
|
2018-02-05 16:40:32 +00:00
|
|
|
diskdb.Delete(key)
|
|
|
|
if err := checkTrieConsistency(triedb, added[0]); err == nil {
|
2015-12-28 13:20:37 +00:00
|
|
|
t.Fatalf("trie inconsistency not caught, missing: %x", key)
|
|
|
|
}
|
2018-02-05 16:40:32 +00:00
|
|
|
diskdb.Put(key, value)
|
2015-12-28 13:20:37 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-28 07:50:37 +00:00
|
|
|
|
|
|
|
// Tests that trie nodes get scheduled lexicographically when having the same
|
|
|
|
// depth.
|
|
|
|
func TestSyncOrdering(t *testing.T) {
|
|
|
|
// Create a random trie to copy
|
|
|
|
srcDb, srcTrie, srcData := makeTestTrie()
|
|
|
|
|
|
|
|
// Create a destination trie and sync with the scheduler, tracking the requests
|
|
|
|
diskdb := memorydb.New()
|
|
|
|
triedb := NewDatabase(diskdb)
|
|
|
|
sched := NewSync(srcTrie.Hash(), diskdb, nil, NewSyncBloom(1, diskdb))
|
|
|
|
|
|
|
|
nodes, paths, _ := sched.Missing(1)
|
|
|
|
queue := append([]common.Hash{}, nodes...)
|
|
|
|
reqs := append([]SyncPath{}, paths...)
|
|
|
|
|
|
|
|
for len(queue) > 0 {
|
|
|
|
results := make([]SyncResult, len(queue))
|
|
|
|
for i, hash := range queue {
|
|
|
|
data, err := srcDb.Node(hash)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve node data for %x: %v", hash, err)
|
|
|
|
}
|
|
|
|
results[i] = SyncResult{hash, data}
|
|
|
|
}
|
|
|
|
for _, result := range results {
|
|
|
|
if err := sched.Process(result); err != nil {
|
|
|
|
t.Fatalf("failed to process result %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
batch := diskdb.NewBatch()
|
|
|
|
if err := sched.Commit(batch); err != nil {
|
|
|
|
t.Fatalf("failed to commit data: %v", err)
|
|
|
|
}
|
|
|
|
batch.Write()
|
|
|
|
|
|
|
|
nodes, paths, _ = sched.Missing(1)
|
|
|
|
queue = append(queue[:0], nodes...)
|
|
|
|
reqs = append(reqs, paths...)
|
|
|
|
}
|
|
|
|
// Cross check that the two tries are in sync
|
|
|
|
checkTrieContents(t, triedb, srcTrie.Hash().Bytes(), srcData)
|
|
|
|
|
|
|
|
// Check that the trie nodes have been requested path-ordered
|
|
|
|
for i := 0; i < len(reqs)-1; i++ {
|
|
|
|
if len(reqs[i]) > 1 || len(reqs[i+1]) > 1 {
|
|
|
|
// In the case of the trie tests, there's no storage so the tuples
|
|
|
|
// must always be single items. 2-tuples should be tested in state.
|
|
|
|
t.Errorf("Invalid request tuples: len(%v) or len(%v) > 1", reqs[i], reqs[i+1])
|
|
|
|
}
|
|
|
|
if bytes.Compare(compactToHex(reqs[i][0]), compactToHex(reqs[i+1][0])) > 0 {
|
|
|
|
t.Errorf("Invalid request order: %v before %v", compactToHex(reqs[i][0]), compactToHex(reqs[i+1][0]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|