gecko/snow/engine/snowman/block_job.go

78 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 snowman
import (
2020-05-26 12:16:23 -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/snowman"
"github.com/ava-labs/gecko/snow/engine/common/queue"
2020-05-26 12:16:23 -07:00
"github.com/ava-labs/gecko/utils/logging"
2020-03-10 12:20:34 -07:00
)
type parser struct {
2020-05-26 12:16:23 -07:00
log logging.Logger
2020-03-10 12:20:34 -07:00
numAccepted, numDropped prometheus.Counter
vm ChainVM
}
func (p *parser) Parse(blkBytes []byte) (queue.Job, error) {
blk, err := p.vm.ParseBlock(blkBytes)
if err != nil {
return nil, err
}
return &blockJob{
2020-05-26 12:16:23 -07:00
log: p.log,
2020-03-10 12:20:34 -07:00
numAccepted: p.numAccepted,
numDropped: p.numDropped,
blk: blk,
}, nil
}
type blockJob struct {
2020-05-26 12:16:23 -07:00
log logging.Logger
2020-03-10 12:20:34 -07:00
numAccepted, numDropped prometheus.Counter
blk snowman.Block
}
func (b *blockJob) ID() ids.ID { return b.blk.ID() }
func (b *blockJob) MissingDependencies() ids.Set {
missing := ids.Set{}
if parent := b.blk.Parent(); parent.Status() != choices.Accepted {
missing.Add(parent.ID())
}
return missing
}
2020-05-26 12:16:23 -07:00
func (b *blockJob) Execute() error {
2020-03-10 12:20:34 -07:00
if b.MissingDependencies().Len() != 0 {
b.numDropped.Inc()
2020-05-26 12:16:23 -07:00
return errors.New("attempting to accept a block with missing dependencies")
2020-03-10 12:20:34 -07:00
}
2020-05-26 12:16:23 -07:00
status := b.blk.Status()
switch status {
2020-03-10 12:20:34 -07:00
case choices.Unknown, choices.Rejected:
b.numDropped.Inc()
2020-05-26 12:16:23 -07:00
return fmt.Errorf("attempting to execute block with status %s", status)
2020-03-10 12:20:34 -07:00
case choices.Processing:
2020-05-26 12:16:23 -07:00
if err := b.blk.Verify(); err != nil {
2020-05-26 13:02:41 -07:00
b.log.Debug("block %s failed verification during bootstrapping due to %s",
2020-05-26 12:16:23 -07:00
b.blk.ID(), err)
}
2020-03-26 23:42:16 -07:00
b.numAccepted.Inc()
2020-05-26 12:16:23 -07:00
if err := b.blk.Accept(); err != nil {
2020-05-26 13:02:41 -07:00
b.log.Debug("block %s failed to accept during bootstrapping due to %s",
b.blk.ID(), err)
2020-05-26 12:16:23 -07:00
return fmt.Errorf("failed to accept block in bootstrapping: %w", err)
}
2020-03-10 12:20:34 -07:00
}
2020-05-26 12:16:23 -07:00
return nil
2020-03-10 12:20:34 -07:00
}
func (b *blockJob) Bytes() []byte { return b.blk.Bytes() }