Skip to content

Commit ecda4c1

Browse files
authored
fix(evm): bank keeper extension gas meter type (#2183)
* fix: force gas invariant gas meter type * Update bank_extension.go * Update CHANGELOG.md
1 parent 1ddbc2a commit ecda4c1

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ needed to include double quotes around the hexadecimal string.
8989
- [#2177](https://github.com/NibiruChain/nibiru/pull/2177) - fix(cmd): Continue from #2127 and unwire vesting flags and logic from genaccounts.go
9090
- [#2176](https://github.com/NibiruChain/nibiru/pull/2176) - tests(evm): add dirty state tests from code4rena audit
9191
- [#2180](https://github.com/NibiruChain/nibiru/pull/2180) - fix(evm): apply gas consumption across the entire EVM codebase at `CallContractWithInput`
92-
92+
- [#2183](https://github.com/NibiruChain/nibiru/pull/2183) - fix(evm): bank keeper extension gas meter type
93+
-
9394
#### Nibiru EVM | Before Audit 2 - 2024-12-06
9495

9596
The codebase went through a third-party [Code4rena

x/evm/keeper/bank_extension.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -185,24 +185,20 @@ func (bk NibiruBankKeeper) ForceGasInvariant(
185185
// Assign vars for the tx gas meter
186186
gasMeterBefore := ctx.GasMeter() // Tx gas meter MUST be defined
187187
gasConsumedBefore := gasMeterBefore.GasConsumed()
188-
// Don't modify the "ctx.BlockGasMeter()" directly because this is
189-
// handled in "BaseApp.runTx"
190-
191-
// Start baseGasConsumed at 0 in case we panic before BaseOp completes and
192-
// baseGasConsumed gets a value assignment
193188
baseOpGasConsumed := uint64(0)
194189

195190
defer func() {
191+
// NOTE: we have to refund the entire gasMeterBefore because it's modified by AfterOp
192+
// stateDB.getStateObject() reads from state using the local root ctx which affects the gas meter
196193
gasMeterBefore.RefundGas(gasMeterBefore.GasConsumed(), "")
197194
gasMeterBefore.ConsumeGas(gasConsumedBefore+baseOpGasConsumed, "NibiruBankKeeper invariant")
198195
}()
199196

200-
// Note that because the ctx gas meter uses private variables to track gas,
201-
// we have to branch off with a new gas meter instance to avoid mutating the
202-
// "true" gas meter (called GasMeterBefore here).
203-
// We use an infinite gas meter because we consume gas in the deferred function
204-
// and gasMeterBefore will panic if we consume too much gas.
205-
ctx = ctx.WithGasMeter(sdk.NewInfiniteGasMeter())
197+
// We keep the same gas meter type but reset the gas consumed prior to measuring the base op
198+
// We need the same gas meter type because we use a custom FixedGasMeter for oracle votes in the AnteHandler
199+
// In the defer function, we reset the gas meter again and then add the gasConsumedBefore to baseOpGasConsumed,
200+
// so any modifications to the gasMeterBefore after this point will be inconsequential.
201+
ctx.GasMeter().RefundGas(gasConsumedBefore, "reset gas meter before measuring base op")
206202

207203
err := BaseOp(ctx)
208204
baseOpGasConsumed = ctx.GasMeter().GasConsumed()

0 commit comments

Comments
 (0)