mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-27 00:35:18 +00:00
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:
parent
be7242d86d
commit
55284aa575
@ -164,13 +164,17 @@ func (suite *ConversionTestSuite) TestConvertCoinToERC20() {
|
|||||||
)
|
)
|
||||||
suite.Require().NoError(err)
|
suite.Require().NoError(err)
|
||||||
|
|
||||||
|
// convert coin to erc20
|
||||||
|
ctx := suite.Ctx.WithGasMeter(sdk.NewInfiniteGasMeter())
|
||||||
err = suite.Keeper.ConvertCoinToERC20(
|
err = suite.Keeper.ConvertCoinToERC20(
|
||||||
suite.Ctx,
|
ctx,
|
||||||
originAcc,
|
originAcc,
|
||||||
recipientAcc,
|
recipientAcc,
|
||||||
sdk.NewCoin(pair.Denom, sdk.NewIntFromBigInt(amount)),
|
sdk.NewCoin(pair.Denom, sdk.NewIntFromBigInt(amount)),
|
||||||
)
|
)
|
||||||
suite.Require().NoError(err)
|
suite.Require().NoError(err)
|
||||||
|
suite.Require().LessOrEqual(ctx.GasMeter().GasConsumed(), uint64(500000))
|
||||||
|
suite.Require().GreaterOrEqual(ctx.GasMeter().GasConsumed(), uint64(50000))
|
||||||
|
|
||||||
// Source should decrease
|
// Source should decrease
|
||||||
bal := suite.App.GetBankKeeper().GetBalance(suite.Ctx, originAcc, pair.Denom)
|
bal := suite.App.GetBankKeeper().GetBalance(suite.Ctx, originAcc, pair.Denom)
|
||||||
@ -279,15 +283,18 @@ func (suite *ConversionTestSuite) TestConvertERC20ToCoin() {
|
|||||||
)
|
)
|
||||||
suite.Require().NoError(err)
|
suite.Require().NoError(err)
|
||||||
|
|
||||||
|
ctx := suite.Ctx.WithGasMeter(sdk.NewInfiniteGasMeter())
|
||||||
convertAmt := sdk.NewInt(50)
|
convertAmt := sdk.NewInt(50)
|
||||||
err = suite.Keeper.ConvertERC20ToCoin(
|
err = suite.Keeper.ConvertERC20ToCoin(
|
||||||
suite.Ctx,
|
ctx,
|
||||||
userEvmAddr,
|
userEvmAddr,
|
||||||
userAddr,
|
userAddr,
|
||||||
pair.GetAddress(),
|
pair.GetAddress(),
|
||||||
convertAmt,
|
convertAmt,
|
||||||
)
|
)
|
||||||
suite.Require().NoError(err)
|
suite.Require().NoError(err)
|
||||||
|
suite.Require().LessOrEqual(ctx.GasMeter().GasConsumed(), uint64(500000))
|
||||||
|
suite.Require().GreaterOrEqual(ctx.GasMeter().GasConsumed(), uint64(50000))
|
||||||
|
|
||||||
// bank balance should decrease
|
// bank balance should decrease
|
||||||
bal := suite.App.GetBankKeeper().GetBalance(suite.Ctx, userAddr, pair.Denom)
|
bal := suite.App.GetBankKeeper().GetBalance(suite.Ctx, userAddr, pair.Denom)
|
||||||
|
@ -89,6 +89,8 @@ func (k Keeper) CallEVMWithData(
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ethGasContext := ctx.WithGasMeter(sdk.NewInfiniteGasMeter())
|
||||||
|
|
||||||
// EstimateGas applies the transaction against current block state to get
|
// EstimateGas applies the transaction against current block state to get
|
||||||
// optimal gas value. Since this is done right before the ApplyMessage
|
// optimal gas value. Since this is done right before the ApplyMessage
|
||||||
// below, it should essentially do the same thing but without affecting
|
// 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
|
// 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
|
// to estimate and apply are the exact same (ie. no txs between estimate and
|
||||||
// apply, tx order is the same, etc.)
|
// 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,
|
Args: args,
|
||||||
GasCap: config.DefaultGasCap,
|
GasCap: config.DefaultGasCap,
|
||||||
})
|
})
|
||||||
@ -118,7 +120,7 @@ func (k Keeper) CallEVMWithData(
|
|||||||
true, // checkNonce
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -127,6 +129,8 @@ func (k Keeper) CallEVMWithData(
|
|||||||
return nil, sdkerrors.Wrap(evmtypes.ErrVMExecution, res.VmError)
|
return nil, sdkerrors.Wrap(evmtypes.ErrVMExecution, res.VmError)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ctx.GasMeter().ConsumeGas(res.GasUsed, "evm gas consumed")
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user