-
Notifications
You must be signed in to change notification settings - Fork 26
/
config.go
176 lines (141 loc) · 4.75 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
package conf
import (
"errors"
"net/url"
"os"
"strings"
thrown "github.com/0chain/errors"
"github.com/0chain/gosdk/core/sys"
"github.com/spf13/viper"
)
const (
// DefaultMinSubmit default value for min_submit
DefaultMinSubmit = 10
// DefaultMinConfirmation default value for min_confirmation
DefaultMinConfirmation = 10
// DefaultMaxTxnQuery default value for max_txn_query
DefaultMaxTxnQuery = 5
// DefaultConfirmationChainLength default value for confirmation_chain_length
DefaultConfirmationChainLength = 3
// DefaultQuerySleepTime default value for query_sleep_time
DefaultQuerySleepTime = 5
// DefaultSharderConsensous default consensous to take make SCRestAPI calls
DefaultSharderConsensous = 3
)
// Config settings from ~/.zcn/config.yaml
// block_worker: http://198.18.0.98:9091
// signature_scheme: bls0chain
// min_submit: 50
// min_confirmation: 50
// confirmation_chain_length: 3
// max_txn_query: 5
// query_sleep_time: 5
// # # OPTIONAL - Uncomment to use/ Add more if you want
// # preferred_blobbers:
// # - http://one.devnet-0chain.net:31051
// # - http://one.devnet-0chain.net:31052
// # - http://one.devnet-0chain.net:31053
type Config struct {
// BlockWorker the url of 0dns's network api
BlockWorker string `json:"block_worker,omitempty"`
// PreferredBlobbers preferred blobbers on new allocation
PreferredBlobbers []string `json:"preferred_blobbers,omitempty"`
// MinSubmit mininal submit from blobber
MinSubmit int `json:"min_submit,omitempty"`
// MinConfirmation mininal confirmation from sharders
MinConfirmation int `json:"min_confirmation,omitempty"`
// CconfirmationChainLength minial confirmation chain length
ConfirmationChainLength int `json:"confirmation_chain_length,omitempty"`
// additional settings depending network latency
// MaxTxnQuery maximum transcation query from sharders
MaxTxnQuery int `json:"max_txn_query,omitempty"`
// QuerySleepTime sleep time before transcation query
QuerySleepTime int `json:"query_sleep_time,omitempty"`
// SignatureScheme signature scheme
SignatureScheme string `json:"signature_scheme,omitempty"`
// ChainID which blockchain it is working
ChainID string `json:"chain_id,omitempty"`
VerifyOptimistic bool
// Ethereum node: "https://ropsten.infura.io/v3/xxxxxxxxxxxxxxx"
EthereumNode string `json:"ethereum_node,omitempty"`
// ZboxHost 0box api host host: "https://0box.dev.0chain.net"
ZboxHost string `json:"zbox_host"`
// ZboxAppType app type name
ZboxAppType string `json:"zbox_app_type"`
// SharderConsensous is consensous for when quering for SCRestAPI calls
SharderConsensous int `json:"sharder_consensous"`
}
// LoadConfigFile load and parse Config from file
func LoadConfigFile(file string) (Config, error) {
var cfg Config
var err error
_, err = sys.Files.Stat(file)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return cfg, thrown.Throw(ErrMssingConfig, file)
}
return cfg, err
}
v := viper.New()
v.SetConfigFile(file)
if err := v.ReadInConfig(); err != nil {
return cfg, thrown.Throw(ErrBadParsing, err.Error())
}
return LoadConfig(v)
}
// LoadConfig load and parse config
func LoadConfig(v Reader) (Config, error) {
var cfg Config
blockWorker := strings.TrimSpace(v.GetString("block_worker"))
if !isURL(blockWorker) {
return cfg, thrown.Throw(ErrInvalidValue, "block_worker="+blockWorker)
}
minSubmit := v.GetInt("min_submit")
if minSubmit < 1 {
minSubmit = DefaultMinSubmit
} else if minSubmit > 100 {
minSubmit = 100
}
minCfm := v.GetInt("min_confirmation")
if minCfm < 1 {
minCfm = DefaultMinConfirmation
} else if minCfm > 100 {
minCfm = 100
}
CfmChainLength := v.GetInt("confirmation_chain_length")
if CfmChainLength < 1 {
CfmChainLength = DefaultConfirmationChainLength
}
// additional settings depending network latency
maxTxnQuery := v.GetInt("max_txn_query")
if maxTxnQuery < 1 {
maxTxnQuery = DefaultMaxTxnQuery
}
querySleepTime := v.GetInt("query_sleep_time")
if querySleepTime < 1 {
querySleepTime = DefaultQuerySleepTime
}
VerifyOptimisticString := v.GetString("verify_optimistic")
if VerifyOptimisticString == "true" {
cfg.VerifyOptimistic = true
}
sharderConsensous := v.GetInt("sharder_consensous")
if sharderConsensous < 1 {
sharderConsensous = DefaultSharderConsensous
}
cfg.BlockWorker = blockWorker
cfg.PreferredBlobbers = v.GetStringSlice("preferred_blobbers")
cfg.MinSubmit = minSubmit
cfg.MinConfirmation = minCfm
cfg.ConfirmationChainLength = CfmChainLength
cfg.MaxTxnQuery = maxTxnQuery
cfg.QuerySleepTime = querySleepTime
cfg.SharderConsensous = sharderConsensous
cfg.SignatureScheme = v.GetString("signature_scheme")
cfg.ChainID = v.GetString("chain_id")
return cfg, nil
}
func isURL(s string) bool {
u, err := url.Parse(s)
return err == nil && u.Scheme != "" && u.Host != ""
}