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