forked from regen-network/mainnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
237 lines (183 loc) · 6.04 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
regen "github.com/regen-network/regen-ledger/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 CommunityPoolRegenAmount = 2_000_000
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)
err = Process(doc, accountsCsv, CommunityPoolRegenAmount, auditTsv, errorsAsWarnings)
if err != nil {
return err
}
genFile := filepath.Join(genDir, "genesis.json")
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)
err := rootCmd.Execute()
if err != nil {
panic(err)
}
}
func Process(doc *types.GenesisDoc, accountsCsv io.Reader, communityPoolRegen 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, _ := regen.MakeCodecs()
err = setAccounts(cdc, genState, accounts, balances, communityPoolRegen)
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)
}
accounts, err = MergeAccounts(accounts)
if err != nil {
return nil, nil, fmt.Errorf("error on MergeAccounts: %w", err)
}
accounts = SortAccounts(accounts)
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.Marshaler, genesis map[string]json.RawMessage, accounts []auth.AccountI, balances []bank.Balance, communityPoolRegen 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(communityPoolRegen)
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(regenAmount int) (auth.ModuleAccountI, *bank.Balance, error) {
maccPerms := regen.GetMaccPerms()
distrMacc := auth.NewEmptyModuleAccount(distribution.ModuleName, maccPerms[distribution.ModuleName]...)
distrCoins, err := RegenToCoins(NewDecFromInt64(int64(regenAmount)))
if err != nil {
return nil, nil, err
}
distrBalance := &bank.Balance{
Coins: distrCoins,
Address: distrMacc.Address,
}
return distrMacc, distrBalance, nil
}