@@ -326,6 +326,11 @@ func (k *Keeper) ApplyMessageWithConfig(
326326 leftoverGas := msg .GasLimit
327327 sender := vm .AccountRef (msg .From )
328328 tracer := cfg .GetTracer ()
329+ debugFn := func () {
330+ if tracer != nil && cfg .DebugTrace {
331+ stateDB .AddBalance (sender .Address (), new (big.Int ).Mul (msg .GasPrice , new (big.Int ).SetUint64 (leftoverGas )))
332+ }
333+ }
329334 if tracer != nil {
330335 if cfg .DebugTrace {
331336 amount := new (big.Int ).Mul (msg .GasPrice , new (big.Int ).SetUint64 (msg .GasLimit ))
@@ -337,9 +342,7 @@ func (k *Keeper) ApplyMessageWithConfig(
337342 }
338343 tracer .CaptureTxStart (leftoverGas )
339344 defer func () {
340- if cfg .DebugTrace {
341- stateDB .AddBalance (sender .Address (), new (big.Int ).Mul (msg .GasPrice , new (big.Int ).SetUint64 (leftoverGas )))
342- }
345+ debugFn ()
343346 tracer .CaptureTxEnd (leftoverGas )
344347 }()
345348 }
@@ -404,13 +407,6 @@ func (k *Keeper) ApplyMessageWithConfig(
404407 vmError = vmErr .Error ()
405408 }
406409
407- // The dirty states in `StateDB` is either committed or discarded after return
408- if commit {
409- if err := stateDB .Commit (); err != nil {
410- return nil , errorsmod .Wrap (err , "failed to commit stateDB" )
411- }
412- }
413-
414410 // calculate a minimum amount of gas to be charged to sender if GasLimit
415411 // is considerably higher than GasUsed to stay more aligned with Tendermint gas mechanics
416412 // for more info https://github.com/evmos/ethermint/issues/1085
@@ -438,6 +434,16 @@ func (k *Keeper) ApplyMessageWithConfig(
438434 // reset leftoverGas, to be used by the tracer
439435 leftoverGas = msg .GasLimit - gasUsed
440436
437+ debugFn ()
438+ debugFn = func () {}
439+
440+ // The dirty states in `StateDB` is either committed or discarded after return
441+ if commit {
442+ if err := stateDB .Commit (); err != nil {
443+ return nil , errorsmod .Wrap (err , "failed to commit stateDB" )
444+ }
445+ }
446+
441447 return & types.MsgEthereumTxResponse {
442448 GasUsed : gasUsed ,
443449 VmError : vmError ,
0 commit comments