-
Notifications
You must be signed in to change notification settings - Fork 20
/
root.go
217 lines (185 loc) · 5.84 KB
/
root.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
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sync"
"github.com/0chain/gosdk/core/conf"
"github.com/0chain/gosdk/core/logger"
"github.com/spf13/cobra"
"github.com/0chain/gosdk/zboxcore/blockchain"
"github.com/0chain/zboxcli/util"
"github.com/0chain/gosdk/core/zcncrypto"
"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/0chain/gosdk/zcncore"
)
var cfgFile string
var networkFile string
var walletFile string
var walletClientID string
var walletClientKey string
var cDir string
var bSilent bool
var allocUnderRepair bool
var walletJSON string
var rootCmd = &cobra.Command{
Use: "zbox",
Short: "zbox is a decentralized storage application written on the 0Chain platform",
Long: `zbox is a decentralized storage application written on the 0Chain platform.
Complete documentation is available at https://docs.0chain.net/0chain/`,
}
var clientWallet *zcncrypto.Wallet
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is config.yaml)")
rootCmd.PersistentFlags().StringVar(&networkFile, "network", "", "network file to overwrite the network details (if required, default is network.yaml)")
rootCmd.PersistentFlags().StringVar(&walletFile, "wallet", "", "wallet file (default is wallet.json)")
rootCmd.PersistentFlags().StringVar(&walletClientID, "wallet_client_id", "", "wallet client_id")
rootCmd.PersistentFlags().StringVar(&walletClientKey, "wallet_client_key", "", "wallet client_key")
rootCmd.PersistentFlags().StringVar(&cDir, "configDir", "", "configuration directory (default is $HOME/.zcn)")
rootCmd.PersistentFlags().BoolVar(&bSilent, "silent", false, "Do not show interactive sdk logs (shown by default)")
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func initConfig() {
var configDir string
if cDir != "" {
configDir = cDir
} else {
configDir = util.GetConfigDir()
}
if cfgFile == "" {
cfgFile = "config.yaml"
}
cfg, err := conf.LoadConfigFile(filepath.Join(configDir, cfgFile))
if err != nil {
fmt.Println("Can't read config:", err)
os.Exit(1)
}
if networkFile == "" {
networkFile = "network.yaml"
}
network, _ := conf.LoadNetworkFile(filepath.Join(configDir, networkFile))
// syncing loggers
logger.SyncLoggers([]*logger.Logger{zcncore.GetLogger(), sdk.GetLogger()})
// set the log file
zcncore.SetLogFile("cmdlog.log", !bSilent)
sdk.SetLogFile("cmdlog.log", !bSilent)
if network.IsValid() {
zcncore.SetNetwork(network.Miners, network.Sharders)
conf.InitChainNetwork(&conf.Network{
Miners: network.Miners,
Sharders: network.Sharders,
})
}
err = zcncore.InitZCNSDK(cfg.BlockWorker, cfg.SignatureScheme,
zcncore.WithChainID(cfg.ChainID),
zcncore.WithMinSubmit(cfg.MinSubmit),
zcncore.WithMinConfirmation(cfg.MinConfirmation),
zcncore.WithConfirmationChainLength(cfg.ConfirmationChainLength))
if err != nil {
fmt.Println("Error initializing core SDK.", err)
os.Exit(1)
}
// is freshly created wallet?
var fresh bool
wallet := &zcncrypto.Wallet{}
if (&walletClientID != nil) && (len(walletClientID) > 0) && (&walletClientKey != nil) && (len(walletClientKey) > 0) {
wallet.ClientID = walletClientID
wallet.ClientKey = walletClientKey
var clientBytes []byte
clientBytes, err = json.Marshal(wallet)
walletJSON = string(clientBytes)
if err != nil {
fmt.Println("Invalid wallet data passed:" + walletClientID + " " + walletClientKey)
os.Exit(1)
}
clientWallet = wallet
fresh = false
} else {
var walletFilePath string
if &walletFile != nil && len(walletFile) > 0 {
if filepath.IsAbs(walletFile) {
walletFilePath = walletFile
} else {
walletFilePath = configDir + string(os.PathSeparator) + walletFile
}
} else {
walletFilePath = configDir + string(os.PathSeparator) + "wallet.json"
}
if _, err = os.Stat(walletFilePath); os.IsNotExist(err) {
wg := &sync.WaitGroup{}
statusBar := &ZCNStatus{wg: wg}
wg.Add(1)
err = zcncore.CreateWallet(statusBar)
if err == nil {
wg.Wait()
} else {
fmt.Println(err.Error())
os.Exit(1)
}
if len(statusBar.walletString) == 0 || !statusBar.success {
fmt.Println("Error creating the wallet." + statusBar.errMsg)
os.Exit(1)
}
fmt.Println("ZCN wallet created")
walletJSON = string(statusBar.walletString)
file, err := os.Create(walletFilePath)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
defer file.Close()
fmt.Fprintf(file, walletJSON)
fresh = true
} else {
f, err := os.Open(walletFilePath)
if err != nil {
fmt.Println("Error opening the wallet", err)
os.Exit(1)
}
clientBytes, err := ioutil.ReadAll(f)
if err != nil {
fmt.Println("Error reading the wallet", err)
os.Exit(1)
}
walletJSON = string(clientBytes)
}
//minerjson, _ := json.Marshal(miners)
//sharderjson, _ := json.Marshal(sharders)
err = json.Unmarshal([]byte(walletJSON), wallet)
clientWallet = wallet
if err != nil {
fmt.Println("Invalid wallet at path:" + walletFilePath)
os.Exit(1)
}
}
//init the storage sdk with the known miners, sharders and client wallet info
err = sdk.InitStorageSDK(walletJSON, cfg.BlockWorker, cfg.ChainID, cfg.SignatureScheme, cfg.PreferredBlobbers)
if err != nil {
fmt.Println("Error in sdk init", err)
os.Exit(1)
}
// additional settings depending network latency
blockchain.SetMaxTxnQuery(cfg.MaxTxnQuery)
blockchain.SetQuerySleepTime(cfg.QuerySleepTime)
conf.InitClientConfig(&cfg)
if network.IsValid() {
sdk.SetNetwork(network.Miners, network.Sharders)
}
sdk.SetNumBlockDownloads(10)
if fresh {
fmt.Println("Creating related read pool for storage smart-contract...")
if err = sdk.CreateReadPool(); err != nil {
fmt.Printf("Failed to create read pool: %v\n", err)
os.Exit(1)
}
fmt.Println("Read pool created successfully")
}
}