pathdb: use hash(address,slot) as storage key

Signed-off-by: Delweng <delweng@gmail.com>
This commit is contained in:
Delweng 2025-07-24 23:46:06 +08:00 committed by jsvisa
parent da3abe3fe7
commit 1c693226b8
2 changed files with 21 additions and 13 deletions

View file

@ -82,7 +82,7 @@ type indexReader struct {
// loadIndexData loads the index data associated with the specified state. // loadIndexData loads the index data associated with the specified state.
func loadIndexData(db ethdb.KeyValueReader, state stateIdent, timings *readTimings, cacher *historyCacher) ([]*indexBlockDesc, error) { func loadIndexData(db ethdb.KeyValueReader, state stateIdent, timings *readTimings, cacher *historyCacher) ([]*indexBlockDesc, error) {
start := time.Now() start := time.Now()
key := state.String() key := state.CacheKey()
var blob []byte var blob []byte
if cacher != nil && cacher.index.Contains(key) { if cacher != nil && cacher.index.Contains(key) {
blob, _ = cacher.index.Get(key) blob, _ = cacher.index.Get(key)
@ -164,7 +164,7 @@ func (r *indexReader) readGreaterThan(id uint64) (uint64, error) {
blob []byte blob []byte
) )
start := time.Now() start := time.Now()
key := fmt.Sprintf("%s:%d", r.state.String(), desc.id) key := fmt.Sprintf("%s:%d", r.state.CacheKey(), desc.id)
if r.cacher != nil && r.cacher.block.Contains(key) { if r.cacher != nil && r.cacher.block.Contains(key) {
blob, _ = r.cacher.block.Get(key) blob, _ = r.cacher.block.Get(key)
} else { } else {
@ -211,8 +211,7 @@ type indexWriter struct {
// newIndexWriter constructs the index writer for the specified state. // newIndexWriter constructs the index writer for the specified state.
func newIndexWriter(db ethdb.KeyValueReader, state stateIdent, cacher *historyCacher) (*indexWriter, error) { func newIndexWriter(db ethdb.KeyValueReader, state stateIdent, cacher *historyCacher) (*indexWriter, error) {
var blob []byte var blob []byte
key := state.CacheKey()
key := state.String()
if cacher != nil && cacher.index.Contains(key) { if cacher != nil && cacher.index.Contains(key) {
blob, _ = cacher.index.Get(key) blob, _ = cacher.index.Get(key)
} else { } else {
@ -244,7 +243,7 @@ func newIndexWriter(db ethdb.KeyValueReader, state stateIdent, cacher *historyCa
indexBlock []byte indexBlock []byte
lastDesc = descList[len(descList)-1] lastDesc = descList[len(descList)-1]
) )
key = fmt.Sprintf("%s:%d", state.String(), lastDesc.id) key = fmt.Sprintf("%s:%d", state.CacheKey(), lastDesc.id)
if cacher != nil && cacher.block.Contains(key) { if cacher != nil && cacher.block.Contains(key) {
indexBlock, _ = cacher.block.Get(key) indexBlock, _ = cacher.block.Get(key)
} else { } else {
@ -327,7 +326,7 @@ func (w *indexWriter) finish(batch ethdb.Batch) {
for _, bw := range writers { for _, bw := range writers {
buf := bw.finish() buf := bw.finish()
if w.cacher != nil { if w.cacher != nil {
if key := fmt.Sprintf("%s:%d", w.state.String(), bw.desc.id); w.cacher.block.Contains(key) { if key := fmt.Sprintf("%s:%d", w.state.CacheKey(), bw.desc.id); w.cacher.block.Contains(key) {
w.cacher.block.Add(key, buf) w.cacher.block.Add(key, buf)
} }
} }
@ -344,7 +343,7 @@ func (w *indexWriter) finish(batch ethdb.Batch) {
buf = append(buf, desc.encode()...) buf = append(buf, desc.encode()...)
} }
if w.cacher != nil { if w.cacher != nil {
if key := w.state.String(); w.cacher.index.Contains(key) { if key := w.state.CacheKey(); w.cacher.index.Contains(key) {
w.cacher.index.Add(key, buf) w.cacher.index.Add(key, buf)
} }
} }
@ -369,7 +368,7 @@ type indexDeleter struct {
// newIndexDeleter constructs the index deleter for the specified state. // newIndexDeleter constructs the index deleter for the specified state.
func newIndexDeleter(db ethdb.KeyValueReader, state stateIdent, cacher *historyCacher) (*indexDeleter, error) { func newIndexDeleter(db ethdb.KeyValueReader, state stateIdent, cacher *historyCacher) (*indexDeleter, error) {
var blob []byte var blob []byte
key := state.String() key := state.CacheKey()
if cacher != nil && cacher.index.Contains(key) { if cacher != nil && cacher.index.Contains(key) {
blob, _ = cacher.index.Get(key) blob, _ = cacher.index.Get(key)
} else { } else {
@ -403,7 +402,7 @@ func newIndexDeleter(db ethdb.KeyValueReader, state stateIdent, cacher *historyC
indexBlock []byte indexBlock []byte
lastDesc = descList[len(descList)-1] lastDesc = descList[len(descList)-1]
) )
key = fmt.Sprintf("%s:%d", state.String(), lastDesc.id) key = fmt.Sprintf("%s:%d", state.CacheKey(), lastDesc.id)
if cacher != nil && cacher.block.Contains(key) { if cacher != nil && cacher.block.Contains(key) {
indexBlock, _ = cacher.block.Get(key) indexBlock, _ = cacher.block.Get(key)
} else { } else {
@ -485,7 +484,7 @@ func (d *indexDeleter) pop(id uint64) error {
func (d *indexDeleter) finish(batch ethdb.Batch) { func (d *indexDeleter) finish(batch ethdb.Batch) {
for _, id := range d.dropped { for _, id := range d.dropped {
if d.cacher != nil { if d.cacher != nil {
if key := fmt.Sprintf("%s:%d", d.state.String(), id); d.cacher.block.Contains(key) { if key := fmt.Sprintf("%s:%d", d.state.CacheKey(), id); d.cacher.block.Contains(key) {
d.cacher.block.Remove(key) d.cacher.block.Remove(key)
} }
} }
@ -501,7 +500,7 @@ func (d *indexDeleter) finish(batch ethdb.Batch) {
if !d.bw.empty() { if !d.bw.empty() {
buf := d.bw.finish() buf := d.bw.finish()
if d.cacher != nil { if d.cacher != nil {
if key := fmt.Sprintf("%s:%d", d.state.String(), d.bw.desc.id); d.cacher.block.Contains(key) { if key := fmt.Sprintf("%s:%d", d.state.CacheKey(), d.bw.desc.id); d.cacher.block.Contains(key) {
d.cacher.block.Add(key, buf) d.cacher.block.Add(key, buf)
} }
} }
@ -515,7 +514,7 @@ func (d *indexDeleter) finish(batch ethdb.Batch) {
// Flush the index metadata into the supplied batch // Flush the index metadata into the supplied batch
if d.empty() { if d.empty() {
if d.cacher != nil { if d.cacher != nil {
if key := d.state.String(); d.cacher.index.Contains(key) { if key := d.state.CacheKey(); d.cacher.index.Contains(key) {
d.cacher.index.Remove(key) d.cacher.index.Remove(key)
} }
} }
@ -531,7 +530,7 @@ func (d *indexDeleter) finish(batch ethdb.Batch) {
} }
if d.cacher != nil { if d.cacher != nil {
if key := d.state.String(); d.cacher.index.Contains(key) { if key := d.state.CacheKey(); d.cacher.index.Contains(key) {
d.cacher.index.Add(key, buf) d.cacher.index.Add(key, buf)
} }
} }

View file

@ -28,6 +28,7 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/lru" "github.com/ethereum/go-ethereum/common/lru"
"github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
) )
@ -61,6 +62,14 @@ func (ident stateIdent) String() string {
return ident.addressHash.Hex() + ident.storageHash.Hex() return ident.addressHash.Hex() + ident.storageHash.Hex()
} }
// CacheKey returns the cache key for the state identifier.
func (ident stateIdent) CacheKey() string {
if ident.account {
return ident.addressHash.Hex()
}
return crypto.Keccak256Hash(ident.addressHash.Bytes(), ident.storageHash.Bytes()).Hex()
}
// newAccountIdent constructs a state identifier for an account. // newAccountIdent constructs a state identifier for an account.
func newAccountIdent(addressHash common.Hash) stateIdent { func newAccountIdent(addressHash common.Hash) stateIdent {
return stateIdent{ return stateIdent{