-
Notifications
You must be signed in to change notification settings - Fork 1
/
genesis.go
233 lines (204 loc) · 7.57 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
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
231
232
233
package e2e
import (
"encoding/json"
"fmt"
"os"
"time"
"github.com/cosmos/cosmos-sdk/codec"
"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"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
icagenesistypes "github.com/cosmos/ibc-go/v6/modules/apps/27-interchain-accounts/genesis/types"
icatypes "github.com/cosmos/ibc-go/v6/modules/apps/27-interchain-accounts/types"
appparams "github.com/celinium-network/celinium/app/params"
epochstypes "github.com/celinium-network/celinium/x/epochs/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(cdc codec.Codec, path, moniker, amountStr string, addrAll []sdk.AccAddress, 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 icagenesistypes.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
stakingGenState := stakingtypes.GetGenesisStateFromAppState(cdc, appState)
stakingGenState.Params.BondDenom = denom
stakingGenState.Params.UnbondingTime = time.Hour * 2
stakingGenStateBz, err := cdc.MarshalJSON(stakingGenState)
if err != nil {
return fmt.Errorf("failed to marshal staking genesis state: %s", err)
}
appState[stakingtypes.ModuleName] = stakingGenStateBz
epochGenState := mockEpochGenesis()
epochGenStateBz, err := cdc.MarshalJSON(epochGenState)
if err != nil {
return fmt.Errorf("failed to marshal staking genesis state: %s", err)
}
appState[epochstypes.ModuleName] = epochGenStateBz
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)
}
func mockEpochGenesis() *epochstypes.GenesisState {
epochs := []epochstypes.EpochInfo{
{
Identifier: epochstypes.WeekEpochID,
StartTime: time.Time{},
Duration: time.Hour * 24 * 7,
CurrentEpoch: 0,
CurrentEpochStartHeight: 0,
CurrentEpochStartTime: time.Time{},
EpochCountingStarted: false,
},
{
Identifier: epochstypes.DayEpochID,
StartTime: time.Time{},
Duration: time.Hour * 24,
CurrentEpoch: 0,
CurrentEpochStartHeight: 0,
CurrentEpochStartTime: time.Time{},
EpochCountingStarted: false,
},
{
Identifier: appparams.DelegationEpochIdentifier,
StartTime: time.Time{},
Duration: time.Minute * 10,
CurrentEpoch: 0,
CurrentEpochStartHeight: 0,
CurrentEpochStartTime: time.Time{},
EpochCountingStarted: false,
},
{
Identifier: appparams.UndelegationEpochIdentifier,
StartTime: time.Time{},
Duration: time.Minute * 20,
CurrentEpoch: 0,
CurrentEpochStartHeight: 0,
CurrentEpochStartTime: time.Time{},
EpochCountingStarted: false,
},
{
Identifier: appparams.ReinvestEpochIdentifier,
StartTime: time.Time{},
Duration: time.Minute * 15,
CurrentEpoch: 0,
CurrentEpochStartHeight: 0,
CurrentEpochStartTime: time.Time{},
EpochCountingStarted: false,
},
}
return epochstypes.NewGenesisState(epochs)
}