sync_test.go 15.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// 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"
	"math/big"
	"testing"

	"github.com/ethereum/go-ethereum/common"
25
	"github.com/ethereum/go-ethereum/core/rawdb"
26
	"github.com/ethereum/go-ethereum/crypto"
27
	"github.com/ethereum/go-ethereum/ethdb"
28
	"github.com/ethereum/go-ethereum/ethdb/memorydb"
29
	"github.com/ethereum/go-ethereum/rlp"
30 31 32 33 34 35 36 37 38 39 40 41
	"github.com/ethereum/go-ethereum/trie"
)

// testAccount is the data associated with an account used by the state tests.
type testAccount struct {
	address common.Address
	balance *big.Int
	nonce   uint64
	code    []byte
}

// makeTestState create a sample test state to test node-wise reconstruction.
42
func makeTestState() (Database, common.Hash, []*testAccount) {
43
	// Create an empty state
44
	db := NewDatabase(rawdb.NewMemoryDatabase())
45
	state, _ := New(common.Hash{}, db, nil)
46 47

	// Fill it with some arbitrary data
48
	var accounts []*testAccount
49
	for i := byte(0); i < 96; i++ {
50 51 52 53 54 55 56 57 58 59
		obj := state.GetOrNewStateObject(common.BytesToAddress([]byte{i}))
		acc := &testAccount{address: common.BytesToAddress([]byte{i})}

		obj.AddBalance(big.NewInt(int64(11 * i)))
		acc.balance = big.NewInt(int64(11 * i))

		obj.SetNonce(uint64(42 * i))
		acc.nonce = uint64(42 * i)

		if i%3 == 0 {
60
			obj.SetCode(crypto.Keccak256Hash([]byte{i, i, i, i, i}), []byte{i, i, i, i, i})
61 62
			acc.code = []byte{i, i, i, i, i}
		}
63 64
		if i%5 == 0 {
			for j := byte(0); j < 5; j++ {
65 66
				hash := crypto.Keccak256Hash([]byte{i, i, i, i, i, j, j})
				obj.SetState(db, hash, hash)
67 68
			}
		}
69
		state.updateStateObject(obj)
70 71
		accounts = append(accounts, acc)
	}
72
	root, _ := state.Commit(false)
73 74

	// Return the generated state
75
	return db, root, accounts
76 77 78 79 80
}

// checkStateAccounts cross references a reconstructed state with an expected
// account array.
func checkStateAccounts(t *testing.T, db ethdb.Database, root common.Hash, accounts []*testAccount) {
81
	// Check root availability and state contents
82
	state, err := New(root, NewDatabase(db), nil)
83 84 85 86 87 88 89
	if err != nil {
		t.Fatalf("failed to create state trie at %x: %v", root, err)
	}
	if err := checkStateConsistency(db, root); err != nil {
		t.Fatalf("inconsistent state trie at %x: %v", root, err)
	}
	for i, acc := range accounts {
90 91 92 93 94 95
		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)
		}
96
		if code := state.GetCode(acc.address); !bytes.Equal(code, acc.code) {
97 98 99 100 101
			t.Errorf("account %d: code mismatch: have %x, want %x", i, code, acc.code)
		}
	}
}

102 103 104 105 106
// checkTrieConsistency checks that all nodes in a (sub-)trie are indeed present.
func checkTrieConsistency(db ethdb.Database, root common.Hash) error {
	if v, _ := db.Get(root[:]); v == nil {
		return nil // Consider a non existent state consistent.
	}
107
	trie, err := trie.New(root, trie.NewDatabase(db))
108 109 110 111 112 113 114 115 116 117
	if err != nil {
		return err
	}
	it := trie.NodeIterator(nil)
	for it.Next(true) {
	}
	return it.Error()
}

// checkStateConsistency checks that all data of a state root is present.
118
func checkStateConsistency(db ethdb.Database, root common.Hash) error {
119 120
	// Create and iterate a state trie rooted in a sub-node
	if _, err := db.Get(root.Bytes()); err != nil {
121
		return nil // Consider a non existent state consistent.
122
	}
123
	state, err := New(root, NewDatabase(db), nil)
124
	if err != nil {
125
		return err
126
	}
127 128
	it := NewNodeIterator(state)
	for it.Next() {
129
	}
130
	return it.Error
131 132
}

