This repository has been archived by the owner on Apr 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 558
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(evm): cleanup EVM files (#1582)
* chore(evm): cleanup EVM files * move EVMConfig
- Loading branch information
Showing
10 changed files
with
236 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// 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 | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// The Ethermint library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the Ethermint library. If not, see https://github.com/evmos/ethermint/blob/main/LICENSE | ||
package keeper | ||
|
||
import ( | ||
"math/big" | ||
|
||
errorsmod "cosmossdk.io/errors" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core" | ||
"github.com/ethereum/go-ethereum/core/vm" | ||
"github.com/evmos/ethermint/x/evm/statedb" | ||
"github.com/evmos/ethermint/x/evm/types" | ||
) | ||
|
||
// EVMConfig creates the EVMConfig based on current state | ||
func (k *Keeper) EVMConfig(ctx sdk.Context, proposerAddress sdk.ConsAddress, chainID *big.Int) (*statedb.EVMConfig, error) { | ||
params := k.GetParams(ctx) | ||
ethCfg := params.ChainConfig.EthereumConfig(chainID) | ||
|
||
// get the coinbase address from the block proposer | ||
coinbase, err := k.GetCoinbaseAddress(ctx, proposerAddress) | ||
if err != nil { | ||
return nil, errorsmod.Wrap(err, "failed to obtain coinbase address") | ||
} | ||
|
||
baseFee := k.GetBaseFee(ctx, ethCfg) | ||
return &statedb.EVMConfig{ | ||
Params: params, | ||
ChainConfig: ethCfg, | ||
CoinBase: coinbase, | ||
BaseFee: baseFee, | ||
}, nil | ||
} | ||
|
||
// TxConfig loads `TxConfig` from current transient storage | ||
func (k *Keeper) TxConfig(ctx sdk.Context, txHash common.Hash) statedb.TxConfig { | ||
return statedb.NewTxConfig( | ||
common.BytesToHash(ctx.HeaderHash()), // BlockHash | ||
txHash, // TxHash | ||
uint(k.GetTxIndexTransient(ctx)), // TxIndex | ||
uint(k.GetLogSizeTransient(ctx)), // LogIndex | ||
) | ||
} | ||
|
||
// VMConfig creates an EVM configuration from the debug setting and the extra EIPs enabled on the | ||
// module parameters. The config generated uses the default JumpTable from the EVM. | ||
func (k Keeper) VMConfig(ctx sdk.Context, msg core.Message, cfg *statedb.EVMConfig, tracer vm.EVMLogger) vm.Config { | ||
noBaseFee := true | ||
if types.IsLondon(cfg.ChainConfig, ctx.BlockHeight()) { | ||
noBaseFee = k.feeMarketKeeper.GetParams(ctx).NoBaseFee | ||
} | ||
|
||
var debug bool | ||
if _, ok := tracer.(types.NoOpTracer); !ok { | ||
debug = true | ||
} | ||
|
||
return vm.Config{ | ||
Debug: debug, | ||
Tracer: tracer, | ||
NoBaseFee: noBaseFee, | ||
ExtraEips: cfg.Params.EIPs(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// 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 | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// The Ethermint library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the Ethermint library. If not, see https://github.com/evmos/ethermint/blob/main/LICENSE | ||
package keeper | ||
|
||
import ( | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/core" | ||
"github.com/ethereum/go-ethereum/params" | ||
|
||
errorsmod "cosmossdk.io/errors" | ||
sdkmath "cosmossdk.io/math" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
errortypes "github.com/cosmos/cosmos-sdk/types/errors" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
|
||
"github.com/evmos/ethermint/x/evm/types" | ||
) | ||
|
||
// GetEthIntrinsicGas returns the intrinsic gas cost for the transaction | ||
func (k *Keeper) GetEthIntrinsicGas(ctx sdk.Context, msg core.Message, cfg *params.ChainConfig, isContractCreation bool) (uint64, error) { | ||
height := big.NewInt(ctx.BlockHeight()) | ||
homestead := cfg.IsHomestead(height) | ||
istanbul := cfg.IsIstanbul(height) | ||
|
||
return core.IntrinsicGas(msg.Data(), msg.AccessList(), isContractCreation, homestead, istanbul) | ||
} | ||
|
||
// RefundGas transfers the leftover gas to the sender of the message, caped to half of the total gas | ||
// consumed in the transaction. Additionally, the function sets the total gas consumed to the value | ||
// returned by the EVM execution, thus ignoring the previous intrinsic gas consumed during in the | ||
// AnteHandler. | ||
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()) | ||
|
||
switch remaining.Sign() { | ||
case -1: | ||
// negative refund errors | ||
return errorsmod.Wrapf(types.ErrInvalidRefund, "refunded amount value cannot be negative %d", remaining.Int64()) | ||
case 1: | ||
// positive amount refund | ||
refundedCoins := sdk.Coins{sdk.NewCoin(denom, sdkmath.NewIntFromBigInt(remaining))} | ||
|
||
// 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) | ||
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()) | ||
} | ||
default: | ||
// no refund, consume gas and update the tx gas meter | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// ResetGasMeterAndConsumeGas reset first the gas meter consumed value to zero and set it back to the new value | ||
// 'gasUsed' | ||
func (k *Keeper) ResetGasMeterAndConsumeGas(ctx sdk.Context, gasUsed uint64) { | ||
// reset the gas count | ||
ctx.GasMeter().RefundGas(ctx.GasMeter().GasConsumed(), "reset the gas count") | ||
ctx.GasMeter().ConsumeGas(gasUsed, "apply evm transaction") | ||
} | ||
|
||
// GasToRefund calculates the amount of gas the state machine should refund to the sender. It is | ||
// capped by the refund quotient value. | ||
// Note: do not pass 0 to refundQuotient | ||
func GasToRefund(availableRefund, gasConsumed, refundQuotient uint64) uint64 { | ||
// Apply refund counter | ||
refund := gasConsumed / refundQuotient | ||
if refund > availableRefund { | ||
return availableRefund | ||
} | ||
return refund | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.