-
Notifications
You must be signed in to change notification settings - Fork 115
/
vm.go
1220 lines (1084 loc) · 36.6 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
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
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package vm
import (
"context"
"encoding/binary"
"encoding/json"
"fmt"
"net/http"
"path/filepath"
"sync"
"time"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/network/p2p"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/snow/consensus/snowman"
"github.com/ava-labs/avalanchego/snow/engine/common"
"github.com/ava-labs/avalanchego/utils/crypto/bls"
"github.com/ava-labs/avalanchego/utils/profiler"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/version"
"github.com/ava-labs/avalanchego/x/merkledb"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
"github.com/ava-labs/hypersdk/api"
"github.com/ava-labs/hypersdk/chain"
"github.com/ava-labs/hypersdk/codec"
"github.com/ava-labs/hypersdk/event"
"github.com/ava-labs/hypersdk/fees"
"github.com/ava-labs/hypersdk/genesis"
"github.com/ava-labs/hypersdk/internal/builder"
"github.com/ava-labs/hypersdk/internal/cache"
"github.com/ava-labs/hypersdk/internal/gossiper"
"github.com/ava-labs/hypersdk/internal/mempool"
"github.com/ava-labs/hypersdk/internal/pebble"
"github.com/ava-labs/hypersdk/internal/trace"
"github.com/ava-labs/hypersdk/internal/validators"
"github.com/ava-labs/hypersdk/internal/validitywindow"
"github.com/ava-labs/hypersdk/internal/workers"
"github.com/ava-labs/hypersdk/state"
"github.com/ava-labs/hypersdk/statesync"
"github.com/ava-labs/hypersdk/storage"
"github.com/ava-labs/hypersdk/utils"
avacache "github.com/ava-labs/avalanchego/cache"
avatrace "github.com/ava-labs/avalanchego/trace"
avautils "github.com/ava-labs/avalanchego/utils"
internalfees "github.com/ava-labs/hypersdk/internal/fees"
)
const (
blockDB = "blockdb"
stateDB = "statedb"
vmDataDir = "vm"
MaxAcceptorSize = 256
MinAcceptedBlockWindow = 1024
txGossipHandlerID = 0x2
)
type VM struct {
DataDir string
v *version.Semantic
snowCtx *snow.Context
pkBytes []byte
proposerMonitor *validators.ProposerMonitor
config Config
genesisAndRuleFactory genesis.GenesisAndRuleFactory
genesis genesis.Genesis
GenesisBytes []byte
ruleFactory chain.RuleFactory
options []Option
chain *chain.Chain
chainTimeValidityWindow chain.ValidityWindow
syncer *validitywindow.Syncer[*chain.Transaction]
seenValidityWindowOnce sync.Once
seenValidityWindow chan struct{}
builder builder.Builder
gossiper gossiper.Gossiper
asyncAcceptedSubscriptionFactories []event.SubscriptionFactory[*chain.ExecutedBlock]
asyncAcceptedSubscriptions []event.Subscription[*chain.ExecutedBlock]
acceptedSubscriptions []event.Subscription[*StatefulBlock]
verifiedSubscriptions []event.Subscription[*chain.ExecutedBlock]
rejectedSubscriptions []event.Subscription[*chain.ExecutedBlock]
vmAPIHandlerFactories []api.HandlerFactory[api.VM]
rawStateDB database.Database
stateDB merkledb.MerkleDB
vmDB database.Database
handlers map[string]http.Handler
balanceHandler chain.BalanceHandler
metadataManager chain.MetadataManager
actionCodec *codec.TypeParser[chain.Action]
authCodec *codec.TypeParser[chain.Auth]
outputCodec *codec.TypeParser[codec.Typed]
authEngine map[uint8]AuthEngine
network *p2p.Network
tracer avatrace.Tracer
mempool *mempool.Mempool[*chain.Transaction]
// We cannot use a map here because we may parse blocks up in the ancestry
parsedBlocks *avacache.LRU[ids.ID, *StatefulBlock]
// Each element is a block that passed verification but
// hasn't yet been accepted/rejected
verifiedL sync.RWMutex
verifiedBlocks map[ids.ID]*StatefulBlock
// We store the last [AcceptedBlockWindowCache] blocks in memory
// to avoid reading blocks from disk.
acceptedBlocksByID *cache.FIFO[ids.ID, *StatefulBlock]
acceptedBlocksByHeight *cache.FIFO[uint64, ids.ID]
// Accepted block queue
acceptedQueue chan *StatefulBlock
acceptorDone chan struct{}
// authVerifiers are used to verify signatures in parallel
// with limited parallelism
authVerifiers workers.Workers
bootstrapped avautils.Atomic[bool]
genesisBlk *StatefulBlock
preferred ids.ID
lastAccepted *StatefulBlock
toEngine chan<- common.Message
// State Sync client and AppRequest handlers
StateSyncClient *statesync.Client[*StatefulBlock]
StateSyncServer *statesync.Server[*StatefulBlock]
metrics *Metrics
profiler profiler.ContinuousProfiler
ready chan struct{}
stop chan struct{}
}
func New(
v *version.Semantic,
genesisFactory genesis.GenesisAndRuleFactory,
balanceHandler chain.BalanceHandler,
metadataManager chain.MetadataManager,
actionCodec *codec.TypeParser[chain.Action],
authCodec *codec.TypeParser[chain.Auth],
outputCodec *codec.TypeParser[codec.Typed],
authEngine map[uint8]AuthEngine,
options ...Option,
) (*VM, error) {
allocatedNamespaces := set.NewSet[string](len(options))
for _, option := range options {
if allocatedNamespaces.Contains(option.Namespace) {
return nil, fmt.Errorf("namespace %s already allocated", option.Namespace)
}
allocatedNamespaces.Add(option.Namespace)
}
return &VM{
v: v,
balanceHandler: balanceHandler,
metadataManager: metadataManager,
config: NewConfig(),
actionCodec: actionCodec,
authCodec: authCodec,
outputCodec: outputCodec,
authEngine: authEngine,
genesisAndRuleFactory: genesisFactory,
options: options,
}, nil
}
// implements "block.ChainVM.common.VM"
func (vm *VM) Initialize(
ctx context.Context,
snowCtx *snow.Context,
_ database.Database,
genesisBytes []byte,
upgradeBytes []byte,
configBytes []byte,
toEngine chan<- common.Message,
_ []*common.Fx,
appSender common.AppSender,
) error {
vm.DataDir = filepath.Join(snowCtx.ChainDataDir, vmDataDir)
vm.snowCtx = snowCtx
// Init channels before initializing other structs
vm.toEngine = toEngine
vm.pkBytes = bls.PublicKeyToCompressedBytes(vm.snowCtx.PublicKey)
vm.seenValidityWindow = make(chan struct{})
vm.ready = make(chan struct{})
vm.stop = make(chan struct{})
// TODO: cleanup metrics registration
defaultRegistry, metrics, err := newMetrics()
if err != nil {
return err
}
if err := vm.snowCtx.Metrics.Register("hypersdk", defaultRegistry); err != nil {
return err
}
vm.metrics = metrics
vm.proposerMonitor = validators.NewProposerMonitor(vm, vm.snowCtx)
vm.network, err = p2p.NewNetwork(vm.snowCtx.Log, appSender, defaultRegistry, "p2p")
if err != nil {
return fmt.Errorf("failed to initialize p2p: %w", err)
}
blockDBRegistry := prometheus.NewRegistry()
if err := vm.snowCtx.Metrics.Register("blockdb", blockDBRegistry); err != nil {
return fmt.Errorf("failed to register blockdb metrics: %w", err)
}
pebbleConfig := pebble.NewDefaultConfig()
vm.vmDB, err = storage.New(pebbleConfig, vm.snowCtx.ChainDataDir, blockDB, blockDBRegistry)
if err != nil {
return err
}
rawStateDBRegistry := prometheus.NewRegistry()
if err := vm.snowCtx.Metrics.Register("rawstatedb", rawStateDBRegistry); err != nil {
return fmt.Errorf("failed to register rawstatedb metrics: %w", err)
}
vm.rawStateDB, err = storage.New(pebbleConfig, vm.snowCtx.ChainDataDir, stateDB, rawStateDBRegistry)
if err != nil {
return err
}
vm.genesis, vm.ruleFactory, err = vm.genesisAndRuleFactory.Load(genesisBytes, upgradeBytes, vm.snowCtx.NetworkID, vm.snowCtx.ChainID)
vm.GenesisBytes = genesisBytes
if err != nil {
return err
}
if len(configBytes) > 0 {
if err := json.Unmarshal(configBytes, &vm.config); err != nil {
return fmt.Errorf("failed to unmarshal config: %w", err)
}
}
snowCtx.Log.Info("initialized hypersdk config", zap.Any("config", vm.config))
// Setup tracer
vm.tracer, err = trace.New(&vm.config.TraceConfig)
if err != nil {
return err
}
ctx, span := vm.tracer.Start(ctx, "VM.Initialize")
defer span.End()
// Set defaults
vm.mempool = mempool.New[*chain.Transaction](vm.tracer, vm.config.MempoolSize, vm.config.MempoolSponsorSize)
vm.acceptedSubscriptions = append(vm.acceptedSubscriptions, event.SubscriptionFunc[*StatefulBlock]{
NotifyF: func(ctx context.Context, b *StatefulBlock) error {
droppedTxs := vm.mempool.SetMinTimestamp(ctx, b.Tmstmp)
vm.snowCtx.Log.Debug("dropping expired transactions from mempool",
zap.Stringer("blkID", b.ID()),
zap.Int("numTxs", len(droppedTxs)),
)
return nil
},
})
vm.verifiedSubscriptions = append(vm.verifiedSubscriptions, event.SubscriptionFunc[*chain.ExecutedBlock]{
NotifyF: func(ctx context.Context, b *chain.ExecutedBlock) error {
vm.mempool.Remove(ctx, b.Block.Txs)
return nil
},
})
vm.rejectedSubscriptions = append(vm.rejectedSubscriptions, event.SubscriptionFunc[*chain.ExecutedBlock]{
NotifyF: func(ctx context.Context, b *chain.ExecutedBlock) error {
vm.mempool.Add(ctx, b.Block.Txs)
return nil
},
})
// Setup profiler
if cfg := vm.config.ContinuousProfilerConfig; cfg.Enabled {
vm.profiler = profiler.NewContinuous(cfg.Dir, cfg.Freq, cfg.MaxNumFiles)
go vm.profiler.Dispatch() //nolint:errcheck
}
// Instantiate DBs
merkleRegistry := prometheus.NewRegistry()
vm.stateDB, err = merkledb.New(ctx, vm.rawStateDB, merkledb.Config{
BranchFactor: vm.genesis.GetStateBranchFactor(),
// RootGenConcurrency limits the number of goroutines
// that will be used across all concurrent root generations
RootGenConcurrency: uint(vm.config.RootGenerationCores),
HistoryLength: uint(vm.config.StateHistoryLength),
ValueNodeCacheSize: uint(vm.config.ValueNodeCacheSize),
IntermediateNodeCacheSize: uint(vm.config.IntermediateNodeCacheSize),
IntermediateWriteBufferSize: uint(vm.config.StateIntermediateWriteBufferSize),
IntermediateWriteBatchSize: uint(vm.config.StateIntermediateWriteBatchSize),
Reg: merkleRegistry,
TraceLevel: merkledb.InfoTrace,
Tracer: vm.tracer,
})
if err != nil {
return err
}
if err := vm.snowCtx.Metrics.Register("state", merkleRegistry); err != nil {
return err
}
// Setup worker cluster for verifying signatures
//
// If [parallelism] is odd, we assign the extra
// core to signature verification.
vm.authVerifiers = workers.NewParallel(vm.config.AuthVerificationCores, 100) // TODO: make job backlog a const
vm.parsedBlocks = &avacache.LRU[ids.ID, *StatefulBlock]{Size: vm.config.ParsedBlockCacheSize}
vm.verifiedBlocks = make(map[ids.ID]*StatefulBlock)
vm.acceptedBlocksByID, err = cache.NewFIFO[ids.ID, *StatefulBlock](vm.config.AcceptedBlockWindowCache)
if err != nil {
return err
}
vm.acceptedBlocksByHeight, err = cache.NewFIFO[uint64, ids.ID](vm.config.AcceptedBlockWindowCache)
if err != nil {
return err
}
acceptorSize := vm.config.AcceptorSize
if acceptorSize > MaxAcceptorSize {
return fmt.Errorf("AcceptorSize (%d) must be <= MaxAcceptorSize (%d)", acceptorSize, MaxAcceptorSize)
}
acceptedBlockWindow := vm.config.AcceptedBlockWindow
if acceptedBlockWindow < MinAcceptedBlockWindow {
return fmt.Errorf("AcceptedBlockWindow (%d) must be >= to MinAcceptedBlockWindow (%d)", acceptedBlockWindow, MinAcceptedBlockWindow)
}
vm.acceptedQueue = make(chan *StatefulBlock, vm.config.AcceptorSize)
vm.acceptorDone = make(chan struct{})
// Set defaults
options := &Options{}
for _, Option := range vm.options {
config := vm.config.ServiceConfig[Option.Namespace]
opt, err := Option.optionFunc(vm, config)
if err != nil {
return err
}
opt.apply(options)
}
err = vm.applyOptions(options)
if err != nil {
return fmt.Errorf("failed to apply options : %w", err)
}
vm.chainTimeValidityWindow = validitywindow.NewTimeValidityWindow(vm.snowCtx.Log, vm.tracer, vm)
registerer := prometheus.NewRegistry()
if err := vm.snowCtx.Metrics.Register("chain", registerer); err != nil {
return err
}
vm.chain, err = chain.NewChain(
vm.Tracer(),
registerer,
vm,
vm.Mempool(),
vm.Logger(),
vm.ruleFactory,
vm.MetadataManager(),
vm.BalanceHandler(),
vm.AuthVerifiers(),
vm,
vm.chainTimeValidityWindow,
vm.config.ChainConfig,
)
if err != nil {
return err
}
vm.syncer = validitywindow.NewSyncer(vm, vm.chainTimeValidityWindow, func(time int64) int64 {
return vm.ruleFactory.GetRules(time).GetValidityWindow()
})
vm.acceptedSubscriptions = append(vm.acceptedSubscriptions, event.SubscriptionFunc[*StatefulBlock]{
NotifyF: func(ctx context.Context, b *StatefulBlock) error {
seenValidityWindow, err := vm.syncer.Accept(ctx, b.ExecutionBlock)
if err != nil {
vm.Fatal("syncer failed to accept block", zap.Error(err))
}
if seenValidityWindow {
vm.seenValidityWindowOnce.Do(func() {
close(vm.seenValidityWindow)
})
}
return nil
},
})
// Try to load last accepted
has, err := vm.HasLastAccepted()
if err != nil {
snowCtx.Log.Error("could not determine if have last accepted")
return err
}
if has { //nolint:nestif
genesisBlk, err := vm.GetGenesis(ctx)
if err != nil {
snowCtx.Log.Error("could not get genesis", zap.Error(err))
return err
}
vm.genesisBlk = genesisBlk
lastAcceptedHeight, err := vm.GetLastAcceptedHeight()
if err != nil {
snowCtx.Log.Error("could not get last accepted height", zap.Error(err))
return err
}
blk, err := vm.GetDiskBlock(ctx, lastAcceptedHeight)
if err != nil {
snowCtx.Log.Error("could not get last accepted block", zap.Error(err))
return err
}
vm.preferred, vm.lastAccepted = blk.ID(), blk
vm.loadAcceptedBlocks(ctx)
// It is not guaranteed that the last accepted state on-disk matches the post-execution
// result of the last accepted block.
snowCtx.Log.Info("initialized vm from last accepted", zap.Stringer("block", blk.ID()))
} else {
sps := state.NewSimpleMutable(vm.stateDB)
if err := vm.genesis.InitializeState(ctx, vm.tracer, sps, vm.balanceHandler); err != nil {
snowCtx.Log.Error("could not set genesis state", zap.Error(err))
return err
}
if err := sps.Commit(ctx); err != nil {
return err
}
root, err := vm.stateDB.GetMerkleRoot(ctx)
if err != nil {
snowCtx.Log.Error("could not get merkle root", zap.Error(err))
return err
}
snowCtx.Log.Info("genesis state created", zap.Stringer("root", root))
// Create genesis block
genesisExecutionBlk, err := chain.NewGenesisBlock(root)
if err != nil {
snowCtx.Log.Error("could not create genesis block", zap.Error(err))
return err
}
genesisBlk, err := ParseStatefulBlock(
ctx,
genesisExecutionBlk,
true,
vm,
)
if err != nil {
snowCtx.Log.Error("unable to init genesis block", zap.Error(err))
return err
}
// Set executed block, since we will never execute the genesis block
genesisBlk.executedBlock = &chain.ExecutedBlock{
Block: genesisExecutionBlk.StatelessBlock,
}
// Update chain metadata
sps = state.NewSimpleMutable(vm.stateDB)
if err := sps.Insert(ctx, chain.HeightKey(vm.MetadataManager().HeightPrefix()), binary.BigEndian.AppendUint64(nil, 0)); err != nil {
return err
}
if err := sps.Insert(ctx, chain.TimestampKey(vm.MetadataManager().TimestampPrefix()), binary.BigEndian.AppendUint64(nil, 0)); err != nil {
return err
}
genesisRules := vm.Rules(0)
feeManager := internalfees.NewManager(nil)
minUnitPrice := genesisRules.GetMinUnitPrice()
for i := fees.Dimension(0); i < fees.FeeDimensions; i++ {
feeManager.SetUnitPrice(i, minUnitPrice[i])
snowCtx.Log.Info("set genesis unit price", zap.Int("dimension", int(i)), zap.Uint64("price", feeManager.UnitPrice(i)))
}
if err := sps.Insert(ctx, chain.FeeKey(vm.MetadataManager().FeePrefix()), feeManager.Bytes()); err != nil {
return err
}
// Commit genesis block post-execution state and compute root
if err := sps.Commit(ctx); err != nil {
return err
}
genesisRoot, err := vm.stateDB.GetMerkleRoot(ctx)
if err != nil {
snowCtx.Log.Error("could not get merkle root", zap.Error(err))
return err
}
// Update last accepted and preferred block
vm.genesisBlk = genesisBlk
if err := vm.UpdateLastAccepted(genesisBlk); err != nil {
snowCtx.Log.Error("could not set genesis block as last accepted", zap.Error(err))
return err
}
gBlkID := genesisBlk.ID()
vm.preferred, vm.lastAccepted = gBlkID, genesisBlk
snowCtx.Log.Info("initialized vm from genesis",
zap.Stringer("block", gBlkID),
zap.Stringer("pre-execution root", genesisBlk.StateRoot),
zap.Stringer("post-execution root", genesisRoot),
)
}
// accept the last block in order to initialize the internal lastBlockHeight
vm.chainTimeValidityWindow.Accept(vm.lastAccepted.ExecutionBlock)
go vm.processAcceptedBlocks()
if err := vm.initStateSync(); err != nil {
return err
}
if err := vm.network.AddHandler(
txGossipHandlerID,
gossiper.NewTxGossipHandler(
vm,
vm.snowCtx.Log,
vm.gossiper,
),
); err != nil {
return err
}
// Startup block builder and gossiper
go vm.builder.Run()
go vm.gossiper.Run(vm.network.NewClient(txGossipHandlerID))
// Wait until VM is ready and then send a state sync message to engine
go vm.markReady()
for _, factory := range vm.asyncAcceptedSubscriptionFactories {
subscription, err := factory.New()
if err != nil {
return fmt.Errorf("failed to initialize block subscription: %w", err)
}
vm.asyncAcceptedSubscriptions = append(vm.asyncAcceptedSubscriptions, subscription)
}
vm.handlers = make(map[string]http.Handler)
for _, apiFactory := range vm.vmAPIHandlerFactories {
api, err := apiFactory.New(vm)
if err != nil {
return fmt.Errorf("failed to initialize api: %w", err)
}
if _, ok := vm.handlers[api.Path]; ok {
return fmt.Errorf("failed to register duplicate vm api path: %s", api.Path)
}
vm.handlers[api.Path] = api.Handler
}
err = vm.restoreAcceptedQueue(ctx)
if err != nil {
return fmt.Errorf("failed to restore accepted blocks to the queue: %w", err)
}
return nil
}
func (vm *VM) applyOptions(o *Options) error {
vm.asyncAcceptedSubscriptionFactories = o.blockSubscriptionFactories
vm.vmAPIHandlerFactories = o.vmAPIHandlerFactories
if o.builder {
vm.builder = builder.NewManual(vm.toEngine, vm.snowCtx.Log)
} else {
vm.builder = builder.NewTime(vm.toEngine, vm.snowCtx.Log, vm.mempool, func(ctx context.Context, t int64) (int64, int64, error) {
blk, err := vm.GetStatefulBlock(ctx, vm.preferred)
if err != nil {
return 0, 0, err
}
return blk.Tmstmp, vm.ruleFactory.GetRules(t).GetMinBlockGap(), nil
})
}
gossipRegistry := prometheus.NewRegistry()
err := vm.snowCtx.Metrics.Register("gossiper", gossipRegistry)
if err != nil {
return fmt.Errorf("failed to register gossiper metrics: %w", err)
}
if o.gossiper {
vm.gossiper, err = gossiper.NewManual[*chain.Transaction](
vm.snowCtx.Log,
gossipRegistry,
vm.mempool,
&chain.TxSerializer{
ActionRegistry: vm.actionCodec,
AuthRegistry: vm.authCodec,
},
vm,
vm.config.TargetGossipDuration,
)
if err != nil {
return fmt.Errorf("failed to create manual gossiper: %w", err)
}
} else {
txGossiper, err := gossiper.NewTarget[*chain.Transaction](
vm.tracer,
vm.snowCtx.Log,
gossipRegistry,
vm.mempool,
&chain.TxSerializer{
ActionRegistry: vm.actionCodec,
AuthRegistry: vm.authCodec,
},
vm,
vm,
vm.config.TargetGossipDuration,
&gossiper.TargetProposers[*chain.Transaction]{
Validators: vm,
Config: gossiper.DefaultTargetProposerConfig(),
},
gossiper.DefaultTargetConfig(),
vm.stop,
)
if err != nil {
return err
}
vm.gossiper = txGossiper
vm.verifiedSubscriptions = append(vm.verifiedSubscriptions, event.SubscriptionFunc[*chain.ExecutedBlock]{
NotifyF: func(_ context.Context, b *chain.ExecutedBlock) error {
txGossiper.BlockVerified(b.Block.Tmstmp)
return nil
},
})
}
return nil
}
func (vm *VM) checkActivity(ctx context.Context) {
vm.gossiper.Queue(ctx)
vm.builder.Queue(ctx)
}
func (vm *VM) markReady() {
// Wait for state syncing to complete
select {
case <-vm.stop:
return
case <-vm.StateSyncClient.Done():
}
// We can begin partailly verifying blocks here because
// we have the full state but can't detect duplicate transactions
// because we haven't yet observed a full [ValidityWindow].
vm.snowCtx.Log.Info("state sync client ready")
// Wait for a full [ValidityWindow] before
// we are willing to vote on blocks.
select {
case <-vm.stop:
return
case <-vm.seenValidityWindow:
}
vm.snowCtx.Log.Info("validity window ready")
if vm.StateSyncClient.Started() {
vm.toEngine <- common.StateSyncDone
}
close(vm.ready)
// Mark node ready and attempt to build a block.
vm.snowCtx.Log.Info(
"node is now ready",
zap.Bool("synced", vm.StateSyncClient.Started()),
)
vm.checkActivity(context.TODO())
}
func (vm *VM) IsReady() bool {
select {
case <-vm.ready:
return true
default:
vm.snowCtx.Log.Info("node is not ready yet")
return false
}
}
func (vm *VM) ReadState(ctx context.Context, keys [][]byte) ([][]byte, []error) {
if !vm.IsReady() {
return utils.Repeat[[]byte](nil, len(keys)), utils.Repeat(ErrNotReady, len(keys))
}
// Atomic read to ensure consistency
return vm.stateDB.GetValues(ctx, keys)
}
func (vm *VM) SetState(ctx context.Context, state snow.State) error {
switch state {
case snow.StateSyncing:
vm.Logger().Info("state sync started")
return nil
case snow.Bootstrapping:
// Ensure state sync client marks itself as done if it was never started
syncStarted := vm.StateSyncClient.Started()
if !syncStarted {
// We must check if we finished syncing before starting bootstrapping.
// This should only ever occur if we began a state sync, restarted, and
// were unable to find any acceptable summaries.
syncing, err := vm.GetDiskIsSyncing()
if err != nil {
vm.Logger().Error("could not determine if syncing", zap.Error(err))
return err
}
if syncing {
vm.Logger().Error("cannot start bootstrapping", zap.Error(ErrStateSyncing))
// This is a fatal error that will require retrying sync or deleting the
// node database.
return ErrStateSyncing
}
// If we weren't previously syncing, we force state syncer completion so
// that the node will mark itself as ready.
vm.StateSyncClient.ForceDone()
// TODO: add a config to FATAL here if could not state sync (likely won't be
// able to recover in networks where no one has the full state, bypass
// still starts sync): https://github.com/ava-labs/hypersdk/issues/438
}
// Start the chain syncer and mark the validity window as completed if possible.
seenValidityWindow, err := vm.syncer.Accept(ctx, vm.lastAccepted.ExecutionBlock)
if err != nil {
return err
}
if seenValidityWindow {
vm.seenValidityWindowOnce.Do(func() {
close(vm.seenValidityWindow)
})
}
// Trigger that bootstrapping has started
vm.Logger().Info("bootstrapping started", zap.Bool("state sync started", syncStarted))
return vm.onBootstrapStarted()
case snow.NormalOp:
vm.Logger().
Info("normal operation started", zap.Bool("state sync started", vm.StateSyncClient.Started()))
return vm.onNormalOperationsStarted()
default:
return snow.ErrUnknownState
}
}
// onBootstrapStarted marks this VM as bootstrapping
func (vm *VM) onBootstrapStarted() error {
vm.bootstrapped.Set(false)
return nil
}
// ForceReady is used in integration testing
func (vm *VM) ForceReady() {
// Only works if haven't already started syncing
vm.StateSyncClient.ForceDone()
vm.seenValidityWindowOnce.Do(func() {
close(vm.seenValidityWindow)
})
}
// onNormalOperationsStarted marks this VM as bootstrapped
func (vm *VM) onNormalOperationsStarted() error {
defer vm.checkActivity(context.TODO())
if vm.bootstrapped.Get() {
return nil
}
vm.bootstrapped.Set(true)
return nil
}
// implements "block.ChainVM.common.VM"
func (vm *VM) Shutdown(context.Context) error {
close(vm.stop)
// Shutdown state sync client if still running
if err := vm.StateSyncClient.Shutdown(); err != nil {
return err
}
// Process remaining accepted blocks before shutdown
close(vm.acceptedQueue)
<-vm.acceptorDone
// Shutdown other async VM mechanisms
vm.builder.Done()
vm.gossiper.Done()
vm.authVerifiers.Stop()
if vm.profiler != nil {
vm.profiler.Shutdown()
}
// Close DBs
if vm.snowCtx == nil {
return nil
}
if err := vm.vmDB.Close(); err != nil {
return err
}
if err := vm.stateDB.Close(); err != nil {
return err
}
if err := vm.rawStateDB.Close(); err != nil {
return err
}
for _, subscription := range vm.asyncAcceptedSubscriptions {
if err := subscription.Close(); err != nil {
return err
}
}
return nil
}
// implements "block.ChainVM.common.VM"
// TODO: this must be callable in the factory before initializing
func (vm *VM) Version(_ context.Context) (string, error) { return vm.v.String(), nil }
// implements "block.ChainVM.common.VM"
// for "ext/vm/[chainID]"
func (vm *VM) CreateHandlers(_ context.Context) (map[string]http.Handler, error) {
return vm.handlers, nil
}
// implements "block.ChainVM.commom.VM.health.Checkable"
func (vm *VM) HealthCheck(context.Context) (interface{}, error) {
// TODO: engine will mark VM as ready when we return
// [block.StateSyncDynamic]. This should change in v1.9.11.
//
// We return "unhealthy" here until synced to block RPC traffic in the
// meantime.
if !vm.IsReady() {
return http.StatusServiceUnavailable, ErrNotReady
}
return http.StatusOK, nil
}
// implements "block.ChainVM.commom.VM.Getter"
// replaces "core.SnowmanVM.GetBlock"
//
// This is ONLY called on accepted blocks pre-ProposerVM fork.
func (vm *VM) GetBlock(ctx context.Context, id ids.ID) (snowman.Block, error) {
ctx, span := vm.tracer.Start(ctx, "VM.GetBlock")
defer span.End()
// We purposely don't return parsed but unverified blocks from here
return vm.GetStatefulBlock(ctx, id)
}
func (vm *VM) GetStatefulBlock(ctx context.Context, blkID ids.ID) (*StatefulBlock, error) {
_, span := vm.tracer.Start(ctx, "VM.GetStatefulBlock")
defer span.End()
// Check if verified block
vm.verifiedL.RLock()
if blk, exists := vm.verifiedBlocks[blkID]; exists {
vm.verifiedL.RUnlock()
return blk, nil
}
vm.verifiedL.RUnlock()
// Check if last accepted
if vm.lastAccepted.ID() == blkID {
return vm.lastAccepted, nil
}
// Check if genesis
if vm.genesisBlk.ID() == blkID {
return vm.genesisBlk, nil
}
// Check if recently accepted block
if blk, ok := vm.acceptedBlocksByID.Get(blkID); ok {
return blk, nil
}
// Check to see if the block is on disk
blkHeight, err := vm.GetBlockIDHeight(blkID)
if err != nil {
return nil, err
}
// We wait to count this metric until we know we have
// the index on-disk because peers may query us for
// blocks we don't have yet at tip and we don't want
// to count that as a historical read.
vm.metrics.blocksFromDisk.Inc()
return vm.GetDiskBlock(ctx, blkHeight)
}
func (vm *VM) ParseStatefulBlock(ctx context.Context, source []byte) (*StatefulBlock, error) {
start := time.Now()
defer func() {
vm.metrics.blockParse.Observe(float64(time.Since(start)))
}()
ctx, span := vm.tracer.Start(ctx, "VM.ParseBlock")
defer span.End()
// Check to see if we've already parsed
id := utils.ToID(source)
// If we have seen this block before, return it with the most
// up-to-date info
if oldBlk, err := vm.GetStatefulBlock(ctx, id); err == nil {
vm.snowCtx.Log.Debug("returning previously parsed block", zap.Stringer("id", oldBlk.ID()))
return oldBlk, nil
}
// Attempt to parse and cache block
blk, exist := vm.parsedBlocks.Get(id)
if exist {
return blk, nil
}
newBlk, err := ParseBlock(
ctx,
source,
false,
vm,
)
if err != nil {
vm.snowCtx.Log.Error("could not parse block", zap.Stringer("blkID", id), zap.Error(err))
return nil, err
}
vm.parsedBlocks.Put(id, newBlk)
vm.snowCtx.Log.Info(
"parsed block",
zap.Stringer("id", newBlk.ID()),
zap.Uint64("height", newBlk.Hght),
)
return newBlk, nil
}
// implements "block.ChainVM.commom.VM.Parser"
func (vm *VM) ParseBlock(ctx context.Context, source []byte) (snowman.Block, error) {
return vm.ParseStatefulBlock(ctx, source)
}
// implements "block.ChainVM"
func (vm *VM) BuildBlock(ctx context.Context) (snowman.Block, error) {
start := time.Now()
defer func() {
vm.metrics.blockBuild.Observe(float64(time.Since(start)))
}()
ctx, span := vm.tracer.Start(ctx, "VM.BuildBlock")
defer span.End()
// If the node isn't ready, we should exit.
//
// We call [QueueNotify] when the VM becomes ready, so exiting
// early here should not cause us to stop producing blocks.
if !vm.IsReady() {
vm.snowCtx.Log.Warn("not building block", zap.Error(ErrNotReady))
return nil, ErrNotReady
}
// Notify builder if we should build again (whether or not we are successful this time)
//
// Note: builder should regulate whether or not it actually decides to build based on state
// of the mempool.
defer vm.checkActivity(ctx)
vm.verifiedL.RLock()
processingBlocks := len(vm.verifiedBlocks)
vm.verifiedL.RUnlock()
if processingBlocks > vm.config.ProcessingBuildSkip {
vm.snowCtx.Log.Warn("not building block", zap.Error(ErrTooManyProcessing))
return nil, ErrTooManyProcessing
}
// Build block and store as parsed
preferredBlk, err := vm.GetStatefulBlock(ctx, vm.preferred)
if err != nil {
vm.snowCtx.Log.Warn("unable to get preferred block", zap.Error(err))
return nil, err
}
preferredView, err := preferredBlk.View(ctx, true)
if err != nil {
vm.snowCtx.Log.Warn("unable to get preferred block view", zap.Error(err))
return nil, err
}
executionBlk, executedBlk, view, err := vm.chain.BuildBlock(ctx, preferredView, preferredBlk.ExecutionBlock)
if err != nil {
// This is a DEBUG log because BuildBlock may fail before
// the min build gap (especially when there are no transactions).
vm.snowCtx.Log.Debug("BuildBlock failed", zap.Error(err))
return nil, err
}
blk := &StatefulBlock{
ExecutionBlock: executionBlk,
accepted: false,
t: time.UnixMilli(executionBlk.Tmstmp),
executedBlock: executedBlk,
vm: vm,
executor: vm.chain,
view: view,
}
vm.parsedBlocks.Put(blk.ID(), blk)
return blk, nil
}