/
common.go
259 lines (243 loc) · 6.68 KB
/
common.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
// Copyright (c) 2018 The MATRIX Authors
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php
package support
import (
"fmt"
"github.com/MatrixAINetwork/go-matrix/common"
"github.com/MatrixAINetwork/go-matrix/log"
"github.com/MatrixAINetwork/go-matrix/mc"
)
func DelIndex(native AllNative, flag int) AllNative {
ans := AllNative{
Master: native.Master,
BackUp: native.BackUp,
Candidate: native.Candidate,
ElectInfo: native.ElectInfo,
}
switch flag {
case 1:
for k, v := range native.MasterQ {
if k == 0 {
continue
}
ans.MasterQ = append(ans.MasterQ, v)
}
ans.BackUpQ = append(ans.BackUpQ, native.BackUpQ[0:]...)
ans.CandidateQ = append(ans.CandidateQ, native.CandidateQ[0:]...)
return ans
case 2:
for k, v := range native.BackUpQ {
if k == 0 {
continue
}
ans.BackUpQ = append(ans.BackUpQ, v)
}
ans.MasterQ = append(ans.MasterQ, native.MasterQ[0:]...)
ans.CandidateQ = append(ans.CandidateQ, native.CandidateQ[0:]...)
return ans
case 3:
for k, v := range native.CandidateQ {
if k == 0 {
continue
}
ans.CandidateQ = append(ans.CandidateQ, v)
}
ans.MasterQ = append(ans.MasterQ, native.MasterQ[0:]...)
ans.BackUpQ = append(ans.BackUpQ, native.BackUpQ[0:]...)
return ans
default:
return ans
}
}
func GetNodeFromBuff(native AllNative) (AllNative, common.Address) {
if len(native.MasterQ) > 0 {
return DelIndex(native, 1), native.MasterQ[0]
}
if len(native.BackUpQ) > 0 {
return DelIndex(native, 2), native.BackUpQ[0]
}
if len(native.CandidateQ) > 0 {
return DelIndex(native, 3), native.CandidateQ[0]
}
return native, common.Address{}
}
func CalcGradeDependAddr(addr common.Address, native AllNative) int {
for _, v := range native.Master {
if v.Account.Equal(addr) {
return 3
}
}
for _, v := range native.BackUp {
if v.Account.Equal(addr) {
return 2
}
}
for _, v := range native.Candidate {
if v.Account.Equal(addr) {
return 1
}
}
return 0
}
func BackUpdata(top *mc.TopologyGraph, mapp map[uint16]common.Address, native AllNative) uint16 {
ChoseStatus := -1
AimPosition := uint16(0)
for _, v := range top.NodeList {
types := common.GetRoleTypeFromPosition(v.Position)
if _, ok := mapp[v.Position]; ok == false {
continue
}
if types != common.RoleBackupValidator {
continue
}
AddrGrade := CalcGradeDependAddr(v.Account, native)
// fmt.Println("AddrGrade",AddrGrade,"v.Account",v.Account.String())
if AddrGrade > ChoseStatus {
AimPosition = v.Position
ChoseStatus = AddrGrade
}
}
//fmt.Println("AimPositon",AimPosition)
return AimPosition
}
func KInTop(aim uint16, topoG *mc.TopologyGraph) bool {
for _, v := range topoG.NodeList {
if v.Position == aim {
return true
}
}
return false
}
func ToPoUpdate(allNative AllNative, topoG *mc.TopologyGraph) []mc.Alternative {
ans := []mc.Alternative{}
mapMaster := make(map[uint16]common.Address)
mapBackup := make(map[uint16]common.Address)
//fmt.Println("len topG.NodeList",len(topoG.NodeList))
//for _,v:=range topoG.NodeList{
// fmt.Println(v.Position,v.Account.String())
//}
for _, v := range topoG.NodeList {
//fmt.Println("v.Pos",v.Position,"v.addr",v.Account.String())
types := common.GetRoleTypeFromPosition(v.Position)
if types == common.RoleValidator {
mapMaster[v.Position] = v.Account
}
if types == common.RoleBackupValidator {
mapBackup[v.Position] = v.Account
}
}
//fmt.Println("mapMaster",mapMaster,"len",len(mapMaster))
//fmt.Println("mapBackup",mapBackup,"len",len(mapBackup))
for index := 0; index < int(allNative.ElectInfo.ValidatorNum); index++ { //用一级在线去补
k := common.GeneratePosition(uint16(index), common.ElectRoleValidator)
_, ok := mapMaster[k]
if ok == true {
// fmt.Println("该位置已存在",k)
continue
}
trans := BackUpdata(topoG, mapBackup, allNative)
if trans == 0 {
//fmt.Println("已空")
continue
}
if _, ok := mapBackup[trans]; ok == false {
// fmt.Println("mapBack没有",trans)
continue
}
ans = append(ans, mc.Alternative{
A: mapBackup[trans],
Position: k,
})
// fmt.Println("一级在线去补",mapBackup[trans].String(),k)
mapMaster[k] = mapBackup[trans]
delete(mapBackup, trans)
}
for index := 0; index < int(allNative.ElectInfo.ValidatorNum); index++ { //用buff去补
k := common.GeneratePosition(uint16(index), common.ElectRoleValidator)
_, ok := mapMaster[k]
if ok == true {
continue
}
var addr common.Address
allNative, addr = GetNodeFromBuff(allNative)
if addr.Equal(common.Address{}) {
continue
}
ans = append(ans, mc.Alternative{
A: addr,
Position: k,
})
mapMaster[k] = addr
// fmt.Println("用buff去补",addr,k)
}
for index := 0; index < int(allNative.ElectInfo.BackValidator); index++ {
k := common.GeneratePosition(uint16(index), common.ElectRoleValidatorBackUp)
_, ok := mapBackup[k]
if ok == true {
continue
}
var addr common.Address
allNative, addr = GetNodeFromBuff(allNative)
if addr.Equal(common.Address{}) {
continue
}
ans = append(ans, mc.Alternative{
A: addr,
Position: k,
})
mapBackup[k] = addr
}
for index := 0; index < int(allNative.ElectInfo.ValidatorNum); index++ { //算一级下线
k := common.GeneratePosition(uint16(index), common.ElectRoleValidator)
if KInTop(k, topoG) == false {
fmt.Println("一级 该点不在顶层内", "不处理")
continue
}
if _, ok := mapMaster[k]; ok == false {
//fmt.Println("该店直接下线-一级")
ans = append(ans, mc.Alternative{
A: common.Address{},
Position: k,
})
}
}
for index := 0; index < int(allNative.ElectInfo.BackValidator); index++ { //算二级下线
k := common.GeneratePosition(uint16(index), common.ElectRoleValidatorBackUp)
//fmt.Println("222222222---k",k)
if KInTop(k, topoG) == false {
log.Trace(ModuleLogName, "二级 该点不在顶层内", "不处理")
continue
}
if _, ok := mapBackup[k]; ok == false {
// fmt.Println("该店直接下线-二级")
ans = append(ans, mc.Alternative{
A: common.Address{},
Position: k,
})
}
}
return ans
}
func PrimarylistUpdate(Q0, Q1, Q2 []mc.TopologyNodeInfo, online mc.TopologyNodeInfo, flag int) ([]mc.TopologyNodeInfo, []mc.TopologyNodeInfo, []mc.TopologyNodeInfo) {
log.Info("Elector PrimarylistUpdate")
if flag == 0 {
var tQ0 []mc.TopologyNodeInfo
tQ0 = append(tQ0, online)
tQ0 = append(tQ0, Q0...)
Q0 = tQ0
}
if flag == 1 {
var tQ1 []mc.TopologyNodeInfo
tQ1 = append(tQ1, Q1...)
tQ1 = append(tQ1, online)
Q1 = tQ1
}
if flag == 2 {
var tQ2 []mc.TopologyNodeInfo
tQ2 = append(tQ2, Q2...)
tQ2 = append(tQ2, online)
Q2 = tQ2
}
return Q0, Q1, Q2
}