-
Notifications
You must be signed in to change notification settings - Fork 175
/
core.go
390 lines (338 loc) · 13.4 KB
/
core.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// (c) 2019 Dapper Labs - ALL RIGHTS RESERVED
package compliance
import (
"context"
"errors"
"fmt"
"time"
"github.com/rs/zerolog"
"go.opentelemetry.io/otel/attribute"
"github.com/onflow/flow-go/consensus/hotstuff"
"github.com/onflow/flow-go/consensus/hotstuff/model"
"github.com/onflow/flow-go/engine"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/model/messages"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/module/compliance"
"github.com/onflow/flow-go/module/metrics"
"github.com/onflow/flow-go/module/trace"
"github.com/onflow/flow-go/state"
"github.com/onflow/flow-go/state/protocol"
"github.com/onflow/flow-go/storage"
"github.com/onflow/flow-go/utils/logging"
)
// Core contains the central business logic for the main consensus' compliance engine.
// It is responsible for handling communication for the embedded consensus algorithm.
// NOTE: Core is designed to be non-thread safe and cannot be used in concurrent environment
// user of this object needs to ensure single thread access.
type Core struct {
log zerolog.Logger // used to log relevant actions with context
config compliance.Config
metrics module.EngineMetrics
tracer module.Tracer
mempool module.MempoolMetrics
complianceMetrics module.ComplianceMetrics
cleaner storage.Cleaner
headers storage.Headers
payloads storage.Payloads
state protocol.MutableState
pending module.PendingBlockBuffer // pending block cache
sync module.BlockRequester
hotstuff module.HotStuff
voteAggregator hotstuff.VoteAggregator
}
// NewCore instantiates the business logic for the main consensus' compliance engine.
func NewCore(
log zerolog.Logger,
collector module.EngineMetrics,
tracer module.Tracer,
mempool module.MempoolMetrics,
complianceMetrics module.ComplianceMetrics,
cleaner storage.Cleaner,
headers storage.Headers,
payloads storage.Payloads,
state protocol.MutableState,
pending module.PendingBlockBuffer,
sync module.BlockRequester,
voteAggregator hotstuff.VoteAggregator,
opts ...compliance.Opt,
) (*Core, error) {
config := compliance.DefaultConfig()
for _, apply := range opts {
apply(&config)
}
e := &Core{
log: log.With().Str("compliance", "core").Logger(),
config: config,
metrics: collector,
tracer: tracer,
mempool: mempool,
complianceMetrics: complianceMetrics,
cleaner: cleaner,
headers: headers,
payloads: payloads,
state: state,
pending: pending,
sync: sync,
hotstuff: nil, // use `WithConsensus`
voteAggregator: voteAggregator,
}
e.mempool.MempoolEntries(metrics.ResourceProposal, e.pending.Size())
return e, nil
}
// OnBlockProposal handles incoming block proposals.
func (c *Core) OnBlockProposal(originID flow.Identifier, proposal *messages.BlockProposal, inBlockRangeResponse bool) error {
var traceID string
span, _, isSampled := c.tracer.StartBlockSpan(context.Background(), proposal.Header.ID(), trace.CONCompOnBlockProposal)
if isSampled {
span.SetAttributes(
attribute.Int64("view", int64(proposal.Header.View)),
attribute.String("origin_id", originID.String()),
attribute.String("proposer", proposal.Header.ProposerID.String()),
)
traceID = span.SpanContext().TraceID().String()
}
defer span.End()
header := proposal.Header
log := c.log.With().
Hex("origin_id", originID[:]).
Str("chain_id", header.ChainID.String()).
Uint64("block_height", header.Height).
Uint64("block_view", header.View).
Hex("block_id", logging.Entity(header)).
Hex("parent_id", header.ParentID[:]).
Hex("payload_hash", header.PayloadHash[:]).
Time("timestamp", header.Timestamp).
Hex("proposer", header.ProposerID[:]).
Hex("parent_signer_indices", header.ParentVoterIndices).
Str("traceID", traceID). // traceID is used to connect logs to traces
Logger()
log.Info().Msg("block proposal received")
// first, we reject all blocks that we don't need to process:
// 1) blocks already in the cache; they will already be processed later
// 2) blocks already on disk; they were processed and await finalization
// ignore proposals that are already cached
_, cached := c.pending.ByID(header.ID())
if cached {
log.Debug().Msg("skipping already cached proposal")
return nil
}
// ignore proposals that were already processed
_, err := c.headers.ByBlockID(header.ID())
if err == nil {
log.Debug().Msg("skipping already processed proposal")
return nil
}
if !errors.Is(err, storage.ErrNotFound) {
return fmt.Errorf("could not check proposal: %w", err)
}
// ignore proposals which are too far ahead of our local finalized state
// instead, rely on sync engine to catch up finalization more effectively, and avoid
// large subtree of blocks to be cached.
final, err := c.state.Final().Head()
if err != nil {
return fmt.Errorf("could not get latest finalized header: %w", err)
}
if header.Height > final.Height && header.Height-final.Height > c.config.SkipNewProposalsThreshold {
log.Debug().
Uint64("final_height", final.Height).
Msg("dropping block too far ahead of locally finalized height")
return nil
}
// there are two possibilities if the proposal is neither already pending
// processing in the cache, nor has already been processed:
// 1) the proposal is unverifiable because the parent is unknown
// => we cache the proposal
// 2) the proposal is connected to finalized state through an unbroken chain
// => we verify the proposal and forward it to hotstuff if valid
// if the parent is a pending block (disconnected from the incorporated state), we cache this block as well.
// we don't have to request its parent block or its ancestor again, because as a
// pending block, its parent block must have been requested.
// if there was problem requesting its parent or ancestors, the sync engine's forward
// syncing with range requests for finalized blocks will request for the blocks.
_, found := c.pending.ByID(header.ParentID)
if found {
// add the block to the cache
_ = c.pending.Add(originID, proposal)
c.mempool.MempoolEntries(metrics.ResourceProposal, c.pending.Size())
return nil
}
// if the proposal is connected to a block that is neither in the cache, nor
// in persistent storage, its direct parent is missing; cache the proposal
// and request the parent
_, err = c.headers.ByBlockID(header.ParentID)
if errors.Is(err, storage.ErrNotFound) {
_ = c.pending.Add(originID, proposal)
c.mempool.MempoolEntries(metrics.ResourceProposal, c.pending.Size())
log.Debug().Msg("requesting missing parent for proposal")
c.sync.RequestBlock(header.ParentID, header.Height-1)
return nil
}
if err != nil {
return fmt.Errorf("could not check parent: %w", err)
}
// At this point, we should be able to connect the proposal to the finalized
// state and should process it to see whether to forward to hotstuff or not.
// processBlockAndDescendants is a recursive function. Here we trace the
// execution of the entire recursion, which might include processing the
// proposal's pending children. There is another span within
// processBlockProposal that measures the time spent for a single proposal.
err = c.processBlockAndDescendants(proposal, inBlockRangeResponse)
c.mempool.MempoolEntries(metrics.ResourceProposal, c.pending.Size())
if err != nil {
return fmt.Errorf("could not process block proposal: %w", err)
}
// most of the heavy database checks are done at this point, so this is a
// good moment to potentially kick-off a garbage collection of the DB
// NOTE: this is only effectively run every 1000th calls, which corresponds
// to every 1000th successfully processed block
c.cleaner.RunGC()
return nil
}
// processBlockAndDescendants is a recursive function that processes a block and
// its pending proposals for its children. By induction, any children connected
// to a valid proposal are validly connected to the finalized state and can be
// processed as well.
func (c *Core) processBlockAndDescendants(proposal *messages.BlockProposal, inRangeBlockResponse bool) error {
blockID := proposal.Header.ID()
// process block itself
err := c.processBlockProposal(proposal, inRangeBlockResponse)
// child is outdated by the time we started processing it
// => node was probably behind and is catching up. Log as warning
if engine.IsOutdatedInputError(err) {
c.log.Info().Msg("dropped processing of abandoned fork; this might be an indicator that the node is slightly behind")
return nil
}
// the block is invalid; log as error as we desire honest participation
// ToDo: potential slashing
if engine.IsInvalidInputError(err) {
c.log.Warn().
Err(err).
Bool(logging.KeySuspicious, true).
Msg("received invalid block from other node (potential slashing evidence?)")
return nil
}
if err != nil {
// unexpected error: potentially corrupted internal state => abort processing and escalate error
return fmt.Errorf("failed to process block %x: %w", blockID, err)
}
// process all children
// do not break on invalid or outdated blocks as they should not prevent us
// from processing other valid children
children, has := c.pending.ByParentID(blockID)
if !has {
return nil
}
for _, child := range children {
childProposal := &messages.BlockProposal{
Header: child.Header,
Payload: child.Payload,
}
cpr := c.processBlockAndDescendants(childProposal, inRangeBlockResponse)
if cpr != nil {
// unexpected error: potentially corrupted internal state => abort processing and escalate error
return cpr
}
}
// drop all of the children that should have been processed now
c.pending.DropForParent(blockID)
return nil
}
// processBlockProposal processes the given block proposal. The proposal must connect to
// the finalized state.
func (c *Core) processBlockProposal(proposal *messages.BlockProposal, inRangeBlockResponse bool) error {
startTime := time.Now()
defer c.complianceMetrics.BlockProposalDuration(time.Since(startTime))
span, ctx, isSampled := c.tracer.StartBlockSpan(context.Background(), proposal.Header.ID(), trace.ConCompProcessBlockProposal)
if isSampled {
span.SetAttributes(
attribute.String("proposer", proposal.Header.ProposerID.String()),
)
}
defer span.End()
header := proposal.Header
log := c.log.With().
Str("chain_id", header.ChainID.String()).
Uint64("block_height", header.Height).
Uint64("block_view", header.View).
Hex("block_id", logging.Entity(header)).
Hex("parent_id", header.ParentID[:]).
Hex("payload_hash", header.PayloadHash[:]).
Time("timestamp", header.Timestamp).
Hex("proposer", header.ProposerID[:]).
Hex("parent_signer_indices", header.ParentVoterIndices).
Logger()
log.Info().Msg("processing block proposal")
// see if the block is a valid extension of the protocol state
block := &flow.Block{
Header: proposal.Header,
Payload: proposal.Payload,
}
err := c.state.Extend(ctx, block)
// if the block proposes an invalid extension of the protocol state, then the block is invalid
if state.IsInvalidExtensionError(err) {
return engine.NewInvalidInputErrorf("invalid extension of protocol state (block: %x, height: %d): %w",
header.ID(), header.Height, err)
}
// protocol state aborted processing of block as it is on an abandoned fork: block is outdated
if state.IsOutdatedExtensionError(err) {
return engine.NewOutdatedInputErrorf("outdated extension of protocol state: %w", err)
}
if err != nil {
return fmt.Errorf("could not extend protocol state (block: %x, height: %d): %w", header.ID(), header.Height, err)
}
// retrieve the parent
parent, err := c.headers.ByBlockID(header.ParentID)
if err != nil {
return fmt.Errorf("could not retrieve proposal parent: %w", err)
}
// submit the model to hotstuff for processing
log.Info().Msg("forwarding block proposal to hotstuff")
// when the block is in range response, we should wait for hotstuff to finish processing the block,
// otherwise processing the next block might fail because the current block hasn't been added
// to protocol state yet.
if inRangeBlockResponse {
select {
case <-c.hotstuff.SubmitProposal(header, parent.View):
break
case <-c.hotstuff.Done():
break
}
} else {
c.hotstuff.SubmitProposal(header, parent.View)
}
return nil
}
// OnBlockVote handles incoming block votes.
func (c *Core) OnBlockVote(originID flow.Identifier, vote *messages.BlockVote) error {
span, _, isSampled := c.tracer.StartBlockSpan(context.Background(), vote.BlockID, trace.CONCompOnBlockVote)
if isSampled {
span.SetAttributes(
attribute.String("origin_id", originID.String()),
)
}
defer span.End()
v := &model.Vote{
View: vote.View,
BlockID: vote.BlockID,
SignerID: originID,
SigData: vote.SigData,
}
c.log.Info().
Uint64("block_view", vote.View).
Hex("block_id", vote.BlockID[:]).
Hex("voter", v.SignerID[:]).
Str("vote_id", v.ID().String()).
Msg("block vote received, forwarding block vote to hotstuff vote aggregator")
// forward the vote to hotstuff for processing
c.voteAggregator.AddVote(v)
return nil
}
// ProcessFinalizedView performs pruning of stale data based on finalization event
// removes pending blocks below the finalized view
func (c *Core) ProcessFinalizedView(finalizedView uint64) {
// remove all pending blocks at or below the finalized view
c.pending.PruneByView(finalizedView)
// always record the metric
c.mempool.MempoolEntries(metrics.ResourceProposal, c.pending.Size())
}