/
stage.go
314 lines (269 loc) · 9.14 KB
/
stage.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
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package executor
import (
"github.com/33cn/chain33/common"
dbm "github.com/33cn/chain33/common/db"
"github.com/golang/protobuf/proto"
"sort"
"github.com/33cn/chain33/types"
pt "github.com/33cn/plugin/plugin/dapp/paracross/types"
"github.com/pkg/errors"
)
func getStageID(db dbm.KV, id string) (*pt.SelfConsensStageInfo, error) {
realID := calcParaSelfConsensStageIDKey(id)
val, err := getDb(db, []byte(realID))
if err != nil {
return nil, err
}
var status pt.SelfConsensStageInfo
err = types.Decode(val, &status)
return &status, err
}
func makeStageConfigReceipt(prev, current *pt.SelfConsensStageInfo) *types.Receipt {
log := &pt.ReceiptSelfConsStageConfig{
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.TyLogParaSelfConsStageConfig,
Log: types.Encode(log),
},
},
}
}
func makeStageVoteDoneReceipt(config *pt.SelfConsensStage, totalCount, commitCount, most int, pass string) *types.Receipt {
log := &pt.ReceiptSelfConsStageVoteDone{
Stage: config,
TotalNodes: int32(totalCount),
TotalVote: int32(commitCount),
MostVote: int32(most),
VoteRst: pass,
}
return &types.Receipt{
Ty: types.ExecOk,
KV: nil,
Logs: []*types.ReceiptLog{
{
Ty: pt.TyLogParaNodeVoteDone,
Log: types.Encode(log),
},
},
}
}
func makeStageGroupReceipt(prev, current *pt.SelfConsensStages) *types.Receipt {
key := calcParaSelfConsStagesKey()
log := &pt.ReceiptSelfConsStagesUpdate{Prev: prev, Current: current}
return &types.Receipt{
Ty: types.ExecOk,
KV: []*types.KeyValue{
{Key: key, Value: types.Encode(current)},
},
Logs: []*types.ReceiptLog{
{
Ty: pt.TyLogParaStageGroupUpdate,
Log: types.Encode(log),
},
},
}
}
func getSelfConsensStages(db dbm.KV) (*pt.SelfConsensStages, error) {
key := calcParaSelfConsStagesKey()
item, err := db.Get(key)
if err != nil {
if isNotFound(err) {
err = pt.ErrKeyNotExist
}
return nil, errors.Wrapf(err, "getSelfConsensStages key:%s", string(key))
}
var config pt.SelfConsensStages
err = types.Decode(item, &config)
if err != nil {
return nil, errors.Wrap(err, "getSelfConsensStages decode config")
}
return &config, nil
}
func getSelfConsStagesMap(stages []*pt.SelfConsensStage) map[int64]uint32 {
stagesMap := make(map[int64]uint32)
for _, v := range stages {
stagesMap[v.StartHeight] = v.Enable
}
return stagesMap
}
func sortStages(stages *pt.SelfConsensStages, new *pt.SelfConsensStage) {
stages.Items = append(stages.Items, new)
sort.Slice(stages.Items, func(i, j int) bool { return stages.Items[i].StartHeight < stages.Items[j].StartHeight })
}
func updateStages(db dbm.KV, stage *pt.SelfConsensStage) (*types.Receipt, error) {
stages, err := getSelfConsensStages(db)
if err != nil && errors.Cause(err) != pt.ErrKeyNotExist {
return nil, err
}
if stages == nil {
stages = &pt.SelfConsensStages{}
stages.Items = append(stages.Items, stage)
return makeStageGroupReceipt(nil, stages), nil
}
old := proto.Clone(stages).(*pt.SelfConsensStages)
sortStages(stages, stage)
return makeStageGroupReceipt(old, stages), nil
}
func selfConsensInitStage(cfg *types.Chain33Config) *types.Receipt {
close := cfg.IsEnable(pt.ParaConsSubConf + "." + pt.ParaSelfConsInitConf)
stage := &pt.SelfConsensStage{StartHeight: 0, Enable: pt.ParaConfigYes}
if close {
stage.Enable = pt.ParaConfigNo
}
stages := &pt.SelfConsensStages{Items: []*pt.SelfConsensStage{stage}}
return makeStageGroupReceipt(nil, stages)
}
func getSelfConsOneStage(db dbm.KV, height int64) (*pt.SelfConsensStage, error) {
stages, err := getSelfConsensStages(db)
if err != nil {
return nil, err
}
for i := len(stages.Items) - 1; i >= 0; i-- {
if height >= stages.Items[i].StartHeight {
return stages.Items[i], nil
}
}
return nil, errors.Wrapf(pt.ErrKeyNotExist, "SelfConsStage not found to height:%d", height)
}
func isSelfConsOn(db dbm.KV, height int64) (bool, error) {
stage, err := getSelfConsOneStage(db, height)
if err != nil {
return false, err
}
return stage.Enable == pt.ParaConfigYes, nil
}
func (a *action) checkValidStage(config *pt.SelfConsensStage) error {
cfg := a.api.GetConfig()
//0. 设置高度必须大于fork高度
if !cfg.IsDappFork(config.StartHeight, pt.ParaX, pt.ForkParaSelfConsStages) {
return errors.Wrapf(types.ErrNotAllow, "checkValidStage config height:%d less than fork height", config.StartHeight)
}
//1. 设置高度必须大于当前区块高度
if config.StartHeight <= a.height {
return errors.Wrapf(pt.ErrHeightHasPast, "checkValidStage config height:%d less than block height:%d", config.StartHeight, a.height)
}
//2. 如果已经设置到stages中,简单起见,就不能更改了,应该也不会有很大影响
stages, err := getSelfConsensStages(a.db)
if err != nil && errors.Cause(err) != pt.ErrKeyNotExist {
return errors.Wrapf(err, "checkValidStage get stages")
}
if stages != nil {
stageMap := getSelfConsStagesMap(stages.Items)
if _, exist := stageMap[config.StartHeight]; exist {
return errors.Wrapf(err, "checkValidStage config height:%d existed", config.StartHeight)
}
}
//3. enable check
if config.Enable != pt.ParaConfigYes && config.Enable != pt.ParaConfigNo {
return errors.Wrapf(err, "checkValidStage config enable:%d not support", config.Enable)
}
return nil
}
func (a *action) stageApply(stage *pt.SelfConsensStage) (*types.Receipt, error) {
err := a.checkValidStage(stage)
if err != nil {
return nil, err
}
stat := &pt.SelfConsensStageInfo{
Id: calcParaSelfConsensStageIDKey(common.ToHex(a.txhash)),
Status: pt.ParaApplyJoining,
Stage: stage,
FromAddr: a.fromaddr,
ExecHeight: a.height}
return makeStageConfigReceipt(nil, stat), nil
}
func (a *action) stageCancel(config *pt.ConfigCancelInfo) (*types.Receipt, error) {
stat, err := getStageID(a.db, config.Id)
if err != nil {
return nil, err
}
//只能提案发起人撤销
if a.fromaddr != stat.FromAddr {
return nil, errors.Wrapf(types.ErrNotAllow, "stage id create by:%s,not by:%s", stat.FromAddr, a.fromaddr)
}
if stat.Status != pt.ParaApplyJoining && stat.Status != pt.ParaApplyVoting {
return nil, errors.Wrapf(pt.ErrParaNodeOpStatusWrong, "stage config id:%s,status:%d", config.Id, stat.Status)
}
copyStat := proto.Clone(stat).(*pt.SelfConsensStageInfo)
stat.Status = pt.ParaApplyCanceled
stat.ExecHeight = a.height
return makeStageConfigReceipt(copyStat, stat), nil
}
func (a *action) stageVote(config *pt.ConfigVoteInfo) (*types.Receipt, error) {
cfg := a.api.GetConfig()
nodes, _, err := getParacrossNodes(a.db, cfg.GetTitle())
if err != nil {
return nil, errors.Wrapf(err, "getNodes for title:%s", cfg.GetTitle())
}
if !validNode(a.fromaddr, nodes) {
return nil, errors.Wrapf(pt.ErrNodeNotForTheTitle, "not validNode:%s", a.fromaddr)
}
stat, err := getStageID(a.db, config.Id)
if err != nil {
return nil, errors.Wrapf(err, "stageVote getStageID:%s", config.Id)
}
if stat.Status != pt.ParaApplyJoining && stat.Status != pt.ParaApplyVoting {
return nil, errors.Wrapf(pt.ErrParaNodeOpStatusWrong, "config id:%s,status:%d", config.Id, stat.Status)
}
//stage blockHeight 也不能小于当前vote tx height,不然没有意义
err = a.checkValidStage(stat.Stage)
if err != nil {
return nil, err
}
copyStat := proto.Clone(stat).(*pt.SelfConsensStageInfo)
stat.Status = pt.ParaApplyVoting
if stat.Votes == nil {
stat.Votes = &pt.ParaNodeVoteDetail{}
}
found, index := hasVoted(stat.Votes.Addrs, a.fromaddr)
if found {
stat.Votes.Votes[index] = pt.ParaNodeVoteStr[config.Value]
} else {
stat.Votes.Addrs = append(stat.Votes.Addrs, a.fromaddr)
stat.Votes.Votes = append(stat.Votes.Votes, pt.ParaNodeVoteStr[config.Value])
}
//剔除已退出nodegroup的addr的投票
stat.Votes = updateVotes(stat.Votes, nodes)
most, vote := getMostVote(stat.Votes)
if !isCommitDone(len(nodes), most) {
return makeStageConfigReceipt(copyStat, stat), nil
}
clog.Info("paracross.stageVote ----pass", "most", most, "pass", vote)
receipt := &types.Receipt{Ty: types.ExecOk}
if vote == pt.ParaVoteYes {
r, err := updateStages(a.db, stat.Stage)
if err != nil {
return nil, errors.Wrapf(err, "stageVote updateStages")
}
receipt = mergeReceipt(receipt, r)
}
stat.Status = pt.ParaApplyClosed
stat.ExecHeight = a.height
r := makeStageConfigReceipt(copyStat, stat)
receipt = mergeReceipt(receipt, r)
r = makeStageVoteDoneReceipt(stat.Stage, len(nodes), len(stat.Votes.Addrs), most, pt.ParaNodeVoteStr[vote])
receipt = mergeReceipt(receipt, r)
return receipt, nil
}
//SelfConsensStageConfig support self consens stage config
func (a *action) SelfStageConfig(config *pt.ParaStageConfig) (*types.Receipt, error) {
if config.Ty == pt.ParaOpNewApply {
return a.stageApply(config.GetStage())
} else if config.Ty == pt.ParaOpCancel {
return a.stageCancel(config.GetCancel())
} else if config.Ty == pt.ParaOpVote {
return a.stageVote(config.GetVote())
}
return nil, pt.ErrParaUnSupportNodeOper
}