-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_suite.go
152 lines (130 loc) · 4.59 KB
/
test_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
package apptesting
import (
"context"
"crypto/rand"
"fmt"
"time"
dbm "github.com/cometbft/cometbft-db"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/crypto/ed25519"
"github.com/cometbft/cometbft/libs/log"
tmtypes "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/store/rootmulti"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/duality-labs/duality/app"
"github.com/stretchr/testify/suite"
)
type KeeperTestHelper struct {
suite.Suite
App *app.App
Ctx sdk.Context
GoCtx context.Context
// Used for testing queries end to end.
// You can wrap this in a module-specific QueryClient()
// and then make calls as you would a normal GRPC client.
QueryHelper *baseapp.QueryServiceTestHelper
}
// Setup sets up basic environment for suite (App, Ctx, and test accounts)
func (s *KeeperTestHelper) Setup() {
s.App = app.Setup(s.T(), false)
s.Ctx = s.App.BaseApp.NewContext(
false,
tmtypes.Header{Height: 1, ChainID: "duality-1", Time: time.Now().UTC()},
)
s.GoCtx = sdk.WrapSDKContext(s.Ctx)
s.QueryHelper = &baseapp.QueryServiceTestHelper{
GRPCQueryRouter: s.App.GRPCQueryRouter(),
Ctx: s.Ctx,
}
s.SetEpochStartTime()
}
// func (s *KeeperTestHelper) SetupTestForInitGenesis() {
// // Setting to True, leads to init genesis not running
// s.App = app.Setup(true)
// s.Ctx = s.App.BaseApp.NewContext(true, tmtypes.Header{})
// }
func (s *KeeperTestHelper) SetEpochStartTime() {
epochsKeeper := s.App.EpochsKeeper
for _, epoch := range epochsKeeper.AllEpochInfos(s.Ctx) {
epoch.StartTime = s.Ctx.BlockTime()
epochsKeeper.DeleteEpochInfo(s.Ctx, epoch.Identifier)
err := epochsKeeper.AddEpochInfo(s.Ctx, epoch)
if err != nil {
panic(err)
}
}
}
// setupAddr takes a balance, prefix, and address number. Then returns the respective account address byte array.
// If prefix is left blank, it will be replaced with a random prefix.
func SetupAddr(index int) sdk.AccAddress {
prefixBz := make([]byte, 8)
_, _ = rand.Read(prefixBz)
prefix := string(prefixBz)
addr := sdk.AccAddress([]byte(fmt.Sprintf("addr%s%8d", prefix, index)))
return addr
}
func (s *KeeperTestHelper) SetupAddr(index int) sdk.AccAddress {
return SetupAddr(index)
}
func SetupAddrs(numAddrs int) []sdk.AccAddress {
addrs := make([]sdk.AccAddress, numAddrs)
for i := 0; i < numAddrs; i++ {
addrs[i] = SetupAddr(i)
}
return addrs
}
func (s *KeeperTestHelper) SetupAddrs(numAddrs int) []sdk.AccAddress {
return SetupAddrs(numAddrs)
}
// These are for testing msg.ValidateBasic() functions
// which need to validate for valid/invalid addresses.
// Should not be used for anything else because these addresses
// are totally uninterpretable (100% random).
func GenerateTestAddrs() (string, string) {
pk1 := ed25519.GenPrivKey().PubKey()
validAddr := sdk.AccAddress(pk1.Address()).String()
invalidAddr := sdk.AccAddress("").String()
return validAddr, invalidAddr
}
// CreateTestContext creates a test context.
func (s *KeeperTestHelper) CreateTestContext() sdk.Context {
ctx, _ := s.CreateTestContextWithMultiStore()
return ctx
}
// CreateTestContextWithMultiStore creates a test context and returns it together with multi store.
func (s *KeeperTestHelper) CreateTestContextWithMultiStore() (sdk.Context, sdk.CommitMultiStore) {
db := dbm.NewMemDB()
logger := log.NewNopLogger()
ms := rootmulti.NewStore(db, logger)
return sdk.NewContext(ms, tmtypes.Header{}, false, logger), ms
}
// CreateTestContext creates a test context.
func (s *KeeperTestHelper) Commit() {
s.App.EndBlock(abci.RequestEndBlock{Height: s.Ctx.BlockHeight()})
oldHeight := s.Ctx.BlockHeight()
oldHeader := s.Ctx.BlockHeader()
s.App.Commit()
newHeader := tmtypes.Header{
Height: oldHeight + 1,
ChainID: oldHeader.ChainID,
Time: oldHeader.Time.Add(time.Minute),
}
s.App.BeginBlock(abci.RequestBeginBlock{Header: newHeader})
s.Ctx = s.App.GetBaseApp().NewContext(false, newHeader)
}
// FundAcc funds target address with specified amount.
func (s *KeeperTestHelper) FundAcc(acc sdk.AccAddress, amounts sdk.Coins) {
err := s.App.BankKeeper.MintCoins(s.Ctx, banktypes.ModuleName, amounts)
s.Require().NoError(err)
err = s.App.BankKeeper.SendCoinsFromModuleToAccount(s.Ctx, banktypes.ModuleName, acc, amounts)
s.Require().NoError(err)
}
// StateNotAltered validates that app state is not altered. Fails if it is.
func (s *KeeperTestHelper) StateNotAltered() {
oldState := s.App.ExportState(s.Ctx)
s.App.Commit()
newState := s.App.ExportState(s.Ctx)
s.Require().Equal(oldState, newState)
}