|
| 1 | +package contract |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "math/big" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/ethereum/go-ethereum/accounts/abi/bind" |
| 9 | + "github.com/ethereum/go-ethereum/core" |
| 10 | + "github.com/ethereum/go-ethereum/core/txpool" |
| 11 | + "github.com/ethereum/go-ethereum/core/vm" |
| 12 | + |
| 13 | + "github.com/smartcontractkit/chainlink-deployments-framework/chain/evm" |
| 14 | + "github.com/smartcontractkit/chainlink-deployments-framework/operations" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + defaultInitialGasLimit = uint64(5_000_000) |
| 19 | + defaultGasLimitIncrement = uint64(500_000) |
| 20 | + defaultInitialGasPrice = uint64(20_000_000_000) |
| 21 | + defaultGasPriceIncrement = uint64(10_000_000_000) |
| 22 | +) |
| 23 | + |
| 24 | +// GasBoostConfig defines gas limit and price increments applied on deploy retries. |
| 25 | +// Increment fields use pointers so zero is a valid configured value (no increment). |
| 26 | +// A nil increment pointer uses the package default increment. |
| 27 | +type GasBoostConfig struct { |
| 28 | + InitialGasLimit uint64 `json:"initialGasLimit"` |
| 29 | + GasLimitIncrement *uint64 `json:"gasLimitIncrement,omitempty"` |
| 30 | + InitialGasPrice uint64 `json:"initialGasPrice"` |
| 31 | + GasPriceIncrement *uint64 `json:"gasPriceIncrement,omitempty"` |
| 32 | +} |
| 33 | + |
| 34 | +// RetryDeployWithGasBoost enables deploy retries with increasing gas on EVM chains. |
| 35 | +// The first execution attempt uses the chain deployer's default gas settings (auto-estimation). |
| 36 | +// Gas overrides apply only after a gas-related failure on subsequent retry attempts. |
| 37 | +// On ZkSync VM chains, gas fields are not adjusted; omit this option for ZkSync-only deploy flows. |
| 38 | +// When cfg is nil, returns a no-op option and retry remains disabled (omit this option instead). |
| 39 | +func RetryDeployWithGasBoost[ARGS any](cfg *GasBoostConfig) operations.ExecuteOption[DeployInput[ARGS], evm.Chain] { |
| 40 | + if cfg == nil { |
| 41 | + return func(*operations.ExecuteConfig[DeployInput[ARGS], evm.Chain]) {} |
| 42 | + } |
| 43 | + c := *cfg |
| 44 | + |
| 45 | + return operations.WithRetryInput(func(attempt uint, err error, in DeployInput[ARGS], deps evm.Chain) DeployInput[ARGS] { |
| 46 | + if deps.IsZkSyncVM || !isGasRetryableError(err) { |
| 47 | + return in |
| 48 | + } |
| 49 | + |
| 50 | + gasLimit, gasPrice := nextBoostedGas(c, attempt, in.GasLimit, in.GasPrice) |
| 51 | + in.GasLimit = gasLimit |
| 52 | + in.GasPrice = gasPrice |
| 53 | + |
| 54 | + return in |
| 55 | + }) |
| 56 | +} |
| 57 | + |
| 58 | +func (cfg GasBoostConfig) gasLimitIncrement() uint64 { |
| 59 | + if cfg.GasLimitIncrement == nil { |
| 60 | + return defaultGasLimitIncrement |
| 61 | + } |
| 62 | + |
| 63 | + return *cfg.GasLimitIncrement |
| 64 | +} |
| 65 | + |
| 66 | +func (cfg GasBoostConfig) gasPriceIncrement() uint64 { |
| 67 | + if cfg.GasPriceIncrement == nil { |
| 68 | + return defaultGasPriceIncrement |
| 69 | + } |
| 70 | + |
| 71 | + return *cfg.GasPriceIncrement |
| 72 | +} |
| 73 | + |
| 74 | +func resolveInitialGasLimit(cfg GasBoostConfig) uint64 { |
| 75 | + if cfg.InitialGasLimit > 0 { |
| 76 | + return cfg.InitialGasLimit |
| 77 | + } |
| 78 | + |
| 79 | + return defaultInitialGasLimit |
| 80 | +} |
| 81 | + |
| 82 | +func resolveInitialGasPrice(cfg GasBoostConfig) uint64 { |
| 83 | + if cfg.InitialGasPrice > 0 { |
| 84 | + return cfg.InitialGasPrice |
| 85 | + } |
| 86 | + |
| 87 | + return defaultInitialGasPrice |
| 88 | +} |
| 89 | + |
| 90 | +func nextBoostedGas(cfg GasBoostConfig, attempt uint, previousLimit, previousPrice uint64) (gasLimit uint64, gasPrice uint64) { |
| 91 | + initialGasLimit := resolveInitialGasLimit(cfg) |
| 92 | + gasLimitIncrement := cfg.gasLimitIncrement() |
| 93 | + initialGasPrice := resolveInitialGasPrice(cfg) |
| 94 | + gasPriceIncrement := cfg.gasPriceIncrement() |
| 95 | + |
| 96 | + if previousLimit > 0 { |
| 97 | + gasLimit = previousLimit + gasLimitIncrement |
| 98 | + } else { |
| 99 | + gasLimit = initialGasLimit + uint64(attempt)*gasLimitIncrement |
| 100 | + } |
| 101 | + |
| 102 | + if previousPrice > 0 { |
| 103 | + gasPrice = previousPrice + gasPriceIncrement |
| 104 | + } else { |
| 105 | + gasPrice = initialGasPrice + uint64(attempt)*gasPriceIncrement |
| 106 | + } |
| 107 | + |
| 108 | + return gasLimit, gasPrice |
| 109 | +} |
| 110 | + |
| 111 | +func isGasRetryableError(err error) bool { |
| 112 | + if err == nil { |
| 113 | + return false |
| 114 | + } |
| 115 | + |
| 116 | + if errors.Is(err, vm.ErrOutOfGas) || |
| 117 | + errors.Is(err, vm.ErrCodeStoreOutOfGas) || |
| 118 | + errors.Is(err, core.ErrIntrinsicGas) || |
| 119 | + errors.Is(err, core.ErrFeeCapTooLow) || |
| 120 | + errors.Is(err, core.ErrGasLimitReached) || |
| 121 | + errors.Is(err, txpool.ErrUnderpriced) || |
| 122 | + errors.Is(err, txpool.ErrReplaceUnderpriced) || |
| 123 | + errors.Is(err, txpool.ErrTxGasPriceTooLow) || |
| 124 | + errors.Is(err, txpool.ErrGasLimit) { |
| 125 | + return true |
| 126 | + } |
| 127 | + |
| 128 | + msg := strings.ToLower(err.Error()) |
| 129 | + for _, pattern := range gasRetryableErrorPatterns { |
| 130 | + if strings.Contains(msg, pattern) { |
| 131 | + return true |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return false |
| 136 | +} |
| 137 | + |
| 138 | +// gasRetryableErrorPatterns covers RPC and wrapped errors that no longer carry geth sentinels. |
| 139 | +var gasRetryableErrorPatterns = []string{ |
| 140 | + "out of gas", |
| 141 | + "gas required exceeds allowance", |
| 142 | + "intrinsic gas too low", |
| 143 | + "underpriced", |
| 144 | + "replacement transaction underpriced", |
| 145 | + "max fee per gas less than block base fee", |
| 146 | + "exceeds block gas limit", |
| 147 | +} |
| 148 | + |
| 149 | +func deployTransactOpts(base *bind.TransactOpts, gasLimit, gasPrice uint64) *bind.TransactOpts { |
| 150 | + if base == nil { |
| 151 | + return nil |
| 152 | + } |
| 153 | + if gasLimit == 0 && gasPrice == 0 { |
| 154 | + return base |
| 155 | + } |
| 156 | + |
| 157 | + opts := *base |
| 158 | + if gasLimit > 0 { |
| 159 | + opts.GasLimit = gasLimit |
| 160 | + } |
| 161 | + if gasPrice > 0 { |
| 162 | + opts.GasPrice = new(big.Int).SetUint64(gasPrice) |
| 163 | + opts.GasFeeCap = nil |
| 164 | + opts.GasTipCap = nil |
| 165 | + } |
| 166 | + |
| 167 | + return &opts |
| 168 | +} |
0 commit comments