feat: compute a better gas limit for recursive external contract calls
This commit is contained in:
+129
-3
@@ -24,6 +24,7 @@ import (
|
||||
"github.com/filecoin-project/go-state-types/builtin/v10/eam"
|
||||
"github.com/filecoin-project/go-state-types/builtin/v10/evm"
|
||||
"github.com/filecoin-project/go-state-types/crypto"
|
||||
"github.com/filecoin-project/go-state-types/exitcode"
|
||||
|
||||
"github.com/filecoin-project/lotus/api"
|
||||
"github.com/filecoin-project/lotus/build"
|
||||
@@ -809,12 +810,137 @@ func (a *EthModule) EthEstimateGas(ctx context.Context, tx ethtypes.EthCall) (et
|
||||
// gas estimation actually run.
|
||||
msg.GasLimit = 0
|
||||
|
||||
msg, err = a.GasAPI.GasEstimateMessageGas(ctx, msg, nil, types.EmptyTSK)
|
||||
ts := a.Chain.GetHeaviestTipSet()
|
||||
msg, err = a.GasAPI.GasEstimateMessageGas(ctx, msg, nil, ts.Key())
|
||||
if err != nil {
|
||||
return ethtypes.EthUint64(0), err
|
||||
return ethtypes.EthUint64(0), xerrors.Errorf("failed to estimate gas: %w", err)
|
||||
}
|
||||
|
||||
return ethtypes.EthUint64(msg.GasLimit), nil
|
||||
expectedGas, err := ethGasSearch(ctx, a.Chain, a.Stmgr, a.Mpool, msg, ts)
|
||||
if err != nil {
|
||||
log.Errorw("expected gas", "err", err)
|
||||
}
|
||||
|
||||
return ethtypes.EthUint64(expectedGas), nil
|
||||
}
|
||||
|
||||
// gasSearch does an exponential search to find a gas value to execute the
|
||||
// message with. It first finds a high gas limit that allows the message to execute
|
||||
// by doubling the previous gas limit until it succeeds then does a binary
|
||||
// search till it gets within a range of 1%
|
||||
func gasSearch(
|
||||
ctx context.Context,
|
||||
smgr *stmgr.StateManager,
|
||||
msgIn *types.Message,
|
||||
priorMsgs []types.ChainMsg,
|
||||
ts *types.TipSet,
|
||||
) (int64, error) {
|
||||
msg := *msgIn
|
||||
|
||||
high := msg.GasLimit
|
||||
low := msg.GasLimit
|
||||
|
||||
canSucceed := func(limit int64) (bool, error) {
|
||||
msg.GasLimit = limit
|
||||
|
||||
res, err := smgr.CallWithGas(ctx, &msg, priorMsgs, ts)
|
||||
if err != nil {
|
||||
return false, xerrors.Errorf("CallWithGas failed: %w", err)
|
||||
}
|
||||
|
||||
if res.MsgRct.ExitCode.IsSuccess() {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
for {
|
||||
ok, err := canSucceed(high)
|
||||
if err != nil {
|
||||
return -1, xerrors.Errorf("searching for high gas limit failed: %w", err)
|
||||
}
|
||||
if ok {
|
||||
break
|
||||
}
|
||||
|
||||
low = high
|
||||
high = high * 2
|
||||
|
||||
if high > build.BlockGasLimit {
|
||||
high = build.BlockGasLimit
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
checkThreshold := high / 100
|
||||
for (high - low) > checkThreshold {
|
||||
median := (low + high) / 2
|
||||
ok, err := canSucceed(median)
|
||||
if err != nil {
|
||||
return -1, xerrors.Errorf("searching for optimal gas limit failed: %w", err)
|
||||
}
|
||||
|
||||
if ok {
|
||||
high = median
|
||||
} else {
|
||||
low = median
|
||||
}
|
||||
|
||||
checkThreshold = median / 100
|
||||
}
|
||||
|
||||
return high, nil
|
||||
}
|
||||
|
||||
func traceContainsExitCode(et types.ExecutionTrace, ex exitcode.ExitCode) bool {
|
||||
if et.MsgRct.ExitCode == ex {
|
||||
return true
|
||||
}
|
||||
|
||||
for _, et := range et.Subcalls {
|
||||
if traceContainsExitCode(et, ex) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// ethGasSearch executes a message for gas estimation using the previously estimated gas.
|
||||
// If the message fails due to an out of gas error then a gas search is performed.
|
||||
// See gasSearch.
|
||||
func ethGasSearch(
|
||||
ctx context.Context,
|
||||
cstore *store.ChainStore,
|
||||
smgr *stmgr.StateManager,
|
||||
mpool *messagepool.MessagePool,
|
||||
msgIn *types.Message,
|
||||
ts *types.TipSet,
|
||||
) (int64, error) {
|
||||
msg := *msgIn
|
||||
currTs := ts
|
||||
|
||||
res, priorMsgs, ts, err := gasEstimateCallWithGas(ctx, cstore, smgr, mpool, &msg, currTs)
|
||||
if err != nil {
|
||||
return -1, xerrors.Errorf("gas estimation failed: %w", err)
|
||||
}
|
||||
|
||||
if res.MsgRct.ExitCode.IsSuccess() {
|
||||
return msg.GasLimit, nil
|
||||
}
|
||||
|
||||
if traceContainsExitCode(res.ExecutionTrace, exitcode.SysErrOutOfGas) {
|
||||
ret, err := gasSearch(ctx, smgr, &msg, priorMsgs, ts)
|
||||
if err != nil {
|
||||
return -1, xerrors.Errorf("gas estimation search failed: %w", err)
|
||||
}
|
||||
|
||||
ret = int64(float64(ret) * mpool.GetConfig().GasLimitOverestimation)
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
return -1, xerrors.Errorf("message execution failed: exit %s, reason: %s", res.MsgRct.ExitCode, res.Error)
|
||||
}
|
||||
|
||||
func (a *EthModule) EthCall(ctx context.Context, tx ethtypes.EthCall, blkParam string) (ethtypes.EthBytes, error) {
|
||||
|
||||
+34
-9
@@ -248,22 +248,23 @@ func (m *GasModule) GasEstimateGasLimit(ctx context.Context, msgIn *types.Messag
|
||||
}
|
||||
return gasEstimateGasLimit(ctx, m.Chain, m.Stmgr, m.Mpool, msgIn, ts)
|
||||
}
|
||||
func gasEstimateGasLimit(
|
||||
|
||||
// gasEstimateCallWithGas invokes a message "msgIn" on the earliest available tipset with pending
|
||||
// messages in the message pool. The function returns the result of the message invocation, the
|
||||
// pending messages, the tipset used for the invocation, and an error if occurred.
|
||||
// The returned information can be used to make subsequent calls to CallWithGas with the same parameters.
|
||||
func gasEstimateCallWithGas(
|
||||
ctx context.Context,
|
||||
cstore *store.ChainStore,
|
||||
smgr *stmgr.StateManager,
|
||||
mpool *messagepool.MessagePool,
|
||||
msgIn *types.Message,
|
||||
currTs *types.TipSet,
|
||||
) (int64, error) {
|
||||
) (*api.InvocResult, []types.ChainMsg, *types.TipSet, error) {
|
||||
msg := *msgIn
|
||||
msg.GasLimit = build.BlockGasLimit
|
||||
msg.GasFeeCap = big.Zero()
|
||||
msg.GasPremium = big.Zero()
|
||||
|
||||
fromA, err := smgr.ResolveToDeterministicAddress(ctx, msgIn.From, currTs)
|
||||
if err != nil {
|
||||
return -1, xerrors.Errorf("getting key address: %w", err)
|
||||
return nil, []types.ChainMsg{}, nil, xerrors.Errorf("getting key address: %w", err)
|
||||
}
|
||||
|
||||
pending, ts := mpool.PendingFor(ctx, fromA)
|
||||
@@ -284,12 +285,34 @@ func gasEstimateGasLimit(
|
||||
}
|
||||
ts, err = cstore.GetTipSetFromKey(ctx, ts.Parents())
|
||||
if err != nil {
|
||||
return -1, xerrors.Errorf("getting parent tipset: %w", err)
|
||||
return nil, []types.ChainMsg{}, nil, xerrors.Errorf("getting parent tipset: %w", err)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return -1, xerrors.Errorf("CallWithGas failed: %w", err)
|
||||
return nil, []types.ChainMsg{}, nil, xerrors.Errorf("CallWithGas failed: %w", err)
|
||||
}
|
||||
|
||||
return res, priorMsgs, ts, nil
|
||||
}
|
||||
|
||||
func gasEstimateGasLimit(
|
||||
ctx context.Context,
|
||||
cstore *store.ChainStore,
|
||||
smgr *stmgr.StateManager,
|
||||
mpool *messagepool.MessagePool,
|
||||
msgIn *types.Message,
|
||||
currTs *types.TipSet,
|
||||
) (int64, error) {
|
||||
msg := *msgIn
|
||||
msg.GasLimit = build.BlockGasLimit
|
||||
msg.GasFeeCap = big.Zero()
|
||||
msg.GasPremium = big.Zero()
|
||||
|
||||
res, _, ts, err := gasEstimateCallWithGas(ctx, cstore, smgr, mpool, &msg, currTs)
|
||||
if err != nil {
|
||||
return -1, xerrors.Errorf("gas estimation failed: %w", err)
|
||||
}
|
||||
|
||||
if res.MsgRct.ExitCode == exitcode.SysErrOutOfGas {
|
||||
return -1, &api.ErrOutOfGas{}
|
||||
}
|
||||
@@ -300,6 +323,8 @@ func gasEstimateGasLimit(
|
||||
|
||||
ret := res.MsgRct.GasUsed
|
||||
|
||||
log.Infow("GasEstimateMessageGas CallWithGas Result", "GasUsed", ret, "ExitCode", res.MsgRct.ExitCode)
|
||||
|
||||
transitionalMulti := 1.0
|
||||
// Overestimate gas around the upgrade
|
||||
if ts.Height() <= build.UpgradeSkyrHeight && (build.UpgradeSkyrHeight-ts.Height() <= 20) {
|
||||
|
||||
Reference in New Issue
Block a user