github.com/core-coin/go-core/v2@v2.1.9/core/state/snapshot/wipe_test.go (about)

     1  // Copyright 2019 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package snapshot
    18  
    19  import (
    20  	"math/rand"
    21  	"testing"
    22  
    23  	"github.com/core-coin/go-core/v2/xcbdb/memorydb"
    24  
    25  	"github.com/core-coin/go-core/v2/common"
    26  	"github.com/core-coin/go-core/v2/core/rawdb"
    27  )
    28  
    29  // Tests that given a database with random data content, all parts of a snapshot
    30  // can be crrectly wiped without touching anything else.
    31  func TestWipe(t *testing.T) {
    32  	// Create a database with some random snapshot data
    33  	db := memorydb.New()
    34  
    35  	for i := 0; i < 128; i++ {
    36  		account := randomHash()
    37  		rawdb.WriteAccountSnapshot(db, account, randomHash().Bytes())
    38  		for j := 0; j < 1024; j++ {
    39  			rawdb.WriteStorageSnapshot(db, account, randomHash(), randomHash().Bytes())
    40  		}
    41  	}
    42  	rawdb.WriteSnapshotRoot(db, randomHash())
    43  
    44  	// Add some random non-snapshot data too to make wiping harder
    45  	for i := 0; i < 65536; i++ {
    46  		// Generate a key that's the wrong length for a state snapshot item
    47  		var keysize int
    48  		for keysize == 0 || keysize == 32 || keysize == 64 {
    49  			keysize = 8 + rand.Intn(64) // +8 to ensure we will "never" randomize duplicates
    50  		}
    51  		// Randomize the suffix, dedup and inject it under the snapshot namespace
    52  		keysuffix := make([]byte, keysize)
    53  		rand.Read(keysuffix)
    54  
    55  		if rand.Int31n(2) == 0 {
    56  			db.Put(append(rawdb.SnapshotAccountPrefix, keysuffix...), randomHash().Bytes())
    57  		} else {
    58  			db.Put(append(rawdb.SnapshotStoragePrefix, keysuffix...), randomHash().Bytes())
    59  		}
    60  	}
    61  	// Sanity check that all the keys are present
    62  	var items int
    63  
    64  	it := db.NewIterator(rawdb.SnapshotAccountPrefix, nil)
    65  	defer it.Release()
    66  
    67  	for it.Next() {
    68  		key := it.Key()
    69  		if len(key) == len(rawdb.SnapshotAccountPrefix)+common.HashLength {
    70  			items++
    71  		}
    72  	}
    73  	it = db.NewIterator(rawdb.SnapshotStoragePrefix, nil)
    74  	defer it.Release()
    75  
    76  	for it.Next() {
    77  		key := it.Key()
    78  		if len(key) == len(rawdb.SnapshotStoragePrefix)+2*common.HashLength {
    79  			items++
    80  		}
    81  	}
    82  	if items != 128+128*1024 {
    83  		t.Fatalf("snapshot size mismatch: have %d, want %d", items, 128+128*1024)
    84  	}
    85  	if hash := rawdb.ReadSnapshotRoot(db); hash == (common.Hash{}) {
    86  		t.Errorf("snapshot block marker mismatch: have %#x, want <not-nil>", hash)
    87  	}
    88  	// Wipe all snapshot entries from the database
    89  	<-wipeSnapshot(db, true)
    90  
    91  	// Iterate over the database end ensure no snapshot information remains
    92  	it = db.NewIterator(rawdb.SnapshotAccountPrefix, nil)
    93  	defer it.Release()
    94  
    95  	for it.Next() {
    96  		key := it.Key()
    97  		if len(key) == len(rawdb.SnapshotAccountPrefix)+common.HashLength {
    98  			t.Errorf("snapshot entry remained after wipe: %x", key)
    99  		}
   100  	}
   101  	it = db.NewIterator(rawdb.SnapshotStoragePrefix, nil)
   102  	defer it.Release()
   103  
   104  	for it.Next() {
   105  		key := it.Key()
   106  		if len(key) == len(rawdb.SnapshotStoragePrefix)+2*common.HashLength {
   107  			t.Errorf("snapshot entry remained after wipe: %x", key)
   108  		}
   109  	}
   110  	if hash := rawdb.ReadSnapshotRoot(db); hash != (common.Hash{}) {
   111  		t.Errorf("snapshot block marker remained after wipe: %#x", hash)
   112  	}
   113  	// Iterate over the database and ensure miscellaneous items are present
   114  	items = 0
   115  
   116  	it = db.NewIterator(nil, nil)
   117  	defer it.Release()
   118  
   119  	for it.Next() {
   120  		items++
   121  	}
   122  	if items != 65536 {
   123  		t.Fatalf("misc item count mismatch: have %d, want %d", items, 65536)
   124  	}
   125  }