-
Notifications
You must be signed in to change notification settings - Fork 8
/
suite.go
322 lines (272 loc) · 9.16 KB
/
suite.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package suite
import (
"testing"
txsigning "cosmossdk.io/x/tx/signing"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/address"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/std"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/gogoproto/proto"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
testkeeper "github.com/skip-mev/feemarket/testutils/keeper"
feemarketante "github.com/skip-mev/feemarket/x/feemarket/ante"
"github.com/skip-mev/feemarket/x/feemarket/ante/mocks"
feemarketpost "github.com/skip-mev/feemarket/x/feemarket/post"
feemarkettypes "github.com/skip-mev/feemarket/x/feemarket/types"
)
type TestSuite struct {
suite.Suite
Ctx sdk.Context
AnteHandler sdk.AnteHandler
PostHandler sdk.PostHandler
ClientCtx client.Context
TxBuilder client.TxBuilder
AccountKeeper feemarketante.AccountKeeper
FeeMarketKeeper feemarketpost.FeeMarketKeeper
BankKeeper feemarketante.BankKeeper
FeeGrantKeeper feemarketante.FeeGrantKeeper
MockBankKeeper *mocks.BankKeeper
MockFeeGrantKeeper *mocks.FeeGrantKeeper
EncCfg TestEncodingConfig
}
// TestAccount represents an account used in the tests in x/auth/ante.
type TestAccount struct {
Account sdk.AccountI
Priv cryptotypes.PrivKey
}
func (s *TestSuite) CreateTestAccounts(numAccs int) []TestAccount {
var accounts []TestAccount
for i := 0; i < numAccs; i++ {
priv, _, addr := testdata.KeyTestPubAddr()
acc := s.AccountKeeper.NewAccountWithAddress(s.Ctx, addr)
err := acc.SetAccountNumber(uint64(i + 1000))
if err != nil {
panic(err)
}
s.AccountKeeper.SetAccount(s.Ctx, acc)
accounts = append(accounts, TestAccount{acc, priv})
}
return accounts
}
// SetupTestSuite setups a new test, with new app, context, and anteHandler.
func SetupTestSuite(t *testing.T, mock bool) *TestSuite {
s := &TestSuite{}
s.EncCfg = MakeTestEncodingConfig()
ctx, testKeepers, _ := testkeeper.NewTestSetup(t)
s.Ctx = ctx
s.AccountKeeper = testKeepers.AccountKeeper
s.FeeMarketKeeper = testKeepers.FeeMarketKeeper
s.MockBankKeeper = mocks.NewBankKeeper(t)
s.MockFeeGrantKeeper = mocks.NewFeeGrantKeeper(t)
s.ClientCtx = client.Context{}.WithTxConfig(s.EncCfg.TxConfig)
s.TxBuilder = s.ClientCtx.TxConfig.NewTxBuilder()
s.SetupHandlers(mock)
s.SetT(t)
return s
}
func (s *TestSuite) SetupHandlers(mock bool) {
bankKeeper := s.BankKeeper
feeGrantKeeper := s.FeeGrantKeeper
if mock {
bankKeeper = s.MockBankKeeper
feeGrantKeeper = s.MockFeeGrantKeeper
}
// create basic antehandler with the feemarket decorator
anteDecorators := []sdk.AnteDecorator{
authante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
feemarketante.NewFeeMarketCheckDecorator( // fee market replaces fee deduct decorator
s.FeeMarketKeeper,
authante.NewDeductFeeDecorator(
s.AccountKeeper,
s.BankKeeper,
s.FeeGrantKeeper,
nil,
),
),
authante.NewSigGasConsumeDecorator(s.AccountKeeper, authante.DefaultSigVerificationGasConsumer),
}
s.AnteHandler = sdk.ChainAnteDecorators(anteDecorators...)
// create basic postHandler with the feemarket decorator
postDecorators := []sdk.PostDecorator{
feemarketpost.NewFeeMarketDeductDecorator(
s.AccountKeeper,
bankKeeper,
feeGrantKeeper,
s.FeeMarketKeeper,
),
}
s.PostHandler = sdk.ChainPostDecorators(postDecorators...)
}
// TestCase represents a test case used in test tables.
type TestCase struct {
Name string
Malleate func(*TestSuite) TestCaseArgs
RunAnte bool
RunPost bool
Simulate bool
ExpPass bool
ExpErr error
}
type TestCaseArgs struct {
ChainID string
AccNums []uint64
AccSeqs []uint64
FeeAmount sdk.Coins
GasLimit uint64
Msgs []sdk.Msg
Privs []cryptotypes.PrivKey
}
// DeliverMsgs constructs a tx and runs it through the ante handler. This is used to set the context for a test case, for
// example to test for replay protection.
func (s *TestSuite) DeliverMsgs(t *testing.T, privs []cryptotypes.PrivKey, msgs []sdk.Msg, feeAmount sdk.Coins, gasLimit uint64, accNums, accSeqs []uint64, chainID string, simulate bool) (sdk.Context, error) {
require.NoError(t, s.TxBuilder.SetMsgs(msgs...))
s.TxBuilder.SetFeeAmount(feeAmount)
s.TxBuilder.SetGasLimit(gasLimit)
tx, txErr := s.CreateTestTx(privs, accNums, accSeqs, chainID)
require.NoError(t, txErr)
return s.AnteHandler(s.Ctx, tx, simulate)
}
func (s *TestSuite) RunTestCase(t *testing.T, tc TestCase, args TestCaseArgs) {
require.NoError(t, s.TxBuilder.SetMsgs(args.Msgs...))
s.TxBuilder.SetFeeAmount(args.FeeAmount)
s.TxBuilder.SetGasLimit(args.GasLimit)
// Theoretically speaking, ante handler unit tests should only test
// ante handlers, but here we sometimes also test the tx creation
// process.
tx, txErr := s.CreateTestTx(args.Privs, args.AccNums, args.AccSeqs, args.ChainID)
var (
newCtx sdk.Context
handleErr error
)
if tc.RunAnte {
newCtx, handleErr = s.AnteHandler(s.Ctx, tx, tc.Simulate)
}
if tc.RunPost {
newCtx, handleErr = s.PostHandler(s.Ctx, tx, tc.Simulate, true)
}
if tc.ExpPass {
require.NoError(t, txErr)
require.NoError(t, handleErr)
require.NotNil(t, newCtx)
s.Ctx = newCtx
} else {
switch {
case txErr != nil:
require.Error(t, txErr)
require.ErrorIs(t, txErr, tc.ExpErr)
case handleErr != nil:
require.Error(t, handleErr)
require.ErrorIs(t, handleErr, tc.ExpErr)
default:
t.Fatal("expected one of txErr, handleErr to be an error")
}
}
}
// CreateTestTx is a helper function to create a tx given multiple inputs.
func (s *TestSuite) CreateTestTx(privs []cryptotypes.PrivKey, accNums []uint64, accSeqs []uint64, chainID string) (authsigning.Tx, error) {
// First round: we gather all the signer infos. We use the "set empty
// signature" hack to do that.
var sigsV2 []signing.SignatureV2
for i, priv := range privs {
sigV2 := signing.SignatureV2{
PubKey: priv.PubKey(),
Data: &signing.SingleSignatureData{
SignMode: signing.SignMode(s.ClientCtx.TxConfig.SignModeHandler().DefaultMode()),
Signature: nil,
},
Sequence: accSeqs[i],
}
sigsV2 = append(sigsV2, sigV2)
}
err := s.TxBuilder.SetSignatures(sigsV2...)
if err != nil {
return nil, err
}
// Second round: all signer infos are set, so each signer can sign.
sigsV2 = []signing.SignatureV2{}
for i, priv := range privs {
signerData := authsigning.SignerData{
ChainID: chainID,
AccountNumber: accNums[i],
Sequence: accSeqs[i],
}
sigV2, err := tx.SignWithPrivKey(
s.Ctx,
signing.SignMode(s.ClientCtx.TxConfig.SignModeHandler().DefaultMode()), signerData,
s.TxBuilder, priv, s.ClientCtx.TxConfig, accSeqs[i])
if err != nil {
return nil, err
}
sigsV2 = append(sigsV2, sigV2)
}
err = s.TxBuilder.SetSignatures(sigsV2...)
if err != nil {
return nil, err
}
return s.TxBuilder.GetTx(), nil
}
// NewTestFeeAmount is a test fee amount.
func NewTestFeeAmount() sdk.Coins {
return sdk.NewCoins(sdk.NewInt64Coin("stake", 150))
}
// NewTestGasLimit is a test fee gas limit.
func NewTestGasLimit() uint64 {
return 200000
}
// TestEncodingConfig specifies the concrete encoding types to use for a given app.
// This is provided for compatibility between protobuf and amino implementations.
type TestEncodingConfig struct {
InterfaceRegistry codectypes.InterfaceRegistry
Codec codec.Codec
TxConfig client.TxConfig
Amino *codec.LegacyAmino
}
// MakeTestEncodingConfig creates a test EncodingConfig for a test configuration.
func MakeTestEncodingConfig() TestEncodingConfig {
amino := codec.NewLegacyAmino()
interfaceRegistry := InterfaceRegistry()
cdc := codec.NewProtoCodec(interfaceRegistry)
txCfg := authtx.NewTxConfig(cdc, authtx.DefaultSignModes)
std.RegisterLegacyAminoCodec(amino)
std.RegisterInterfaces(interfaceRegistry)
return TestEncodingConfig{
InterfaceRegistry: interfaceRegistry,
Codec: cdc,
TxConfig: txCfg,
Amino: amino,
}
}
func InterfaceRegistry() codectypes.InterfaceRegistry {
interfaceRegistry, err := codectypes.NewInterfaceRegistryWithOptions(codectypes.InterfaceRegistryOptions{
ProtoFiles: proto.HybridResolver,
SigningOptions: txsigning.Options{
AddressCodec: address.Bech32Codec{
Bech32Prefix: sdk.GetConfig().GetBech32AccountAddrPrefix(),
},
ValidatorAddressCodec: address.Bech32Codec{
Bech32Prefix: sdk.GetConfig().GetBech32ValidatorAddrPrefix(),
},
},
})
if err != nil {
panic(err)
}
// always register
cryptocodec.RegisterInterfaces(interfaceRegistry)
authtypes.RegisterInterfaces(interfaceRegistry)
// call extra registry functions
feemarkettypes.RegisterInterfaces(interfaceRegistry)
return interfaceRegistry
}