-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
268 lines (233 loc) · 6.85 KB
/
state.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
/*
Copyright (C) THL A29 Limited, a Tencent company. All rights reserved.
SPDX-License-Identifier: Apache-2.0
*/
package tbft
import (
"sync"
consensuspb "chainmaker.org/chainmaker/pb-go/v2/consensus"
tbftpb "chainmaker.org/chainmaker/pb-go/v2/consensus/tbft"
"chainmaker.org/chainmaker/protocol/v2"
)
// ConsensusState represents the consensus state of the node
type ConsensusState struct {
logger protocol.Logger
// node id
Id string
// current height
Height uint64
// current round
Round int32
// current step
Step tbftpb.Step
// proposal
Proposal *TBFTProposal
// verifing proposal
VerifingProposal *TBFTProposal
LockedRound int32
// locked proposal
LockedProposal *tbftpb.Proposal
ValidRound int32
// valid proposal
ValidProposal *tbftpb.Proposal
heightRoundVoteSet *heightRoundVoteSet
}
// ConsensusFutureMsg represents the consensus msg of future
//
type ConsensusFutureMsg struct {
Proposal map[int32]*tbftpb.Proposal
heightRoundVoteSet *heightRoundVoteSet
}
// ConsensusFutureMsgCache cache future consensus msg
//
type ConsensusFutureMsgCache struct {
logger protocol.Logger
size uint64
consensusHeight uint64
cache map[uint64]*ConsensusFutureMsg
}
//
// ConsensusFutureMsgCache
// @Description: Create a new future Msg cache
// @param size
// @return *ConsensusFutureMsgCache
//
func newConsensusFutureMsgCache(logger protocol.Logger, size, height uint64) *ConsensusFutureMsgCache {
return &ConsensusFutureMsgCache{
logger: logger,
size: size,
consensusHeight: height,
cache: make(map[uint64]*ConsensusFutureMsg, size),
}
}
// NewConsensusFutureMsg creates a new future msg instance
func NewConsensusFutureMsg(logger protocol.Logger, height uint64, round int32,
validators *validatorSet) *ConsensusFutureMsg {
cs := &ConsensusFutureMsg{Proposal: make(map[int32]*tbftpb.Proposal)}
cs.heightRoundVoteSet = newHeightRoundVoteSet(logger, height, round, validators)
return cs
}
// updateConsensusHeight update height of consensus
func (cfc *ConsensusFutureMsgCache) updateConsensusHeight(height uint64) {
cfc.consensusHeight = height
// delete the cache
cfc.gc()
}
// addFutureProposal add future proposal in cache
func (cfc *ConsensusFutureMsgCache) addFutureProposal(validators *validatorSet, proposal *tbftpb.Proposal) {
// cache future proposal
// we cahce : consensusHeight <= futureMsg.height <= consensusHeight + size
if cfc.consensusHeight+cfc.size < proposal.Height || proposal.Height < cfc.consensusHeight {
return
}
if _, ok := cfc.cache[proposal.Height]; !ok {
cs := NewConsensusFutureMsg(cfc.logger, proposal.Height, proposal.Round, validators)
cfc.cache[proposal.Height] = cs
}
cfc.cache[proposal.Height].Proposal[proposal.Round] = proposal
cfc.logger.Debugf("addFutureProposal proposal is [%s/%d/%d] %x", proposal.Voter, proposal.Height,
proposal.Round, proposal.Block.Hash())
}
// addFutureVote add future vote in cache
func (cfc *ConsensusFutureMsgCache) addFutureVote(validators *validatorSet, vote *tbftpb.Vote) {
// cache future vote
// we cahce : consensusHeight < futureMsg.height <= consensusHeight + size
if cfc.consensusHeight+cfc.size < vote.Height || vote.Height <= cfc.consensusHeight {
return
}
if _, ok := cfc.cache[vote.Height]; !ok {
cs := NewConsensusFutureMsg(cfc.logger, vote.Height, vote.Round, validators)
cfc.cache[vote.Height] = cs
}
_, err := cfc.cache[vote.Height].heightRoundVoteSet.addVote(vote)
if err != nil {
cfc.logger.Debugf("addFutureVote addVote %v, err: %v", vote, err)
}
}
// getConsensusFutureProposal get future proposal in cache
func (cfc *ConsensusFutureMsgCache) getConsensusFutureProposal(height uint64, round int32) *tbftpb.Proposal {
if state, ok := cfc.cache[height]; ok {
return state.Proposal[round]
}
return nil
}
// getConsensusFutureProposal get future vote in cache
func (cfc *ConsensusFutureMsgCache) getConsensusFutureVote(height uint64, round int32) *roundVoteSet {
if state, ok := cfc.cache[height]; ok {
return state.heightRoundVoteSet.getRoundVoteSet(round)
}
return nil
}
//
// gc
// @Description: Delete the cache before the consensus height
// @receiver cache
// @param height
//
func (cfc *ConsensusFutureMsgCache) gc() {
// delete every 10 heights
if cfc.consensusHeight%10 != 0 {
return
}
// delete the cache before the consensus height
for k := range cfc.cache {
cfc.logger.Debugf("futureCahce delete ,gc params: %d,%d,%d", k, cfc.size, cfc.consensusHeight)
if k < cfc.consensusHeight {
delete(cfc.cache, k)
}
}
}
// NewConsensusState creates a new ConsensusState instance
func NewConsensusState(logger protocol.Logger, id string) *ConsensusState {
cs := &ConsensusState{
logger: logger,
Id: id,
}
return cs
}
// toProto serializes the ConsensusState instance
func (cs *ConsensusState) toProto() *tbftpb.ConsensusState {
if cs == nil {
return nil
}
csProto := &tbftpb.ConsensusState{
Id: cs.Id,
Height: cs.Height,
Round: cs.Round,
Step: cs.Step,
Proposal: cs.Proposal.PbMsg,
VerifingProposal: cs.VerifingProposal.PbMsg,
HeightRoundVoteSet: cs.heightRoundVoteSet.ToProto(),
}
return csProto
}
//
// consensusStateCache
// @Description: Cache historical consensus state
//
type consensusStateCache struct {
sync.Mutex
size uint64
cache map[uint64]*ConsensusState
}
//
// newConsensusStateCache
// @Description: Create a new state cache
// @param size
// @return *consensusStateCache
//
func newConsensusStateCache(size uint64) *consensusStateCache {
return &consensusStateCache{
size: size,
cache: make(map[uint64]*ConsensusState, size),
}
}
//
// addConsensusState
// @Description: Add a new state to the cache
// @receiver cache
// @param state
//
func (cache *consensusStateCache) addConsensusState(state *ConsensusState) {
if state == nil || state.Height <= 0 {
return
}
cache.Lock()
defer cache.Unlock()
cache.cache[state.Height] = state
cache.gc(state.Height)
}
//
// getConsensusState
// @Description: Get the desired consensus state from the cache, and return nil if it doesn't exist
// @receiver cache
// @param height
// @return *ConsensusState
//
func (cache *consensusStateCache) getConsensusState(height uint64) *ConsensusState {
cache.Lock()
defer cache.Unlock()
if state, ok := cache.cache[height]; ok {
return state
}
return nil
}
//
// gc
// @Description: Delete too many caches, triggered every time a new state is added to the cache
// @receiver cache
// @param height
//
func (cache *consensusStateCache) gc(height uint64) {
for k := range cache.cache {
//if k < (height - cache.size) {
cache.cache[k].logger.Debugf("state delete ,gc params: %d,%d,%d", k, cache.size, height)
if (k + cache.size) <= height {
delete(cache.cache, k)
}
}
}
type proposedProposal struct {
proposedBlock *consensuspb.ProposalBlock
qc []*tbftpb.Vote
}