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

EIP-1559 - Dynamic Base Fee #1843

Merged
merged 67 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
b199ea7
initial impl
jewei1997 Sep 5, 2024
766313d
remove libjs changes
jewei1997 Sep 5, 2024
ff28228
fix some broken tests
jewei1997 Sep 5, 2024
6bbb91e
fix some more tests
jewei1997 Sep 5, 2024
0190d40
Merge branch 'main' into dynamic-gas-fee-eip-1559
jewei1997 Sep 5, 2024
243446f
fix some more tests
jewei1997 Sep 5, 2024
83250ff
fix test
jewei1997 Sep 5, 2024
0d1e2c1
fix test
jewei1997 Sep 5, 2024
412b2c0
fix test
jewei1997 Sep 5, 2024
7c54337
fix test
jewei1997 Sep 5, 2024
61d1d03
fix test
jewei1997 Sep 5, 2024
531b14d
change BaseFee Set and Get to use sdk.Dec instead of float
jewei1997 Sep 6, 2024
1eda2f1
fix test
jewei1997 Sep 6, 2024
dcde85b
fix test
jewei1997 Sep 6, 2024
578adc4
reset base fee after fee_test
jewei1997 Sep 6, 2024
7318c66
Merge branch 'main' into dynamic-gas-fee-eip-1559
jewei1997 Sep 6, 2024
f528a0c
don't burn base fee
jewei1997 Sep 7, 2024
ca01e3f
fix
jewei1997 Sep 7, 2024
53cbb7d
fix
jewei1997 Sep 7, 2024
8f3736f
fix
jewei1997 Sep 7, 2024
fe68b5e
fix
jewei1997 Sep 7, 2024
7f2f2ad
fix
jewei1997 Sep 7, 2024
6190a52
try to fix blocktests
jewei1997 Sep 7, 2024
58d9ec3
fix block test and hopefully dont break other tests
jewei1997 Sep 9, 2024
cac5812
skip some failing block tests
jewei1997 Sep 9, 2024
3ac31f9
skip some failing block tests
jewei1997 Sep 9, 2024
5e7a815
Merge branch 'main' into dynamic-gas-fee-eip-1559
jewei1997 Sep 9, 2024
b420f5b
fix test
jewei1997 Sep 9, 2024
ac48cf3
applyEVMMessageNoBaseFee for CallEVM
jewei1997 Sep 9, 2024
47d6dd2
cleanup prints
jewei1997 Sep 10, 2024
89c4ded
comment out failing wasmd precompile test
jewei1997 Sep 10, 2024
84ede69
fix
jewei1997 Sep 10, 2024
880645c
adjust block base fee on next height
jewei1997 Sep 10, 2024
75b229a
fix
jewei1997 Sep 10, 2024
6930864
fix wasm integration precompile test
jewei1997 Sep 10, 2024
ebc0b8f
Merge branch 'main' into dynamic-gas-fee-eip-1559
jewei1997 Sep 11, 2024
c0c41ca
fix loadtest client
jewei1997 Sep 11, 2024
77964a1
update params.proto
jewei1997 Sep 11, 2024
4873539
update params.pb.go
jewei1997 Sep 11, 2024
b63c46b
new params for upward and downward adjustment
jewei1997 Sep 12, 2024
a42381f
incorporate new params
jewei1997 Sep 13, 2024
261d188
Merge branch 'main' into dynamic-gas-fee-eip-1559
jewei1997 Sep 13, 2024
21e4327
fix
jewei1997 Sep 13, 2024
c99e19b
fix
jewei1997 Sep 13, 2024
7c7e21c
fix failing unit tests
jewei1997 Sep 13, 2024
f9522d2
Merge branch 'main' into dynamic-gas-fee-eip-1559
jewei1997 Sep 13, 2024
89db5a4
add migration handlers
jewei1997 Sep 14, 2024
bf0b780
bump consensus version
jewei1997 Sep 14, 2024
b3bfc76
add upgrade version for testing purposes (revert later)
jewei1997 Sep 14, 2024
30a474d
add upgrade version for testing purposes (revert later)
jewei1997 Sep 14, 2024
32b64b3
Revert "add upgrade version for testing purposes (revert later)"
jewei1997 Sep 14, 2024
3ac86c8
Revert "add upgrade version for testing purposes (revert later)"
jewei1997 Sep 14, 2024
0a8f9a4
some fixes, check if all tests still pass
jewei1997 Sep 16, 2024
54b1d9e
address comments
jewei1997 Sep 16, 2024
a28d553
address more comments
jewei1997 Sep 16, 2024
df291ae
bump geth version
jewei1997 Sep 16, 2024
efa600e
Merge branch 'main' into dynamic-gas-fee-eip-1559
jewei1997 Sep 18, 2024
a4b9102
Merge branch 'main' into dynamic-gas-fee-eip-1559
jewei1997 Sep 24, 2024
9d4b61a
fix previous commit - regenerate params proto
jewei1997 Sep 24, 2024
5a4accf
fix tests
jewei1997 Sep 24, 2024
754ab16
Merge branch 'main' into dynamic-gas-fee-eip-1559
jewei1997 Sep 25, 2024
181d61f
bump geth after merge and max up/down 0%
jewei1997 Sep 25, 2024
4b4ae2e
fix TestAdjustBaseFeePerGas
jewei1997 Sep 25, 2024
44a85a7
revert burn base fee changes on geth
jewei1997 Sep 26, 2024
588c382
In CallEVM, set baseFeeCap to 0
jewei1997 Sep 26, 2024
0b9336e
merge in sei-tendermint to pass compat check
jewei1997 Sep 26, 2024
5c0083f
remove get coinbases for replay verify account
jewei1997 Sep 26, 2024
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
9 changes: 8 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1593,8 +1593,15 @@ func (app *App) ProcessBlock(ctx sdk.Context, txs [][]byte, req BlockProcessRequ
lazyWriteEvents := app.BankKeeper.WriteDeferredBalances(ctx)
events = append(events, lazyWriteEvents...)

// Sum up total used per block
blockTotalGasUsed := int64(0)
for _, txResult := range txResults {
blockTotalGasUsed += txResult.GasUsed
}

endBlockResp := app.EndBlock(ctx, abci.RequestEndBlock{
Height: req.GetHeight(),
Height: req.GetHeight(),
BlockGasUsed: blockTotalGasUsed,
})

events = append(events, endBlockResp.Events...)
Expand Down
2 changes: 1 addition & 1 deletion evmrpc/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func EncodeTmBlock(
txHash := common.HexToHash(block.Block.DataHash.String())
resultHash := common.HexToHash(block.Block.LastResultsHash.String())
miner := common.HexToAddress(block.Block.ProposerAddress.String())
baseFeePerGas := k.GetBaseFeePerGas(ctx).TruncateInt().BigInt()
baseFeePerGas := k.GetDynamicBaseFeePerGas(ctx).TruncateInt().BigInt()
var blockGasUsed int64
chainConfig := types.DefaultChainConfig().EthereumConfig(k.ChainID(ctx))
transactions := []interface{}{}
Expand Down
29 changes: 24 additions & 5 deletions evmrpc/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package evmrpc
import (
"context"
"errors"
"fmt"
"math/big"
"slices"
"time"
Expand Down Expand Up @@ -80,16 +81,27 @@ func (i *InfoAPI) GasPrice(ctx context.Context) (result *hexutil.Big, returnErr
}
if len(feeHist.Reward) == 0 || len(feeHist.Reward[0]) == 0 {
// if there is no EVM tx in the most recent block, return the minimum fee param
return (*hexutil.Big)(i.keeper.GetMinimumFeePerGas(i.ctxProvider(LatestCtxHeight)).TruncateInt().BigInt()), nil
baseFee := i.keeper.GetMinimumFeePerGas(i.ctxProvider(LatestCtxHeight)).TruncateInt().BigInt()
fmt.Println("JEREMYDEBUG: eth_gasPrice, min base fee = ", baseFee)
return (*hexutil.Big)(baseFee), nil
}
return (*hexutil.Big)(new(big.Int).Add(
baseFee := i.keeper.GetBaseFeePerGas(i.ctxProvider(LatestCtxHeight)).TruncateInt().BigInt()
fmt.Println("JEREMYDEBUG: eth_gasPrice, baseFee = ", baseFee)
reward := feeHist.Reward[0][0].ToInt()
fmt.Println("JEREMYDEBUG: eth_gasPrice, reward = ", reward)
sum := new(big.Int).Add(
feeHist.Reward[0][0].ToInt(),
i.keeper.GetBaseFeePerGas(i.ctxProvider(LatestCtxHeight)).TruncateInt().BigInt(),
)), nil
baseFee,
)
fmt.Println("JEREMYDEBUG: eth_gasPrice, sum = ", sum)
res := (*hexutil.Big)(sum)
fmt.Println("JEREMYDEBUG: eth_gasPrice, res = ", res)
return res, nil
}

// lastBlock is inclusive
func (i *InfoAPI) FeeHistory(ctx context.Context, blockCount math.HexOrDecimal64, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (result *FeeHistoryResult, returnErr error) {
fmt.Println("rewardPercentages = ", rewardPercentiles)
startTime := time.Now()
defer recordMetrics("eth_feeHistory", i.connectionType, startTime, returnErr == nil)
result = &FeeHistoryResult{}
Expand Down Expand Up @@ -159,6 +171,8 @@ func (i *InfoAPI) FeeHistory(ctx context.Context, blockCount math.HexOrDecimal64
// the block has been pruned
continue
}
// baseFee = big.NewInt(100000000000)
fmt.Println("in feeHistory, got baseFee = ", baseFee)
result.BaseFee = append(result.BaseFee, (*hexutil.Big)(baseFee))
height := blockNum
block, err := blockByNumber(ctx, i.tmClient, &height)
Expand All @@ -172,6 +186,8 @@ func (i *InfoAPI) FeeHistory(ctx context.Context, blockCount math.HexOrDecimal64
}
result.Reward = append(result.Reward, rewards)
}
fmt.Println("result.BaseFee = ", result.BaseFee)
fmt.Println("result.Reward = ", result.Reward)
return result, nil
}

Expand All @@ -195,7 +211,10 @@ func (i *InfoAPI) safeGetBaseFee(targetHeight int64) (res *big.Int) {
res = nil
}
}()
res = i.keeper.GetBaseFeePerGas(i.ctxProvider(targetHeight)).BigInt()
baseFee := i.keeper.GetDynamicBaseFeePerGas(i.ctxProvider(targetHeight))
fmt.Println("safeGetBaseFee = ", baseFee)
res = baseFee.TruncateInt().BigInt()
fmt.Println("res = ", res)
return
}

