Skip to content

Commit

Permalink
fix(evm): non-deterministic gas usage (#2110)
Browse files Browse the repository at this point in the history
* fix: typo

* fix: return to old StateDB pointer in BankKeeper after ApplyEvmMsg

* Update CHANGELOG.md
  • Loading branch information
k-yang authored Nov 12, 2024
1 parent b4283f1 commit f5d639d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ tests for race conditions within funtoken precompile
- [#2107](https://github.com/NibiruChain/nibiru/pull/2107) -
feat(evm-funtoken-precompile): Implement methods: balance, bankBalance, whoAmI
- [#2108](https://github.com/NibiruChain/nibiru/pull/2108) - fix(evm): removed deprecated root key from eth_getTransactionReceipt

- [#2110](https://github.com/NibiruChain/nibiru/pull/2110) - fix(evm): Restore StateDB to its state prior to ApplyEvmMsg call to ensure deterministic gas usage. This fixes an issue where the StateDB pointer field in NibiruBankKeeper was being updated during readonly query endpoints like eth_estimateGas, leading to non-deterministic gas usage in subsequent transactions.

#### Nibiru EVM | Before Audit 1 - 2024-10-18

Expand Down
2 changes: 1 addition & 1 deletion x/evm/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ func (k Keeper) EstimateGasForEvmCallType(
return nil, fmt.Errorf("gas required exceeds allowance (%d)", gasCap)
}

return nil, fmt.Errorf("Estimgate gas VMError: %s", result.VmError)
return nil, fmt.Errorf("Estimate gas VMError: %s", result.VmError)
}
}

Expand Down
6 changes: 6 additions & 0 deletions x/evm/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,12 @@ func (k *Keeper) ApplyEvmMsg(ctx sdk.Context,
vmErr error // vm errors do not effect consensus and are therefore not assigned to err
)

// save a reference to return to the previous stateDB
oldStateDb := k.Bank.StateDB
defer func() {
k.Bank.StateDB = oldStateDb
}()

stateDB := k.NewStateDB(ctx, txConfig)
evmObj = k.NewEVM(ctx, msg, evmConfig, tracer, stateDB)

Expand Down

0 comments on commit f5d639d

Please sign in to comment.