github.com/ethereum/go-ethereum@v1.16.1/trie/utils/verkle_test.go (about)

     1  // Copyright 2023 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 utils
    18  
    19  import (
    20  	"bytes"
    21  	"testing"
    22  
    23  	"github.com/ethereum/go-verkle"
    24  	"github.com/holiman/uint256"
    25  )
    26  
    27  func TestTreeKey(t *testing.T) {
    28  	var (
    29  		address      = []byte{0x01}
    30  		addressEval  = evaluateAddressPoint(address)
    31  		smallIndex   = uint256.NewInt(1)
    32  		largeIndex   = uint256.NewInt(10000)
    33  		smallStorage = []byte{0x1}
    34  		largeStorage = bytes.Repeat([]byte{0xff}, 16)
    35  	)
    36  	if !bytes.Equal(BasicDataKey(address), BasicDataKeyWithEvaluatedAddress(addressEval)) {
    37  		t.Fatal("Unmatched basic data key")
    38  	}
    39  	if !bytes.Equal(CodeHashKey(address), CodeHashKeyWithEvaluatedAddress(addressEval)) {
    40  		t.Fatal("Unmatched code hash key")
    41  	}
    42  	if !bytes.Equal(CodeChunkKey(address, smallIndex), CodeChunkKeyWithEvaluatedAddress(addressEval, smallIndex)) {
    43  		t.Fatal("Unmatched code chunk key")
    44  	}
    45  	if !bytes.Equal(CodeChunkKey(address, largeIndex), CodeChunkKeyWithEvaluatedAddress(addressEval, largeIndex)) {
    46  		t.Fatal("Unmatched code chunk key")
    47  	}
    48  	if !bytes.Equal(StorageSlotKey(address, smallStorage), StorageSlotKeyWithEvaluatedAddress(addressEval, smallStorage)) {
    49  		t.Fatal("Unmatched storage slot key")
    50  	}
    51  	if !bytes.Equal(StorageSlotKey(address, largeStorage), StorageSlotKeyWithEvaluatedAddress(addressEval, largeStorage)) {
    52  		t.Fatal("Unmatched storage slot key")
    53  	}
    54  }
    55  
    56  // goos: darwin
    57  // goarch: amd64
    58  // pkg: github.com/ethereum/go-ethereum/trie/utils
    59  // cpu: VirtualApple @ 2.50GHz
    60  // BenchmarkTreeKey
    61  // BenchmarkTreeKey-8   	  398731	      2961 ns/op	      32 B/op	       1 allocs/op
    62  func BenchmarkTreeKey(b *testing.B) {
    63  	// Initialize the IPA settings which can be pretty expensive.
    64  	verkle.GetConfig()
    65  
    66  	b.ReportAllocs()
    67  	b.ResetTimer()
    68  
    69  	for i := 0; i < b.N; i++ {
    70  		BasicDataKey([]byte{0x01})
    71  	}
    72  }
    73  
    74  // goos: darwin
    75  // goarch: amd64
    76  // pkg: github.com/ethereum/go-ethereum/trie/utils
    77  // cpu: VirtualApple @ 2.50GHz
    78  // BenchmarkTreeKeyWithEvaluation
    79  // BenchmarkTreeKeyWithEvaluation-8   	  513855	      2324 ns/op	      32 B/op	       1 allocs/op
    80  func BenchmarkTreeKeyWithEvaluation(b *testing.B) {
    81  	// Initialize the IPA settings which can be pretty expensive.
    82  	verkle.GetConfig()
    83  
    84  	addr := []byte{0x01}
    85  	eval := evaluateAddressPoint(addr)
    86  
    87  	b.ReportAllocs()
    88  	b.ResetTimer()
    89  	for i := 0; i < b.N; i++ {
    90  		BasicDataKeyWithEvaluatedAddress(eval)
    91  	}
    92  }
    93  
    94  // goos: darwin
    95  // goarch: amd64
    96  // pkg: github.com/ethereum/go-ethereum/trie/utils
    97  // cpu: VirtualApple @ 2.50GHz
    98  // BenchmarkStorageKey
    99  // BenchmarkStorageKey-8   	  230516	      4584 ns/op	      96 B/op	       3 allocs/op
   100  func BenchmarkStorageKey(b *testing.B) {
   101  	// Initialize the IPA settings which can be pretty expensive.
   102  	verkle.GetConfig()
   103  
   104  	b.ReportAllocs()
   105  	b.ResetTimer()
   106  
   107  	for i := 0; i < b.N; i++ {
   108  		StorageSlotKey([]byte{0x01}, bytes.Repeat([]byte{0xff}, 32))
   109  	}
   110  }
   111  
   112  // goos: darwin
   113  // goarch: amd64
   114  // pkg: github.com/ethereum/go-ethereum/trie/utils
   115  // cpu: VirtualApple @ 2.50GHz
   116  // BenchmarkStorageKeyWithEvaluation
   117  // BenchmarkStorageKeyWithEvaluation-8   	  320125	      3753 ns/op	      96 B/op	       3 allocs/op
   118  func BenchmarkStorageKeyWithEvaluation(b *testing.B) {
   119  	// Initialize the IPA settings which can be pretty expensive.
   120  	verkle.GetConfig()
   121  
   122  	addr := []byte{0x01}
   123  	eval := evaluateAddressPoint(addr)
   124  
   125  	b.ReportAllocs()
   126  	b.ResetTimer()
   127  	for i := 0; i < b.N; i++ {
   128  		StorageSlotKeyWithEvaluatedAddress(eval, bytes.Repeat([]byte{0xff}, 32))
   129  	}
   130  }