-
Notifications
You must be signed in to change notification settings - Fork 664
/
no_early_term.go
59 lines (45 loc) · 1.55 KB
/
no_early_term.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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package poll
import (
"fmt"
"github.com/ava-labs/avalanchego/ids"
)
type noEarlyTermFactory struct{}
// NewNoEarlyTermFactory returns a factory that returns polls with no early
// termination
func NewNoEarlyTermFactory() Factory { return noEarlyTermFactory{} }
func (noEarlyTermFactory) New(vdrs ids.NodeIDBag) Poll {
return &noEarlyTermPoll{polled: vdrs}
}
// noEarlyTermPoll finishes when all polled validators either respond to the
// query or a timeout occurs
type noEarlyTermPoll struct {
votes ids.Bag
polled ids.NodeIDBag
}
// Vote registers a response for this poll
func (p *noEarlyTermPoll) Vote(vdr ids.NodeID, vote ids.ID) {
count := p.polled.Count(vdr)
// make sure that a validator can't respond multiple times
p.polled.Remove(vdr)
// track the votes the validator responded with
p.votes.AddCount(vote, count)
}
// Drop any future response for this poll
func (p *noEarlyTermPoll) Drop(vdr ids.NodeID) { p.polled.Remove(vdr) }
// Finished returns true when all validators have voted
func (p *noEarlyTermPoll) Finished() bool {
return p.polled.Len() == 0
}
// Result returns the result of this poll
func (p *noEarlyTermPoll) Result() ids.Bag { return p.votes }
func (p *noEarlyTermPoll) PrefixedString(prefix string) string {
return fmt.Sprintf(
"waiting on %s\n%sreceived %s",
p.polled.PrefixedString(prefix),
prefix,
p.votes.PrefixedString(prefix),
)
}
func (p *noEarlyTermPoll) String() string { return p.PrefixedString("") }