forked from envadiv/mainnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
246 lines (195 loc) · 6.39 KB
/
main.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
234
235
236
237
238
239
240
241
242
243
244
245
246
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
passage "github.com/envadiv/Passage3D/app"
"github.com/tendermint/tendermint/types"
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
auth "github.com/cosmos/cosmos-sdk/x/auth/types"
bank "github.com/cosmos/cosmos-sdk/x/bank/types"
distribution "github.com/cosmos/cosmos-sdk/x/distribution/types"
"github.com/spf13/cobra"
)
const CommunityPoolPassage3DAmount = 59_750_485
func main() {
rootCmd := &cobra.Command{}
var errorsAsWarnings bool
buildGenesisCmd := &cobra.Command{
Use: "build-genesis [genesis-dir]",
Long: "Builds a [genesis-dir]/genesis.json file from accounts.csv and [genesis-dir]/genesis.tmpl.json",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
genDir := args[0]
genTmpl := filepath.Join(genDir, "genesis.tmpl.json")
doc, err := types.GenesisDocFromFile(genTmpl)
if err != nil {
return err
}
accountsCsv, err := os.Open("accounts.csv")
if err != nil {
return err
}
auditTsv, err := os.OpenFile(filepath.Join(genDir, "account_dump.tsv"), os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
if err != nil {
return err
}
err = Process(doc, accountsCsv, CommunityPoolPassage3DAmount, auditTsv, errorsAsWarnings)
if err != nil {
return err
}
genFile := filepath.Join(genDir, "vesting-accounts-genesis.json")
doc.ChainID = genDir
return doc.SaveAs(genFile)
},
}
buildGenesisCmd.Flags().BoolVar(&errorsAsWarnings, "errors-as-warnings", false, "Allows records with errors to be ignored with a warning rather than failing")
rootCmd.AddCommand(buildGenesisCmd)
// adding the claim records cmd
rootCmd.AddCommand(AddClaimRecords())
// address converter
rootCmd.AddCommand(AddressConverter())
err := rootCmd.Execute()
if err != nil {
panic(err)
}
}
func Process(doc *types.GenesisDoc, accountsCsv io.Reader, communityPoolPassage3D int, auditOutput io.Writer, errorsAsWarnings bool) error {
accounts, balances, err := buildAccounts(accountsCsv, doc.GenesisTime, auditOutput, errorsAsWarnings)
if err != nil {
return err
}
var genState map[string]json.RawMessage
err = json.Unmarshal(doc.AppState, &genState)
if err != nil {
return err
}
cdc := passage.MakeEncodingConfig()
err = setAccounts(cdc.Marshaler, genState, accounts, balances, communityPoolPassage3D)
if err != nil {
return err
}
doc.AppState, err = json.Marshal(genState)
if err != nil {
return err
}
return nil
}
func buildAccounts(accountsCsv io.Reader, genesisTime time.Time, auditOutput io.Writer, errorsAsWarnings bool) ([]auth.AccountI, []bank.Balance, error) {
records, err := ParseAccountsCsv(accountsCsv, genesisTime, errorsAsWarnings)
if err != nil {
return nil, nil, err
}
accounts := make([]Account, 0, len(records))
for _, record := range records {
acc, err := RecordToAccount(record, genesisTime)
if err != nil {
return nil, nil, err
}
err = acc.Validate()
if err != nil {
buf := new(bytes.Buffer)
PrintAccountAudit([]Account{acc}, genesisTime, buf)
return nil, nil, fmt.Errorf("error on RecordToAccount: %w, Account: %s", err, buf.String())
}
accounts = append(accounts, acc)
}
accMap, err := MergeAccounts(accounts)
if err != nil {
return nil, nil, fmt.Errorf("error on MergeAccounts: %w", err)
}
// err = AirdropPassage3DForMinFees(accMap, genesisTime)
// if err != nil {
// return nil, nil, err
// }
accounts = SortAccounts(accMap)
PrintAccountAudit(accounts, genesisTime, auditOutput)
authAccounts := make([]auth.AccountI, 0, len(accounts))
balances := make([]bank.Balance, 0, len(accounts))
for _, acc := range accounts {
authAcc, bal, err := ToCosmosAccount(acc, genesisTime)
if err != nil {
return nil, nil, fmt.Errorf("error on ToCosmosAccount: %w", err)
}
genAcc, ok := authAcc.(auth.GenesisAccount)
if ok {
err = genAcc.Validate()
if err != nil {
return nil, nil, err
}
}
err = ValidateVestingAccount(authAcc)
if err != nil {
return nil, nil, err
}
authAccounts = append(authAccounts, authAcc)
balances = append(balances, *bal)
}
return authAccounts, balances, nil
}
func setAccounts(cdc codec.Codec, genesis map[string]json.RawMessage, accounts []auth.AccountI, balances []bank.Balance, communityPoolPassage3D int) error {
var bankGenesis bank.GenesisState
err := cdc.UnmarshalJSON(genesis[bank.ModuleName], &bankGenesis)
if err != nil {
return err
}
// create distribution module account and corresponding balance with community pool funded
distrMacc, distrBalance, err := buildDistrMaccAndBalance(communityPoolPassage3D)
if err != nil {
return err
}
bankGenesis.Balances = append(bankGenesis.Balances, balances...)
bankGenesis.Balances = append(bankGenesis.Balances, *distrBalance)
var supply sdk.Coins
for _, bal := range bankGenesis.Balances {
supply = supply.Add(bal.Coins...)
}
bankGenesis.Supply = supply
genesis[bank.ModuleName], err = cdc.MarshalJSON(&bankGenesis)
var authGenesis auth.GenesisState
err = cdc.UnmarshalJSON(genesis[auth.ModuleName], &authGenesis)
if err != nil {
return err
}
for _, acc := range accounts {
any, err := cdctypes.NewAnyWithValue(acc)
if err != nil {
return err
}
authGenesis.Accounts = append(authGenesis.Accounts, any)
}
distrMaccAny, err := cdctypes.NewAnyWithValue(distrMacc)
if err != nil {
return err
}
authGenesis.Accounts = append(authGenesis.Accounts, distrMaccAny)
genesis[auth.ModuleName], err = cdc.MarshalJSON(&authGenesis)
var distrGenesis distribution.GenesisState
err = cdc.UnmarshalJSON(genesis[distribution.ModuleName], &distrGenesis)
if err != nil {
return err
}
// set CommunityPool to balance of distribution module account
distrGenesis.FeePool.CommunityPool = sdk.NewDecCoinsFromCoins(distrBalance.Coins...)
genesis[distribution.ModuleName], err = cdc.MarshalJSON(&distrGenesis)
return nil
}
func buildDistrMaccAndBalance(passageAmount int) (auth.ModuleAccountI, *bank.Balance, error) {
maccPerms := passage.GetMaccPerms()
distrMacc := auth.NewEmptyModuleAccount(distribution.ModuleName, maccPerms[distribution.ModuleName]...)
distrCoins, err := Passage3DToCoins(NewDecFromInt64(int64(passageAmount)))
if err != nil {
return nil, nil, err
}
distrBalance := &bank.Balance{
Coins: distrCoins,
Address: distrMacc.Address,
}
return distrMacc, distrBalance, nil
}