github.com/KiraCore/sekai@v0.3.43/app/ante/ante_test.go (about)

     1  package ante_test
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	customante "github.com/KiraCore/sekai/app/ante"
     8  	"github.com/KiraCore/sekai/types"
     9  	govtypes "github.com/KiraCore/sekai/x/gov/types"
    10  	tokenstypes "github.com/KiraCore/sekai/x/tokens/types"
    11  	cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
    12  	"github.com/cosmos/cosmos-sdk/testutil/testdata"
    13  	sdk "github.com/cosmos/cosmos-sdk/types"
    14  	bank "github.com/cosmos/cosmos-sdk/x/bank/types"
    15  	minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
    16  )
    17  
    18  func (suite *AnteTestSuite) SetBalance(addr sdk.AccAddress, coin sdk.Coin) {
    19  	coins := sdk.Coins{coin}
    20  	suite.app.BankKeeper.MintCoins(suite.ctx, minttypes.ModuleName, coins)
    21  	suite.app.BankKeeper.SendCoinsFromModuleToAccount(suite.ctx, minttypes.ModuleName, addr, coins)
    22  }
    23  
    24  // Test that simulate transaction process execution fee correctly on ante handler step
    25  func (suite *AnteTestSuite) TestCustomAnteHandlerExecutionFee() {
    26  	suite.SetupTest(false) // reset
    27  
    28  	// set execution fee for set network properties
    29  	suite.app.CustomGovKeeper.SetExecutionFee(suite.ctx, govtypes.ExecutionFee{
    30  		TransactionType:   types.MsgTypeSetNetworkProperties,
    31  		ExecutionFee:      10000,
    32  		FailureFee:        1000,
    33  		Timeout:           0,
    34  		DefaultParameters: 0,
    35  	})
    36  	suite.app.CustomGovKeeper.SetNetworkProperties(suite.ctx, &govtypes.NetworkProperties{
    37  		MinTxFee:                 2,
    38  		MaxTxFee:                 10000,
    39  		EnableForeignFeePayments: true,
    40  	})
    41  
    42  	// Same data for every test cases
    43  	accounts := suite.CreateTestAccounts(5)
    44  
    45  	suite.SetBalance(accounts[0].acc.GetAddress(), sdk.NewInt64Coin("ukex", 10000))
    46  	suite.SetBalance(accounts[1].acc.GetAddress(), sdk.NewInt64Coin("ukex", 10000))
    47  	suite.SetBalance(accounts[2].acc.GetAddress(), sdk.NewInt64Coin("ukex", 10000))
    48  	suite.SetBalance(accounts[3].acc.GetAddress(), sdk.NewInt64Coin("ukex", 1))
    49  	suite.SetBalance(accounts[4].acc.GetAddress(), sdk.NewInt64Coin("ukex", 10000))
    50  	suite.SetBalance(accounts[4].acc.GetAddress(), sdk.NewInt64Coin("ubtc", 10000))
    51  
    52  	defaultFee := sdk.NewCoins(sdk.NewInt64Coin("ukex", 100))
    53  	gasLimit := testdata.NewTestGasLimit()
    54  	privs := []cryptotypes.PrivKey{accounts[0].priv, accounts[1].priv, accounts[2].priv, accounts[3].priv, accounts[4].priv}
    55  	accNums := []uint64{0, 1, 2, 3, 4}
    56  
    57  	testCases := []TestCase{
    58  		{
    59  			"insufficient max execution fee set",
    60  			func() ([]sdk.Msg, []cryptotypes.PrivKey, []uint64, []uint64, sdk.Coins) {
    61  				msgs := []sdk.Msg{
    62  					govtypes.NewMsgSetNetworkProperties(accounts[0].acc.GetAddress(), &govtypes.NetworkProperties{
    63  						MinTxFee:                 2,
    64  						MaxTxFee:                 10000,
    65  						EnableForeignFeePayments: true,
    66  					}),
    67  				}
    68  				return msgs, privs[0:1], accNums[0:1], []uint64{0}, defaultFee
    69  			},
    70  			true,
    71  			false,
    72  			errors.New("fee 100ukex(100) is less than max execution fee 10000ukex: invalid request"),
    73  		},
    74  		{
    75  			"execution failure fee deduction",
    76  			func() ([]sdk.Msg, []cryptotypes.PrivKey, []uint64, []uint64, sdk.Coins) {
    77  				msgs := []sdk.Msg{
    78  					govtypes.NewMsgSetNetworkProperties(accounts[1].acc.GetAddress(), &govtypes.NetworkProperties{
    79  						MinTxFee:                 2,
    80  						MaxTxFee:                 10000,
    81  						EnableForeignFeePayments: true,
    82  					}),
    83  				}
    84  				return msgs, privs[1:2], accNums[1:2], []uint64{0}, sdk.NewCoins(sdk.NewInt64Coin("ukex", 10000))
    85  			},
    86  			true,
    87  			true,
    88  			nil,
    89  		},
    90  		{
    91  			"no execution fee deduction when does not exist",
    92  			func() ([]sdk.Msg, []cryptotypes.PrivKey, []uint64, []uint64, sdk.Coins) {
    93  				msgs := []sdk.Msg{
    94  					govtypes.NewMsgSetExecutionFee(
    95  						types.MsgTypeSetNetworkProperties,
    96  						10000,
    97  						1000,
    98  						0,
    99  						0,
   100  						accounts[2].acc.GetAddress(),
   101  					),
   102  				}
   103  				return msgs, privs[2:3], accNums[2:3], []uint64{0}, defaultFee
   104  			},
   105  			false,
   106  			true,
   107  			nil,
   108  		},
   109  		{
   110  			"insufficient balance to pay for fee",
   111  			func() ([]sdk.Msg, []cryptotypes.PrivKey, []uint64, []uint64, sdk.Coins) {
   112  				msgs := []sdk.Msg{
   113  					govtypes.NewMsgSetExecutionFee(
   114  						types.MsgTypeSetNetworkProperties,
   115  						10000,
   116  						1000,
   117  						0,
   118  						0,
   119  						accounts[3].acc.GetAddress(),
   120  					),
   121  				}
   122  				return msgs, privs[3:4], accNums[3:4], []uint64{0}, sdk.NewCoins(sdk.NewInt64Coin("ukex", 10))
   123  			},
   124  			false,
   125  			false,
   126  			errors.New("1ukex is smaller than 10ukex: insufficient funds"),
   127  		},
   128  		{
   129  			"fee out of range",
   130  			func() ([]sdk.Msg, []cryptotypes.PrivKey, []uint64, []uint64, sdk.Coins) {
   131  				msgs := []sdk.Msg{
   132  					govtypes.NewMsgSetExecutionFee(
   133  						types.MsgTypeSetNetworkProperties,
   134  						10000,
   135  						1000,
   136  						0,
   137  						0,
   138  						accounts[4].acc.GetAddress(),
   139  					),
   140  				}
   141  				return msgs, privs[4:5], accNums[4:5], []uint64{0}, sdk.NewCoins(sdk.NewInt64Coin("ukex", 1))
   142  			},
   143  			false,
   144  			false,
   145  			errors.New("fee 1ukex(1) is out of range [2, 10000]ukex: invalid request"),
   146  		},
   147  		{
   148  			"foreign currency as fee payment when EnableForeignFeePayments is enabled by governance",
   149  			func() ([]sdk.Msg, []cryptotypes.PrivKey, []uint64, []uint64, sdk.Coins) {
   150  				suite.app.CustomGovKeeper.SetNetworkProperties(suite.ctx, &govtypes.NetworkProperties{
   151  					MinTxFee:                 2,
   152  					MaxTxFee:                 10000,
   153  					EnableForeignFeePayments: true,
   154  				})
   155  				msgs := []sdk.Msg{
   156  					govtypes.NewMsgSetExecutionFee(
   157  						types.MsgTypeSetNetworkProperties,
   158  						10000,
   159  						1000,
   160  						0,
   161  						0,
   162  						accounts[4].acc.GetAddress(),
   163  					),
   164  				}
   165  				return msgs, privs[4:5], accNums[4:5], []uint64{0}, sdk.NewCoins(sdk.NewInt64Coin("ubtc", 10))
   166  			},
   167  			false,
   168  			true,
   169  			nil,
   170  		},
   171  		{
   172  			"foreign currency as fee payment when EnableForeignFeePayments is disabled by governance",
   173  			func() ([]sdk.Msg, []cryptotypes.PrivKey, []uint64, []uint64, sdk.Coins) {
   174  				suite.app.CustomGovKeeper.SetNetworkProperties(suite.ctx, &govtypes.NetworkProperties{
   175  					MinTxFee:                 2,
   176  					MaxTxFee:                 10000,
   177  					EnableForeignFeePayments: false,
   178  				})
   179  				msgs := []sdk.Msg{
   180  					govtypes.NewMsgSetExecutionFee(
   181  						types.MsgTypeSetNetworkProperties,
   182  						10000,
   183  						1000,
   184  						0,
   185  						0,
   186  						accounts[4].acc.GetAddress(),
   187  					),
   188  				}
   189  				return msgs, privs[4:5], accNums[4:5], []uint64{0}, sdk.NewCoins(sdk.NewInt64Coin("ubtc", 10))
   190  			},
   191  			false,
   192  			false,
   193  			errors.New("foreign fee payments is disabled by governance: invalid request"),
   194  		},
   195  		{
   196  			"try sending non bond denom coins on poor network",
   197  			func() ([]sdk.Msg, []cryptotypes.PrivKey, []uint64, []uint64, sdk.Coins) {
   198  				suite.app.CustomGovKeeper.SetNetworkProperties(suite.ctx, &govtypes.NetworkProperties{
   199  					MinTxFee:                 2,
   200  					MaxTxFee:                 10000,
   201  					EnableForeignFeePayments: true,
   202  					MinValidators:            100,
   203  				})
   204  				msgs := []sdk.Msg{
   205  					bank.NewMsgSend(
   206  						accounts[4].acc.GetAddress(),
   207  						accounts[3].acc.GetAddress(),
   208  						sdk.NewCoins(sdk.NewInt64Coin("ubtc", 10)),
   209  					),
   210  				}
   211  				return msgs, privs[4:5], accNums[4:5], []uint64{0}, sdk.NewCoins(sdk.NewInt64Coin("ukex", 10))
   212  			},
   213  			false,
   214  			false,
   215  			errors.New("only bond denom is allowed on poor network: invalid request"),
   216  		},
   217  		{
   218  			"try sending more bond denom than restricted amount on poor network",
   219  			func() ([]sdk.Msg, []cryptotypes.PrivKey, []uint64, []uint64, sdk.Coins) {
   220  				suite.app.CustomGovKeeper.SetNetworkProperties(suite.ctx, &govtypes.NetworkProperties{
   221  					MinTxFee:                 2,
   222  					MaxTxFee:                 10000,
   223  					EnableForeignFeePayments: true,
   224  					MinValidators:            100,
   225  					PoorNetworkMaxBankSend:   1000,
   226  				})
   227  				msgs := []sdk.Msg{
   228  					bank.NewMsgSend(
   229  						accounts[4].acc.GetAddress(),
   230  						accounts[3].acc.GetAddress(),
   231  						sdk.NewCoins(sdk.NewInt64Coin("ukex", 10000000)),
   232  					),
   233  				}
   234  				return msgs, privs[4:5], accNums[4:5], []uint64{0}, sdk.NewCoins(sdk.NewInt64Coin("ukex", 10))
   235  			},
   236  			false,
   237  			false,
   238  			errors.New("only restricted amount send is allowed on poor network: invalid request"),
   239  		},
   240  		{
   241  			"try sending lower than restriction amount on poor network",
   242  			func() ([]sdk.Msg, []cryptotypes.PrivKey, []uint64, []uint64, sdk.Coins) {
   243  				suite.app.CustomGovKeeper.SetNetworkProperties(suite.ctx, &govtypes.NetworkProperties{
   244  					MinTxFee:                 2,
   245  					MaxTxFee:                 10000,
   246  					EnableForeignFeePayments: true,
   247  					MinValidators:            100,
   248  					PoorNetworkMaxBankSend:   1000,
   249  				})
   250  				msgs := []sdk.Msg{
   251  					bank.NewMsgSend(
   252  						accounts[4].acc.GetAddress(),
   253  						accounts[3].acc.GetAddress(),
   254  						sdk.NewCoins(sdk.NewInt64Coin("ukex", 1000)),
   255  					),
   256  				}
   257  				return msgs, privs[4:5], accNums[4:5], []uint64{1}, sdk.NewCoins(sdk.NewInt64Coin("ubtc", 10))
   258  			},
   259  			false,
   260  			true,
   261  			nil,
   262  		},
   263  		{
   264  			"try sending enabled message on poor network",
   265  			func() ([]sdk.Msg, []cryptotypes.PrivKey, []uint64, []uint64, sdk.Coins) {
   266  				suite.app.CustomGovKeeper.SetNetworkProperties(suite.ctx, &govtypes.NetworkProperties{
   267  					MinTxFee:                 2,
   268  					MaxTxFee:                 10000,
   269  					EnableForeignFeePayments: true,
   270  					MinValidators:            100,
   271  				})
   272  				msgs := []sdk.Msg{
   273  					govtypes.NewMsgSetNetworkProperties(
   274  						accounts[4].acc.GetAddress(),
   275  						&govtypes.NetworkProperties{
   276  							MinTxFee:                 2,
   277  							MaxTxFee:                 10000,
   278  							EnableForeignFeePayments: true,
   279  							MinValidators:            100,
   280  						},
   281  					),
   282  				}
   283  				return msgs, privs[4:5], accNums[4:5], []uint64{2}, sdk.NewCoins(sdk.NewInt64Coin("ubtc", 1000))
   284  			},
   285  			false,
   286  			true,
   287  			nil,
   288  		},
   289  		{
   290  			"try sending not enabled message on poor network",
   291  			func() ([]sdk.Msg, []cryptotypes.PrivKey, []uint64, []uint64, sdk.Coins) {
   292  				suite.app.CustomGovKeeper.SetNetworkProperties(suite.ctx, &govtypes.NetworkProperties{
   293  					MinTxFee:                 2,
   294  					MaxTxFee:                 10000,
   295  					EnableForeignFeePayments: true,
   296  					MinValidators:            100,
   297  				})
   298  				msgs := []sdk.Msg{
   299  					govtypes.NewMsgSetExecutionFee(
   300  						types.MsgTypeSetNetworkProperties,
   301  						10000,
   302  						1000,
   303  						0,
   304  						0,
   305  						accounts[4].acc.GetAddress(),
   306  					),
   307  				}
   308  				return msgs, privs[4:5], accNums[4:5], []uint64{0}, sdk.NewCoins(sdk.NewInt64Coin("ubtc", 10))
   309  			},
   310  			false,
   311  			false,
   312  			errors.New("invalid transaction type on poor network: invalid request"),
   313  		},
   314  	}
   315  
   316  	for _, tc := range testCases {
   317  		suite.Run(fmt.Sprintf("Case %s", tc.desc), func() {
   318  			suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder()
   319  			msgs, privs, accNums, accSeqs, feeAmount := tc.buildTest()
   320  
   321  			// this runs multi signature transaction with the params provided
   322  			suite.RunTestCase(privs, msgs, feeAmount, gasLimit, accNums, accSeqs, suite.ctx.ChainID(), tc)
   323  		})
   324  	}
   325  }
   326  
   327  // Test that simulate transaction process fee range decorator correctly on ante handler step
   328  func (suite *AnteTestSuite) TestValidateFeeRangeDecorator() {
   329  	suite.SetupTest(false) // reset
   330  
   331  	// set execution fee for set network properties
   332  	suite.app.CustomGovKeeper.SetNetworkProperties(suite.ctx, &govtypes.NetworkProperties{
   333  		MinTxFee:                 2,
   334  		MaxTxFee:                 10000,
   335  		EnableForeignFeePayments: true,
   336  		EnableTokenBlacklist:     true,
   337  		EnableTokenWhitelist:     true,
   338  	})
   339  
   340  	suite.app.CustomGovKeeper.SetExecutionFee(suite.ctx, govtypes.ExecutionFee{
   341  		TransactionType:   types.MsgTypeSetNetworkProperties,
   342  		ExecutionFee:      10000,
   343  		FailureFee:        1000,
   344  		Timeout:           0,
   345  		DefaultParameters: 0,
   346  	})
   347  
   348  	// Same data for every test cases
   349  	accounts := suite.CreateTestAccounts(5)
   350  
   351  	suite.SetBalance(accounts[0].acc.GetAddress(), sdk.NewInt64Coin("ukex", 10000))
   352  	suite.SetBalance(accounts[0].acc.GetAddress(), sdk.NewInt64Coin("frozen", 10000))
   353  	suite.SetBalance(accounts[0].acc.GetAddress(), sdk.NewInt64Coin("nofeetoken", 10000))
   354  	suite.SetBalance(accounts[1].acc.GetAddress(), sdk.NewInt64Coin("ukex", 10000))
   355  	suite.SetBalance(accounts[2].acc.GetAddress(), sdk.NewInt64Coin("ukex", 10000))
   356  	suite.SetBalance(accounts[3].acc.GetAddress(), sdk.NewInt64Coin("ukex", 1))
   357  	suite.SetBalance(accounts[4].acc.GetAddress(), sdk.NewInt64Coin("ukex", 10000))
   358  	suite.SetBalance(accounts[4].acc.GetAddress(), sdk.NewInt64Coin("ubtc", 10000))
   359  	gasLimit := testdata.NewTestGasLimit()
   360  	privs := []cryptotypes.PrivKey{accounts[0].priv, accounts[1].priv, accounts[2].priv, accounts[3].priv, accounts[4].priv}
   361  	accNums := []uint64{0, 1, 2, 3, 4}
   362  
   363  	testCases := []TestCase{
   364  		{
   365  			"frozen fee set test",
   366  			func() ([]sdk.Msg, []cryptotypes.PrivKey, []uint64, []uint64, sdk.Coins) {
   367  				msgs := []sdk.Msg{
   368  					govtypes.NewMsgSetNetworkProperties(accounts[0].acc.GetAddress(), &govtypes.NetworkProperties{
   369  						MinTxFee:                 2,
   370  						MaxTxFee:                 10000,
   371  						EnableForeignFeePayments: true,
   372  					}),
   373  				}
   374  				return msgs, privs[0:1], accNums[0:1], []uint64{0}, sdk.NewCoins(sdk.NewInt64Coin("frozen", 100))
   375  			},
   376  			true,
   377  			false,
   378  			errors.New("currency you are trying to use as fee is frozen: invalid request"),
   379  		},
   380  		{
   381  			"not whitelisted token",
   382  			func() ([]sdk.Msg, []cryptotypes.PrivKey, []uint64, []uint64, sdk.Coins) {
   383  				msgs := []sdk.Msg{
   384  					govtypes.NewMsgSetNetworkProperties(accounts[0].acc.GetAddress(), &govtypes.NetworkProperties{
   385  						MinTxFee:                 2,
   386  						MaxTxFee:                 10000,
   387  						EnableForeignFeePayments: true,
   388  					}),
   389  				}
   390  				return msgs, privs[0:1], accNums[0:1], []uint64{0}, sdk.NewCoins(sdk.NewInt64Coin("nofeetoken", 100))
   391  			},
   392  			true,
   393  			false,
   394  			errors.New("currency you are trying to use was not whitelisted as fee payment: invalid request"),
   395  		},
   396  		{
   397  			"foreign fee payment disable check",
   398  			func() ([]sdk.Msg, []cryptotypes.PrivKey, []uint64, []uint64, sdk.Coins) {
   399  				err := suite.app.CustomGovKeeper.SetNetworkProperty(suite.ctx, govtypes.EnableForeignFeePayments, govtypes.NetworkPropertyValue{Value: 0})
   400  				suite.Require().NoError(err)
   401  				msgs := []sdk.Msg{
   402  					govtypes.NewMsgSetNetworkProperties(accounts[4].acc.GetAddress(), &govtypes.NetworkProperties{
   403  						MinTxFee:                 2,
   404  						MaxTxFee:                 10000,
   405  						EnableForeignFeePayments: true,
   406  					}),
   407  				}
   408  				return msgs, privs[0:1], accNums[0:1], []uint64{0}, sdk.NewCoins(sdk.NewInt64Coin("ubtc", 100))
   409  			},
   410  			true,
   411  			false,
   412  			errors.New("foreign fee payments is disabled by governance: invalid request"),
   413  		},
   414  		{
   415  			"fee out of range for low amount",
   416  			func() ([]sdk.Msg, []cryptotypes.PrivKey, []uint64, []uint64, sdk.Coins) {
   417  				err := suite.app.CustomGovKeeper.SetNetworkProperty(suite.ctx, govtypes.EnableForeignFeePayments, govtypes.NetworkPropertyValue{Value: 0})
   418  				suite.Require().NoError(err)
   419  				msgs := []sdk.Msg{
   420  					govtypes.NewMsgSetNetworkProperties(accounts[4].acc.GetAddress(), &govtypes.NetworkProperties{
   421  						MinTxFee:                 2,
   422  						MaxTxFee:                 10000,
   423  						EnableForeignFeePayments: true,
   424  					}),
   425  				}
   426  				return msgs, privs[0:1], accNums[0:1], []uint64{0}, sdk.NewCoins(sdk.NewInt64Coin("ukex", 1))
   427  			},
   428  			true,
   429  			false,
   430  			errors.New("fee 1ukex(1) is out of range [2, 10000]ukex: invalid request"),
   431  		},
   432  		{
   433  			"fee out of range for big amount",
   434  			func() ([]sdk.Msg, []cryptotypes.PrivKey, []uint64, []uint64, sdk.Coins) {
   435  				err := suite.app.CustomGovKeeper.SetNetworkProperty(suite.ctx, govtypes.EnableForeignFeePayments, govtypes.NetworkPropertyValue{Value: 0})
   436  				suite.Require().NoError(err)
   437  				msgs := []sdk.Msg{
   438  					govtypes.NewMsgSetNetworkProperties(accounts[4].acc.GetAddress(), &govtypes.NetworkProperties{
   439  						MinTxFee:                 2,
   440  						MaxTxFee:                 10000,
   441  						EnableForeignFeePayments: true,
   442  					}),
   443  				}
   444  				return msgs, privs[0:1], accNums[0:1], []uint64{0}, sdk.NewCoins(sdk.NewInt64Coin("ukex", 10001))
   445  			},
   446  			true,
   447  			false,
   448  			errors.New("fee 10001ukex(10001) is out of range [2, 10000]ukex: invalid request"),
   449  		},
   450  		{
   451  			"fee should be bigger than max of execution and failure fee",
   452  			func() ([]sdk.Msg, []cryptotypes.PrivKey, []uint64, []uint64, sdk.Coins) {
   453  				err := suite.app.CustomGovKeeper.SetNetworkProperty(suite.ctx, govtypes.EnableForeignFeePayments, govtypes.NetworkPropertyValue{Value: 0})
   454  				suite.Require().NoError(err)
   455  				msgs := []sdk.Msg{
   456  					govtypes.NewMsgSetNetworkProperties(accounts[4].acc.GetAddress(), &govtypes.NetworkProperties{
   457  						MinTxFee:                 2,
   458  						MaxTxFee:                 10000,
   459  						EnableForeignFeePayments: true,
   460  					}),
   461  				}
   462  				return msgs, privs[0:1], accNums[0:1], []uint64{0}, sdk.NewCoins(sdk.NewInt64Coin("ukex", 3))
   463  			},
   464  			true,
   465  			false,
   466  			errors.New("fee 3ukex(3) is less than max execution fee 10000ukex: invalid request"),
   467  		},
   468  	}
   469  
   470  	for _, tc := range testCases {
   471  		suite.Run(fmt.Sprintf("Case %s", tc.desc), func() {
   472  			suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder()
   473  			msgs, privs, accNums, accSeqs, feeAmount := tc.buildTest()
   474  
   475  			// this runs multi signature transaction with the params provided
   476  			suite.RunTestCase(privs, msgs, feeAmount, gasLimit, accNums, accSeqs, suite.ctx.ChainID(), tc)
   477  		})
   478  	}
   479  }
   480  
   481  // Test that simulate transaction process poor network manager correctly on ante handler step
   482  func (suite *AnteTestSuite) TestPoorNetworkManagementDecorator() {
   483  	suite.SetupTest(false) // reset
   484  
   485  	// set execution fee for set network properties
   486  	suite.app.CustomGovKeeper.SetNetworkProperties(suite.ctx, &govtypes.NetworkProperties{
   487  		MinTxFee:                 2,
   488  		MaxTxFee:                 10000,
   489  		EnableForeignFeePayments: true,
   490  		EnableTokenBlacklist:     true,
   491  		EnableTokenWhitelist:     false,
   492  		MinValidators:            10,
   493  		PoorNetworkMaxBankSend:   1000,
   494  	})
   495  
   496  	suite.app.CustomGovKeeper.SetExecutionFee(suite.ctx, govtypes.ExecutionFee{
   497  		TransactionType:   types.MsgTypeSetNetworkProperties,
   498  		ExecutionFee:      10000,
   499  		FailureFee:        1000,
   500  		Timeout:           0,
   501  		DefaultParameters: 0,
   502  	})
   503  
   504  	// Same data for every test cases
   505  	accounts := suite.CreateTestAccounts(5)
   506  
   507  	suite.SetBalance(accounts[0].acc.GetAddress(), sdk.NewInt64Coin("ukex", 10000))
   508  	suite.SetBalance(accounts[0].acc.GetAddress(), sdk.NewInt64Coin("frozen", 10000))
   509  	suite.SetBalance(accounts[0].acc.GetAddress(), sdk.NewInt64Coin("nofeetoken", 10000))
   510  	suite.SetBalance(accounts[1].acc.GetAddress(), sdk.NewInt64Coin("ukex", 10000))
   511  	suite.SetBalance(accounts[2].acc.GetAddress(), sdk.NewInt64Coin("ukex", 10000))
   512  	suite.SetBalance(accounts[3].acc.GetAddress(), sdk.NewInt64Coin("ukex", 1))
   513  	suite.SetBalance(accounts[4].acc.GetAddress(), sdk.NewInt64Coin("ukex", 10000))
   514  	suite.SetBalance(accounts[4].acc.GetAddress(), sdk.NewInt64Coin("ubtc", 10000))
   515  	gasLimit := testdata.NewTestGasLimit()
   516  	privs := []cryptotypes.PrivKey{accounts[0].priv, accounts[1].priv, accounts[2].priv, accounts[3].priv, accounts[4].priv}
   517  	accNums := []uint64{0, 1, 2, 3, 4}
   518  
   519  	testCases := []TestCase{
   520  		{
   521  			"only bond denom is allowed on poor network",
   522  			func() ([]sdk.Msg, []cryptotypes.PrivKey, []uint64, []uint64, sdk.Coins) {
   523  				msgs := []sdk.Msg{
   524  					bank.NewMsgSend(accounts[4].acc.GetAddress(), accounts[3].acc.GetAddress(), sdk.Coins{sdk.NewInt64Coin("ubtc", 1)}),
   525  				}
   526  				return msgs, privs[0:1], accNums[0:1], []uint64{0}, sdk.NewCoins(sdk.NewInt64Coin("ukex", 100))
   527  			},
   528  			true,
   529  			false,
   530  			errors.New("only bond denom is allowed on poor network"),
   531  		},
   532  		{
   533  			"only restricted amount send is allowed on poor network",
   534  			func() ([]sdk.Msg, []cryptotypes.PrivKey, []uint64, []uint64, sdk.Coins) {
   535  				msgs := []sdk.Msg{
   536  					bank.NewMsgSend(accounts[4].acc.GetAddress(), accounts[3].acc.GetAddress(), sdk.Coins{sdk.NewInt64Coin("ukex", 2000)}),
   537  				}
   538  				return msgs, privs[0:1], accNums[0:1], []uint64{0}, sdk.NewCoins(sdk.NewInt64Coin("ukex", 100))
   539  			},
   540  			true,
   541  			false,
   542  			errors.New("only restricted amount send is allowed on poor network"),
   543  		},
   544  		{
   545  			"invalid transaction type on poor network",
   546  			func() ([]sdk.Msg, []cryptotypes.PrivKey, []uint64, []uint64, sdk.Coins) {
   547  				msgs := []sdk.Msg{
   548  					tokenstypes.NewMsgUpsertTokenRate(
   549  						accounts[4].acc.GetAddress(),
   550  						"foo",
   551  						sdk.NewDec(1),
   552  						true,
   553  						sdk.ZeroDec(),
   554  						sdk.ZeroInt(),
   555  						false,
   556  						false,
   557  					),
   558  				}
   559  				return msgs, privs[0:1], accNums[0:1], []uint64{0}, sdk.NewCoins(sdk.NewInt64Coin("ukex", 100))
   560  			},
   561  			true,
   562  			false,
   563  			errors.New("invalid transaction type on poor network"),
   564  		},
   565  	}
   566  
   567  	for _, tc := range testCases {
   568  		suite.Run(fmt.Sprintf("Case %s", tc.desc), func() {
   569  			suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder()
   570  			msgs, privs, accNums, accSeqs, feeAmount := tc.buildTest()
   571  
   572  			// this runs multi signature transaction with the params provided
   573  			suite.RunTestCase(privs, msgs, feeAmount, gasLimit, accNums, accSeqs, suite.ctx.ChainID(), tc)
   574  		})
   575  	}
   576  }
   577  
   578  // Test that simulate transaction process black/white tokens on transfer
   579  func (suite *AnteTestSuite) TestBlackWhiteTokensCheckDecorator() {
   580  	suite.SetupTest(false) // reset
   581  
   582  	// set execution fee for set network properties
   583  	suite.app.CustomGovKeeper.SetNetworkProperties(suite.ctx, &govtypes.NetworkProperties{
   584  		MinTxFee:                 2,
   585  		MaxTxFee:                 10000,
   586  		EnableForeignFeePayments: true,
   587  		EnableTokenBlacklist:     true,
   588  		EnableTokenWhitelist:     false,
   589  		MinValidators:            0,
   590  		PoorNetworkMaxBankSend:   1000,
   591  	})
   592  
   593  	suite.app.CustomGovKeeper.SetExecutionFee(suite.ctx, govtypes.ExecutionFee{
   594  		TransactionType:   types.MsgTypeSetNetworkProperties,
   595  		ExecutionFee:      10000,
   596  		FailureFee:        1000,
   597  		Timeout:           0,
   598  		DefaultParameters: 0,
   599  	})
   600  
   601  	// Same data for every test cases
   602  	accounts := suite.CreateTestAccounts(5)
   603  
   604  	suite.SetBalance(accounts[0].acc.GetAddress(), sdk.NewInt64Coin("ukex", 10000))
   605  	suite.SetBalance(accounts[0].acc.GetAddress(), sdk.NewInt64Coin("frozen", 10000))
   606  	suite.SetBalance(accounts[0].acc.GetAddress(), sdk.NewInt64Coin("nofeetoken", 10000))
   607  	suite.SetBalance(accounts[1].acc.GetAddress(), sdk.NewInt64Coin("ukex", 10000))
   608  	suite.SetBalance(accounts[2].acc.GetAddress(), sdk.NewInt64Coin("ukex", 10000))
   609  	suite.SetBalance(accounts[3].acc.GetAddress(), sdk.NewInt64Coin("ukex", 1))
   610  	suite.SetBalance(accounts[4].acc.GetAddress(), sdk.NewInt64Coin("ukex", 10000))
   611  	suite.SetBalance(accounts[4].acc.GetAddress(), sdk.NewInt64Coin("ubtc", 10000))
   612  	suite.SetBalance(accounts[4].acc.GetAddress(), sdk.NewInt64Coin("frozen", 10000))
   613  	gasLimit := testdata.NewTestGasLimit()
   614  	privs := []cryptotypes.PrivKey{accounts[0].priv, accounts[1].priv, accounts[2].priv, accounts[3].priv, accounts[4].priv}
   615  	accNums := []uint64{0, 1, 2, 3, 4}
   616  
   617  	testCases := []TestCase{
   618  		{
   619  			"token frozen check",
   620  			func() ([]sdk.Msg, []cryptotypes.PrivKey, []uint64, []uint64, sdk.Coins) {
   621  				msgs := []sdk.Msg{
   622  					bank.NewMsgSend(accounts[4].acc.GetAddress(), accounts[3].acc.GetAddress(), sdk.Coins{sdk.NewInt64Coin("frozen", 1)}),
   623  				}
   624  				return msgs, privs[0:1], accNums[0:1], []uint64{0}, sdk.NewCoins(sdk.NewInt64Coin("ukex", 100))
   625  			},
   626  			true,
   627  			false,
   628  			errors.New("token is frozen"),
   629  		},
   630  		{
   631  			"no frozen",
   632  			func() ([]sdk.Msg, []cryptotypes.PrivKey, []uint64, []uint64, sdk.Coins) {
   633  				msgs := []sdk.Msg{
   634  					bank.NewMsgSend(accounts[4].acc.GetAddress(), accounts[3].acc.GetAddress(), sdk.Coins{sdk.NewInt64Coin("ukex", 1)}),
   635  				}
   636  				return msgs, privs[0:1], accNums[0:1], []uint64{0}, sdk.NewCoins(sdk.NewInt64Coin("ukex", 100))
   637  			},
   638  			true,
   639  			true,
   640  			nil,
   641  		},
   642  	}
   643  
   644  	for _, tc := range testCases {
   645  		suite.Run(fmt.Sprintf("Case %s", tc.desc), func() {
   646  			suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder()
   647  			msgs, privs, accNums, accSeqs, feeAmount := tc.buildTest()
   648  
   649  			// this runs multi signature transaction with the params provided
   650  			suite.RunTestCase(privs, msgs, feeAmount, gasLimit, accNums, accSeqs, suite.ctx.ChainID(), tc)
   651  		})
   652  	}
   653  }
   654  
   655  // Test that simulate transaction process execution fee registration process
   656  func (suite *AnteTestSuite) TestExecutionFeeRegistrationDecorator() {
   657  	suite.SetupTest(false) // reset
   658  
   659  	// set execution fee for set network properties
   660  	suite.app.CustomGovKeeper.SetNetworkProperties(suite.ctx, &govtypes.NetworkProperties{
   661  		MinTxFee:                 2,
   662  		MaxTxFee:                 10000,
   663  		EnableForeignFeePayments: true,
   664  		EnableTokenBlacklist:     true,
   665  		EnableTokenWhitelist:     false,
   666  		MinValidators:            0,
   667  		PoorNetworkMaxBankSend:   1000,
   668  	})
   669  
   670  	suite.app.CustomGovKeeper.SetExecutionFee(suite.ctx, govtypes.ExecutionFee{
   671  		TransactionType:   types.MsgTypeSetNetworkProperties,
   672  		ExecutionFee:      10000,
   673  		FailureFee:        1000,
   674  		Timeout:           0,
   675  		DefaultParameters: 0,
   676  	})
   677  
   678  	// Same data for every test cases
   679  	accounts := suite.CreateTestAccounts(5)
   680  
   681  	suite.SetBalance(accounts[0].acc.GetAddress(), sdk.NewInt64Coin("ukex", 10000))
   682  	suite.SetBalance(accounts[0].acc.GetAddress(), sdk.NewInt64Coin("frozen", 10000))
   683  	suite.SetBalance(accounts[0].acc.GetAddress(), sdk.NewInt64Coin("nofeetoken", 10000))
   684  	suite.SetBalance(accounts[1].acc.GetAddress(), sdk.NewInt64Coin("ukex", 10000))
   685  	suite.SetBalance(accounts[2].acc.GetAddress(), sdk.NewInt64Coin("ukex", 10000))
   686  	suite.SetBalance(accounts[3].acc.GetAddress(), sdk.NewInt64Coin("ukex", 1))
   687  	suite.SetBalance(accounts[4].acc.GetAddress(), sdk.NewInt64Coin("ukex", 10000))
   688  	suite.SetBalance(accounts[4].acc.GetAddress(), sdk.NewInt64Coin("ubtc", 10000))
   689  	suite.SetBalance(accounts[4].acc.GetAddress(), sdk.NewInt64Coin("frozen", 10000))
   690  	gasLimit := testdata.NewTestGasLimit()
   691  	privs := []cryptotypes.PrivKey{accounts[0].priv, accounts[1].priv, accounts[2].priv, accounts[3].priv, accounts[4].priv}
   692  	accNums := []uint64{0, 1, 2, 3, 4}
   693  
   694  	testCases := []TestCase{
   695  		{
   696  			"check correctly add executions",
   697  			func() ([]sdk.Msg, []cryptotypes.PrivKey, []uint64, []uint64, sdk.Coins) {
   698  				msgs := []sdk.Msg{
   699  					govtypes.NewMsgSetNetworkProperties(accounts[0].acc.GetAddress(), &govtypes.NetworkProperties{
   700  						MinTxFee:                 2,
   701  						MaxTxFee:                 10000,
   702  						EnableForeignFeePayments: true,
   703  					}),
   704  				}
   705  				return msgs, privs[0:1], accNums[0:1], []uint64{0}, sdk.NewCoins(sdk.NewInt64Coin("ukex", 10000))
   706  			},
   707  			true,
   708  			true,
   709  			nil,
   710  		},
   711  	}
   712  
   713  	for _, tc := range testCases {
   714  		suite.Run(fmt.Sprintf("Case %s", tc.desc), func() {
   715  			suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder()
   716  			msgs, privs, accNums, accSeqs, feeAmount := tc.buildTest()
   717  
   718  			// this runs multi signature transaction with the params provided
   719  			suite.RunTestCase(privs, msgs, feeAmount, gasLimit, accNums, accSeqs, suite.ctx.ChainID(), tc)
   720  			execs := suite.app.FeeProcessingKeeper.GetExecutionsStatus(suite.ctx)
   721  			suite.Require().Len(execs, 1)
   722  		})
   723  	}
   724  }
   725  
   726  // Test that simulate transaction set gas limit correctly on ante handler step
   727  func (suite *AnteTestSuite) TestInfiniteGasMeterDecorator() {
   728  	suite.SetupTest(true) // setup
   729  	suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder()
   730  
   731  	// keys and addresses
   732  	priv1, _, addr1 := testdata.KeyTestPubAddr()
   733  
   734  	// msg and signatures
   735  	msg := testdata.NewTestMsg(addr1)
   736  	feeAmount := testdata.NewTestFeeAmount()
   737  	gasLimit := testdata.NewTestGasLimit()
   738  	suite.Require().NoError(suite.txBuilder.SetMsgs(msg))
   739  	suite.txBuilder.SetFeeAmount(feeAmount)
   740  	suite.txBuilder.SetGasLimit(gasLimit)
   741  
   742  	privs, accNums, accSeqs := []cryptotypes.PrivKey{priv1}, []uint64{0}, []uint64{0}
   743  	tx, err := suite.CreateTestTx(privs, accNums, accSeqs, suite.ctx.ChainID())
   744  	suite.Require().NoError(err)
   745  
   746  	sud := customante.NewZeroGasMeterDecorator()
   747  	antehandler := sdk.ChainAnteDecorators(sud)
   748  
   749  	// Set height to non-zero value for GasMeter to be set
   750  	suite.ctx = suite.ctx.WithBlockHeight(1)
   751  
   752  	// Context GasMeter Limit not set
   753  	suite.Require().Equal(uint64(0xffffffffffffffff), suite.ctx.GasMeter().Limit(), "GasMeter set with limit before setup")
   754  
   755  	newCtx, err := antehandler(suite.ctx, tx, false)
   756  	suite.Require().Nil(err, "InfiniteGasMeterDecorator returned error")
   757  
   758  	// Context GasMeter Limit should be set after InfiniteGasMeterDecorator runs
   759  	suite.Require().Equal(uint64(0x0), newCtx.GasMeter().Limit(), "GasMeter not set correctly")
   760  
   761  	sud = customante.NewZeroGasMeterDecorator()
   762  	antehandler = sdk.ChainAnteDecorators(sud, OutOfGasDecorator{})
   763  
   764  	// Set height to non-zero value for GasMeter to be set
   765  	suite.ctx = suite.ctx.WithBlockHeight(1)
   766  	newCtx, err = antehandler(suite.ctx, tx, false)
   767  	suite.Require().Nil(err, "no error for gas overflow")
   768  
   769  	antehandler = sdk.ChainAnteDecorators(sud, PanicDecorator{})
   770  	suite.Require().Panics(func() { antehandler(suite.ctx, tx, false) }, "Recovered from non-Out-of-Gas panic") // nolint:errcheck
   771  }
   772  
   773  type OutOfGasDecorator struct{}
   774  
   775  // AnteDecorator that will throw OutOfGas panic
   776  func (ogd OutOfGasDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
   777  	overLimit := ctx.GasMeter().Limit() + 1
   778  
   779  	// Should panic with outofgas error
   780  	ctx.GasMeter().ConsumeGas(overLimit, "test panic")
   781  
   782  	// not reached
   783  	return next(ctx, tx, simulate)
   784  }
   785  
   786  type PanicDecorator struct{}
   787  
   788  func (pd PanicDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
   789  	panic("random error")
   790  }