Skip to content

Commit

Permalink
feat: add cache for Read/WriteTxIndexTail
Browse files Browse the repository at this point in the history
  • Loading branch information
owen-reorg committed Aug 31, 2024
1 parent 4fe68ff commit f53ba6c
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions core/rawdb/accessors_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"errors"
"fmt"
"math/big"
"sync/atomic"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus/misc/eip4844"
Expand Down Expand Up @@ -259,14 +260,27 @@ func WriteLastPivotNumber(db ethdb.KeyValueWriter, pivot uint64) {
}
}

var cache atomic.Uint64

const cacheNotSet = ^uint64(0)

func init() {
cache.Store(cacheNotSet)
}

// ReadTxIndexTail retrieves the number of oldest indexed block
// whose transaction indices has been indexed.
func ReadTxIndexTail(db ethdb.KeyValueReader) *uint64 {
tail := cache.Load()
if tail != cacheNotSet {
return &tail
}
data, _ := db.Get(txIndexTailKey)
if len(data) != 8 {
return nil
}
number := binary.BigEndian.Uint64(data)
cache.Store(number)
return &number
}

Expand All @@ -276,6 +290,7 @@ func WriteTxIndexTail(db ethdb.KeyValueWriter, number uint64) {
if err := db.Put(txIndexTailKey, encodeBlockNumber(number)); err != nil {
log.Crit("Failed to store the transaction index tail", "err", err)
}
cache.Store(number)
}

// ReadHeaderRange returns the rlp-encoded headers, starting at 'number', and going
Expand Down

0 comments on commit f53ba6c

Please sign in to comment.