From edb398a10255878b18f4325d7d37afc760deca1a Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Tue, 9 Jun 2026 20:48:06 +0000 Subject: [PATCH] triedb/pathdb: extract cached block reader helper Deduplicate the load-and-cache logic in newBlockIter into a blockReader method on indexReader, for reuse by upcoming ordinal accessors. --- triedb/pathdb/history_index.go | 14 ++++++++++++++ triedb/pathdb/history_index_iterator.go | 11 +++-------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/triedb/pathdb/history_index.go b/triedb/pathdb/history_index.go index eee4c10273..d446480aaa 100644 --- a/triedb/pathdb/history_index.go +++ b/triedb/pathdb/history_index.go @@ -124,6 +124,20 @@ func (r *indexReader) refresh() error { return nil } +// blockReader returns a cached block reader for the given block id, loading it +// from disk on first access. +func (r *indexReader) blockReader(id uint32) (*blockReader, error) { + if br, ok := r.readers[id]; ok { + return br, nil + } + br, err := newBlockReader(readStateIndexBlock(r.state, r.db, id), r.bitmapSize != 0) + if err != nil { + return nil, err + } + r.readers[id] = br + return br, nil +} + // readGreaterThan locates the first element that is greater than the specified // id. If no such element is found, MaxUint64 is returned. func (r *indexReader) readGreaterThan(id uint64) (uint64, error) { diff --git a/triedb/pathdb/history_index_iterator.go b/triedb/pathdb/history_index_iterator.go index e4aca24f5d..2e570ea152 100644 --- a/triedb/pathdb/history_index_iterator.go +++ b/triedb/pathdb/history_index_iterator.go @@ -446,14 +446,9 @@ type indexIterator struct { // newBlockIter initializes the block iterator with the specified block ID. func (r *indexReader) newBlockIter(id uint32, filter *extFilter) (*blockIterator, error) { - br, ok := r.readers[id] - if !ok { - var err error - br, err = newBlockReader(readStateIndexBlock(r.state, r.db, id), r.bitmapSize != 0) - if err != nil { - return nil, err - } - r.readers[id] = br + br, err := r.blockReader(id) + if err != nil { + return nil, err } return br.newIterator(filter), nil }