133 134 135
// Tests that an empty state is not scheduled for syncing.
func TestEmptyStateSync(t *testing.T) {
	empty := common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
136 137 138
	sync := NewStateSync(empty, rawdb.NewMemoryDatabase(), trie.NewSyncBloom(1, memorydb.New()))
	if nodes, paths, codes := sync.Missing(1); len(nodes) != 0 || len(paths) != 0 || len(codes) != 0 {
		t.Errorf(" content requested for empty state: %v, %v, %v", nodes, paths, codes)
139 140 141 142 143
	}
}

// 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.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
func TestIterativeStateSyncIndividual(t *testing.T) {
	testIterativeStateSync(t, 1, false, false)
}
func TestIterativeStateSyncBatched(t *testing.T) {
	testIterativeStateSync(t, 100, false, false)
}
func TestIterativeStateSyncIndividualFromDisk(t *testing.T) {
	testIterativeStateSync(t, 1, true, false)
}
func TestIterativeStateSyncBatchedFromDisk(t *testing.T) {
	testIterativeStateSync(t, 100, true, false)
}
func TestIterativeStateSyncIndividualByPath(t *testing.T) {
	testIterativeStateSync(t, 1, false, true)
}
func TestIterativeStateSyncBatchedByPath(t *testing.T) {
	testIterativeStateSync(t, 100, false, true)
}
162

163
func testIterativeStateSync(t *testing.T, count int, commit bool, bypath bool) {
164
	// Create a random state to copy
165
	srcDb, srcRoot, srcAccounts := makeTestState()
166 167 168
	if commit {
		srcDb.TrieDB().Commit(srcRoot, false, nil)
	}
169 170
	srcTrie, _ := trie.New(srcRoot, srcDb.TrieDB())

171
	// Create a destination state and sync with the scheduler
172
	dstDb := rawdb.NewMemoryDatabase()
173
	sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb))
174

175 176 177 178 179 180 181 182 183 184 185 186 187 188
	nodes, paths, codes := sched.Missing(count)
	var (
		hashQueue []common.Hash
		pathQueue []trie.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([]trie.SyncResult, len(hashQueue)+len(pathQueue))
		for i, hash := range hashQueue {
189
			data, err := srcDb.TrieDB().Node(hash)
190 191 192
			if err != nil {
				data, err = srcDb.ContractCode(common.Hash{}, hash)
			}
193
			if err != nil {
194
				t.Fatalf("failed to retrieve node data for hash %x", hash)
195
			}
Felix Lange's avatar
Felix Lange committed
196
			results[i] = trie.SyncResult{Hash: hash, Data: data}
197
		}
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
		for i, path := range pathQueue {
			if len(path) == 1 {
				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] = trie.SyncResult{Hash: crypto.Keccak256Hash(data), Data: data}
			} else {
				var acc Account
				if err := rlp.DecodeBytes(srcTrie.Get(path[0]), &acc); err != nil {
					t.Fatalf("failed to decode account on path %x: %v", path, err)
				}
				stTrie, err := trie.New(acc.Root, srcDb.TrieDB())
				if err != nil {
					t.Fatalf("failed to retriev storage trie for path %x: %v", path, err)
				}
				data, _, err := stTrie.TryGetNode(path[1])
				if err != nil {
					t.Fatalf("failed to retrieve node data for path %x: %v", path, err)
				}
				results[len(hashQueue)+i] = trie.SyncResult{Hash: crypto.Keccak256Hash(data), Data: data}
			}
		}
221 222
		for _, result := range results {
			if err := sched.Process(result); err != nil {
223
				t.Errorf("failed to process result %v", err)
224
			}
225
		}
226 227 228
		batch := dstDb.NewBatch()
		if err := sched.Commit(batch); err != nil {
			t.Fatalf("failed to commit data: %v", err)
229
		}
230
		batch.Write()
231 232 233 234 235 236 237 238

		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...)
		}
239 240 241 242 243 244 245 246 247
	}
	// Cross check that the two states are in sync
	checkStateAccounts(t, dstDb, srcRoot, srcAccounts)
}

// 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) {
	// Create a random state to copy
248
	srcDb, srcRoot, srcAccounts := makeTestState()
249 250

	// Create a destination state and sync with the scheduler
251
	dstDb := rawdb.NewMemoryDatabase()
252
	sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb))
253

254 255 256
	nodes, _, codes := sched.Missing(0)
	queue := append(append([]common.Hash{}, nodes...), codes...)

