github.com/codysnider/go-ethereum@v1.10.18-0.20220420071915-14f4ae99222a/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  	for i := 0; i < 128; i++ {
    34  		rawdb.WriteAccountSnapshot(db, randomHash(), randomHash().Bytes())
    35  	}
    36  	// Add some random non-snapshot data too to make wiping harder
    37  	for i := 0; i < 500; i++ {
    38  		// Generate keys with wrong length for a state snapshot item
    39  		keysuffix := make([]byte, 31)
    40  		rand.Read(keysuffix)
    41  		db.Put(append(rawdb.SnapshotAccountPrefix, keysuffix...), randomHash().Bytes())
    42  		keysuffix = make([]byte, 33)
    43  		rand.Read(keysuffix)
    44  		db.Put(append(rawdb.SnapshotAccountPrefix, keysuffix...), randomHash().Bytes())
    45  	}
    46  	count := func() (items int) {
    47  		it := db.NewIterator(rawdb.SnapshotAccountPrefix, nil)
    48  		defer it.Release()
    49  		for it.Next() {
    50  			if len(it.Key()) == len(rawdb.SnapshotAccountPrefix)+common.HashLength {
    51  				items++
    52  			}
    53  		}
    54  		return items
    55  	}
    56  	// Sanity check that all the keys are present
    57  	if items := count(); items != 128 {
    58  		t.Fatalf("snapshot size mismatch: have %d, want %d", items, 128)
    59  	}
    60  	// Wipe the accounts
    61  	if err := wipeKeyRange(db, "accounts", rawdb.SnapshotAccountPrefix, nil, nil,
    62  		len(rawdb.SnapshotAccountPrefix)+common.HashLength, snapWipedAccountMeter, true); err != nil {
    63  		t.Fatal(err)
    64  	}
    65  	// Iterate over the database end ensure no snapshot information remains
    66  	if items := count(); items != 0 {
    67  		t.Fatalf("snapshot size mismatch: have %d, want %d", items, 0)
    68  	}
    69  	// Iterate over the database and ensure miscellaneous items are present
    70  	items := 0
    71  	it := db.NewIterator(nil, nil)
    72  	defer it.Release()
    73  	for it.Next() {
    74  		items++
    75  	}
    76  	if items != 1000 {
    77  		t.Fatalf("misc item count mismatch: have %d, want %d", items, 1000)
    78  	}
    79  }