github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/trie/utils/verkle_test.go (about)

     1  // Copyright 2023 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/gballet/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(VersionKey(address), VersionKeyWithEvaluatedAddress(addressEval)) {
    37  		t.Fatal("Unmatched version key")
    38  	}
    39  	if !bytes.Equal(BalanceKey(address), BalanceKeyWithEvaluatedAddress(addressEval)) {
    40  		t.Fatal("Unmatched balance key")
    41  	}
    42  	if !bytes.Equal(NonceKey(address), NonceKeyWithEvaluatedAddress(addressEval)) {
    43  		t.Fatal("Unmatched nonce key")
    44  	}
    45  	if !bytes.Equal(CodeKeccakKey(address), CodeKeccakKeyWithEvaluatedAddress(addressEval)) {
    46  		t.Fatal("Unmatched code keccak key")
    47  	}
    48  	if !bytes.Equal(CodeSizeKey(address), CodeSizeKeyWithEvaluatedAddress(addressEval)) {
    49  		t.Fatal("Unmatched code size key")
    50  	}
    51  	if !bytes.Equal(CodeChunkKey(address, smallIndex), CodeChunkKeyWithEvaluatedAddress(addressEval, smallIndex)) {
    52  		t.Fatal("Unmatched code chunk key")
    53  	}
    54  	if !bytes.Equal(CodeChunkKey(address, largeIndex), CodeChunkKeyWithEvaluatedAddress(addressEval, largeIndex)) {
    55  		t.Fatal("Unmatched code chunk key")
    56  	}
    57  	if !bytes.Equal(StorageSlotKey(address, smallStorage), StorageSlotKeyWithEvaluatedAddress(addressEval, smallStorage)) {
    58  		t.Fatal("Unmatched storage slot key")
    59  	}
    60  	if !bytes.Equal(StorageSlotKey(address, largeStorage), StorageSlotKeyWithEvaluatedAddress(addressEval, largeStorage)) {
    61  		t.Fatal("Unmatched storage slot key")
    62  	}
    63  }
    64  
    65  // goos: darwin
    66  // goarch: amd64
    67  // pkg: github.com/ethereum/go-ethereum/trie/utils
    68  // cpu: VirtualApple @ 2.50GHz
    69  // BenchmarkTreeKey
    70  // BenchmarkTreeKey-8   	  398731	      2961 ns/op	      32 B/op	       1 allocs/op
    71  func BenchmarkTreeKey(b *testing.B) {
    72  	// Initialize the IPA settings which can be pretty expensive.
    73  	verkle.GetConfig()
    74  
    75  	b.ReportAllocs()
    76  	b.ResetTimer()
    77  
    78  	for i := 0; i < b.N; i++ {
    79  		BalanceKey([]byte{0x01})
    80  	}
    81  }
    82  
    83  // goos: darwin
    84  // goarch: amd64
    85  // pkg: github.com/ethereum/go-ethereum/trie/utils
    86  // cpu: VirtualApple @ 2.50GHz
    87  // BenchmarkTreeKeyWithEvaluation
    88  // BenchmarkTreeKeyWithEvaluation-8   	  513855	      2324 ns/op	      32 B/op	       1 allocs/op
    89  func BenchmarkTreeKeyWithEvaluation(b *testing.B) {
    90  	// Initialize the IPA settings which can be pretty expensive.
    91  	verkle.GetConfig()
    92  
    93  	addr := []byte{0x01}
    94  	eval := evaluateAddressPoint(addr)
    95  
    96  	b.ReportAllocs()
    97  	b.ResetTimer()
    98  	for i := 0; i < b.N; i++ {
    99  		BalanceKeyWithEvaluatedAddress(eval)
   100  	}
   101  }
   102  
   103  // goos: darwin
   104  // goarch: amd64
   105  // pkg: github.com/ethereum/go-ethereum/trie/utils
   106  // cpu: VirtualApple @ 2.50GHz
   107  // BenchmarkStorageKey
   108  // BenchmarkStorageKey-8   	  230516	      4584 ns/op	      96 B/op	       3 allocs/op
   109  func BenchmarkStorageKey(b *testing.B) {
   110  	// Initialize the IPA settings which can be pretty expensive.
   111  	verkle.GetConfig()
   112  
   113  	b.ReportAllocs()
   114  	b.ResetTimer()
   115  
   116  	for i := 0; i < b.N; i++ {
   117  		StorageSlotKey([]byte{0x01}, bytes.Repeat([]byte{0xff}, 32))
   118  	}
   119  }
   120  
   121  // goos: darwin
   122  // goarch: amd64
   123  // pkg: github.com/ethereum/go-ethereum/trie/utils
   124  // cpu: VirtualApple @ 2.50GHz
   125  // BenchmarkStorageKeyWithEvaluation
   126  // BenchmarkStorageKeyWithEvaluation-8   	  320125	      3753 ns/op	      96 B/op	       3 allocs/op
   127  func BenchmarkStorageKeyWithEvaluation(b *testing.B) {
   128  	// Initialize the IPA settings which can be pretty expensive.
   129  	verkle.GetConfig()
   130  
   131  	addr := []byte{0x01}
   132  	eval := evaluateAddressPoint(addr)
   133  
   134  	b.ReportAllocs()
   135  	b.ResetTimer()
   136  	for i := 0; i < b.N; i++ {
   137  		StorageSlotKeyWithEvaluatedAddress(eval, bytes.Repeat([]byte{0xff}, 32))
   138  	}
   139  }