|
|
|
@ -28,8 +28,10 @@ import ( |
|
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common" |
|
|
|
|
"github.com/ethereum/go-ethereum/consensus/misc/eip1559" |
|
|
|
|
"github.com/ethereum/go-ethereum/consensus/misc/eip4844" |
|
|
|
|
"github.com/ethereum/go-ethereum/core/types" |
|
|
|
|
"github.com/ethereum/go-ethereum/log" |
|
|
|
|
"github.com/ethereum/go-ethereum/params" |
|
|
|
|
"github.com/ethereum/go-ethereum/rpc" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
@ -63,9 +65,11 @@ type cacheKey struct { |
|
|
|
|
|
|
|
|
|
// processedFees contains the results of a processed block.
|
|
|
|
|
type processedFees struct { |
|
|
|
|
reward []*big.Int |
|
|
|
|
baseFee, nextBaseFee *big.Int |
|
|
|
|
gasUsedRatio float64 |
|
|
|
|
reward []*big.Int |
|
|
|
|
baseFee, nextBaseFee *big.Int |
|
|
|
|
gasUsedRatio float64 |
|
|
|
|
blobGasUsedRatio float64 |
|
|
|
|
blobBaseFee, nextBlobBaseFee *big.Int |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// txGasAndReward is sorted in ascending order based on reward
|
|
|
|
@ -78,16 +82,31 @@ type txGasAndReward struct { |
|
|
|
|
// the block field filled in, retrieves the block from the backend if not present yet and
|
|
|
|
|
// fills in the rest of the fields.
|
|
|
|
|
func (oracle *Oracle) processBlock(bf *blockFees, percentiles []float64) { |
|
|
|
|
chainconfig := oracle.backend.ChainConfig() |
|
|
|
|
config := oracle.backend.ChainConfig() |
|
|
|
|
|
|
|
|
|
// Fill in base fee and next base fee.
|
|
|
|
|
if bf.results.baseFee = bf.header.BaseFee; bf.results.baseFee == nil { |
|
|
|
|
bf.results.baseFee = new(big.Int) |
|
|
|
|
} |
|
|
|
|
if chainconfig.IsLondon(big.NewInt(int64(bf.blockNumber + 1))) { |
|
|
|
|
bf.results.nextBaseFee = eip1559.CalcBaseFee(chainconfig, bf.header) |
|
|
|
|
if config.IsLondon(big.NewInt(int64(bf.blockNumber + 1))) { |
|
|
|
|
bf.results.nextBaseFee = eip1559.CalcBaseFee(config, bf.header) |
|
|
|
|
} else { |
|
|
|
|
bf.results.nextBaseFee = new(big.Int) |
|
|
|
|
} |
|
|
|
|
// Fill in blob base fee and next blob base fee.
|
|
|
|
|
if excessBlobGas := bf.header.ExcessBlobGas; excessBlobGas != nil { |
|
|
|
|
bf.results.blobBaseFee = eip4844.CalcBlobFee(*excessBlobGas) |
|
|
|
|
bf.results.nextBlobBaseFee = eip4844.CalcBlobFee(eip4844.CalcExcessBlobGas(*excessBlobGas, *bf.header.BlobGasUsed)) |
|
|
|
|
} else { |
|
|
|
|
bf.results.blobBaseFee = new(big.Int) |
|
|
|
|
bf.results.nextBlobBaseFee = new(big.Int) |
|
|
|
|
} |
|
|
|
|
// Compute gas used ratio for normal and blob gas.
|
|
|
|
|
bf.results.gasUsedRatio = float64(bf.header.GasUsed) / float64(bf.header.GasLimit) |
|
|
|
|
if blobGasUsed := bf.header.BlobGasUsed; blobGasUsed != nil { |
|
|
|
|
bf.results.blobGasUsedRatio = float64(*blobGasUsed) / params.MaxBlobGasPerBlock |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if len(percentiles) == 0 { |
|
|
|
|
// rewards were not requested, return null
|
|
|
|
|
return |
|
|
|
@ -203,17 +222,19 @@ func (oracle *Oracle) resolveBlockRange(ctx context.Context, reqEnd rpc.BlockNum |
|
|
|
|
// or blocks older than a certain age (specified in maxHistory). The first block of the
|
|
|
|
|
// actually processed range is returned to avoid ambiguity when parts of the requested range
|
|
|
|
|
// are not available or when the head has changed during processing this request.
|
|
|
|
|
// Three arrays are returned based on the processed blocks:
|
|
|
|
|
// Five arrays are returned based on the processed blocks:
|
|
|
|
|
// - reward: the requested percentiles of effective priority fees per gas of transactions in each
|
|
|
|
|
// block, sorted in ascending order and weighted by gas used.
|
|
|
|
|
// - baseFee: base fee per gas in the given block
|
|
|
|
|
// - gasUsedRatio: gasUsed/gasLimit in the given block
|
|
|
|
|
// - blobBaseFee: the blob base fee per gas in the given block
|
|
|
|
|
// - blobGasUsedRatio: blobGasUsed/blobGasLimit in the given block
|
|
|
|
|
//
|
|
|
|
|
// Note: baseFee includes the next block after the newest of the returned range, because this
|
|
|
|
|
// value can be derived from the newest block.
|
|
|
|
|
func (oracle *Oracle) FeeHistory(ctx context.Context, blocks uint64, unresolvedLastBlock rpc.BlockNumber, rewardPercentiles []float64) (*big.Int, [][]*big.Int, []*big.Int, []float64, error) { |
|
|
|
|
// Note: baseFee and blobBaseFee both include the next block after the newest of the returned range,
|
|
|
|
|
// because this value can be derived from the newest block.
|
|
|
|
|
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) { |
|
|
|
|
if blocks < 1 { |
|
|
|
|
return common.Big0, nil, nil, nil, nil // returning with no data and no error means there are no retrievable blocks
|
|
|
|
|
return common.Big0, nil, nil, nil, nil, nil, nil // returning with no data and no error means there are no retrievable blocks
|
|
|
|
|
} |
|
|
|
|
maxFeeHistory := oracle.maxHeaderHistory |
|
|
|
|
if len(rewardPercentiles) != 0 { |
|
|
|
@ -225,10 +246,10 @@ func (oracle *Oracle) FeeHistory(ctx context.Context, blocks uint64, unresolvedL |
|
|
|
|
} |
|
|
|
|
for i, p := range rewardPercentiles { |
|
|
|
|
if p < 0 || p > 100 { |
|
|
|
|
return common.Big0, nil, nil, nil, fmt.Errorf("%w: %f", errInvalidPercentile, p) |
|
|
|
|
return common.Big0, nil, nil, nil, nil, nil, fmt.Errorf("%w: %f", errInvalidPercentile, p) |
|
|
|
|
} |
|
|
|
|
if i > 0 && p <= rewardPercentiles[i-1] { |
|
|
|
|
return common.Big0, nil, nil, nil, fmt.Errorf("%w: #%d:%f >= #%d:%f", errInvalidPercentile, i-1, rewardPercentiles[i-1], i, p) |
|
|
|
|
return common.Big0, nil, nil, nil, nil, nil, fmt.Errorf("%w: #%d:%f >= #%d:%f", errInvalidPercentile, i-1, rewardPercentiles[i-1], i, p) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
var ( |
|
|
|
@ -238,7 +259,7 @@ func (oracle *Oracle) FeeHistory(ctx context.Context, blocks uint64, unresolvedL |
|
|
|
|
) |
|
|
|
|
pendingBlock, pendingReceipts, lastBlock, blocks, err := oracle.resolveBlockRange(ctx, unresolvedLastBlock, blocks) |
|
|
|
|
if err != nil || blocks == 0 { |
|
|
|
|
return common.Big0, nil, nil, nil, err |
|
|
|
|
return common.Big0, nil, nil, nil, nil, nil, err |
|
|
|
|
} |
|
|
|
|
oldestBlock := lastBlock + 1 - blocks |
|
|
|
|
|
|
|
|
@ -295,19 +316,22 @@ func (oracle *Oracle) FeeHistory(ctx context.Context, blocks uint64, unresolvedL |
|
|
|
|
}() |
|
|
|
|
} |
|
|
|
|
var ( |
|
|
|
|
reward = make([][]*big.Int, blocks) |
|
|
|
|
baseFee = make([]*big.Int, blocks+1) |
|
|
|
|
gasUsedRatio = make([]float64, blocks) |
|
|
|
|
firstMissing = blocks |
|
|
|
|
reward = make([][]*big.Int, blocks) |
|
|
|
|
baseFee = make([]*big.Int, blocks+1) |
|
|
|
|
gasUsedRatio = make([]float64, blocks) |
|
|
|
|
blobGasUsedRatio = make([]float64, blocks) |
|
|
|
|
blobBaseFee = make([]*big.Int, blocks+1) |
|
|
|
|
firstMissing = blocks |
|
|
|
|
) |
|
|
|
|
for ; blocks > 0; blocks-- { |
|
|
|
|
fees := <-results |
|
|
|
|
if fees.err != nil { |
|
|
|
|
return common.Big0, nil, nil, nil, fees.err |
|
|
|
|
return common.Big0, nil, nil, nil, nil, nil, fees.err |
|
|
|
|
} |
|
|
|
|
i := fees.blockNumber - oldestBlock |
|
|
|
|
if fees.results.baseFee != nil { |
|
|
|
|
reward[i], baseFee[i], baseFee[i+1], gasUsedRatio[i] = fees.results.reward, fees.results.baseFee, fees.results.nextBaseFee, fees.results.gasUsedRatio |
|
|
|
|
blobGasUsedRatio[i], blobBaseFee[i], blobBaseFee[i+1] = fees.results.blobGasUsedRatio, fees.results.blobBaseFee, fees.results.nextBlobBaseFee |
|
|
|
|
} else { |
|
|
|
|
// getting no block and no error means we are requesting into the future (might happen because of a reorg)
|
|
|
|
|
if i < firstMissing { |
|
|
|
@ -316,7 +340,7 @@ func (oracle *Oracle) FeeHistory(ctx context.Context, blocks uint64, unresolvedL |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if firstMissing == 0 { |
|
|
|
|
return common.Big0, nil, nil, nil, nil |
|
|
|
|
return common.Big0, nil, nil, nil, nil, nil, nil |
|
|
|
|
} |
|
|
|
|
if len(rewardPercentiles) != 0 { |
|
|
|
|
reward = reward[:firstMissing] |
|
|
|
@ -324,5 +348,6 @@ func (oracle *Oracle) FeeHistory(ctx context.Context, blocks uint64, unresolvedL |
|
|
|
|
reward = nil |
|
|
|
|
} |
|
|
|
|
baseFee, gasUsedRatio = baseFee[:firstMissing+1], gasUsedRatio[:firstMissing] |
|
|
|
|
return new(big.Int).SetUint64(oldestBlock), reward, baseFee, gasUsedRatio, nil |
|
|
|
|
blobBaseFee, blobGasUsedRatio = blobBaseFee[:firstMissing+1], blobGasUsedRatio[:firstMissing] |
|
|
|
|
return new(big.Int).SetUint64(oldestBlock), reward, baseFee, gasUsedRatio, blobBaseFee, blobGasUsedRatio, nil |
|
|
|
|
} |
|
|
|
|