-
Notifications
You must be signed in to change notification settings - Fork 574
/
state.go
175 lines (154 loc) · 5.19 KB
/
state.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
package simapp
import (
"encoding/json"
"log"
"math/rand"
"os"
"time"
"github.com/osmosis-labs/osmosis/osmomath"
"github.com/osmosis-labs/osmosis/v22/app"
osmosim "github.com/osmosis-labs/osmosis/v22/simulation/executor"
osmosimtypes "github.com/osmosis-labs/osmosis/v22/simulation/simtypes"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
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"
)
// InitChainFn returns the initial application state using a genesis or the simulation parameters.
// It panics if the user provides files for both of them.
// If a file is not given for the genesis or the sim params, it creates a randomized one.
func InitChainFn() osmosim.InitChainFn {
cdc := app.MakeEncodingConfig().Marshaler
return func(simManager osmosimtypes.ModuleGenesisGenerator, r *rand.Rand, accs []simtypes.Account, config osmosim.InitializationConfig,
) (simAccs []simtypes.Account, req abci.RequestInitChain) {
// N.B.: wasmd has the following check in its simulator:
// https://github.com/osmosis-labs/wasmd/blob/c2ec9092d086b5ac6dd367f33ce8b5cce8e4c5f5/x/wasm/types/types.go#L261-L264
// As a result, it is easy to overflow and become negative if seconds are set too large.
genesisTime := time.Unix(0, r.Int63())
appParams := make(simtypes.AppParams)
if config.ParamsFile != "" {
bz, err := os.ReadFile(config.ParamsFile)
if err != nil {
panic(err)
}
err = json.Unmarshal(bz, &appParams)
if err != nil {
panic(err)
}
}
appState, simAccs := AppStateRandomizedFn(simManager, r, cdc, accs, genesisTime, appParams)
appState = updateStakingAndBankState(appState, cdc)
req = abci.RequestInitChain{
Time: genesisTime,
ChainId: config.ChainID,
ConsensusParams: osmosim.DefaultRandomConsensusParams(r, appState, cdc),
// Validators: ...,
AppStateBytes: appState,
// InitialHeight: ...,
}
return simAccs, req
}
}
func updateStakingAndBankState(appState json.RawMessage, cdc codec.JSONCodec) json.RawMessage {
rawState := make(map[string]json.RawMessage)
err := json.Unmarshal(appState, &rawState)
if err != nil {
panic(err)
}
stakingStateBz, ok := rawState[stakingtypes.ModuleName]
if !ok {
panic("staking genesis state is missing")
}
stakingState := new(stakingtypes.GenesisState)
err = cdc.UnmarshalJSON(stakingStateBz, stakingState)
if err != nil {
panic(err)
}
// compute not bonded balance
notBondedTokens := osmomath.ZeroInt()
for _, val := range stakingState.Validators {
if val.Status != stakingtypes.Unbonded {
continue
}
notBondedTokens = notBondedTokens.Add(val.GetTokens())
}
notBondedCoins := sdk.NewCoin(stakingState.Params.BondDenom, notBondedTokens)
// edit bank state to make it have the not bonded pool tokens
bankStateBz, ok := rawState[banktypes.ModuleName]
if !ok {
panic("bank genesis state is missing")
}
bankState := new(banktypes.GenesisState)
err = cdc.UnmarshalJSON(bankStateBz, bankState)
if err != nil {
panic(err)
}
stakingAddr := authtypes.NewModuleAddress(stakingtypes.NotBondedPoolName).String()
var found bool
for _, balance := range bankState.Balances {
if balance.Address == stakingAddr {
found = true
break
}
}
if !found {
bankState.Balances = append(bankState.Balances, banktypes.Balance{
Address: stakingAddr,
Coins: sdk.NewCoins(notBondedCoins),
})
}
// change appState back
rawState[stakingtypes.ModuleName] = cdc.MustMarshalJSON(stakingState)
rawState[banktypes.ModuleName] = cdc.MustMarshalJSON(bankState)
// replace appstate
appState, err = json.Marshal(rawState)
if err != nil {
panic(err)
}
return appState
}
// AppStateRandomizedFn creates calls each module's GenesisState generator function
// and creates the simulation params.
func AppStateRandomizedFn(
simManager osmosimtypes.ModuleGenesisGenerator, r *rand.Rand, cdc codec.JSONCodec,
accs []simtypes.Account, genesisTimestamp time.Time, appParams simtypes.AppParams,
) (json.RawMessage, []simtypes.Account) {
numAccs := int64(len(accs))
genesisState := app.NewDefaultGenesisState()
// generate a random amount of initial stake coins and a random initial
// number of bonded accounts
initialStake := sdk.NewInt(r.Int63n(1e12))
// Don't allow 0 validators to start off with
numInitiallyBonded := int64(r.Intn(299)) + 1
if numInitiallyBonded > numAccs {
numInitiallyBonded = numAccs
}
log.Printf(
`Selected randomly generated parameters for simulated genesis:
{
stake_per_account: "%d",
initially_bonded_validators: "%d"
}
`, initialStake, numInitiallyBonded,
)
simState := &module.SimulationState{
AppParams: appParams,
Cdc: cdc,
Rand: r,
GenState: genesisState,
Accounts: accs,
InitialStake: initialStake,
NumBonded: numInitiallyBonded,
GenTimestamp: genesisTimestamp,
}
simManager.GenerateGenesisStates(simState, &osmosimtypes.SimCtx{})
appState, err := json.Marshal(genesisState)
if err != nil {
panic(err)
}
return appState, accs
}