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

refactor: use decimal as min_base_fee #66

Merged
merged 13 commits into from
Apr 8, 2024

Conversation

MSalopek
Copy link
Contributor

This PR changes the feemarket implementation to use a decimal instead of an int for the min_base_fee field.

Not using a decimal could cause an inadvertant and unexpeced rise in the gas fee costs.

In some calculations I was not sure about the correctnes of the operation. Search for // Note: not sure about across the repo.

Feedback is appreciated.

README.md Outdated Show resolved Hide resolved
proto/feemarket/feemarket/v1/genesis.proto Outdated Show resolved Hide resolved
@aljo242
Copy link
Collaborator

aljo242 commented Apr 1, 2024

@MSalopek I'm not so sure that moving to use a decimal value is going to be a good idea here. Or at least this implementation of it.

In the event where the decimal value goes below 1, anytime the TruncateInt is called, the value is rounded down, meaning the resulting fee will be 0 right?

I don't thing there should ever be a case where the on-chain fee is 0. Is this behavior you are intending?

@aljo242
Copy link
Collaborator

aljo242 commented Apr 1, 2024

There should at least be logic that ensures that the resulting fee is non-zero

@MSalopek
Copy link
Contributor Author

MSalopek commented Apr 2, 2024

@MSalopek I'm not so sure that moving to use a decimal value is going to be a good idea here. Or at least this implementation of it.

In the event where the decimal value goes below 1, anytime the TruncateInt is called, the value is rounded down, meaning the resulting fee will be 0 right?

I don't thing there should ever be a case where the on-chain fee is 0. Is this behavior you are intending?

I see your point, we can add changes that mitigate that issue.

We only truncate because sdk.Coin requires an integer value.

I was checking why sdk.Coins is used in GetMinPrices - it was not clear to me why that was chosen. It seems that we can return sdk.DecCoins in GetMinGasPrices to avoid truncation and change the logic to always round up to the nearest integer value.

Rounding up can be seen in the gaia x/globalfee.

The WithGasPrices context func also handles sdk.DecCoins and not sdk.Coins and there are some Coins -> DecCoins conversions already happening in the feemarket antehandler. Those can be refactored:

That could solve the issue of inadvertant zero fee transactions.

The changes are outlined below (+ some changes in protos):

// FILE: x/feemarket/keeper/feemarket.go

func (k *Keeper) GetMinGasPrices(ctx sdk.Context) (sdk.DecCoins, error) {
	baseFee, err := k.GetBaseFee(ctx)
	if err != nil {
		return sdk.NewDecCoins(), err
	}

	params, err := k.GetParams(ctx)
	if err != nil {
		return sdk.NewDecCoins(), err
	}

	// NOTE: no truncation
	fee := sdk.NewDecCoinFromDec(params.FeeDenom, baseFee)
	minGasPrices := sdk.NewDecCoins(fee)

	return minGasPrices, nil
}
// FILE: x/feemarket/ante/fee.go

- func CheckTxFees(ctx sdk.Context, minFees sdk.Coins, feeTx sdk.FeeTx, isCheck bool) (feeCoins sdk.Coins, tip sdk.Coins, err error) {
- 	minFeesDecCoins := sdk.NewDecCoinsFromCoins(minFees...)
+ func CheckTxFees(ctx sdk.Context, minFeesDecCoins sdk.DecCoins, feeTx sdk.FeeTx, isCheck bool) (feeCoins sdk.Coins, tip sdk.Coins, err error) {
	feeCoins = feeTx.GetFee()
// FILE: x/feemarket/types/state.go
func (s *State) UpdateBaseFee(params Params) (fee math.LegacyDec) {
       ...

	// Update the base fee.
-	fee = s.BaseFee.Mul(learningRateAdjustment).Add(net).TruncateDec()
+	fee = s.BaseFee.Mul(learningRateAdjustment).Add(net)
       ...
}

What do you think?

@MSalopek MSalopek requested a review from aljo242 April 2, 2024 13:18
x/feemarket/post/fee.go Outdated Show resolved Hide resolved
@MSalopek MSalopek requested a review from aljo242 April 3, 2024 10:13
Copy link
Collaborator

@aljo242 aljo242 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving - but there are some caveats here:

This code must be tested much further before it is brought into production. I would recommend expanding on the tests/e2e directory we have significantly before bringing to prod. Load tests as well

@aljo242 aljo242 merged commit b8d3623 into skip-mev:main Apr 8, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants