From 1c693226b88282591ee190401f31a4afb7057c00 Mon Sep 17 00:00:00 2001 From: Delweng Date: Thu, 24 Jul 2025 23:46:06 +0800 Subject: [PATCH] pathdb: use hash(address,slot) as storage key Signed-off-by: Delweng --- triedb/pathdb/history_index.go | 25 ++++++++++++------------- triedb/pathdb/history_reader.go | 9 +++++++++ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/triedb/pathdb/history_index.go b/triedb/pathdb/history_index.go index 7dbc19b7c4..a19694db7d 100644 --- a/triedb/pathdb/history_index.go +++ b/triedb/pathdb/history_index.go @@ -82,7 +82,7 @@ type indexReader struct { // loadIndexData loads the index data associated with the specified state. func loadIndexData(db ethdb.KeyValueReader, state stateIdent, timings *readTimings, cacher *historyCacher) ([]*indexBlockDesc, error) { start := time.Now() - key := state.String() + key := state.CacheKey() var blob []byte if cacher != nil && cacher.index.Contains(key) { blob, _ = cacher.index.Get(key) @@ -164,7 +164,7 @@ func (r *indexReader) readGreaterThan(id uint64) (uint64, error) { blob []byte ) 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) { blob, _ = r.cacher.block.Get(key) } else { @@ -211,8 +211,7 @@ type indexWriter struct { // newIndexWriter constructs the index writer for the specified state. func newIndexWriter(db ethdb.KeyValueReader, state stateIdent, cacher *historyCacher) (*indexWriter, error) { var blob []byte - - key := state.String() + key := state.CacheKey() if cacher != nil && cacher.index.Contains(key) { blob, _ = cacher.index.Get(key) } else { @@ -244,7 +243,7 @@ func newIndexWriter(db ethdb.KeyValueReader, state stateIdent, cacher *historyCa indexBlock []byte 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) { indexBlock, _ = cacher.block.Get(key) } else { @@ -327,7 +326,7 @@ func (w *indexWriter) finish(batch ethdb.Batch) { for _, bw := range writers { buf := bw.finish() 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) } } @@ -344,7 +343,7 @@ func (w *indexWriter) finish(batch ethdb.Batch) { buf = append(buf, desc.encode()...) } 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) } } @@ -369,7 +368,7 @@ type indexDeleter struct { // newIndexDeleter constructs the index deleter for the specified state. func newIndexDeleter(db ethdb.KeyValueReader, state stateIdent, cacher *historyCacher) (*indexDeleter, error) { var blob []byte - key := state.String() + key := state.CacheKey() if cacher != nil && cacher.index.Contains(key) { blob, _ = cacher.index.Get(key) } else { @@ -403,7 +402,7 @@ func newIndexDeleter(db ethdb.KeyValueReader, state stateIdent, cacher *historyC indexBlock []byte 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) { indexBlock, _ = cacher.block.Get(key) } else { @@ -485,7 +484,7 @@ func (d *indexDeleter) pop(id uint64) error { func (d *indexDeleter) finish(batch ethdb.Batch) { for _, id := range d.dropped { 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) } } @@ -501,7 +500,7 @@ func (d *indexDeleter) finish(batch ethdb.Batch) { if !d.bw.empty() { buf := d.bw.finish() 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) } } @@ -515,7 +514,7 @@ func (d *indexDeleter) finish(batch ethdb.Batch) { // Flush the index metadata into the supplied batch if d.empty() { 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) } } @@ -531,7 +530,7 @@ func (d *indexDeleter) finish(batch ethdb.Batch) { } 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) } } diff --git a/triedb/pathdb/history_reader.go b/triedb/pathdb/history_reader.go index daa98dee1b..05ed4b268a 100644 --- a/triedb/pathdb/history_reader.go +++ b/triedb/pathdb/history_reader.go @@ -28,6 +28,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/lru" "github.com/ethereum/go-ethereum/core/rawdb" + "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/log" ) @@ -61,6 +62,14 @@ func (ident stateIdent) String() string { 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. func newAccountIdent(addressHash common.Hash) stateIdent { return stateIdent{