Skip to content

Commit

Permalink
Problem: trace block not accurate for dynamic fee transaction
Browse files Browse the repository at this point in the history
fix

changelog

temp
  • Loading branch information
yihuang committed Sep 23, 2024
1 parent 7e30762 commit 66a799e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (rpc) [#521](https://github.com/crypto-org-chain/ethermint/pull/521) Align hash and miner in subscribe newHeads with eth_getBlockByNumber.
* (rpc) [#516](https://github.com/crypto-org-chain/ethermint/pull/516) Avoid method eth_chainId crashed due to nil pointer on IsEIP155 check.
* (cli) [#524](https://github.com/crypto-org-chain/ethermint/pull/524) Allow tx evm raw run for generate only when offline with evm-denom flag.
* (rpc) [#527](https://github.com/crypto-org-chain/ethermint/pull/527) Fix balance consistency between trace-block and state machine.

### Improvements

Expand Down
41 changes: 24 additions & 17 deletions x/evm/keeper/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,33 +322,40 @@ func (k *Keeper) ApplyMessageWithConfig(
return nil, errorsmod.Wrap(types.ErrCallDisabled, "failed to call contract")
}

stateDB := statedb.NewWithParams(ctx, k, cfg.TxConfig, cfg.Params.EvmDenom)
var evm *vm.EVM
if cfg.Overrides != nil {
if err := cfg.Overrides.Apply(stateDB); err != nil {
return nil, errorsmod.Wrap(err, "failed to apply state override")
}
}
evm = k.NewEVM(ctx, msg, cfg, stateDB)
leftoverGas := msg.GasLimit
sender := vm.AccountRef(msg.From)
// Allow the tracer captures the tx level events, mainly the gas consumption.
vmCfg := evm.Config
if vmCfg.Tracer != nil {
leftoverGas := msg.GasLimit
senderAddr := sdk.AccAddress(msg.From.Bytes())
if cfg.Tracer != nil {
if cfg.DebugTrace {
// msg.GasPrice should have been set to effective gas price
stateDB.SubBalance(sender.Address(), new(big.Int).Mul(msg.GasPrice, new(big.Int).SetUint64(msg.GasLimit)))
stateDB.SetNonce(sender.Address(), stateDB.GetNonce(sender.Address())+1)
amount := new(big.Int).Mul(msg.GasPrice, new(big.Int).SetUint64(msg.GasLimit))
if err := k.SubBalance(ctx, senderAddr, sdk.NewCoins(sdk.NewCoin(cfg.Params.EvmDenom, sdkmath.NewIntFromBigInt(amount)))); err != nil {
return nil, errorsmod.Wrap(err, "failed to subtract balance")
}
if err := k.incrNonce(ctx, senderAddr); err != nil {
return nil, errorsmod.Wrap(err, "failed to increment nonce")
}
}
vmCfg.Tracer.CaptureTxStart(leftoverGas)
cfg.Tracer.CaptureTxStart(leftoverGas)
defer func() {
if cfg.DebugTrace {
stateDB.AddBalance(sender.Address(), new(big.Int).Mul(msg.GasPrice, new(big.Int).SetUint64(leftoverGas)))
amount := new(big.Int).Mul(msg.GasPrice, new(big.Int).SetUint64(leftoverGas))
k.AddBalance(ctx, senderAddr, sdk.NewCoins(sdk.NewCoin(cfg.Params.EvmDenom, sdkmath.NewIntFromBigInt(amount))))

Check failure on line 343 in x/evm/keeper/state_transition.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

Error return value of `k.AddBalance` is not checked (errcheck)
}
vmCfg.Tracer.CaptureTxEnd(leftoverGas)
cfg.Tracer.CaptureTxEnd(leftoverGas)
}()
}

stateDB := statedb.NewWithParams(ctx, k, cfg.TxConfig, cfg.Params.EvmDenom)
var evm *vm.EVM
if cfg.Overrides != nil {
if err := cfg.Overrides.Apply(stateDB); err != nil {
return nil, errorsmod.Wrap(err, "failed to apply state override")
}
}
evm = k.NewEVM(ctx, msg, cfg, stateDB)
sender := vm.AccountRef(msg.From)

rules := cfg.Rules
contractCreation := msg.To == nil
intrinsicGas, err := k.GetEthIntrinsicGas(msg, rules, contractCreation)
Expand Down
14 changes: 14 additions & 0 deletions x/evm/keeper/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@ func (k *Keeper) SetAccount(ctx sdk.Context, addr common.Address, account stated
return nil
}

func (k *Keeper) incrNonce(ctx sdk.Context, addr sdk.AccAddress) error {
acct := k.accountKeeper.GetAccount(ctx, addr)
if acct == nil {
acct = k.accountKeeper.NewAccountWithAddress(ctx, addr)
}

if err := acct.SetSequence(acct.GetSequence() + 1); err != nil {
return err
}

k.accountKeeper.SetAccount(ctx, acct)
return nil
}

// SetState update contract storage, delete if value is empty.
func (k *Keeper) SetState(ctx sdk.Context, addr common.Address, key common.Hash, value []byte) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.AddressStoragePrefix(addr))
Expand Down

0 comments on commit 66a799e

Please sign in to comment.