257 258 259 260
	for len(queue) > 0 {
		// Sync only half of the scheduled nodes
		results := make([]trie.SyncResult, len(queue)/2+1)
		for i, hash := range queue[:len(results)] {
261
			data, err := srcDb.TrieDB().Node(hash)
262 263 264
			if err != nil {
				data, err = srcDb.ContractCode(common.Hash{}, hash)
			}
265
			if err != nil {
266
				t.Fatalf("failed to retrieve node data for %x", hash)
267
			}
Felix Lange's avatar
Felix Lange committed
268
			results[i] = trie.SyncResult{Hash: hash, Data: data}
269
		}
270 271 272 273
		for _, result := range results {
			if err := sched.Process(result); err != nil {
				t.Fatalf("failed to process result %v", err)
			}
274
		}
275 276 277
		batch := dstDb.NewBatch()
		if err := sched.Commit(batch); err != nil {
			t.Fatalf("failed to commit data: %v", err)
278
		}
279
		batch.Write()
280 281 282

		nodes, _, codes = sched.Missing(0)
		queue = append(append(queue[len(results):], nodes...), codes...)
283 284 285 286 287 288 289 290 291 292 293
	}
	// Cross check that the two states are in sync
	checkStateAccounts(t, dstDb, srcRoot, srcAccounts)
}

// 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.
func TestIterativeRandomStateSyncIndividual(t *testing.T) { testIterativeRandomStateSync(t, 1) }
func TestIterativeRandomStateSyncBatched(t *testing.T)    { testIterativeRandomStateSync(t, 100) }

294
func testIterativeRandomStateSync(t *testing.T, count int) {
295
	// Create a random state to copy
296
	srcDb, srcRoot, srcAccounts := makeTestState()
297 298

	// Create a destination state and sync with the scheduler
299
	dstDb := rawdb.NewMemoryDatabase()
300
	sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb))
301 302

	queue := make(map[common.Hash]struct{})
303 304
	nodes, _, codes := sched.Missing(count)
	for _, hash := range append(nodes, codes...) {
305 306 307 308 309
		queue[hash] = struct{}{}
	}
	for len(queue) > 0 {
		// Fetch all the queued nodes in a random order
		results := make([]trie.SyncResult, 0, len(queue))
Felix Lange's avatar
Felix Lange committed
310
		for hash := range queue {
311
			data, err := srcDb.TrieDB().Node(hash)
312 313 314
			if err != nil {
				data, err = srcDb.ContractCode(common.Hash{}, hash)
			}
315
			if err != nil {
316
				t.Fatalf("failed to retrieve node data for %x", hash)
317
			}
Felix Lange's avatar
Felix Lange committed
318
			results = append(results, trie.SyncResult{Hash: hash, Data: data})
319 320
		}
		// Feed the retrieved results back and queue new tasks
321 322 323 324
		for _, result := range results {
			if err := sched.Process(result); err != nil {
				t.Fatalf("failed to process result %v", err)
			}
325
		}
326 327 328
		batch := dstDb.NewBatch()
		if err := sched.Commit(batch); err != nil {
			t.Fatalf("failed to commit data: %v", err)
329
		}
330
		batch.Write()
331

332
		queue = make(map[common.Hash]struct{})
333 334
		nodes, _, codes = sched.Missing(count)
		for _, hash := range append(nodes, codes...) {
335 336 337 338 339 340 341 342 343 344 345
			queue[hash] = struct{}{}
		}
	}
	// Cross check that the two states are in sync
	checkStateAccounts(t, dstDb, srcRoot, srcAccounts)
}

// 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) {
	// Create a random state to copy
346
	srcDb, srcRoot, srcAccounts := makeTestState()
347 348

	// Create a destination state and sync with the scheduler
349
	dstDb := rawdb.NewMemoryDatabase()
350
	sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb))
351 352

	queue := make(map[common.Hash]struct{})
