github.com/snowblossomcoin/go-ethereum@v1.9.25/core/state/sync_test.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package state
    18  
    19  import (
    20  	"bytes"
    21  	"math/big"
    22  	"testing"
    23  
    24  	"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  	"github.com/ethereum/go-ethereum/trie"
    31  )
    32  
    33  // testAccount is the data associated with an account used by the state tests.
    34  type testAccount struct {
    35  	address common.Address
    36  	balance *big.Int
    37  	nonce   uint64
    38  	code    []byte
    39  }
    40  
    41  // 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  		obj := state.GetOrNewStateObject(common.BytesToAddress([]byte{i}))
    51  		acc := &testAccount{address: common.BytesToAddress([]byte{i})}
    52  
    53  		obj.AddBalance(big.NewInt(int64(11 * i)))
    54  		acc.balance = big.NewInt(int64(11 * i))
    55  
    56  		obj.SetNonce(uint64(42 * i))
    57  		acc.nonce = uint64(42 * i)
    58  
    59  		if i%3 == 0 {
    60  			obj.SetCode(crypto.Keccak256Hash([]byte{i, i, i, i, i}), []byte{i, i, i, i, i})
    61  			acc.code = []byte{i, i, i, i, i}
    62  		}
    63  		if i%5 == 0 {
    64  			for j := byte(0); j < 5; j++ {
    65  				obj.SetState(db, crypto.Keccak256Hash([]byte{i, i, i, i, i, j, j}), crypto.Keccak256Hash([]byte{i, i, i, i, i, j, j}))
    66  			}
    67  		}
    68  		state.updateStateObject(obj)
    69  		accounts = append(accounts, acc)
    70  	}
    71  	root, _ := state.Commit(false)
    72  
    73  	// Return the generated state
    74  	return db, root, accounts
    75  }
    76  
    77  // checkStateAccounts cross references a reconstructed state with an expected
    78  // account array.
    79  func checkStateAccounts(t *testing.T, db ethdb.Database, root common.Hash, accounts []*testAccount) {
    80  	// Check root availability and state contents
    81  	state, err := New(root, NewDatabase(db), nil)
    82  	if err != nil {
    83  		t.Fatalf("failed to create state trie at %x: %v", root, err)
    84  	}
    85  	if err := checkStateConsistency(db, root); err != nil {
    86  		t.Fatalf("inconsistent state trie at %x: %v", root, err)
    87  	}
    88  	for i, acc := range accounts {
    89  		if balance := state.GetBalance(acc.address); balance.Cmp(acc.balance) != 0 {
    90  			t.Errorf("account %d: balance mismatch: have %v, want %v", i, balance, acc.balance)
    91  		}
    92  		if nonce := state.GetNonce(acc.address); nonce != acc.nonce {
    93  			t.Errorf("account %d: nonce mismatch: have %v, want %v", i, nonce, acc.nonce)
    94  		}
    95  		if code := state.GetCode(acc.address); !bytes.Equal(code, acc.code) {
    96  			t.Errorf("account %d: code mismatch: have %x, want %x", i, code, acc.code)
    97  		}
    98  	}
    99  }
   100  
   101  // checkTrieConsistency checks that all nodes in a (sub-)trie are indeed present.
   102  func checkTrieConsistency(db ethdb.Database, root common.Hash) error {
   103  	if v, _ := db.Get(root[:]); v == nil {
   104  		return nil // Consider a non existent state consistent.
   105  	}
   106  	trie, err := trie.New(root, trie.NewDatabase(db))
   107  	if err != nil {
   108  		return err
   109  	}
   110  	it := trie.NodeIterator(nil)
   111  	for it.Next(true) {
   112  	}
   113  	return it.Error()
   114  }
   115  
   116  // checkStateConsistency checks that all data of a state root is present.
   117  func checkStateConsistency(db ethdb.Database, root common.Hash) error {
   118  	// Create and iterate a state trie rooted in a sub-node
   119  	if _, err := db.Get(root.Bytes()); err != nil {
   120  		return nil // Consider a non existent state consistent.
   121  	}
   122  	state, err := New(root, NewDatabase(db), nil)
   123  	if err != nil {
   124  		return err
   125  	}
   126  	it := NewNodeIterator(state)
   127  	for it.Next() {
   128  	}
   129  	return it.Error
   130  }
   131  
   132  // Tests that an empty state is not scheduled for syncing.
   133  func TestEmptyStateSync(t *testing.T) {
   134  	empty := common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
   135  	sync := NewStateSync(empty, rawdb.NewMemoryDatabase(), trie.NewSyncBloom(1, memorydb.New()))
   136  	if nodes, paths, codes := sync.Missing(1); len(nodes) != 0 || len(paths) != 0 || len(codes) != 0 {
   137  		t.Errorf(" content requested for empty state: %v, %v, %v", nodes, paths, codes)
   138  	}
   139  }
   140  
   141  // Tests that given a root hash, a state can sync iteratively on a single thread,
   142  // requesting retrieval tasks and returning all of them in one go.
   143  func TestIterativeStateSyncIndividual(t *testing.T) {
   144  	testIterativeStateSync(t, 1, false, false)
   145  }
   146  func TestIterativeStateSyncBatched(t *testing.T) {
   147  	testIterativeStateSync(t, 100, false, false)
   148  }
   149  func TestIterativeStateSyncIndividualFromDisk(t *testing.T) {
   150  	testIterativeStateSync(t, 1, true, false)
   151  }
   152  func TestIterativeStateSyncBatchedFromDisk(t *testing.T) {
   153  	testIterativeStateSync(t, 100, true, false)
   154  }
   155  func TestIterativeStateSyncIndividualByPath(t *testing.T) {
   156  	testIterativeStateSync(t, 1, false, true)
   157  }
   158  func TestIterativeStateSyncBatchedByPath(t *testing.T) {
   159  	testIterativeStateSync(t, 100, false, true)
   160  }
   161  
   162  func testIterativeStateSync(t *testing.T, count int, commit bool, bypath bool) {
   163  	// Create a random state to copy
   164  	srcDb, srcRoot, srcAccounts := makeTestState()
   165  	if commit {
   166  		srcDb.TrieDB().Commit(srcRoot, false, nil)
   167  	}
   168  	srcTrie, _ := trie.New(srcRoot, srcDb.TrieDB())
   169  
   170  	// Create a destination state and sync with the scheduler
   171  	dstDb := rawdb.NewMemoryDatabase()
   172  	sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb))
   173  
   174  	nodes, paths, codes := sched.Missing(count)
   175  	var (
   176  		hashQueue []common.Hash
   177  		pathQueue []trie.SyncPath
   178  	)
   179  	if !bypath {
   180  		hashQueue = append(append(hashQueue[:0], nodes...), codes...)
   181  	} else {
   182  		hashQueue = append(hashQueue[:0], codes...)
   183  		pathQueue = append(pathQueue[:0], paths...)
   184  	}
   185  	for len(hashQueue)+len(pathQueue) > 0 {
   186  		results := make([]trie.SyncResult, len(hashQueue)+len(pathQueue))
   187  		for i, hash := range hashQueue {
   188  			data, err := srcDb.TrieDB().Node(hash)
   189  			if err != nil {
   190  				data, err = srcDb.ContractCode(common.Hash{}, hash)
   191  			}
   192  			if err != nil {
   193  				t.Fatalf("failed to retrieve node data for hash %x", hash)
   194  			}
   195  			results[i] = trie.SyncResult{Hash: hash, Data: data}
   196  		}
   197  		for i, path := range pathQueue {
   198  			if len(path) == 1 {
   199  				data, _, err := srcTrie.TryGetNode(path[0])
   200  				if err != nil {
   201  					t.Fatalf("failed to retrieve node data for path %x: %v", path, err)
   202  				}
   203  				results[len(hashQueue)+i] = trie.SyncResult{Hash: crypto.Keccak256Hash(data), Data: data}
   204  			} else {
   205  				var acc Account
   206  				if err := rlp.DecodeBytes(srcTrie.Get(path[0]), &acc); err != nil {
   207  					t.Fatalf("failed to decode account on path %x: %v", path, err)
   208  				}
   209  				stTrie, err := trie.New(acc.Root, srcDb.TrieDB())
   210  				if err != nil {
   211  					t.Fatalf("failed to retriev storage trie for path %x: %v", path, err)
   212  				}
   213  				data, _, err := stTrie.TryGetNode(path[1])
   214  				if err != nil {
   215  					t.Fatalf("failed to retrieve node data for path %x: %v", path, err)
   216  				}
   217  				results[len(hashQueue)+i] = trie.SyncResult{Hash: crypto.Keccak256Hash(data), Data: data}
   218  			}
   219  		}
   220  		for _, result := range results {
   221  			if err := sched.Process(result); err != nil {
   222  				t.Errorf("failed to process result %v", err)
   223  			}
   224  		}
   225  		batch := dstDb.NewBatch()
   226  		if err := sched.Commit(batch); err != nil {
   227  			t.Fatalf("failed to commit data: %v", err)
   228  		}
   229  		batch.Write()
   230  
   231  		nodes, paths, codes = sched.Missing(count)
   232  		if !bypath {
   233  			hashQueue = append(append(hashQueue[:0], nodes...), codes...)
   234  		} else {
   235  			hashQueue = append(hashQueue[:0], codes...)
   236  			pathQueue = append(pathQueue[:0], paths...)
   237  		}
   238  	}
   239  	// Cross check that the two states are in sync
   240  	checkStateAccounts(t, dstDb, srcRoot, srcAccounts)
   241  }
   242  
   243  // Tests that the trie scheduler can correctly reconstruct the state even if only
   244  // partial results are returned, and the others sent only later.
   245  func TestIterativeDelayedStateSync(t *testing.T) {
   246  	// Create a random state to copy
   247  	srcDb, srcRoot, srcAccounts := makeTestState()
   248  
   249  	// Create a destination state and sync with the scheduler
   250  	dstDb := rawdb.NewMemoryDatabase()
   251  	sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb))
   252  
   253  	nodes, _, codes := sched.Missing(0)
   254  	queue := append(append([]common.Hash{}, nodes...), codes...)
   255  
   256  	for len(queue) > 0 {
   257  		// Sync only half of the scheduled nodes
   258  		results := make([]trie.SyncResult, len(queue)/2+1)
   259  		for i, hash := range queue[:len(results)] {
   260  			data, err := srcDb.TrieDB().Node(hash)
   261  			if err != nil {
   262  				data, err = srcDb.ContractCode(common.Hash{}, hash)
   263  			}
   264  			if err != nil {
   265  				t.Fatalf("failed to retrieve node data for %x", hash)
   266  			}
   267  			results[i] = trie.SyncResult{Hash: hash, Data: data}
   268  		}
   269  		for _, result := range results {
   270  			if err := sched.Process(result); err != nil {
   271  				t.Fatalf("failed to process result %v", err)
   272  			}
   273  		}
   274  		batch := dstDb.NewBatch()
   275  		if err := sched.Commit(batch); err != nil {
   276  			t.Fatalf("failed to commit data: %v", err)
   277  		}
   278  		batch.Write()
   279  
   280  		nodes, _, codes = sched.Missing(0)
   281  		queue = append(append(queue[len(results):], nodes...), codes...)
   282  	}
   283  	// Cross check that the two states are in sync
   284  	checkStateAccounts(t, dstDb, srcRoot, srcAccounts)
   285  }
   286  
   287  // Tests that given a root hash, a trie can sync iteratively on a single thread,
   288  // requesting retrieval tasks and returning all of them in one go, however in a
   289  // random order.
   290  func TestIterativeRandomStateSyncIndividual(t *testing.T) { testIterativeRandomStateSync(t, 1) }
   291  func TestIterativeRandomStateSyncBatched(t *testing.T)    { testIterativeRandomStateSync(t, 100) }
   292  
   293  func testIterativeRandomStateSync(t *testing.T, count int) {
   294  	// Create a random state to copy
   295  	srcDb, srcRoot, srcAccounts := makeTestState()
   296  
   297  	// Create a destination state and sync with the scheduler
   298  	dstDb := rawdb.NewMemoryDatabase()
   299  	sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb))
   300  
   301  	queue := make(map[common.Hash]struct{})
   302  	nodes, _, codes := sched.Missing(count)
   303  	for _, hash := range append(nodes, codes...) {
   304  		queue[hash] = struct{}{}
   305  	}
   306  	for len(queue) > 0 {
   307  		// Fetch all the queued nodes in a random order
   308  		results := make([]trie.SyncResult, 0, len(queue))
   309  		for hash := range queue {
   310  			data, err := srcDb.TrieDB().Node(hash)
   311  			if err != nil {
   312  				data, err = srcDb.ContractCode(common.Hash{}, hash)
   313  			}
   314  			if err != nil {
   315  				t.Fatalf("failed to retrieve node data for %x", hash)
   316  			}
   317  			results = append(results, trie.SyncResult{Hash: hash, Data: data})
   318  		}
   319  		// Feed the retrieved results back and queue new tasks
   320  		for _, result := range results {
   321  			if err := sched.Process(result); err != nil {
   322  				t.Fatalf("failed to process result %v", err)
   323  			}
   324  		}
   325  		batch := dstDb.NewBatch()
   326  		if err := sched.Commit(batch); err != nil {
   327  			t.Fatalf("failed to commit data: %v", err)
   328  		}
   329  		batch.Write()
   330  
   331  		queue = make(map[common.Hash]struct{})
   332  		nodes, _, codes = sched.Missing(count)
   333  		for _, hash := range append(nodes, codes...) {
   334  			queue[hash] = struct{}{}
   335  		}
   336  	}
   337  	// Cross check that the two states are in sync
   338  	checkStateAccounts(t, dstDb, srcRoot, srcAccounts)
   339  }
   340  
   341  // Tests that the trie scheduler can correctly reconstruct the state even if only
   342  // partial results are returned (Even those randomly), others sent only later.
   343  func TestIterativeRandomDelayedStateSync(t *testing.T) {
   344  	// Create a random state to copy
   345  	srcDb, srcRoot, srcAccounts := makeTestState()
   346  
   347  	// Create a destination state and sync with the scheduler
   348  	dstDb := rawdb.NewMemoryDatabase()
   349  	sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb))
   350  
   351  	queue := make(map[common.Hash]struct{})
   352  	nodes, _, codes := sched.Missing(0)
   353  	for _, hash := range append(nodes, codes...) {
   354  		queue[hash] = struct{}{}
   355  	}
   356  	for len(queue) > 0 {
   357  		// Sync only half of the scheduled nodes, even those in random order
   358  		results := make([]trie.SyncResult, 0, len(queue)/2+1)
   359  		for hash := range queue {
   360  			delete(queue, hash)
   361  
   362  			data, err := srcDb.TrieDB().Node(hash)
   363  			if err != nil {
   364  				data, err = srcDb.ContractCode(common.Hash{}, hash)
   365  			}
   366  			if err != nil {
   367  				t.Fatalf("failed to retrieve node data for %x", hash)
   368  			}
   369  			results = append(results, trie.SyncResult{Hash: hash, Data: data})
   370  
   371  			if len(results) >= cap(results) {
   372  				break
   373  			}
   374  		}
   375  		// Feed the retrieved results back and queue new tasks
   376  		for _, result := range results {
   377  			if err := sched.Process(result); err != nil {
   378  				t.Fatalf("failed to process result %v", err)
   379  			}
   380  		}
   381  		batch := dstDb.NewBatch()
   382  		if err := sched.Commit(batch); err != nil {
   383  			t.Fatalf("failed to commit data: %v", err)
   384  		}
   385  		batch.Write()
   386  		for _, result := range results {
   387  			delete(queue, result.Hash)
   388  		}
   389  		nodes, _, codes = sched.Missing(0)
   390  		for _, hash := range append(nodes, codes...) {
   391  			queue[hash] = struct{}{}
   392  		}
   393  	}
   394  	// Cross check that the two states are in sync
   395  	checkStateAccounts(t, dstDb, srcRoot, srcAccounts)
   396  }
   397  
   398  // Tests that at any point in time during a sync, only complete sub-tries are in
   399  // the database.
   400  func TestIncompleteStateSync(t *testing.T) {
   401  	// Create a random state to copy
   402  	srcDb, srcRoot, srcAccounts := makeTestState()
   403  
   404  	// isCode reports whether the hash is contract code hash.
   405  	isCode := func(hash common.Hash) bool {
   406  		for _, acc := range srcAccounts {
   407  			if hash == crypto.Keccak256Hash(acc.code) {
   408  				return true
   409  			}
   410  		}
   411  		return false
   412  	}
   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  	var added []common.Hash
   420  
   421  	nodes, _, codes := sched.Missing(1)
   422  	queue := append(append([]common.Hash{}, nodes...), codes...)
   423  
   424  	for len(queue) > 0 {
   425  		// Fetch a batch of state nodes
   426  		results := make([]trie.SyncResult, len(queue))
   427  		for i, hash := range queue {
   428  			data, err := srcDb.TrieDB().Node(hash)
   429  			if err != nil {
   430  				data, err = srcDb.ContractCode(common.Hash{}, hash)
   431  			}
   432  			if err != nil {
   433  				t.Fatalf("failed to retrieve node data for %x", hash)
   434  			}
   435  			results[i] = trie.SyncResult{Hash: hash, Data: data}
   436  		}
   437  		// Process each of the state nodes
   438  		for _, result := range results {
   439  			if err := sched.Process(result); err != nil {
   440  				t.Fatalf("failed to process result %v", err)
   441  			}
   442  		}
   443  		batch := dstDb.NewBatch()
   444  		if err := sched.Commit(batch); err != nil {
   445  			t.Fatalf("failed to commit data: %v", err)
   446  		}
   447  		batch.Write()
   448  		for _, result := range results {
   449  			added = append(added, result.Hash)
   450  		}
   451  		// Check that all known sub-tries added so far are complete or missing entirely.
   452  		for _, hash := range added {
   453  			if isCode(hash) {
   454  				continue
   455  			}
   456  			// Can't use checkStateConsistency here because subtrie keys may have odd
   457  			// length and crash in LeafKey.
   458  			if err := checkTrieConsistency(dstDb, hash); err != nil {
   459  				t.Fatalf("state inconsistent: %v", err)
   460  			}
   461  		}
   462  		// Fetch the next batch to retrieve
   463  		nodes, _, codes = sched.Missing(1)
   464  		queue = append(append(queue[:0], nodes...), codes...)
   465  	}
   466  	// Sanity check that removing any node from the database is detected
   467  	for _, node := range added[1:] {
   468  		var (
   469  			key  = node.Bytes()
   470  			code = isCode(node)
   471  			val  []byte
   472  		)
   473  		if code {
   474  			val = rawdb.ReadCode(dstDb, node)
   475  			rawdb.DeleteCode(dstDb, node)
   476  		} else {
   477  			val = rawdb.ReadTrieNode(dstDb, node)
   478  			rawdb.DeleteTrieNode(dstDb, node)
   479  		}
   480  		if err := checkStateConsistency(dstDb, added[0]); err == nil {
   481  			t.Fatalf("trie inconsistency not caught, missing: %x", key)
   482  		}
   483  		if code {
   484  			rawdb.WriteCode(dstDb, node, val)
   485  		} else {
   486  			rawdb.WriteTrieNode(dstDb, node, val)
   487  		}
   488  	}
   489  }