-
Notifications
You must be signed in to change notification settings - Fork 671
/
vm.go
747 lines (659 loc) · 20.7 KB
/
vm.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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package proposervm
import (
"context"
"errors"
"fmt"
"time"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
"github.com/ava-labs/avalanchego/cache"
"github.com/ava-labs/avalanchego/cache/metercacher"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/database/prefixdb"
"github.com/ava-labs/avalanchego/database/versiondb"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/snow/consensus/snowman"
"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/math"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/avalanchego/utils/units"
"github.com/ava-labs/avalanchego/vms/proposervm/proposer"
"github.com/ava-labs/avalanchego/vms/proposervm/scheduler"
"github.com/ava-labs/avalanchego/vms/proposervm/state"
"github.com/ava-labs/avalanchego/vms/proposervm/tree"
statelessblock "github.com/ava-labs/avalanchego/vms/proposervm/block"
)
const (
// DefaultMinBlockDelay should be kept as whole seconds because block
// timestamps are only specific to the second.
DefaultMinBlockDelay = time.Second
// DefaultNumHistoricalBlocks as 0 results in never deleting any historical
// blocks.
DefaultNumHistoricalBlocks uint64 = 0
checkIndexedFrequency = 10 * time.Second
innerBlkCacheSize = 64 * units.MiB
)
var (
_ block.ChainVM = (*VM)(nil)
_ block.BatchedChainVM = (*VM)(nil)
_ block.StateSyncableVM = (*VM)(nil)
dbPrefix = []byte("proposervm")
)
func cachedBlockSize(_ ids.ID, blk snowman.Block) int {
return ids.IDLen + len(blk.Bytes()) + constants.PointerOverhead
}
type VM struct {
block.ChainVM
Config
blockBuilderVM block.BuildBlockWithContextChainVM
batchedVM block.BatchedChainVM
ssVM block.StateSyncableVM
state.State
proposer.Windower
tree.Tree
scheduler.Scheduler
mockable.Clock
ctx *snow.Context
db *versiondb.Database
toScheduler chan<- common.Message
// Block ID --> Block
// Each element is a block that passed verification but
// hasn't yet been accepted/rejected
verifiedBlocks map[ids.ID]PostForkBlock
// Stateless block ID --> inner block.
// Only contains post-fork blocks near the tip so that the cache doesn't get
// filled with random blocks every time this node parses blocks while
// processing a GetAncestors message from a bootstrapping node.
innerBlkCache cache.Cacher[ids.ID, snowman.Block]
preferred ids.ID
consensusState snow.State
context context.Context
onShutdown func()
// lastAcceptedTime is set to the last accepted PostForkBlock's timestamp
// if the last accepted block has been a PostForkOption block since having
// initialized the VM.
lastAcceptedTime time.Time
// lastAcceptedHeight is set to the last accepted PostForkBlock's height.
lastAcceptedHeight uint64
// proposerBuildSlotGauge reports the slot index when this node may attempt
// to build a block.
proposerBuildSlotGauge prometheus.Gauge
// acceptedBlocksSlotHistogram reports the slots that accepted blocks were
// proposed in.
acceptedBlocksSlotHistogram prometheus.Histogram
}
// New performs best when [minBlkDelay] is whole seconds. This is because block
// timestamps are only specific to the second.
func New(
vm block.ChainVM,
config Config,
) *VM {
blockBuilderVM, _ := vm.(block.BuildBlockWithContextChainVM)
batchedVM, _ := vm.(block.BatchedChainVM)
ssVM, _ := vm.(block.StateSyncableVM)
return &VM{
ChainVM: vm,
Config: config,
blockBuilderVM: blockBuilderVM,
batchedVM: batchedVM,
ssVM: ssVM,
}
}
func (vm *VM) Initialize(
ctx context.Context,
chainCtx *snow.Context,
db database.Database,
genesisBytes []byte,
upgradeBytes []byte,
configBytes []byte,
toEngine chan<- common.Message,
fxs []*common.Fx,
appSender common.AppSender,
) error {
vm.ctx = chainCtx
vm.db = versiondb.New(prefixdb.New(dbPrefix, db))
baseState, err := state.NewMetered(vm.db, "state", vm.Config.Registerer)
if err != nil {
return err
}
vm.State = baseState
vm.Windower = proposer.New(chainCtx.ValidatorState, chainCtx.SubnetID, chainCtx.ChainID)
vm.Tree = tree.New()
innerBlkCache, err := metercacher.New(
"inner_block_cache",
vm.Config.Registerer,
cache.NewSizedLRU(
innerBlkCacheSize,
cachedBlockSize,
),
)
if err != nil {
return err
}
vm.innerBlkCache = innerBlkCache
scheduler, vmToEngine := scheduler.New(vm.ctx.Log, toEngine)
vm.Scheduler = scheduler
vm.toScheduler = vmToEngine
go chainCtx.Log.RecoverAndPanic(func() {
scheduler.Dispatch(time.Now())
})
vm.verifiedBlocks = make(map[ids.ID]PostForkBlock)
detachedCtx := context.WithoutCancel(ctx)
context, cancel := context.WithCancel(detachedCtx)
vm.context = context
vm.onShutdown = cancel
err = vm.ChainVM.Initialize(
ctx,
chainCtx,
db,
genesisBytes,
upgradeBytes,
configBytes,
vmToEngine,
fxs,
appSender,
)
if err != nil {
return err
}
if err := vm.repairAcceptedChainByHeight(ctx); err != nil {
return err
}
if err := vm.setLastAcceptedMetadata(ctx); err != nil {
return err
}
if err := vm.pruneOldBlocks(); err != nil {
return err
}
forkHeight, err := vm.GetForkHeight()
switch err {
case nil:
chainCtx.Log.Info("initialized proposervm",
zap.String("state", "after fork"),
zap.Uint64("forkHeight", forkHeight),
zap.Uint64("lastAcceptedHeight", vm.lastAcceptedHeight),
)
case database.ErrNotFound:
chainCtx.Log.Info("initialized proposervm",
zap.String("state", "before fork"),
)
default:
return err
}
vm.proposerBuildSlotGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "block_building_slot",
Help: "the slot that this node may attempt to build a block",
})
vm.acceptedBlocksSlotHistogram = prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "accepted_blocks_slot",
Help: "the slot accepted blocks were proposed in",
// define the following ranges:
// (-inf, 0]
// (0, 1]
// (1, 2]
// (2, inf)
// the usage of ".5" before was to ensure we work around the limitation
// of comparing floating point of the same numerical value.
Buckets: []float64{0.5, 1.5, 2.5},
})
return errors.Join(
vm.Config.Registerer.Register(vm.proposerBuildSlotGauge),
vm.Config.Registerer.Register(vm.acceptedBlocksSlotHistogram),
)
}
// shutdown ops then propagate shutdown to innerVM
func (vm *VM) Shutdown(ctx context.Context) error {
vm.onShutdown()
vm.Scheduler.Close()
if err := vm.db.Commit(); err != nil {
return err
}
return vm.ChainVM.Shutdown(ctx)
}
func (vm *VM) SetState(ctx context.Context, newState snow.State) error {
if err := vm.ChainVM.SetState(ctx, newState); err != nil {
return err
}
oldState := vm.consensusState
vm.consensusState = newState
if oldState != snow.StateSyncing {
return nil
}
// When finishing StateSyncing, if state sync has failed or was skipped,
// repairAcceptedChainByHeight rolls back the chain to the previously last
// accepted block. If state sync has completed successfully, this call is a
// no-op.
if err := vm.repairAcceptedChainByHeight(ctx); err != nil {
return err
}
return vm.setLastAcceptedMetadata(ctx)
}
func (vm *VM) BuildBlock(ctx context.Context) (snowman.Block, error) {
preferredBlock, err := vm.getBlock(ctx, vm.preferred)
if err != nil {
vm.ctx.Log.Error("unexpected build block failure",
zap.String("reason", "failed to fetch preferred block"),
zap.Stringer("parentID", vm.preferred),
zap.Error(err),
)
return nil, err
}
return preferredBlock.buildChild(ctx)
}
func (vm *VM) ParseBlock(ctx context.Context, b []byte) (snowman.Block, error) {
if blk, err := vm.parsePostForkBlock(ctx, b); err == nil {
return blk, nil
}
return vm.parsePreForkBlock(ctx, b)
}
func (vm *VM) GetBlock(ctx context.Context, id ids.ID) (snowman.Block, error) {
return vm.getBlock(ctx, id)
}
func (vm *VM) SetPreference(ctx context.Context, preferred ids.ID) error {
if vm.preferred == preferred {
return nil
}
vm.preferred = preferred
blk, err := vm.getPostForkBlock(ctx, preferred)
if err != nil {
return vm.ChainVM.SetPreference(ctx, preferred)
}
if err := vm.ChainVM.SetPreference(ctx, blk.getInnerBlk().ID()); err != nil {
return err
}
pChainHeight, err := blk.pChainHeight(ctx)
if err != nil {
return err
}
var (
childBlockHeight = blk.Height() + 1
parentTimestamp = blk.Timestamp()
nextStartTime time.Time
)
if vm.IsDurangoActivated(parentTimestamp) {
currentTime := vm.Clock.Time().Truncate(time.Second)
if nextStartTime, err = vm.getPostDurangoSlotTime(
ctx,
childBlockHeight,
pChainHeight,
proposer.TimeToSlot(parentTimestamp, currentTime),
parentTimestamp,
); err == nil {
vm.proposerBuildSlotGauge.Set(float64(proposer.TimeToSlot(parentTimestamp, nextStartTime)))
}
} else {
nextStartTime, err = vm.getPreDurangoSlotTime(
ctx,
childBlockHeight,
pChainHeight,
parentTimestamp,
)
}
if err != nil {
vm.ctx.Log.Debug("failed to fetch the expected delay",
zap.Error(err),
)
// A nil error is returned here because it is possible that
// bootstrapping caused the last accepted block to move past the latest
// P-chain height. This will cause building blocks to return an error
// until the P-chain's height has advanced.
return nil
}
vm.Scheduler.SetBuildBlockTime(nextStartTime)
vm.ctx.Log.Debug("set preference",
zap.Stringer("blkID", blk.ID()),
zap.Time("blockTimestamp", parentTimestamp),
zap.Time("nextStartTime", nextStartTime),
)
return nil
}
func (vm *VM) getPreDurangoSlotTime(
ctx context.Context,
blkHeight,
pChainHeight uint64,
parentTimestamp time.Time,
) (time.Time, error) {
delay, err := vm.Windower.Delay(ctx, blkHeight, pChainHeight, vm.ctx.NodeID, proposer.MaxBuildWindows)
if err != nil {
return time.Time{}, err
}
// Note: The P-chain does not currently try to target any block time. It
// notifies the consensus engine as soon as a new block may be built. To
// avoid fast runs of blocks there is an additional minimum delay that
// validators can specify. This delay may be an issue for high performance,
// custom VMs. Until the P-chain is modified to target a specific block
// time, ProposerMinBlockDelay can be configured in the subnet config.
delay = max(delay, vm.MinBlkDelay)
return parentTimestamp.Add(delay), nil
}
func (vm *VM) getPostDurangoSlotTime(
ctx context.Context,
blkHeight,
pChainHeight,
slot uint64,
parentTimestamp time.Time,
) (time.Time, error) {
delay, err := vm.Windower.MinDelayForProposer(
ctx,
blkHeight,
pChainHeight,
vm.ctx.NodeID,
slot,
)
// Note: The P-chain does not currently try to target any block time. It
// notifies the consensus engine as soon as a new block may be built. To
// avoid fast runs of blocks there is an additional minimum delay that
// validators can specify. This delay may be an issue for high performance,
// custom VMs. Until the P-chain is modified to target a specific block
// time, ProposerMinBlockDelay can be configured in the subnet config.
switch {
case err == nil:
delay = max(delay, vm.MinBlkDelay)
return parentTimestamp.Add(delay), err
case errors.Is(err, proposer.ErrAnyoneCanPropose):
return parentTimestamp.Add(vm.MinBlkDelay), err
default:
return time.Time{}, err
}
}
func (vm *VM) LastAccepted(ctx context.Context) (ids.ID, error) {
lastAccepted, err := vm.State.GetLastAccepted()
if err == database.ErrNotFound {
return vm.ChainVM.LastAccepted(ctx)
}
return lastAccepted, err
}
func (vm *VM) repairAcceptedChainByHeight(ctx context.Context) error {
innerLastAcceptedID, err := vm.ChainVM.LastAccepted(ctx)
if err != nil {
return err
}
innerLastAccepted, err := vm.ChainVM.GetBlock(ctx, innerLastAcceptedID)
if err != nil {
return err
}
proLastAcceptedID, err := vm.State.GetLastAccepted()
if err == database.ErrNotFound {
// If the last accepted block isn't indexed yet, then the underlying
// chain is the only chain and there is nothing to repair.
return nil
}
if err != nil {
return err
}
proLastAccepted, err := vm.getPostForkBlock(ctx, proLastAcceptedID)
if err != nil {
return err
}
proLastAcceptedHeight := proLastAccepted.Height()
innerLastAcceptedHeight := innerLastAccepted.Height()
if proLastAcceptedHeight < innerLastAcceptedHeight {
return fmt.Errorf("proposervm height index (%d) should never be lower than the inner height index (%d)", proLastAcceptedHeight, innerLastAcceptedHeight)
}
if proLastAcceptedHeight == innerLastAcceptedHeight {
// There is nothing to repair - as the heights match
return nil
}
vm.ctx.Log.Info("repairing accepted chain by height",
zap.Uint64("outerHeight", proLastAcceptedHeight),
zap.Uint64("innerHeight", innerLastAcceptedHeight),
)
// The inner vm must be behind the proposer vm, so we must roll the
// proposervm back.
forkHeight, err := vm.State.GetForkHeight()
if err != nil {
return err
}
if forkHeight > innerLastAcceptedHeight {
// We are rolling back past the fork, so we should just forget about all
// of our proposervm indices.
if err := vm.State.DeleteLastAccepted(); err != nil {
return err
}
return vm.db.Commit()
}
newProLastAcceptedID, err := vm.State.GetBlockIDAtHeight(innerLastAcceptedHeight)
if err != nil {
// This fatal error can happen if NumHistoricalBlocks is set too
// aggressively and the inner vm rolled back before the oldest
// proposervm block.
return fmt.Errorf("proposervm failed to rollback last accepted block to height (%d): %w", innerLastAcceptedHeight, err)
}
if err := vm.State.SetLastAccepted(newProLastAcceptedID); err != nil {
return err
}
return vm.db.Commit()
}
func (vm *VM) setLastAcceptedMetadata(ctx context.Context) error {
lastAcceptedID, err := vm.GetLastAccepted()
if err == database.ErrNotFound {
// If the last accepted block wasn't a PostFork block, then we don't
// initialize the metadata.
vm.lastAcceptedHeight = 0
vm.lastAcceptedTime = time.Time{}
return nil
}
if err != nil {
return err
}
lastAccepted, err := vm.getPostForkBlock(ctx, lastAcceptedID)
if err != nil {
return err
}
// Set the last accepted height
vm.lastAcceptedHeight = lastAccepted.Height()
if _, ok := lastAccepted.getStatelessBlk().(statelessblock.SignedBlock); ok {
// If the last accepted block wasn't a PostForkOption, then we don't
// initialize the time.
return nil
}
acceptedParent, err := vm.getPostForkBlock(ctx, lastAccepted.Parent())
if err != nil {
return err
}
vm.lastAcceptedTime = acceptedParent.Timestamp()
return nil
}
func (vm *VM) parsePostForkBlock(ctx context.Context, b []byte) (PostForkBlock, error) {
statelessBlock, err := statelessblock.Parse(b, vm.ctx.ChainID)
if err != nil {
return nil, err
}
// if the block already exists, then make sure the status is set correctly
blkID := statelessBlock.ID()
blk, err := vm.getPostForkBlock(ctx, blkID)
if err == nil {
return blk, nil
}
if err != database.ErrNotFound {
return nil, err
}
innerBlkBytes := statelessBlock.Block()
innerBlk, err := vm.parseInnerBlock(ctx, blkID, innerBlkBytes)
if err != nil {
return nil, err
}
if statelessSignedBlock, ok := statelessBlock.(statelessblock.SignedBlock); ok {
blk = &postForkBlock{
SignedBlock: statelessSignedBlock,
postForkCommonComponents: postForkCommonComponents{
vm: vm,
innerBlk: innerBlk,
status: choices.Processing,
},
}
} else {
blk = &postForkOption{
Block: statelessBlock,
postForkCommonComponents: postForkCommonComponents{
vm: vm,
innerBlk: innerBlk,
status: choices.Processing,
},
}
}
return blk, nil
}
func (vm *VM) parsePreForkBlock(ctx context.Context, b []byte) (*preForkBlock, error) {
blk, err := vm.ChainVM.ParseBlock(ctx, b)
return &preForkBlock{
Block: blk,
vm: vm,
}, err
}
func (vm *VM) getBlock(ctx context.Context, id ids.ID) (Block, error) {
if blk, err := vm.getPostForkBlock(ctx, id); err == nil {
return blk, nil
}
return vm.getPreForkBlock(ctx, id)
}
func (vm *VM) getPostForkBlock(ctx context.Context, blkID ids.ID) (PostForkBlock, error) {
block, exists := vm.verifiedBlocks[blkID]
if exists {
return block, nil
}
statelessBlock, status, err := vm.State.GetBlock(blkID)
if err != nil {
return nil, err
}
innerBlkBytes := statelessBlock.Block()
innerBlk, err := vm.parseInnerBlock(ctx, blkID, innerBlkBytes)
if err != nil {
return nil, err
}
if statelessSignedBlock, ok := statelessBlock.(statelessblock.SignedBlock); ok {
return &postForkBlock{
SignedBlock: statelessSignedBlock,
postForkCommonComponents: postForkCommonComponents{
vm: vm,
innerBlk: innerBlk,
status: status,
},
}, nil
}
return &postForkOption{
Block: statelessBlock,
postForkCommonComponents: postForkCommonComponents{
vm: vm,
innerBlk: innerBlk,
status: status,
},
}, nil
}
func (vm *VM) getPreForkBlock(ctx context.Context, blkID ids.ID) (*preForkBlock, error) {
blk, err := vm.ChainVM.GetBlock(ctx, blkID)
return &preForkBlock{
Block: blk,
vm: vm,
}, err
}
func (vm *VM) acceptPostForkBlock(blk PostForkBlock) error {
height := blk.Height()
blkID := blk.ID()
vm.lastAcceptedHeight = height
delete(vm.verifiedBlocks, blkID)
// Persist this block, its height index, and its status
if err := vm.State.SetLastAccepted(blkID); err != nil {
return err
}
if err := vm.State.PutBlock(blk.getStatelessBlk(), choices.Accepted); err != nil {
return err
}
if err := vm.updateHeightIndex(height, blkID); err != nil {
return err
}
return vm.db.Commit()
}
func (vm *VM) verifyAndRecordInnerBlk(ctx context.Context, blockCtx *block.Context, postFork PostForkBlock) error {
innerBlk := postFork.getInnerBlk()
postForkID := postFork.ID()
originalInnerBlock, previouslyVerified := vm.Tree.Get(innerBlk)
if previouslyVerified {
innerBlk = originalInnerBlock
// We must update all of the mappings from postFork -> innerBlock to
// now point to originalInnerBlock.
postFork.setInnerBlk(originalInnerBlock)
vm.innerBlkCache.Put(postForkID, originalInnerBlock)
}
var (
shouldVerifyWithCtx = blockCtx != nil
blkWithCtx block.WithVerifyContext
err error
)
if shouldVerifyWithCtx {
blkWithCtx, shouldVerifyWithCtx = innerBlk.(block.WithVerifyContext)
if shouldVerifyWithCtx {
shouldVerifyWithCtx, err = blkWithCtx.ShouldVerifyWithContext(ctx)
if err != nil {
return err
}
}
}
// Invariant: If either [Verify] or [VerifyWithContext] returns nil, this
// function must return nil. This maintains the inner block's
// invariant that successful verification will eventually result
// in accepted or rejected being called.
if shouldVerifyWithCtx {
// This block needs to know the P-Chain height during verification.
// Note that [VerifyWithContext] with context may be called multiple
// times with multiple contexts.
err = blkWithCtx.VerifyWithContext(ctx, blockCtx)
} else if !previouslyVerified {
// This isn't a [block.WithVerifyContext] so we only call [Verify] once.
err = innerBlk.Verify(ctx)
}
if err != nil {
return err
}
// Since verification passed, we should ensure the inner block tree is
// populated.
if !previouslyVerified {
vm.Tree.Add(innerBlk)
}
vm.verifiedBlocks[postForkID] = postFork
return nil
}
// notifyInnerBlockReady tells the scheduler that the inner VM is ready to build
// a new block
func (vm *VM) notifyInnerBlockReady() {
select {
case vm.toScheduler <- common.PendingTxs:
default:
vm.ctx.Log.Debug("dropping message to consensus engine")
}
}
func (vm *VM) optimalPChainHeight(ctx context.Context, minPChainHeight uint64) (uint64, error) {
minimumHeight, err := vm.ctx.ValidatorState.GetMinimumHeight(ctx)
if err != nil {
return 0, err
}
return max(minimumHeight, minPChainHeight), nil
}
// parseInnerBlock attempts to parse the provided bytes as an inner block. If
// the inner block happens to be cached, then the inner block will not be
// parsed.
func (vm *VM) parseInnerBlock(ctx context.Context, outerBlkID ids.ID, innerBlkBytes []byte) (snowman.Block, error) {
if innerBlk, ok := vm.innerBlkCache.Get(outerBlkID); ok {
return innerBlk, nil
}
innerBlk, err := vm.ChainVM.ParseBlock(ctx, innerBlkBytes)
if err != nil {
return nil, err
}
vm.cacheInnerBlock(outerBlkID, innerBlk)
return innerBlk, nil
}
// Caches proposervm block ID --> inner block if the inner block's height
// is within [innerBlkCacheSize] of the last accepted block's height.
func (vm *VM) cacheInnerBlock(outerBlkID ids.ID, innerBlk snowman.Block) {
diff := math.AbsDiff(innerBlk.Height(), vm.lastAcceptedHeight)
if diff < innerBlkCacheSize {
vm.innerBlkCache.Put(outerBlkID, innerBlk)
}
}