Skip to content

Commit

Permalink
Fallback to max gas limit in estimategas (#4853)
Browse files Browse the repository at this point in the history
Fallback to max gas limit
  • Loading branch information
connorwstein committed Aug 12, 2021
1 parent 016d568 commit af94120
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
1 change: 1 addition & 0 deletions core/services/pipeline/runner.go
Expand Up @@ -237,6 +237,7 @@ func (r *runner) run(
task.(*VRFTaskV2).keyStore = r.vrfKeyStore
case TaskTypeEstimateGasLimit:
task.(*EstimateGasLimitTask).GasEstimator = r.ethClient
task.(*EstimateGasLimitTask).EvmGasLimit = r.config.EvmGasLimitDefault()
case TaskTypeETHTx:
task.(*ETHTxTask).db = r.orm.DB()
task.(*ETHTxTask).config = r.config
Expand Down
18 changes: 15 additions & 3 deletions core/services/pipeline/task.estimategas.go
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"strconv"

"github.com/smartcontractkit/chainlink/core/logger"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
Expand All @@ -22,14 +24,17 @@ type EstimateGasLimitTask struct {
Multiplier string `json:"multiplier"`
Data string `json:"data"`

EvmGasLimit uint64
GasEstimator GasEstimator
}

type GasEstimator interface {
EstimateGas(ctx context.Context, call ethereum.CallMsg) (uint64, error)
}

var _ Task = (*EstimateGasLimitTask)(nil)
var (
_ Task = (*EstimateGasLimitTask)(nil)
)

func (t *EstimateGasLimitTask) Type() TaskType {
return TaskTypeEstimateGasLimit
Expand Down Expand Up @@ -57,7 +62,10 @@ func (t *EstimateGasLimitTask) Run(_ context.Context, vars Vars, inputs []Result
Data: data,
})
if err != nil {
return Result{Error: err}
// Fallback to the maximum conceivable gas limit
// if we're unable to call estimate gas for whatever reason.
logger.Warnw("EstimateGas: unable to estimate, fallback to configured limit", "err", err, "fallback", t.EvmGasLimit)
return Result{Value: t.EvmGasLimit}
}
gasLimitDecimal, err := decimal.NewFromString(strconv.FormatUint(gasLimit, 10))
if err != nil {
Expand All @@ -67,5 +75,9 @@ func (t *EstimateGasLimitTask) Run(_ context.Context, vars Vars, inputs []Result
if !gasLimitWithMultiplier.IsUint64() {
return Result{Error: errors.New("Invalid multiplier")}
}
return Result{Value: gasLimitWithMultiplier.Uint64()}
gasLimitFinal := gasLimitWithMultiplier.Uint64()
if gasLimitFinal > t.EvmGasLimit {
gasLimitFinal = t.EvmGasLimit
}
return Result{Value: gasLimitFinal}
}
6 changes: 3 additions & 3 deletions core/services/vrf/integration_v2_test.go
Expand Up @@ -144,7 +144,7 @@ func TestIntegrationVRFV2(t *testing.T) {
defer cleanupDB()
key := cltest.MustGenerateRandomKey(t)
uni := newVRFCoordinatorV2Universe(t, key)
config.Overrides.EvmGasLimitDefault = null.IntFrom(500000)
config.Overrides.EvmGasLimitDefault = null.IntFrom(2000000)

app, cleanup := cltest.NewApplicationWithConfigAndKeyOnSimulatedBlockchain(t, config, uni.backend, key)
defer cleanup()
Expand Down Expand Up @@ -287,11 +287,11 @@ func TestIntegrationVRFV2(t *testing.T) {
t.Logf("subscription charged %s with gas prices of %s gwei and %s ETH per LINK\n", linkCharged, gasPrice.Div(gwei), weiPerUnitLink.Div(wei))
expected := decimal.RequireFromString(strconv.Itoa(int(fulfillReceipt.GasUsed))).Mul(gasPrice).Div(weiPerUnitLink)
t.Logf("expected sub charge gas use %v %v off by %v", fulfillReceipt.GasUsed, expected, expected.Sub(linkCharged))
// The expected sub charge should be within 100 gas of the actual gas usage.
// The expected sub charge should be within 200 gas of the actual gas usage.
// wei/link * link / wei/gas = wei / (wei/gas) = gas
gasDiff := linkCharged.Sub(expected).Mul(weiPerUnitLink).Div(gasPrice).Abs().IntPart()
t.Log("gasDiff", gasDiff)
assert.Less(t, gasDiff, int64(100))
assert.Less(t, gasDiff, int64(200))

// Oracle tries to withdraw move than it was paid should fail
_, err = uni.rootContract.OracleWithdraw(uni.nallory, uni.nallory.From, linkWeiCharged.Add(decimal.NewFromInt(1)).BigInt())
Expand Down
4 changes: 2 additions & 2 deletions core/testdata/tomlspecs/vrf-v2-spec.toml
Expand Up @@ -19,10 +19,10 @@ encode_tx [type=ethabiencode
abi="fulfillRandomWords(bytes proof)"
data="{\\"proof\\": $(vrf.proof)}"]
estimate_gas [type=estimategaslimit
to="%s"
to="0xABA5eDc1a551E55b1A570c0e1f1055e5BE11eca7"
multiplier="1.1"
data="$(encode_tx)"]
submit_tx [type=ethtx to="%s"
submit_tx [type=ethtx to="0xABA5eDc1a551E55b1A570c0e1f1055e5BE11eca7"
data="$(encode_tx)"
gasLimit="$(estimate_gas)"
txMeta="{\\"requestTxHash\\": $(jobRun.logTxHash),\\"requestID\\": $(vrf.requestID),\\"jobID\\": $(jobSpec.databaseID)}"]
Expand Down

0 comments on commit af94120

Please sign in to comment.