/
type.go
262 lines (221 loc) · 6.28 KB
/
type.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
// Copyright 2021 The Alaya Network Authors
// This file is part of the Alaya-Go library.
//
// The Alaya-Go library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The Alaya-Go library 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Alaya-Go library. If not, see <http://www.gnu.org/licenses/>.
package cbfttypes
import (
"bytes"
"crypto/ecdsa"
"encoding/json"
"errors"
"fmt"
"github.com/AlayaNetwork/Alaya-Go/common/hexutil"
"math"
"math/big"
"sort"
"github.com/AlayaNetwork/Alaya-Go/consensus/cbft/protocols"
"github.com/AlayaNetwork/Alaya-Go/consensus/cbft/utils"
"github.com/AlayaNetwork/Alaya-Go/common"
"github.com/AlayaNetwork/Alaya-Go/core/types"
"github.com/AlayaNetwork/Alaya-Go/crypto/bls"
"github.com/AlayaNetwork/Alaya-Go/p2p/discover"
)
// Block's Signature info
type BlockSignature struct {
SignHash common.Hash // Signature hash,header[0:32]
Hash common.Hash // Block hash,header[:]
Number *big.Int
Signature *common.BlockConfirmSign
}
func (bs *BlockSignature) Copy() *BlockSignature {
sign := *bs.Signature
return &BlockSignature{
SignHash: bs.SignHash,
Hash: bs.Hash,
Number: new(big.Int).Set(bs.Number),
Signature: &sign,
}
}
type UpdateChainStateFn func(qcState, lockState, commitState *protocols.State)
type CbftResult struct {
Block *types.Block
ExtraData []byte
SyncState chan error
ChainStateUpdateCB func()
}
type ProducerState struct {
count int
miner common.Address
}
func (ps *ProducerState) Add(miner common.Address) {
if ps.miner == miner {
ps.count++
} else {
ps.miner = miner
ps.count = 1
}
}
func (ps *ProducerState) Get() (common.Address, int) {
return ps.miner, ps.count
}
func (ps *ProducerState) Validate(period int) bool {
return ps.count < period
}
type AddValidatorEvent struct {
NodeID discover.NodeID
}
type RemoveValidatorEvent struct {
NodeID discover.NodeID
}
type UpdateValidatorEvent struct{}
type ValidateNode struct {
Index uint32 `json:"index"`
Address common.NodeAddress `json:"address"`
PubKey *ecdsa.PublicKey `json:"-"`
NodeID discover.NodeID `json:"nodeID"`
BlsPubKey *bls.PublicKey `json:"blsPubKey"`
}
type ValidateNodeMap map[discover.NodeID]*ValidateNode
type SortedValidatorNode []*ValidateNode
func (sv SortedValidatorNode) Len() int { return len(sv) }
func (sv SortedValidatorNode) Swap(i, j int) { sv[i], sv[j] = sv[j], sv[i] }
func (sv SortedValidatorNode) Less(i, j int) bool { return sv[i].Index < sv[j].Index }
type Validators struct {
Nodes ValidateNodeMap `json:"validateNodes"`
ValidBlockNumber uint64 `json:"validateBlockNumber"`
sortedNodes SortedValidatorNode
}
func (vn *ValidateNode) String() string {
b, _ := json.Marshal(vn)
return string(b)
}
func (vn *ValidateNode) Verify(data, sign []byte) error {
var sig bls.Sign
err := sig.Deserialize(sign)
if err != nil {
return err
}
if !sig.Verify(vn.BlsPubKey, string(data)) {
return errors.New(fmt.Sprintf("bls verifies signature fail, data:%s, sign:%s, pubkey:%s", hexutil.Encode(data), hexutil.Encode(sign), hexutil.Encode(vn.BlsPubKey.Serialize())))
}
return nil
}
func (vnm ValidateNodeMap) String() string {
s := ""
for k, v := range vnm {
s = s + fmt.Sprintf("{%s:%s},", k, v)
}
return s
}
func (vs *Validators) String() string {
b, _ := json.Marshal(vs)
return string(b)
}
func (vs *Validators) NodeList() []discover.NodeID {
nodeList := make([]discover.NodeID, 0)
for id, _ := range vs.Nodes {
nodeList = append(nodeList, id)
}
return nodeList
}
func (vs *Validators) NodeListByIndexes(indexes []uint32) ([]*ValidateNode, error) {
if len(vs.sortedNodes) == 0 {
vs.sort()
}
l := make([]*ValidateNode, 0)
for _, index := range indexes {
if int(index) >= len(vs.sortedNodes) {
return nil, errors.New("invalid index")
}
l = append(l, vs.sortedNodes[int(index)])
}
return l, nil
}
func (vs *Validators) NodeListByBitArray(vSet *utils.BitArray) ([]*ValidateNode, error) {
if len(vs.sortedNodes) == 0 {
vs.sort()
}
l := make([]*ValidateNode, 0)
for index := uint32(0); index < vSet.Size(); index++ {
if vSet.GetIndex(index) {
if int(index) >= len(vs.sortedNodes) {
return nil, errors.New("invalid index")
}
l = append(l, vs.sortedNodes[int(index)])
}
}
return l, nil
}
func (vs *Validators) FindNodeByID(id discover.NodeID) (*ValidateNode, error) {
node, ok := vs.Nodes[id]
if ok {
return node, nil
}
return nil, errors.New("not found the node")
}
func (vs *Validators) FindNodeByIndex(index int) (*ValidateNode, error) {
if len(vs.sortedNodes) == 0 {
vs.sort()
}
if index >= len(vs.sortedNodes) {
return nil, errors.New("not found the specified validator")
} else {
return vs.sortedNodes[index], nil
}
}
func (vs *Validators) FindNodeByAddress(addr common.NodeAddress) (*ValidateNode, error) {
for _, node := range vs.Nodes {
if bytes.Equal(node.Address[:], addr[:]) {
return node, nil
}
}
return nil, errors.New("invalid address")
}
func (vs *Validators) NodeID(idx int) discover.NodeID {
if len(vs.sortedNodes) == 0 {
vs.sort()
}
if idx >= vs.sortedNodes.Len() {
return discover.NodeID{}
}
return vs.sortedNodes[idx].NodeID
}
func (vs *Validators) Index(nodeID discover.NodeID) (uint32, error) {
if node, ok := vs.Nodes[nodeID]; ok {
return node.Index, nil
}
return math.MaxUint32, errors.New("not found the specified validator")
}
func (vs *Validators) Len() int {
return len(vs.Nodes)
}
func (vs *Validators) Equal(rsh *Validators) bool {
if vs.Len() != rsh.Len() {
return false
}
equal := true
for k, v := range vs.Nodes {
if vv, ok := rsh.Nodes[k]; !ok || vv.Index != v.Index {
equal = false
break
}
}
return equal
}
func (vs *Validators) sort() {
for _, node := range vs.Nodes {
vs.sortedNodes = append(vs.sortedNodes, node)
}
sort.Sort(vs.sortedNodes)
}