/
gossiper.go
421 lines (393 loc) · 13.1 KB
/
gossiper.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//go:generate ../../scripts/mockery_generate.sh Gossiper
package consensus
import (
"context"
"fmt"
"github.com/gogo/protobuf/proto"
"github.com/hashicorp/go-multierror"
cstypes "github.com/dashpay/tenderdash/internal/consensus/types"
sm "github.com/dashpay/tenderdash/internal/state"
"github.com/dashpay/tenderdash/libs/log"
tmcons "github.com/dashpay/tenderdash/proto/tendermint/consensus"
tmproto "github.com/dashpay/tenderdash/proto/tendermint/types"
"github.com/dashpay/tenderdash/types"
)
// Gossiper is the interface that wraps the methods needed to gossip a state between connected peers
type Gossiper interface {
GossipProposal(ctx context.Context, rs cstypes.RoundState, prs *cstypes.PeerRoundState)
GossipProposalBlockParts(ctx context.Context, rs cstypes.RoundState, prs *cstypes.PeerRoundState)
GossipBlockPartsForCatchup(ctx context.Context, rs cstypes.RoundState, prs *cstypes.PeerRoundState)
GossipVote(ctx context.Context, rs cstypes.RoundState, prs *cstypes.PeerRoundState)
GossipVoteSetMaj23(ctx context.Context, rs cstypes.RoundState, prs *cstypes.PeerRoundState)
GossipCommit(ctx context.Context, rs cstypes.RoundState, prs *cstypes.PeerRoundState)
}
type msgGossiper struct {
logger log.Logger
ps *PeerState
msgSender *p2pMsgSender
blockStore *blockRepository
optimistic bool
}
func newVoteSetMaj23(height int64, round int32, msgType tmproto.SignedMsgType, maj23 types.BlockID) *tmcons.VoteSetMaj23 {
return &tmcons.VoteSetMaj23{
Height: height,
Round: round,
Type: msgType,
BlockID: maj23.ToProto(),
}
}
func newVoteSetMaj23FromCommit(commit *types.Commit, msgType tmproto.SignedMsgType) *tmcons.VoteSetMaj23 {
return newVoteSetMaj23(commit.Height, commit.Round, msgType, commit.BlockID)
}
func newVoteSetMaj23FromPRS(prs *cstypes.PeerRoundState, msgType tmproto.SignedMsgType, maj23 types.BlockID) *tmcons.VoteSetMaj23 {
return newVoteSetMaj23(prs.Height, prs.Round, msgType, maj23)
}
// GossipVoteSetMaj23 sends VoteSetMaj23 messages to the peer
func (g *msgGossiper) GossipVoteSetMaj23(
ctx context.Context,
rs cstypes.RoundState,
prs *cstypes.PeerRoundState,
) {
msgs := make([]*tmcons.VoteSetMaj23, 0, 4)
if rs.Height == prs.Height {
// maybe send Height/Round/Prevotes
maj23, ok := rs.Votes.Prevotes(prs.Round).TwoThirdsMajority()
if ok {
msgs = append(msgs, newVoteSetMaj23FromPRS(prs, tmproto.PrevoteType, maj23))
}
}
if rs.Height == prs.Height && prs.ProposalPOLRound >= 0 {
// maybe send Height/Round/ProposalPOL
maj23, ok := rs.Votes.Prevotes(prs.ProposalPOLRound).TwoThirdsMajority()
if ok {
msgs = append(msgs, newVoteSetMaj23FromPRS(prs, tmproto.PrevoteType, maj23))
}
}
if rs.Height == prs.Height {
// maybe send Height/Round/Precommits
maj23, ok := rs.Votes.Precommits(prs.Round).TwoThirdsMajority()
if ok {
msgs = append(msgs, newVoteSetMaj23FromPRS(prs, tmproto.PrecommitType, maj23))
}
}
// Little point sending LastCommitRound/LastCommit, these are fleeting and
// non-blocking.
if prs.CatchupCommitRound != -1 && prs.Height > 0 {
bsHeight := g.blockStore.Height()
bsBase := g.blockStore.Base()
if prs.Height <= bsHeight && prs.Height >= bsBase {
// maybe send Height/CatchupCommitRound/CatchupCommit
commit := g.blockStore.loadCommit(prs.Height)
if commit != nil {
msgs = append(msgs, newVoteSetMaj23FromCommit(commit, tmproto.PrecommitType))
}
}
}
logger := g.logger.With([]any{
"height", prs.Height,
"round", prs.Round,
})
for _, msg := range msgs {
logger.Trace("syncing vote set +2/3 message")
err := g.msgSender.send(ctx, msg)
if err != nil {
logger.Error("failed to syncing vote set +2/3 message to the peer", "error", err)
}
}
}
// GossipProposalBlockParts sends a block part message to the peer
func (g *msgGossiper) GossipProposalBlockParts(
ctx context.Context,
rs cstypes.RoundState,
prs *cstypes.PeerRoundState,
) {
index, ok := rs.ProposalBlockParts.BitArray().Sub(prs.ProposalBlockParts.Copy()).PickRandom()
if !ok {
return
}
logger := g.logger.With([]any{
"height", prs.Height,
"round", prs.Round,
"part_index", index,
})
logger.Trace("syncing proposal block part to the peer")
part := rs.ProposalBlockParts.GetPart(index)
// NOTE: A peer might have received a different proposal message, so this Proposal msg will be rejected!
err := g.syncProposalBlockPart(ctx, part, rs.Height, rs.Round)
if err != nil {
logger.Error("failed to sync proposal block part to the peer", "error", err)
}
}
// GossipProposal sends a proposal message to the peer
func (g *msgGossiper) GossipProposal(ctx context.Context, rs cstypes.RoundState, prs *cstypes.PeerRoundState) {
logger := g.logger.With([]any{
"height", prs.Height,
"round", prs.Round,
})
// Proposal: share the proposal metadata with peer.
logger.Trace("syncing proposal")
err := g.sync(ctx, rs.Proposal.ToProto(), updatePeerProposal(g.ps, rs.Proposal))
if err != nil {
logger.Error("failed to sync proposal to the peer", "error", err)
}
// ProposalPOL: lets peer know which POL votes we have so far. The peer
// must receive ProposalMessage first. Note, rs.Proposal was validated,
// so rs.Proposal.POLRound <= rs.Round, so we definitely have
// rs.Votes.Prevotes(rs.Proposal.POLRound).
if rs.Proposal.POLRound < 0 {
return
}
pPol := rs.Votes.Prevotes(rs.Proposal.POLRound).BitArray()
pPolProto := pPol.ToProto()
propPOLMsg := &tmcons.ProposalPOL{
Height: rs.Height,
ProposalPolRound: rs.Proposal.POLRound,
ProposalPol: *pPolProto,
}
logger.Trace("syncing proposal POL")
err = g.sync(ctx, propPOLMsg, nil)
if err != nil {
logger.Error("failed to sync proposal POL to the peer", "error", err)
}
}
// GossipBlockPartsForCatchup sends a block part for catch up
func (g *msgGossiper) GossipBlockPartsForCatchup(
ctx context.Context,
_ cstypes.RoundState,
prs *cstypes.PeerRoundState,
) {
index, ok := prs.ProposalBlockParts.Not().PickRandom()
if !ok {
return
}
logger := g.logger.With([]any{
"height", prs.Height,
"round", prs.Round,
})
meta, err := g.blockStore.loadMeta(prs.Height)
if err != nil {
logger.Error("couldn't find a block meta", "error", err)
return
}
err = g.ensurePeerPartSetHeader(meta.BlockID.PartSetHeader, prs.ProposalBlockPartSetHeader)
if err != nil {
logger.Error("block and peer part-set headers do not match", "error", err)
return
}
part, err := g.blockStore.loadPart(prs.Height, index)
if err != nil {
logger.Error("couldn't find a block part", "part_index", index, "error", err)
return
}
err = g.syncProposalBlockPart(ctx, part, prs.Height, meta.Round)
if err != nil {
logger.Error("failed to sync proposal block part to the peer", "error", err)
}
}
// GossipCommit sends a commit message to the peer
func (g *msgGossiper) GossipCommit(ctx context.Context, rs cstypes.RoundState, prs *cstypes.PeerRoundState) {
if prs.HasCommit {
return
}
logger := g.logger.With([]any{
"height", rs.Height,
"peer_height", prs.Height,
})
var commit *types.Commit
blockStoreBase := g.blockStore.Base()
if rs.Height == prs.Height+1 {
commit = rs.LastCommit
}
if rs.Height >= prs.Height+2 && prs.Height >= blockStoreBase {
// Load the block commit for prs.Height, which contains precommit
// signatures for prs.Height.
commit = g.blockStore.LoadBlockCommit(prs.Height)
}
if commit == nil {
if prs.Height == 0 {
return // not an error when we are at genesis
}
logger.Error("commit not found")
return
}
logger.Debug("syncing commit")
err := g.sync(ctx, commit.ToProto(), updatePeerCommit(g.ps, commit))
if err != nil {
logger.Error("failed to sync commit to the peer", "error", err)
}
}
// GossipVote sends a vote message to the peer
func (g *msgGossiper) GossipVote(ctx context.Context, rs cstypes.RoundState, prs *cstypes.PeerRoundState) {
vote, found := g.pickVoteForGossip(rs, prs)
if !found {
return
}
protoVote := vote.ToProto()
logger := g.logger.With([]any{
"vote", vote,
"val_proTxHash", vote.ValidatorProTxHash.ShortString(),
"vote_height", vote.Height,
"vote_round", vote.Round,
"proto_vote_size", protoVote.Size(),
})
logger.Trace("syncing vote message")
err := g.sync(ctx, protoVote, updatePeerVote(g.ps, vote))
if err != nil {
logger.Error("failed to sync vote message to the peer", "error", err)
}
}
func (g *msgGossiper) syncProposalBlockPart(ctx context.Context, part *types.Part, height int64, round int32) error {
protoPart, err := part.ToProto()
if err != nil {
return fmt.Errorf("failed to convert block part to proto, error: %w", err)
}
logger := g.logger.With([]any{
"height", height,
"round", round,
"part_index", part.Index,
})
protoBlockPart := &tmcons.BlockPart{
Height: height, // not our height, so it does not matter
Round: round, // not our height, so it does not matter
Part: *protoPart,
}
logger.Debug("syncing proposal block part")
err = g.sync(ctx, protoBlockPart, updatePeerProposalBlockPart(g.ps, height, round, int(part.Index)))
if err != nil {
logger.Error("failed to sync proposal block part to the peer", "error", err)
}
return nil
}
func (g *msgGossiper) sync(ctx context.Context, protoMsg proto.Message, syncFunc func() error) error {
err := g.msgSender.send(ctx, protoMsg)
if err != nil {
if !g.optimistic {
return err
}
}
if syncFunc == nil {
return err
}
syncErr := syncFunc()
if syncErr != nil {
err = multierror.Append(syncErr)
}
return err
}
func (g *msgGossiper) ensurePeerPartSetHeader(blockPartSetHeader types.PartSetHeader, peerPartSetHeader types.PartSetHeader) error {
// ensure that the peer's PartSetHeader is correct
if blockPartSetHeader.Equals(peerPartSetHeader) {
return nil
}
g.logger.Debug(
"peer ProposalBlockPartSetHeader mismatch",
"block_part_set_header", blockPartSetHeader,
"peer_block_part_set_header", peerPartSetHeader,
)
return fmt.Errorf("peer block part-set header %s is mismatch with block part-set header %s",
peerPartSetHeader.String(),
blockPartSetHeader.String())
}
// pickVoteForGossip picks a vote to sends it to the peer. It will return (*types.Vote and true) if
// there is a vote to send and (nil,false) otherwise.
func (g *msgGossiper) pickVoteForGossip(rs cstypes.RoundState, prs *cstypes.PeerRoundState) (*types.Vote, bool) {
var voteSets []*types.VoteSet
// if there are lastPrecommits to send
if prs.Step == cstypes.RoundStepNewHeight {
voteSets = append(voteSets, rs.LastPrecommits)
}
if prs.Round != -1 && prs.Round <= rs.Round {
// if there are POL prevotes to send
if prs.Step <= cstypes.RoundStepPropose && prs.ProposalPOLRound != -1 {
voteSets = append(voteSets, rs.Votes.Prevotes(prs.ProposalPOLRound))
}
// if there are prevotes to send
if prs.Step <= cstypes.RoundStepPrevoteWait {
voteSets = append(voteSets, rs.Votes.Prevotes(prs.Round))
}
// if there are precommits to send
if prs.Step <= cstypes.RoundStepPrecommitWait {
voteSets = append(voteSets, rs.Votes.Precommits(prs.Round))
}
// if there are prevotes to send (which are needed because of validBlock mechanism)
voteSets = append(voteSets, rs.Votes.Prevotes(prs.Round))
}
// if there are POLPrevotes to send
if prs.ProposalPOLRound != -1 {
voteSets = append(voteSets, rs.Votes.Prevotes(prs.ProposalPOLRound))
}
for _, voteSet := range voteSets {
vote, ok := g.ps.PickVoteToSend(voteSet)
if ok {
return vote, true
}
}
return nil, false
}
type blockRepository struct {
sm.BlockStore
logger log.Logger
}
// LoadCommit loads the commit for a given height.
func (r *blockRepository) loadCommit(height int64) *types.Commit {
if height == r.Height() {
commit := r.LoadSeenCommit()
// NOTE: Retrieving the height of the most recent block and retrieving
// the most recent commit does not currently occur as an atomic
// operation. We check the height and commit here in case a more recent
// commit has arrived since retrieving the latest height.
if commit != nil && commit.Height == height {
return commit
}
}
return r.LoadBlockCommit(height)
}
func (r *blockRepository) loadMeta(height int64) (*types.BlockMeta, error) {
// ensure that the peer's PartSetHeader is correct
blockMeta := r.LoadBlockMeta(height)
if blockMeta != nil {
return blockMeta, nil
}
r.logger.Error(
"failed to load block meta",
"our_height", height,
"blockstore_base", r.Base(),
"blockstore_height", r.Height(),
)
return nil, fmt.Errorf("failed to load block meta at height %d", height)
}
func (r *blockRepository) loadPart(height int64, index int) (*types.Part, error) {
part := r.LoadBlockPart(height, index)
if part != nil {
return part, nil
}
r.logger.Error(
"failed to load block part",
"height", height,
"index", index,
)
return nil, errFailedLoadBlockPart
}
func updatePeerProposal(ps *PeerState, proposal *types.Proposal) func() error {
return func() error {
ps.SetHasProposal(proposal)
return nil
}
}
func updatePeerCommit(ps *PeerState, commit *types.Commit) func() error {
return func() error {
ps.SetHasCommit(commit)
return nil
}
}
func updatePeerVote(ps *PeerState, vote *types.Vote) func() error {
return func() error {
return ps.SetHasVote(vote)
}
}
func updatePeerProposalBlockPart(ps *PeerState, height int64, round int32, index int) func() error {
return func() error {
ps.SetHasProposalBlockPart(height, round, index)
return nil
}
}