/
config.go
executable file
·223 lines (190 loc) · 5.87 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
/*
This file is part of go-palletone.
go-palletone is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
go-palletone is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with go-palletone. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* @author PalletOne core developer Albert·Gou <dev@pallet.one>
* @date 2018
*/
package mediatorplugin
import (
"encoding/json"
"os"
"github.com/palletone/go-palletone/common"
"github.com/palletone/go-palletone/common/log"
"github.com/palletone/go-palletone/core"
"go.dedis.ch/kyber/v3"
"gopkg.in/urfave/cli.v1"
)
const (
DefaultPassword = "1"
DefaultInitPrivKey = "3ouzX3aTwrJM6PaqJrvE9XEWiycKuFgnZcfrW7duMEFu"
DefaultRequiredParticipation = 33
)
var (
MediatorFlags = []cli.Flag{
NoProduceUnitFlag,
StaleProductionFlag,
ConsecutiveProductionFlag,
RequiredParticipationFlag,
NoGroupSignFlag,
IdledProducingFlag,
MediatorsFlag,
}
NoProduceUnitFlag = cli.BoolFlag{
Name: "noProduce",
Usage: "Disable producing unit when start up node.",
}
StaleProductionFlag = cli.BoolFlag{
Name: "staleProduce",
Usage: "Enable unit production(only once), even if the chain is stale.",
}
ConsecutiveProductionFlag = cli.BoolFlag{
Name: "allowConsecutive",
Usage: "Enable unit production(only once), even if the last unit was generated by the same mediator.",
}
RequiredParticipationFlag = cli.UintFlag{
Name: "requiredParticipation",
Usage: "Percent of mediators (0-99) that must be participating in order to produce units.",
//Value: DefaultRequiredParticipation,
}
NoGroupSignFlag = cli.BoolFlag{
Name: "noGroupSign",
Usage: "Disable group-signing in this node.",
}
MediatorsFlag = cli.StringSliceFlag{
Name: "mediators",
Usage: "the mediator account controlled by this node, may specify multiple times. for example:\n" +
"{\\\"Address\\\":\\\"P1xx\\\",\\\"Password\\\":\\\"xxx\\\",\\\"InitPrivKey\\\":\\\"xxx\\\"," +
"\\\"InitPubKey\\\":\\\"xxx\\\"}",
}
IdledProducingFlag = cli.BoolFlag{
Name: "idledProducing",
Usage: "Enable producing unit, even if we have no pending transactions.",
}
)
// config data for mediator plugin
type Config struct {
// the set of mediator accounts controlled by this node
Mediators []*MediatorConf
// Percent of mediators (0-99) that must be participating in order to produce uints
RequiredParticipation uint32
// 主程序启动时,是否立即开启unit生产
EnableProducing bool
// Enable Unit production(only once), even if the chain is stale. 运行本节点开始生产unit,即使数据不是最新的
EnableStaleProduction bool
// Enable Unit production(only once), even if the last unit was generated by the same mediator.
// 允许本节点的mediator可以连续生产unit
EnableConsecutiveProduction bool
// 标记本节点是否开启群签名的功能
EnableGroupSigning bool
// 标记本节点是否生产无交易的区块
EnableIdledProduction bool
}
func DefaultMediatorConf() *MediatorConf {
return &MediatorConf{
core.DefaultMediator,
DefaultPassword,
DefaultInitPrivKey,
core.DefaultInitPubKey,
}
}
// mediator plugin default config
var DefaultConfig = Config{
EnableProducing: true,
EnableStaleProduction: false,
EnableConsecutiveProduction: false,
RequiredParticipation: DefaultRequiredParticipation,
EnableGroupSigning: true,
EnableIdledProduction: true,
Mediators: []*MediatorConf{
DefaultMediatorConf(),
},
}
func MakeConfig() Config {
cfg := DefaultConfig
cfg.Mediators = nil
return cfg
}
func SetMediatorConfig(ctx *cli.Context, cfg *Config) {
if ctx.GlobalIsSet(NoProduceUnitFlag.Name) {
cfg.EnableProducing = false
}
if ctx.GlobalIsSet(StaleProductionFlag.Name) {
cfg.EnableStaleProduction = true
}
if ctx.GlobalIsSet(ConsecutiveProductionFlag.Name) {
cfg.EnableConsecutiveProduction = true
}
if ctx.GlobalIsSet(RequiredParticipationFlag.Name) {
cfg.RequiredParticipation = uint32(ctx.GlobalUint(RequiredParticipationFlag.Name))
}
if ctx.GlobalIsSet(NoGroupSignFlag.Name) {
cfg.EnableGroupSigning = false
}
if ctx.GlobalIsSet(IdledProducingFlag.Name) {
cfg.EnableIdledProduction = true
}
if ctx.GlobalIsSet(MediatorsFlag.Name) {
mjs := ctx.GlobalStringSlice(MediatorsFlag.Name)
//log.Debugf("%v", mjs)
for _, mj := range mjs {
//log.Debugf("%v", mj)
mc := new(MediatorConf)
err := json.Unmarshal([]byte(mj), mc)
if err != nil {
log.Errorf("%v", err)
os.Exit(1)
}
//log.Debugf("%v", mc)
cfg.Mediators = append(cfg.Mediators, mc)
}
}
}
type MediatorConf struct {
Address string //`json:"account" toml:"account"`
Password string //`json:"password" toml:"password"`
InitPrivKey string //`json:"initPrivKey" toml:"initPrivKey"`
InitPubKey string //`json:"initPubKey" toml:"initPubKey"`
}
func (medConf *MediatorConf) configToAccount() *MediatorAccount {
// 1. 解析 mediator 账户地址
addr, err := core.StrToMedAdd(medConf.Address)
if err != nil {
log.Debugf(err.Error())
return nil
}
// 2. 解析 mediator 的 DKS 初始公私钥
sec, err := core.StrToScalar(medConf.InitPrivKey)
if err != nil {
log.Debugf(err.Error())
return nil
}
pub, err := core.StrToPoint(medConf.InitPubKey)
if err != nil {
log.Debugf(err.Error())
return nil
}
medAcc := &MediatorAccount{
addr,
medConf.Password,
sec,
pub,
}
return medAcc
}
type MediatorAccount struct {
Address common.Address
Password string
InitPrivKey kyber.Scalar
InitPubKey kyber.Point
}