-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #257 from persistenceOne/ajeet/fee-denom-fixes
feat: fee denoms whiltelist decorator
- Loading branch information
Showing
6 changed files
with
195 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package app | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
errorsmod "cosmossdk.io/errors" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
) | ||
|
||
type FeeDenomWhitelistDecorator struct { | ||
whitelistMap map[string]struct{} | ||
whitelistStr string // this is used for err msg only | ||
} | ||
|
||
func NewFeeDenomWhitelistDecorator(denomsWhitelist []string) *FeeDenomWhitelistDecorator { | ||
if len(denomsWhitelist) == 0 { | ||
panic("at least one fee denom must be whitelisted") | ||
} | ||
|
||
whitelistMap := map[string]struct{}{} | ||
for _, denom := range denomsWhitelist { | ||
// must be valid denom | ||
if err := sdk.ValidateDenom(denom); err != nil { | ||
panic(fmt.Sprintf("invalid denoms whiltelist; err: %v", err)) | ||
} | ||
whitelistMap[denom] = struct{}{} | ||
} | ||
|
||
return &FeeDenomWhitelistDecorator{ | ||
whitelistMap: whitelistMap, | ||
whitelistStr: strings.Join(denomsWhitelist, ","), | ||
} | ||
} | ||
|
||
func (fdd *FeeDenomWhitelistDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) { | ||
feeTx, ok := tx.(sdk.FeeTx) | ||
if !ok { | ||
return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx") | ||
} | ||
|
||
feeCoins := feeTx.GetFee() | ||
for _, coin := range feeCoins { | ||
if _, found := fdd.whitelistMap[coin.Denom]; !found { | ||
return ctx, errorsmod.Wrapf(sdkerrors.ErrInvalidCoins, | ||
"fee denom is not allowed; got: %v, allowed: %v", | ||
coin.Denom, fdd.whitelistStr) | ||
} | ||
} | ||
return next(ctx, tx, simulate) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package app | ||
|
||
import ( | ||
"testing" | ||
|
||
"cosmossdk.io/math" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/cometbft/cometbft/libs/log" | ||
tmtypes "github.com/cometbft/cometbft/proto/tendermint/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
func TestFeeDenomWhiltelistDecorator(t *testing.T) { | ||
testcases := []struct { | ||
name string | ||
txFee sdk.Coins | ||
denomsWhitelist []string | ||
expectedErr string | ||
panics bool | ||
}{ | ||
{ | ||
name: "empty denoms list", | ||
txFee: coins(), | ||
denomsWhitelist: []string{}, | ||
expectedErr: "at least one fee denom must be whitelisted", | ||
panics: true, | ||
}, | ||
{ | ||
name: "invalid denoms list", | ||
txFee: coins(), | ||
denomsWhitelist: []string{"a"}, | ||
expectedErr: "invalid denoms whiltelist; err: invalid denom: a", | ||
panics: true, | ||
}, | ||
{ | ||
name: "valid denoms - valid fee", | ||
txFee: coins(10, "uxprt"), | ||
denomsWhitelist: FeeDenomsWhitelistMainnet, | ||
expectedErr: "", | ||
}, | ||
{ | ||
name: "valid denoms - multiple valid fees", | ||
txFee: coins(10, FeeDenomsWhitelistMainnet[1], 10, "uxprt"), | ||
denomsWhitelist: FeeDenomsWhitelistMainnet, | ||
expectedErr: "", | ||
}, | ||
{ | ||
name: "valid denoms - invalid fee", | ||
txFee: coins(10, "abcd"), | ||
denomsWhitelist: FeeDenomsWhitelistMainnet, | ||
expectedErr: "fee denom is not allowed; got: abcd, allowed: uxprt,ibc/C8A74ABBE2AF892E15680D916A7C22130585CE5704F9B17A10F184A90D53BECA: invalid coins", | ||
}, | ||
{ | ||
name: "valid denoms - multiple invalid fee", | ||
txFee: coins(10, "uxprt", 10, "xyz"), | ||
denomsWhitelist: FeeDenomsWhitelistMainnet, | ||
expectedErr: "fee denom is not allowed; got: xyz, allowed: uxprt,ibc/C8A74ABBE2AF892E15680D916A7C22130585CE5704F9B17A10F184A90D53BECA: invalid coins", | ||
}, | ||
} | ||
|
||
isCheckTx := false | ||
ctx := sdk.NewContext(nil, tmtypes.Header{}, isCheckTx, log.NewNopLogger()) | ||
for _, tc := range testcases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
defer func() { | ||
r := recover() | ||
if tc.panics { | ||
require.Equal(t, r, tc.expectedErr) | ||
} else { | ||
require.Nil(t, r, "Test did not panic") | ||
} | ||
}() | ||
|
||
fdd := NewFeeDenomWhitelistDecorator(tc.denomsWhitelist) | ||
antehandlerFFD := sdk.ChainAnteDecorators(fdd) | ||
tx := &mockFeeTx{fee: tc.txFee} | ||
|
||
_, err := antehandlerFFD(ctx, tx, false) | ||
|
||
if tc.expectedErr == "" { | ||
require.NoError(t, err) | ||
} else { | ||
require.EqualError(t, err, tc.expectedErr) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func coins(amountDenomPairs ...interface{}) sdk.Coins { | ||
coins := sdk.Coins{} | ||
for i := 0; i < len(amountDenomPairs); i += 2 { | ||
coins = append(coins, sdk.Coin{ | ||
Amount: math.NewInt(int64(amountDenomPairs[i].(int))), | ||
Denom: amountDenomPairs[i+1].(string), | ||
}) | ||
} | ||
return coins | ||
} | ||
|
||
var _ sdk.Tx = (*mockFeeTx)(nil) | ||
var _ sdk.FeeTx = (*mockFeeTx)(nil) | ||
|
||
type mockFeeTx struct{ fee sdk.Coins } | ||
|
||
func (m *mockFeeTx) GetFee() sdk.Coins { return m.fee } | ||
func (m *mockFeeTx) GetGas() uint64 { return 1 } | ||
|
||
func (*mockFeeTx) FeeGranter() sdk.AccAddress { return nil } | ||
func (*mockFeeTx) FeePayer() sdk.AccAddress { return nil } | ||
func (*mockFeeTx) GetMsgs() []sdk.Msg { return nil } | ||
func (*mockFeeTx) ValidateBasic() error { return nil } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters