-
Notifications
You must be signed in to change notification settings - Fork 672
/
voter.go
159 lines (137 loc) · 3.55 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
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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package avalanche
import (
"go.uber.org/zap"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/consensus/snowstorm"
"github.com/ava-labs/avalanchego/snow/engine/avalanche/vertex"
)
// Voter records chits received from [vdr] once its dependencies are met.
type voter struct {
t *Transitive
vdr ids.NodeID
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
}
results := v.t.polls.Vote(v.requestID, v.vdr, v.response)
if len(results) == 0 {
return
}
for _, result := range results {
_, err := v.bubbleVotes(result)
if err != nil {
v.t.errs.Add(err)
return
}
}
for _, result := range results {
result := result
v.t.Ctx.Log.Debug("finishing poll",
zap.Stringer("result", &result),
)
if err := v.t.Consensus.RecordPoll(result); err != nil {
v.t.errs.Add(err)
return
}
}
orphans := v.t.Consensus.Orphans()
txs := make([]snowstorm.Tx, 0, orphans.Len())
for orphanID := range orphans {
if tx, err := v.t.VM.GetTx(orphanID); err == nil {
txs = append(txs, tx)
} else {
v.t.Ctx.Log.Warn("failed to fetch tx during attempted re-issuance",
zap.Stringer("txID", orphanID),
zap.Error(err),
)
}
}
if len(txs) > 0 {
v.t.Ctx.Log.Debug("re-issuing transactions",
zap.Int("numTxs", len(txs)),
)
}
if _, err := v.t.batch(txs, batchOption{force: true}); err != nil {
v.t.errs.Add(err)
return
}
if v.t.Consensus.Quiesce() {
v.t.Ctx.Log.Debug("avalanche engine can quiesce")
return
}
v.t.Ctx.Log.Debug("avalanche engine can't quiesce")
v.t.repoll()
}
func (v *voter) bubbleVotes(votes ids.UniqueBag) (ids.UniqueBag, error) {
vertexHeap := vertex.NewHeap()
for vote, set := range votes {
vtx, err := v.t.Manager.GetVtx(vote)
if err != nil {
v.t.Ctx.Log.Debug("dropping vote(s)",
zap.String("reason", "failed to fetch vertex"),
zap.Stringer("voteID", vote),
zap.Int("numVotes", set.Len()),
zap.Error(err),
)
votes.RemoveSet(vote)
continue
}
vertexHeap.Push(vtx)
}
for vertexHeap.Len() > 0 {
vtx := vertexHeap.Pop()
vtxID := vtx.ID()
set := votes.GetSet(vtxID)
status := vtx.Status()
if !status.Fetched() {
v.t.Ctx.Log.Debug("dropping vote(s)",
zap.String("reason", "vertex unknown"),
zap.Int("numVotes", set.Len()),
zap.Stringer("vtxID", vtxID),
)
votes.RemoveSet(vtxID)
continue
}
if status.Decided() {
v.t.Ctx.Log.Verbo("dropping vote(s)",
zap.String("reason", "vertex already decided"),
zap.Int("numVotes", set.Len()),
zap.Stringer("vtxID", vtxID),
zap.Stringer("status", status),
)
votes.RemoveSet(vtxID)
continue
}
if !v.t.Consensus.VertexIssued(vtx) {
v.t.Ctx.Log.Verbo("bubbling vote(s)",
zap.String("reason", "vertex not issued"),
zap.Int("numVotes", set.Len()),
zap.Stringer("vtxID", vtxID),
)
votes.RemoveSet(vtxID) // Remove votes for this vertex because it hasn't been issued
parents, err := vtx.Parents()
if err != nil {
return votes, err
}
for _, parentVtx := range parents {
votes.UnionSet(parentVtx.ID(), set)
vertexHeap.Push(parentVtx)
}
}
}
return votes, nil
}