@@ -28,8 +28,10 @@ import (
28
28
29
29
"github.com/ethereum/go-ethereum/common"
30
30
"github.com/ethereum/go-ethereum/consensus/misc/eip1559"
31
+ "github.com/ethereum/go-ethereum/consensus/misc/eip4844"
31
32
"github.com/ethereum/go-ethereum/core/types"
32
33
"github.com/ethereum/go-ethereum/log"
34
+ "github.com/ethereum/go-ethereum/params"
33
35
"github.com/ethereum/go-ethereum/rpc"
34
36
)
35
37
@@ -63,9 +65,11 @@ type cacheKey struct {
63
65
64
66
// processedFees contains the results of a processed block.
65
67
type processedFees struct {
66
- reward []* big.Int
67
- baseFee , nextBaseFee * big.Int
68
- gasUsedRatio float64
68
+ reward []* big.Int
69
+ baseFee , nextBaseFee * big.Int
70
+ gasUsedRatio float64
71
+ blobGasUsedRatio float64
72
+ blobBaseFee , nextBlobBaseFee * big.Int
69
73
}
70
74
71
75
// txGasAndReward is sorted in ascending order based on reward
@@ -78,16 +82,31 @@ type txGasAndReward struct {
78
82
// the block field filled in, retrieves the block from the backend if not present yet and
79
83
// fills in the rest of the fields.
80
84
func (oracle * Oracle ) processBlock (bf * blockFees , percentiles []float64 ) {
81
- chainconfig := oracle .backend .ChainConfig ()
85
+ config := oracle .backend .ChainConfig ()
86
+
87
+ // Fill in base fee and next base fee.
82
88
if bf .results .baseFee = bf .header .BaseFee ; bf .results .baseFee == nil {
83
89
bf .results .baseFee = new (big.Int )
84
90
}
85
- if chainconfig .IsLondon (big .NewInt (int64 (bf .blockNumber + 1 ))) {
86
- bf .results .nextBaseFee = eip1559 .CalcBaseFee (chainconfig , bf .header )
91
+ if config .IsLondon (big .NewInt (int64 (bf .blockNumber + 1 ))) {
92
+ bf .results .nextBaseFee = eip1559 .CalcBaseFee (config , bf .header )
87
93
} else {
88
94
bf .results .nextBaseFee = new (big.Int )
89
95
}
96
+ // Fill in blob base fee and next blob base fee.
97
+ if excessBlobGas := bf .header .ExcessBlobGas ; excessBlobGas != nil {
98
+ bf .results .blobBaseFee = eip4844 .CalcBlobFee (* excessBlobGas )
99
+ bf .results .nextBlobBaseFee = eip4844 .CalcBlobFee (eip4844 .CalcExcessBlobGas (* excessBlobGas , * bf .header .BlobGasUsed ))
100
+ } else {
101
+ bf .results .blobBaseFee = new (big.Int )
102
+ bf .results .nextBlobBaseFee = new (big.Int )
103
+ }
104
+ // Compute gas used ratio for normal and blob gas.
90
105
bf .results .gasUsedRatio = float64 (bf .header .GasUsed ) / float64 (bf .header .GasLimit )
106
+ if blobGasUsed := bf .header .BlobGasUsed ; blobGasUsed != nil {
107
+ bf .results .blobGasUsedRatio = float64 (* blobGasUsed ) / params .MaxBlobGasPerBlock
108
+ }
109
+
91
110
if len (percentiles ) == 0 {
92
111
// rewards were not requested, return null
93
112
return
@@ -203,17 +222,19 @@ func (oracle *Oracle) resolveBlockRange(ctx context.Context, reqEnd rpc.BlockNum
203
222
// or blocks older than a certain age (specified in maxHistory). The first block of the
204
223
// actually processed range is returned to avoid ambiguity when parts of the requested range
205
224
// are not available or when the head has changed during processing this request.
206
- // Three arrays are returned based on the processed blocks:
225
+ // Five arrays are returned based on the processed blocks:
207
226
// - reward: the requested percentiles of effective priority fees per gas of transactions in each
208
227
// block, sorted in ascending order and weighted by gas used.
209
228
// - baseFee: base fee per gas in the given block
210
229
// - gasUsedRatio: gasUsed/gasLimit in the given block
230
+ // - blobBaseFee: the blob base fee per gas in the given block
231
+ // - blobGasUsedRatio: blobGasUsed/blobGasLimit in the given block
211
232
//
212
- // Note: baseFee includes the next block after the newest of the returned range, because this
213
- // value can be derived from the newest block.
214
- func (oracle * Oracle ) FeeHistory (ctx context.Context , blocks uint64 , unresolvedLastBlock rpc.BlockNumber , rewardPercentiles []float64 ) (* big.Int , [][]* big.Int , []* big.Int , []float64 , error ) {
233
+ // Note: baseFee and blobBaseFee both include the next block after the newest of the returned range,
234
+ // because this value can be derived from the newest block.
235
+ func (oracle * Oracle ) FeeHistory (ctx context.Context , blocks uint64 , unresolvedLastBlock rpc.BlockNumber , rewardPercentiles []float64 ) (* big.Int , [][]* big.Int , []* big.Int , []float64 , [] * big. Int , [] float64 , error ) {
215
236
if blocks < 1 {
216
- return common .Big0 , nil , nil , nil , nil // returning with no data and no error means there are no retrievable blocks
237
+ return common .Big0 , nil , nil , nil , nil , nil , nil // returning with no data and no error means there are no retrievable blocks
217
238
}
218
239
maxFeeHistory := oracle .maxHeaderHistory
219
240
if len (rewardPercentiles ) != 0 {
@@ -225,10 +246,10 @@ func (oracle *Oracle) FeeHistory(ctx context.Context, blocks uint64, unresolvedL
225
246
}
226
247
for i , p := range rewardPercentiles {
227
248
if p < 0 || p > 100 {
228
- return common .Big0 , nil , nil , nil , fmt .Errorf ("%w: %f" , errInvalidPercentile , p )
249
+ return common .Big0 , nil , nil , nil , nil , nil , fmt .Errorf ("%w: %f" , errInvalidPercentile , p )
229
250
}
230
251
if i > 0 && p <= rewardPercentiles [i - 1 ] {
231
- return common .Big0 , nil , nil , nil , fmt .Errorf ("%w: #%d:%f >= #%d:%f" , errInvalidPercentile , i - 1 , rewardPercentiles [i - 1 ], i , p )
252
+ return common .Big0 , nil , nil , nil , nil , nil , fmt .Errorf ("%w: #%d:%f >= #%d:%f" , errInvalidPercentile , i - 1 , rewardPercentiles [i - 1 ], i , p )
232
253
}
233
254
}
234
255
var (
@@ -238,7 +259,7 @@ func (oracle *Oracle) FeeHistory(ctx context.Context, blocks uint64, unresolvedL
238
259
)
239
260
pendingBlock , pendingReceipts , lastBlock , blocks , err := oracle .resolveBlockRange (ctx , unresolvedLastBlock , blocks )
240
261
if err != nil || blocks == 0 {
241
- return common .Big0 , nil , nil , nil , err
262
+ return common .Big0 , nil , nil , nil , nil , nil , err
242
263
}
243
264
oldestBlock := lastBlock + 1 - blocks
244
265
@@ -295,19 +316,22 @@ func (oracle *Oracle) FeeHistory(ctx context.Context, blocks uint64, unresolvedL
295
316
}()
296
317
}
297
318
var (
298
- reward = make ([][]* big.Int , blocks )
299
- baseFee = make ([]* big.Int , blocks + 1 )
300
- gasUsedRatio = make ([]float64 , blocks )
301
- firstMissing = blocks
319
+ reward = make ([][]* big.Int , blocks )
320
+ baseFee = make ([]* big.Int , blocks + 1 )
321
+ gasUsedRatio = make ([]float64 , blocks )
322
+ blobGasUsedRatio = make ([]float64 , blocks )
323
+ blobBaseFee = make ([]* big.Int , blocks + 1 )
324
+ firstMissing = blocks
302
325
)
303
326
for ; blocks > 0 ; blocks -- {
304
327
fees := <- results
305
328
if fees .err != nil {
306
- return common .Big0 , nil , nil , nil , fees .err
329
+ return common .Big0 , nil , nil , nil , nil , nil , fees .err
307
330
}
308
331
i := fees .blockNumber - oldestBlock
309
332
if fees .results .baseFee != nil {
310
333
reward [i ], baseFee [i ], baseFee [i + 1 ], gasUsedRatio [i ] = fees .results .reward , fees .results .baseFee , fees .results .nextBaseFee , fees .results .gasUsedRatio
334
+ blobGasUsedRatio [i ], blobBaseFee [i ], blobBaseFee [i + 1 ] = fees .results .blobGasUsedRatio , fees .results .blobBaseFee , fees .results .nextBlobBaseFee
311
335
} else {
312
336
// getting no block and no error means we are requesting into the future (might happen because of a reorg)
313
337
if i < firstMissing {
@@ -316,13 +340,14 @@ func (oracle *Oracle) FeeHistory(ctx context.Context, blocks uint64, unresolvedL
316
340
}
317
341
}
318
342
if firstMissing == 0 {
319
- return common .Big0 , nil , nil , nil , nil
343
+ return common .Big0 , nil , nil , nil , nil , nil , nil
320
344
}
321
345
if len (rewardPercentiles ) != 0 {
322
346
reward = reward [:firstMissing ]
323
347
} else {
324
348
reward = nil
325
349
}
326
350
baseFee , gasUsedRatio = baseFee [:firstMissing + 1 ], gasUsedRatio [:firstMissing ]
327
- return new (big.Int ).SetUint64 (oldestBlock ), reward , baseFee , gasUsedRatio , nil
351
+ blobBaseFee , blobGasUsedRatio = blobBaseFee [:firstMissing + 1 ], blobGasUsedRatio [:firstMissing ]
352
+ return new (big.Int ).SetUint64 (oldestBlock ), reward , baseFee , gasUsedRatio , blobBaseFee , blobGasUsedRatio , nil
328
353
}
0 commit comments