-
Notifications
You must be signed in to change notification settings - Fork 175
/
common.go
45 lines (36 loc) · 1.63 KB
/
common.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
package votecollector
import (
"errors"
"fmt"
"github.com/onflow/flow-go/consensus/hotstuff"
"github.com/onflow/flow-go/consensus/hotstuff/model"
)
var (
VoteForIncompatibleViewError = errors.New("vote for incompatible view")
VoteForIncompatibleBlockError = errors.New("vote for incompatible block")
)
/******************************* NoopProcessor *******************************/
// NoopProcessor implements hotstuff.VoteProcessor. It drops all votes.
type NoopProcessor struct {
status hotstuff.VoteCollectorStatus
}
func NewNoopCollector(status hotstuff.VoteCollectorStatus) *NoopProcessor {
return &NoopProcessor{status}
}
func (c *NoopProcessor) Process(*model.Vote) error { return nil }
func (c *NoopProcessor) Status() hotstuff.VoteCollectorStatus { return c.status }
/************************ enforcing vote is for block ************************/
// EnsureVoteForBlock verifies that the vote is for the given block.
// Returns nil on success and sentinel errors:
// * model.VoteForIncompatibleViewError if the vote is from a different view than block
// * model.VoteForIncompatibleBlockError if the vote is from the same view as block
// but for a different blockID
func EnsureVoteForBlock(vote *model.Vote, block *model.Block) error {
if vote.View != block.View {
return fmt.Errorf("vote %v has view %d while block's view is %d: %w ", vote.ID(), vote.View, block.View, VoteForIncompatibleViewError)
}
if vote.BlockID != block.BlockID {
return fmt.Errorf("expecting only votes for block %v, but vote %v is for block %v: %w ", block.BlockID, vote.ID(), vote.BlockID, VoteForIncompatibleBlockError)
}
return nil
}