forked from cosmos/interchain-security
-
Notifications
You must be signed in to change notification settings - Fork 1
/
state_rapid_test.go
230 lines (204 loc) · 7.73 KB
/
state_rapid_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
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
package main
import (
"testing"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
"pgregory.net/rapid"
)
// This file contains tests for serialization/deserialization of state.
// The tests are written using the rapid testing library, which allows us to
// generate arbitrary state structs and test that they can be serialized and
// deserialized without error.
// The generators for the various parts of the state are defined in this file, and
// essentially tell rapid how to build the state.
func TestChainStateMarshalling(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
chainState := GetChainStateGen().Draw(t, "ChainState")
err := MarshalAndUnmarshalChainState(chainState)
if err != nil {
t.Fatalf("error marshalling and unmarshalling chain state: %v", err)
}
})
}
// Below this are utility functions for Rapid that define generators for the various structs that can appear in testing.
// These are used in the rapid tests and generate arbitrary test traces for fuzzing.
// These traces will not in general be useful to execute as e2e tests, since they are filled with essentially completely random values.
func GetStateGen() *rapid.Generator[State] {
return rapid.Custom(func(t *rapid.T) State {
return rapid.MapOf(GetChainIDGen(), GetChainStateGen()).Draw(t, "State")
})
}
func GetChainStateGen() *rapid.Generator[ChainState] {
return rapid.Custom(
func(t *rapid.T) ChainState {
valBalances := GetValBalancesGen().Draw(t, "ValBalances")
proposals := GetProposalsGen().Draw(t, "Proposals")
valPowers := GetValPowersGen().Draw(t, "ValPowers")
stakedTokens := GetStakedTokensGen().Draw(t, "StakedTokens")
ibctransferparams := GetIBCTransferParamsGen().Draw(t, "IBCTransferParams")
rewards := GetRewardsGen().Draw(t, "Rewards")
consumerChains := GetConsumerChainsGen().Draw(t, "ConsumerChains")
assignedKeys := GetAssignedKeysGen().Draw(t, "AssignedKeys")
providerKeys := GetProviderKeysGen().Draw(t, "ProviderKeys")
consumerPacketQueueSize := GetConsumerChainQueueSizesGen().Draw(t, "ConsumerChainQueueSizes")
registeredConsumerRewardDenoms := GetRegisteredConsumerRewardDenomsGen().Draw(t, "RegisteredConsumerRewardDenoms")
return ChainState{
ValBalances: &valBalances,
Proposals: &proposals,
ValPowers: &valPowers,
StakedTokens: &stakedTokens,
IBCTransferParams: &ibctransferparams,
Rewards: &rewards,
ConsumerChains: &consumerChains,
AssignedKeys: &assignedKeys,
ProviderKeys: &providerKeys,
ConsumerPendingPacketQueueSize: &consumerPacketQueueSize,
RegisteredConsumerRewardDenoms: ®isteredConsumerRewardDenoms,
}
})
}
func GetRegisteredConsumerRewardDenomsGen() *rapid.Generator[[]string] {
return rapid.Custom(func(t *rapid.T) []string {
return rapid.SliceOf(rapid.String()).Draw(t, "RegisteredConsumerRewardDenoms")
})
}
func GetConsumerChainQueueSizesGen() *rapid.Generator[uint] {
return rapid.Custom(func(t *rapid.T) uint {
return rapid.Uint().Draw(t, "ConsumerChainQueueSizes")
})
}
func GetProviderKeysGen() *rapid.Generator[map[ValidatorID]string] {
return rapid.Custom(func(t *rapid.T) map[ValidatorID]string {
return rapid.MapOf(GetValidatorIDGen(), rapid.String()).Draw(t, "ProviderKeys")
})
}
func GetAssignedKeysGen() *rapid.Generator[map[ValidatorID]string] {
return rapid.Custom(func(t *rapid.T) map[ValidatorID]string {
return rapid.MapOf(GetValidatorIDGen(), rapid.String()).Draw(t, "AssignedKeys")
})
}
func GetChainIDGen() *rapid.Generator[ChainID] {
return rapid.Custom(func(t *rapid.T) ChainID {
return ChainID(rapid.String().Draw(t, "ChainID"))
})
}
func GetConsumerChainsGen() *rapid.Generator[map[ChainID]bool] {
return rapid.Custom(func(t *rapid.T) map[ChainID]bool {
return rapid.MapOf(GetChainIDGen(), rapid.Bool()).Draw(t, "ConsumerChains")
})
}
func GetRewardsGen() *rapid.Generator[Rewards] {
return rapid.Custom(func(t *rapid.T) Rewards {
return Rewards{
IsIncrementalReward: rapid.Bool().Draw(t, "IsIncrementalReward"),
// Denom: rapid.Str,
IsRewarded: rapid.MapOf(GetValidatorIDGen(), rapid.Bool()).Draw(t, "IsRewarded"),
}
})
}
func GetIBCTransferParamsGen() *rapid.Generator[IBCTransferParams] {
return rapid.Custom(func(t *rapid.T) IBCTransferParams {
return IBCTransferParams{
SendEnabled: rapid.Bool().Draw(t, "SendEnabled"),
ReceiveEnabled: rapid.Bool().Draw(t, "ReceiveEnabled"),
}
})
}
func GetIBCTransferParamsProposalGen() *rapid.Generator[IBCTransferParamsProposal] {
return rapid.Custom(func(t *rapid.T) IBCTransferParamsProposal {
return IBCTransferParamsProposal{
Title: rapid.String().Draw(t, "Title"),
Deposit: rapid.Uint().Draw(t, "Deposit"),
Status: rapid.String().Draw(t, "Status"),
Params: IBCTransferParams{
SendEnabled: rapid.Bool().Draw(t, "SendEnabled"),
ReceiveEnabled: rapid.Bool().Draw(t, "ReceiveEnabled"),
},
}
})
}
func GetStakedTokensGen() *rapid.Generator[map[ValidatorID]uint] {
return rapid.Custom(func(t *rapid.T) map[ValidatorID]uint {
return rapid.MapOf(
GetValidatorIDGen(),
rapid.Uint(),
).Draw(t, "StakedTokens")
})
}
func GetValPowersGen() *rapid.Generator[map[ValidatorID]uint] {
return rapid.Custom(func(t *rapid.T) map[ValidatorID]uint {
return rapid.MapOf(
GetValidatorIDGen(),
rapid.Uint(),
).Draw(t, "ValPowers")
})
}
func GetValBalancesGen() *rapid.Generator[map[ValidatorID]uint] {
return rapid.Custom(func(t *rapid.T) map[ValidatorID]uint {
return rapid.MapOf(
GetValidatorIDGen(),
rapid.Uint(),
).Draw(t, "ValBalances")
})
}
func GetValidatorIDGen() *rapid.Generator[ValidatorID] {
return rapid.Custom(func(t *rapid.T) ValidatorID {
return ValidatorID(rapid.String().Draw(t, "ValidatorID"))
})
}
func GetProposalsGen() *rapid.Generator[map[uint]Proposal] {
return rapid.Custom(func(t *rapid.T) map[uint]Proposal {
return rapid.MapOf(
rapid.Uint(),
GetProposalGen(),
).Draw(t, "Proposals")
})
}
func GetProposalGen() *rapid.Generator[Proposal] {
return rapid.Custom(func(t *rapid.T) Proposal {
gen := rapid.OneOf(
GetConsumerAdditionProposalGen().AsAny(),
GetConsumerRemovalProposalGen().AsAny(),
GetTextProposalGen().AsAny(),
GetIBCTransferParamsProposalGen().AsAny(),
)
return gen.Draw(t, "Proposal").(Proposal)
})
}
func GetConsumerAdditionProposalGen() *rapid.Generator[ConsumerAdditionProposal] {
return rapid.Custom(func(t *rapid.T) ConsumerAdditionProposal {
return ConsumerAdditionProposal{
Deposit: rapid.Uint().Draw(t, "Deposit"),
Chain: GetChainIDGen().Draw(t, "Chain"),
SpawnTime: rapid.Int().Draw(t, "SpawnTime"),
InitialHeight: GetHeightGen().Draw(t, "InitialHeight"),
Status: rapid.String().Draw(t, "Status"),
}
})
}
func GetConsumerRemovalProposalGen() *rapid.Generator[ConsumerRemovalProposal] {
return rapid.Custom(func(t *rapid.T) ConsumerRemovalProposal {
return ConsumerRemovalProposal{
Deposit: rapid.Uint().Draw(t, "Deposit"),
Chain: GetChainIDGen().Draw(t, "Chain"),
Status: rapid.String().Draw(t, "Status"),
}
})
}
func GetTextProposalGen() *rapid.Generator[TextProposal] {
return rapid.Custom(func(t *rapid.T) TextProposal {
return TextProposal{
Title: rapid.String().Draw(t, "Title"),
Description: rapid.String().Draw(t, "Description"),
Deposit: rapid.Uint().Draw(t, "Deposit"),
Status: rapid.String().Draw(t, "Status"),
}
})
}
func GetHeightGen() *rapid.Generator[clienttypes.Height] {
return rapid.Custom(func(t *rapid.T) clienttypes.Height {
return clienttypes.Height{
RevisionNumber: rapid.Uint64().Draw(t, "RevisionNumber"),
RevisionHeight: rapid.Uint64().Draw(t, "RevisionHeight"),
}
})
}