core: opt storageHistoryIndexBlockKey

This commit is contained in:
cuiweixie 2025-08-14 09:45:11 +08:00
parent 3ff99ae52c
commit c0f04bd2d9
2 changed files with 35 additions and 3 deletions

View file

@ -396,9 +396,19 @@ func accountHistoryIndexBlockKey(addressHash common.Hash, blockID uint32) []byte
// storageHistoryIndexBlockKey = StateHistoryStorageBlockPrefix + addressHash + storageHash + blockID
func storageHistoryIndexBlockKey(addressHash common.Hash, storageHash common.Hash, blockID uint32) []byte {
var buf [4]byte
binary.BigEndian.PutUint32(buf[:], blockID)
return append(append(append(StateHistoryStorageBlockPrefix, addressHash.Bytes()...), storageHash.Bytes()...), buf[:]...)
var buf4 [4]byte
binary.BigEndian.PutUint32(buf4[:], blockID)
totalLen := len(StateHistoryStorageBlockPrefix) + len(addressHash) + len(storageHash) + 4
out := make([]byte, totalLen)
off := 0
off += copy(out[off:], StateHistoryStorageBlockPrefix)
off += copy(out[off:], addressHash.Bytes())
off += copy(out[off:], storageHash.Bytes())
copy(out[off:], buf4[:])
return out
}
// transitionStateKey = transitionStatusKey + hash

22
core/rawdb/schema_test.go Normal file
View file

@ -0,0 +1,22 @@
package rawdb
import (
"math/rand"
"testing"
"github.com/ethereum/go-ethereum/common"
)
var sink []byte
func BenchmarkStorageHistoryIndexBlockKey(b *testing.B) {
var h1, h2 common.Hash
for i := range h1 {
h1[i] = byte(rand.Intn(256))
h2[i] = byte(rand.Intn(256))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
sink = storageHistoryIndexBlockKey(h1, h2, uint32(i))
}
}