Skip to content

Commit

Permalink
Dynamic fee types cleanup (#14602)
Browse files Browse the repository at this point in the history
* Major dynamic fee types cleanup

* Add changeset

* Fix lint

* Fix lint

* Fix lint one more time

* Disable G115 misfire

* Linter things

* Update comment

* Fix merge conflict

* Update Int64ToHex

---------

Co-authored-by: Patrick Dowell <[email protected]>
  • Loading branch information
dimriou and patrick-dowell authored Oct 9, 2024
1 parent 3738ee4 commit 002296d
Show file tree
Hide file tree
Showing 44 changed files with 535 additions and 801 deletions.
5 changes: 5 additions & 0 deletions .changeset/curvy-eyes-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"chainlink": minor
---

Update dynamic fee types to align with geth #internal
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ func makeTestEvmTxm(
keyStore keystore.Eth) (txmgr.TxManager, gas.EvmFeeEstimator) {
config, dbConfig, evmConfig := MakeTestConfigs(t)

estimator, err := gas.NewEstimator(logger.TestLogger(t), ethClient, config, evmConfig.GasEstimator())
estimator, err := gas.NewEstimator(logger.TestLogger(t), ethClient, config.ChainType(), evmConfig.GasEstimator())
require.NoError(t, err, "failed to create gas estimator")

lggr := logger.TestLogger(t)
Expand Down
4 changes: 2 additions & 2 deletions core/chains/evm/gas/arbitrum_estimator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ func TestArbitrumEstimator(t *testing.T) {

o := gas.NewArbitrumEstimator(logger.Test(t), &arbConfig{}, feeEstimatorClient, l1Oracle)
fee := gas.DynamicFee{
FeeCap: assets.NewWeiI(42),
TipCap: assets.NewWeiI(5),
GasFeeCap: assets.NewWeiI(42),
GasTipCap: assets.NewWeiI(5),
}
_, err = o.BumpDynamicFee(tests.Context(t), fee, maxGasPrice, nil)
assert.EqualError(t, err, "dynamic fees are not implemented for this estimator")
Expand Down
54 changes: 39 additions & 15 deletions core/chains/evm/gas/block_history_estimator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/rpc"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
Expand All @@ -23,6 +24,7 @@ import (
commonfee "github.com/smartcontractkit/chainlink/v2/common/fee"
feetypes "github.com/smartcontractkit/chainlink/v2/common/fee/types"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets"
evmconfig "github.com/smartcontractkit/chainlink/v2/core/chains/evm/config"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/config/chaintype"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/gas/rollups"
evmtypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types"
Expand Down Expand Up @@ -76,10 +78,6 @@ const BumpingHaltedLabel = "Tx gas bumping halted since price exceeds current bl

var _ EvmEstimator = &BlockHistoryEstimator{}

type chainConfig interface {
ChainType() chaintype.ChainType
}

type estimatorGasEstimatorConfig interface {
EIP1559DynamicFees() bool
BumpThreshold() uint64
Expand All @@ -95,9 +93,9 @@ type BlockHistoryEstimator struct {
services.StateMachine
ethClient feeEstimatorClient
chainID *big.Int
config chainConfig
chaintype chaintype.ChainType
eConfig estimatorGasEstimatorConfig
bhConfig BlockHistoryConfig
bhConfig evmconfig.BlockHistory
// NOTE: it is assumed that blocks will be kept sorted by
// block number ascending
blocks []evmtypes.Block
Expand Down Expand Up @@ -125,11 +123,11 @@ type BlockHistoryEstimator struct {
// NewBlockHistoryEstimator returns a new BlockHistoryEstimator that listens
// for new heads and updates the base gas price dynamically based on the
// configured percentile of gas prices in that block
func NewBlockHistoryEstimator(lggr logger.Logger, ethClient feeEstimatorClient, cfg chainConfig, eCfg estimatorGasEstimatorConfig, bhCfg BlockHistoryConfig, chainID *big.Int, l1Oracle rollups.L1Oracle) EvmEstimator {
func NewBlockHistoryEstimator(lggr logger.Logger, ethClient feeEstimatorClient, chaintype chaintype.ChainType, eCfg estimatorGasEstimatorConfig, bhCfg evmconfig.BlockHistory, chainID *big.Int, l1Oracle rollups.L1Oracle) EvmEstimator {
return &BlockHistoryEstimator{
ethClient: ethClient,
chainID: chainID,
config: cfg,
chaintype: chaintype,
eConfig: eCfg,
bhConfig: bhCfg,
blocks: make([]evmtypes.Block, 0),
Expand Down Expand Up @@ -376,15 +374,15 @@ func (b *BlockHistoryEstimator) haltBumping(attempts []EvmPriorAttempt) error {
// feecap must >= tipcap+basefee for the block, otherwise there
// is no way this could have been included, and we must bail
// out of the check
attemptFeeCap := attempt.DynamicFee.FeeCap
attemptTipCap := attempt.DynamicFee.TipCap
attemptFeeCap := attempt.DynamicFee.GasFeeCap
attemptTipCap := attempt.DynamicFee.GasTipCap
if attemptFeeCap.Cmp(attemptTipCap.Add(b.BaseFeePerGas)) < 0 {
sufficientFeeCap = false
break
}
}
if sufficientFeeCap && attempt.DynamicFee.TipCap.Cmp(maxTipCap) > 0 {
return fmt.Errorf("transaction %s has tip cap of %s, which is above percentile=%d%% (percentile tip cap: %s): %w", attempt.TxHash, attempt.DynamicFee.TipCap, percentile, maxTipCap, commonfee.ErrConnectivity)
if sufficientFeeCap && attempt.DynamicFee.GasTipCap.Cmp(maxTipCap) > 0 {
return fmt.Errorf("transaction %s has tip cap of %s, which is above percentile=%d%% (percentile tip cap: %s): %w", attempt.TxHash, attempt.DynamicFee.GasTipCap, percentile, maxTipCap, commonfee.ErrConnectivity)
}
}
return nil
Expand Down Expand Up @@ -435,8 +433,8 @@ func (b *BlockHistoryEstimator) GetDynamicFee(_ context.Context, maxGasPriceWei
if err != nil {
return fee, err
}
fee.FeeCap = feeCap
fee.TipCap = tipCap
fee.GasFeeCap = feeCap
fee.GasTipCap = tipCap
return
}

Expand Down Expand Up @@ -828,7 +826,7 @@ func (b *BlockHistoryEstimator) getPricesFromBlocks(blocks []evmtypes.Block, eip
b.logger.Warnw(fmt.Sprintf("Block %v is not usable, %s", block.Number, err.Error()), "block", block, "err", err)
}
for _, tx := range block.Transactions {
if b.IsUsable(tx, block, b.config.ChainType(), b.eConfig.PriceMin(), b.logger) {
if b.IsUsable(tx, block, b.chaintype, b.eConfig.PriceMin(), b.logger) {
gp := b.EffectiveGasPrice(block, tx)
if gp == nil {
b.logger.Warnw("Unable to get gas price for tx", "tx", tx, "block", block)
Expand Down Expand Up @@ -977,3 +975,29 @@ func (b *BlockHistoryEstimator) EffectiveTipCap(block evmtypes.Block, tx evmtype
return nil
}
}

// Int64ToHex formats an int64 as a hex string with 0x prefix.
func Int64ToHex(n int64) string {
return fmt.Sprintf("0x%x", n)
}

// HexToInt64 performs the inverse of Int64ToHex
// Returns 0 on invalid input
func HexToInt64(input interface{}) int64 {
switch v := input.(type) {
case string:
big, err := hexutil.DecodeBig(v)
if err != nil {
return 0
}
return big.Int64()
case []byte:
big, err := hexutil.DecodeBig(string(v))
if err != nil {
return 0
}
return big.Int64()
default:
return 0
}
}
Loading

0 comments on commit 002296d

Please sign in to comment.