Skip to content

Commit

Permalink
add extensions logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamer-zq committed Jul 12, 2024
1 parent 3cea3d7 commit 7958e26
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 7 deletions.
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 {

Check failure on line 51 in x/evm/keeper/gas.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

File is not `gofumpt`-ed (gofumpt)
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{

Check failure on line 134 in x/evm/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

File is not `gofumpt`-ed (gofumpt)
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
}

Check failure on line 28 in x/evm/types/extensions.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

File is not `gofumpt`-ed (gofumpt)
5 changes: 4 additions & 1 deletion x/evm/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,10 @@ func (msg MsgEthereumTx) AsMessage(signer ethtypes.Signer, baseFee *big.Int) (co
false,
)

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

0 comments on commit 7958e26

Please sign in to comment.