forked from iotexproject/iotex-core
/
endorsementmanager.go
229 lines (197 loc) · 5.71 KB
/
endorsementmanager.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
// Copyright (c) 2018 IoTeX
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
package rolldpos
import (
"encoding/hex"
"time"
"github.com/pkg/errors"
"go.uber.org/zap"
"github.com/iotexproject/iotex-core/blockchain/block"
"github.com/iotexproject/iotex-core/endorsement"
)
var (
// ErrExpiredEndorsement indicates that the endorsement is expired
ErrExpiredEndorsement = errors.New("the endorsement has been replaced or expired")
)
type endorserEndorsementCollection struct {
endorsements map[ConsensusVoteTopic]*endorsement.Endorsement
}
func newEndorserEndorsementCollection() *endorserEndorsementCollection {
return &endorserEndorsementCollection{
endorsements: map[ConsensusVoteTopic]*endorsement.Endorsement{},
}
}
func (ee *endorserEndorsementCollection) AddEndorsement(
topic ConsensusVoteTopic,
en *endorsement.Endorsement,
) error {
if e, exists := ee.endorsements[topic]; exists {
if e.Timestamp().After(en.Timestamp()) {
return ErrExpiredEndorsement
}
}
ee.endorsements[topic] = en
return nil
}
func (ee *endorserEndorsementCollection) Cleanup(timestamp time.Time) *endorserEndorsementCollection {
cleaned := newEndorserEndorsementCollection()
if e, exists := ee.endorsements[PROPOSAL]; exists {
if !e.Timestamp().Before(timestamp) {
cleaned.endorsements[PROPOSAL] = e
}
}
if e, exists := ee.endorsements[LOCK]; exists {
if !e.Timestamp().Before(timestamp) {
cleaned.endorsements[LOCK] = e
}
}
if e, exists := ee.endorsements[COMMIT]; exists {
cleaned.endorsements[COMMIT] = e
}
return cleaned
}
func (ee *endorserEndorsementCollection) Endorsement(
topic ConsensusVoteTopic,
) *endorsement.Endorsement {
return ee.endorsements[topic]
}
type blockEndorsementCollection struct {
blk *block.Block
endorsers map[string]*endorserEndorsementCollection
}
func newBlockEndorsementCollection(blk *block.Block) *blockEndorsementCollection {
return &blockEndorsementCollection{
blk: blk,
endorsers: map[string]*endorserEndorsementCollection{},
}
}
func (bc *blockEndorsementCollection) SetBlock(blk *block.Block) error {
bc.blk = blk
return nil
}
func (bc *blockEndorsementCollection) Block() *block.Block {
return bc.blk
}
func (bc *blockEndorsementCollection) AddEndorsement(
topic ConsensusVoteTopic,
en *endorsement.Endorsement,
) error {
endorser := en.Endorser().HexString()
ee, exists := bc.endorsers[endorser]
if !exists {
ee = newEndorserEndorsementCollection()
}
if err := ee.AddEndorsement(topic, en); err != nil {
return err
}
bc.endorsers[endorser] = ee
return nil
}
func (bc *blockEndorsementCollection) Cleanup(timestamp time.Time) *blockEndorsementCollection {
cleaned := newBlockEndorsementCollection(bc.blk)
for endorser, ee := range bc.endorsers {
cleaned.endorsers[endorser] = ee.Cleanup(timestamp)
}
return cleaned
}
func (bc *blockEndorsementCollection) Endorsements(
topics []ConsensusVoteTopic,
) []*endorsement.Endorsement {
endorsements := []*endorsement.Endorsement{}
for _, ee := range bc.endorsers {
for _, topic := range topics {
if en := ee.Endorsement(topic); en != nil {
endorsements = append(endorsements, en)
break
}
}
}
return endorsements
}
type endorsementManager struct {
collections map[string]*blockEndorsementCollection
}
func newEndorsementManager() *endorsementManager {
return &endorsementManager{
collections: map[string]*blockEndorsementCollection{},
}
}
func (m *endorsementManager) CollectionByBlockHash(blkHash []byte) *blockEndorsementCollection {
encodedBlockHash := encodeToString(blkHash)
c, exists := m.collections[encodedBlockHash]
if !exists {
return nil
}
return c
}
func (m *endorsementManager) Size() int {
return len(m.collections)
}
func (m *endorsementManager) SizeWithBlock() int {
size := 0
for _, c := range m.collections {
if c.Block() != nil {
size++
}
}
return size
}
func (m *endorsementManager) RegisterBlock(blk *block.Block) error {
blkHash := blk.HashBlock()
encodedBlockHash := encodeToString(blkHash[:])
if c, exists := m.collections[encodedBlockHash]; exists {
return c.SetBlock(blk)
}
m.collections[encodedBlockHash] = newBlockEndorsementCollection(blk)
return nil
}
func (m *endorsementManager) AddVoteEndorsement(
vote *ConsensusVote,
en *endorsement.Endorsement,
) error {
encoded := encodeToString(vote.BlockHash())
c, exists := m.collections[encoded]
if !exists {
c = newBlockEndorsementCollection(nil)
}
if err := c.AddEndorsement(vote.Topic(), en); err != nil {
return err
}
m.collections[encoded] = c
return nil
}
func (m *endorsementManager) Cleanup(timestamp time.Time) *endorsementManager {
cleaned := newEndorsementManager()
for encoded, c := range m.collections {
cleaned.collections[encoded] = c.Cleanup(timestamp)
}
return cleaned
}
func (m *endorsementManager) Log(
logger *zap.Logger,
delegates []string,
) *zap.Logger {
for encoded, c := range m.collections {
proposalEndorsements := c.Endorsements(
[]ConsensusVoteTopic{PROPOSAL},
)
lockEndorsements := c.Endorsements(
[]ConsensusVoteTopic{LOCK},
)
commitEndorsments := c.Endorsements(
[]ConsensusVoteTopic{COMMIT},
)
return logger.With(
zap.Int("numProposals:"+encoded, len(proposalEndorsements)),
zap.Int("numLocks:"+encoded, len(lockEndorsements)),
zap.Int("numCommits:"+encoded, len(commitEndorsments)),
)
}
return logger
}
func encodeToString(hash []byte) string {
return hex.EncodeToString(hash)
}