Skip to content

Commit fa36f38

Browse files
committed
Logging and gas calculation improvements
1 parent 55f1972 commit fa36f38

4 files changed

Lines changed: 21 additions & 7 deletions

File tree

api/api.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,11 @@ func (b *BlockChainAPI) FeeHistory(
839839
blockRewards := make([]*hexutil.Big, len(rewardPercentiles))
840840
feeParams, err := b.feeParameters.Get()
841841
if err != nil {
842+
b.logger.Warn().
843+
Uint64("height", blockHeight).
844+
Err(err).
845+
Msg("failed to get fee parameters for block in fee history")
846+
842847
continue
843848
}
844849
gasPrice := feeParams.CalculateGasPrice(b.config.GasPrice)
@@ -1028,9 +1033,9 @@ func (b *BlockChainAPI) Coinbase(ctx context.Context) (common.Address, error) {
10281033
func (b *BlockChainAPI) GasPrice(ctx context.Context) (*hexutil.Big, error) {
10291034
feeParams, err := b.feeParameters.Get()
10301035
if err != nil {
1031-
return nil, err
1036+
b.logger.Warn().Err(err).Msg("fee parameters unavailable; falling back to base gas price")
1037+
return (*hexutil.Big)(b.config.GasPrice), nil
10321038
}
1033-
10341039
gasPrice := feeParams.CalculateGasPrice(b.config.GasPrice)
10351040
return (*hexutil.Big)(gasPrice), nil
10361041
}
@@ -1075,9 +1080,9 @@ func (b *BlockChainAPI) GetUncleByBlockNumberAndIndex(
10751080
func (b *BlockChainAPI) MaxPriorityFeePerGas(ctx context.Context) (*hexutil.Big, error) {
10761081
feeParams, err := b.feeParameters.Get()
10771082
if err != nil {
1078-
return nil, err
1083+
b.logger.Warn().Err(err).Msg("fee parameters unavailable; falling back to base gas price")
1084+
return (*hexutil.Big)(b.config.GasPrice), nil
10791085
}
1080-
10811086
gasPrice := feeParams.CalculateGasPrice(b.config.GasPrice)
10821087
return (*hexutil.Big)(gasPrice), nil
10831088
}

bootstrap/bootstrap.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,8 @@ func setupStorage(
653653
if err := feeParameters.Store(models.DefaultFeeParameters, batch); err != nil {
654654
return nil, nil, fmt.Errorf("failed to bootstrap fee parameters: %w", err)
655655
}
656+
} else if err != nil {
657+
return nil, nil, fmt.Errorf("failed to load latest fee parameters: %w", err)
656658
}
657659

658660
if batch.Count() > 0 {

models/fee_parameters.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,14 @@ func (f *FeeParameters) ToBytes() ([]byte, error) {
2929
}
3030

3131
func (f *FeeParameters) CalculateGasPrice(currentGasPrice *big.Int) *big.Int {
32-
gasPrice := new(big.Int).SetUint64(currentGasPrice.Uint64() * uint64(f.SurgeFactor))
33-
return new(big.Int).Div(gasPrice, surgeFactorScale)
32+
if currentGasPrice == nil {
33+
return new(big.Int) // zero
34+
}
35+
36+
// gasPrice = (currentGasPrice * surgeFactor) / feeParamsPrecision
37+
surgeFactor := new(big.Int).SetUint64(uint64(f.SurgeFactor))
38+
gasPrice := new(big.Int).Mul(currentGasPrice, surgeFactor)
39+
return new(big.Int).Quo(gasPrice, surgeFactorScale)
3440
}
3541

3642
func NewFeeParametersFromBytes(data []byte) (*FeeParameters, error) {

tests/web3js/eth_gas_price_surge_test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ it('should update the value of eth_MaxPriorityFeePerGas', async () => {
1717
)
1818
assert.equal(response.status, 200)
1919
assert.isDefined(response.body.result)
20-
let maxPriorityFeePerGas = utils.hexToNumber(response.body.result)
20+
// Convert hex quantity to BigInt (e.g., "0x3a98" -> 15000n)
21+
const maxPriorityFeePerGas = BigInt(response.body.result)
2122
// The surge factor was last set to 100.0
2223
assert.equal(maxPriorityFeePerGas, 100n * conf.minGasPrice)
2324
})

0 commit comments

Comments
 (0)