forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
block_job.go
99 lines (87 loc) · 2.82 KB
/
block_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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package bootstrap
import (
"errors"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/MetalBlockchain/metalgo/ids"
"github.com/MetalBlockchain/metalgo/snow/choices"
"github.com/MetalBlockchain/metalgo/snow/consensus/snowman"
"github.com/MetalBlockchain/metalgo/snow/engine/common/queue"
"github.com/MetalBlockchain/metalgo/snow/engine/snowman/block"
"github.com/MetalBlockchain/metalgo/utils/logging"
)
var errMissingDependenciesOnAccept = errors.New("attempting to accept a block with missing dependencies")
type parser struct {
log logging.Logger
numAccepted, numDropped prometheus.Counter
vm block.ChainVM
}
func (p *parser) Parse(blkBytes []byte) (queue.Job, error) {
blk, err := p.vm.ParseBlock(blkBytes)
if err != nil {
return nil, err
}
return &blockJob{
parser: p,
log: p.log,
numAccepted: p.numAccepted,
numDropped: p.numDropped,
blk: blk,
vm: p.vm,
}, nil
}
type blockJob struct {
parser *parser
log logging.Logger
numAccepted, numDropped prometheus.Counter
blk snowman.Block
vm block.Getter
}
func (b *blockJob) ID() ids.ID { return b.blk.ID() }
func (b *blockJob) MissingDependencies() (ids.Set, error) {
missing := ids.Set{}
parentID := b.blk.Parent()
if parent, err := b.vm.GetBlock(parentID); err != nil || parent.Status() != choices.Accepted {
missing.Add(parentID)
}
return missing, nil
}
func (b *blockJob) HasMissingDependencies() (bool, error) {
parentID := b.blk.Parent()
if parent, err := b.vm.GetBlock(parentID); err != nil || parent.Status() != choices.Accepted {
return true, nil
}
return false, nil
}
func (b *blockJob) Execute() error {
hasMissingDeps, err := b.HasMissingDependencies()
if err != nil {
return err
}
if hasMissingDeps {
b.numDropped.Inc()
return errMissingDependenciesOnAccept
}
status := b.blk.Status()
switch status {
case choices.Unknown, choices.Rejected:
b.numDropped.Inc()
return fmt.Errorf("attempting to execute block with status %s", status)
case choices.Processing:
blkID := b.blk.ID()
if err := b.blk.Verify(); err != nil {
b.log.Error("block %s failed verification during bootstrapping due to %s", blkID, err)
return fmt.Errorf("failed to verify block in bootstrapping: %w", err)
}
b.numAccepted.Inc()
b.log.Trace("accepting block (%s, %d) in bootstrapping", blkID, b.blk.Height())
if err := b.blk.Accept(); err != nil {
b.log.Debug("block %s failed to accept during bootstrapping due to %s", blkID, err)
return fmt.Errorf("failed to accept block in bootstrapping: %w", err)
}
}
return nil
}
func (b *blockJob) Bytes() []byte { return b.blk.Bytes() }