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

Merged
merged 13 commits into from
Oct 23, 2024
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
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