Skip to content

Commit

Permalink
Merge branch 'feature/firehose-tracer-at-latest-release-tag' into rel…
Browse files Browse the repository at this point in the history
…ease/firehose
  • Loading branch information
maoueh committed Dec 6, 2024
2 parents 20ebfbc + 341e626 commit f0d1173
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
4 changes: 2 additions & 2 deletions precompiles/bank/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions x/evm/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 }))
Expand Down
21 changes: 21 additions & 0 deletions x/evm/tracers/addresses.go
Original file line number Diff line number Diff line change
@@ -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)
}
17 changes: 12 additions & 5 deletions x/evm/tracers/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit f0d1173

Please sign in to comment.