/
supervisionaccount.go
478 lines (416 loc) · 14.6 KB
/
supervisionaccount.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
package executor
import (
"strings"
"github.com/33cn/chain33/common"
dbm "github.com/33cn/chain33/common/db"
"github.com/33cn/chain33/types"
pt "github.com/33cn/plugin/plugin/dapp/paracross/types"
"github.com/golang/protobuf/proto"
"github.com/pkg/errors"
)
func makeParaSupervisionNodeGroupReceipt(title string, prev, current *types.ConfigItem) *types.Receipt {
key := calcParaSupervisionNodeGroupAddrsKey(title)
log := &types.ReceiptConfig{Prev: prev, Current: current}
return &types.Receipt{
Ty: types.ExecOk,
KV: []*types.KeyValue{
{Key: key, Value: types.Encode(current)},
},
Logs: []*types.ReceiptLog{
{
Ty: pt.TyLogParaSupervisionNodeGroupAddrsUpdate,
Log: types.Encode(log),
},
},
}
}
func makeSupervisionNodeConfigReceipt(fromAddr string, config *pt.ParaNodeGroupConfig, prev, current *pt.ParaNodeGroupStatus) *types.Receipt {
log := &pt.ReceiptParaNodeGroupConfig{
Addr: fromAddr,
Config: config,
Prev: prev,
Current: current,
}
return &types.Receipt{
Ty: types.ExecOk,
KV: []*types.KeyValue{
{Key: []byte(current.Id), Value: types.Encode(current)},
},
Logs: []*types.ReceiptLog{
{
Ty: pt.TyLogParaSupervisionNodeConfig,
Log: types.Encode(log),
},
},
}
}
func makeParaSupervisionNodeStatusReceipt(fromAddr string, prev, current *pt.ParaNodeAddrIdStatus) *types.Receipt {
key := calcParaNodeAddrKey(current.Title, current.Addr)
log := &pt.ReceiptParaNodeAddrStatUpdate{
FromAddr: fromAddr,
Prev: prev,
Current: current,
}
return &types.Receipt{
Ty: types.ExecOk,
KV: []*types.KeyValue{
{Key: key, Value: types.Encode(current)},
},
Logs: []*types.ReceiptLog{
{
Ty: pt.TyLogParaSupervisionNodeStatusUpdate,
Log: types.Encode(log),
},
},
}
}
func getSupervisionNodeID(db dbm.KV, id string) (*pt.ParaNodeGroupStatus, error) {
val, err := getDb(db, []byte(id))
if err != nil {
return nil, err
}
var status pt.ParaNodeGroupStatus
err = types.Decode(val, &status)
return &status, err
}
func (a *action) updateSupervisionNodeGroup(title, addr string, add bool) (*types.Receipt, error) {
var item types.ConfigItem
key := calcParaSupervisionNodeGroupAddrsKey(title)
value, err := a.db.Get(key)
if err != nil {
return nil, err
}
if value != nil {
err = types.Decode(value, &item)
if err != nil {
clog.Error("updateSupervisionNodeGroup", "decode db key", key)
return nil, err
}
}
copyValue := *item.GetArr()
copyItem := item
copyItem.Value = &types.ConfigItem_Arr{Arr: ©Value}
receipt := &types.Receipt{Ty: types.ExecOk}
item.Addr = addr
if add {
item.GetArr().Value = append(item.GetArr().Value, addr)
clog.Info("updateSupervisionNodeGroup add", "addr", addr)
} else {
item.GetArr().Value = make([]string, 0)
for _, value := range copyItem.GetArr().Value {
if value != addr {
item.GetArr().Value = append(item.GetArr().Value, value)
}
}
clog.Info("updateSupervisionNodeGroup delete", "addr", addr)
}
err = a.db.Set(key, types.Encode(&item))
if err != nil {
return nil, errors.Wrapf(err, "updateNodeGroup set dbkey=%s", key)
}
r := makeParaSupervisionNodeGroupReceipt(title, ©Item, &item)
receipt = mergeReceipt(receipt, r)
return receipt, nil
}
func (a *action) checkValidSupervisionNode(config *pt.ParaNodeGroupConfig) (bool, error) {
key := calcParaSupervisionNodeGroupAddrsKey(config.Title)
nodes, _, err := getNodes(a.db, key)
if err != nil && !(isNotFound(err) || errors.Cause(err) == pt.ErrTitleNotExist) {
return false, errors.Wrapf(err, "getNodes for title:%s", config.Title)
}
if validNode(config.Addrs, nodes) {
return true, nil
}
return false, nil
}
func (a *action) checkSupervisionNodeGroupExist(title string) (bool, error) {
key := calcParaSupervisionNodeGroupAddrsKey(title)
value, err := a.db.Get(key)
if err != nil && !isNotFound(err) {
return false, err
}
if value != nil {
var item types.ConfigItem
err = types.Decode(value, &item)
if err != nil {
clog.Error("updateSupervisionNodeGroup", "decode db key", key)
return false, err
}
return true, nil
}
return false, nil
}
func (a *action) supervisionNodeGroupCreate(title, targetAddrs string) (*types.Receipt, error) {
var item types.ConfigItem
key := calcParaSupervisionNodeGroupAddrsKey(title)
item.Key = string(key)
emptyValue := &types.ArrayConfig{Value: make([]string, 0)}
arr := types.ConfigItem_Arr{Arr: emptyValue}
item.Value = &arr
item.GetArr().Value = append(item.GetArr().Value, targetAddrs)
item.Addr = a.fromaddr
receipt := makeParaSupervisionNodeGroupReceipt(title, nil, &item)
return receipt, nil
}
//由于propasal id 和quit id分开,quit id不知道对应addr proposal id的coinfrozen信息,需要维护一个围绕addr的数据库结构信息
func (a *action) updateSupervisionNodeAddrStatus(stat *pt.ParaNodeGroupStatus) (*types.Receipt, error) {
addrStat, err := getNodeAddr(a.db, stat.Title, stat.TargetAddrs)
if err != nil {
if !isNotFound(err) {
return nil, errors.Wrapf(err, "nodeAddr:%s get error", stat.TargetAddrs)
}
addrStat = &pt.ParaNodeAddrIdStatus{}
addrStat.Title = stat.Title
addrStat.Addr = stat.TargetAddrs
addrStat.BlsPubKey = stat.BlsPubKeys
addrStat.Status = pt.ParacrossSupervisionNodeApprove
addrStat.ProposalId = stat.Id
addrStat.QuitId = ""
return makeParaSupervisionNodeStatusReceipt(a.fromaddr, nil, addrStat), nil
}
preStat := *addrStat
if stat.Status == pt.ParacrossSupervisionNodeQuit {
proposalStat, err := getSupervisionNodeID(a.db, addrStat.ProposalId)
if err != nil {
return nil, errors.Wrapf(err, "nodeAddr:%s quiting wrong proposeid:%s", stat.TargetAddrs, addrStat.ProposalId)
}
addrStat.Status = stat.Status
addrStat.QuitId = stat.Id
receipt := makeParaSupervisionNodeStatusReceipt(a.fromaddr, &preStat, addrStat)
cfg := a.api.GetConfig()
if !cfg.IsPara() {
r, err := a.nodeGroupCoinsActive(proposalStat.FromAddr, proposalStat.CoinsFrozen, 1)
if err != nil {
return nil, err
}
receipt = mergeReceipt(receipt, r)
}
return receipt, nil
} else {
addrStat.Status = stat.Status
addrStat.ProposalId = stat.Id
addrStat.QuitId = ""
return makeParaSupervisionNodeStatusReceipt(a.fromaddr, &preStat, addrStat), nil
}
}
func (a *action) supervisionNodeApply(config *pt.ParaNodeGroupConfig) (*types.Receipt, error) {
// 必须要有授权节点 监督节点才有意义 判断是否存在授权节点
if strings.Contains(config.Addrs, ",") {
return nil, errors.Wrapf(types.ErrInvalidParam, "not support multi addr currently,addrs=%s", config.Addrs)
}
nodeCfg := &pt.ParaNodeAddrConfig{Title: config.Title, Addr: config.Addrs}
addrExist, err := a.checkValidNode(nodeCfg)
if err != nil {
return nil, err
}
// 不能跟授权节点一致
if addrExist {
return nil, errors.Wrapf(pt.ErrParaNodeAddrExisted, "supervisionNodeGroup Apply Addr existed:%s in super group", config.Addrs)
}
// 判断 node 是否已经申请
addrExist, err = a.checkValidSupervisionNode(config)
if err != nil {
return nil, err
}
if addrExist {
return nil, errors.Wrapf(pt.ErrParaSupervisionNodeAddrExisted, "supervisionNodeGroup Apply Addr existed:%s", config.Addrs)
}
// 在主链上冻结金额
receipt := &types.Receipt{Ty: types.ExecOk}
cfg := a.api.GetConfig()
if !cfg.IsPara() {
r, err := a.nodeGroupCoinsFrozen(a.fromaddr, config.CoinsFrozen, 1)
if err != nil {
return nil, err
}
receipt = mergeReceipt(receipt, r)
}
stat := &pt.ParaNodeGroupStatus{
Id: calcParaSupervisionNodeIDKey(config.Title, common.ToHex(a.txhash)),
Status: pt.ParacrossSupervisionNodeApply,
Title: config.Title,
TargetAddrs: config.Addrs,
BlsPubKeys: config.BlsPubKeys,
CoinsFrozen: config.CoinsFrozen,
FromAddr: a.fromaddr,
Height: a.height,
}
r := makeSupervisionNodeConfigReceipt(a.fromaddr, config, nil, stat)
receipt = mergeReceipt(receipt, r)
return receipt, nil
}
func (a *action) supervisionNodeApprove(config *pt.ParaNodeGroupConfig) (*types.Receipt, error) {
cfg := a.api.GetConfig()
apply, err := getSupervisionNodeID(a.db, calcParaSupervisionNodeIDKey(config.Title, config.Id))
if err != nil {
return nil, err
}
if config.Title != apply.Title {
return nil, errors.Wrapf(pt.ErrNodeNotForTheTitle, "config title:%s,id title:%s", config.Title, apply.Title)
}
if apply.CoinsFrozen < config.CoinsFrozen {
return nil, errors.Wrapf(pt.ErrParaNodeGroupFrozenCoinsNotEnough, "id not enough coins apply:%d,config:%d", apply.CoinsFrozen, config.CoinsFrozen)
}
//只在主链检查, 主链检查失败不会同步到平行链,主链成功,平行链默认成功
if !cfg.IsPara() {
err := a.checkApproveOp(config, apply)
if err != nil {
return nil, err
}
}
// 判断监督账户组是否已经存在
exist, err := a.checkSupervisionNodeGroupExist(config.Title)
if err != nil {
return nil, err
}
receipt := &types.Receipt{Ty: types.ExecOk}
if !exist {
// 监督账户组不存在
r, err := a.supervisionNodeGroupCreate(apply.Title, apply.TargetAddrs)
if err != nil {
return nil, errors.Wrapf(err, "nodegroup create:title:%s,addrs:%s", config.Title, apply.TargetAddrs)
}
receipt = mergeReceipt(receipt, r)
} else {
// 监督账户组已经存在
r, err := a.updateSupervisionNodeGroup(config.Title, apply.TargetAddrs, true)
if err != nil {
return nil, err
}
receipt = mergeReceipt(receipt, r)
}
copyStat := proto.Clone(apply).(*pt.ParaNodeGroupStatus)
apply.Status = pt.ParacrossSupervisionNodeApprove
apply.Height = a.height
r, err := a.updateSupervisionNodeAddrStatus(apply)
if err != nil {
return nil, err
}
receipt = mergeReceipt(receipt, r)
r = makeSupervisionNodeConfigReceipt(a.fromaddr, config, copyStat, apply)
receipt = mergeReceipt(receipt, r)
return receipt, nil
}
func (a *action) supervisionNodeQuit(config *pt.ParaNodeGroupConfig) (*types.Receipt, error) {
addrExist, err := a.checkValidSupervisionNode(config)
if err != nil {
return nil, err
}
if !addrExist {
return nil, errors.Wrapf(pt.ErrParaSupervisionNodeAddrNotExisted, "nodeAddr not existed:%s", config.Addrs)
}
status, err := getNodeAddr(a.db, config.Title, config.Addrs)
if err != nil {
return nil, errors.Wrapf(err, "nodeAddr:%s get error", config.Addrs)
}
if status.Status != pt.ParacrossSupervisionNodeApprove {
return nil, errors.Wrapf(pt.ErrParaSupervisionNodeAddrNotExisted, "nodeAddr:%s status:%d", config.Addrs, status.Status)
}
cfg := a.api.GetConfig()
stat := &pt.ParaNodeGroupStatus{
Id: calcParaSupervisionNodeIDKey(config.Title, common.ToHex(a.txhash)),
Status: pt.ParacrossSupervisionNodeQuit,
Title: config.Title,
TargetAddrs: config.Addrs,
FromAddr: a.fromaddr,
Height: a.height,
}
//只能提案发起人或超级节点可以撤销
if a.fromaddr != status.Addr && !cfg.IsPara() && !isSuperManager(cfg, a.fromaddr) {
return nil, errors.Wrapf(types.ErrNotAllow, "id create by:%s,not by:%s", status.Addr, a.fromaddr)
}
if config.Title != status.Title {
return nil, errors.Wrapf(pt.ErrNodeNotForTheTitle, "config title:%s,id title:%s", config.Title, status.Title)
}
receipt := &types.Receipt{Ty: types.ExecOk}
// node quit后,如果committx满足2/3目标,自动触发commitDone 后期增加
r, err := a.loopCommitTxDone(config.Title)
if err != nil {
clog.Error("updateSupervisionNodeGroup.loopCommitTxDone", "title", config.Title, "err", err.Error())
}
receipt = mergeReceipt(receipt, r)
r, err = a.updateSupervisionNodeGroup(config.Title, stat.TargetAddrs, false)
if err != nil {
return nil, err
}
receipt = mergeReceipt(receipt, r)
r, err = a.updateSupervisionNodeAddrStatus(stat)
if err != nil {
return nil, err
}
receipt = mergeReceipt(receipt, r)
r = makeSupervisionNodeConfigReceipt(a.fromaddr, config, nil, stat)
receipt = mergeReceipt(receipt, r)
return receipt, nil
}
func (a *action) supervisionNodeCancel(config *pt.ParaNodeGroupConfig) (*types.Receipt, error) {
cfg := a.api.GetConfig()
status, err := getSupervisionNodeID(a.db, calcParaSupervisionNodeIDKey(config.Title, config.Id))
if err != nil {
return nil, errors.Wrapf(err, "getSupervisionNodeID=%s", config.Id)
}
//只能提案发起人可以撤销
if a.fromaddr != status.FromAddr {
return nil, errors.Wrapf(types.ErrNotAllow, "id create by:%s,not by:%s", status.FromAddr, a.fromaddr)
}
if config.Title != status.Title {
return nil, errors.Wrapf(pt.ErrNodeNotForTheTitle, "config title:%s,id title:%s", config.Title, status.Title)
}
if status.Status != pt.ParacrossSupervisionNodeApply {
return nil, errors.Wrapf(pt.ErrParaNodeOpStatusWrong, "config id:%s,status:%d", config.Id, status.Status)
}
receipt := &types.Receipt{Ty: types.ExecOk}
//main chain
if !cfg.IsPara() {
r, err := a.nodeGroupCoinsActive(status.FromAddr, status.CoinsFrozen, 1)
if err != nil {
return nil, err
}
receipt = mergeReceipt(receipt, r)
}
copyStat := proto.Clone(status).(*pt.ParaNodeGroupStatus)
status.Status = pt.ParacrossSupervisionNodeCancel
status.Height = a.height
r := makeSupervisionNodeConfigReceipt(a.fromaddr, config, copyStat, status)
receipt = mergeReceipt(receipt, r)
return receipt, nil
}
func (a *action) supervisionNodeModify(config *pt.ParaNodeGroupConfig) (*types.Receipt, error) {
addrStat, err := getNodeAddr(a.db, config.Title, config.Addrs)
if err != nil {
return nil, errors.Wrapf(err, "nodeAddr:%s get error", config.Addrs)
}
// 只能提案发起人
if a.fromaddr != config.Addrs {
return nil, errors.Wrapf(types.ErrNotAllow, "addr create by:%s,not by:%s", config.Addrs, a.fromaddr)
}
preStat := *addrStat
addrStat.BlsPubKey = config.BlsPubKeys
return makeParaSupervisionNodeStatusReceipt(a.fromaddr, &preStat, addrStat), nil
}
func (a *action) SupervisionNodeConfig(config *pt.ParaNodeGroupConfig) (*types.Receipt, error) {
cfg := a.api.GetConfig()
if !validTitle(cfg, config.Title) {
return nil, pt.ErrInvalidTitle
}
if !types.IsParaExecName(string(a.tx.Execer)) {
return nil, errors.Wrapf(types.ErrInvalidParam, "exec=%s,should prefix with user.p.", string(a.tx.Execer))
}
if (config.Op == pt.ParacrossSupervisionNodeApprove || config.Op == pt.ParacrossSupervisionNodeCancel) && config.Id == "" {
return nil, types.ErrInvalidParam
}
if config.Op == pt.ParacrossSupervisionNodeApply {
return a.supervisionNodeApply(config)
} else if config.Op == pt.ParacrossSupervisionNodeApprove {
return a.supervisionNodeApprove(config)
} else if config.Op == pt.ParacrossSupervisionNodeQuit {
// 退出 group
return a.supervisionNodeQuit(config)
} else if config.Op == pt.ParacrossSupervisionNodeCancel {
// 撤销未批准的申请
return a.supervisionNodeCancel(config)
} else if config.Op == pt.ParacrossSupervisionNodeModify {
return a.supervisionNodeModify(config)
}
return nil, pt.ErrParaUnSupportNodeOper
}