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: Be resilient to target gas used 0 and cap gas used to gas limit #1885

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion contracts/test/ERC20toCW20PointerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe("ERC20 to CW20 Pointer", function () {
});

describe("transfer()", function () {
it.only("should transfer", async function () {
it("should transfer", async function () {
let sender = accounts[0];
let recipient = accounts[1];

Expand Down
10 changes: 9 additions & 1 deletion x/evm/keeper/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@ func (k *Keeper) AdjustDynamicBaseFeePerGas(ctx sdk.Context, blockGasUsed uint64
return nil
}
currentBaseFee := k.GetDynamicBaseFeePerGas(ctx)
targetGasUsed := sdk.NewDec(int64(k.GetTargetGasUsedPerBlock(ctx)))
if targetGasUsed.IsZero() {
return &currentBaseFee
}
minimumFeePerGas := k.GetParams(ctx).MinimumFeePerGas
blockGasLimit := sdk.NewDec(ctx.ConsensusParams().Block.MaxGas)
blockGasUsedDec := sdk.NewDec(int64(blockGasUsed))
targetGasUsed := sdk.NewDec(int64(k.GetTargetGasUsedPerBlock(ctx)))

// cap block gas used to block gas limit
if blockGasUsedDec.GT(blockGasLimit) {
blockGasUsedDec = blockGasLimit
}

var newBaseFee sdk.Dec
if blockGasUsedDec.GT(targetGasUsed) {
Expand Down
23 changes: 23 additions & 0 deletions x/evm/keeper/fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,29 @@ func TestAdjustBaseFeePerGas(t *testing.T) {
targetGasUsed: 500000,
expectedBaseFee: 99, // Should not go below the minimum fee
},
{
name: "target gas used is 0",
currentBaseFee: 10000,
minimumFee: 10,
blockGasUsed: 0,
blockGasLimit: 1000000,
upwardAdj: sdk.NewDecWithPrec(5, 1),
downwardAdj: sdk.NewDecWithPrec(5, 1),
targetGasUsed: 0,
expectedBaseFee: 10000,
},
{
name: "cap block gas used to block gas limit",
// block gas used is 1.5x block gas limit
currentBaseFee: 10000,
minimumFee: 10,
blockGasUsed: 1500000,
blockGasLimit: 1000000,
upwardAdj: sdk.NewDecWithPrec(5, 1),
downwardAdj: sdk.NewDecWithPrec(5, 1),
targetGasUsed: 500000,
expectedBaseFee: 15000,
},
}

for _, tc := range testCases {
Expand Down
2 changes: 0 additions & 2 deletions x/evm/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ func (server msgServer) EVMTransaction(goCtx context.Context, msg *types.MsgEVMT
originalGasMeter.ConsumeGas(adjustedGasUsed.TruncateInt().Uint64(), "evm transaction")
}()

fmt.Println("JEREMYDEBUG: calling apply evm message from EVMTransaction")
res, applyErr := server.applyEVMMessage(ctx, emsg, stateDB, gp)
serverRes = &types.MsgEVMTransactionResponse{
Hash: tx.Hash().Hex(),
Expand Down Expand Up @@ -230,7 +229,6 @@ func (k *Keeper) GetEVMMessage(ctx sdk.Context, tx *ethtypes.Transaction, sender
}

func (k Keeper) applyEVMMessage(ctx sdk.Context, msg *core.Message, stateDB *state.DBImpl, gp core.GasPool) (*core.ExecutionResult, error) {
// fmt.Printf("JEREMYDEBUG: In applyEVMMessage, msg = %+v\n", msg)
blockCtx, err := k.GetVMBlockContext(ctx, gp)
if err != nil {
return nil, err
Expand Down
1 change: 0 additions & 1 deletion x/evm/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
// EndBlock executes all ABCI EndBlock logic respective to the evm module. It
// returns no validator updates.
func (am AppModule) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) []abci.ValidatorUpdate {
fmt.Println("JEREMYDEBUG: block gas used", req.BlockGasUsed, ", block height", ctx.BlockHeight())
newBaseFee := am.keeper.AdjustDynamicBaseFeePerGas(ctx, uint64(req.BlockGasUsed))
if newBaseFee != nil {
metrics.GaugeEvmBlockBaseFee(newBaseFee.TruncateInt().BigInt(), req.Height)
Expand Down
16 changes: 1 addition & 15 deletions x/evm/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
paramtypes.NewParamSetPair(KeyMinFeePerGas, &p.MinimumFeePerGas, validateMinFeePerGas),
paramtypes.NewParamSetPair(KeyWhitelistedCwCodeHashesForDelegateCall, &p.WhitelistedCwCodeHashesForDelegateCall, validateWhitelistedCwHashesForDelegateCall),
paramtypes.NewParamSetPair(KeyDeliverTxHookWasmGasLimit, &p.DeliverTxHookWasmGasLimit, validateDeliverTxHookWasmGasLimit),
paramtypes.NewParamSetPair(KeyTargetGasUsedPerBlock, &p.TargetGasUsedPerBlock, validateTargetGasUsedPerBlock),
paramtypes.NewParamSetPair(KeyTargetGasUsedPerBlock, &p.TargetGasUsedPerBlock, func(i interface{}) error { return nil} ),

Check failure on line 67 in x/evm/types/params.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` (gofmt)
}
}

Expand All @@ -90,23 +90,9 @@
if err := validateBaseFeeAdjustment(p.MaxDynamicBaseFeeDownwardAdjustment); err != nil {
return fmt.Errorf("invalid max dynamic base fee downward adjustment: %s, err: %s", p.MaxDynamicBaseFeeDownwardAdjustment, err)
}
if err := validateTargetGasUsedPerBlock(p.TargetGasUsedPerBlock); err != nil {
return err
}
return validateWhitelistedCwHashesForDelegateCall(p.WhitelistedCwCodeHashesForDelegateCall)
}

func validateTargetGasUsedPerBlock(i interface{}) error {
v, ok := i.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v == 0 {
return fmt.Errorf("invalid target gas used per block: must be greater than 0, got %d", v)
}
return nil
}

func validateBaseFeeAdjustment(i interface{}) error {
adjustment, ok := i.(sdk.Dec)
if !ok {
Expand Down
Loading