-
Notifications
You must be signed in to change notification settings - Fork 0
/
voter.go
95 lines (77 loc) · 2.04 KB
/
voter.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
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package snowman
import (
"github.com/ava-labs/avalanchego/ids"
)
// Voter records chits received from [vdr] once its dependencies are met.
type voter struct {
t *Transitive
vdr ids.ShortID
requestID uint32
response ids.ID
deps ids.Set
}
func (v *voter) Dependencies() ids.Set { return v.deps }
// Mark that a dependency has been met.
func (v *voter) Fulfill(id ids.ID) {
v.deps.Remove(id)
v.Update()
}
// Abandon this attempt to record chits.
func (v *voter) Abandon(id ids.ID) { v.Fulfill(id) }
func (v *voter) Update() {
if v.deps.Len() != 0 || v.t.errs.Errored() {
return
}
var results []ids.Bag
if v.response == ids.Empty {
results = v.t.polls.Drop(v.requestID, v.vdr)
} else {
results = v.t.polls.Vote(v.requestID, v.vdr, v.response)
}
if len(results) == 0 {
return
}
// To prevent any potential deadlocks with un-disclosed dependencies, votes
// must be bubbled to the nearest valid block
for i, result := range results {
results[i] = v.bubbleVotes(result)
}
v.t.Ctx.Log.Debug("Finishing poll [%d] with:\n%s", v.requestID, &results)
for _, result := range results {
if err := v.t.Consensus.RecordPoll(result); err != nil {
v.t.errs.Add(err)
}
}
if v.t.errs.Errored() {
return
}
if err := v.t.VM.SetPreference(v.t.Consensus.Preference()); err != nil {
v.t.errs.Add(err)
return
}
if v.t.Consensus.Finalized() {
v.t.Ctx.Log.Debug("Snowman engine can quiesce")
return
}
v.t.Ctx.Log.Debug("Snowman engine can't quiesce")
v.t.repoll()
}
func (v *voter) bubbleVotes(votes ids.Bag) ids.Bag {
bubbledVotes := ids.Bag{}
for _, vote := range votes.List() {
count := votes.Count(vote)
blk, err := v.t.VM.GetBlock(vote)
if err != nil {
continue
}
for blk.Status().Fetched() && !v.t.Consensus.DecidedOrProcessing(blk) {
blk = blk.Parent()
}
if !blk.Status().Decided() && v.t.Consensus.DecidedOrProcessing(blk) {
bubbledVotes.AddCount(blk.ID(), count)
}
}
return bubbledVotes
}