/
genesis.go
200 lines (165 loc) · 5.92 KB
/
genesis.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
package simulation
// DONTCOVER
import (
"fmt"
"time"
"github.com/gridiron-zone/huddle/testutil/profilestesting"
subspacestypes "github.com/gridiron-zone/huddle/x/subspaces/types"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/gridiron-zone/huddle/x/profiles/types"
)
// RandomizedGenState generates a random GenesisState for profile
func RandomizedGenState(simsState *module.SimulationState) {
profilesNumber := len(simsState.Accounts)
profiles := NewRandomProfiles(simsState.Rand, simsState.Accounts, profilesNumber)
// Update the auth state with the profiles
var authState authtypes.GenesisState
err := simsState.Cdc.UnmarshalJSON(simsState.GenState[authtypes.ModuleName], &authState)
if err != nil {
panic(err)
}
genAccounts, err := mergeAccountsWithProfiles(authState.Accounts, profiles)
if err != nil {
panic(err)
}
authState.Accounts = genAccounts
bz, err := simsState.Cdc.MarshalJSON(&authState)
if err != nil {
panic(err)
}
simsState.GenState[authtypes.ModuleName] = bz
// Create and set profiles state
var subspacesState subspacestypes.GenesisState
err = simsState.Cdc.UnmarshalJSON(simsState.GenState[subspacestypes.ModuleName], &subspacesState)
if err != nil {
panic(err)
}
chainLinks := randomChainLinks(profiles, simsState)
profileGenesis := types.NewGenesisState(
randomDTagTransferRequests(profiles, simsState, simsState.Rand.Intn(profilesNumber)),
types.NewParams(
RandomNicknameParams(simsState.Rand),
RandomDTagParams(simsState.Rand),
RandomBioParams(simsState.Rand),
RandomOracleParams(simsState.Rand),
RandomAppLinksParams(simsState.Rand),
),
types.IBCPortID,
chainLinks,
getDefaultExternalAddressEntries(chainLinks),
nil,
)
bz, err = simsState.Cdc.MarshalJSON(profileGenesis)
if err != nil {
panic(err)
}
fmt.Printf("Selected randomly generated profile parameters:\n%s\n", bz)
simsState.GenState[types.ModuleName] = simsState.Cdc.MustMarshalJSON(profileGenesis)
}
// mergeAccountsWithProfiles merges the provided x/auth genesis accounts with the given profiles, replacing
// any existing account with the associated profile (if existing).
func mergeAccountsWithProfiles(genAccounts []*codectypes.Any, profiles []*types.Profile) ([]*codectypes.Any, error) {
// Unpack the accounts
accounts, err := authtypes.UnpackAccounts(genAccounts)
if err != nil {
return nil, err
}
for index, account := range accounts {
// See if the account also has a profile
var profile *types.Profile
for _, p := range profiles {
if p.GetAddress().Equals(account.GetAddress()) {
profile = p
break
}
}
// If found replace the account with the profile
if profile != nil {
accounts[index] = profile
}
}
return authtypes.PackAccounts(accounts)
}
// -------------------------------------------------------------------------------------------------------------------
// randomDTagTransferRequests returns randomly generated genesis dTag transfer requests
func randomDTagTransferRequests(
profiles []*types.Profile, simState *module.SimulationState, number int,
) []types.DTagTransferRequest {
dTagTransferRequests := make([]types.DTagTransferRequest, number)
for i := 0; i < number; {
profile1 := RandomProfile(simState.Rand, profiles)
profile2 := RandomProfile(simState.Rand, profiles)
// Skip same profiles
if profile1.GetAddress().Equals(profile2.GetAddress()) {
continue
}
request := types.NewDTagTransferRequest(
profile2.DTag,
profile1.GetAddress().String(),
profile2.GetAddress().String(),
)
// Skip duplicated requests
if !containsDTagTransferRequest(dTagTransferRequests, request) {
dTagTransferRequests[i] = request
i++
}
}
return dTagTransferRequests
}
func containsDTagTransferRequest(slice []types.DTagTransferRequest, request types.DTagTransferRequest) bool {
for _, req := range slice {
if req.Sender == request.Sender && req.Receiver == request.Receiver {
return true
}
}
return false
}
// -------------------------------------------------------------------------------------------------------------------
// randomChainLinks returns randomly generated genesis chain links
func randomChainLinks(
profiles []*types.Profile, simsState *module.SimulationState,
) []types.ChainLink {
linksNumber := simsState.Rand.Intn(100)
links := make([]types.ChainLink, linksNumber)
for i := 0; i < linksNumber; {
// Get a random profile
profile := RandomProfile(simsState.Rand, profiles)
chainAccount := profilestesting.GetChainLinkAccount("cosmos", "cosmos")
link := chainAccount.GetBech32ChainLink(profile.GetAddress().String(), time.Date(2022, 6, 9, 0, 0, 0, 0, time.UTC))
if !containsChainLink(links, link) {
links[i] = link
i++
}
}
return links
}
func containsChainLink(slice []types.ChainLink, link types.ChainLink) bool {
for _, l := range slice {
if l.User == link.User && l.Address.Equal(link.Address) && l.ChainConfig.Name == link.ChainConfig.Name {
return true
}
}
return false
}
// -------------------------------------------------------------------------------------------------------------------
// getDefaultExternalAddressEntries returns randomly generated genesis default external address entries
func getDefaultExternalAddressEntries(links []types.ChainLink) []types.DefaultExternalAddressEntry {
entries := make([]types.DefaultExternalAddressEntry, 0, len(links))
for _, link := range links {
entry := types.NewDefaultExternalAddressEntry(link.User, link.ChainConfig.Name, link.GetAddressData().GetValue())
if !containsDefaultExternalAddressEntry(entries, entry) {
entries = append(entries, entry)
}
}
return entries
}
func containsDefaultExternalAddressEntry(slice []types.DefaultExternalAddressEntry, entry types.DefaultExternalAddressEntry) bool {
for _, e := range slice {
if e.Owner == entry.Owner && e.ChainName == entry.ChainName {
return true
}
}
return false
}