forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
block_job.go
116 lines (102 loc) · 3.12 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (C) 2019-2024, 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/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"
"github.com/MetalBlockchain/metalgo/utils/set"
)
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(ctx context.Context, blkBytes []byte) (queue.Job, error) {
blk, err := p.vm.ParseBlock(ctx, blkBytes)
if err != nil {
return nil, err
}
return &blockJob{
log: p.log,
numAccepted: p.numAccepted,
numDropped: p.numDropped,
blk: blk,
vm: p.vm,
}, nil
}
type blockJob struct {
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(ctx context.Context) (set.Set[ids.ID], error) {
missing := set.Set[ids.ID]{}
parentID := b.blk.Parent()
if parent, err := b.vm.GetBlock(ctx, parentID); err != nil || parent.Status() != choices.Accepted {
missing.Add(parentID)
}
return missing, nil
}
func (b *blockJob) HasMissingDependencies(ctx context.Context) (bool, error) {
parentID := b.blk.Parent()
if parent, err := b.vm.GetBlock(ctx, parentID); err != nil || parent.Status() != choices.Accepted {
return true, nil
}
return false, nil
}
func (b *blockJob) Execute(ctx context.Context) error {
hasMissingDeps, err := b.HasMissingDependencies(ctx)
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(ctx); err != nil {
b.log.Error("block failed verification during bootstrapping",
zap.Stringer("blkID", blkID),
zap.Error(err),
)
return fmt.Errorf("failed to verify block in bootstrapping: %w", err)
}
b.numAccepted.Inc()
b.log.Trace("accepting block in bootstrapping",
zap.Stringer("blkID", blkID),
zap.Uint64("height", b.blk.Height()),
zap.Time("timestamp", b.blk.Timestamp()),
)
if err := b.blk.Accept(ctx); err != nil {
b.log.Debug("failed to accept block during bootstrapping",
zap.Stringer("blkID", blkID),
zap.Error(err),
)
return fmt.Errorf("failed to accept block in bootstrapping: %w", err)
}
}
return nil
}
func (b *blockJob) Bytes() []byte {
return b.blk.Bytes()
}