Skip to content

Commit 994fcbf

Browse files
committed
Logging and gas calculation improvements
1 parent 1ddf848 commit 994fcbf

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

api/api.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,11 @@ func (b *BlockChainAPI) FeeHistory(
832832
blockRewards := make([]*hexutil.Big, len(rewardPercentiles))
833833
feeParams, err := b.feeParameters.Get()
834834
if err != nil {
835+
b.logger.Warn().
836+
Uint64("height", blockHeight).
837+
Err(err).
838+
Msg("failed to get fee parameters for block in fee history")
839+
835840
continue
836841
}
837842
gasPrice := feeParams.CalculateGasPrice(b.config.GasPrice)
@@ -1021,9 +1026,9 @@ func (b *BlockChainAPI) Coinbase(ctx context.Context) (common.Address, error) {
10211026
func (b *BlockChainAPI) GasPrice(ctx context.Context) (*hexutil.Big, error) {
10221027
feeParams, err := b.feeParameters.Get()
10231028
if err != nil {
1024-
return nil, err
1029+
b.logger.Warn().Err(err).Msg("fee parameters unavailable; falling back to base gas price")
1030+
return (*hexutil.Big)(b.config.GasPrice), nil
10251031
}
1026-
10271032
gasPrice := feeParams.CalculateGasPrice(b.config.GasPrice)
10281033
return (*hexutil.Big)(gasPrice), nil
10291034
}
@@ -1068,9 +1073,9 @@ func (b *BlockChainAPI) GetUncleByBlockNumberAndIndex(
10681073
func (b *BlockChainAPI) MaxPriorityFeePerGas(ctx context.Context) (*hexutil.Big, error) {
10691074
feeParams, err := b.feeParameters.Get()
10701075
if err != nil {
1071-
return nil, err
1076+
b.logger.Warn().Err(err).Msg("fee parameters unavailable; falling back to base gas price")
1077+
return (*hexutil.Big)(b.config.GasPrice), nil
10721078
}
1073-
10741079
gasPrice := feeParams.CalculateGasPrice(b.config.GasPrice)
10751080
return (*hexutil.Big)(gasPrice), nil
10761081
}

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)