-
Notifications
You must be signed in to change notification settings - Fork 879
Expand file tree
/
Copy pathtest_helpers.go
More file actions
1028 lines (882 loc) · 30.2 KB
/
test_helpers.go
File metadata and controls
1028 lines (882 loc) · 30.2 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package app
import (
"context"
"encoding/json"
"fmt"
"math/rand"
"os"
"path/filepath"
"testing"
"time"
"github.com/cosmos/cosmos-sdk/client"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
"github.com/cosmos/cosmos-sdk/x/staking"
gigalib "github.com/sei-protocol/sei-chain/giga/executor/lib"
ssconfig "github.com/sei-protocol/sei-chain/sei-db/config"
receipt "github.com/sei-protocol/sei-chain/sei-db/ledger_db/receipt"
"github.com/sei-protocol/sei-chain/sei-tendermint/config"
"github.com/sei-protocol/sei-chain/sei-wasmd/x/wasm"
wasmkeeper "github.com/sei-protocol/sei-chain/sei-wasmd/x/wasm/keeper"
evmtypes "github.com/sei-protocol/sei-chain/x/evm/types"
"github.com/stretchr/testify/suite"
"bytes"
"encoding/hex"
"strconv"
abci "github.com/sei-protocol/sei-chain/sei-tendermint/abci/types"
"github.com/sei-protocol/sei-chain/sei-tendermint/libs/log"
tmproto "github.com/sei-protocol/sei-chain/sei-tendermint/proto/tendermint/types"
tmtypes "github.com/sei-protocol/sei-chain/sei-tendermint/types"
"github.com/stretchr/testify/require"
dbm "github.com/tendermint/tm-db"
bam "github.com/cosmos/cosmos-sdk/baseapp"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authsign "github.com/cosmos/cosmos-sdk/x/auth/signing"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/sei-protocol/sei-chain/app/legacyabci"
minttypes "github.com/sei-protocol/sei-chain/x/mint/types"
gigaconfig "github.com/sei-protocol/sei-chain/giga/executor/config"
)
const TestContract = "TEST"
const TestUser = "sei1jdppe6fnj2q7hjsepty5crxtrryzhuqsjrj95y"
type TestTx struct {
msgs []sdk.Msg
}
func NewTestTx(msgs []sdk.Msg) TestTx {
return TestTx{msgs: msgs}
}
func (t TestTx) GetMsgs() []sdk.Msg {
return t.msgs
}
func (t TestTx) ValidateBasic() error {
return nil
}
func (t TestTx) GetGasEstimate() uint64 {
return 0
}
type TestAppOpts struct {
UseSc bool
EnableGiga bool
EnableGigaOCC bool
}
func (t TestAppOpts) Get(s string) interface{} {
if s == "chain-id" {
return "sei-test"
}
if s == FlagSCEnable {
return t.UseSc
}
// Disable snapshot creation in tests to avoid background goroutines
// that are not relevant to the test logic
if s == FlagSCSnapshotInterval {
return uint32(0) // 0 = disabled
}
if s == gigaconfig.FlagEnabled {
return t.EnableGiga
}
if s == gigaconfig.FlagOCCEnabled {
return t.EnableGigaOCC
}
// Disable EVM HTTP and WebSocket servers in tests to avoid port conflicts
// when multiple tests run in parallel (all would try to bind to port 8545)
if s == "evm.http_enabled" || s == "evm.ws_enabled" {
return false
}
return nil
}
type TestWrapper struct {
suite.Suite
App *App
Ctx sdk.Context
// hasT tracks whether SetT was called, so we know if we can use Require()
hasT bool
}
func NewTestWrapper(tb testing.TB, tm time.Time, valPub cryptotypes.PubKey, enableEVMCustomPrecompiles bool, baseAppOptions ...func(*bam.BaseApp)) *TestWrapper {
return newTestWrapper(tb, tm, valPub, enableEVMCustomPrecompiles, false, TestAppOpts{}, baseAppOptions...)
}
func NewTestWrapperWithSc(t *testing.T, tm time.Time, valPub cryptotypes.PubKey, enableEVMCustomPrecompiles bool, baseAppOptions ...func(*bam.BaseApp)) *TestWrapper {
return newTestWrapper(t, tm, valPub, enableEVMCustomPrecompiles, true, TestAppOpts{UseSc: true}, baseAppOptions...)
}
func NewGigaTestWrapper(t *testing.T, tm time.Time, valPub cryptotypes.PubKey, enableEVMCustomPrecompiles bool, useOcc bool, baseAppOptions ...func(*bam.BaseApp)) *TestWrapper {
wrapper := newTestWrapper(t, tm, valPub, enableEVMCustomPrecompiles, true, TestAppOpts{UseSc: true, EnableGiga: true, EnableGigaOCC: useOcc}, baseAppOptions...)
genState := evmtypes.DefaultGenesis()
wrapper.App.EvmKeeper.InitGenesis(wrapper.Ctx, *genState)
return wrapper
}
// NewGigaTestWrapperWithRegularStore creates a test wrapper that runs Giga executor
// but uses regular KVStore instead of GigaKVStore. This is for debugging - it isolates
// whether issues are in the Giga executor logic vs the GigaKVStore layer.
//
// How it works:
// - Creates app with UseSc=true but EnableGiga=false (so GigaKVStore is NOT registered)
// - Manually enables Giga executor flags on the app
// - Sets GigaEvmKeeper.UseRegularStore=true so it uses ctx.KVStore instead of ctx.GigaKVStore
func NewGigaTestWrapperWithRegularStore(t *testing.T, tm time.Time, valPub cryptotypes.PubKey, enableEVMCustomPrecompiles bool, useOcc bool, baseAppOptions ...func(*bam.BaseApp)) *TestWrapper {
// Create wrapper with Sc but WITHOUT EnableGiga - this means GigaKVStore won't be registered
wrapper := newTestWrapper(t, tm, valPub, enableEVMCustomPrecompiles, true, TestAppOpts{UseSc: true, EnableGiga: false, EnableGigaOCC: false}, baseAppOptions...)
// Manually enable Giga executor on the app
wrapper.App.GigaExecutorEnabled = true
wrapper.App.GigaOCCEnabled = useOcc
tmtypes.SkipLastResultsHashValidation.Store(true)
// Configure GigaEvmKeeper to use regular KVStore instead of GigaKVStore
wrapper.App.GigaEvmKeeper.UseRegularStore = true
// Configure GigaBankKeeper to use regular KVStore instead of GigaKVStore
wrapper.App.GigaBankKeeper.UseRegularStore = true
// Initialize evmone VM if not already initialized
if wrapper.App.GigaEvmKeeper.EvmoneVM == nil {
evmoneVM, err := gigalib.InitEvmoneVM()
if err != nil {
panic(fmt.Sprintf("failed to load evmone: %s", err))
}
wrapper.App.GigaEvmKeeper.EvmoneVM = evmoneVM
}
// Init genesis for GigaEvmKeeper (now uses regular KVStore)
genState := evmtypes.DefaultGenesis()
wrapper.App.EvmKeeper.InitGenesis(wrapper.Ctx, *genState)
return wrapper
}
func newTestWrapper(tb testing.TB, tm time.Time, valPub cryptotypes.PubKey, enableEVMCustomPrecompiles bool, UseSc bool, testAppOpts TestAppOpts, baseAppOptions ...func(*bam.BaseApp)) *TestWrapper {
var appPtr *App
originalHome := DefaultNodeHome
tempHome := tb.TempDir()
DefaultNodeHome = tempHome
tb.Cleanup(func() {
DefaultNodeHome = originalHome
})
if UseSc {
if testT, ok := tb.(*testing.T); ok {
appPtr = SetupWithSc(testT, false, enableEVMCustomPrecompiles, testAppOpts, baseAppOptions...)
} else {
panic("SetupWithSc requires *testing.T, cannot use with *testing.B")
}
} else {
appPtr = Setup(tb, false, enableEVMCustomPrecompiles, false, baseAppOptions...)
}
ctx := appPtr.NewContext(false, sdk.Header{Height: 1, ChainID: "sei-test", Time: tm})
wrapper := &TestWrapper{
App: appPtr,
Ctx: ctx,
hasT: false,
}
if testT, ok := tb.(*testing.T); ok {
wrapper.SetT(testT)
wrapper.hasT = true
}
wrapper.setupValidator(stakingtypes.Unbonded, valPub)
return wrapper
}
// requireNoError handles errors for both tests and benchmarks.
// For tests (when SetT was called), it uses Require().NoError().
// For benchmarks (when SetT wasn't called), it panics on error.
func (s *TestWrapper) requireNoError(err error) {
if err == nil {
return
}
if s.hasT {
s.Require().NoError(err)
} else {
// Benchmark context - panic on error
panic(err)
}
}
func (s *TestWrapper) FundAcc(acc sdk.AccAddress, amounts sdk.Coins) {
err := s.App.BankKeeper.MintCoins(s.Ctx, minttypes.ModuleName, amounts)
s.requireNoError(err)
err = s.App.BankKeeper.SendCoinsFromModuleToAccount(s.Ctx, minttypes.ModuleName, acc, amounts)
s.requireNoError(err)
}
func (s *TestWrapper) setupValidator(bondStatus stakingtypes.BondStatus, valPub cryptotypes.PubKey) sdk.ValAddress {
valAddr := sdk.ValAddress(valPub.Address())
bondDenom := s.App.StakingKeeper.GetParams(s.Ctx).BondDenom
selfBond := sdk.NewCoins(sdk.Coin{Amount: sdk.NewInt(100), Denom: bondDenom})
s.FundAcc(sdk.AccAddress(valAddr), selfBond)
// Create validator message and handle it
commission := stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 2), sdk.NewDecWithPrec(5, 2), sdk.ZeroDec())
msg, err := stakingtypes.NewMsgCreateValidator(valAddr, valPub, selfBond[0], stakingtypes.Description{}, commission, sdk.OneInt())
s.requireNoError(err)
handler := staking.NewHandler(s.App.StakingKeeper)
res, err := handler(s.Ctx, msg)
s.requireNoError(err)
if res == nil {
panic("validator creation returned nil result")
}
val, found := s.App.StakingKeeper.GetValidator(s.Ctx, valAddr)
if !found {
panic("validator not found after creation")
}
if s.hasT {
s.Require().True(found)
}
val = val.UpdateStatus(bondStatus)
s.App.StakingKeeper.SetValidator(s.Ctx, val)
consAddr, err := val.GetConsAddr()
s.requireNoError(err)
signingInfo := slashingtypes.NewValidatorSigningInfo(
consAddr,
s.Ctx.BlockHeight(),
0,
time.Unix(0, 0),
false,
0,
)
s.App.SlashingKeeper.SetValidatorSigningInfo(s.Ctx, consAddr, signingInfo)
return valAddr
}
func (s *TestWrapper) BeginBlock() {
var proposer sdk.ValAddress
validators := s.App.StakingKeeper.GetAllValidators(s.Ctx)
s.Require().Equal(1, len(validators))
valAddrFancy, err := validators[0].GetConsAddr()
s.Require().NoError(err)
proposer = valAddrFancy.Bytes()
validator, found := s.App.StakingKeeper.GetValidator(s.Ctx, proposer)
s.Assert().True(found)
valConsAddr, err := validator.GetConsAddr()
s.Require().NoError(err)
valAddr := valConsAddr.Bytes()
newBlockTime := s.Ctx.BlockTime().Add(2 * time.Second)
header := sdk.Header{Height: s.Ctx.BlockHeight() + 1, Time: newBlockTime}
newCtx := s.Ctx.WithBlockTime(newBlockTime).WithBlockHeight(s.Ctx.BlockHeight() + 1)
s.Ctx = newCtx
lastCommitInfo := abci.LastCommitInfo{
Votes: []abci.VoteInfo{{
Validator: abci.Validator{Address: valAddr, Power: 1000},
SignedLastBlock: true,
}},
}
legacyabci.BeginBlock(s.Ctx, header.Height, lastCommitInfo.Votes, []abci.Misbehavior{}, s.App.BeginBlockKeepers)
}
func (s *TestWrapper) EndBlock() {
legacyabci.EndBlock(s.Ctx, s.Ctx.BlockHeight(), 0, s.App.EndBlockKeepers)
}
func setupReceiptStore(storeKey sdk.StoreKey) (receipt.ReceiptStore, error) {
// Create a unique temporary directory per test process to avoid Pebble DB lock conflicts
baseDir := filepath.Join(os.TempDir(), "sei-testing")
if err := os.MkdirAll(baseDir, 0o750); err != nil {
return nil, err
}
tempDir, err := os.MkdirTemp(baseDir, "receipt.db-*")
if err != nil {
return nil, err
}
receiptConfig := ssconfig.DefaultReceiptStoreConfig()
receiptConfig.KeepRecent = 0 // No min retain blocks in test
receiptConfig.DBDirectory = tempDir
receiptStore, err := receipt.NewReceiptStore(log.NewNopLogger(), receiptConfig, storeKey)
if err != nil {
return nil, err
}
return receiptStore, nil
}
func SetupWithDefaultHome(isCheckTx bool, enableEVMCustomPrecompiles bool, overrideWasmGasMultiplier bool, baseAppOptions ...func(*bam.BaseApp)) (res *App) {
return SetupWithAppOptsAndDefaultHome(isCheckTx, TestAppOpts{}, enableEVMCustomPrecompiles, overrideWasmGasMultiplier, baseAppOptions...)
}
func SetupWithAppOptsAndDefaultHome(isCheckTx bool, appOpts TestAppOpts, enableEVMCustomPrecompiles bool, overrideWasmGasMultiplier bool, baseAppOptions ...func(*bam.BaseApp)) (res *App) {
encodingConfig := MakeEncodingConfig()
cdc := encodingConfig.Marshaler
options := []AppOption{
func(app *App) {
receiptStore, err := setupReceiptStore(app.keys[evmtypes.StoreKey])
if err != nil {
panic(fmt.Sprintf("error while creating receipt store: %s", err))
}
app.receiptStore = receiptStore
},
}
wasmOpts := EmptyWasmOpts
if overrideWasmGasMultiplier {
gasRegisterConfig := wasmkeeper.DefaultGasRegisterConfig()
gasRegisterConfig.GasMultiplier = 21_000_000
wasmOpts = []wasm.Option{
wasmkeeper.WithGasRegister(
wasmkeeper.NewWasmGasRegister(
gasRegisterConfig,
),
),
}
}
res = New(
log.NewNopLogger(),
dbm.NewMemDB(),
nil,
true,
map[int64]bool{},
DefaultNodeHome,
1,
enableEVMCustomPrecompiles,
config.TestConfig(),
encodingConfig,
wasm.EnableAllProposals,
appOpts,
wasmOpts,
options,
baseAppOptions...,
)
if !isCheckTx {
genesisState := NewDefaultGenesisState(cdc)
stateBytes, err := json.MarshalIndent(genesisState, "", " ")
if err != nil {
panic(err)
}
// TODO: remove once init chain works with SC
defer func() { _ = recover() }()
_, err = res.InitChain(
context.Background(), &abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
ConsensusParams: DefaultConsensusParams,
AppStateBytes: stateBytes,
},
)
if err != nil {
panic(err)
}
}
return res
}
func Setup(tb testing.TB, isCheckTx bool, enableEVMCustomPrecompiles bool, overrideWasmGasMultiplier bool, baseAppOptions ...func(*bam.BaseApp)) (res *App) {
db := dbm.NewMemDB()
return SetupWithDB(tb, db, isCheckTx, enableEVMCustomPrecompiles, overrideWasmGasMultiplier, baseAppOptions...)
}
func SetupWithDB(tb testing.TB, db dbm.DB, isCheckTx bool, enableEVMCustomPrecompiles bool, overrideWasmGasMultiplier bool, baseAppOptions ...func(*bam.BaseApp)) (res *App) {
encodingConfig := MakeEncodingConfig()
cdc := encodingConfig.Marshaler
options := []AppOption{
func(app *App) {
receiptStore, err := setupReceiptStore(app.keys[evmtypes.StoreKey])
if err != nil {
panic(fmt.Sprintf("error while creating receipt store: %s", err))
}
app.receiptStore = receiptStore
},
}
wasmOpts := EmptyWasmOpts
if overrideWasmGasMultiplier {
gasRegisterConfig := wasmkeeper.DefaultGasRegisterConfig()
gasRegisterConfig.GasMultiplier = 21_000_000
wasmOpts = []wasm.Option{
wasmkeeper.WithGasRegister(
wasmkeeper.NewWasmGasRegister(
gasRegisterConfig,
),
),
}
}
res = New(
log.NewNopLogger(),
db,
nil,
true,
map[int64]bool{},
tb.TempDir(),
1,
enableEVMCustomPrecompiles,
config.TestConfig(),
encodingConfig,
wasm.EnableAllProposals,
TestAppOpts{},
wasmOpts,
options,
baseAppOptions...,
)
if !isCheckTx {
genesisState := NewDefaultGenesisState(cdc)
stateBytes, err := json.MarshalIndent(genesisState, "", " ")
if err != nil {
panic(err)
}
_, err = res.InitChain(
context.Background(), &abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
ConsensusParams: DefaultConsensusParams,
AppStateBytes: stateBytes,
},
)
if err != nil {
panic(err)
}
}
return res
}
func SetupWithSc(t *testing.T, isCheckTx bool, enableEVMCustomPrecompiles bool, testAppOpts TestAppOpts, baseAppOptions ...func(*bam.BaseApp)) (res *App) {
db := dbm.NewMemDB()
encodingConfig := MakeEncodingConfig()
cdc := encodingConfig.Marshaler
options := []AppOption{
func(app *App) {
receiptStore, err := setupReceiptStore(app.keys[evmtypes.StoreKey])
if err != nil {
panic(fmt.Sprintf("error while creating receipt store: %s", err))
}
app.receiptStore = receiptStore
},
}
res = New(
log.NewNopLogger(),
db,
nil,
true,
map[int64]bool{},
t.TempDir(),
1,
enableEVMCustomPrecompiles,
config.TestConfig(),
encodingConfig,
wasm.EnableAllProposals,
testAppOpts,
EmptyWasmOpts,
options,
baseAppOptions...,
)
if !isCheckTx {
genesisState := NewDefaultGenesisState(cdc)
stateBytes, err := json.MarshalIndent(genesisState, "", " ")
if err != nil {
panic(err)
}
// TODO: remove once init chain works with SC
defer func() { _ = recover() }()
_, err = res.InitChain(
context.Background(), &abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
ConsensusParams: DefaultConsensusParams,
AppStateBytes: stateBytes,
},
)
if err != nil {
panic(err)
}
}
return res
}
func SetupTestingAppWithLevelDb(t *testing.T, isCheckTx bool, enableEVMCustomPrecompiles bool) (*App, func()) {
dir := "sei_testing"
db, err := sdk.NewLevelDB("sei_leveldb_testing", dir)
if err != nil {
panic(err)
}
encodingConfig := MakeEncodingConfig()
cdc := encodingConfig.Marshaler
app := New(
log.NewNopLogger(),
db,
nil,
true,
map[int64]bool{},
t.TempDir(),
5,
enableEVMCustomPrecompiles,
nil,
encodingConfig,
wasm.EnableAllProposals,
TestAppOpts{},
EmptyWasmOpts,
nil,
)
if !isCheckTx {
genesisState := NewDefaultGenesisState(cdc)
stateBytes, err := json.MarshalIndent(genesisState, "", " ")
if err != nil {
panic(err)
}
_, err = app.InitChain(
context.Background(), &abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
ConsensusParams: DefaultConsensusParams,
AppStateBytes: stateBytes,
},
)
if err != nil {
panic(err)
}
}
cleanupFn := func() {
_ = db.Close()
err = os.RemoveAll(dir)
if err != nil {
panic(err)
}
}
return app, cleanupFn
}
// DefaultConsensusParams defines the default Tendermint consensus params used in
// SimApp testing.
var DefaultConsensusParams = &tmproto.ConsensusParams{
Block: &tmproto.BlockParams{
MaxBytes: 200000,
MaxGas: 100000000,
},
Evidence: &tmproto.EvidenceParams{
MaxAgeNumBlocks: 302400,
MaxAgeDuration: 504 * time.Hour, // 3 weeks is the max duration
MaxBytes: 10000,
},
Validator: &tmproto.ValidatorParams{
PubKeyTypes: []string{
tmtypes.ABCIPubKeyTypeEd25519,
},
},
}
func setup(t *testing.T, withGenesis bool, invCheckPeriod uint) (*App, GenesisState) {
db := dbm.NewMemDB()
encCdc := MakeEncodingConfig()
app := New(
log.NewNopLogger(),
db,
nil,
true,
map[int64]bool{},
t.TempDir(),
1,
false,
config.TestConfig(),
encCdc,
wasm.EnableAllProposals,
TestAppOpts{},
EmptyWasmOpts,
[]AppOption{},
)
if withGenesis {
return app, NewDefaultGenesisState(encCdc.Marshaler)
}
return app, GenesisState{}
}
// SetupWithGenesisValSet initializes a new SimApp with a validator set and genesis accounts
// that also act as delegators. For simplicity, each validator is bonded with a delegation
// of one consensus engine unit (10^6) in the default token of the simapp from first genesis
// account. A Nop logger is set in SimApp.
func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs []authtypes.GenesisAccount, balances ...banktypes.Balance) *App {
app, genesisState := setup(t, true, 5)
// set genesis accounts
authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs)
genesisState[authtypes.ModuleName] = app.AppCodec().MustMarshalJSON(authGenesis)
validators := make([]stakingtypes.Validator, 0, len(valSet.Validators))
delegations := make([]stakingtypes.Delegation, 0, len(valSet.Validators))
bondAmt := sdk.NewInt(1000000)
for _, val := range valSet.Validators {
pk, err := cryptocodec.FromTmPubKeyInterface(val.PubKey)
require.NoError(t, err)
pkAny, err := codectypes.NewAnyWithValue(pk)
require.NoError(t, err)
validator := stakingtypes.Validator{
OperatorAddress: sdk.ValAddress(val.Address).String(),
ConsensusPubkey: pkAny,
Jailed: false,
Status: stakingtypes.Bonded,
Tokens: bondAmt,
DelegatorShares: sdk.OneDec(),
Description: stakingtypes.Description{},
UnbondingHeight: int64(0),
UnbondingTime: time.Unix(0, 0).UTC(),
Commission: stakingtypes.NewCommission(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()),
MinSelfDelegation: sdk.ZeroInt(),
}
validators = append(validators, validator)
delegations = append(delegations, stakingtypes.NewDelegation(genAccs[0].GetAddress(), val.Address.Bytes(), sdk.OneDec()))
}
// set validators and delegations
stakingGenesis := stakingtypes.NewGenesisState(stakingtypes.DefaultParams(), validators, delegations)
genesisState[stakingtypes.ModuleName] = app.AppCodec().MustMarshalJSON(stakingGenesis)
totalSupply := sdk.NewCoins()
for _, b := range balances {
// add genesis acc tokens and delegated tokens to total supply
totalSupply = totalSupply.Add(b.Coins.Add(sdk.NewCoin(sdk.DefaultBondDenom, bondAmt))...)
}
// add bonded amount to bonded pool module account
balances = append(balances, banktypes.Balance{
Address: authtypes.NewModuleAddress(stakingtypes.BondedPoolName).String(),
Coins: sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, bondAmt)},
})
// update total supply
bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, totalSupply, []banktypes.Metadata{}, []banktypes.WeiBalance{})
genesisState[banktypes.ModuleName] = app.AppCodec().MustMarshalJSON(bankGenesis)
stateBytes, err := json.MarshalIndent(genesisState, "", " ")
require.NoError(t, err)
// init chain will set the validator set and initialize the genesis accounts
_, _ = app.InitChain(
context.Background(), &abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
ConsensusParams: DefaultConsensusParams,
AppStateBytes: stateBytes,
},
)
// commit genesis changes
_, _ = app.Commit(context.Background())
_, _ = app.FinalizeBlock(context.Background(), &abci.RequestFinalizeBlock{
Height: app.LastBlockHeight() + 1,
Hash: app.LastCommitID().Hash,
NextValidatorsHash: valSet.Hash(),
})
return app
}
// SetupWithGenesisAccounts initializes a new SimApp with the provided genesis
// accounts and possible balances.
func SetupWithGenesisAccounts(t *testing.T, genAccs []authtypes.GenesisAccount, balances ...banktypes.Balance) *App {
app, genesisState := setup(t, true, 0)
authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs)
genesisState[authtypes.ModuleName] = app.AppCodec().MustMarshalJSON(authGenesis)
totalSupply := sdk.NewCoins()
for _, b := range balances {
totalSupply = totalSupply.Add(b.Coins...)
}
bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, totalSupply, []banktypes.Metadata{}, []banktypes.WeiBalance{})
genesisState[banktypes.ModuleName] = app.AppCodec().MustMarshalJSON(bankGenesis)
stateBytes, err := json.MarshalIndent(genesisState, "", " ")
if err != nil {
panic(err)
}
_, _ = app.InitChain(
context.Background(), &abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
ConsensusParams: DefaultConsensusParams,
AppStateBytes: stateBytes,
},
)
_, _ = app.Commit(context.Background())
_, _ = app.FinalizeBlock(context.Background(), &abci.RequestFinalizeBlock{Height: app.LastBlockHeight() + 1})
return app
}
type GenerateAccountStrategy func(int) []sdk.AccAddress
// createRandomAccounts is a strategy used by addTestAddrs() in order to generated addresses in random order.
func createRandomAccounts(accNum int) []sdk.AccAddress {
testAddrs := make([]sdk.AccAddress, accNum)
for i := 0; i < accNum; i++ {
pk := ed25519.GenPrivKey().PubKey()
testAddrs[i] = sdk.AccAddress(pk.Address())
}
return testAddrs
}
// createIncrementalAccounts is a strategy used by addTestAddrs() in order to generated addresses in ascending order.
func createIncrementalAccounts(accNum int) []sdk.AccAddress {
var addresses []sdk.AccAddress
var buffer bytes.Buffer
// start at 100 so we can make up to 999 test addresses with valid test addresses
for i := 100; i < (accNum + 100); i++ {
numString := strconv.Itoa(i)
buffer.WriteString("A58856F0FD53BF058B4909A21AEC019107BA6") // base address string
buffer.WriteString(numString) // adding on final two digits to make addresses unique
res, _ := sdk.AccAddressFromHex(buffer.String())
bech := res.String()
addr, _ := TestAddr(buffer.String(), bech)
addresses = append(addresses, addr)
buffer.Reset()
}
return addresses
}
// AddTestAddrsFromPubKeys adds the addresses into the SimApp providing only the public keys.
func AddTestAddrsFromPubKeys(app *App, ctx sdk.Context, pubKeys []cryptotypes.PubKey, accAmt sdk.Int) {
initCoins := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), accAmt))
for _, pk := range pubKeys {
initAccountWithCoins(app, ctx, sdk.AccAddress(pk.Address()), initCoins)
}
}
// AddTestAddrs constructs and returns accNum amount of accounts with an
// initial balance of accAmt in random order
func AddTestAddrs(app *App, ctx sdk.Context, accNum int, accAmt sdk.Int) []sdk.AccAddress {
return addTestAddrs(app, ctx, accNum, accAmt, createRandomAccounts)
}
// AddTestAddrs constructs and returns accNum amount of accounts with an
// initial balance of accAmt in random order
func AddTestAddrsIncremental(app *App, ctx sdk.Context, accNum int, accAmt sdk.Int) []sdk.AccAddress {
return addTestAddrs(app, ctx, accNum, accAmt, createIncrementalAccounts)
}
func addTestAddrs(app *App, ctx sdk.Context, accNum int, accAmt sdk.Int, strategy GenerateAccountStrategy) []sdk.AccAddress {
testAddrs := strategy(accNum)
initCoins := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), accAmt))
for _, addr := range testAddrs {
initAccountWithCoins(app, ctx, addr, initCoins)
}
return testAddrs
}
func initAccountWithCoins(app *App, ctx sdk.Context, addr sdk.AccAddress, coins sdk.Coins) {
err := app.BankKeeper.MintCoins(ctx, minttypes.ModuleName, coins)
if err != nil {
panic(err)
}
err = app.BankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, addr, coins)
if err != nil {
panic(err)
}
}
// ConvertAddrsToValAddrs converts the provided addresses to ValAddress.
func ConvertAddrsToValAddrs(addrs []sdk.AccAddress) []sdk.ValAddress {
valAddrs := make([]sdk.ValAddress, len(addrs))
for i, addr := range addrs {
valAddrs[i] = sdk.ValAddress(addr)
}
return valAddrs
}
func TestAddr(addr string, bech string) (sdk.AccAddress, error) {
res, err := sdk.AccAddressFromHex(addr)
if err != nil {
return nil, err
}
bechexpected := res.String()
if bech != bechexpected {
return nil, fmt.Errorf("bech encoding doesn't match reference")
}
bechres, err := sdk.AccAddressFromBech32(bech)
if err != nil {
return nil, err
}
if !bytes.Equal(bechres, res) {
return nil, err
}
return res, nil
}
func GenTx(gen client.TxConfig, msgs []sdk.Msg, feeAmt sdk.Coins, gas uint64, chainID string, accNums, accSeqs []uint64, priv ...cryptotypes.PrivKey) (sdk.Tx, error) {
sigs := make([]signing.SignatureV2, len(priv))
// create a random length memo
r := rand.New(rand.NewSource(time.Now().UnixNano()))
memo := simulation.RandStringOfLength(r, simulation.RandIntBetween(r, 0, 100))
signMode := gen.SignModeHandler().DefaultMode()
// 1st round: set SignatureV2 with empty signatures, to set correct
// signer infos.
for i, p := range priv {
sigs[i] = signing.SignatureV2{
PubKey: p.PubKey(),
Data: &signing.SingleSignatureData{
SignMode: signMode,
},
Sequence: accSeqs[i],
}
}
tx := gen.NewTxBuilder()
err := tx.SetMsgs(msgs...)
if err != nil {
return nil, err
}
err = tx.SetSignatures(sigs...)
if err != nil {
return nil, err
}
tx.SetMemo(memo)
tx.SetFeeAmount(feeAmt)
tx.SetGasLimit(gas)
// 2nd round: once all signer infos are set, every signer can sign.
for i, p := range priv {
signerData := authsign.SignerData{
ChainID: chainID,
AccountNumber: accNums[i],
Sequence: accSeqs[i],
}
signBytes, err := gen.SignModeHandler().GetSignBytes(signMode, signerData, tx.GetTx())
if err != nil {
panic(err)
}
sig, err := p.Sign(signBytes)
if err != nil {
panic(err)
}
sigs[i].Data.(*signing.SingleSignatureData).Signature = sig
err = tx.SetSignatures(sigs...)
if err != nil {
panic(err)
}
}
return tx.GetTx(), nil
}
func SignCheckDeliver(
t *testing.T, txCfg client.TxConfig, app *bam.BaseApp, header sdk.Header, msgs []sdk.Msg,
chainID string, accNums, accSeqs []uint64, expSimPass, expPass bool, priv ...cryptotypes.PrivKey,
) (sdk.GasInfo, *sdk.Result, error) {
tx, err := GenTx(
txCfg,
msgs,
sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)},
DefaultGenTxGas,
chainID,
accNums,
accSeqs,
priv...,
)
require.NoError(t, err)
txBytes, err := txCfg.TxEncoder()(tx)
require.Nil(t, err)
// Must simulate now as CheckTx doesn't run Msgs anymore
_, res, err := app.Simulate(txBytes)
if expSimPass {
require.NoError(t, err)
require.NotNil(t, res)
} else {
require.Error(t, err)
require.Nil(t, res)
}
// Simulate a sending a transaction and committing a block
_, _ = app.FinalizeBlock(context.Background(), &abci.RequestFinalizeBlock{Height: header.Height})
gInfo, res, err := app.Deliver(txCfg.TxEncoder(), tx)
if expPass {
require.NoError(t, err)
require.NotNil(t, res)
} else {
require.Error(t, err)
require.Nil(t, res)
}
_, _ = app.FinalizeBlock(context.Background(), &abci.RequestFinalizeBlock{Height: header.Height})
_, _ = app.Commit(context.Background())
return gInfo, res, err
}
func GenSequenceOfTxs(txGen client.TxConfig, msgs []sdk.Msg, accNums []uint64, initSeqNums []uint64, numToGenerate int, priv ...cryptotypes.PrivKey) ([]sdk.Tx, error) {
txs := make([]sdk.Tx, numToGenerate)
var err error
for i := 0; i < numToGenerate; i++ {
txs[i], err = GenTx(
txGen,
msgs,
sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)},
DefaultGenTxGas,
"",
accNums,
initSeqNums,
priv...,
)
if err != nil {
break
}
incrementAllSequenceNumbers(initSeqNums)
}
return txs, err
}
func incrementAllSequenceNumbers(initSeqNums []uint64) {
for i := 0; i < len(initSeqNums); i++ {
initSeqNums[i]++
}
}
// CheckBalance checks the balance of an account.
func CheckBalance(t *testing.T, app *App, addr sdk.AccAddress, balances sdk.Coins) {
ctxCheck := app.NewContext(true, sdk.Header{})
require.True(t, balances.IsEqual(app.BankKeeper.GetAllBalances(ctxCheck, addr)))
}
// CreateTestPubKeys returns a total of numPubKeys public keys in ascending order.
func CreateTestPubKeys(numPubKeys int) []cryptotypes.PubKey {