-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathfinalization.go
More file actions
368 lines (317 loc) · 11.2 KB
/
Copy pathfinalization.go
File metadata and controls
368 lines (317 loc) · 11.2 KB
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
package roothash
import (
"fmt"
"github.com/oasisprotocol/oasis-core/go/common"
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
"github.com/oasisprotocol/oasis-core/go/common/logging"
"github.com/oasisprotocol/oasis-core/go/common/node"
tmapi "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/api"
registryState "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/apps/registry/state"
roothashApi "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/apps/roothash/api"
roothashState "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/apps/roothash/state"
registry "github.com/oasisprotocol/oasis-core/go/registry/api"
roothash "github.com/oasisprotocol/oasis-core/go/roothash/api"
"github.com/oasisprotocol/oasis-core/go/roothash/api/block"
"github.com/oasisprotocol/oasis-core/go/roothash/api/commitment"
staking "github.com/oasisprotocol/oasis-core/go/staking/api"
)
func (app *Application) tryFinalizeRounds(
ctx *tmapi.Context,
) error {
for _, runtimeID := range roothashApi.RuntimesToFinalize(ctx) {
if err := app.tryFinalizeRound(ctx, runtimeID, false); err != nil {
ctx.Logger().Error("failed to finalize block",
"err", err,
)
return err
}
}
return nil
}
func (app *Application) tryFinalizeRound(
ctx *tmapi.Context,
runtimeID common.Namespace,
timeout bool,
) error {
ctx = ctx.NewTransaction()
defer ctx.Close()
state := roothashState.NewMutableState(ctx.State())
// Fetch runtime state.
rtState, err := app.getRuntimeState(ctx, state, runtimeID)
if err != nil {
return fmt.Errorf("failed to get runtime state: %w", err)
}
// Finalize round.
if err = app.tryFinalizeRoundInsideTx(ctx, rtState, timeout); err != nil {
return err
}
// Update runtime state.
if err := state.SetRuntimeState(ctx, rtState); err != nil {
return fmt.Errorf("failed to set runtime state: %w", err)
}
ctx.Commit()
return nil
}
func (app *Application) tryFinalizeRoundInsideTx( //nolint: gocyclo
ctx *tmapi.Context,
rtState *roothash.RuntimeState,
timeout bool,
) error {
round := rtState.LastBlock.Header.Round + 1
pool := rtState.CommitmentPool
// Initialize per-epoch liveness statistics.
if rtState.LivenessStatistics == nil {
rtState.LivenessStatistics = roothash.NewLivenessStatistics(len(rtState.Committee.Members))
}
livenessStats := rtState.LivenessStatistics
sc, err := pool.ProcessCommitments(rtState.Committee, rtState.Runtime.Executor.AllowedStragglers, timeout)
switch err {
case commitment.ErrDiscrepancyDetected:
ctx.Logger().Warn("executor discrepancy detected",
"runtime_id", rtState.Runtime.ID,
"round", round,
"rank", rtState.CommitmentPool.HighestRank,
"timeout", timeout,
logging.LogEvent, roothash.LogEventExecutionDiscrepancyDetected,
)
ctx.EmitEvent(
tmapi.NewEventBuilder(app.Name()).
TypedAttribute(&roothash.ExecutionDiscrepancyDetectedEvent{
Round: round,
Rank: rtState.CommitmentPool.HighestRank,
Timeout: timeout,
}).
TypedAttribute(&roothash.RuntimeIDAttribute{ID: rtState.Runtime.ID}),
)
// Re-arm round timeout. Give backup workers enough time to submit commitments.
prevTimeout := rtState.NextTimeout
rtState.NextTimeout = ctx.CurrentHeight() + (rtState.Runtime.Executor.RoundTimeout*backupWorkerTimeoutFactorNumerator)/backupWorkerTimeoutFactorDenominator
if err = rearmRoundTimeout(ctx, rtState.Runtime.ID, round, prevTimeout, rtState.NextTimeout); err != nil {
return err
}
// Update the timeout flag to correctly handle the case when the round timeout is set to 0.
timeout = rtState.NextTimeout == ctx.CurrentHeight()
// Retry as we may be able to already perform discrepancy resolution.
sc, err = pool.ProcessCommitments(rtState.Committee, rtState.Runtime.Executor.AllowedStragglers, timeout)
}
switch err {
case nil:
// The round has been finalized.
case commitment.ErrStillWaiting:
// Need more commits.
ctx.Logger().Debug("insufficient commitments for finality, waiting",
"runtime_id", rtState.Runtime.ID,
"round", round,
)
return nil
case commitment.ErrNoSchedulerCommitment, commitment.ErrBadSchedulerCommitment:
// TODO: Consider slashing the primary scheduler for these offenses.
fallthrough
case commitment.ErrInsufficientVotes:
// Emit empty block and fail the round.
return app.failRound(ctx, rtState, err)
case commitment.ErrDiscrepancyDetected:
// This was already handled above, so it should not happen.
fallthrough
default:
return err
}
// The round has been finalized.
ctx.Logger().Debug("finalized round",
"runtime_id", rtState.Runtime.ID,
"round", round,
"rank", pool.HighestRank,
"scheduler_id", sc.Commitment.Header.SchedulerID,
"timeout", timeout,
)
livenessStats.TotalRounds++
// Record if the highest-ranked scheduler received enough commitments.
firstSchedulerIdx, ok := rtState.Committee.SchedulerIdx(round, 0)
if !ok {
// Should never happen.
return fmt.Errorf("failed to query primary scheduler, no workers in committee")
}
firstScheduler := rtState.Committee.Members[firstSchedulerIdx]
switch firstScheduler.PublicKey.Equal(sc.Commitment.Header.SchedulerID) {
case true:
livenessStats.FinalizedProposals[firstSchedulerIdx]++
case false:
livenessStats.MissedProposals[firstSchedulerIdx]++
}
state := roothashState.NewMutableState(ctx.State())
header := sc.Commitment.Header.Header
// Update the incoming message queue by removing processed messages. Do one final check to
// make sure that the processed messages actually correspond to the provided hash.
msgs, err := fetchRuntimeMessages(ctx, state, rtState.Runtime.ID, header.InMessagesCount)
if err != nil {
return err
}
if err = verifyRuntimeMessages(ctx, msgs, header.InMessagesHash); err != nil {
// TODO: All nodes contributing to this round should be penalized.
return app.failRound(ctx, rtState, err)
}
if err = app.removeRuntimeMessages(ctx, state, rtState.Runtime.ID, msgs, round); err != nil {
return err
}
msgEvents, err := app.processRuntimeMessages(ctx, rtState, sc.Commitment.Messages)
if err != nil {
return fmt.Errorf("failed to process runtime messages: %w", err)
}
// Compute good and bad entities.
var (
goodComputeEntities []signature.PublicKey
badComputeEntities []signature.PublicKey
)
seen := make(map[signature.PublicKey]struct{})
regState := registryState.NewMutableState(ctx.State())
schedulerVote := sc.Commitment.ToVote()
for i, n := range rtState.Committee.Members {
vote, ok := sc.Votes[n.PublicKey]
switch {
case !ok:
continue
case vote == nil:
// Skip failures.
continue
}
// Make sure to not include nodes in multiple roles multiple times.
if _, ok := seen[n.PublicKey]; ok {
continue
}
seen[n.PublicKey] = struct{}{}
// Resolve the entity owning the node.
var node *node.Node
node, err = regState.Node(ctx, n.PublicKey)
switch err {
case nil:
case registry.ErrNoSuchNode:
// This should never happen as nodes cannot disappear mid-epoch.
ctx.Logger().Error("runtime node not found by commitment signature public key",
"public_key", n.PublicKey,
)
continue
default:
ctx.Logger().Error("failed to get runtime node by commitment signature public key",
"public_key", n.PublicKey,
"err", err,
)
return fmt.Errorf("cometbft/roothash: getting node %s: %w", n.PublicKey, err)
}
// Determine whether the entity was good or bad.
switch vote.Equal(&schedulerVote) {
case true:
goodComputeEntities = append(goodComputeEntities, node.EntityID)
livenessStats.LiveRounds[i]++
case false:
badComputeEntities = append(badComputeEntities, node.EntityID)
}
}
// If there was a discrepancy, slash entities for incorrect results if configured.
switch rtState.CommitmentPool.Discrepancy {
case true:
ctx.Logger().Debug("executor pool discrepancy",
"runtime_id", rtState.Runtime.ID,
"round", round,
"slashing", rtState.Runtime.Staking.Slashing,
)
penalty, ok := rtState.Runtime.Staking.Slashing[staking.SlashRuntimeIncorrectResults]
if !ok || penalty.Amount.IsZero() {
break
}
// Slash for incorrect results.
if err = onRuntimeIncorrectResults(
ctx,
badComputeEntities,
goodComputeEntities,
rtState.Runtime,
&penalty.Amount,
); err != nil {
return fmt.Errorf("failed to slash for incorrect results: %w", err)
}
case false:
// No slashing needed.
}
// Set last normal round results.
results := roothash.RoundResults{
Messages: msgEvents,
GoodComputeEntities: goodComputeEntities,
BadComputeEntities: badComputeEntities,
}
if err = state.SetLastRoundResults(ctx, rtState.Runtime.ID, &results); err != nil {
return fmt.Errorf("failed to set last round results: %w", err)
}
// Generate the final block.
app.finalizeBlock(ctx, rtState, block.Normal, &sc.Commitment.Header.Header)
if err := resetCommitments(ctx, rtState, false); err != nil {
return fmt.Errorf("failed to reset commitments: %w", err)
}
return nil
}
func (app *Application) finalizeBlock(ctx *tmapi.Context, rtState *roothash.RuntimeState, hdrType block.HeaderType, hdr *commitment.ComputeResultsHeader) {
// Generate a new block.
blk := block.NewEmptyBlock(rtState.LastBlock, uint64(ctx.Now().Unix()), hdrType)
switch hdrType {
case block.Normal:
blk.Header.IORoot = *hdr.IORoot
blk.Header.StateRoot = *hdr.StateRoot
blk.Header.MessagesHash = *hdr.MessagesHash
blk.Header.InMessagesHash = *hdr.InMessagesHash
}
// Hook up the new block.
rtState.LastBlock = blk
rtState.LastBlockHeight = ctx.CurrentHeight()
switch hdrType {
case block.Normal:
rtState.LastNormalRound = blk.Header.Round
rtState.LastNormalHeight = ctx.CurrentHeight()
}
// Emit event.
ctx.Logger().Debug("new runtime block",
"runtime_id", rtState.Runtime.ID,
"height", ctx.CurrentHeight(),
"round", blk.Header.Round,
"type", blk.Header.HeaderType,
"time", blk.Header.Timestamp,
)
ctx.EmitEvent(
tmapi.NewEventBuilder(app.Name()).
TypedAttribute(&roothash.FinalizedEvent{Round: blk.Header.Round}).
TypedAttribute(&roothash.RuntimeIDAttribute{ID: rtState.Runtime.ID}),
)
}
func resetCommitments(ctx *tmapi.Context, rtState *roothash.RuntimeState, suspended bool) error {
// Reset scheduler commitments.
if suspended {
rtState.CommitmentPool = nil
} else {
rtState.CommitmentPool = commitment.NewPool()
}
// Re-arm round timeout. Give schedulers unlimited time to submit commitments.
prevTimeout := rtState.NextTimeout
rtState.NextTimeout = roothash.TimeoutNever
return rearmRoundTimeout(ctx, rtState.Runtime.ID, rtState.LastBlock.Header.Round, prevTimeout, rtState.NextTimeout)
}
func (app *Application) failRound(
ctx *tmapi.Context,
rtState *roothash.RuntimeState,
err error,
) error {
round := rtState.LastBlock.Header.Round + 1
ctx.Logger().Debug("round failed",
"runtime_id", rtState.Runtime.ID,
"round", round,
"err", err,
logging.LogEvent, roothash.LogEventRoundFailed,
)
// Record that the scheduler did not receive enough commitments.
firstSchedulerIdx, ok := rtState.Committee.SchedulerIdx(round, 0)
if !ok {
return fmt.Errorf("failed to query primary scheduler, no workers in committee")
}
rtState.LivenessStatistics.MissedProposals[firstSchedulerIdx]++
app.finalizeBlock(ctx, rtState, block.RoundFailed, nil)
if err := resetCommitments(ctx, rtState, false); err != nil {
return fmt.Errorf("failed to reset commitments: %w", err)
}
return nil
}