-
Notifications
You must be signed in to change notification settings - Fork 178
/
pending_status.go
55 lines (48 loc) · 1.57 KB
/
pending_status.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
package voteaggregator
import (
"github.com/onflow/flow-go/consensus/hotstuff/model"
"github.com/onflow/flow-go/model/flow"
)
// PendingVotes stores all the pending votes for different block proposals
type PendingVotes struct {
// maps block ID to pending status for that block
votes map[flow.Identifier]*PendingStatus
}
// PendingStatus keeps track of pending votes for the same block
type PendingStatus struct {
// When receiving missing block, first received votes will be accumulated
orderedVotes []*model.Vote
// For avoiding duplicate votes
voteMap map[flow.Identifier]struct{}
}
// AddVote adds a vote as a pending vote
// returns true if it can be added to a PendingStatus successfully
// returns false otherwise
func (pv *PendingVotes) AddVote(vote *model.Vote) bool {
status, exists := pv.votes[vote.BlockID]
if !exists {
status = NewPendingStatus()
pv.votes[vote.BlockID] = status
}
return status.AddVote(vote)
}
// AddVote adds a vote as a pending vote
// returns false if it has been added before
// returns true otherwise
func (ps *PendingStatus) AddVote(vote *model.Vote) bool {
_, exists := ps.voteMap[vote.ID()]
if exists {
return false
}
ps.voteMap[vote.ID()] = struct{}{}
ps.orderedVotes = append(ps.orderedVotes, vote)
return true
}
// NewPendingVotes creates a PendingVotes instance
func NewPendingVotes() *PendingVotes {
return &PendingVotes{votes: make(map[flow.Identifier]*PendingStatus)}
}
// NewPendingStatus creates a PendingStatus instance
func NewPendingStatus() *PendingStatus {
return &PendingStatus{voteMap: make(map[flow.Identifier]struct{})}
}