/
fee_test.go
148 lines (113 loc) · 5.15 KB
/
fee_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package ante_test
import (
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
"github.com/cosmos/cosmos-sdk/x/bank/testutil"
)
func (s *AnteTestSuite) TestDeductFeeDecorator_ZeroGas() {
s.SetupTest(true) // setup
s.txBuilder = s.clientCtx.TxConfig.NewTxBuilder()
mfd := ante.NewDeductFeeDecorator(s.accountKeeper, s.bankKeeper, s.feeGrantKeeper, nil)
antehandler := sdk.ChainAnteDecorators(mfd)
// keys and addresses
priv1, _, addr1 := testdata.KeyTestPubAddr()
coins := sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(300)))
testutil.FundAccount(s.bankKeeper, s.ctx, addr1, coins)
// msg and signatures
msg := testdata.NewTestMsg(addr1)
s.Require().NoError(s.txBuilder.SetMsgs(msg))
// set zero gas
s.txBuilder.SetGasLimit(0)
privs, accNums, accSeqs := []cryptotypes.PrivKey{priv1}, []uint64{0}, []uint64{0}
tx, err := s.CreateTestTx(privs, accNums, accSeqs, s.ctx.ChainID())
s.Require().NoError(err)
// Set IsCheckTx to true
s.ctx = s.ctx.WithIsCheckTx(true)
_, err = antehandler(s.ctx, tx, false)
s.Require().Error(err)
// zero gas is accepted in simulation mode
_, err = antehandler(s.ctx, tx, true)
s.Require().NoError(err)
}
func (s *AnteTestSuite) TestEnsureMempoolFees() {
s.SetupTest(true) // setup
s.txBuilder = s.clientCtx.TxConfig.NewTxBuilder()
mfd := ante.NewDeductFeeDecorator(s.accountKeeper, s.bankKeeper, s.feeGrantKeeper, nil)
antehandler := sdk.ChainAnteDecorators(mfd)
// keys and addresses
priv1, _, addr1 := testdata.KeyTestPubAddr()
coins := sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(300)))
testutil.FundAccount(s.bankKeeper, s.ctx, addr1, coins)
// msg and signatures
msg := testdata.NewTestMsg(addr1)
feeAmount := testdata.NewTestFeeAmount()
gasLimit := testdata.NewTestGasLimit()
s.Require().NoError(s.txBuilder.SetMsgs(msg))
s.txBuilder.SetFeeAmount(feeAmount)
s.txBuilder.SetGasLimit(gasLimit)
privs, accNums, accSeqs := []cryptotypes.PrivKey{priv1}, []uint64{0}, []uint64{0}
tx, err := s.CreateTestTx(privs, accNums, accSeqs, s.ctx.ChainID())
s.Require().NoError(err)
// Set high gas price so standard test fee fails
atomPrice := sdk.NewDecCoinFromDec("atom", sdk.NewDec(200).Quo(sdk.NewDec(100000)))
highGasPrice := []sdk.DecCoin{atomPrice}
s.ctx = s.ctx.WithMinGasPrices(highGasPrice)
// Set IsCheckTx to true
s.ctx = s.ctx.WithIsCheckTx(true)
// antehandler errors with insufficient fees
_, err = antehandler(s.ctx, tx, false)
s.Require().NotNil(err, "Decorator should have errored on too low fee for local gasPrice")
// antehandler should not error since we do not check minGasPrice in simulation mode
cacheCtx, _ := s.ctx.CacheContext()
_, err = antehandler(cacheCtx, tx, true)
s.Require().Nil(err, "Decorator should not have errored in simulation mode")
// Set IsCheckTx to false
s.ctx = s.ctx.WithIsCheckTx(false)
// antehandler should not error since we do not check minGasPrice in DeliverTx
_, err = antehandler(s.ctx, tx, false)
s.Require().Nil(err, "MempoolFeeDecorator returned error in DeliverTx")
// Set IsCheckTx back to true for testing sufficient mempool fee
s.ctx = s.ctx.WithIsCheckTx(true)
atomPrice = sdk.NewDecCoinFromDec("atom", sdk.NewDec(0).Quo(sdk.NewDec(100000)))
lowGasPrice := []sdk.DecCoin{atomPrice}
s.ctx = s.ctx.WithMinGasPrices(lowGasPrice)
newCtx, err := antehandler(s.ctx, tx, false)
s.Require().Nil(err, "Decorator should not have errored on fee higher than local gasPrice")
// Priority is the smallest amount in any denom. Since we have only 1 fee
// of 150atom, the priority here is 150.
s.Require().Equal(feeAmount.AmountOf("atom").Int64(), newCtx.Priority())
}
func (s *AnteTestSuite) TestDeductFees() {
s.SetupTest(false) // setup
s.txBuilder = s.clientCtx.TxConfig.NewTxBuilder()
// keys and addresses
priv1, _, addr1 := testdata.KeyTestPubAddr()
// msg and signatures
msg := testdata.NewTestMsg(addr1)
feeAmount := testdata.NewTestFeeAmount()
gasLimit := testdata.NewTestGasLimit()
s.Require().NoError(s.txBuilder.SetMsgs(msg))
s.txBuilder.SetFeeAmount(feeAmount)
s.txBuilder.SetGasLimit(gasLimit)
privs, accNums, accSeqs := []cryptotypes.PrivKey{priv1}, []uint64{0}, []uint64{0}
tx, err := s.CreateTestTx(privs, accNums, accSeqs, s.ctx.ChainID())
s.Require().NoError(err)
// Set account with insufficient funds
acc := s.accountKeeper.NewAccountWithAddress(s.ctx, addr1)
s.accountKeeper.SetAccount(s.ctx, acc)
coins := sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(10)))
err = testutil.FundAccount(s.bankKeeper, s.ctx, addr1, coins)
s.Require().NoError(err)
dfd := ante.NewDeductFeeDecorator(s.accountKeeper, s.bankKeeper, nil, nil)
antehandler := sdk.ChainAnteDecorators(dfd)
_, err = antehandler(s.ctx, tx, false)
s.Require().NotNil(err, "Tx did not error when fee payer had insufficient funds")
// Set account with sufficient funds
s.accountKeeper.SetAccount(s.ctx, acc)
err = testutil.FundAccount(s.bankKeeper, s.ctx, addr1, sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(200))))
s.Require().NoError(err)
_, err = antehandler(s.ctx, tx, false)
s.Require().Nil(err, "Tx errored after account has been set with sufficient funds")
}