353 354
	nodes, _, codes := sched.Missing(0)
	for _, hash := range append(nodes, codes...) {
355 356 357 358 359
		queue[hash] = struct{}{}
	}
	for len(queue) > 0 {
		// Sync only half of the scheduled nodes, even those in random order
		results := make([]trie.SyncResult, 0, len(queue)/2+1)
Felix Lange's avatar
Felix Lange committed
360
		for hash := range queue {
361 362
			delete(queue, hash)

363
			data, err := srcDb.TrieDB().Node(hash)
364 365 366
			if err != nil {
				data, err = srcDb.ContractCode(common.Hash{}, hash)
			}
367
			if err != nil {
368
				t.Fatalf("failed to retrieve node data for %x", hash)
369
			}
Felix Lange's avatar
Felix Lange committed
370
			results = append(results, trie.SyncResult{Hash: hash, Data: data})
371 372 373 374 375 376

			if len(results) >= cap(results) {
				break
			}
		}
		// Feed the retrieved results back and queue new tasks
377 378 379 380
		for _, result := range results {
			if err := sched.Process(result); err != nil {
				t.Fatalf("failed to process result %v", err)
			}
381
		}
382 383 384
		batch := dstDb.NewBatch()
		if err := sched.Commit(batch); err != nil {
			t.Fatalf("failed to commit data: %v", err)
385
		}
386
		batch.Write()
387 388 389 390 391
		for _, result := range results {
			delete(queue, result.Hash)
		}
		nodes, _, codes = sched.Missing(0)
		for _, hash := range append(nodes, codes...) {
392 393 394 395 396 397
			queue[hash] = struct{}{}
		}
	}
	// Cross check that the two states are in sync
	checkStateAccounts(t, dstDb, srcRoot, srcAccounts)
}
398 399 400 401 402

// Tests that at any point in time during a sync, only complete sub-tries are in
// the database.
func TestIncompleteStateSync(t *testing.T) {
	// Create a random state to copy
403
	srcDb, srcRoot, srcAccounts := makeTestState()
404

405 406 407 408 409
	// 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{}{}
410 411
		}
	}
412
	isCode[common.BytesToHash(emptyCodeHash)] = struct{}{}
413
	checkTrieConsistency(srcDb.TrieDB().DiskDB().(ethdb.Database), srcRoot)
414 415

	// Create a destination state and sync with the scheduler
416
	dstDb := rawdb.NewMemoryDatabase()
417
	sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb))
418

419 420 421 422 423
	var added []common.Hash

	nodes, _, codes := sched.Missing(1)
	queue := append(append([]common.Hash{}, nodes...), codes...)

424 425 426 427
	for len(queue) > 0 {
		// Fetch a batch of state nodes
		results := make([]trie.SyncResult, len(queue))
		for i, hash := range queue {
428
			data, err := srcDb.TrieDB().Node(hash)
429 430 431
			if err != nil {
				data, err = srcDb.ContractCode(common.Hash{}, hash)
			}
432
			if err != nil {
433
				t.Fatalf("failed to retrieve node data for %x", hash)
434
			}
Felix Lange's avatar
Felix Lange committed
435
			results[i] = trie.SyncResult{Hash: hash, Data: data}
436 437
		}
		// Process each of the state nodes
438 439 440 441
		for _, result := range results {
			if err := sched.Process(result); err != nil {
				t.Fatalf("failed to process result %v", err)
			}
442
		}
443 444 445
		batch := dstDb.NewBatch()
		if err := sched.Commit(batch); err != nil {
			t.Fatalf("failed to commit data: %v", err)
446
		}
447
		batch.Write()
448 449
		for _, result := range results {
			added = append(added, result.Hash)
450 451
			// Check that all known sub-tries added so far are complete or missing entirely.
			if _, ok := isCode[result.Hash]; ok {
452
				continue
453
			}
454 455
			// Can't use checkStateConsistency here because subtrie keys may have odd
			// length and crash in LeafKey.
456
			if err := checkTrieConsistency(dstDb, result.Hash); err != nil {
457
				t.Fatalf("state inconsistent: %v", err)
458 459 460
			}
		}
		// Fetch the next batch to retrieve
461 462
		nodes, _, codes = sched.Missing(1)
		queue = append(append(queue[:0], nodes...), codes...)
463 464 465
	}
	// Sanity check that removing any node from the database is detected
	for _, node := range added[1:] {
466
		var (
467 468 469
			key     = node.Bytes()
			_, code = isCode[node]
			val     []byte
470 471 472 473 474 475 476 477
		)
		if code {
			val = rawdb.ReadCode(dstDb, node)
			rawdb.DeleteCode(dstDb, node)
		} else {
			val = rawdb.ReadTrieNode(dstDb, node)
			rawdb.DeleteTrieNode(dstDb, node)
		}
478 479 480
		if err := checkStateConsistency(dstDb, added[0]); err == nil {
			t.Fatalf("trie inconsistency not caught, missing: %x", key)
		}
481 482 483 484 485
		if code {
			rawdb.WriteCode(dstDb, node, val)
		} else {
			rawdb.WriteTrieNode(dstDb, node, val)
		}
486 487
	}
}