Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add extensions logic #22

Merged
merged 2 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion x/evm/keeper/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package keeper
import (
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/params"

Expand Down Expand Up @@ -46,6 +47,10 @@ func (k *Keeper) GetEthIntrinsicGas(ctx sdk.Context, msg core.Message, cfg *para
func (k *Keeper) RefundGas(ctx sdk.Context, msg core.Message, leftoverGas uint64, denom string) error {
// Return EVM tokens for remaining gas, exchanged at the original rate.
remaining := new(big.Int).Mul(new(big.Int).SetUint64(leftoverGas), msg.GasPrice())
feePayer := msg.From().Bytes()
if m,ok := msg.(types.Message); ok {
feePayer = common.HexToAddress(m.FeePayer).Bytes()
}

switch remaining.Sign() {
case -1:
Expand All @@ -57,7 +62,7 @@ func (k *Keeper) RefundGas(ctx sdk.Context, msg core.Message, leftoverGas uint64

// refund to sender from the fee collector module account, which is the escrow account in charge of collecting tx fees

err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, authtypes.FeeCollectorName, msg.From().Bytes(), refundedCoins)
err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, authtypes.FeeCollectorName, feePayer, refundedCoins)
if err != nil {
err = errorsmod.Wrapf(errortypes.ErrInsufficientFunds, "fee collector account failed to refund fees: %s", err.Error())
return errorsmod.Wrapf(err, "failed to refund %d leftover gas (%s)", leftoverGas, refundedCoins.String())
Expand Down
12 changes: 11 additions & 1 deletion x/evm/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Evmos Foundation
// Package keeper Copyright 2021 Evmos Foundation
// This file is part of Evmos' Ethermint library.
//
// The Ethermint library is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -78,6 +78,9 @@ type Keeper struct {
evmConstructor evm.Constructor
// Legacy subspace
ss paramstypes.Subspace

// options for the EVM
opts types.Options
}

// NewKeeper generates new evm module keeper
Expand Down Expand Up @@ -118,6 +121,7 @@ func NewKeeper(
evmConstructor: evmConstructor,
tracer: tracer,
ss: ss,
opts: types.DefaultOptions(),
}
}

Expand All @@ -126,6 +130,12 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", types.ModuleName)
}

// WithOptions sets the options to the keeper
func (k *Keeper) WithOptions(opts types.Options) *Keeper{
k.opts = opts
return k
}

// WithChainID sets the chain id to the local variable in the keeper
func (k *Keeper) WithChainID(ctx sdk.Context) {
chainID, err := ethermint.ParseChainID(ctx.ChainID())
Expand Down
7 changes: 3 additions & 4 deletions x/evm/keeper/state_transition.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Evmos Foundation
// Package keeper Copyright 2021 Evmos Foundation
// This file is part of Evmos' Ethermint library.
//
// The Ethermint library is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -44,7 +44,6 @@ import (
// NOTE: the RANDOM opcode is currently not supported since it requires
// RANDAO implementation. See https://github.com/evmos/ethermint/pull/1520#pullrequestreview-1200504697
// for more information.

func (k *Keeper) NewEVM(
ctx sdk.Context,
msg core.Message,
Expand All @@ -53,8 +52,8 @@ func (k *Keeper) NewEVM(
stateDB vm.StateDB,
) evm.EVM {
blockCtx := vm.BlockContext{
CanTransfer: core.CanTransfer,
Transfer: core.Transfer,
CanTransfer: k.opts.CanTransfer,
Transfer: k.opts.Transfer,
GetHash: k.GetHashFn(ctx),
Coinbase: cfg.CoinBase,
GasLimit: ethermint.BlockGasLimit(ctx),
Expand Down
28 changes: 28 additions & 0 deletions x/evm/types/extensions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package types

import (
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/vm"
)

var _ core.Message = (*Message)(nil)

// Options for the EVM module
type Options struct {
CanTransfer vm.CanTransferFunc
Transfer vm.TransferFunc
}

// DefaultOptions for the EVM module
func DefaultOptions() Options {
return Options{
CanTransfer: core.CanTransfer,
Transfer: core.Transfer,
}
}

// Message wrap the core.Message interface.
type Message struct {
core.Message
FeePayer string
}
9 changes: 8 additions & 1 deletion x/evm/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,14 @@ func (msg MsgEthereumTx) AsMessage(signer ethtypes.Signer, baseFee *big.Int) (co
false,
)

return ethMsg, nil
if msg.FeePayer == "" {
return ethMsg, nil
}

return Message{
Message: ethMsg,
FeePayer: msg.FeePayer,
}, nil
}

// GetSender extracts the sender address from the signature values using the latest signer for the given chainID.
Expand Down
Loading