Skip to content

Commit

Permalink
fix tests multidenom
Browse files Browse the repository at this point in the history
  • Loading branch information
TropicalDog17 committed Nov 24, 2024
2 parents 0c7401d + d1af289 commit 8b9902e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 44 deletions.
65 changes: 44 additions & 21 deletions custom/auth/ante/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,40 +239,63 @@ func (fd FeeDecorator) checkTxFee(ctx sdk.Context, tx sdk.Tx, taxes sdk.Coins, n
refundNonTaxableTaxes := false

// Ensure that the provided fees meet a minimum threshold for the validator,
// if this is a CheckTx. This is only for local mempool purposes, and thus
// is only ran on check tx.
// Check if the transaction is an oracle transaction and skip gas fees for such transactions.
if !isOracleTx {
requiredGasFees := sdk.Coins{}
minRequiredGasFees := sdk.Coins{}
minGasPrices := fd.taxKeeper.GetEffectiveGasPrices(ctx)
if !minGasPrices.IsZero() {
requiredGasFees = make(sdk.Coins, len(minGasPrices))

// Determine the required fees by multiplying each required minimum gas
// price by the gas limit, where fee = ceil(minGasPrice * gasLimit).
glDec := sdk.NewDec(int64(gas))
for i, gp := range minGasPrices {
fee := gp.Amount.Mul(glDec)
requiredGasFees[i] = sdk.NewCoin(gp.Denom, fee.Ceil().RoundInt())
minRequiredGasFees = make(sdk.Coins, len(minGasPrices))
for i, gasPrice := range minGasPrices {
fee := gasPrice.Amount.Mul(glDec)
minRequiredGasFees[i] = sdk.NewCoin(gasPrice.Denom, fee.Ceil().RoundInt())
}
}

requiredFees := requiredGasFees.Add(taxes...)
allFees := requiredFees.Add(nonTaxableTaxes...)

// Check required fees
if !requiredFees.IsZero() && !feeCoins.IsAllGTE(requiredFees) {
// we don't have enough for tax and gas fees. But do we have enough for gas alone?
if !requiredGasFees.IsZero() && !feeCoins.IsAllGTE(requiredGasFees) {
return 0, false, false, errorsmod.Wrapf(sdkerrors.ErrInsufficientFee, "insufficient fees; got: %q, required: %q = %q(gas) + %q(stability)", feeCoins, requiredFees, requiredGasFees, taxes)
remainingFees := feeCoins

// Check if taxes are covered by the fees
if !taxes.IsZero() {
if !remainingFees.IsAllGTE(taxes) {
// If the fees do not cover the taxes, reverse charge
reverseCharge = true
} else {
remainingFees = remainingFees.Sub(taxes...)

// Check if remaining fees cover gas after taxes
if !minRequiredGasFees.IsZero() && !remainingFees.IsAnyGTE(minRequiredGasFees) {
// If the remaining fees do not cover the gas fees, tax cannot be covered
// So fall back to reverse charge
reverseCharge = true
remainingFees = feeCoins
}
}
}

// we have enough for gas fees but not for tax fees
reverseCharge = true
// Attempt to refund non-taxable taxes
if !nonTaxableTaxes.IsZero() && remainingFees.IsAllGTE(nonTaxableTaxes) {
feeCoinsAfterTax := remainingFees.Sub(nonTaxableTaxes...)

// Check if remaining fees cover gas after non-taxable taxes
if !minRequiredGasFees.IsZero() && !feeCoinsAfterTax.IsAnyGTE(minRequiredGasFees) {
// If the remaining fees do not cover the gas fees, non-taxable taxes cannot be refunded
// We cannot reset to feeCoins as tax might have been deducted earlier
refundNonTaxableTaxes = false
} else {
refundNonTaxableTaxes = true
remainingFees = feeCoinsAfterTax
}
}

if !allFees.IsZero() && feeCoins.IsAllGTE(allFees) {
// we have enough for all fees
refundNonTaxableTaxes = true
// Check if the remaining paid fees are enough to cover the gas fees
if !minRequiredGasFees.IsZero() && !remainingFees.IsAnyGTE(minRequiredGasFees) {
return 0, reverseCharge, refundNonTaxableTaxes, errorsmod.Wrapf(
sdkerrors.ErrInsufficientFee,
"insufficient fees; got: %q, required: %q(gas) [+ %q(tax)]",
feeCoins, minRequiredGasFees, taxes,
)
}
}

Expand Down
42 changes: 19 additions & 23 deletions custom/auth/ante/fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1067,19 +1067,19 @@ func (s *AnteTestSuite) TestTaxExemptionWithGasPriceEnabled() {
denom2Price := sdk.NewDecCoinFromDec(denom2, sdk.NewDecWithPrec(10, 1))
customGasPrices := []sdk.DecCoin{denom1Price, denom2Price}

requiredFees := sdk.NewCoins(sdk.NewInt64Coin(denom1, 200000), sdk.NewInt64Coin(denom2, 200000))

requiredFees := sdk.NewCoins(sdk.NewInt64Coin(denom2, 200000))
requiredTaxes := sdk.NewCoins(sdk.NewInt64Coin(denom1, 5000), sdk.NewInt64Coin(denom2, 5000))
cases := []struct {
name string
msgSigner cryptotypes.PrivKey
msgCreator func() []sdk.Msg
minFeeAmounts []sdk.Coin
expectProceeds sdk.Coins
expectPanic bool
expectAnteError bool
expectReverseCharge bool
}{
{
name: "MsgSend(exemption -> exemption) with multiple fee denoms, not enough gas fees",
name: "MsgSend(exemption -> exemption) with multiple fee denoms, two denom not enough gases",
msgSigner: privs[0],
msgCreator: func() []sdk.Msg {
var msgs []sdk.Msg
Expand All @@ -1092,7 +1092,7 @@ func (s *AnteTestSuite) TestTaxExemptionWithGasPriceEnabled() {
sdk.NewInt64Coin(denom2, 0),
},
expectProceeds: sdk.NewCoins(),
expectPanic: true,
expectAnteError: true,
expectReverseCharge: false,
},
{
Expand All @@ -1109,39 +1109,30 @@ func (s *AnteTestSuite) TestTaxExemptionWithGasPriceEnabled() {
expectReverseCharge: true,
},
{
name: "MsgSend(normal -> normal), enough taxes for both denoms",
name: "MsgSend(normal -> normal), one denom not enough tax",
msgSigner: privs[2],
msgCreator: func() []sdk.Msg {
var msgs []sdk.Msg
msg1 := banktypes.NewMsgSend(addrs[2], addrs[3], sdk.NewCoins(sendCoin, anotherSendCoin))
msgs = append(msgs, msg1)
return msgs
},
minFeeAmounts: []sdk.Coin{
sdk.NewInt64Coin(denom1, 5000),
sdk.NewInt64Coin(denom2, 5000),
},
expectProceeds: []sdk.Coin{
sdk.NewInt64Coin(denom1, 5000),
sdk.NewInt64Coin(denom2, 5000),
},
expectReverseCharge: false,
minFeeAmounts: requiredFees.Add(requiredTaxes...).Sub(sdk.NewInt64Coin(denom1, 1)),
expectProceeds: sdk.NewCoins(),
expectReverseCharge: true,
},
{
name: "MsgSend(normal -> normal), one denom not enough tax",
name: "MsgSend(normal -> normal), enough taxes + gases",
msgSigner: privs[2],
msgCreator: func() []sdk.Msg {
var msgs []sdk.Msg
msg1 := banktypes.NewMsgSend(addrs[2], addrs[3], sdk.NewCoins(sendCoin, anotherSendCoin))
msgs = append(msgs, msg1)
return msgs
},
minFeeAmounts: []sdk.Coin{
sdk.NewInt64Coin(denom1, 5000),
sdk.NewInt64Coin(denom2, 2500),
},
expectProceeds: []sdk.Coin{},
expectReverseCharge: true,
minFeeAmounts: requiredFees.Add(requiredTaxes...),
expectProceeds: requiredTaxes,
expectReverseCharge: false,
},
}

Expand Down Expand Up @@ -1194,7 +1185,12 @@ func (s *AnteTestSuite) TestTaxExemptionWithGasPriceEnabled() {
require.NoError(err)

newCtx, err := antehandler(s.ctx, tx, false)
require.NoError(err)
if c.expectAnteError {
require.Error(err)
return
} else {

Check warning on line 1191 in custom/auth/ante/fee_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)
require.NoError(err)
}
newCtx, err = posthandler(newCtx, tx, false, true)
require.NoError(err)

Expand Down

0 comments on commit 8b9902e

Please sign in to comment.