github.com/ethereum/go-ethereum@v1.16.1/triedb/pathdb/holdable_iterator_test.go (about)

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