diff --git a/precompiles/bank/bank.go b/precompiles/bank/bank.go index dcfa01fc3a..b0591b813e 100644 --- a/precompiles/bank/bank.go +++ b/precompiles/bank/bank.go @@ -230,9 +230,9 @@ func (p PrecompileExecutor) sendNative(ctx sdk.Context, method *abi.Method, args if hooks := tracers.GetCtxEthTracingHooks(ctx); hooks != nil && hooks.OnBalanceChange != nil && (value.Sign() != 0) { // The SendCoinsAndWei function above works with Sei addresses that haven't been associated here. Hence we cannot // use `GetEVMAddress` and enforce to have a mapping. So we use GetEVMAddressOrDefault to get the EVM address. - receveirEvmAddr := p.evmKeeper.GetEVMAddressOrDefault(ctx, receiverSeiAddr) + receiverEvmAddr := tracers.GetEVMAddress(ctx, p.evmKeeper, receiverSeiAddr) - tracers.TraceTransferEVMValue(ctx, hooks, p.bankKeeper, senderSeiAddr, caller, receiverSeiAddr, receveirEvmAddr, value) + tracers.TraceTransferEVMValue(ctx, hooks, p.bankKeeper, senderSeiAddr, caller, receiverSeiAddr, receiverEvmAddr, value) } bz, err := method.Outputs.Pack(true) diff --git a/x/evm/module.go b/x/evm/module.go index d176e3ea6e..b5f0f2c140 100644 --- a/x/evm/module.go +++ b/x/evm/module.go @@ -314,7 +314,7 @@ func (am AppModule) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) []abci.V var coinbaseEVMAddress common.Address if evmHooks != nil { - coinbaseEVMAddress = am.keeper.GetEVMAddressOrDefault(ctx, coinbase) + coinbaseEVMAddress = tracers.GetEVMAddress(ctx, am.keeper, coinbase) } denom := am.keeper.GetBaseDenom(ctx) surplus := am.keeper.GetAnteSurplusSum(ctx) @@ -360,7 +360,9 @@ func (am AppModule) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) []abci.V if evmHooks != nil && evmHooks.OnBalanceChange != nil && (surplusUsei.GT(sdk.ZeroInt()) || surplusWei.GT(sdk.ZeroInt())) { evmModuleAddress := am.keeper.AccountKeeper().GetModuleAddress(types.ModuleName) - tracers.TraceBlockReward(ctx, evmHooks, am.keeper.BankKeeper(), evmModuleAddress, am.keeper.GetEVMAddressOrDefault(ctx, evmModuleAddress), surplusUsei, surplusWei) + evmModuleAddressETH := tracers.GetEVMAddress(ctx, am.keeper, evmModuleAddress) + + tracers.TraceBlockReward(ctx, evmHooks, am.keeper.BankKeeper(), evmModuleAddress, evmModuleAddressETH, surplusUsei, surplusWei) } } am.keeper.SetTxHashesOnHeight(ctx, ctx.BlockHeight(), utils.Filter(utils.Map(evmTxDeferredInfoList, func(i *types.DeferredInfo) common.Hash { return common.BytesToHash(i.TxHash) }), func(h common.Hash) bool { return h.Cmp(ethtypes.EmptyTxsHash) != 0 })) diff --git a/x/evm/tracers/addresses.go b/x/evm/tracers/addresses.go new file mode 100644 index 0000000000..6237972c57 --- /dev/null +++ b/x/evm/tracers/addresses.go @@ -0,0 +1,21 @@ +package tracers + +import ( + storetypes "github.com/cosmos/cosmos-sdk/store/types" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/ethereum/go-ethereum/common" +) + +type EVMKeeper interface { + GetEVMAddressOrDefault(sdk.Context, sdk.AccAddress) common.Address +} + +// GetEVMAddress is a thin wrapper around GetEVMAddressOrDefault in the EVMKeeper interface +// with the important differences that: +// - It does **not** bill gas as this operation is for tracing purposes +// - It also returns the default EVM address if the mapping does not exist +func GetEVMAddress(ctx sdk.Context, keeper EVMKeeper, address sdk.AccAddress) common.Address { + noGasBillingCtx := ctx.WithGasMeter(storetypes.NewNoConsumptionInfiniteGasMeter()) + + return keeper.GetEVMAddressOrDefault(noGasBillingCtx, address) +} diff --git a/x/evm/tracers/balances.go b/x/evm/tracers/balances.go index 563028379d..97e4f99aa0 100644 --- a/x/evm/tracers/balances.go +++ b/x/evm/tracers/balances.go @@ -3,6 +3,7 @@ package tracers import ( "math/big" + storetypes "github.com/cosmos/cosmos-sdk/store/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/tracing" @@ -11,7 +12,7 @@ import ( type BankBalanceKeeper interface { GetBalance(sdk.Context, sdk.AccAddress, string) sdk.Coin - GetWeiBalance(ctx sdk.Context, addr sdk.AccAddress) sdk.Int + GetWeiBalance(sdk.Context, sdk.AccAddress) sdk.Int } // TraceTransactionRewards is a helper function to trace the payment of the transaction rewards @@ -25,9 +26,11 @@ func TraceTransactionRewards( usei sdk.Int, wei sdk.Int, ) { + noGasBillingCtx := ctx.WithGasMeter(storetypes.NewNoConsumptionInfiniteGasMeter()) + value := usei.Mul(state.SdkUseiToSweiMultiplier).Add(wei).BigInt() - newBalance := getEVMBalance(ctx, bankKeeper, toSeiAddr) + newBalance := getEVMBalance(noGasBillingCtx, bankKeeper, toSeiAddr) oldBalance := new(big.Int).Sub(newBalance, value) hooks.OnBalanceChange(toEVMAddr, oldBalance, newBalance, tracing.BalanceIncreaseRewardTransactionFee) @@ -43,14 +46,16 @@ func TraceTransferEVMValue( toEVMAddr common.Address, value *big.Int, ) { + noGasBillingCtx := ctx.WithGasMeter(storetypes.NewNoConsumptionInfiniteGasMeter()) + // From address got value removed from it - newBalance := getEVMBalance(ctx, bankKeeper, fromSeiAddr) + newBalance := getEVMBalance(noGasBillingCtx, bankKeeper, fromSeiAddr) oldBalance := new(big.Int).Add(newBalance, value) hooks.OnBalanceChange(fromEVMAddr, oldBalance, newBalance, tracing.BalanceChangeTransfer) // To received valye from the sender - newBalance = getEVMBalance(ctx, bankKeeper, toSeiAddr) + newBalance = getEVMBalance(noGasBillingCtx, bankKeeper, toSeiAddr) oldBalance = new(big.Int).Sub(newBalance, value) hooks.OnBalanceChange(toEVMAddr, oldBalance, newBalance, tracing.BalanceChangeTransfer) @@ -65,10 +70,12 @@ func TraceBlockReward( usei sdk.Int, wei sdk.Int, ) { + noGasBillingCtx := ctx.WithGasMeter(storetypes.NewNoConsumptionInfiniteGasMeter()) + value := usei.Mul(state.SdkUseiToSweiMultiplier).Add(wei).BigInt() // To received value - newBalance := getEVMBalance(ctx, bankKeeper, toSeiAddr) + newBalance := getEVMBalance(noGasBillingCtx, bankKeeper, toSeiAddr) oldBalance := new(big.Int).Sub(newBalance, value) hooks.OnBalanceChange(toEVMAddr, oldBalance, newBalance, tracing.BalanceIncreaseRewardMineBlock)