-
Notifications
You must be signed in to change notification settings - Fork 646
/
getter.go
194 lines (172 loc) · 5.58 KB
/
getter.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package getter
import (
"context"
"go.uber.org/zap"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/snow/engine/common"
"github.com/ava-labs/avalanchego/snow/engine/snowman/block"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/metric"
)
// Get requests are always served, regardless node state (bootstrapping or normal operations).
var _ common.AllGetsServer = (*getter)(nil)
func New(
vm block.ChainVM,
commonCfg common.Config,
) (common.AllGetsServer, error) {
ssVM, _ := vm.(block.StateSyncableVM)
gh := &getter{
vm: vm,
ssVM: ssVM,
sender: commonCfg.Sender,
cfg: commonCfg,
log: commonCfg.Ctx.Log,
}
var err error
gh.getAncestorsBlks, err = metric.NewAverager(
"bs",
"get_ancestors_blks",
"blocks fetched in a call to GetAncestors",
commonCfg.Ctx.Registerer,
)
return gh, err
}
type getter struct {
vm block.ChainVM
ssVM block.StateSyncableVM // can be nil
sender common.Sender
cfg common.Config
log logging.Logger
getAncestorsBlks metric.Averager
}
func (gh *getter) GetStateSummaryFrontier(ctx context.Context, nodeID ids.NodeID, requestID uint32) error {
// Note: we do not check if gh.ssVM.StateSyncEnabled since we want all
// nodes, including those disabling state sync to serve state summaries if
// these are available
if gh.ssVM == nil {
gh.log.Debug("dropping GetStateSummaryFrontier message",
zap.String("reason", "state sync not supported"),
zap.Stringer("nodeID", nodeID),
zap.Uint32("requestID", requestID),
)
return nil
}
summary, err := gh.ssVM.GetLastStateSummary(ctx)
if err != nil {
gh.log.Debug("dropping GetStateSummaryFrontier message",
zap.String("reason", "couldn't get state summary frontier"),
zap.Stringer("nodeID", nodeID),
zap.Uint32("requestID", requestID),
zap.Error(err),
)
return nil
}
gh.sender.SendStateSummaryFrontier(ctx, nodeID, requestID, summary.Bytes())
return nil
}
func (gh *getter) GetAcceptedStateSummary(ctx context.Context, nodeID ids.NodeID, requestID uint32, heights []uint64) error {
// If there are no requested heights, then we can return the result
// immediately, regardless of if the underlying VM implements state sync.
if len(heights) == 0 {
gh.sender.SendAcceptedStateSummary(ctx, nodeID, requestID, nil)
return nil
}
// Note: we do not check if gh.ssVM.StateSyncEnabled since we want all
// nodes, including those disabling state sync to serve state summaries if
// these are available
if gh.ssVM == nil {
gh.log.Debug("dropping GetAcceptedStateSummary message",
zap.String("reason", "state sync not supported"),
zap.Stringer("nodeID", nodeID),
zap.Uint32("requestID", requestID),
)
return nil
}
summaryIDs := make([]ids.ID, 0, len(heights))
for _, height := range heights {
summary, err := gh.ssVM.GetStateSummary(ctx, height)
if err == block.ErrStateSyncableVMNotImplemented {
gh.log.Debug("dropping GetAcceptedStateSummary message",
zap.String("reason", "state sync not supported"),
zap.Stringer("nodeID", nodeID),
zap.Uint32("requestID", requestID),
)
return nil
}
if err != nil {
gh.log.Debug("couldn't get state summary",
zap.Uint64("height", height),
zap.Error(err),
)
continue
}
summaryIDs = append(summaryIDs, summary.ID())
}
gh.sender.SendAcceptedStateSummary(ctx, nodeID, requestID, summaryIDs)
return nil
}
func (gh *getter) GetAcceptedFrontier(ctx context.Context, nodeID ids.NodeID, requestID uint32) error {
lastAccepted, err := gh.vm.LastAccepted(ctx)
if err != nil {
return err
}
gh.sender.SendAcceptedFrontier(ctx, nodeID, requestID, lastAccepted)
return nil
}
func (gh *getter) GetAccepted(ctx context.Context, nodeID ids.NodeID, requestID uint32, containerIDs []ids.ID) error {
acceptedIDs := make([]ids.ID, 0, len(containerIDs))
for _, blkID := range containerIDs {
blk, err := gh.vm.GetBlock(ctx, blkID)
if err == nil && blk.Status() == choices.Accepted {
acceptedIDs = append(acceptedIDs, blkID)
}
}
gh.sender.SendAccepted(ctx, nodeID, requestID, acceptedIDs)
return nil
}
func (gh *getter) GetAncestors(ctx context.Context, nodeID ids.NodeID, requestID uint32, blkID ids.ID) error {
ancestorsBytes, err := block.GetAncestors(
ctx,
gh.log,
gh.vm,
blkID,
gh.cfg.AncestorsMaxContainersSent,
constants.MaxContainersLen,
gh.cfg.MaxTimeGetAncestors,
)
if err != nil {
gh.log.Verbo("dropping GetAncestors message",
zap.String("reason", "couldn't get ancestors"),
zap.Stringer("nodeID", nodeID),
zap.Uint32("requestID", requestID),
zap.Stringer("blkID", blkID),
zap.Error(err),
)
return nil
}
gh.getAncestorsBlks.Observe(float64(len(ancestorsBytes)))
gh.sender.SendAncestors(ctx, nodeID, requestID, ancestorsBytes)
return nil
}
func (gh *getter) Get(ctx context.Context, nodeID ids.NodeID, requestID uint32, blkID ids.ID) error {
blk, err := gh.vm.GetBlock(ctx, blkID)
if err != nil {
// If we failed to get the block, that means either an unexpected error
// has occurred, [vdr] is not following the protocol, or the
// block has been pruned.
gh.log.Debug("failed Get request",
zap.Stringer("nodeID", nodeID),
zap.Uint32("requestID", requestID),
zap.Stringer("blkID", blkID),
zap.Error(err),
)
return nil
}
// Respond to the validator with the fetched block and the same requestID.
gh.sender.SendPut(ctx, nodeID, requestID, blk.Bytes())
return nil
}