github.com/tirogen/go-ethereum@v1.10.12-0.20221226051715-250cfede41b6/core/state/snapshot/holdable_iterator_test.go (about)

     1  // Copyright 2022 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  	"bytes"
    21  	"testing"
    22  
    23  	"github.com/tirogen/go-ethereum/common"
    24  	"github.com/tirogen/go-ethereum/core/rawdb"
    25  )
    26  
    27  func TestIteratorHold(t *testing.T) {
    28  	// Create the key-value data store
    29  	var (
    30  		content = map[string]string{"k1": "v1", "k2": "v2", "k3": "v3"}
    31  		order   = []string{"k1", "k2", "k3"}
    32  		db      = rawdb.NewMemoryDatabase()
    33  	)
    34  	for key, val := range content {
    35  		if err := db.Put([]byte(key), []byte(val)); err != nil {
    36  			t.Fatalf("failed to insert item %s:%s into database: %v", key, val, err)
    37  		}
    38  	}
    39  	// Iterate over the database with the given configs and verify the results
    40  	it, idx := newHoldableIterator(db.NewIterator(nil, nil)), 0
    41  
    42  	// Nothing should be affected for calling Discard on non-initialized iterator
    43  	it.Hold()
    44  
    45  	for it.Next() {
    46  		if len(content) <= idx {
    47  			t.Errorf("more items than expected: checking idx=%d (key %q), expecting len=%d", idx, it.Key(), len(order))
    48  			break
    49  		}
    50  		if !bytes.Equal(it.Key(), []byte(order[idx])) {
    51  			t.Errorf("item %d: key mismatch: have %s, want %s", idx, string(it.Key()), order[idx])
    52  		}
    53  		if !bytes.Equal(it.Value(), []byte(content[order[idx]])) {
    54  			t.Errorf("item %d: value mismatch: have %s, want %s", idx, string(it.Value()), content[order[idx]])
    55  		}
    56  		// Should be safe to call discard multiple times
    57  		it.Hold()
    58  		it.Hold()
    59  
    60  		// Shift iterator to the discarded element
    61  		it.Next()
    62  		if !bytes.Equal(it.Key(), []byte(order[idx])) {
    63  			t.Errorf("item %d: key mismatch: have %s, want %s", idx, string(it.Key()), order[idx])
    64  		}
    65  		if !bytes.Equal(it.Value(), []byte(content[order[idx]])) {
    66  			t.Errorf("item %d: value mismatch: have %s, want %s", idx, string(it.Value()), content[order[idx]])
    67  		}
    68  
    69  		// Discard/Next combo should work always
    70  		it.Hold()
    71  		it.Next()
    72  		if !bytes.Equal(it.Key(), []byte(order[idx])) {
    73  			t.Errorf("item %d: key mismatch: have %s, want %s", idx, string(it.Key()), order[idx])
    74  		}
    75  		if !bytes.Equal(it.Value(), []byte(content[order[idx]])) {
    76  			t.Errorf("item %d: value mismatch: have %s, want %s", idx, string(it.Value()), content[order[idx]])
    77  		}
    78  		idx++
    79  	}
    80  	if err := it.Error(); err != nil {
    81  		t.Errorf("iteration failed: %v", err)
    82  	}
    83  	if idx != len(order) {
    84  		t.Errorf("iteration terminated prematurely: have %d, want %d", idx, len(order))
    85  	}
    86  	db.Close()
    87  }
    88  
    89  func TestReopenIterator(t *testing.T) {
    90  	var (
    91  		content = map[common.Hash]string{
    92  			common.HexToHash("a1"): "v1",
    93  			common.HexToHash("a2"): "v2",
    94  			common.HexToHash("a3"): "v3",
    95  			common.HexToHash("a4"): "v4",
    96  			common.HexToHash("a5"): "v5",
    97  			common.HexToHash("a6"): "v6",
    98  		}
    99  		order = []common.Hash{
   100  			common.HexToHash("a1"),
   101  			common.HexToHash("a2"),
   102  			common.HexToHash("a3"),
   103  			common.HexToHash("a4"),
   104  			common.HexToHash("a5"),
   105  			common.HexToHash("a6"),
   106  		}
   107  		db = rawdb.NewMemoryDatabase()
   108  	)
   109  	for key, val := range content {
   110  		rawdb.WriteAccountSnapshot(db, key, []byte(val))
   111  	}
   112  	checkVal := func(it *holdableIterator, index int) {
   113  		if !bytes.Equal(it.Key(), append(rawdb.SnapshotAccountPrefix, order[index].Bytes()...)) {
   114  			t.Fatalf("Unexpected data entry key, want %v got %v", order[index], it.Key())
   115  		}
   116  		if !bytes.Equal(it.Value(), []byte(content[order[index]])) {
   117  			t.Fatalf("Unexpected data entry key, want %v got %v", []byte(content[order[index]]), it.Value())
   118  		}
   119  	}
   120  	// Iterate over the database with the given configs and verify the results
   121  	ctx, idx := newGeneratorContext(&generatorStats{}, db, nil, nil), -1
   122  
   123  	idx++
   124  	ctx.account.Next()
   125  	checkVal(ctx.account, idx)
   126  
   127  	ctx.reopenIterator(snapAccount)
   128  	idx++
   129  	ctx.account.Next()
   130  	checkVal(ctx.account, idx)
   131  
   132  	// reopen twice
   133  	ctx.reopenIterator(snapAccount)
   134  	ctx.reopenIterator(snapAccount)
   135  	idx++
   136  	ctx.account.Next()
   137  	checkVal(ctx.account, idx)
   138  
   139  	// reopen iterator with held value
   140  	ctx.account.Next()
   141  	ctx.account.Hold()
   142  	ctx.reopenIterator(snapAccount)
   143  	idx++
   144  	ctx.account.Next()
   145  	checkVal(ctx.account, idx)
   146  
   147  	// reopen twice iterator with held value
   148  	ctx.account.Next()
   149  	ctx.account.Hold()
   150  	ctx.reopenIterator(snapAccount)
   151  	ctx.reopenIterator(snapAccount)
   152  	idx++
   153  	ctx.account.Next()
   154  	checkVal(ctx.account, idx)
   155  
   156  	// shift to the end and reopen
   157  	ctx.account.Next() // the end
   158  	ctx.reopenIterator(snapAccount)
   159  	ctx.account.Next()
   160  	if ctx.account.Key() != nil {
   161  		t.Fatal("Unexpected iterated entry")
   162  	}
   163  }