-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
1269 lines (1158 loc) · 46.2 KB
/
app.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
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 (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"math"
"math/rand"
"os"
"path/filepath"
"sync"
"testing"
"time"
tmcfg "github.com/cometbft/cometbft/config"
tmcli "github.com/cometbft/cometbft/libs/cli"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/server"
svrcmd "github.com/cosmos/cosmos-sdk/server/cmd"
srvtypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/furyanprotocol/v4-chain/protocol/cmd/furyaprotocold/cmd"
"github.com/furyanprotocol/v4-chain/protocol/indexer"
dbm "github.com/cometbft/cometbft-db"
abcitypes "github.com/cometbft/cometbft/abci/types"
tmjson "github.com/cometbft/cometbft/libs/json"
"github.com/cometbft/cometbft/libs/log"
"github.com/cometbft/cometbft/mempool"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/cometbft/cometbft/types"
"github.com/cosmos/cosmos-sdk/baseapp"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govtypesv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
sdkproto "github.com/cosmos/gogoproto/proto"
"github.com/furyanprotocol/v4-chain/protocol/app"
"github.com/furyanprotocol/v4-chain/protocol/testutil/appoptions"
"github.com/furyanprotocol/v4-chain/protocol/testutil/constants"
testlog "github.com/furyanprotocol/v4-chain/protocol/testutil/logger"
testtx "github.com/furyanprotocol/v4-chain/protocol/testutil/tx"
assettypes "github.com/furyanprotocol/v4-chain/protocol/x/assets/types"
blocktimetypes "github.com/furyanprotocol/v4-chain/protocol/x/blocktime/types"
bridgetypes "github.com/furyanprotocol/v4-chain/protocol/x/bridge/types"
clobtypes "github.com/furyanprotocol/v4-chain/protocol/x/clob/types"
delaymsgtypes "github.com/furyanprotocol/v4-chain/protocol/x/delaymsg/types"
epochstypes "github.com/furyanprotocol/v4-chain/protocol/x/epochs/types"
feetiertypes "github.com/furyanprotocol/v4-chain/protocol/x/feetiers/types"
perptypes "github.com/furyanprotocol/v4-chain/protocol/x/perpetuals/types"
pricestypes "github.com/furyanprotocol/v4-chain/protocol/x/prices/types"
rewardstypes "github.com/furyanprotocol/v4-chain/protocol/x/rewards/types"
sendingtypes "github.com/furyanprotocol/v4-chain/protocol/x/sending/types"
stattypes "github.com/furyanprotocol/v4-chain/protocol/x/stats/types"
satypes "github.com/furyanprotocol/v4-chain/protocol/x/subaccounts/types"
vesttypes "github.com/furyanprotocol/v4-chain/protocol/x/vest/types"
"github.com/stretchr/testify/require"
"golang.org/x/exp/slices"
)
// localfuryaprotocol Alice config/priv_validator_key.json.
const alicePrivValidatorKeyJson = `{
"address": "124B880684400B4C0086BD4EE882DCC5B61CF7E3",
"pub_key": {
"type": "tendermint/PubKeyEd25519",
"value": "YiARx8259Z+fGFUxQLrz/5FU2RYRT6f5yzvt7D7CrQM="
},
"priv_key": {
"type": "tendermint/PrivKeyEd25519",
"value": "65frslxv5ig0KSNKlJOHT2FKTkOzkb/66eDPsiBaNUtiIBHHzbn1n58YVTFAuvP/kVTZFhFPp/nLO+3sPsKtAw=="
}
}
`
// localfuryaprotocol Alice config/node_key.json.
const aliceNodeKeyJson = `{
"priv_key": {
"type": "tendermint/PrivKeyEd25519",
"value": "8EGQBxfGMcRfH0C45UTedEG5Xi3XAcukuInLUqFPpskjp1Ny0c5XvwlKevAwtVvkwoeYYQSe0geQG/cF3GAcUA=="
}
}
`
// MustMakeCheckTxOptions is a struct containing options for MustMakeCheckTx.* functions.
type MustMakeCheckTxOptions struct {
// AccAddressForSigning is the account that's used to sign the transaction.
AccAddressForSigning string
// AccSequenceNumberForSigning is the account sequence number that's used to sign the transaction.
AccSequenceNumberForSigning uint64
// Amount of Gas for the transaction.
Gas uint64
// Gas fees offered for the transaction.
FeeAmt sdk.Coins
}
// ValidateResponsePrepareProposal is a function that validates the response from the PrepareProposalHandler.
type ValidateResponsePrepareProposalFn func(sdk.Context, abcitypes.ResponsePrepareProposal) (haltChain bool)
// ValidateResponseProcessProposal is a function that validates the response from the ProcessProposalHandler.
type ValidateResponseProcessProposalFn func(sdk.Context, abcitypes.ResponseProcessProposal) (haltChain bool)
// ValidateDeliverTxsFn is a function that validates the response from each transaction that is delivered.
// txIndex specifies the index of the transaction in the block.
type ValidateDeliverTxsFn func(
ctx sdk.Context,
request abcitypes.RequestDeliverTx,
response abcitypes.ResponseDeliverTx,
txIndex int,
) (haltchain bool)
// AdvanceToBlockOptions is a struct containing options for AdvanceToBlock.* functions.
type AdvanceToBlockOptions struct {
// The time associated with the block. If left at the default value then block time will be left unchanged.
BlockTime time.Time
// Whether to increment the block time using linear interpolation among the blocks.
// TODO(DEC-2156): Instead of an option, pass in a `BlockTimeFunc` to map each block to a
// time giving user greater flexibility.
LinearBlockTimeInterpolation bool
// RequestPrepareProposalTxsOverride allows overriding the txs that gets passed into the
// PrepareProposalHandler. This is useful for testing scenarios where unintended msg txs
// end up in the mempool (i.e. CheckTx failed to filter bad msg txs out).
RequestPrepareProposalTxsOverride [][]byte
// RequestProcessProposalTxsOverride allows overriding the txs that gets passed into the
// ProcessProposalHandler. This is useful for testing scenarios where bad validators end
// up proposing an invalid block proposal.
RequestProcessProposalTxsOverride [][]byte
// DeliverTxsOverride allows overriding the TestApp from being the block proposer and
// allows simulating transactions that were agreed to upon consensus to be delivered.
// This skips the PrepareProposal and ProcessProposal phase.
DeliverTxsOverride [][]byte
ValidateRespPrepare ValidateResponsePrepareProposalFn
ValidateRespProcess ValidateResponseProcessProposalFn
ValidateDeliverTxs ValidateDeliverTxsFn
}
// DefaultTestApp creates an instance of app.App with default settings, suitable for unit testing. The app will be
// initialized with any specified flags as overrides, and with any specified base app options.
func DefaultTestApp(customFlags map[string]interface{}, baseAppOptions ...func(*baseapp.BaseApp)) *app.App {
appOptions := appoptions.GetDefaultTestAppOptionsFromTempDirectory("", customFlags)
logger, ok := appOptions.Get(testlog.LoggerInstanceForTest).(log.Logger)
if !ok {
logger, _ = testlog.TestLogger()
}
db := dbm.NewMemDB()
snapshotsDB := dbm.NewMemDB()
furyaApp := app.New(
logger,
db,
snapshotsDB,
nil,
true,
appOptions,
baseAppOptions...,
)
return furyaApp
}
// DefaultGenesis returns a genesis doc using configuration from the local net with a genesis time
// equivalent to unix epoch + 1 nanosecond. We specifically use non-zero because stateful orders
// validate that block time is non-zero (https://github.com/furyanprotocol/v4-chain/protocol/blob/
// 84a046554ab1b4725475500d94a0b3179fdd18c2/x/clob/keeper/stateful_order_state.go#L237).
func DefaultGenesis() (genesis types.GenesisDoc) {
// NOTE: Tendermint uses a custom JSON decoder for GenesisDoc
err := tmjson.Unmarshal([]byte(constants.GenesisState), &genesis)
if err != nil {
panic(err)
}
genesis.GenesisTime = time.Unix(0, 1)
return genesis
}
// GenesisStates is a type constraint for all well known genesis state types.
type GenesisStates interface {
authtypes.GenesisState |
banktypes.GenesisState |
perptypes.GenesisState |
feetiertypes.GenesisState |
stattypes.GenesisState |
vesttypes.GenesisState |
rewardstypes.GenesisState |
blocktimetypes.GenesisState |
clobtypes.GenesisState |
pricestypes.GenesisState |
satypes.GenesisState |
assettypes.GenesisState |
epochstypes.GenesisState |
sendingtypes.GenesisState |
delaymsgtypes.GenesisState |
bridgetypes.GenesisState |
govtypesv1.GenesisState
}
// UpdateGenesisDocWithAppStateForModule updates the supplied genesis doc using the provided function. The function
// type (any one of the well known GenesisStates) is used to derive which module will be updated.
// Will panic if there is an error in marshalling or unmarshalling the app state.
func UpdateGenesisDocWithAppStateForModule[T GenesisStates](genesisDoc *types.GenesisDoc, fn func(genesisState *T)) {
var appState map[string]json.RawMessage
err := json.Unmarshal(genesisDoc.AppState, &appState)
if err != nil {
panic(err)
}
var moduleName string
var t T
switch any(t).(type) {
case authtypes.GenesisState:
moduleName = authtypes.ModuleName
case banktypes.GenesisState:
moduleName = banktypes.ModuleName
case blocktimetypes.GenesisState:
moduleName = blocktimetypes.ModuleName
case bridgetypes.GenesisState:
moduleName = bridgetypes.ModuleName
case delaymsgtypes.GenesisState:
moduleName = delaymsgtypes.ModuleName
case perptypes.GenesisState:
moduleName = perptypes.ModuleName
case clobtypes.GenesisState:
moduleName = clobtypes.ModuleName
case feetiertypes.GenesisState:
moduleName = feetiertypes.ModuleName
case pricestypes.GenesisState:
moduleName = pricestypes.ModuleName
case rewardstypes.GenesisState:
moduleName = rewardstypes.ModuleName
case vesttypes.GenesisState:
moduleName = vesttypes.ModuleName
case stattypes.GenesisState:
moduleName = stattypes.ModuleName
case satypes.GenesisState:
moduleName = satypes.ModuleName
case assettypes.GenesisState:
moduleName = assettypes.ModuleName
case epochstypes.GenesisState:
moduleName = epochstypes.ModuleName
case sendingtypes.GenesisState:
moduleName = sendingtypes.ModuleName
case govtypesv1.GenesisState:
moduleName = govtypes.ModuleName
default:
panic(fmt.Errorf("Unsupported type %T", t))
}
if protoMsg, ok := any(&t).(sdkproto.Message); ok {
constants.TestEncodingCfg.Codec.MustUnmarshalJSON(appState[moduleName], protoMsg)
fn(&t)
appState[moduleName] = constants.TestEncodingCfg.Codec.MustMarshalJSON(protoMsg)
} else {
panic(fmt.Errorf("Unsupported type %T", t))
}
bz, err := json.Marshal(appState)
if err != nil {
panic(err)
}
genesisDoc.AppState = bz
}
// Used to instantiate new instances of the App.
type AppCreatorFn func() *app.App
// Used to instantiate new instances of the genesis doc.
type GenesisDocCreatorFn func() (genesis types.GenesisDoc)
// ExecuteCheckTxs is invoked once per block. Returning true will halt execution.
// The provided context will be a new CheckTx context using the last committed block height.
type ExecuteCheckTxs func(ctx sdk.Context, app *app.App) (stop bool)
// NewTestAppBuilder returns a new builder for TestApp.
//
// The default instance will return a builder with:
// - DefaultGenesis
// - no custom flags
// - an ExecuteCheckTxs function that will stop on after the first block
// - non-determinism checks enabled
//
// Note that the TestApp instance will have 3 non-determinism state checking apps:
// - `parallelApp` is responsible for seeing all CheckTx requests, block proposals, blocks, and RecheckTx requests.
// This allows it to detect state differences due to inconsistent in-memory structures (for example iteration order
// in maps).
// - `noCheckTxApp` is responsible for seeing all block proposals and blocks. This allows it to simulate a validator
// that never received any of the CheckTx requests and that it will still accept blocks and arrive at the same
// state hash.
// - `crashingApp` is responsible for restarting before processing a block and sees all CheckTx requests, block
// proposals, and blocks. This allows it to check that in memory state can be restored successfully on application
// and that it will accept a block after a crash and arrive at the same state hash.
//
// Tests that rely on mutating internal application state directly (for example via keepers) will want to disable
// non-determinism checks via `WithNonDeterminismChecksEnabled(false)` otherwise the test will likely hit a
// non-determinism check that fails causing the test to fail. If possible, update the test instead to use genesis state
// to initialize state or `CheckTx` transactions to initialize the appropriate keeper state.
//
// Tests that rely on in-memory state to survive across block boundaries will want to disable crashing App CheckTx
// non-determinism checks via `WithCrashingAppCheckTxNonDeterminismChecksEnabled(false)` otherwise the test will likely
// hit a non-determinism check that fails causing the test to fail. For example unmatched short term
// orders in the memclob and order rate limits are only stored in memory and lost on application restart, and it would
// thus make sense to disable the crashing App CheckTx non-determinism check for tests that rely on this information
// surviving across block boundaries.
func NewTestAppBuilder(t testing.TB) TestAppBuilder {
if t == nil {
panic("t must not be nil")
}
return TestAppBuilder{
genesisDocFn: DefaultGenesis,
disableHealthMonitorForTesting: true,
appOptions: make(map[string]interface{}),
enableNonDeterminismChecks: true,
enableCrashingAppCheckTxNonDeterminismChecks: true,
executeCheckTxs: func(ctx sdk.Context, app *app.App) (stop bool) {
return true
},
t: t,
}
}
// A builder containing fields necessary for the TestApp.
//
// Note that we specifically use value receivers for the With... methods because we want to make the builder instances
// immutable.
type TestAppBuilder struct {
genesisDocFn GenesisDocCreatorFn
disableHealthMonitorForTesting bool
appOptions map[string]interface{}
executeCheckTxs ExecuteCheckTxs
enableNonDeterminismChecks bool
enableCrashingAppCheckTxNonDeterminismChecks bool
t testing.TB
}
// WithGenesisDocFn returns a builder like this one with specified function that will be used to create
// the genesis doc.
func (tApp TestAppBuilder) WithGenesisDocFn(fn GenesisDocCreatorFn) TestAppBuilder {
tApp.genesisDocFn = fn
return tApp
}
// WithHealthMonitorDisabledForTesting controls whether the daemon server health monitor is disabled for testing.
func (tApp TestAppBuilder) WithHealthMonitorDisabledForTesting(disableHealthMonitorForTesting bool) TestAppBuilder {
tApp.disableHealthMonitorForTesting = disableHealthMonitorForTesting
return tApp
}
// WithNonDeterminismChecksEnabled controls whether non-determinism checks via distinct application instances
// state hash and CheckTx/ReCheckTx response comparisons.
//
// Tests that rely on mutating internal application state directly (for example via keepers) will want to disable
// non-determinism checks via `WithNonDeterminismChecksEnabled(false)` otherwise the test will likely hit a
// non-determinism check that fails causing the test to fail. If possible, update the test instead to use genesis state
// to initialize state or `CheckTx` transactions to initialize the appropriate keeper state.
func (tApp TestAppBuilder) WithNonDeterminismChecksEnabled(enableNonDeterminismChecks bool) TestAppBuilder {
tApp.enableNonDeterminismChecks = enableNonDeterminismChecks
return tApp
}
// WithCrashingAppCheckTxNonDeterminismChecksEnabled controls whether the crashing App instance will ensure that
// the `CheckTx` result matches that of the main `App`.
//
// Tests that rely on in-memory state to survive across block boundaries will want to disable crashing App CheckTx
// non-determinism checks via `WithCrashingAppCheckTxNonDeterminismChecksEnabled(false)` otherwise the test will likely
// hit a non-determinism check that fails causing the test to fail. For example unmatched short term
// orders in the memclob and order rate limits are only stored in memory and lost on application restart, and it would
// thus make sense to disable the crashing App CheckTx non-determinism check for tests that rely on this information
// surviving across block boundaries.
func (tApp TestAppBuilder) WithCrashingAppCheckTxNonDeterminismChecksEnabled(
enableCrashingAppCheckTxNonDeterminismChecks bool) TestAppBuilder {
tApp.enableCrashingAppCheckTxNonDeterminismChecks = enableCrashingAppCheckTxNonDeterminismChecks
return tApp
}
// WithAppOptions returns a builder like this one with the specified app options.
func (tApp TestAppBuilder) WithAppOptions(
appOptions map[string]interface{},
) TestAppBuilder {
tApp.appOptions = appOptions
return tApp
}
// Build returns a new TestApp capable of being executed.
func (tApp TestAppBuilder) Build() *TestApp {
rval := TestApp{
builder: tApp,
}
return &rval
}
// A TestApp used to executed ABCI++ flows. Note that callers should invoke `TestApp.CheckTx` over `TestApp.App.CheckTx`
// to ensure that the transaction is added to a "mempool" that will be considered during the Prepare/Process proposal
// phase.
//
// Note that the TestApp instance has 3 non-determinism state checking apps:
// - `parallelApp` is responsible for seeing all CheckTx requests, block proposals, blocks, and RecheckTx requests.
// This allows it to detect state differences due to inconsistent in-memory structures (for example iteration order
// in maps).
// - `noCheckTxApp` is responsible for seeing all block proposals and blocks. This allows it to simulate a validator
// that never received any of the CheckTx requests and that it will still accept blocks and arrive at the same
// state hash.
// - `crashingApp` is responsible for restarting before processing a block and sees all CheckTx requests, block
// proposals, and blocks. This allows it to check that in memory state can be restored successfully on application
// and that it will accept a block after a crash and arrive at the same state hash.
//
// Note that TestApp.CheckTx is thread safe. All other methods are not thread safe.
type TestApp struct {
// Should only be used to fetch read only state, all mutations should preferably happen through Genesis state,
// TestApp.CheckTx, and block proposals.
// TODO(CLOB-545): Hide App and copy the pointers to keepers to be prevent incorrect usage of App.CheckTx over
// TestApp.CheckTx.
App *app.App
parallelApp *app.App
noCheckTxApp *app.App
crashingApp *app.App
restartCrashingApp func()
builder TestAppBuilder
genesis types.GenesisDoc
header tmproto.Header
passingCheckTxs [][]byte
passingCheckTxsMtx sync.Mutex
halted bool
}
func (tApp *TestApp) Builder() TestAppBuilder {
return tApp.builder
}
// InitChain initializes the chain. Will panic if initialized more than once.
func (tApp *TestApp) InitChain() sdk.Context {
if tApp.App != nil {
panic(errors.New("Cannot initialize chain that has been initialized already."))
}
tApp.initChainIfNeeded()
return tApp.App.NewContext(true, tApp.header)
}
func (tApp *TestApp) initChainIfNeeded() {
if tApp.App != nil {
return
}
// Get the initial genesis state and initialize the chain and commit the results of the initialization.
tApp.genesis = tApp.builder.genesisDocFn()
if tApp.genesis.GenesisTime.UnixNano() <= time.UnixMilli(0).UnixNano() {
tApp.builder.t.Fatal(fmt.Errorf(
"Unable to start chain at time %v, must be greater than unix epoch.",
tApp.genesis.GenesisTime,
))
return
}
// Prevent Cosmos SDK code from waiting for 5 seconds on each start-up.
// TODO(CORE-538): Remove this during the upgrade since 0.50 Cosmos SDK no longer relies on this.
// There is a benign race here where another instance of the app running at the same time might use the shared
// value which will lead to possibly using the wrong server start time.
originalServerStartTime := srvtypes.ServerStartTime.Load()
srvtypes.ServerStartTime.Store(int64(time.Millisecond * 10))
tApp.builder.t.Cleanup(func() {
// Restore the original server time.
srvtypes.ServerStartTime.Store(originalServerStartTime)
})
// Launch the main instance of the application
// TODO(CORE-721): Consolidate launch of apps into an abstraction since the logic is mostly repeated 4 times.
{
validatorHomeDir, err := prepareValidatorHomeDir(tApp.genesis)
if err != nil {
tApp.builder.t.Fatal(err)
return
}
app, shutdownFn, err := launchValidatorInDir(validatorHomeDir, tApp.builder.appOptions)
if err != nil {
tApp.builder.t.Fatal(err)
return
}
tApp.App = app
tApp.builder.t.Cleanup(func() {
doneErr := shutdownFn()
// Clean-up the home directory.
if err := os.RemoveAll(validatorHomeDir); err != nil {
tApp.builder.t.Logf("Failed to clean-up temporary validator dir %s", validatorHomeDir)
}
if doneErr != nil {
tApp.builder.t.Fatal(doneErr)
}
})
}
if tApp.builder.disableHealthMonitorForTesting {
tApp.App.DisableHealthMonitorForTesting()
}
if tApp.builder.enableNonDeterminismChecks {
// Filter out appOptions that shouldn't be shared to the App instances used for non-determinism checks.
// TODO(CORE-720): Improve integration of in memory objects for e2e test framework that shouldn't be shared
// across application instances.
filteredAppOptions := make(map[string]interface{})
for key, value := range tApp.builder.appOptions {
if key != testlog.LoggerInstanceForTest && key != indexer.MsgSenderInstanceForTest {
filteredAppOptions[key] = value
}
}
// Launch the `parallelApp` instance.
{
validatorHomeDir, err := prepareValidatorHomeDir(tApp.genesis)
if err != nil {
tApp.builder.t.Fatal(err)
return
}
app, shutdownFn, err := launchValidatorInDir(validatorHomeDir, filteredAppOptions)
if err != nil {
tApp.builder.t.Fatal(err)
return
}
tApp.parallelApp = app
tApp.builder.t.Cleanup(func() {
doneErr := shutdownFn()
// Clean-up the home directory.
if err := os.RemoveAll(validatorHomeDir); err != nil {
tApp.builder.t.Logf("Failed to clean-up temporary validator dir %s", validatorHomeDir)
}
if doneErr != nil {
tApp.builder.t.Fatal(doneErr)
}
})
}
// Launch the `noCheckTx` instance.
{
validatorHomeDir, err := prepareValidatorHomeDir(tApp.genesis)
if err != nil {
tApp.builder.t.Fatal(err)
return
}
app, shutdownFn, err := launchValidatorInDir(validatorHomeDir, filteredAppOptions)
if err != nil {
tApp.builder.t.Fatal(err)
return
}
tApp.noCheckTxApp = app
tApp.builder.t.Cleanup(func() {
doneErr := shutdownFn()
// Clean-up the home directory.
if err := os.RemoveAll(validatorHomeDir); err != nil {
tApp.builder.t.Logf("Failed to clean-up temporary validator dir %s", validatorHomeDir)
}
if doneErr != nil {
tApp.builder.t.Fatal(doneErr)
}
})
}
// Launch the `crashingApp` instance.
{
validatorHomeDir, err := prepareValidatorHomeDir(tApp.genesis)
if err != nil {
tApp.builder.t.Fatal(err)
return
}
app, shutdownFn, err := launchValidatorInDir(validatorHomeDir, filteredAppOptions)
if err != nil {
tApp.builder.t.Fatal(err)
return
}
tApp.crashingApp = app
tApp.builder.t.Cleanup(func() {
doneErr := shutdownFn()
// Clean-up the home directory.
if err := os.RemoveAll(validatorHomeDir); err != nil {
tApp.builder.t.Logf("Failed to clean-up temporary validator dir %s", validatorHomeDir)
}
if doneErr != nil {
tApp.builder.t.Fatal(doneErr)
}
})
tApp.restartCrashingApp = func() {
// We shutdown the instance of the existing crashingApp.
doneOrRestartErr := shutdownFn()
tApp.crashingApp = nil
if err == nil {
app, shutdownFn, doneOrRestartErr = launchValidatorInDir(validatorHomeDir, filteredAppOptions)
}
// If we errored shutting down or relaunching then update the shutdownFn to return this error
// and fatal the test.
if err != nil {
shutdownFn = func() error {
return doneOrRestartErr
}
tApp.builder.t.Fatal(doneOrRestartErr)
return
}
// Update the crashingApp pointer to the new instance of the application.
tApp.crashingApp = app
}
}
}
consensusParamsProto := tApp.genesis.ConsensusParams.ToProto()
initChainRequest := abcitypes.RequestInitChain{
InitialHeight: tApp.genesis.InitialHeight,
AppStateBytes: tApp.genesis.AppState,
ChainId: tApp.genesis.ChainID,
ConsensusParams: &consensusParamsProto,
Time: tApp.genesis.GenesisTime,
}
tApp.App.InitChain(initChainRequest)
if tApp.builder.enableNonDeterminismChecks {
tApp.parallelApp.InitChain(initChainRequest)
tApp.noCheckTxApp.InitChain(initChainRequest)
tApp.crashingApp.InitChain(initChainRequest)
}
tApp.App.Commit()
if tApp.builder.enableNonDeterminismChecks {
tApp.parallelApp.Commit()
tApp.noCheckTxApp.Commit()
tApp.crashingApp.Commit()
}
tApp.header = tmproto.Header{
ChainID: tApp.genesis.ChainID,
ProposerAddress: constants.AliceAccAddress,
Height: tApp.App.LastBlockHeight(),
Time: tApp.genesis.GenesisTime,
LastCommitHash: tApp.App.LastCommitID().Hash,
NextValidatorsHash: tApp.App.LastCommitID().Hash,
}
}
// AdvanceToBlock advances the chain to the specified block and block time.
// If the specified block is the current block, simply returns the current context.
// For example, block = 10, t = 90 will advance to a block with height 10 and with a time of 90.
func (tApp *TestApp) AdvanceToBlock(
block uint32,
options AdvanceToBlockOptions,
) sdk.Context {
tApp.panicIfChainIsHalted()
tApp.initChainIfNeeded()
if options.BlockTime.IsZero() { // if time is not specified, use the current block time.
options.BlockTime = tApp.header.Time
}
if int64(block) <= tApp.header.Height {
panic(fmt.Errorf("Expected block height (%d) > current block height (%d).", block, tApp.header.Height))
}
if options.BlockTime.UnixNano() < tApp.header.Time.UnixNano() {
panic(fmt.Errorf("Expected time (%v) >= current block time (%v).", options.BlockTime, tApp.header.Time))
}
if int64(block) == tApp.GetBlockHeight() {
return tApp.App.NewContext(true, tApp.header)
}
// Ensure that we grab the lock so that we can read and write passingCheckTxs correctly.
tApp.passingCheckTxsMtx.Lock()
defer tApp.passingCheckTxsMtx.Unlock()
// Advance to the requested block using the requested block time.
for tApp.App.LastBlockHeight() < int64(block) {
tApp.panicIfChainIsHalted()
tApp.header.Height = tApp.App.LastBlockHeight() + 1
if tApp.header.Height == int64(block) {
// By default, only update block time at the requested block.
tApp.header.Time = options.BlockTime
} else if options.LinearBlockTimeInterpolation {
remainingDuration := options.BlockTime.Sub(tApp.header.Time)
nextBlockDuration := remainingDuration / time.Duration(int64(block)-tApp.App.LastBlockHeight())
tApp.header.Time = tApp.header.Time.Add(nextBlockDuration)
}
tApp.header.LastCommitHash = tApp.App.LastCommitID().Hash
tApp.header.NextValidatorsHash = tApp.App.LastCommitID().Hash
deliverTxs := options.DeliverTxsOverride
if deliverTxs == nil {
// Prepare the proposal and process it.
prepareRequest := abcitypes.RequestPrepareProposal{
Txs: tApp.passingCheckTxs,
MaxTxBytes: math.MaxInt64,
Height: tApp.header.Height,
Time: tApp.header.Time,
NextValidatorsHash: tApp.header.NextValidatorsHash,
ProposerAddress: tApp.header.ProposerAddress,
}
if options.RequestPrepareProposalTxsOverride != nil {
prepareRequest.Txs = options.RequestPrepareProposalTxsOverride
}
prepareResponse := tApp.App.PrepareProposal(prepareRequest)
if options.ValidateRespPrepare != nil {
haltChain := options.ValidateRespPrepare(
tApp.App.NewContext(true, tApp.header),
prepareResponse,
)
tApp.halted = haltChain
if tApp.halted {
return tApp.App.NewContext(true, tApp.header)
}
}
// Pass forward any transactions that were modified through the preparation phase and process them.
if options.RequestProcessProposalTxsOverride != nil {
prepareResponse.Txs = options.RequestProcessProposalTxsOverride
}
processRequest := abcitypes.RequestProcessProposal{
Txs: prepareResponse.Txs,
Hash: tApp.header.AppHash,
Height: tApp.header.Height,
Time: tApp.header.Time,
NextValidatorsHash: tApp.header.NextValidatorsHash,
ProposerAddress: tApp.header.ProposerAddress,
}
processResponse := tApp.App.ProcessProposal(processRequest)
if options.ValidateRespProcess != nil {
haltChain := options.ValidateRespProcess(
tApp.App.NewContext(true, tApp.header),
processResponse,
)
tApp.halted = haltChain
if tApp.halted {
return tApp.App.NewContext(true, tApp.header)
}
}
require.Truef(
tApp.builder.t,
processResponse.IsAccepted(),
"Expected process proposal request %+v to be accepted, but failed with %+v.",
processRequest,
processResponse,
)
// Check that all instances of the application can process the proposoal and come to the same result.
if tApp.builder.enableNonDeterminismChecks {
parallelProcessResponse := tApp.parallelApp.ProcessProposal(processRequest)
require.Truef(
tApp.builder.t,
parallelProcessResponse.IsAccepted(),
"Non-determinism detected, expected process proposal request %+v to be accepted, but failed with %+v.",
processRequest,
parallelProcessResponse,
)
noCheckTxProcessResponse := tApp.noCheckTxApp.ProcessProposal(processRequest)
require.Truef(
tApp.builder.t,
noCheckTxProcessResponse.IsAccepted(),
"Non-determinism detected, expected process proposal request %+v to be accepted, but failed with %+v.",
processRequest,
noCheckTxProcessResponse,
)
crashingProcessResponse := tApp.crashingApp.ProcessProposal(processRequest)
require.Truef(
tApp.builder.t,
crashingProcessResponse.IsAccepted(),
"Non-determinism detected, expected process proposal request %+v to be accepted, but failed with %+v.",
processRequest,
crashingProcessResponse,
)
}
deliverTxs = prepareResponse.Txs
}
txsNotInLastProposal := make([][]byte, 0)
for _, tx := range tApp.passingCheckTxs {
if !slices.ContainsFunc(deliverTxs, func(tx2 []byte) bool {
return bytes.Equal(tx, tx2)
}) {
txsNotInLastProposal = append(txsNotInLastProposal, tx)
}
}
tApp.passingCheckTxs = txsNotInLastProposal
// Restart the crashingApp instance before processing the block.
if tApp.builder.enableNonDeterminismChecks {
tApp.restartCrashingApp()
}
// Start the next block
beginBlockRequest := abcitypes.RequestBeginBlock{
Header: tApp.header,
}
tApp.App.BeginBlock(beginBlockRequest)
if tApp.builder.enableNonDeterminismChecks {
tApp.parallelApp.BeginBlock(beginBlockRequest)
tApp.noCheckTxApp.BeginBlock(beginBlockRequest)
tApp.crashingApp.BeginBlock(beginBlockRequest)
}
// Deliver the transaction from the previous block
for i, bz := range deliverTxs {
deliverTxRequest := abcitypes.RequestDeliverTx{Tx: bz}
deliverTxResponse := tApp.App.DeliverTx(deliverTxRequest)
// Use the supplied validator otherwise use the default validation which expects all delivered
// transactions to succeed.
if options.ValidateDeliverTxs != nil {
haltChain := options.ValidateDeliverTxs(
tApp.App.NewContext(false, tApp.header),
deliverTxRequest,
deliverTxResponse,
i,
)
tApp.halted = haltChain
if tApp.halted {
return tApp.App.NewContext(true, tApp.header)
}
} else {
require.Truef(
tApp.builder.t,
deliverTxResponse.IsOK(),
"Failed to deliver transaction that was accepted: %+v.",
deliverTxResponse,
)
}
// Ensure that all instances of the application have the blocks delivered.
if tApp.builder.enableNonDeterminismChecks {
tApp.parallelApp.DeliverTx(deliverTxRequest)
tApp.noCheckTxApp.DeliverTx(deliverTxRequest)
tApp.crashingApp.DeliverTx(deliverTxRequest)
}
}
// End the block and commit it.
endBlockRequest := abcitypes.RequestEndBlock{Height: tApp.header.Height}
tApp.App.EndBlock(endBlockRequest)
tApp.App.Commit()
if tApp.builder.enableNonDeterminismChecks {
tApp.parallelApp.EndBlock(endBlockRequest)
tApp.noCheckTxApp.EndBlock(endBlockRequest)
tApp.crashingApp.EndBlock(endBlockRequest)
tApp.parallelApp.Commit()
tApp.noCheckTxApp.Commit()
tApp.crashingApp.Commit()
// Ensure that all instances after committing the block came to the same commit hash.
require.Equalf(tApp.builder.t,
tApp.App.LastCommitID(),
tApp.parallelApp.LastCommitID(),
"Non-determinism in state detected, expected LastCommitID to match.",
)
require.Equalf(tApp.builder.t,
tApp.App.LastCommitID(),
tApp.noCheckTxApp.LastCommitID(),
"Non-determinism in state detected, expected LastCommitID to match.",
)
require.Equalf(tApp.builder.t,
tApp.App.LastCommitID(),
tApp.crashingApp.LastCommitID(),
"Non-determinism in state detected, expected LastCommitID to match.",
)
}
// Recheck the remaining transactions in the mempool pruning any that have failed during recheck.
passingRecheckTxs := make([][]byte, 0)
for _, passingCheckTx := range tApp.passingCheckTxs {
recheckTxRequest := abcitypes.RequestCheckTx{
Tx: passingCheckTx,
Type: abcitypes.CheckTxType_Recheck,
}
recheckTxResponse := tApp.App.CheckTx(recheckTxRequest)
if recheckTxResponse.IsOK() {
passingRecheckTxs = append(passingRecheckTxs, passingCheckTx)
}
if tApp.builder.enableNonDeterminismChecks {
parallelRecheckTxResponse := tApp.parallelApp.CheckTx(recheckTxRequest)
require.Equalf(
tApp.builder.t,
recheckTxResponse.Code,
parallelRecheckTxResponse.Code,
"Non-determinism detected during RecheckTx, expected %+v, got %+v.",
recheckTxResponse,
parallelRecheckTxResponse,
)
// None of the transactions should be rechecked in `noCheckTxApp` since the transaction will only
// process block proposals and blocks. Also, none of the transactions should be rechecked for
// tApp.crashingApp since the mempool should be discarded on each crash.
}
}
tApp.passingCheckTxs = passingRecheckTxs
}
return tApp.App.NewContext(true, tApp.header)
}
// GetHeader fetches the current header of the test app.
func (tApp *TestApp) GetHeader() tmproto.Header {
return tApp.header
}
// GetBlockHeight fetches the current block height of the test app.
func (tApp *TestApp) GetBlockHeight() int64 {
return tApp.header.Height
}
// GetHalted fetches the halted flag.
func (tApp *TestApp) GetHalted() bool {
return tApp.halted
}
// newTestingLogger returns a logger that will write to stdout if testing is verbose. This method replaces
// cometbft's log.TestingLogger, which re-uses the same logger for all tests, which can cause race test false positives
// when accessed by concurrent go routines in the same test.
func newTestingLogger() log.Logger {
if testing.Verbose() {
return log.NewTMLogger(log.NewSyncWriter(os.Stdout))
} else {
return log.NewNopLogger()
}
}
// CheckTx adds the transaction to a test specific "mempool" that will be used to deliver the transaction during
// Prepare/Process proposal. Note that this must be invoked over TestApp.App.CheckTx as the transaction will not
// be added to the "mempool" causing the transaction to not be supplied during the Prepare/Process proposal phase.
//
// This method is thread-safe.
func (tApp *TestApp) CheckTx(req abcitypes.RequestCheckTx) abcitypes.ResponseCheckTx {
tApp.panicIfChainIsHalted()
res := tApp.App.CheckTx(req)
// Note that the dYdX fork of CometBFT explicitly excludes place and cancel order messages. See
// https://github.com/dydxprotocol/cometbft/blob/4d4d3b0/mempool/v0/clist_mempool.go#L416
if res.IsOK() && !mempool.IsShortTermClobOrderTransaction(req.Tx, newTestingLogger()) {
// We want to ensure that we hold the lock only for updating passingCheckTxs so that App.CheckTx can execute
// concurrently.
tApp.passingCheckTxsMtx.Lock()
defer tApp.passingCheckTxsMtx.Unlock()
tApp.passingCheckTxs = append(tApp.passingCheckTxs, req.Tx)
}
if tApp.builder.enableNonDeterminismChecks {
// We expect the parallel app to always produce the same result since all in memory state should be
// consistent with tApp.App and produce the same result.
parallelRes := tApp.parallelApp.CheckTx(req)
require.Equalf(
tApp.builder.t,
res.Code,
parallelRes.Code,
"Non-determinism detected during CheckTx, expected %+v, got %+v.",
res,
parallelRes,
)
// The crashing app may or may not be able to get to a recoverable state that would produce equivalent
// results. For example short-term orders and cancellations will be lost from in-memory state.
crashingRes := tApp.crashingApp.CheckTx(req)
if tApp.builder.enableCrashingAppCheckTxNonDeterminismChecks {
require.Equalf(
tApp.builder.t,
res.Code,
crashingRes.Code,
"Non-determinism detected during CheckTx, expected %+v, got %+v.",
res,
crashingRes,
)
}
}
return res
}
// panicIfChainIsHalted panics if the chain is halted.
func (tApp *TestApp) panicIfChainIsHalted() {
if tApp.halted {
panic("Chain is halted")
}
}
// PrepareProposal creates an abci `RequestPrepareProposal` using the current state of the chain
// and calls the PrepareProposal handler to return an abci `ResponsePrepareProposal`.
func (tApp *TestApp) PrepareProposal() abcitypes.ResponsePrepareProposal {
return tApp.App.PrepareProposal(abcitypes.RequestPrepareProposal{
Txs: tApp.passingCheckTxs,
MaxTxBytes: math.MaxInt64,
Height: tApp.header.Height,
Time: tApp.header.Time,
NextValidatorsHash: tApp.header.NextValidatorsHash,
ProposerAddress: tApp.header.ProposerAddress,
})