-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathconfig.go
510 lines (425 loc) · 13.2 KB
/
config.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
package launchtools
import (
"bufio"
"encoding/json"
"fmt"
"math/rand"
"os"
"reflect"
"time"
"github.com/pkg/errors"
"github.com/cosmos/cosmos-sdk/client/input"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/go-bip39"
"github.com/initia-labs/OPinit/contrib/launchtools/utils"
ophosttypes "github.com/initia-labs/OPinit/x/ophost/types"
)
type Config struct {
L1Config *L1Config `json:"l1_config,omitempty"`
L2Config *L2Config `json:"l2_config,omitempty"`
OpBridge *OpBridge `json:"op_bridge,omitempty"`
SystemKeys *SystemKeys `json:"system_keys,omitempty"`
GenesisAccounts *GenesisAccounts `json:"genesis_accounts,omitempty"`
}
func NewConfig(path string) (*Config, error) {
if path == "" {
return &Config{}, nil
}
bz, err := os.ReadFile(path)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("failed to read file: %s", path))
}
ret := new(Config)
if err := json.Unmarshal(bz, ret); err != nil {
return nil, err
}
return ret, nil
}
func (i *Config) Finalize(buf *bufio.Reader) error {
if i.L1Config == nil {
i.L1Config = &L1Config{}
}
if i.L2Config == nil {
i.L2Config = &L2Config{}
}
if i.OpBridge == nil {
i.OpBridge = &OpBridge{}
}
if i.SystemKeys == nil {
i.SystemKeys = &SystemKeys{}
}
if i.GenesisAccounts == nil {
i.GenesisAccounts = &GenesisAccounts{}
}
// finalize all fields
if err := i.L1Config.Finalize(buf); err != nil {
return err
}
if err := i.L2Config.Finalize(); err != nil {
return err
}
if err := i.OpBridge.Finalize(buf); err != nil {
return err
}
if err := i.SystemKeys.Finalize(buf, i.OpBridge.BatchSubmissionTarget); err != nil {
return err
}
if err := i.GenesisAccounts.Finalize(*i.SystemKeys); err != nil {
return err
}
return nil
}
type L2Config struct {
ChainID string `json:"chain_id,omitempty"`
Denom string `json:"denom,omitempty"`
Moniker string `json:"moniker,omitempty"`
// block parameters
BlockMaxBytes int64 `json:"block_max_bytes,omitempty"`
BlockMaxGas int64 `json:"block_max_gas,omitempty"`
// BridgeID will be generated after the launch.
BridgeID uint64 `json:"bridge_id,omitempty"`
}
func (l2config *L2Config) Finalize() error {
if l2config.ChainID == "" {
l2config.ChainID = fmt.Sprintf("minitia-%s-1", randString(6))
}
if l2config.Denom == "" {
l2config.Denom = "umin"
}
if l2config.Moniker == "" {
l2config.Moniker = "operator"
}
if l2config.BlockMaxBytes == 0 {
l2config.BlockMaxBytes = 22_020_096 // 21MB
}
if l2config.BlockMaxGas == 0 {
l2config.BlockMaxGas = 100_000_000 // 100M
}
return nil
}
type OpBridge struct {
// output submission setup
OutputSubmissionInterval *time.Duration `json:"output_submission_interval,omitempty"`
OutputFinalizationPeriod *time.Duration `json:"output_finalization_period,omitempty"`
OutputSubmissionStartHeight uint64 `json:"output_submission_start_height,omitempty"`
// batch submission setup
BatchSubmissionTarget ophosttypes.BatchInfo_ChainType `json:"batch_submission_target"`
// oracle setup
EnableOracle *bool `json:"enable_oracle,omitempty"`
}
func (opBridge *OpBridge) Finalize(buf *bufio.Reader) error {
if opBridge.OutputSubmissionStartHeight == 0 {
opBridge.OutputSubmissionStartHeight = 1
}
if opBridge.BatchSubmissionTarget == ophosttypes.BatchInfo_UNSPECIFIED {
useCelestia, err := input.GetConfirmation("Use Celestia as DA layer?", buf, os.Stderr)
if err != nil {
return err
}
if useCelestia {
opBridge.BatchSubmissionTarget = ophosttypes.BatchInfo_CELESTIA
} else {
opBridge.BatchSubmissionTarget = ophosttypes.BatchInfo_INITIA
}
}
if opBridge.OutputSubmissionInterval == nil {
interval := time.Hour
opBridge.OutputSubmissionInterval = &interval
}
if opBridge.OutputFinalizationPeriod == nil {
period := time.Hour * 24 * 7 // 7 days
opBridge.OutputFinalizationPeriod = &period
}
if opBridge.EnableOracle == nil {
enableOracle, err := input.GetConfirmation("Enable oracle?", buf, os.Stderr)
if err != nil {
return err
}
opBridge.EnableOracle = &enableOracle
}
return nil
}
func (opBridge *OpBridge) UnmarshalJSON(data []byte) error {
var tmp struct {
OutputSubmissionInterval string `json:"output_submission_interval,omitempty"`
OutputFinalizationPeriod string `json:"output_finalization_period,omitempty"`
OutputSubmissionStartHeight uint64 `json:"output_submission_start_height,omitempty"`
BatchSubmissionTarget ophosttypes.BatchInfo_ChainType `json:"batch_submission_target,omitempty"`
EnableOracle *bool `json:"enable_oracle,omitempty"`
}
if err := json.Unmarshal(data, &tmp); err != nil {
return err
}
if tmp.OutputSubmissionInterval != "" {
d, err := time.ParseDuration(tmp.OutputSubmissionInterval)
if err != nil {
return err
}
opBridge.OutputSubmissionInterval = &d
}
if tmp.OutputFinalizationPeriod != "" {
d, err := time.ParseDuration(tmp.OutputFinalizationPeriod)
if err != nil {
return err
}
opBridge.OutputFinalizationPeriod = &d
}
opBridge.OutputSubmissionStartHeight = tmp.OutputSubmissionStartHeight
opBridge.BatchSubmissionTarget = tmp.BatchSubmissionTarget
opBridge.EnableOracle = tmp.EnableOracle
return nil
}
func (opBridge OpBridge) MarshalJSON() ([]byte, error) {
tmp := struct {
OutputSubmissionInterval string `json:"output_submission_interval,omitempty"`
OutputFinalizationPeriod string `json:"output_finalization_period,omitempty"`
OutputSubmissionStartHeight uint64 `json:"output_submission_start_height,omitempty"`
BatchSubmissionTarget ophosttypes.BatchInfo_ChainType `json:"batch_submission_target,omitempty"`
EnableOracle *bool `json:"enable_oracle,omitempty"`
}{
OutputSubmissionStartHeight: opBridge.OutputSubmissionStartHeight,
BatchSubmissionTarget: opBridge.BatchSubmissionTarget,
EnableOracle: opBridge.EnableOracle,
}
if opBridge.OutputSubmissionInterval != nil {
tmp.OutputSubmissionInterval = opBridge.OutputSubmissionInterval.String()
}
if opBridge.OutputFinalizationPeriod != nil {
tmp.OutputFinalizationPeriod = opBridge.OutputFinalizationPeriod.String()
}
return json.Marshal(tmp)
}
type L1Config struct {
ChainID string `json:"chain_id,omitempty"`
RPC_URL string `json:"rpc_url,omitempty"`
GasPrices string `json:"gas_prices,omitempty"`
}
func (l1config *L1Config) Finalize(buf *bufio.Reader) error {
if l1config.ChainID == "" {
chainID, err := input.GetString("Enter L1 chain id", buf)
if err != nil {
return err
}
l1config.ChainID = chainID
}
if l1config.RPC_URL == "" {
defaultRPC := fmt.Sprintf("https://rpc.%s.initia.xyz:443", l1config.ChainID)
prompt := fmt.Sprintf("Use default L1 rpc [%s]?", defaultRPC)
useDefault, err := input.GetConfirmation(prompt, buf, os.Stderr)
if err != nil {
return err
}
if useDefault {
l1config.RPC_URL = fmt.Sprintf("https://rpc.%s.initia.xyz:443", l1config.ChainID)
} else {
rpcURL, err := input.GetString("Enter L1 rpc url", buf)
if err != nil {
return err
}
l1config.RPC_URL = rpcURL
}
}
if l1config.GasPrices == "" {
l1config.GasPrices = "0.015uinit"
}
_, err := sdk.ParseDecCoins(l1config.GasPrices)
if err != nil {
return errors.Wrap(err, "failed to parse gas prices")
}
return nil
}
type SystemAccount struct {
L1Address string `json:"l1_address,omitempty"`
L2Address string `json:"l2_address,omitempty"`
DAAddress string `json:"da_address,omitempty"`
Mnemonic string `json:"mnemonic,omitempty"`
}
type GenesisAccount struct {
Address string `json:"address,omitempty"`
Coins string `json:"coins,omitempty"`
}
type GenesisAccounts []GenesisAccount
func (gas *GenesisAccounts) Finalize(systemKeys SystemKeys) error {
keys := reflect.ValueOf(systemKeys)
for idx := 0; idx < keys.NumField(); idx++ {
k, ok := keys.Field(idx).Interface().(*SystemAccount)
if !ok {
return errors.New("systemKeys must be of type launcher.Account")
}
if k.L2Address == "" {
continue
}
found := false
for _, ga := range *gas {
if ga.Address == k.L2Address {
found = true
break
}
}
if found {
continue
}
*gas = append(*gas, GenesisAccount{
Address: k.L2Address,
Coins: "",
})
}
for _, ga := range *gas {
if ga.Address == "" {
return errors.New("genesis account address cannot be empty")
}
_, err := sdk.ParseCoinsNormalized(ga.Coins)
if err != nil {
return errors.Wrap(err, "failed to parse genesis account coins")
}
}
return nil
}
type SystemKeys struct {
Admin *SystemAccount `json:"admin,omitempty"`
Validator *SystemAccount `json:"validator,omitempty"`
BridgeExecutor *SystemAccount `json:"bridge_executor,omitempty"`
OutputSubmitter *SystemAccount `json:"output_submitter,omitempty"`
BatchSubmitter *SystemAccount `json:"batch_submitter,omitempty"`
// Challenger does not require mnemonic
Challenger *SystemAccount `json:"challenger,omitempty"`
}
const mnemonicEntropySize = 256
func generateMnemonic() (string, error) {
entropySeed, err := bip39.NewEntropy(mnemonicEntropySize)
if err != nil {
return "", err
}
mnemonic, err := bip39.NewMnemonic(entropySeed)
if err != nil {
return "", err
}
return mnemonic, nil
}
func (systemKeys *SystemKeys) Finalize(buf *bufio.Reader, batchSubmissionTarget ophosttypes.BatchInfo_ChainType) error {
if systemKeys.Validator == nil {
mnemonic, err := generateMnemonic()
if err != nil {
return errors.New("failed to generate mnemonic")
}
// derive address
l2Addr, err := utils.DeriveL2Address(mnemonic)
if err != nil {
return errors.Wrap(err, "failed to derive address")
}
systemKeys.Validator = &SystemAccount{
L2Address: l2Addr,
Mnemonic: mnemonic,
}
}
if systemKeys.BatchSubmitter == nil {
mnemonic, err := generateMnemonic()
if err != nil {
return errors.New("failed to generate mnemonic")
}
// derive address
daAddr, err := utils.DeriveDAAddress(mnemonic, batchSubmissionTarget)
if err != nil {
return errors.Wrap(err, "failed to derive address")
}
systemKeys.BatchSubmitter = &SystemAccount{
DAAddress: daAddr,
Mnemonic: mnemonic,
}
}
if systemKeys.BridgeExecutor == nil {
mnemonic, err := input.GetString("Enter L1 gas token funded bridge_executor bip39 mnemonic", buf)
if err != nil {
return err
}
if !bip39.IsMnemonicValid(mnemonic) {
return errors.New("invalid mnemonic")
}
// derive address
l1Addr, err := utils.DeriveL1Address(mnemonic)
if err != nil {
return errors.Wrap(err, "failed to derive address")
}
l2Addr, err := utils.DeriveL2Address(mnemonic)
if err != nil {
return errors.Wrap(err, "failed to derive address")
}
systemKeys.BridgeExecutor = &SystemAccount{
L1Address: l1Addr,
L2Address: l2Addr,
Mnemonic: mnemonic,
}
}
if systemKeys.Challenger == nil {
mnemonic, err := generateMnemonic()
if err != nil {
return errors.New("failed to generate mnemonic")
}
// derive address
l1Addr, err := utils.DeriveL1Address(mnemonic)
if err != nil {
return errors.Wrap(err, "failed to derive address")
}
l2Addr, err := utils.DeriveL2Address(mnemonic)
if err != nil {
return errors.Wrap(err, "failed to derive address")
}
systemKeys.Challenger = &SystemAccount{
L1Address: l1Addr,
L2Address: l2Addr,
Mnemonic: mnemonic,
}
}
if systemKeys.OutputSubmitter == nil {
mnemonic, err := generateMnemonic()
if err != nil {
return errors.New("failed to generate mnemonic")
}
// derive address
l1Addr, err := utils.DeriveL1Address(mnemonic)
if err != nil {
return errors.Wrap(err, "failed to derive address")
}
systemKeys.OutputSubmitter = &SystemAccount{
L1Address: l1Addr,
Mnemonic: mnemonic,
}
}
if systemKeys.Admin == nil {
// use validator account as admin account if not set
systemKeys.Admin = &SystemAccount{
L1Address: systemKeys.Validator.L2Address,
L2Address: systemKeys.Validator.L2Address,
Mnemonic: systemKeys.Validator.Mnemonic,
}
}
// validate all accounts
if systemKeys.Admin.L2Address == "" {
return errors.New("admin account not initialized")
}
if systemKeys.Validator.L2Address == "" || systemKeys.Validator.Mnemonic == "" {
return errors.New("validator account not initialized")
}
if systemKeys.BridgeExecutor.L1Address == "" || systemKeys.BridgeExecutor.L2Address == "" || systemKeys.BridgeExecutor.Mnemonic == "" {
return errors.New("bridge_executor account not initialized")
}
if systemKeys.BatchSubmitter.DAAddress == "" {
return errors.New("batch_submitter account not initialized")
}
if systemKeys.OutputSubmitter.L1Address == "" {
return errors.New("output_submitter account not initialized")
}
if systemKeys.Challenger.L1Address == "" || systemKeys.Challenger.L2Address == "" {
return errors.New("challenger account not initialized")
}
return nil
}
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randString(n int) string {
rand := rand.New(rand.NewSource(time.Now().UnixNano())) //nolint
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}