/
genesis.go
201 lines (171 loc) · 6.62 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
201
package e2e
import (
"encoding/json"
"fmt"
"os"
"time"
"github.com/cosmos/cosmos-sdk/server"
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"
"github.com/cosmos/cosmos-sdk/x/genutil"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
globfeetypes "github.com/cosmos/gaia/v8/x/globalfee/types"
icatypes "github.com/cosmos/ibc-go/v3/modules/apps/27-interchain-accounts/types"
tmtypes "github.com/tendermint/tendermint/types"
)
func getGenDoc(path string) (*tmtypes.GenesisDoc, error) {
serverCtx := server.NewDefaultContext()
config := serverCtx.Config
config.SetRoot(path)
genFile := config.GenesisFile()
doc := &tmtypes.GenesisDoc{}
if _, err := os.Stat(genFile); err != nil {
if !os.IsNotExist(err) {
return nil, err
}
} else {
var err error
doc, err = tmtypes.GenesisDocFromFile(genFile)
if err != nil {
return nil, fmt.Errorf("failed to read genesis doc from file: %w", err)
}
}
return doc, nil
}
func modifyGenesis(path, moniker, amountStr string, addrAll []sdk.AccAddress, globfees string, denom string) error {
serverCtx := server.NewDefaultContext()
config := serverCtx.Config
config.SetRoot(path)
config.Moniker = moniker
coins, err := sdk.ParseCoinsNormalized(amountStr)
if err != nil {
return fmt.Errorf("failed to parse coins: %w", err)
}
var balances []banktypes.Balance
var genAccounts []*authtypes.BaseAccount
for _, addr := range addrAll {
balance := banktypes.Balance{Address: addr.String(), Coins: coins.Sort()}
balances = append(balances, balance)
genAccount := authtypes.NewBaseAccount(addr, nil, 0, 0)
genAccounts = append(genAccounts, genAccount)
}
genFile := config.GenesisFile()
appState, genDoc, err := genutiltypes.GenesisStateFromGenFile(genFile)
if err != nil {
return fmt.Errorf("failed to unmarshal genesis state: %w", err)
}
authGenState := authtypes.GetGenesisStateFromAppState(cdc, appState)
accs, err := authtypes.UnpackAccounts(authGenState.Accounts)
if err != nil {
return fmt.Errorf("failed to get accounts from any: %w", err)
}
for _, addr := range addrAll {
if accs.Contains(addr) {
return fmt.Errorf("failed to add account to genesis state; account already exists: %s", addr)
}
}
// Add the new account to the set of genesis accounts and sanitize the
// accounts afterwards.
for _, genAcct := range genAccounts {
accs = append(accs, genAcct)
accs = authtypes.SanitizeGenesisAccounts(accs)
}
genAccs, err := authtypes.PackAccounts(accs)
if err != nil {
return fmt.Errorf("failed to convert accounts into any's: %w", err)
}
authGenState.Accounts = genAccs
authGenStateBz, err := cdc.MarshalJSON(&authGenState)
if err != nil {
return fmt.Errorf("failed to marshal auth genesis state: %w", err)
}
appState[authtypes.ModuleName] = authGenStateBz
bankGenState := banktypes.GetGenesisStateFromAppState(cdc, appState)
bankGenState.Balances = append(bankGenState.Balances, balances...)
bankGenState.Balances = banktypes.SanitizeGenesisBalances(bankGenState.Balances)
bankGenStateBz, err := cdc.MarshalJSON(bankGenState)
if err != nil {
return fmt.Errorf("failed to marshal bank genesis state: %w", err)
}
appState[banktypes.ModuleName] = bankGenStateBz
// add ica host allowed msg types
var icaGenesisState icatypes.GenesisState
if appState[icatypes.ModuleName] != nil {
cdc.MustUnmarshalJSON(appState[icatypes.ModuleName], &icaGenesisState)
}
icaGenesisState.HostGenesisState.Params.AllowMessages = []string{
"/cosmos.authz.v1beta1.MsgExec",
"/cosmos.authz.v1beta1.MsgGrant",
"/cosmos.authz.v1beta1.MsgRevoke",
"/cosmos.bank.v1beta1.MsgSend",
"/cosmos.bank.v1beta1.MsgMultiSend",
"/cosmos.distribution.v1beta1.MsgSetWithdrawAddress",
"/cosmos.distribution.v1beta1.MsgWithdrawValidatorCommission",
"/cosmos.distribution.v1beta1.MsgFundCommunityPool",
"/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward",
"/cosmos.feegrant.v1beta1.MsgGrantAllowance",
"/cosmos.feegrant.v1beta1.MsgRevokeAllowance",
"/cosmos.gov.v1beta1.MsgVoteWeighted",
"/cosmos.gov.v1beta1.MsgSubmitProposal",
"/cosmos.gov.v1beta1.MsgDeposit",
"/cosmos.gov.v1beta1.MsgVote",
"/cosmos.staking.v1beta1.MsgEditValidator",
"/cosmos.staking.v1beta1.MsgDelegate",
"/cosmos.staking.v1beta1.MsgUndelegate",
"/cosmos.staking.v1beta1.MsgBeginRedelegate",
"/cosmos.staking.v1beta1.MsgCreateValidator",
"/cosmos.vesting.v1beta1.MsgCreateVestingAccount",
"/ibc.applications.transfer.v1.MsgTransfer",
"/tendermint.liquidity.v1beta1.MsgCreatePool",
"/tendermint.liquidity.v1beta1.MsgSwapWithinBatch",
"/tendermint.liquidity.v1beta1.MsgDepositWithinBatch",
"/tendermint.liquidity.v1beta1.MsgWithdrawWithinBatch",
}
icaGenesisStateBz, err := cdc.MarshalJSON(&icaGenesisState)
if err != nil {
return fmt.Errorf("failed to marshal interchain accounts genesis state: %w", err)
}
appState[icatypes.ModuleName] = icaGenesisStateBz
// setup global fee in genesis
globfeeState := globfeetypes.GetGenesisStateFromAppState(cdc, appState)
minGases, err := sdk.ParseDecCoins(globfees)
if err != nil {
return fmt.Errorf("failed to parse fee coins: %w", err)
}
globfeeState.Params.MinimumGasPrices = minGases
globFeeStateBz, err := cdc.MarshalJSON(globfeeState)
if err != nil {
return fmt.Errorf("failed to marshal global fee genesis state: %w", err)
}
appState[globfeetypes.ModuleName] = globFeeStateBz
stakingGenState := stakingtypes.GetGenesisStateFromAppState(cdc, appState)
stakingGenState.Params.BondDenom = denom
stakingGenStateBz, err := cdc.MarshalJSON(stakingGenState)
if err != nil {
return fmt.Errorf("failed to marshal staking genesis state: %s", err)
}
appState[stakingtypes.ModuleName] = stakingGenStateBz
// Refactor to separate method
amnt := sdk.NewInt(10000)
quorum, _ := sdk.NewDecFromStr("0.000000000000000001")
threshold, _ := sdk.NewDecFromStr("0.000000000000000001")
govState := govtypes.NewGenesisState(1,
govtypes.NewDepositParams(sdk.NewCoins(sdk.NewCoin(denom, amnt)), 10*time.Minute),
govtypes.NewVotingParams(15*time.Second),
govtypes.NewTallyParams(quorum, threshold, govtypes.DefaultVetoThreshold),
)
govGenStateBz, err := cdc.MarshalJSON(govState)
if err != nil {
return fmt.Errorf("failed to marshal gov genesis state: %w", err)
}
appState[govtypes.ModuleName] = govGenStateBz
appStateJSON, err := json.Marshal(appState)
if err != nil {
return fmt.Errorf("failed to marshal application genesis state: %w", err)
}
genDoc.AppState = appStateJSON
return genutil.ExportGenesisFile(genDoc, genFile)
}