Skip to content

Commit c0c3499

Browse files
committed
address comment
1 parent 95f2381 commit c0c3499

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

eth/gasprice/feehistory.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (s sortGasAndReward) Less(i, j int) bool {
8484
// processBlock takes a blockFees structure with the blockNumber, the header and optionally
8585
// the block field filled in, retrieves the block from the backend if not present yet and
8686
// fills in the rest of the fields.
87-
func (oracle *Oracle) processBlock(bf *blockFees, percentiles []float64, headHeader *types.Header) {
87+
func (oracle *Oracle) processBlock(bf *blockFees, percentiles []float64, nonCongestedPrice *big.Int) {
8888
chainconfig := oracle.backend.ChainConfig()
8989
if bf.results.baseFee = bf.header.BaseFee; bf.results.baseFee == nil {
9090
bf.results.baseFee = new(big.Int)
@@ -122,15 +122,8 @@ func (oracle *Oracle) processBlock(bf *blockFees, percentiles []float64, headHea
122122
sorter := make(sortGasAndReward, len(bf.block.Transactions()))
123123
for i, tx := range bf.block.Transactions() {
124124
var reward *big.Int
125-
pendingTxCount, _ := oracle.backend.StatsWithMinBaseFee(headHeader.BaseFee)
126-
if pendingTxCount < oracle.congestedThreshold {
127-
// Before Curie (EIP-1559), we need to return the total suggested gas price. After Curie we return defaultGasTipCap wei as the tip cap,
128-
// as the base fee is set separately or added manually for legacy transactions.
129-
price := oracle.defaultGasTipCap
130-
if !oracle.backend.ChainConfig().IsCurie(headHeader.Number) {
131-
price = oracle.defaultBasePrice
132-
}
133-
reward = price
125+
if nonCongestedPrice != nil {
126+
reward = nonCongestedPrice
134127
} else {
135128
reward, _ = tx.EffectiveGasTip(bf.block.BaseFee())
136129
}
@@ -251,6 +244,20 @@ func (oracle *Oracle) FeeHistory(ctx context.Context, blocks int, unresolvedLast
251244
}
252245
oldestBlock := lastBlock + 1 - uint64(blocks)
253246

247+
// If pending txs are less than oracle.congestedThreshold, we consider the network to be non-congested and suggest
248+
// a minimal tip cap. This is to prevent users from overpaying for gas when the network is not congested and a few
249+
// high-priced txs are causing the suggested tip cap to be high.
250+
var nonCongestedPrice *big.Int
251+
pendingTxCount, _ := oracle.backend.StatsWithMinBaseFee(headHeader.BaseFee)
252+
if pendingTxCount < oracle.congestedThreshold {
253+
// Before Curie (EIP-1559), we need to return the total suggested gas price. After Curie we return defaultGasTipCap wei as the tip cap,
254+
// as the base fee is set separately or added manually for legacy transactions.
255+
nonCongestedPrice = oracle.defaultGasTipCap
256+
if !oracle.backend.ChainConfig().IsCurie(headHeader.Number) {
257+
nonCongestedPrice = oracle.defaultBasePrice
258+
}
259+
}
260+
254261
var (
255262
next = oldestBlock
256263
results = make(chan *blockFees, blocks)
@@ -259,6 +266,7 @@ func (oracle *Oracle) FeeHistory(ctx context.Context, blocks int, unresolvedLast
259266
for i, p := range rewardPercentiles {
260267
binary.LittleEndian.PutUint64(percentileKey[i*8:(i+1)*8], math.Float64bits(p))
261268
}
269+
262270
for i := 0; i < maxBlockFetchers && i < blocks; i++ {
263271
go func() {
264272
for {
@@ -272,7 +280,7 @@ func (oracle *Oracle) FeeHistory(ctx context.Context, blocks int, unresolvedLast
272280
if pendingBlock != nil && blockNumber >= pendingBlock.NumberU64() {
273281
fees.block, fees.receipts = pendingBlock, pendingReceipts
274282
fees.header = fees.block.Header()
275-
oracle.processBlock(fees, rewardPercentiles, headHeader)
283+
oracle.processBlock(fees, rewardPercentiles, nonCongestedPrice)
276284
results <- fees
277285
} else {
278286
cacheKey := struct {
@@ -294,7 +302,7 @@ func (oracle *Oracle) FeeHistory(ctx context.Context, blocks int, unresolvedLast
294302
fees.header, fees.err = oracle.backend.HeaderByNumber(ctx, rpc.BlockNumber(blockNumber))
295303
}
296304
if fees.header != nil && fees.err == nil {
297-
oracle.processBlock(fees, rewardPercentiles, headHeader)
305+
oracle.processBlock(fees, rewardPercentiles, nonCongestedPrice)
298306
if fees.err == nil {
299307
oracle.historyCache.Add(cacheKey, fees.results)
300308
}

0 commit comments

Comments
 (0)