github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/trie/sync_test.go (about)

     1  // Copyright 2015 The Spectrum Authors
     2  // This file is part of the Spectrum library.
     3  //
     4  // The Spectrum 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 Spectrum 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 Spectrum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package trie
    18  
    19  import (
    20  	"bytes"
    21  	"testing"
    22  
    23  	"github.com/SmartMeshFoundation/Spectrum/common"
    24  	"github.com/SmartMeshFoundation/Spectrum/ethdb"
    25  )
    26  
    27  // makeTestTrie create a sample test trie to test node-wise reconstruction.
    28  func makeTestTrie() (ethdb.Database, *Trie, map[string][]byte) {
    29  	// Create an empty trie
    30  	db, _ := ethdb.NewMemDatabase()
    31  	trie, _ := New(common.Hash{}, db)
    32  
    33  	// Fill it with some arbitrary data
    34  	content := make(map[string][]byte)
    35  	for i := byte(0); i < 255; i++ {
    36  		// Map the same data under multiple keys
    37  		key, val := common.LeftPadBytes([]byte{1, i}, 32), []byte{i}
    38  		content[string(key)] = val
    39  		trie.Update(key, val)
    40  
    41  		key, val = common.LeftPadBytes([]byte{2, i}, 32), []byte{i}
    42  		content[string(key)] = val
    43  		trie.Update(key, val)
    44  
    45  		// Add some other data to inflate the trie
    46  		for j := byte(3); j < 13; j++ {
    47  			key, val = common.LeftPadBytes([]byte{j, i}, 32), []byte{j, i}
    48  			content[string(key)] = val
    49  			trie.Update(key, val)
    50  		}
    51  	}
    52  	trie.Commit()
    53  
    54  	// Return the generated trie
    55  	return db, trie, content
    56  }
    57  
    58  // checkTrieContents cross references a reconstructed trie with an expected data
    59  // content map.
    60  func checkTrieContents(t *testing.T, db Database, root []byte, content map[string][]byte) {
    61  	// Check root availability and trie contents
    62  	trie, err := New(common.BytesToHash(root), db)
    63  	if err != nil {
    64  		t.Fatalf("failed to create trie at %x: %v", root, err)
    65  	}
    66  	if err := checkTrieConsistency(db, common.BytesToHash(root)); err != nil {
    67  		t.Fatalf("inconsistent trie at %x: %v", root, err)
    68  	}
    69  	for key, val := range content {
    70  		if have := trie.Get([]byte(key)); !bytes.Equal(have, val) {
    71  			t.Errorf("entry %x: content mismatch: have %x, want %x", key, have, val)
    72  		}
    73  	}
    74  }
    75  
    76  // checkTrieConsistency checks that all nodes in a trie are indeed present.
    77  func checkTrieConsistency(db Database, root common.Hash) error {
    78  	// Create and iterate a trie rooted in a subnode
    79  	trie, err := New(root, db)
    80  	if err != nil {
    81  		return nil // Consider a non existent state consistent
    82  	}
    83  	it := trie.NodeIterator(nil)
    84  	for it.Next(true) {
    85  	}
    86  	return it.Error()
    87  }
    88  
    89  // Tests that an empty trie is not scheduled for syncing.
    90  func TestEmptyTrieSync(t *testing.T) {
    91  	emptyA, _ := New(common.Hash{}, nil)
    92  	emptyB, _ := New(emptyRoot, nil)
    93  
    94  	for i, trie := range []*Trie{emptyA, emptyB} {
    95  		db, _ := ethdb.NewMemDatabase()
    96  		if req := NewTrieSync(common.BytesToHash(trie.Root()), db, nil).Missing(1); len(req) != 0 {
    97  			t.Errorf("test %d: content requested for empty trie: %v", i, req)
    98  		}
    99  	}
   100  }
   101  
   102  // Tests that given a root hash, a trie can sync iteratively on a single thread,
   103  // requesting retrieval tasks and returning all of them in one go.
   104  func TestIterativeTrieSyncIndividual(t *testing.T) { testIterativeTrieSync(t, 1) }
   105  func TestIterativeTrieSyncBatched(t *testing.T)    { testIterativeTrieSync(t, 100) }
   106  
   107  func testIterativeTrieSync(t *testing.T, batch int) {
   108  	// Create a random trie to copy
   109  	srcDb, srcTrie, srcData := makeTestTrie()
   110  
   111  	// Create a destination trie and sync with the scheduler
   112  	dstDb, _ := ethdb.NewMemDatabase()
   113  	sched := NewTrieSync(common.BytesToHash(srcTrie.Root()), dstDb, nil)
   114  
   115  	queue := append([]common.Hash{}, sched.Missing(batch)...)
   116  	for len(queue) > 0 {
   117  		results := make([]SyncResult, len(queue))
   118  		for i, hash := range queue {
   119  			data, err := srcDb.Get(hash.Bytes())
   120  			if err != nil {
   121  				t.Fatalf("failed to retrieve node data for %x: %v", hash, err)
   122  			}
   123  			results[i] = SyncResult{hash, data}
   124  		}
   125  		if _, index, err := sched.Process(results); err != nil {
   126  			t.Fatalf("failed to process result #%d: %v", index, err)
   127  		}
   128  		if index, err := sched.Commit(dstDb); err != nil {
   129  			t.Fatalf("failed to commit data #%d: %v", index, err)
   130  		}
   131  		queue = append(queue[:0], sched.Missing(batch)...)
   132  	}
   133  	// Cross check that the two tries are in sync
   134  	checkTrieContents(t, dstDb, srcTrie.Root(), srcData)
   135  }
   136  
   137  // Tests that the trie scheduler can correctly reconstruct the state even if only
   138  // partial results are returned, and the others sent only later.
   139  func TestIterativeDelayedTrieSync(t *testing.T) {
   140  	// Create a random trie to copy
   141  	srcDb, srcTrie, srcData := makeTestTrie()
   142  
   143  	// Create a destination trie and sync with the scheduler
   144  	dstDb, _ := ethdb.NewMemDatabase()
   145  	sched := NewTrieSync(common.BytesToHash(srcTrie.Root()), dstDb, nil)
   146  
   147  	queue := append([]common.Hash{}, sched.Missing(10000)...)
   148  	for len(queue) > 0 {
   149  		// Sync only half of the scheduled nodes
   150  		results := make([]SyncResult, len(queue)/2+1)
   151  		for i, hash := range queue[:len(results)] {
   152  			data, err := srcDb.Get(hash.Bytes())
   153  			if err != nil {
   154  				t.Fatalf("failed to retrieve node data for %x: %v", hash, err)
   155  			}
   156  			results[i] = SyncResult{hash, data}
   157  		}
   158  		if _, index, err := sched.Process(results); err != nil {
   159  			t.Fatalf("failed to process result #%d: %v", index, err)
   160  		}
   161  		if index, err := sched.Commit(dstDb); err != nil {
   162  			t.Fatalf("failed to commit data #%d: %v", index, err)
   163  		}
   164  		queue = append(queue[len(results):], sched.Missing(10000)...)
   165  	}
   166  	// Cross check that the two tries are in sync
   167  	checkTrieContents(t, dstDb, srcTrie.Root(), srcData)
   168  }
   169  
   170  // Tests that given a root hash, a trie can sync iteratively on a single thread,
   171  // requesting retrieval tasks and returning all of them in one go, however in a
   172  // random order.
   173  func TestIterativeRandomTrieSyncIndividual(t *testing.T) { testIterativeRandomTrieSync(t, 1) }
   174  func TestIterativeRandomTrieSyncBatched(t *testing.T)    { testIterativeRandomTrieSync(t, 100) }
   175  
   176  func testIterativeRandomTrieSync(t *testing.T, batch int) {
   177  	// Create a random trie to copy
   178  	srcDb, srcTrie, srcData := makeTestTrie()
   179  
   180  	// Create a destination trie and sync with the scheduler
   181  	dstDb, _ := ethdb.NewMemDatabase()
   182  	sched := NewTrieSync(common.BytesToHash(srcTrie.Root()), dstDb, nil)
   183  
   184  	queue := make(map[common.Hash]struct{})
   185  	for _, hash := range sched.Missing(batch) {
   186  		queue[hash] = struct{}{}
   187  	}
   188  	for len(queue) > 0 {
   189  		// Fetch all the queued nodes in a random order
   190  		results := make([]SyncResult, 0, len(queue))
   191  		for hash := range queue {
   192  			data, err := srcDb.Get(hash.Bytes())
   193  			if err != nil {
   194  				t.Fatalf("failed to retrieve node data for %x: %v", hash, err)
   195  			}
   196  			results = append(results, SyncResult{hash, data})
   197  		}
   198  		// Feed the retrieved results back and queue new tasks
   199  		if _, index, err := sched.Process(results); err != nil {
   200  			t.Fatalf("failed to process result #%d: %v", index, err)
   201  		}
   202  		if index, err := sched.Commit(dstDb); err != nil {
   203  			t.Fatalf("failed to commit data #%d: %v", index, err)
   204  		}
   205  		queue = make(map[common.Hash]struct{})
   206  		for _, hash := range sched.Missing(batch) {
   207  			queue[hash] = struct{}{}
   208  		}
   209  	}
   210  	// Cross check that the two tries are in sync
   211  	checkTrieContents(t, dstDb, srcTrie.Root(), srcData)
   212  }
   213  
   214  // Tests that the trie scheduler can correctly reconstruct the state even if only
   215  // partial results are returned (Even those randomly), others sent only later.
   216  func TestIterativeRandomDelayedTrieSync(t *testing.T) {
   217  	// Create a random trie to copy
   218  	srcDb, srcTrie, srcData := makeTestTrie()
   219  
   220  	// Create a destination trie and sync with the scheduler
   221  	dstDb, _ := ethdb.NewMemDatabase()
   222  	sched := NewTrieSync(common.BytesToHash(srcTrie.Root()), dstDb, nil)
   223  
   224  	queue := make(map[common.Hash]struct{})
   225  	for _, hash := range sched.Missing(10000) {
   226  		queue[hash] = struct{}{}
   227  	}
   228  	for len(queue) > 0 {
   229  		// Sync only half of the scheduled nodes, even those in random order
   230  		results := make([]SyncResult, 0, len(queue)/2+1)
   231  		for hash := range queue {
   232  			data, err := srcDb.Get(hash.Bytes())
   233  			if err != nil {
   234  				t.Fatalf("failed to retrieve node data for %x: %v", hash, err)
   235  			}
   236  			results = append(results, SyncResult{hash, data})
   237  
   238  			if len(results) >= cap(results) {
   239  				break
   240  			}
   241  		}
   242  		// Feed the retrieved results back and queue new tasks
   243  		if _, index, err := sched.Process(results); err != nil {
   244  			t.Fatalf("failed to process result #%d: %v", index, err)
   245  		}
   246  		if index, err := sched.Commit(dstDb); err != nil {
   247  			t.Fatalf("failed to commit data #%d: %v", index, err)
   248  		}
   249  		for _, result := range results {
   250  			delete(queue, result.Hash)
   251  		}
   252  		for _, hash := range sched.Missing(10000) {
   253  			queue[hash] = struct{}{}
   254  		}
   255  	}
   256  	// Cross check that the two tries are in sync
   257  	checkTrieContents(t, dstDb, srcTrie.Root(), srcData)
   258  }
   259  
   260  // Tests that a trie sync will not request nodes multiple times, even if they
   261  // have such references.
   262  func TestDuplicateAvoidanceTrieSync(t *testing.T) {
   263  	// Create a random trie to copy
   264  	srcDb, srcTrie, srcData := makeTestTrie()
   265  
   266  	// Create a destination trie and sync with the scheduler
   267  	dstDb, _ := ethdb.NewMemDatabase()
   268  	sched := NewTrieSync(common.BytesToHash(srcTrie.Root()), dstDb, nil)
   269  
   270  	queue := append([]common.Hash{}, sched.Missing(0)...)
   271  	requested := make(map[common.Hash]struct{})
   272  
   273  	for len(queue) > 0 {
   274  		results := make([]SyncResult, len(queue))
   275  		for i, hash := range queue {
   276  			data, err := srcDb.Get(hash.Bytes())
   277  			if err != nil {
   278  				t.Fatalf("failed to retrieve node data for %x: %v", hash, err)
   279  			}
   280  			if _, ok := requested[hash]; ok {
   281  				t.Errorf("hash %x already requested once", hash)
   282  			}
   283  			requested[hash] = struct{}{}
   284  
   285  			results[i] = SyncResult{hash, data}
   286  		}
   287  		if _, index, err := sched.Process(results); err != nil {
   288  			t.Fatalf("failed to process result #%d: %v", index, err)
   289  		}
   290  		if index, err := sched.Commit(dstDb); err != nil {
   291  			t.Fatalf("failed to commit data #%d: %v", index, err)
   292  		}
   293  		queue = append(queue[:0], sched.Missing(0)...)
   294  	}
   295  	// Cross check that the two tries are in sync
   296  	checkTrieContents(t, dstDb, srcTrie.Root(), srcData)
   297  }
   298  
   299  // Tests that at any point in time during a sync, only complete sub-tries are in
   300  // the database.
   301  func TestIncompleteTrieSync(t *testing.T) {
   302  	// Create a random trie to copy
   303  	srcDb, srcTrie, _ := makeTestTrie()
   304  
   305  	// Create a destination trie and sync with the scheduler
   306  	dstDb, _ := ethdb.NewMemDatabase()
   307  	sched := NewTrieSync(common.BytesToHash(srcTrie.Root()), dstDb, nil)
   308  
   309  	added := []common.Hash{}
   310  	queue := append([]common.Hash{}, sched.Missing(1)...)
   311  	for len(queue) > 0 {
   312  		// Fetch a batch of trie nodes
   313  		results := make([]SyncResult, len(queue))
   314  		for i, hash := range queue {
   315  			data, err := srcDb.Get(hash.Bytes())
   316  			if err != nil {
   317  				t.Fatalf("failed to retrieve node data for %x: %v", hash, err)
   318  			}
   319  			results[i] = SyncResult{hash, data}
   320  		}
   321  		// Process each of the trie nodes
   322  		if _, index, err := sched.Process(results); err != nil {
   323  			t.Fatalf("failed to process result #%d: %v", index, err)
   324  		}
   325  		if index, err := sched.Commit(dstDb); err != nil {
   326  			t.Fatalf("failed to commit data #%d: %v", index, err)
   327  		}
   328  		for _, result := range results {
   329  			added = append(added, result.Hash)
   330  		}
   331  		// Check that all known sub-tries in the synced trie are complete
   332  		for _, root := range added {
   333  			if err := checkTrieConsistency(dstDb, root); err != nil {
   334  				t.Fatalf("trie inconsistent: %v", err)
   335  			}
   336  		}
   337  		// Fetch the next batch to retrieve
   338  		queue = append(queue[:0], sched.Missing(1)...)
   339  	}
   340  	// Sanity check that removing any node from the database is detected
   341  	for _, node := range added[1:] {
   342  		key := node.Bytes()
   343  		value, _ := dstDb.Get(key)
   344  
   345  		dstDb.Delete(key)
   346  		if err := checkTrieConsistency(dstDb, added[0]); err == nil {
   347  			t.Fatalf("trie inconsistency not caught, missing: %x", key)
   348  		}
   349  		dstDb.Put(key, value)
   350  	}
   351  }