Fix high gas usage for coin conversions (#1331)

* use an infinite gas meter during evm call operations, then consume
evm reported gas used

* consume gas after failure check -- provide best feedback first
This commit is contained in:
Nick DeLuca 2022-10-05 15:27:06 -07:00 committed by GitHub
parent be7242d86d
commit 55284aa575
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 4 deletions

View File

@ -164,13 +164,17 @@ func (suite *ConversionTestSuite) TestConvertCoinToERC20() {
)
suite.Require().NoError(err)
// convert coin to erc20
ctx := suite.Ctx.WithGasMeter(sdk.NewInfiniteGasMeter())
err = suite.Keeper.ConvertCoinToERC20(
suite.Ctx,
ctx,
originAcc,
recipientAcc,
sdk.NewCoin(pair.Denom, sdk.NewIntFromBigInt(amount)),
)
suite.Require().NoError(err)
suite.Require().LessOrEqual(ctx.GasMeter().GasConsumed(), uint64(500000))
suite.Require().GreaterOrEqual(ctx.GasMeter().GasConsumed(), uint64(50000))
// Source should decrease
bal := suite.App.GetBankKeeper().GetBalance(suite.Ctx, originAcc, pair.Denom)
@ -279,15 +283,18 @@ func (suite *ConversionTestSuite) TestConvertERC20ToCoin() {
)
suite.Require().NoError(err)
ctx := suite.Ctx.WithGasMeter(sdk.NewInfiniteGasMeter())
convertAmt := sdk.NewInt(50)
err = suite.Keeper.ConvertERC20ToCoin(
suite.Ctx,
ctx,
userEvmAddr,
userAddr,
pair.GetAddress(),
convertAmt,
)
suite.Require().NoError(err)
suite.Require().LessOrEqual(ctx.GasMeter().GasConsumed(), uint64(500000))
suite.Require().GreaterOrEqual(ctx.GasMeter().GasConsumed(), uint64(50000))
// bank balance should decrease
bal := suite.App.GetBankKeeper().GetBalance(suite.Ctx, userAddr, pair.Denom)

View File

@ -89,6 +89,8 @@ func (k Keeper) CallEVMWithData(
return nil, err
}
ethGasContext := ctx.WithGasMeter(sdk.NewInfiniteGasMeter())
// EstimateGas applies the transaction against current block state to get
// optimal gas value. Since this is done right before the ApplyMessage
// below, it should essentially do the same thing but without affecting
@ -96,7 +98,7 @@ func (k Keeper) CallEVMWithData(
// accurate exact amount in this case, as both the chain state and tx used
// to estimate and apply are the exact same (ie. no txs between estimate and
// apply, tx order is the same, etc.)
gasRes, err := k.evmKeeper.EstimateGas(sdk.WrapSDKContext(ctx), &evmtypes.EthCallRequest{
gasRes, err := k.evmKeeper.EstimateGas(sdk.WrapSDKContext(ethGasContext), &evmtypes.EthCallRequest{
Args: args,
GasCap: config.DefaultGasCap,
})
@ -118,7 +120,7 @@ func (k Keeper) CallEVMWithData(
true, // checkNonce
)
res, err := k.evmKeeper.ApplyMessage(ctx, msg, evmtypes.NewNoOpTracer(), true)
res, err := k.evmKeeper.ApplyMessage(ethGasContext, msg, evmtypes.NewNoOpTracer(), true)
if err != nil {
return nil, err
}
@ -127,6 +129,8 @@ func (k Keeper) CallEVMWithData(
return nil, sdkerrors.Wrap(evmtypes.ErrVMExecution, res.VmError)
}
ctx.GasMeter().ConsumeGas(res.GasUsed, "evm gas consumed")
return res, nil
}