-
Notifications
You must be signed in to change notification settings - Fork 665
/
vertex_job.go
122 lines (108 loc) · 2.97 KB
/
vertex_job.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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package bootstrap
import (
"context"
"errors"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/snow/consensus/avalanche"
"github.com/ava-labs/avalanchego/snow/engine/avalanche/vertex"
"github.com/ava-labs/avalanchego/snow/engine/common/queue"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/set"
)
var errMissingVtxDependenciesOnAccept = errors.New("attempting to execute blocked vertex")
type vtxParser struct {
log logging.Logger
numAccepted, numDropped prometheus.Counter
manager vertex.Manager
}
func (p *vtxParser) Parse(ctx context.Context, vtxBytes []byte) (queue.Job, error) {
vtx, err := p.manager.ParseVtx(ctx, vtxBytes)
if err != nil {
return nil, err
}
return &vertexJob{
log: p.log,
numAccepted: p.numAccepted,
numDropped: p.numDropped,
vtx: vtx,
}, nil
}
type vertexJob struct {
log logging.Logger
numAccepted, numDropped prometheus.Counter
vtx avalanche.Vertex
}
func (v *vertexJob) ID() ids.ID {
return v.vtx.ID()
}
func (v *vertexJob) MissingDependencies(context.Context) (set.Set[ids.ID], error) {
missing := set.Set[ids.ID]{}
parents, err := v.vtx.Parents()
if err != nil {
return missing, err
}
for _, parent := range parents {
if parent.Status() != choices.Accepted {
missing.Add(parent.ID())
}
}
return missing, nil
}
// Returns true if this vertex job has at least 1 missing dependency
func (v *vertexJob) HasMissingDependencies(context.Context) (bool, error) {
parents, err := v.vtx.Parents()
if err != nil {
return false, err
}
for _, parent := range parents {
if parent.Status() != choices.Accepted {
return true, nil
}
}
return false, nil
}
func (v *vertexJob) Execute(ctx context.Context) error {
hasMissingDependencies, err := v.HasMissingDependencies(ctx)
if err != nil {
return err
}
if hasMissingDependencies {
v.numDropped.Inc()
return errMissingVtxDependenciesOnAccept
}
txs, err := v.vtx.Txs(ctx)
if err != nil {
return err
}
for _, tx := range txs {
if tx.Status() != choices.Accepted {
v.numDropped.Inc()
v.log.Warn("attempting to execute vertex with non-accepted transactions")
return nil
}
}
status := v.vtx.Status()
switch status {
case choices.Unknown, choices.Rejected:
v.numDropped.Inc()
return fmt.Errorf("attempting to execute vertex with status %s", status)
case choices.Processing:
v.numAccepted.Inc()
v.log.Trace("accepting vertex in bootstrapping",
zap.Stringer("vtxID", v.vtx.ID()),
)
if err := v.vtx.Accept(ctx); err != nil {
return fmt.Errorf("failed to accept vertex in bootstrapping: %w", err)
}
}
return nil
}
func (v *vertexJob) Bytes() []byte {
return v.vtx.Bytes()
}