Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(evm): proper block gas calculation in precompile calls #2131

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ only on the "bankkeeper.BaseKeeper"'s gas consumption.
Remove unnecessary argument in the `VerifyFee` function, which returns the token
payment required based on the effective fee from the tx data. Improve
documentation.
- [#2131](https://github.com/NibiruChain/nibiru/pull/2131) - fix(evm): proper block gas calculation in precompile calls

#### Nibiru EVM | Before Audit 2 - 2024-12-06

Expand Down
15 changes: 6 additions & 9 deletions x/evm/keeper/call_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,17 @@
ctx, evmMsg, evm.NewNoOpTracer(), commit, evmCfg, txConfig, true,
)
if err != nil {
// We don't know the actual gas used, so consuming the gas limit
k.ResetGasMeterAndConsumeGas(ctx, gasLimit)
k.ResetGasMeterAndConsumeGas(ctx, ctx.GasMeter().Limit())

Check warning on line 111 in x/evm/keeper/call_contract.go

View check run for this annotation

Codecov / codecov/patch

x/evm/keeper/call_contract.go#L111

Added line #L111 was not covered by tests
err = errors.Wrap(err, "failed to apply ethereum core message")
return
}
blockGasUsed, errBlockGasUsed := k.AddToBlockGasUsed(ctx, evmResp.GasUsed)
if errBlockGasUsed != nil {
return nil, nil, errors.Wrap(errBlockGasUsed, "error adding transient gas used to block")
}

Check warning on line 118 in x/evm/keeper/call_contract.go

View check run for this annotation

Codecov / codecov/patch

x/evm/keeper/call_contract.go#L117-L118

Added lines #L117 - L118 were not covered by tests
k.ResetGasMeterAndConsumeGas(ctx, blockGasUsed)

if evmResp.Failed() {
k.ResetGasMeterAndConsumeGas(ctx, evmResp.GasUsed)
if strings.Contains(evmResp.VmError, vm.ErrOutOfGas.Error()) {
err = fmt.Errorf("gas required exceeds allowance (%d)", gasLimit)
return
Expand All @@ -130,12 +133,6 @@

// Success, update block gas used and bloom filter
if commit {
blockGasUsed, err := k.AddToBlockGasUsed(ctx, evmResp.GasUsed)
if err != nil {
k.ResetGasMeterAndConsumeGas(ctx, ctx.GasMeter().Limit())
return nil, nil, errors.Wrap(err, "error adding transient gas used to block")
}
k.ResetGasMeterAndConsumeGas(ctx, blockGasUsed)
k.updateBlockBloom(ctx, evmResp, uint64(txConfig.LogIndex))
// TODO: remove after migrating logs
//err = k.EmitLogEvents(ctx, evmResp)
Expand Down
2 changes: 2 additions & 0 deletions x/evm/keeper/funtoken_from_coin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ func (s *FunTokenFromCoinSuite) TestConvertCoinToEvmAndBack() {
s.Require().NoError(err)
s.Require().Equal("0", balance.String())

deps.ResetGasMeter()

s.T().Log("sad: Convert more erc-20 to back to bank coin, insufficient funds")
_, err = deps.EvmKeeper.CallContract(
deps.Ctx,
Expand Down
6 changes: 4 additions & 2 deletions x/evm/precompile/funtoken_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ func (s *FuntokenSuite) TestHappyPath() {
sdk.NewCoins(sdk.NewCoin(s.funtoken.BankDenom, sdk.NewInt(69_420))),
))

deps.ResetGasMeter()

s.Run("IFunToken.bankBalance", func() {
s.Require().NotEmpty(funtoken.BankDenom)
evmResp, err := deps.EvmKeeper.CallContract(
Expand Down Expand Up @@ -222,7 +224,6 @@ func (s *FuntokenSuite) TestHappyPath() {
input, err := embeds.SmartContract_FunToken.ABI.Pack(string(precompile.FunTokenMethod_sendToBank), callArgs...)
s.NoError(err)

deps.ResetGasMeter()
_, ethTxResp, err := evmtest.CallContractTx(
&deps,
precompile.PrecompileAddr_FunToken,
Expand All @@ -242,7 +243,6 @@ func (s *FuntokenSuite) TestHappyPath() {
s.Equal(sdk.NewInt(420).String(),
deps.App.BankKeeper.GetBalance(deps.Ctx, randomAcc, funtoken.BankDenom).Amount.String(),
)
s.deps.ResetGasMeter()
s.Require().NotNil(deps.EvmKeeper.Bank.StateDB)

s.T().Log("Parse the response contract addr and response bytes")
Expand All @@ -255,6 +255,8 @@ func (s *FuntokenSuite) TestHappyPath() {
s.NoError(err)
s.Require().Equal("420", sentAmt.String())

deps.ResetGasMeter()

s.Run("IFuntoken.balance", func() {
evmResp, err := deps.EvmKeeper.CallContract(
deps.Ctx,
Expand Down
Loading