/
repo.go
executable file
·233 lines (198 loc) · 5.96 KB
/
repo.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
package repo
import (
"fmt"
"io/ioutil"
"path/filepath"
"reflect"
"github.com/Knetic/govaluate"
"github.com/ethereum/go-ethereum/event"
libp2pcert "github.com/meshplus/go-libp2p-cert"
"github.com/spf13/viper"
)
type Repo struct {
Config *Config
NetworkConfig *NetworkConfig
Key *Key
Certs *libp2pcert.Certs
ConfigChangeFeed event.Feed
}
func (r *Repo) SubscribeConfigChange(ch chan *Repo) event.Subscription {
return r.ConfigChangeFeed.Subscribe(ch)
}
func Load(repoRoot string, passwd string, configPath string, networkPath string) (*Repo, error) {
bViper := viper.New()
nViper := viper.New()
config, err := UnmarshalConfig(bViper, repoRoot, configPath)
if err != nil {
return nil, fmt.Errorf("unmarshal bitxhub config error: %w", err)
}
if err := checkConfig(config); err != nil {
return nil, fmt.Errorf("check bitxhub config failed: %w", err)
}
var networkConfig *NetworkConfig
if len(networkPath) == 0 {
networkConfig, err = loadNetworkConfig(nViper, repoRoot, config.Genesis)
} else {
fileData, err := ioutil.ReadFile(networkPath)
if err != nil {
return nil, fmt.Errorf("read network config error: %w", err)
}
err = ioutil.WriteFile(filepath.Join(repoRoot, "network.toml"), fileData, 0644)
if err != nil {
return nil, fmt.Errorf("write network config failed: %w", err)
}
networkDir := filepath.Dir(networkPath)
networkConfig, err = loadNetworkConfig(nViper, networkDir, config.Genesis)
}
if err != nil {
return nil, fmt.Errorf("load network config: %w", err)
}
certs, err := libp2pcert.LoadCerts(repoRoot, config.NodeCertPath, config.AgencyCertPath, config.CACertPath)
if err != nil {
return nil, fmt.Errorf("load certs failed: %w", err)
}
key, err := loadPrivKey(repoRoot, passwd)
if err != nil {
return nil, fmt.Errorf("load private key: %w", err)
}
repo := &Repo{
Config: config,
NetworkConfig: networkConfig,
Key: key,
Certs: certs,
}
// watch bitxhub.toml on changed
WatchBitxhubConfig(bViper, &repo.ConfigChangeFeed)
// watch network.toml on changed
WatchNetworkConfig(nViper, &repo.ConfigChangeFeed)
return repo, nil
}
func checkConfig(config *Config) error {
// check genesis admin info:
// - There is at least one super administrator.
hasSuperAdmin := false
for _, admin := range config.Genesis.Admins {
if admin.Weight == SuperAdminWeight {
hasSuperAdmin = true
} else if admin.Weight != NormalAdminWeight {
return fmt.Errorf("Illegal admin weight in genesis config!")
}
}
if !hasSuperAdmin {
return fmt.Errorf("Set up at least one super administrator in genesis config!")
}
// check strategy
for _, s := range config.Genesis.Strategy {
if err := CheckStrategyInfo(s.Typ, s.Module, s.Extra, len(config.Genesis.Admins)); err != nil {
return err
}
}
return nil
}
func CheckStrategyInfo(typ, module string, extra string, adminsNum int) error {
if err := CheckStrategyType(typ, extra, adminsNum); err != nil {
return fmt.Errorf("illegal proposal strategy type:%s, err: %v", typ, err)
}
if CheckManageModule(module) != nil {
return fmt.Errorf("illegal proposal strategy module:%s", module)
}
return nil
}
func CheckStrategyType(typ string, extra string, adminsNum int) error {
if typ != SimpleMajority &&
typ != ZeroPermission {
return fmt.Errorf("illegal proposal strategy type")
}
if typ != ZeroPermission {
err := CheckStrategyExpression(extra, adminsNum)
if err != nil {
return err
}
}
return nil
}
func CheckStrategyExpression(expressionStr string, adminsNum int) error {
expression, err := govaluate.NewEvaluableExpression(expressionStr)
if err != nil {
return fmt.Errorf("illegal strategy expression: %w", err)
}
parameters := make(map[string]interface{}, 8)
parameters["r"] = 0
parameters["t"] = adminsNum
for i := 0; i <= adminsNum; i++ {
parameters["a"] = i
result, err := expression.Evaluate(parameters)
if err != nil {
return fmt.Errorf("illegal strategy expression: %w", err)
}
if result.(bool) {
return nil
} else {
continue
}
}
return fmt.Errorf("illegal strategy expression: under this exp(%s), the proposal may never be concluded", expressionStr)
}
// return:
// - whether the proposal is over, if not, you need to wait for the vote
// - whether the proposal is pass, that is, it has ended, may be passed or rejected
// - error
func MakeStrategyDecision(expressionStr string, approve, reject, total, avaliableNum uint64) (bool, bool, error) {
expression, err := govaluate.NewEvaluableExpression(expressionStr)
if err != nil {
return false, false, err
}
parameters := make(map[string]interface{}, 8)
parameters["a"] = approve
parameters["r"] = reject
parameters["t"] = total
result, err := expression.Evaluate(parameters)
if err != nil {
return false, false, err
}
if reflect.TypeOf(result).String() != "bool" {
return false, false, fmt.Errorf("illegal expressionStr: %s", expressionStr)
}
if result.(bool) {
return true, true, nil
}
parameters["a"] = avaliableNum - reject
result, err = expression.Evaluate(parameters)
if err != nil {
return false, false, err
}
if result.(bool) {
return false, false, nil
} else {
return true, false, nil
}
}
func CheckManageModule(moduleTyp string) error {
if moduleTyp != AppchainMgr &&
moduleTyp != RoleMgr &&
moduleTyp != RuleMgr &&
moduleTyp != DappMgr &&
moduleTyp != ProposalStrategyMgr &&
moduleTyp != NodeMgr &&
moduleTyp != ServiceMgr {
return fmt.Errorf("illegal manage module type")
}
return nil
}
func GetAPI(repoRoot string) (string, error) {
data, err := ioutil.ReadFile(filepath.Join(repoRoot, APIName))
if err != nil {
return "", fmt.Errorf("read %s error: %w", filepath.Join(repoRoot, APIName), err)
}
return string(data), nil
}
func GetKeyPath(repoRoot string) string {
return filepath.Join(repoRoot, KeyName)
}
func GetStoragePath(repoRoot string, subPath ...string) string {
p := filepath.Join(repoRoot, "storage")
for _, s := range subPath {
p = filepath.Join(p, s)
}
return p
}