gecko/snow/engine/avalanche/vertex_job.go

80 lines
2.0 KiB
Go
Raw Normal View History

2020-03-10 12:20:34 -07:00
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package avalanche
import (
2020-05-26 10:57:42 -07:00
"errors"
"fmt"
2020-03-10 12:20:34 -07:00
"github.com/prometheus/client_golang/prometheus"
"github.com/ava-labs/gecko/ids"
"github.com/ava-labs/gecko/snow/choices"
"github.com/ava-labs/gecko/snow/consensus/avalanche"
"github.com/ava-labs/gecko/snow/engine/common/queue"
2020-05-20 08:37:01 -07:00
"github.com/ava-labs/gecko/utils/logging"
2020-03-10 12:20:34 -07:00
)
type vtxParser struct {
2020-05-20 08:37:01 -07:00
log logging.Logger
2020-03-10 12:20:34 -07:00
numAccepted, numDropped prometheus.Counter
state State
}
func (p *vtxParser) Parse(vtxBytes []byte) (queue.Job, error) {
vtx, err := p.state.ParseVertex(vtxBytes)
if err != nil {
return nil, err
}
return &vertexJob{
2020-05-20 08:37:01 -07:00
log: p.log,
2020-03-10 12:20:34 -07:00
numAccepted: p.numAccepted,
numDropped: p.numDropped,
vtx: vtx,
}, nil
}
type vertexJob struct {
2020-05-20 08:37:01 -07:00
log logging.Logger
2020-03-10 12:20:34 -07:00
numAccepted, numDropped prometheus.Counter
vtx avalanche.Vertex
}
func (v *vertexJob) ID() ids.ID { return v.vtx.ID() }
func (v *vertexJob) MissingDependencies() ids.Set {
missing := ids.Set{}
for _, parent := range v.vtx.Parents() {
if parent.Status() != choices.Accepted {
missing.Add(parent.ID())
}
}
return missing
}
2020-05-26 10:57:42 -07:00
func (v *vertexJob) Execute() error {
2020-03-10 12:20:34 -07:00
if v.MissingDependencies().Len() != 0 {
v.numDropped.Inc()
2020-05-26 10:57:42 -07:00
return errors.New("attempting to execute blocked vertex")
2020-03-10 12:20:34 -07:00
}
for _, tx := range v.vtx.Txs() {
if tx.Status() != choices.Accepted {
v.numDropped.Inc()
2020-05-28 20:48:08 -07:00
v.log.Warn("attempting to execute vertex with non-accepted transactions")
return nil
2020-03-10 12:20:34 -07:00
}
}
2020-05-26 10:57:42 -07:00
status := v.vtx.Status()
switch status {
2020-03-10 12:20:34 -07:00
case choices.Unknown, choices.Rejected:
v.numDropped.Inc()
2020-05-26 10:57:42 -07:00
return fmt.Errorf("attempting to execute vertex with status %s", status)
2020-03-10 12:20:34 -07:00
case choices.Processing:
v.numAccepted.Inc()
2020-05-26 10:57:42 -07:00
if err := v.vtx.Accept(); err != nil {
return fmt.Errorf("failed to accept vertex in bootstrapping: %w", err)
}
2020-03-10 12:20:34 -07:00
}
2020-05-26 10:57:42 -07:00
return nil
2020-03-10 12:20:34 -07:00
}
func (v *vertexJob) Bytes() []byte { return v.vtx.Bytes() }