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

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