Skip to content

Commit

Permalink
eth/gasprice: add generic LRU implementation (ethereum#26162)
Browse files Browse the repository at this point in the history
  • Loading branch information
gzliudan committed Nov 13, 2024
1 parent 1791854 commit f9e4318
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
14 changes: 8 additions & 6 deletions eth/gasprice/feehistory.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ type blockFees struct {
err error
}

// processedFees contains the results of a processed block and is also used for caching
type cacheKey struct {
number uint64
percentiles string
}

// processedFees contains the results of a processed block.
type processedFees struct {
reward []*big.Int
baseFee, nextBaseFee *big.Int
Expand Down Expand Up @@ -268,13 +273,10 @@ func (oracle *Oracle) FeeHistory(ctx context.Context, blocks uint64, unresolvedL
oracle.processBlock(fees, rewardPercentiles)
results <- fees
} else {
cacheKey := struct {
number uint64
percentiles string
}{blockNumber, string(percentileKey)}
cacheKey := cacheKey{number: blockNumber, percentiles: string(percentileKey)}

if p, ok := oracle.historyCache.Get(cacheKey); ok {
fees.results = p.(processedFees)
fees.results = p
results <- fees
} else {
if len(rewardPercentiles) != 0 {
Expand Down
7 changes: 4 additions & 3 deletions eth/gasprice/gasprice.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/XinFinOrg/XDPoSChain/log"
"github.com/XinFinOrg/XDPoSChain/params"
"github.com/XinFinOrg/XDPoSChain/rpc"
lru "github.com/hashicorp/golang-lru"
"github.com/XinFinOrg/XDPoSChain/common/lru"
)

const sampleNumber = 3 // Number of transactions sampled in a block
Expand Down Expand Up @@ -72,7 +72,8 @@ type Oracle struct {

checkBlocks, percentile int
maxHeaderHistory, maxBlockHistory uint64
historyCache *lru.Cache

historyCache *lru.Cache[cacheKey, processedFees]
}

// NewOracle returns a new gasprice oracle which can recommend suitable
Expand Down Expand Up @@ -114,7 +115,7 @@ func NewOracle(backend OracleBackend, params Config) *Oracle {
log.Warn("Sanitizing invalid gasprice oracle max block history", "provided", params.MaxBlockHistory, "updated", maxBlockHistory)
}

cache, _ := lru.New(2048)
cache := lru.NewCache[cacheKey, processedFees](2048)
headEvent := make(chan core.ChainHeadEvent, 1)
backend.SubscribeChainHeadEvent(headEvent)
go func() {
Expand Down

0 comments on commit f9e4318

Please sign in to comment.