forked from dfuse-io/dfuse-eosio
/
init.go
241 lines (198 loc) · 6.49 KB
/
init.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
// Copyright 2019 dfuse Platform Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cli
import (
"errors"
"fmt"
"io/ioutil"
"os"
"runtime"
"strings"
launcher "github.com/invisible-train-40/zsw-lishi-launcher/launcher"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap"
"gopkg.in/yaml.v2"
)
var initCmd = &cobra.Command{Use: "init", Short: "Initializes dfuse's local environment", RunE: dfuseInitE}
func init() {
RootCmd.AddCommand(initCmd)
}
func dfuseInitE(cmd *cobra.Command, args []string) (err error) {
configFile := viper.GetString("global-config-file")
userLog.Debug("starting init", zap.String("config-file", configFile))
maybeCheckNodeosVersion()
runProducer, err := askProducer()
if err != nil {
return err
}
if err := Init(runProducer, configFile); err != nil {
return err
}
if runProducer {
userLog.Printf("")
userLog.Printf("Here are the key pair controlling 'zswhq' to interact with your local chain:")
userLog.Printf("")
userLog.Printf(" Public Key: EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV")
userLog.Printf(" Private Key: 5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3")
} else {
userLog.Printf("")
userLog.Printf("IMPORANT: Move the remote network's 'genesis.json' file in the './mindreader' directory")
}
userLog.Printf("")
userLog.Printf("Initialization completed! To kickstart your environment, run: 'zswlishi start'")
return nil
}
func Init(runProducer bool, configFile string) error {
toRun := []string{"all", "-mindreader-stdin", "-merged-filter", "-accounthist", "-firehose"}
if !runProducer {
toRun = append(toRun, "-node-manager")
}
apps := launcher.ParseAppsFromArgs(toRun, func(s string) bool {
return true
})
conf := make(map[string]*launcher.DfuseCommandConfig)
conf["start"] = &launcher.DfuseCommandConfig{
Args: apps,
Flags: map[string]string{},
}
var err error
if runProducer {
userLog.Printf("")
// FIXME: would we create an `eosc-vault` ?
if err := os.MkdirAll("./producer", 0755); err != nil {
return fmt.Errorf("mkdir producer: %s", err)
}
userLog.Printf("Writing 'producer/config.ini'")
if err = ioutil.WriteFile("./producer/config.ini", []byte(producerLocalConfigIni), 0600); err != nil {
return fmt.Errorf("writing ./producer/config.ini file: %s", err)
}
userLog.Printf("Writing 'producer/genesis.json'")
if err = ioutil.WriteFile("./producer/genesis.json", []byte(localGenesisJSON), 0644); err != nil {
return fmt.Errorf("writing ./producer/genesis.json file: %s", err)
}
if err := os.MkdirAll("./mindreader", 0755); err != nil {
return fmt.Errorf("mkdir mindreader: %s", err)
}
userLog.Printf("Writing 'mindreader/config.ini'")
mindreaderConfig := fmt.Sprintf(mindreaderLocalConfigIniFormat, eosVMConfig())
if err = ioutil.WriteFile("./mindreader/config.ini", []byte(mindreaderConfig), 0644); err != nil {
return fmt.Errorf("writing mindreader/config.ini file: %s", err)
}
userLog.Printf("Writing 'mindreader/genesis.json'")
if err = ioutil.WriteFile("./mindreader/genesis.json", []byte(localGenesisJSON), 0644); err != nil {
return fmt.Errorf("writing ./mindreader/genesis.json file: %s", err)
}
} else {
peers, err := askPeers()
if err != nil {
return err
}
userLog.Printf("")
if err := os.MkdirAll("./mindreader", 0755); err != nil {
return fmt.Errorf("mkdir mindreader: %s", err)
}
userLog.Printf("Writing 'mindreader/config.ini'")
mindreaderConfig := fmt.Sprintf(mindreaderRemoteConfigIniFormat, eosVMConfig(), peersListConfigEntry(peers))
if err = ioutil.WriteFile("./mindreader/config.ini", []byte(mindreaderConfig), 0644); err != nil {
return fmt.Errorf("writing mindreader/config.ini file: %s", err)
}
}
configBytes, err := yaml.Marshal(conf)
if err != nil {
return err
}
userLog.Printf("Writing config '%s'", strings.TrimPrefix(configFile, "./"))
if err = ioutil.WriteFile(configFile, configBytes, 0644); err != nil {
return fmt.Errorf("writing config file %s: %w", configFile, err)
}
return nil
}
func askPeers() (peers []string, err error) {
peers = viper.GetStringSlice("peer")
if len(peers) == 0 {
for {
peer, err := askPeer(len(peers) == 0)
if err != nil {
return nil, fmt.Errorf("failed to request peers: %w", err)
}
if peer == "" {
break
}
peers = append(peers, peer)
}
}
return
}
func peersListConfigEntry(peers []string) string {
entries := make([]string, len(peers))
for i, peer := range peers {
entries[i] = fmt.Sprintf("p2p-peer-address = %s", peer)
}
return strings.Join(entries, "\n")
}
func askProducer() (bool, error) {
userLog.Printf(`ZSWLiShi can run a local test node configured for block production,
similar to what you use in development, with a clean blank chain and no contracts.
Alternatively, ZSWLiShi can connect to an already existing network.`)
userLog.Printf("")
prompt := promptui.Prompt{
Label: "Do you want ZSWLiShi to run a producing node for you",
IsConfirm: true,
}
result, err := prompt.Run()
if err != nil && err != promptui.ErrAbort {
return false, fmt.Errorf("unable to ask if we run producing node: %w", err)
}
return strings.ToLower(result) == "y", nil
}
func askPeer(first bool) (string, error) {
validate := func(input string) error {
//@TODO improve validation
if !first && input == "" {
return nil
}
if len(input) < 8 {
return errors.New("Invalid peer address")
}
return nil
}
defaultPeer := "127.0.0.1:9876"
label := "First peer to connect"
if !first {
label = "Add another peer? (leave blank to skip)"
defaultPeer = ""
}
prompt := promptui.Prompt{
Label: label,
Validate: validate,
Default: defaultPeer,
}
result, err := prompt.Run()
if err != nil {
return "", err
}
return result, nil
}
func eosVMConfig() string {
if runtime.GOOS == "darwin" {
return "# EOS VM is not supported on OS X platform"
}
return strings.Join([]string{
"wasm-runtime = eos-vm-jit",
"eos-vm-oc-enable = true",
"eos-vm-oc-compile-threads = 4",
}, "\n")
}