/
params.go
276 lines (251 loc) · 6.68 KB
/
params.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
package cluster
import (
"encoding/hex"
"errors"
"fmt"
"log"
"strconv"
"sync"
"time"
"github.com/ofgp/ofgp-core/crypto"
pb "github.com/ofgp/ofgp-core/proto"
"github.com/ofgp/ofgp-core/util/assert"
"github.com/spf13/viper"
)
var (
ClusterSize int
TotalNodeCount int
QuorumN int
AccuseQuorumN int
MaxFaultyN int
NodeList []NodeInfo
ClusterSnapshot *Snapshot //节点列表快照
DbCache = 16
DbFileHandles = 16
NodeSigners map[int32]*crypto.SecureSigner
MultiSigSnapshot MultiSigInfos
CurrMultiSig MultiSigInfo
initNodeHeight int64 // 引导节点监听到的高度
)
type MultiSigInfos struct {
sync.Mutex
SigInfos []MultiSigInfo
}
func (msi MultiSigInfos) GetMultiSigInfos() []MultiSigInfo {
return msi.SigInfos
}
func (msi MultiSigInfos) GetLatestSigInfo() (MultiSigInfo, error) {
msi.Lock()
defer msi.Unlock()
size := len(msi.SigInfos)
if size == 0 {
return MultiSigInfo{}, errors.New("multisigs empty")
}
return msi.SigInfos[size-1], nil
}
const (
// BlockInterval 产出区块的频率
BlockInterval = 10 * time.Second
)
// Init 初始化集群信息
func Init() {
n := viper.GetInt("KEYSTORE.count")
localPubkeyHash := viper.GetString("KEYSTORE.local_pubkey_hash")
localId := viper.GetInt("DGW.local_id")
NodeSigners = make(map[int32]*crypto.SecureSigner, n)
NodeList = nil
for i := 0; i < n; i++ {
hash := ""
if i == localId {
hash = localPubkeyHash
}
key := "KEYSTORE.key_" + strconv.Itoa(i)
pubkey := viper.GetString(key)
NodeSigners[int32(i)] = crypto.NewSecureSigner(pubkey, hash)
NodeList = append(NodeList, NodeInfo{
Id: int32(i),
Name: fmt.Sprintf("server%d", i),
Url: viper.GetString("DGW.host_" + strconv.Itoa(i)),
PublicKey: NodeSigners[int32(i)].Pubkey,
IsNormal: viper.GetBool("DGW.status_" + strconv.Itoa(i)),
})
}
ClusterSize = n
TotalNodeCount = n
MaxFaultyN = (ClusterSize - 1) / 3
QuorumN = ClusterSize - MaxFaultyN
AccuseQuorumN = MaxFaultyN + 1
}
// InitWithNodeList 根据参数而非配置文件来构建集群信息
func InitWithNodeList(nodeList *pb.NodeList) {
NodeSigners = make(map[int32]*crypto.SecureSigner)
for i, node := range nodeList.NodeList {
assert.True(int32(i) == node.NodeId)
NodeSigners[int32(i)] = crypto.NewSecureSigner(node.Pubkey, "")
NodeList = append(NodeList, NodeInfo{
Id: node.NodeId,
Name: node.Name,
Url: node.Host,
PublicKey: NodeSigners[int32(i)].Pubkey,
IsNormal: node.IsNormal,
})
if node.IsNormal {
ClusterSize++
}
}
TotalNodeCount = len(nodeList.NodeList)
MaxFaultyN = (ClusterSize - 1) / 3
QuorumN = ClusterSize - MaxFaultyN
AccuseQuorumN = MaxFaultyN + 1
}
// LeaderNodeOfTerm 根据指定的term计算leader的ID并返回
func LeaderNodeOfTerm(term int64) int32 {
idx := int32(term % int64(TotalNodeCount))
for {
if NodeList[idx].IsNormal {
return idx
}
idx = int32((idx + 1) % int32(TotalNodeCount))
}
}
// AddNode 增加节点到集群信息列表
func AddNode(host string, nodeId int32, pubkey string, pubkeyHash string) {
if nodeId != int32(TotalNodeCount) {
return
}
signer := crypto.NewSecureSigner(pubkey, pubkeyHash)
NodeList = append(NodeList, NodeInfo{
Id: nodeId,
Name: fmt.Sprintf("server%d", nodeId),
Url: host,
PublicKey: signer.Pubkey,
IsNormal: true,
})
NodeSigners[nodeId] = signer
ClusterSize++
TotalNodeCount++
MaxFaultyN = (ClusterSize - 1) / 3
QuorumN = ClusterSize - MaxFaultyN
AccuseQuorumN = MaxFaultyN + 1
}
// AddNodeInfo 将node加入节点列表
func AddNodeInfo(nodeInfo NodeInfo) {
NodeList = append(NodeList, nodeInfo)
ClusterSize++
TotalNodeCount++
MaxFaultyN = (ClusterSize - 1) / 3
QuorumN = (ClusterSize+MaxFaultyN)/2 + 1
AccuseQuorumN = MaxFaultyN + 1
}
// IsNodeExist node 是否已经存在
func IsNodeExist(nodeId int32) bool {
for _, node := range NodeList {
if node.Id == nodeId {
log.Println("node is already exist")
return true
}
}
return false
}
// NewNodeInfo 创建Node
func NewNodeInfo(host string, nodeId int32, pubkey string, pubkeyHash string) NodeInfo {
if nodeId != int32(TotalNodeCount) {
log.Println("nodeId is not equal totalNodeCnt")
return NodeInfo{}
}
signer := crypto.NewSecureSigner(pubkey, pubkeyHash)
nodeInfo := NodeInfo{
Id: nodeId,
Name: fmt.Sprintf("server%d", nodeId),
Url: host,
PublicKey: signer.Pubkey,
IsNormal: true,
}
NodeSigners[nodeId] = signer
return nodeInfo
}
// DeleteNode 把相应ID的节点的状态标记为删除
func DeleteNode(nodeId int32) {
if nodeId >= int32(TotalNodeCount) || nodeId < 0 {
return
}
if NodeList[nodeId].IsNormal {
NodeList[nodeId].IsNormal = false
ClusterSize--
MaxFaultyN = (ClusterSize - 1) / 3
QuorumN = ClusterSize - MaxFaultyN
AccuseQuorumN = MaxFaultyN + 1
}
}
// RecoverNode 恢复节点的状态为正常
func RecoverNode(nodeId int32) {
if nodeId >= int32(TotalNodeCount) || nodeId < 0 {
return
}
NodeList[nodeId].IsNormal = true
ClusterSize++
MaxFaultyN = (ClusterSize - 1) / 3
QuorumN = ClusterSize - MaxFaultyN
AccuseQuorumN = MaxFaultyN + 1
DelSnapShot()
}
// GetPubkeyList 获取所有正常状态节点的公钥
func GetPubkeyList() []string {
var rst []string
for _, nodeInfo := range NodeList {
if nodeInfo.IsNormal {
rst = append(rst, hex.EncodeToString(nodeInfo.PublicKey))
}
}
return rst
}
// AddMultiSigInfo 保存当前多签地址到快照列表
func AddMultiSigInfo(multiSig MultiSigInfo) {
MultiSigSnapshot.Lock()
defer MultiSigSnapshot.Unlock()
sigSnapshot := MultiSigSnapshot.SigInfos
if len(sigSnapshot) > 0 && sigSnapshot[len(sigSnapshot)-1].Equal(multiSig) {
return
}
MultiSigSnapshot.SigInfos = append(sigSnapshot, multiSig)
}
// SetMultiSigSnapshot 设置快照
func SetMultiSigSnapshot(snapshot []MultiSigInfo) {
MultiSigSnapshot.SigInfos = snapshot
}
// SetCurrMultiSig 设置当前的多签信息
func SetCurrMultiSig(multiSig MultiSigInfo) {
CurrMultiSig = multiSig
}
// CreateSnapShot 创建快照保存到变量 ClusterSnapshot
func CreateSnapShot() *Snapshot {
nodes := make([]NodeInfo, len(NodeList))
copy(nodes, NodeList)
ClusterSnapshot = &Snapshot{
NodeList: nodes,
ClusterSize: ClusterSize,
TotalNodeCount: TotalNodeCount,
QuorumN: QuorumN,
AccuseQuorumN: AccuseQuorumN,
}
return ClusterSnapshot
}
func GetSnapshot() Snapshot {
snapshot := Snapshot{
NodeList: NodeList,
ClusterSize: ClusterSize,
TotalNodeCount: TotalNodeCount,
QuorumN: QuorumN,
AccuseQuorumN: AccuseQuorumN,
}
return snapshot
}
func DelSnapShot() {
ClusterSnapshot = nil
}
func SetInitNodeHeight(height int64) {
initNodeHeight = height
}
func GetInitNodeHeight() int64 {
return initNodeHeight
}