Expand Down
18 changes: 10 additions & 8 deletions evmrpc/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package evmrpc_test

import (
"errors"
"fmt"
"math/big"
"testing"

Expand Down Expand Up @@ -58,11 +59,10 @@ func TestCoinbase(t *testing.T) {
}

func TestGasPrice(t *testing.T) {
Ctx = Ctx.WithBlockHeight(1)
resObj := sendRequestGood(t, "gasPrice")
Ctx = Ctx.WithBlockHeight(8)
result := resObj["result"].(string)
require.Equal(t, "0xa", result)
require.Equal(t, "0x174876e800", result)
}

func TestFeeHistory(t *testing.T) {
Expand All @@ -81,12 +81,12 @@ func TestFeeHistory(t *testing.T) {
Ctx = Ctx.WithBlockHeight(1) // Simulate context with a specific block height

testCases := []feeHistoryTestCase{
{name: "Valid request by number", blockCount: 1, lastBlock: "0x8", rewardPercentiles: []interface{}{0.5}, expectedOldest: "0x1", expectedReward: "0xa", expectedBaseFee: "0x0", expectedGasUsed: 0.5},
{name: "Valid request by latest", blockCount: 1, lastBlock: "latest", rewardPercentiles: []interface{}{0.5}, expectedOldest: "0x1", expectedReward: "0xa", expectedBaseFee: "0x0", expectedGasUsed: 0.5},
{name: "Valid request by earliest", blockCount: 1, lastBlock: "earliest", rewardPercentiles: []interface{}{0.5}, expectedOldest: "0x1", expectedReward: "0xa", expectedBaseFee: "0x0", expectedGasUsed: 0.5},
{name: "Request on the same block", blockCount: 1, lastBlock: "0x1", rewardPercentiles: []interface{}{0.5}, expectedOldest: "0x1", expectedReward: "0xa", expectedBaseFee: "0x0", expectedGasUsed: 0.5},
{name: "Request on future block", blockCount: 1, lastBlock: "0x9", rewardPercentiles: []interface{}{0.5}, expectedOldest: "0x1", expectedReward: "0xa", expectedBaseFee: "0x0", expectedGasUsed: 0.5},
{name: "Block count truncates", blockCount: 1025, lastBlock: "latest", rewardPercentiles: []interface{}{25}, expectedOldest: "0x1", expectedReward: "0xa", expectedBaseFee: "0x0", expectedGasUsed: 0.5},
{name: "Valid request by number", blockCount: 1, lastBlock: "0x8", rewardPercentiles: []interface{}{0.5}, expectedOldest: "0x1", expectedReward: "0x0", expectedBaseFee: "0x174876e800", expectedGasUsed: 0.5},
{name: "Valid request by latest", blockCount: 1, lastBlock: "latest", rewardPercentiles: []interface{}{0.5}, expectedOldest: "0x1", expectedReward: "0x0", expectedBaseFee: "0x174876e800", expectedGasUsed: 0.5},
{name: "Valid request by earliest", blockCount: 1, lastBlock: "earliest", rewardPercentiles: []interface{}{0.5}, expectedOldest: "0x1", expectedReward: "0x0", expectedBaseFee: "0x174876e800", expectedGasUsed: 0.5},
{name: "Request on the same block", blockCount: 1, lastBlock: "0x1", rewardPercentiles: []interface{}{0.5}, expectedOldest: "0x1", expectedReward: "0x0", expectedBaseFee: "0x174876e800", expectedGasUsed: 0.5},
{name: "Request on future block", blockCount: 1, lastBlock: "0x9", rewardPercentiles: []interface{}{0.5}, expectedOldest: "0x1", expectedReward: "0x0", expectedBaseFee: "0x174876e800", expectedGasUsed: 0.5},
{name: "Block count truncates", blockCount: 1025, lastBlock: "latest", rewardPercentiles: []interface{}{25}, expectedOldest: "0x1", expectedReward: "0x0", expectedBaseFee: "0x174876e800", expectedGasUsed: 0.5},
{name: "Too many percentiles", blockCount: 10, lastBlock: "latest", rewardPercentiles: make([]interface{}, 101), expectedError: errors.New("rewardPercentiles length must be less than or equal to 100")},
{name: "Invalid percentiles order", blockCount: 10, lastBlock: "latest", rewardPercentiles: []interface{}{99, 1}, expectedError: errors.New("invalid reward percentiles: must be ascending and between 0 and 100")},
}
Expand All @@ -103,6 +103,8 @@ func TestFeeHistory(t *testing.T) {
require.False(t, errorExists)

resObj = resObj["result"].(map[string]interface{})
fmt.Println("resObj = ", resObj)

require.Equal(t, tc.expectedOldest, resObj["oldestBlock"].(string))
rewards, ok := resObj["reward"].([]interface{})

Expand Down
2 changes: 1 addition & 1 deletion evmrpc/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ func generateTxData() {
TxHashHex: tx1.Hash().Hex(),
GasUsed: 55,
Status: 0,
EffectiveGasPrice: 10,
EffectiveGasPrice: 100000000000,
Logs: []*types.Log{{
Address: "0x1111111111111111111111111111111111111111",
Topics: []string{"0x1111111111111111111111111111111111111111111111111111111111111111", "0x1111111111111111111111111111111111111111111111111111111111111112"},
Expand Down
2 changes: 1 addition & 1 deletion evmrpc/simulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ func (b *Backend) getHeader(blockNumber *big.Int) *ethtypes.Header {
header := &ethtypes.Header{
Difficulty: common.Big0,
Number: blockNumber,
BaseFee: b.keeper.GetBaseFeePerGas(b.ctxProvider(LatestCtxHeight)).BigInt(),
BaseFee: b.keeper.GetDynamicBaseFeePerGas(b.ctxProvider(LatestCtxHeight)).BigInt(),
GasLimit: b.config.GasCap,
Time: uint64(time.Now().Unix()),
ExcessBlobGas: &zeroExcessBlobGas,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ replace (
github.com/sei-protocol/sei-db => github.com/sei-protocol/sei-db v0.0.43
// Latest goleveldb is broken, we have to stick to this version
github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
github.com/tendermint/tendermint => github.com/sei-protocol/sei-tendermint v0.3.8
github.com/tendermint/tendermint => github.com/sei-protocol/sei-tendermint v0.3.7-0.20240904023453-8f9a2036b3e9
github.com/tendermint/tm-db => github.com/sei-protocol/tm-db v0.0.4
google.golang.org/grpc => google.golang.org/grpc v1.33.2
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1355,8 +1355,8 @@ github.com/sei-protocol/sei-iavl v0.1.9 h1:y4mVYftxLNRs6533zl7N0/Ch+CzRQc04JDfHo
github.com/sei-protocol/sei-iavl v0.1.9/go.mod h1:7PfkEVT5dcoQE+s/9KWdoXJ8VVVP1QpYYPLdxlkSXFk=
github.com/sei-protocol/sei-ibc-go/v3 v3.3.2 h1:BaMZ6gjwqe3R/5dLmcJ1TkSZ3omcWy2TjaAZAeOJH44=
github.com/sei-protocol/sei-ibc-go/v3 v3.3.2/go.mod h1:VwB/vWu4ysT5DN2aF78d17LYmx3omSAdq6gpKvM7XRA=
github.com/sei-protocol/sei-tendermint v0.3.8 h1:9o+A3tL6q1ki++dLng/J8MHHiT6y3l7D4Ir2UIQSkAQ=
github.com/sei-protocol/sei-tendermint v0.3.8/go.mod h1:4LSlJdhl3nf3OmohliwRNUFLOB1XWlrmSodrIP7fLh4=
github.com/sei-protocol/sei-tendermint v0.3.7-0.20240904023453-8f9a2036b3e9 h1:Q4xhlpnA9qcURKgvyP2hVYcSRI+rzZVZwUY1/39Tk/E=
github.com/sei-protocol/sei-tendermint v0.3.7-0.20240904023453-8f9a2036b3e9/go.mod h1:4LSlJdhl3nf3OmohliwRNUFLOB1XWlrmSodrIP7fLh4=
github.com/sei-protocol/sei-tm-db v0.0.5 h1:3WONKdSXEqdZZeLuWYfK5hP37TJpfaUa13vAyAlvaQY=
github.com/sei-protocol/sei-tm-db v0.0.5/go.mod h1:Cpa6rGyczgthq7/0pI31jys2Fw0Nfrc+/jKdP1prVqY=
github.com/sei-protocol/sei-wasmd v0.2.4-0.20240816184629-eb6d20caf603 h1:aOUWB+ABNMdAjghn/PqCKDuz5WTwfoYfre5I/rtOnKo=
Expand Down
2 changes: 1 addition & 1 deletion x/evm/ante/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (fc EVMFeeCheckDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate b

// fee per gas to be burnt
jewei1997 marked this conversation as resolved.
Show resolved Hide resolved
func (fc EVMFeeCheckDecorator) getBaseFee(ctx sdk.Context) *big.Int {
return fc.evmKeeper.GetBaseFeePerGas(ctx).TruncateInt().BigInt()
return fc.evmKeeper.GetDynamicBaseFeePerGas(ctx).TruncateInt().BigInt()
}

// lowest allowed fee per gas
Expand Down
44 changes: 44 additions & 0 deletions x/evm/keeper/fee.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package keeper

import (
"encoding/binary"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/sei-protocol/sei-chain/x/evm/types"
)

// TODO: make this a param
const MaxBaseFeeChange = 0.125

// eip-1559 adjustment
func (k *Keeper) AdjustDynamicBaseFeePerGas(ctx sdk.Context, blockGasUsed uint64) {
currentBaseFee := k.GetDynamicBaseFeePerGas(ctx).MustFloat64()
minimumFeePerGas := k.GetParams(ctx).MinimumFeePerGas.MustFloat64()
blockGasLimit := ctx.ConsensusParams().Block.MaxGas
blockFullness := float64(blockGasUsed) / float64(blockGasLimit)
Fixed Show fixed Hide fixed
adjustmentFactor := MaxBaseFeeChange * (blockFullness - 0.5) / 0.5 // range between -12.5% to 12.5%
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
newBaseFee := float64(currentBaseFee) * (1 + adjustmentFactor)
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
if newBaseFee < minimumFeePerGas {
newBaseFee = minimumFeePerGas
}
k.SetDynamicBaseFeePerGas(ctx, uint64(newBaseFee))
}

func (k *Keeper) GetDynamicBaseFeePerGas(ctx sdk.Context) sdk.Dec {
h := make([]byte, 8)
binary.BigEndian.PutUint64(h, uint64(ctx.BlockHeight()))
bz := k.PrefixStore(ctx, types.BaseFeePerGasPrefix).Get(h)
jewei1997 marked this conversation as resolved.
Show resolved Hide resolved
if bz == nil {
return k.GetMinimumFeePerGas(ctx)
}

return sdk.NewDecFromInt(sdk.NewInt(int64(binary.BigEndian.Uint64(bz))))
}

func (k *Keeper) SetDynamicBaseFeePerGas(ctx sdk.Context, baseFeePerGas uint64) {
h := make([]byte, 8)
binary.BigEndian.PutUint64(h, uint64(ctx.BlockHeight()))
fee := make([]byte, 8)
binary.BigEndian.PutUint64(fee, baseFeePerGas)
k.PrefixStore(ctx, types.BaseFeePerGasPrefix).Set(h, fee)
}
79 changes: 79 additions & 0 deletions x/evm/keeper/fee_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package keeper_test

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
testkeeper "github.com/sei-protocol/sei-chain/testutil/keeper"
"github.com/sei-protocol/sei-chain/x/evm/keeper"
"github.com/stretchr/testify/require"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
)

func TestBaseFeePerGas(t *testing.T) {
k := &testkeeper.EVMTestApp.EvmKeeper
ctx := testkeeper.EVMTestApp.GetContextForDeliverTx([]byte{})
require.Equal(t, k.GetMinimumFeePerGas(ctx), k.GetDynamicBaseFeePerGas(ctx))
k.SetDynamicBaseFeePerGas(ctx, 1)
require.Equal(t, sdk.NewDecFromInt(sdk.NewInt(1)), k.GetDynamicBaseFeePerGas(ctx))
}

func TestAdjustBaseFeePerGas(t *testing.T) {
k, ctx := testkeeper.MockEVMKeeper()
testCases := []struct {
name string
currentBaseFee float64
minimumFee float64
blockGasUsed uint64
blockGasLimit uint64
expectedBaseFee uint64
}{
{
name: "Block gas usage exactly half of limit, no fee change",
currentBaseFee: 100,
minimumFee: 10,
blockGasUsed: 500000,
blockGasLimit: 1000000,
expectedBaseFee: 100,
},
{
name: "Block gas usage 75%, base fee increases",
currentBaseFee: 10000,
minimumFee: 10,
blockGasUsed: 750000,
blockGasLimit: 1000000,
expectedBaseFee: 10000 + 10000*(keeper.MaxBaseFeeChange/2), // 6.25% increase
},
{
name: "Block gas usage 25%, base fee decreases",
currentBaseFee: 10000,
minimumFee: 10,
blockGasUsed: 250000,
blockGasLimit: 1000000,
expectedBaseFee: 10000 - 10000*(keeper.MaxBaseFeeChange/2), // 6.25% decrease
},
{
name: "Block gas usage low, new base fee below minimum, set to minimum",
currentBaseFee: 100,
minimumFee: 90,
blockGasUsed: 100000,
blockGasLimit: 1000000,
expectedBaseFee: 90, // Should not go below the minimum fee
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ctx = ctx.WithConsensusParams(&tmproto.ConsensusParams{
Block: &tmproto.BlockParams{MaxGas: int64(tc.blockGasLimit)},
})
k.SetDynamicBaseFeePerGas(ctx, uint64(tc.currentBaseFee))
p := k.GetParams(ctx)
p.MinimumFeePerGas = sdk.NewDec(int64(tc.minimumFee))
k.SetParams(ctx, p)
k.AdjustDynamicBaseFeePerGas(ctx, tc.blockGasUsed)
expected := sdk.NewDecFromInt(sdk.NewInt(int64(tc.expectedBaseFee)))
require.Equal(t, expected, k.GetDynamicBaseFeePerGas(ctx), "base fee did not match expected value")
})
}
}
6 changes: 3 additions & 3 deletions x/evm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ func (k *Keeper) GetVMBlockContext(ctx sdk.Context, gp core.GasPool) (*vm.BlockC
GasLimit: gp.Gas(),
BlockNumber: big.NewInt(ctx.BlockHeight()),
Time: uint64(ctx.BlockHeader().Time.Unix()),
Difficulty: utils.Big0, // only needed for PoW
BaseFee: k.GetBaseFeePerGas(ctx).TruncateInt().BigInt(), // feemarket not enabled
BlobBaseFee: utils.Big1, // Cancun not enabled
Difficulty: utils.Big0, // only needed for PoW
BaseFee: k.GetDynamicBaseFeePerGas(ctx).TruncateInt().BigInt(), // feemarket not enabled
jewei1997 marked this conversation as resolved.
Show resolved Hide resolved
BlobBaseFee: utils.Big1, // Cancun not enabled
Random: &rh,
}, nil
}
Expand Down
2 changes: 1 addition & 1 deletion x/evm/keeper/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestParams(t *testing.T) {
ctx := testkeeper.EVMTestApp.GetContextForDeliverTx([]byte{}).WithBlockTime(time.Now())
require.Equal(t, "usei", k.GetBaseDenom(ctx))
require.Equal(t, types.DefaultPriorityNormalizer, k.GetPriorityNormalizer(ctx))
require.Equal(t, types.DefaultBaseFeePerGas, k.GetBaseFeePerGas(ctx))
require.Equal(t, types.DefaultBaseFeePerGas, k.GetDynamicBaseFeePerGas(ctx))
require.Equal(t, types.DefaultMinFeePerGas, k.GetMinimumFeePerGas(ctx))
require.Equal(t, types.DefaultDeliverTxHookWasmGasLimit, k.GetDeliverTxHookWasmGasLimit(ctx))

Expand Down
8 changes: 7 additions & 1 deletion x/evm/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,13 @@

// EndBlock executes all ABCI EndBlock logic respective to the evm module. It
// returns no validator updates.
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
func (am AppModule) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) []abci.ValidatorUpdate {
fmt.Println("JEREMYDEBUG: EndBlock, block gas limit = ", ctx.ConsensusParams().Block.MaxGas)
fmt.Println("JEREMYDEBUG: EndBlock, block gas used = ", req.BlockGasUsed)

Check failure on line 281 in x/evm/module.go

View workflow job for this annotation

GitHub Actions / forward-compatibility

req.BlockGasUsed undefined (type "github.com/tendermint/tendermint/abci/types".RequestEndBlock has no field or method BlockGasUsed)
// Adjust base fee for dynamic base fee
fmt.Println("JEREMYDEBUG: Pre-adjustment Dynamic Base Fee Per Gas = ", am.keeper.GetDynamicBaseFeePerGas(ctx))
Fixed Show fixed Hide fixed
am.keeper.AdjustDynamicBaseFeePerGas(ctx, uint64(req.BlockGasUsed))

Check failure on line 284 in x/evm/module.go

View workflow job for this annotation

GitHub Actions / forward-compatibility

req.BlockGasUsed undefined (type "github.com/tendermint/tendermint/abci/types".RequestEndBlock has no field or method BlockGasUsed)
Fixed Show fixed Hide fixed

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

path flow from Begin/EndBlock to a panic call
path flow from Begin/EndBlock to a panic call
path flow from Begin/EndBlock to a panic call
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
fmt.Println("JEREMYDEBUG: Post-adjustment Dynamic Base Fee Per Gas = ", am.keeper.GetDynamicBaseFeePerGas(ctx))
Fixed Show fixed Hide fixed
var coinbase sdk.AccAddress
if am.keeper.EthBlockTestConfig.Enabled {
blocks := am.keeper.BlockTest.Json.Blocks
Expand Down
2 changes: 2 additions & 0 deletions x/evm/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ var (

AnteSurplusPrefix = []byte{0x18} // transient
DeferredInfoPrefix = []byte{0x19} // transient

BaseFeePerGasPrefix = []byte{0x20}
)

var (
Expand Down
Loading