Skip to content

Commit

Permalink
Add even more unit clarity for base fee and delete code
Browse files Browse the repository at this point in the history
  • Loading branch information
Unique-Divine committed Oct 13, 2024
1 parent 15c6c78 commit a347a72
Show file tree
Hide file tree
Showing 30 changed files with 291 additions and 243 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#2056](https://github.com/NibiruChain/nibiru/pull/2056) - feat(evm): add oracle precompile
- [#2065](https://github.com/NibiruChain/nibiru/pull/2065) - refactor(evm)!: Refactor out dead code from the evm.Params
- [#2073](https://github.com/NibiruChain/nibiru/pull/2073) - fix(evm-keeper): better utilize ERC20 metadata during FunToken creation
- [#2xxx](https://github.com/NibiruChain/nibiru/pull/2xxx) - fix(evm-gas-fees): fix(gas-fees): use effective gas price in RefundGas
- [#2076](https://github.com/NibiruChain/nibiru/pull/2076) - fix(evm-gas-fees):
Use effective gas price in RefundGas and make sure that units are properly
reflected on all occurences of "base fee" in the codebase. This fixes [#2059](https://github.com/NibiruChain/nibiru/issues/2059)
and the [related comments from @Unique-Divine and @berndartmueller](https://github.com/NibiruChain/nibiru/issues/2059#issuecomment-2408625724).


#### Dapp modules: perp, spot, oracle, etc
Expand Down
27 changes: 10 additions & 17 deletions app/evmante/evmante_can_transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,16 @@ import (
// CanTransferDecorator checks if the sender is allowed to transfer funds according to the EVM block
// context rules.
type CanTransferDecorator struct {
evmKeeper EVMKeeper
}

// NewCanTransferDecorator creates a new CanTransferDecorator instance.
func NewCanTransferDecorator(k EVMKeeper) CanTransferDecorator {
return CanTransferDecorator{
evmKeeper: k,
}
*EVMKeeper
}

// AnteHandle creates an EVM from the message and calls the BlockContext CanTransfer function to
// see if the address can execute the transaction.
func (ctd CanTransferDecorator) AnteHandle(
ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler,
) (sdk.Context, error) {
params := ctd.evmKeeper.GetParams(ctx)
ethCfg := evm.EthereumConfig(ctd.evmKeeper.EthChainID(ctx))
params := ctd.GetParams(ctx)
ethCfg := evm.EthereumConfig(ctd.EVMKeeper.EthChainID(ctx))
signer := gethcore.MakeSigner(ethCfg, big.NewInt(ctx.BlockHeight()))

for _, msg := range tx.GetMsgs() {
Expand All @@ -44,7 +37,7 @@ func (ctd CanTransferDecorator) AnteHandle(
"invalid message type %T, expected %T", msg, (*evm.MsgEthereumTx)(nil),
)
}
baseFeeMicronibiPerGas := ctd.evmKeeper.BaseFeeMicronibiPerGas(ctx)
baseFeeMicronibiPerGas := ctd.EVMKeeper.BaseFeeMicronibiPerGas(ctx)
baseFeeWeiPerGas := evm.NativeToWei(baseFeeMicronibiPerGas)

coreMsg, err := msgEthTx.AsMessage(signer, baseFeeWeiPerGas)
Expand All @@ -71,18 +64,18 @@ func (ctd CanTransferDecorator) AnteHandle(

// NOTE: pass in an empty coinbase address and nil tracer as we don't need them for the check below
cfg := &statedb.EVMConfig{
ChainConfig: ethCfg,
Params: params,
CoinBase: gethcommon.Address{},
BaseFee: baseFeeMicronibiPerGas,
ChainConfig: ethCfg,
Params: params,
BlockCoinbase: gethcommon.Address{},
BaseFeeWei: baseFeeMicronibiPerGas,
}

stateDB := statedb.New(
ctx,
ctd.evmKeeper,
ctd.EVMKeeper,
statedb.NewEmptyTxConfig(gethcommon.BytesToHash(ctx.HeaderHash().Bytes())),
)
evmInstance := ctd.evmKeeper.NewEVM(ctx, coreMsg, cfg, evm.NewNoOpTracer(), stateDB)
evmInstance := ctd.EVMKeeper.NewEVM(ctx, coreMsg, cfg, evm.NewNoOpTracer(), stateDB)

// check that caller has enough balance to cover asset transfer for **topmost** call
// NOTE: here the gas consumed is from the context with the infinite gas meter
Expand Down
2 changes: 1 addition & 1 deletion app/evmante/evmante_can_transfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (s *TestSuite) TestCanTransferDecorator() {
s.Run(tc.name, func() {
deps := evmtest.NewTestDeps()
stateDB := deps.StateDB()
anteDec := evmante.NewCanTransferDecorator(&deps.App.AppKeepers.EvmKeeper)
anteDec := evmante.CanTransferDecorator{&deps.App.AppKeepers.EvmKeeper}
tx := tc.txSetup(&deps)

if tc.ctxSetup != nil {
Expand Down
4 changes: 2 additions & 2 deletions app/evmante/evmante_emit_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (

// EthEmitEventDecorator emit events in ante handler in case of tx execution failed (out of block gas limit).
type EthEmitEventDecorator struct {
evmKeeper EVMKeeper
evmKeeper *EVMKeeper
}

// NewEthEmitEventDecorator creates a new EthEmitEventDecorator
func NewEthEmitEventDecorator(k EVMKeeper) EthEmitEventDecorator {
func NewEthEmitEventDecorator(k *EVMKeeper) EthEmitEventDecorator {
return EthEmitEventDecorator{
evmKeeper: k,
}
Expand Down
4 changes: 2 additions & 2 deletions app/evmante/evmante_gas_consume.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ import (
// AnteDecEthGasConsume validates enough intrinsic gas for the transaction and
// gas consumption.
type AnteDecEthGasConsume struct {
evmKeeper EVMKeeper
evmKeeper *EVMKeeper
maxGasWanted uint64
}

// NewAnteDecEthGasConsume creates a new EthGasConsumeDecorator
func NewAnteDecEthGasConsume(
k EVMKeeper,
k *EVMKeeper,
maxGasWanted uint64,
) AnteDecEthGasConsume {
return AnteDecEthGasConsume{
Expand Down
2 changes: 1 addition & 1 deletion app/evmante/evmante_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewAnteHandlerEVM(
NewEthValidateBasicDecorator(&options.EvmKeeper),
NewEthSigVerificationDecorator(&options.EvmKeeper),
NewAnteDecVerifyEthAcc(&options.EvmKeeper, options.AccountKeeper),
NewCanTransferDecorator(&options.EvmKeeper),
CanTransferDecorator{&options.EvmKeeper},
NewAnteDecEthGasConsume(&options.EvmKeeper, options.MaxTxGasWanted),
NewAnteDecEthIncrementSenderSequence(&options.EvmKeeper, options.AccountKeeper),
ante.AnteDecoratorGasWanted{},
Expand Down
4 changes: 2 additions & 2 deletions app/evmante/evmante_increment_sender_seq.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import (

// AnteDecEthIncrementSenderSequence increments the sequence of the signers.
type AnteDecEthIncrementSenderSequence struct {
evmKeeper EVMKeeper
evmKeeper *EVMKeeper
accountKeeper ante.AccountKeeper
}

// NewAnteDecEthIncrementSenderSequence creates a new EthIncrementSenderSequenceDecorator.
func NewAnteDecEthIncrementSenderSequence(k EVMKeeper, ak ante.AccountKeeper) AnteDecEthIncrementSenderSequence {
func NewAnteDecEthIncrementSenderSequence(k *EVMKeeper, ak ante.AccountKeeper) AnteDecEthIncrementSenderSequence {
return AnteDecEthIncrementSenderSequence{
evmKeeper: k,
accountKeeper: ak,
Expand Down
10 changes: 5 additions & 5 deletions app/evmante/evmante_mempool_fees.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ var _ sdk.AnteDecorator = MempoolGasPriceDecorator{}
// is rejected. This applies to CheckTx only.
// If fee is high enough, then call next AnteHandler
type MempoolGasPriceDecorator struct {
evmKeeper EVMKeeper
evmKeeper *EVMKeeper
}

// NewMempoolGasPriceDecorator creates a new MinGasPriceDecorator instance used only for
// Ethereum transactions.
func NewMempoolGasPriceDecorator(k EVMKeeper) MempoolGasPriceDecorator {
func NewMempoolGasPriceDecorator(k *EVMKeeper) MempoolGasPriceDecorator {
return MempoolGasPriceDecorator{
evmKeeper: k,
}
Expand All @@ -40,13 +40,13 @@ func (d MempoolGasPriceDecorator) AnteHandle(

minGasPrice := ctx.MinGasPrices().AmountOf(evm.EVMBankDenom)
baseFeeMicronibi := d.evmKeeper.BaseFeeMicronibiPerGas(ctx)
baseFeeDec := math.LegacyNewDecFromBigInt(baseFeeMicronibi)
baseFeeMicronibiDec := math.LegacyNewDecFromBigInt(baseFeeMicronibi)

// if MinGasPrices is not set, skip the check
if minGasPrice.IsZero() {
return next(ctx, tx, simulate)
} else if minGasPrice.LT(baseFeeDec) {
minGasPrice = baseFeeDec
} else if minGasPrice.LT(baseFeeMicronibiDec) {
minGasPrice = baseFeeMicronibiDec
}

for _, msg := range tx.GetMsgs() {
Expand Down
4 changes: 2 additions & 2 deletions app/evmante/evmante_setup_ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
// EthSetupContextDecorator is adapted from SetUpContextDecorator from cosmos-sdk, it ignores gas consumption
// by setting the gas meter to infinite
type EthSetupContextDecorator struct {
evmKeeper EVMKeeper
evmKeeper *EVMKeeper
}

func NewEthSetUpContextDecorator(k EVMKeeper) EthSetupContextDecorator {
func NewEthSetUpContextDecorator(k *EVMKeeper) EthSetupContextDecorator {
return EthSetupContextDecorator{
evmKeeper: k,
}
Expand Down
4 changes: 2 additions & 2 deletions app/evmante/evmante_sigverify.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (

// EthSigVerificationDecorator validates an ethereum signatures
type EthSigVerificationDecorator struct {
evmKeeper EVMKeeper
evmKeeper *EVMKeeper
}

// NewEthSigVerificationDecorator creates a new EthSigVerificationDecorator
func NewEthSigVerificationDecorator(k EVMKeeper) EthSigVerificationDecorator {
func NewEthSigVerificationDecorator(k *EVMKeeper) EthSigVerificationDecorator {
return EthSigVerificationDecorator{
evmKeeper: k,
}
Expand Down
4 changes: 2 additions & 2 deletions app/evmante/evmante_validate_basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (

// EthValidateBasicDecorator is adapted from ValidateBasicDecorator from cosmos-sdk, it ignores ErrNoSignatures
type EthValidateBasicDecorator struct {
evmKeeper EVMKeeper
evmKeeper *EVMKeeper
}

// NewEthValidateBasicDecorator creates a new EthValidateBasicDecorator
func NewEthValidateBasicDecorator(k EVMKeeper) EthValidateBasicDecorator {
func NewEthValidateBasicDecorator(k *EVMKeeper) EthValidateBasicDecorator {
return EthValidateBasicDecorator{
evmKeeper: k,
}
Expand Down
4 changes: 2 additions & 2 deletions app/evmante/evmante_verify_eth_acc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import (

// AnteDecVerifyEthAcc validates an account balance checks
type AnteDecVerifyEthAcc struct {
evmKeeper EVMKeeper
evmKeeper *EVMKeeper
accountKeeper evm.AccountKeeper
}

// NewAnteDecVerifyEthAcc creates a new EthAccountVerificationDecorator
func NewAnteDecVerifyEthAcc(k EVMKeeper, ak evm.AccountKeeper) AnteDecVerifyEthAcc {
func NewAnteDecVerifyEthAcc(k *EVMKeeper, ak evm.AccountKeeper) AnteDecVerifyEthAcc {
return AnteDecVerifyEthAcc{
evmKeeper: k,
accountKeeper: ak,
Expand Down
26 changes: 2 additions & 24 deletions app/evmante/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,11 @@
package evmante

import (
"math/big"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/vm"

"github.com/NibiruChain/nibiru/v2/x/evm"
evmkeeper "github.com/NibiruChain/nibiru/v2/x/evm/keeper"
"github.com/NibiruChain/nibiru/v2/x/evm/statedb"
"github.com/cosmos/cosmos-sdk/types/tx"
)

// EVMKeeper defines the expected keeper interface used on the AnteHandler
type EVMKeeper interface {
statedb.Keeper

NewEVM(ctx sdk.Context, msg core.Message, cfg *statedb.EVMConfig, tracer vm.EVMLogger, stateDB vm.StateDB) *vm.EVM
DeductTxCostsFromUserBalance(ctx sdk.Context, fees sdk.Coins, from common.Address) error
GetEvmGasBalance(ctx sdk.Context, addr common.Address) *big.Int
ResetTransientGasUsed(ctx sdk.Context)
GetParams(ctx sdk.Context) evm.Params

EVMState() evmkeeper.EvmState
EthChainID(ctx sdk.Context) *big.Int
BaseFeeMicronibiPerGas(ctx sdk.Context) *big.Int
}
type EVMKeeper = evmkeeper.Keeper

type protoTxProvider interface {
GetProtoTx() *tx.Tx
Expand Down
14 changes: 7 additions & 7 deletions eth/rpc/backend/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,13 +316,13 @@ func (b *Backend) HeaderByNumber(blockNum rpc.BlockNumber) (*gethcore.Header, er
b.logger.Debug("HeaderByNumber BlockBloom failed", "height", resBlock.Block.Height)
}

baseFee, err := b.BaseFee(blockRes)
baseFeeWei, err := b.BaseFeeWei(blockRes)
if err != nil {
// handle the error for pruned node.
b.logger.Error("failed to fetch Base Fee from prunned block. Check node prunning configuration", "height", resBlock.Block.Height, "error", err)
}

ethHeader := rpc.EthHeaderFromTendermint(resBlock.Block.Header, bloom, baseFee)
ethHeader := rpc.EthHeaderFromTendermint(resBlock.Block.Header, bloom, baseFeeWei)
return ethHeader, nil
}

Expand Down Expand Up @@ -352,7 +352,7 @@ func (b *Backend) RPCBlockFromTendermintBlock(
ethRPCTxs := []interface{}{}
block := resBlock.Block

baseFee, err := b.BaseFee(blockRes)
baseFeeWei, err := b.BaseFeeWei(blockRes)
if err != nil {
// handle the error for pruned node.
b.logger.Error("failed to fetch Base Fee from prunned block. Check node prunning configuration", "height", block.Height, "error", err)
Expand All @@ -374,7 +374,7 @@ func (b *Backend) RPCBlockFromTendermintBlock(
gethcommon.BytesToHash(block.Hash()),
height,
index,
baseFee,
baseFeeWei,
b.chainID,
)
if err != nil {
Expand Down Expand Up @@ -434,7 +434,7 @@ func (b *Backend) RPCBlockFromTendermintBlock(
formattedBlock := rpc.FormatBlock(
block.Header, block.Size(),
gasLimit, new(big.Int).SetUint64(gasUsed),
ethRPCTxs, bloom, validatorAddr, baseFee,
ethRPCTxs, bloom, validatorAddr, baseFeeWei,
)
return formattedBlock, nil
}
Expand Down Expand Up @@ -471,13 +471,13 @@ func (b *Backend) EthBlockFromTendermintBlock(
b.logger.Debug("HeaderByNumber BlockBloom failed", "height", height)
}

baseFee, err := b.BaseFee(blockRes)
baseFeeWei, err := b.BaseFeeWei(blockRes)
if err != nil {
// handle error for pruned node and log
b.logger.Error("failed to fetch Base Fee from prunned block. Check node prunning configuration", "height", height, "error", err)
}

ethHeader := rpc.EthHeaderFromTendermint(block.Header, bloom, baseFee)
ethHeader := rpc.EthHeaderFromTendermint(block.Header, bloom, baseFeeWei)
msgs := b.EthMsgsFromTendermintBlock(resBlock, blockRes)

txs := make([]*gethcore.Transaction, len(msgs))
Expand Down
11 changes: 4 additions & 7 deletions eth/rpc/backend/chain_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,14 @@ func (b *Backend) ChainConfig() *params.ChainConfig {
return evm.EthereumConfig(b.chainID)
}

// BaseFee returns the base fee tracked by the Fee Market module.
// BaseFeeWei returns the EIP-1559 base fee.
// If the base fee is not enabled globally, the query returns nil.
func (b *Backend) BaseFee(
func (b *Backend) BaseFeeWei(
blockRes *tmrpctypes.ResultBlockResults,
) (baseFee *big.Int, err error) {
// return BaseFee if feemarket is enabled
) (baseFeeWei *big.Int, err error) {
res, err := b.queryClient.BaseFee(rpc.NewContextWithHeight(blockRes.Height), &evm.QueryBaseFeeRequest{})
if err != nil || res.BaseFee == nil {
baseFee = nil
// TODO: feat: dynamic fee handling on events
return baseFee, nil
return nil, nil
}
return res.BaseFee.BigInt(), nil
}
Expand Down
4 changes: 2 additions & 2 deletions eth/rpc/backend/chain_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ func (s *BackendSuite) TestChainConfig() {
func (s *BackendSuite) TestBaseFee() {
resBlock, err := s.backend.TendermintBlockResultByNumber(transferTxBlockNumber.TmHeight())
s.Require().NoError(err)
baseFee, err := s.backend.BaseFee(resBlock)
baseFeeWei, err := s.backend.BaseFeeWei(resBlock)
s.Require().NoError(err)
s.Require().Equal(evm.BASE_FEE_MICRONIBI, baseFee)
s.Require().Equal(evm.BASE_FEE_WEI, baseFeeWei)
}

func (s *BackendSuite) TestCurrentHeader() {
Expand Down
Loading

0 comments on commit a347a72

Please sign in to comment.