-
Notifications
You must be signed in to change notification settings - Fork 210
/
blocks.go
314 lines (265 loc) · 8.55 KB
/
blocks.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package blocks
import (
"context"
"errors"
"fmt"
"time"
peer "github.com/libp2p/go-libp2p-core/peer"
"github.com/prometheus/client_golang/prometheus"
"github.com/spacemeshos/go-spacemesh/blocks/metrics"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/p2p/pubsub"
"github.com/spacemeshos/go-spacemesh/system"
)
// NewBlockProtocol is the protocol indicator for gossip blocks.
const NewBlockProtocol = "newBlock"
var (
errDupTx = errors.New("duplicate TransactionID in block")
errDupAtx = errors.New("duplicate ATXID in block")
errNoActiveSet = errors.New("block does not declare active set")
errZeroActiveSet = errors.New("block declares empty active set")
errConflictingExceptions = errors.New("conflicting exceptions")
errExceptionsOverlow = errors.New("too many exceptions")
)
type mesh interface {
GetBlock(types.BlockID) (*types.Block, error)
AddBlockWithTxs(context.Context, *types.Block) error
}
type blockValidator interface {
BlockSignedAndEligible(block *types.Block) (bool, error)
}
// BlockHandler is the struct responsible for storing meta data needed to process blocks from gossip.
type BlockHandler struct {
logger log.Log
cfg Config
fetcher system.Fetcher
mesh mesh
validator blockValidator
}
// Config defines configuration for block handler.
type Config struct {
MaxExceptions int
}
// defaultConfig for BlockHandler.
func defaultConfig() Config {
return Config{
MaxExceptions: 1000,
}
}
// Opt for configuring BlockHandler.
type Opt func(b *BlockHandler)
// WithMaxExceptions defines max allowed exceptions in a block.
func WithMaxExceptions(max int) Opt {
return func(b *BlockHandler) {
b.cfg.MaxExceptions = max
}
}
// WithLogger defines logger for BlockHandler.
func WithLogger(logger log.Log) Opt {
return func(b *BlockHandler) {
b.logger = logger
}
}
// NewBlockHandler creates new BlockHandler.
func NewBlockHandler(fetcher system.Fetcher, m mesh, v blockValidator, opts ...Opt) *BlockHandler {
b := &BlockHandler{
logger: log.NewNop(),
cfg: defaultConfig(),
fetcher: fetcher,
mesh: m,
validator: v,
}
for _, opt := range opts {
opt(b)
}
return b
}
// HandleBlock handles blocks from gossip.
func (bh *BlockHandler) HandleBlock(ctx context.Context, _ peer.ID, msg []byte) pubsub.ValidationResult {
if err := bh.HandleBlockData(ctx, msg); err != nil {
bh.logger.WithContext(ctx).With().Warning("error handling block data", log.Err(err))
return pubsub.ValidationIgnore
}
return pubsub.ValidationAccept
}
// HandleBlockData handles blocks from gossip and sync.
func (bh *BlockHandler) HandleBlockData(ctx context.Context, data []byte) error {
logger := bh.logger.WithContext(ctx)
logger.Info("handling data for new block")
start := time.Now()
var blk types.Block
if err := types.BytesToInterface(data, &blk); err != nil {
logger.With().Error("received invalid block", log.Err(err))
return fmt.Errorf("malformed block %w", err)
}
// set the block id when received
blk.Initialize()
logger = logger.WithFields(blk.ID(), blk.Layer())
// check if known
if _, err := bh.mesh.GetBlock(blk.ID()); err == nil {
logger.Info("received known block")
return nil
}
logger.With().Info("got new block", blk.Fields()...)
if err := bh.blockSyntacticValidation(ctx, &blk); err != nil {
return fmt.Errorf("failed to validate block %w", err)
}
saveMetrics(blk)
if err := bh.mesh.AddBlockWithTxs(ctx, &blk); err != nil {
return fmt.Errorf("adding block %s: %w", blk.ID(), err)
}
logger.With().Debug("time to process block", log.Duration("duration", time.Since(start)))
return nil
}
func saveMetrics(blk types.Block) {
type metric struct {
hist prometheus.Observer
value float64
}
metricList := []metric{
{
hist: metrics.LayerBlockSize.WithLabelValues(),
value: float64(len(blk.Bytes())),
},
{
hist: metrics.NumTxsInBlock.WithLabelValues(),
value: float64(len(blk.TxIDs)),
},
{
hist: metrics.BaseBlockExceptionLength.With(prometheus.Labels{metrics.DiffTypeLabel: metrics.DiffTypeFor}),
value: float64(len(blk.ForDiff)),
},
{
hist: metrics.BaseBlockExceptionLength.With(prometheus.Labels{metrics.DiffTypeLabel: metrics.DiffTypeNeutral}),
value: float64(len(blk.NeutralDiff)),
},
{
hist: metrics.BaseBlockExceptionLength.With(prometheus.Labels{metrics.DiffTypeLabel: metrics.DiffTypeAgainst}),
value: float64(len(blk.AgainstDiff)),
},
}
for _, m := range metricList {
m.hist.Observe(m.value)
}
}
func blockDependencies(blk *types.Block) []types.BlockID {
combined := []types.BlockID{blk.BaseBlock}
combined = append(combined, blk.ForDiff...)
combined = append(combined, blk.AgainstDiff...)
combined = append(combined, blk.NeutralDiff...)
return combined
}
func validateExceptions(block *types.Block, max int) error {
exceptions := map[types.BlockID]struct{}{}
for _, diff := range [][]types.BlockID{block.ForDiff, block.NeutralDiff, block.AgainstDiff} {
for _, bid := range diff {
_, exist := exceptions[bid]
if exist {
return fmt.Errorf("%w: block %s is referenced multiple times in exceptions of block %s",
errConflictingExceptions, bid, block.ID())
}
exceptions[bid] = struct{}{}
}
}
if len(exceptions) > max {
return fmt.Errorf("%w: %d exceptions with max allowed %d in blocks %s",
errExceptionsOverlow, len(exceptions), max, block.ID())
}
return nil
}
func (bh BlockHandler) blockSyntacticValidation(ctx context.Context, block *types.Block) error {
// Add layer to context, for logging purposes, since otherwise the context will be lost here below
if reqID, ok := log.ExtractRequestID(ctx); ok {
ctx = log.WithRequestID(ctx, reqID, block.Layer())
}
bh.logger.WithContext(ctx).With().Debug("syntactically validating block", block.ID())
if err := validateExceptions(block, bh.cfg.MaxExceptions); err != nil {
return err
}
// if there is a reference block - first validate it
if block.RefBlock != nil {
err := bh.fetcher.FetchBlock(ctx, *block.RefBlock)
if err != nil {
return fmt.Errorf("failed to fetch ref block %v e: %v", *block.RefBlock, err)
}
}
// try fetch referenced ATXs
err := bh.fetchAllReferencedAtxs(ctx, block)
if err != nil {
return fmt.Errorf("fetch all referenced ATXs: %w", err)
}
// fast validation checks if there are no duplicate ATX in active set and no duplicate TXs as well
if err := bh.fastValidation(block); err != nil {
return fmt.Errorf("fast validation: %w", err)
}
// get the TXs
if len(block.TxIDs) > 0 {
err := bh.fetcher.GetTxs(ctx, block.TxIDs)
if err != nil {
return fmt.Errorf("failed to fetch txs %v e: %v", block.ID(), err)
}
}
// get and validate blocks views using the fetch
err = bh.fetcher.GetBlocks(ctx, blockDependencies(block))
if err != nil {
return fmt.Errorf("failed to fetch view %v e: %v", block.ID(), err)
}
bh.logger.WithContext(ctx).With().Debug("validation done: block is syntactically valid", block.ID())
return nil
}
func (bh *BlockHandler) fetchAllReferencedAtxs(ctx context.Context, blk *types.Block) error {
bh.logger.WithContext(ctx).With().Debug("block handler fetching all atxs referenced by block", blk.ID())
// As block with empty or Golden ATXID is considered syntactically invalid, explicit check is not needed here.
atxs := []types.ATXID{blk.ATXID}
if blk.ActiveSet != nil {
if len(*blk.ActiveSet) > 0 {
atxs = append(atxs, *blk.ActiveSet...)
} else {
return errZeroActiveSet
}
} else {
if blk.RefBlock == nil {
return errNoActiveSet
}
}
if len(atxs) > 0 {
if err := bh.fetcher.GetAtxs(ctx, atxs); err != nil {
return fmt.Errorf("get ATXs: %w", err)
}
return nil
}
return nil
}
func (bh *BlockHandler) fastValidation(block *types.Block) error {
// block eligibility
if eligible, err := bh.validator.BlockSignedAndEligible(block); err != nil || !eligible {
return fmt.Errorf("block eligibility check failed: %w", err)
}
// validate unique tx atx
if err := validateUniqueTxAtx(block); err != nil {
return fmt.Errorf("validate unique tx ATX: %w", err)
}
return nil
}
func validateUniqueTxAtx(b *types.Block) error {
// check for duplicate tx id
mt := make(map[types.TransactionID]struct{}, len(b.TxIDs))
for _, tx := range b.TxIDs {
if _, exist := mt[tx]; exist {
return errDupTx
}
mt[tx] = struct{}{}
}
// check for duplicate atx id
if b.ActiveSet != nil {
ma := make(map[types.ATXID]struct{}, len(*b.ActiveSet))
for _, atx := range *b.ActiveSet {
if _, exist := ma[atx]; exist {
return errDupAtx
}
ma[atx] = struct{}{}
}
}
return nil
}