/
roothash.go
830 lines (719 loc) · 27.2 KB
/
roothash.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
// Package roothash implements the roothash application.
package roothash
import (
"fmt"
"github.com/tendermint/tendermint/abci/types"
beacon "github.com/oasisprotocol/oasis-core/go/beacon/api"
"github.com/oasisprotocol/oasis-core/go/common"
"github.com/oasisprotocol/oasis-core/go/common/cbor"
"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"
"github.com/oasisprotocol/oasis-core/go/consensus/api/transaction"
tmapi "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/api"
registryApi "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/registry/api"
registryState "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/registry/state"
roothashApi "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/roothash/api"
roothashState "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/roothash/state"
schedulerapp "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/scheduler"
schedulerApi "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/scheduler/api"
schedulerState "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/scheduler/state"
stakingapp "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/staking"
stakingState "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/staking/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"
"github.com/oasisprotocol/oasis-core/go/roothash/api/message"
scheduler "github.com/oasisprotocol/oasis-core/go/scheduler/api"
staking "github.com/oasisprotocol/oasis-core/go/staking/api"
)
var _ tmapi.Application = (*rootHashApplication)(nil)
type rootHashApplication struct {
state tmapi.ApplicationState
md tmapi.MessageDispatcher
}
func (app *rootHashApplication) Name() string {
return AppName
}
func (app *rootHashApplication) ID() uint8 {
return AppID
}
func (app *rootHashApplication) Methods() []transaction.MethodName {
return roothash.Methods
}
func (app *rootHashApplication) Blessed() bool {
return false
}
func (app *rootHashApplication) Dependencies() []string {
return []string{schedulerapp.AppName, stakingapp.AppName}
}
func (app *rootHashApplication) OnRegister(state tmapi.ApplicationState, md tmapi.MessageDispatcher) {
app.state = state
app.md = md
// Subscribe to messages emitted by other apps.
md.Subscribe(registryApi.MessageNewRuntimeRegistered, app)
md.Subscribe(registryApi.MessageRuntimeUpdated, app)
md.Subscribe(registryApi.MessageRuntimeResumed, app)
md.Subscribe(roothashApi.RuntimeMessageNoop, app)
md.Subscribe(schedulerApi.MessageBeforeSchedule, app)
}
func (app *rootHashApplication) OnCleanup() {
}
func (app *rootHashApplication) BeginBlock(ctx *tmapi.Context, request types.RequestBeginBlock) error {
// Check if rescheduling has taken place.
rescheduled := ctx.HasEvent(schedulerapp.AppName, &scheduler.ElectedEvent{})
// Check if there was an epoch transition.
epochChanged, epoch := app.state.EpochChanged(ctx)
state := roothashState.NewMutableState(ctx.State())
switch {
case epochChanged, rescheduled:
return app.onCommitteeChanged(ctx, state, epoch)
}
return nil
}
func (app *rootHashApplication) onCommitteeChanged(ctx *tmapi.Context, state *roothashState.MutableState, epoch beacon.EpochTime) error {
schedState := schedulerState.NewMutableState(ctx.State())
regState := registryState.NewMutableState(ctx.State())
runtimes, _ := regState.Runtimes(ctx)
params, err := state.ConsensusParameters(ctx)
if err != nil {
return fmt.Errorf("failed to get consensus parameters: %w", err)
}
var stakeAcc *stakingState.StakeAccumulatorCache
if !params.DebugBypassStake {
stakeAcc, err = stakingState.NewStakeAccumulatorCache(ctx)
if err != nil {
return fmt.Errorf("failed to create stake accumulator cache: %w", err)
}
defer stakeAcc.Discard()
}
for _, rt := range runtimes {
if !rt.IsCompute() {
ctx.Logger().Debug("skipping non-compute runtime",
"runtime", rt.ID,
)
continue
}
rtState, err := state.RuntimeState(ctx, rt.ID)
if err != nil {
return fmt.Errorf("failed to fetch runtime state: %w", err)
}
// Expire past evidence of runtime node misbehaviour.
if rtState.CurrentBlock != nil {
if round := rtState.CurrentBlock.Header.Round; round > params.MaxEvidenceAge {
ctx.Logger().Debug("removing expired runtime evidence",
"runtime", rt.ID,
"round", round,
"max_evidence_age", params.MaxEvidenceAge,
)
if err = state.RemoveExpiredEvidence(ctx, rt.ID, round-params.MaxEvidenceAge); err != nil {
return fmt.Errorf("failed to remove expired runtime evidence: %s %w", rt.ID, err)
}
}
}
// Since the runtime is in the list of active runtimes in the registry we
// can safely clear the suspended flag.
rtState.Suspended = false
// Prepare new runtime committees based on what the scheduler did.
executorPool, empty, err := app.prepareNewCommittees(ctx, epoch, rtState, schedState, regState)
if err != nil {
return err
}
// If there are no committees for this runtime, suspend the runtime as this
// means that there is noone to pay the maintenance fees.
//
// Also suspend the runtime in case the registering entity no longer has enough stake to
// cover the entity and runtime deposits (this check is skipped if the runtime would be
// suspended anyway due to nobody being there to pay maintenance fees).
sufficientStake := true
if !empty && !params.DebugBypassStake && rt.GovernanceModel != registry.GovernanceConsensus {
acctAddr := rt.StakingAddress()
if acctAddr == nil {
// This should never happen.
ctx.Logger().Error("unknown runtime governance model",
"rt_id", rt.ID,
"gov_model", rt.GovernanceModel,
)
return fmt.Errorf("unknown runtime governance model on runtime %s: %s", rt.ID, rt.GovernanceModel)
}
if err = stakeAcc.CheckStakeClaims(*acctAddr); err != nil {
ctx.Logger().Warn("insufficient stake for runtime operation",
"err", err,
"entity", rt.EntityID,
"account", *acctAddr,
)
sufficientStake = false
}
}
if (empty || !sufficientStake) && !params.DebugDoNotSuspendRuntimes {
if err = app.suspendUnpaidRuntime(ctx, rtState, regState); err != nil {
return err
}
}
// If the committee has actually changed, force a new round.
if !rtState.Suspended {
ctx.Logger().Debug("updating committee for runtime",
"runtime_id", rt.ID,
)
// Transition the round.
ctx.Logger().Debug("new committee, transitioning round",
"runtime_id", rt.ID,
"round", rtState.CurrentBlock.Header.Round,
)
// Emit an empty epoch transition block in the new round. This is required so that
// the clients can be sure what state is final when an epoch transition occurs.
if err = app.emitEmptyBlock(ctx, rtState, block.EpochTransition); err != nil {
return fmt.Errorf("failed to emit empty block: %w", err)
}
// Set the executor pool.
rtState.ExecutorPool = executorPool
rtState.ExecutorPool.Round = rtState.CurrentBlock.Header.Round
}
// Clear liveness statistics.
rtState.LivenessStatistics = nil
// Update the runtime descriptor to the latest per-epoch value.
rtState.Runtime = rt
if err = state.SetRuntimeState(ctx, rtState); err != nil {
return fmt.Errorf("failed to set runtime state: %w", err)
}
}
return nil
}
func (app *rootHashApplication) suspendUnpaidRuntime(
ctx *tmapi.Context,
rtState *roothash.RuntimeState,
regState *registryState.MutableState,
) error {
ctx.Logger().Warn("maintenance fees not paid for runtime or owner debonded, suspending",
"runtime_id", rtState.Runtime.ID,
)
if err := regState.SuspendRuntime(ctx, rtState.Runtime.ID); err != nil {
return err
}
// Emity an empty block signalling that the runtime was suspended.
if err := app.emitEmptyBlock(ctx, rtState, block.Suspended); err != nil {
return fmt.Errorf("failed to emit empty block: %w", err)
}
// Make sure to only reset the executor pool after any timeouts have been cleared as otherwise
// the emitEmptyBlock method will forget to clear them.
rtState.Suspended = true
rtState.ExecutorPool = nil
return nil
}
func (app *rootHashApplication) prepareNewCommittees(
ctx *tmapi.Context,
epoch beacon.EpochTime,
rtState *roothash.RuntimeState,
schedState *schedulerState.MutableState,
regState *registryState.MutableState,
) (
executorPool *commitment.Pool,
empty bool,
err error,
) {
rtID := rtState.Runtime.ID
executorPool = new(commitment.Pool)
executorCommittee, err := schedState.Committee(ctx, scheduler.KindComputeExecutor, rtID)
if err != nil {
ctx.Logger().Error("checkCommittees: failed to get executor committee from scheduler",
"err", err,
"runtime", rtID,
)
return
}
if executorCommittee == nil {
ctx.Logger().Warn("checkCommittees: no executor committee",
"runtime", rtID,
)
empty = true
} else {
executorPool = &commitment.Pool{
Runtime: rtState.Runtime,
Committee: executorCommittee,
}
}
return
}
func (app *rootHashApplication) emitEmptyBlock(ctx *tmapi.Context, runtime *roothash.RuntimeState, hdrType block.HeaderType) error {
blk := block.NewEmptyBlock(runtime.CurrentBlock, uint64(ctx.Now().Unix()), hdrType)
runtime.CurrentBlock = blk
runtime.CurrentBlockHeight = ctx.BlockHeight() + 1 // Current height is ctx.BlockHeight() + 1
// Do not update LastNormal{Round,Height} as empty blocks are not emitted by the runtime.
if runtime.ExecutorPool != nil {
// Clear timeout if there was one scheduled.
if runtime.ExecutorPool.NextTimeout != commitment.TimeoutNever {
state := roothashState.NewMutableState(ctx.State())
if err := state.ClearRoundTimeout(ctx, runtime.Runtime.ID, runtime.ExecutorPool.NextTimeout); err != nil {
return fmt.Errorf("failed to clear round timeout: %w", err)
}
}
runtime.ExecutorPool.ResetCommitments(blk.Header.Round)
}
ctx.EmitEvent(
tmapi.NewEventBuilder(app.Name()).
TypedAttribute(&roothash.FinalizedEvent{Round: blk.Header.Round}).
TypedAttribute(&roothash.RuntimeIDAttribute{ID: runtime.Runtime.ID}),
)
return nil
}
func (app *rootHashApplication) ExecuteMessage(ctx *tmapi.Context, kind, msg interface{}) (interface{}, error) {
switch kind {
case registryApi.MessageNewRuntimeRegistered:
// A new runtime has been registered.
if ctx.IsInitChain() {
// Ignore messages emitted during InitChain as we handle these separately.
return nil, nil
}
rt := msg.(*registry.Runtime)
ctx.Logger().Debug("ExecuteMessage: new runtime",
"runtime", rt.ID,
)
return nil, app.onNewRuntime(ctx, rt, nil, false)
case registryApi.MessageRuntimeUpdated:
// A runtime registration has been updated or a new runtime has been registered.
if ctx.IsInitChain() {
// Ignore messages emitted during InitChain as we handle these separately.
return nil, nil
}
return nil, app.verifyRuntimeUpdate(ctx, msg.(*registry.Runtime))
case registryApi.MessageRuntimeResumed:
// A previously suspended runtime has been resumed.
return nil, nil
case roothashApi.RuntimeMessageNoop:
// Noop message always succeeds.
return nil, nil
case schedulerApi.MessageBeforeSchedule:
// Scheduler is about to perform scheduling. Process liveness statistics to make sure they
// get taken into account for the next election.
epoch := msg.(beacon.EpochTime)
ctx.Logger().Debug("processing liveness statistics before scheduling",
"epoch", epoch,
)
state := roothashState.NewMutableState(ctx.State())
regState := registryState.NewMutableState(ctx.State())
runtimes, _ := regState.Runtimes(ctx)
for _, rt := range runtimes {
if !rt.IsCompute() {
continue
}
rtState, err := state.RuntimeState(ctx, rt.ID)
if err != nil {
return nil, fmt.Errorf("failed to fetch runtime state: %w", err)
}
if err = processLivenessStatistics(ctx, epoch, rtState); err != nil {
return nil, fmt.Errorf("failed to process liveness statistics for %s: %w", rt.ID, err)
}
}
return nil, nil
default:
return nil, roothash.ErrInvalidArgument
}
}
func (app *rootHashApplication) verifyRuntimeUpdate(ctx *tmapi.Context, rt *registry.Runtime) error {
state := roothashState.NewMutableState(ctx.State())
params, err := state.ConsensusParameters(ctx)
if err != nil {
return fmt.Errorf("failed to get consensus parameters: %w", err)
}
return roothash.VerifyRuntimeParameters(ctx.Logger(), rt, params)
}
func (app *rootHashApplication) ExecuteTx(ctx *tmapi.Context, tx *transaction.Transaction) error {
state := roothashState.NewMutableState(ctx.State())
switch tx.Method {
case roothash.MethodExecutorCommit:
var xc roothash.ExecutorCommit
if err := cbor.Unmarshal(tx.Body, &xc); err != nil {
return err
}
return app.executorCommit(ctx, state, &xc)
case roothash.MethodExecutorProposerTimeout:
var xc roothash.ExecutorProposerTimeoutRequest
if err := cbor.Unmarshal(tx.Body, &xc); err != nil {
return err
}
return app.executorProposerTimeout(ctx, state, &xc)
case roothash.MethodEvidence:
var ev roothash.Evidence
if err := cbor.Unmarshal(tx.Body, &ev); err != nil {
return err
}
return app.submitEvidence(ctx, state, &ev)
case roothash.MethodSubmitMsg:
var msg roothash.SubmitMsg
if err := cbor.Unmarshal(tx.Body, &msg); err != nil {
return err
}
return app.submitMsg(ctx, state, &msg)
default:
return roothash.ErrInvalidArgument
}
}
func (app *rootHashApplication) onNewRuntime(ctx *tmapi.Context, runtime *registry.Runtime, genesis *roothash.Genesis, suspended bool) error {
if !runtime.IsCompute() {
ctx.Logger().Warn("onNewRuntime: ignoring non-compute runtime",
"runtime", runtime,
)
return nil
}
// Check if state already exists for the given runtime.
state := roothashState.NewMutableState(ctx.State())
_, err := state.RuntimeState(ctx, runtime.ID)
switch err {
case nil:
ctx.Logger().Warn("onNewRuntime: state for runtime already exists",
"runtime", runtime,
)
return nil
case roothash.ErrInvalidRuntime:
// Runtime does not yet exist.
default:
return fmt.Errorf("failed to fetch runtime state: %w", err)
}
// Create genesis block.
now := ctx.Now().Unix()
genesisBlock := block.NewGenesisBlock(runtime.ID, uint64(now))
// Fill the Header fields with Genesis runtime states, if this was called during InitChain().
genesisBlock.Header.Round = runtime.Genesis.Round
genesisBlock.Header.StateRoot = runtime.Genesis.StateRoot
if ctx.IsInitChain() {
// NOTE: Outside InitChain the genesis argument will be nil.
if genesisRts := genesis.RuntimeStates[runtime.ID]; genesisRts != nil {
genesisBlock.Header.Round = genesisRts.Round
genesisBlock.Header.StateRoot = genesisRts.StateRoot
if suspended {
genesisBlock.Header.HeaderType = block.Suspended
}
err = state.SetLastRoundResults(ctx, runtime.ID, &roothash.RoundResults{
Messages: genesisRts.MessageResults,
})
if err != nil {
return fmt.Errorf("failed to set last round results: %w", err)
}
}
}
// Create new state containing the genesis block.
err = state.SetRuntimeState(ctx, &roothash.RuntimeState{
Runtime: runtime,
CurrentBlock: genesisBlock,
CurrentBlockHeight: ctx.BlockHeight() + 1, // Current height is ctx.BlockHeight() + 1
LastNormalRound: genesisBlock.Header.Round,
LastNormalHeight: ctx.BlockHeight() + 1, // Current height is ctx.BlockHeight() + 1
GenesisBlock: genesisBlock,
})
if err != nil {
return fmt.Errorf("failed to set runtime state: %w", err)
}
ctx.Logger().Debug("onNewRuntime: created genesis state for runtime",
"runtime", runtime,
)
ctx.EmitEvent(
tmapi.NewEventBuilder(app.Name()).
TypedAttribute(&roothash.FinalizedEvent{Round: genesisBlock.Header.Round}).
TypedAttribute(&roothash.RuntimeIDAttribute{ID: runtime.ID}),
)
return nil
}
func (app *rootHashApplication) EndBlock(ctx *tmapi.Context, request types.RequestEndBlock) (types.ResponseEndBlock, error) {
state := roothashState.NewMutableState(ctx.State())
// Check if any runtimes require round timeouts to expire.
roundTimeouts, err := state.RuntimesWithRoundTimeouts(ctx, ctx.BlockHeight())
if err != nil {
return types.ResponseEndBlock{}, fmt.Errorf("failed to fetch runtimes with round timeouts: %w", err)
}
for _, runtimeID := range roundTimeouts {
if err = app.processRoundTimeout(ctx, state, runtimeID); err != nil {
return types.ResponseEndBlock{}, fmt.Errorf("failed to process round timeout: %w", err)
}
}
return types.ResponseEndBlock{}, nil
}
func (app *rootHashApplication) processRoundTimeout(ctx *tmapi.Context, state *roothashState.MutableState, runtimeID common.Namespace) error {
ctx.Logger().Warn("round timeout expired, forcing finalization",
logging.LogEvent, roothash.LogEventTimerFired,
)
rtState, err := state.RuntimeState(ctx, runtimeID)
if err != nil {
return fmt.Errorf("failed to get runtime state: %w", err)
}
if rtState.ExecutorPool == nil {
// This should NEVER happen as the timeout should be cleared before the pool is reset.
ctx.Logger().Error("no executor pool",
"runtime_id", runtimeID,
)
return fmt.Errorf("no executor pool")
}
if !rtState.ExecutorPool.IsTimeout(ctx.BlockHeight()) {
// This should NEVER happen.
ctx.Logger().Error("no scheduled timeout",
"runtime_id", runtimeID,
"height", ctx.BlockHeight(),
"next_timeout", rtState.ExecutorPool.NextTimeout,
)
return fmt.Errorf("no scheduled timeout")
}
if err = app.tryFinalizeBlock(ctx, rtState, true); err != nil {
ctx.Logger().Error("failed to finalize block",
"err", err,
)
return fmt.Errorf("failed to finalize block: %w", err)
}
return nil
}
// tryFinalizeExecutorCommits tries to finalize the executor commitments into a new runtime block.
// The caller must take care of clearing and scheduling the round timeouts.
func (app *rootHashApplication) tryFinalizeExecutorCommits( //nolint: gocyclo
ctx *tmapi.Context,
rtState *roothash.RuntimeState,
forced bool,
) error {
runtime := rtState.Runtime
round := rtState.CurrentBlock.Header.Round + 1
pool := rtState.ExecutorPool
// Initialize per-epoch liveness statistics.
if rtState.LivenessStatistics == nil {
rtState.LivenessStatistics = roothash.NewLivenessStatistics(len(pool.Committee.Members))
}
livenessStats := rtState.LivenessStatistics
commit, err := pool.TryFinalize(ctx.BlockHeight(), runtime.Executor.RoundTimeout, forced, true)
if err == commitment.ErrDiscrepancyDetected {
ctx.Logger().Warn("executor discrepancy detected",
"round", round,
logging.LogEvent, roothash.LogEventExecutionDiscrepancyDetected,
)
ctx.EmitEvent(
tmapi.NewEventBuilder(app.Name()).
TypedAttribute(&roothash.ExecutionDiscrepancyDetectedEvent{Timeout: forced}).
TypedAttribute(&roothash.RuntimeIDAttribute{ID: runtime.ID}),
)
// We may also be able to already perform discrepancy resolution, check if this is possible
// by retrying finalization. We must make sure to not affect the computed timeout.
nextTimeout := pool.NextTimeout
commit, err = pool.TryFinalize(ctx.BlockHeight(), runtime.Executor.RoundTimeout, false, false)
pool.NextTimeout = nextTimeout
}
switch err {
case nil:
// Round has been finalized.
ctx.Logger().Debug("finalized round",
"round", round,
)
livenessStats.TotalRounds++
ec := commit.ToDDResult().(*commitment.ExecutorCommitment)
// 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.
state := roothashState.NewMutableState(ctx.State())
if ec.Header.InMessagesCount > 0 {
var meta *message.IncomingMessageQueueMeta
meta, err = state.IncomingMessageQueueMeta(ctx, rtState.Runtime.ID)
if err != nil {
return fmt.Errorf("failed to fetch incoming message queue metadata: %w", err)
}
var msgs []*message.IncomingMessage
msgs, err = state.IncomingMessageQueue(ctx, rtState.Runtime.ID, 0, ec.Header.InMessagesCount)
if err != nil {
return fmt.Errorf("failed to fetch incoming message queue: %w", err)
}
if inMsgsHash := message.InMessagesHash(msgs); !ec.Header.InMessagesHash.Equal(&inMsgsHash) {
ctx.Logger().Debug("finalized round contained invalid incoming message hash, failing instead",
"in_msgs_hash", inMsgsHash,
"ec_in_msgs_hash", *ec.Header.InMessagesHash,
)
// Make the round fail.
err = fmt.Errorf("finalized round contained invalid incoming message hash")
// TODO: All nodes contributing to this round should be penalized.
break
}
for _, msg := range msgs {
err = state.RemoveIncomingMessageFromQueue(ctx, rtState.Runtime.ID, msg.ID)
if err != nil {
return fmt.Errorf("failed to remove processed incoming message from queue: %w", err)
}
if meta.Size == 0 {
// This should NEVER happen.
return tmapi.UnavailableStateError(fmt.Errorf("inconsistent queue size (state corruption?)"))
}
meta.Size--
ctx.EmitEvent(
tmapi.NewEventBuilder(app.Name()).
TypedAttribute(&roothash.InMsgProcessedEvent{
ID: msg.ID,
Round: round,
Caller: msg.Caller,
Tag: msg.Tag,
}).
TypedAttribute(&roothash.RuntimeIDAttribute{ID: rtState.Runtime.ID}),
)
}
err = state.SetIncomingMessageQueueMeta(ctx, rtState.Runtime.ID, meta)
if err != nil {
return fmt.Errorf("failed to set incoming message queue metadata: %w", err)
}
}
// Process any runtime messages.
var messageResults []*roothash.MessageEvent
if messageResults, err = app.processRuntimeMessages(ctx, rtState, ec.Messages); err != nil {
return fmt.Errorf("failed to process runtime messages: %w", err)
}
var (
goodComputeEntities []signature.PublicKey
badComputeEntities []signature.PublicKey
)
commitments := pool.ExecuteCommitments
seen := make(map[signature.PublicKey]bool)
regState := registryState.NewMutableState(ctx.State())
for i, n := range pool.Committee.Members {
c, ok := commitments[n.PublicKey]
switch {
case !ok && n.Role == scheduler.RoleBackupWorker && !pool.Discrepancy:
// This is a backup worker that did not submit a commitment and there was no
// discrepancy. Count the worker as live.
livenessStats.LiveRounds[i]++
continue
case !ok || c.IsIndicatingFailure() || seen[n.PublicKey]:
continue
default:
}
// Make sure to not include nodes in multiple roles multiple times.
seen[n.PublicKey] = true
// 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("tendermint/roothash: getting node %s: %w", n.PublicKey, err)
}
switch commit.MostlyEqual(c) {
case true:
// Correct commit.
goodComputeEntities = append(goodComputeEntities, node.EntityID)
livenessStats.LiveRounds[i]++
case false:
// Incorrect commit.
badComputeEntities = append(badComputeEntities, node.EntityID)
}
}
// If there was a discrepancy, slash entities for incorrect results if configured.
if pool.Discrepancy {
ctx.Logger().Debug("executor pool discrepancy",
"slashing", runtime.Staking.Slashing,
)
if penalty, ok := rtState.Runtime.Staking.Slashing[staking.SlashRuntimeIncorrectResults]; ok && !penalty.Amount.IsZero() {
// Slash for incorrect results.
if err = onRuntimeIncorrectResults(
ctx,
badComputeEntities,
goodComputeEntities,
runtime,
&penalty.Amount,
); err != nil {
return fmt.Errorf("failed to slash for incorrect results: %w", err)
}
}
}
// Generate the final block.
blk := block.NewEmptyBlock(rtState.CurrentBlock, uint64(ctx.Now().Unix()), block.Normal)
blk.Header.IORoot = *ec.Header.IORoot
blk.Header.StateRoot = *ec.Header.StateRoot
blk.Header.MessagesHash = *ec.Header.MessagesHash
blk.Header.InMessagesHash = *ec.Header.InMessagesHash
// Timeout will be cleared by caller.
pool.ResetCommitments(blk.Header.Round)
// All good. Hook up the new block.
rtState.CurrentBlock = blk
rtState.CurrentBlockHeight = ctx.BlockHeight() + 1 // Current height is ctx.BlockHeight() + 1
rtState.LastNormalRound = blk.Header.Round
rtState.LastNormalHeight = ctx.BlockHeight() + 1
// Set last normal round results.
err = state.SetLastRoundResults(ctx, rtState.Runtime.ID, &roothash.RoundResults{
Messages: messageResults,
GoodComputeEntities: goodComputeEntities,
BadComputeEntities: badComputeEntities,
})
if err != nil {
return fmt.Errorf("failed to set last round results: %w", err)
}
ctx.EmitEvent(
tmapi.NewEventBuilder(app.Name()).
TypedAttribute(&roothash.FinalizedEvent{Round: blk.Header.Round}).
TypedAttribute(&roothash.RuntimeIDAttribute{ID: rtState.Runtime.ID}),
)
return nil
case commitment.ErrStillWaiting:
// Need more commits.
ctx.Logger().Debug("insufficient commitments for finality, waiting",
"round", round,
)
return nil
case commitment.ErrDiscrepancyDetected:
// This was already handled above, so it should not happen.
return nil
default:
}
// Something else went wrong, emit empty error block.
ctx.Logger().Error("round failed",
"round", round,
"err", err,
logging.LogEvent, roothash.LogEventRoundFailed,
)
if err := app.emitEmptyBlock(ctx, rtState, block.RoundFailed); err != nil {
return fmt.Errorf("failed to emit empty block: %w", err)
}
return nil
}
func (app *rootHashApplication) tryFinalizeBlock(
ctx *tmapi.Context,
rtState *roothash.RuntimeState,
forced bool,
) error {
ctx = ctx.NewTransaction()
defer ctx.Close()
state := roothashState.NewMutableState(ctx.State())
previousTimeout := rtState.ExecutorPool.NextTimeout
if err := app.tryFinalizeExecutorCommits(ctx, rtState, forced); err != nil {
return err
}
// Do not re-arm the round timeout if the timeout has not changed.
if nextTimeout := rtState.ExecutorPool.NextTimeout; previousTimeout != nextTimeout {
if previousTimeout != commitment.TimeoutNever {
if err := state.ClearRoundTimeout(ctx, rtState.Runtime.ID, previousTimeout); err != nil {
return fmt.Errorf("failed to clear round timeout: %w", err)
}
}
switch nextTimeout {
case commitment.TimeoutNever:
// Only clear round timeout (already done).
ctx.Logger().Debug("disarming round timeout")
default:
// Set a different round timeout.
ctx.Logger().Debug("(re-)arming round timeout",
"height", ctx.BlockHeight(),
"next_timeout", nextTimeout,
)
if err := state.ScheduleRoundTimeout(ctx, rtState.Runtime.ID, nextTimeout); err != nil {
return fmt.Errorf("failed to schedule round timeout: %w", 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
}
// New constructs a new roothash application instance.
func New() tmapi.Application {
return &rootHashApplication{}
}