github.com/MetalBlockchain/subnet-evm@v0.4.9/core/state/snapshot/wipe_test.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc.
     2  //
     3  // This file is a derived work, based on the go-ethereum library whose original
     4  // notices appear below.
     5  //
     6  // It is distributed under a license compatible with the licensing terms of the
     7  // original code from which it is derived.
     8  //
     9  // Much love to the original authors for their work.
    10  // **********
    11  // Copyright 2019 The go-ethereum Authors
    12  // This file is part of the go-ethereum library.
    13  //
    14  // The go-ethereum library is free software: you can redistribute it and/or modify
    15  // it under the terms of the GNU Lesser General Public License as published by
    16  // the Free Software Foundation, either version 3 of the License, or
    17  // (at your option) any later version.
    18  //
    19  // The go-ethereum library is distributed in the hope that it will be useful,
    20  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    22  // GNU Lesser General Public License for more details.
    23  //
    24  // You should have received a copy of the GNU Lesser General Public License
    25  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    26  
    27  package snapshot
    28  
    29  import (
    30  	"math/rand"
    31  	"testing"
    32  
    33  	"github.com/MetalBlockchain/subnet-evm/core/rawdb"
    34  	"github.com/MetalBlockchain/subnet-evm/ethdb/memorydb"
    35  	"github.com/ethereum/go-ethereum/common"
    36  )
    37  
    38  // Tests that given a database with random data content, all parts of a snapshot
    39  // can be crrectly wiped without touching anything else.
    40  func TestWipe(t *testing.T) {
    41  	// Create a database with some random snapshot data
    42  	db := memorydb.New()
    43  	for i := 0; i < 128; i++ {
    44  		rawdb.WriteAccountSnapshot(db, randomHash(), randomHash().Bytes())
    45  	}
    46  	rawdb.WriteSnapshotBlockHash(db, randomHash())
    47  	rawdb.WriteSnapshotRoot(db, randomHash())
    48  
    49  	// Add some random non-snapshot data too to make wiping harder
    50  	for i := 0; i < 500; i++ {
    51  		// Generate keys with wrong length for a state snapshot item
    52  		keysuffix := make([]byte, 31)
    53  		rand.Read(keysuffix)
    54  		db.Put(append(rawdb.SnapshotAccountPrefix, keysuffix...), randomHash().Bytes())
    55  		keysuffix = make([]byte, 33)
    56  		rand.Read(keysuffix)
    57  		db.Put(append(rawdb.SnapshotAccountPrefix, keysuffix...), randomHash().Bytes())
    58  	}
    59  	count := func() (items int) {
    60  		it := db.NewIterator(rawdb.SnapshotAccountPrefix, nil)
    61  		defer it.Release()
    62  		for it.Next() {
    63  			if len(it.Key()) == len(rawdb.SnapshotAccountPrefix)+common.HashLength {
    64  				items++
    65  			}
    66  		}
    67  		return items
    68  	}
    69  	// Sanity check that all the keys are present
    70  	if items := count(); items != 128 {
    71  		t.Fatalf("snapshot size mismatch: have %d, want %d", items, 128)
    72  	}
    73  	if hash := rawdb.ReadSnapshotBlockHash(db); hash == (common.Hash{}) {
    74  		t.Errorf("snapshot block hash marker mismatch: have %#x, want <not-nil>", hash)
    75  	}
    76  	if hash := rawdb.ReadSnapshotRoot(db); hash == (common.Hash{}) {
    77  		t.Errorf("snapshot block root marker mismatch: have %#x, want <not-nil>", hash)
    78  	}
    79  	// Wipe all snapshot entries from the database
    80  	<-WipeSnapshot(db, true)
    81  
    82  	// Iterate over the database end ensure no snapshot information remains
    83  	if items := count(); items != 0 {
    84  		t.Fatalf("snapshot size mismatch: have %d, want %d", items, 0)
    85  	}
    86  	// Iterate over the database and ensure miscellaneous items are present
    87  	items := 0
    88  	it := db.NewIterator(nil, nil)
    89  	defer it.Release()
    90  	for it.Next() {
    91  		items++
    92  	}
    93  	if items != 1000 {
    94  		t.Fatalf("misc item count mismatch: have %d, want %d", items, 1000)
    95  	}
    96  
    97  	if hash := rawdb.ReadSnapshotBlockHash(db); hash != (common.Hash{}) {
    98  		t.Errorf("snapshot block hash marker remained after wipe: %#x", hash)
    99  	}
   100  	if hash := rawdb.ReadSnapshotRoot(db); hash != (common.Hash{}) {
   101  		t.Errorf("snapshot block root marker remained after wipe: %#x", hash)
   102  	}
   103  }