-
Notifications
You must be signed in to change notification settings - Fork 217
/
vm.go
1108 lines (965 loc) · 37.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
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package evm
import (
"context"
"encoding/json"
"errors"
"fmt"
"math/big"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"time"
avalanchegoMetrics "github.com/ava-labs/avalanchego/api/metrics"
"github.com/ava-labs/avalanchego/network/p2p"
"github.com/ava-labs/avalanchego/network/p2p/gossip"
avalanchegoConstants "github.com/ava-labs/avalanchego/utils/constants"
"github.com/prometheus/client_golang/prometheus"
"github.com/ava-labs/subnet-evm/commontype"
"github.com/ava-labs/subnet-evm/constants"
"github.com/ava-labs/subnet-evm/core"
"github.com/ava-labs/subnet-evm/core/rawdb"
"github.com/ava-labs/subnet-evm/core/txpool"
"github.com/ava-labs/subnet-evm/core/types"
"github.com/ava-labs/subnet-evm/eth"
"github.com/ava-labs/subnet-evm/eth/ethconfig"
"github.com/ava-labs/subnet-evm/metrics"
subnetEVMPrometheus "github.com/ava-labs/subnet-evm/metrics/prometheus"
"github.com/ava-labs/subnet-evm/miner"
"github.com/ava-labs/subnet-evm/node"
"github.com/ava-labs/subnet-evm/params"
"github.com/ava-labs/subnet-evm/peer"
"github.com/ava-labs/subnet-evm/plugin/evm/message"
"github.com/ava-labs/subnet-evm/rpc"
statesyncclient "github.com/ava-labs/subnet-evm/sync/client"
"github.com/ava-labs/subnet-evm/sync/client/stats"
"github.com/ava-labs/subnet-evm/trie"
"github.com/ava-labs/subnet-evm/warp"
warpValidators "github.com/ava-labs/subnet-evm/warp/validators"
// Force-load tracer engine to trigger registration
//
// We must import this package (not referenced elsewhere) so that the native "callTracer"
// is added to a map of client-accessible tracers. In geth, this is done
// inside of cmd/geth.
_ "github.com/ava-labs/subnet-evm/eth/tracers/js"
_ "github.com/ava-labs/subnet-evm/eth/tracers/native"
"github.com/ava-labs/subnet-evm/precompile/precompileconfig"
// Force-load precompiles to trigger registration
_ "github.com/ava-labs/subnet-evm/precompile/registry"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp"
avalancheRPC "github.com/gorilla/rpc/v2"
"github.com/ava-labs/avalanchego/codec"
"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/snowman/block"
"github.com/ava-labs/avalanchego/utils/perms"
"github.com/ava-labs/avalanchego/utils/profiler"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/avalanchego/utils/units"
"github.com/ava-labs/avalanchego/vms/components/chain"
commonEng "github.com/ava-labs/avalanchego/snow/engine/common"
avalancheJSON "github.com/ava-labs/avalanchego/utils/json"
)
var (
_ block.ChainVM = &VM{}
_ block.BuildBlockWithContextChainVM = &VM{}
)
const (
// Max time from current time allowed for blocks, before they're considered future blocks
// and fail verification
maxFutureBlockTime = 10 * time.Second
decidedCacheSize = 10 * units.MiB
missingCacheSize = 50
unverifiedCacheSize = 5 * units.MiB
bytesToIDCacheSize = 5 * units.MiB
warpSignatureCacheSize = 500
// Prefixes for metrics gatherers
ethMetricsPrefix = "eth"
chainStateMetricsPrefix = "chain_state"
// p2p app protocols
ethTxGossipProtocol = 0x0
// gossip constants
txGossipBloomMinTargetElements = 8 * 1024
txGossipBloomTargetFalsePositiveRate = 0.01
txGossipBloomResetFalsePositiveRate = 0.05
txGossipBloomChurnMultiplier = 3
txGossipTargetMessageSize = 20 * units.KiB
maxValidatorSetStaleness = time.Minute
txGossipThrottlingPeriod = 10 * time.Second
txGossipThrottlingLimit = 2
gossipFrequency = 10 * time.Second
txGossipPollSize = 10
)
// Define the API endpoints for the VM
const (
adminEndpoint = "/admin"
ethRPCEndpoint = "/rpc"
ethWSEndpoint = "/ws"
ethTxGossipNamespace = "eth_tx_gossip"
)
var (
// Set last accepted key to be longer than the keys used to store accepted block IDs.
lastAcceptedKey = []byte("last_accepted_key")
acceptedPrefix = []byte("snowman_accepted")
metadataPrefix = []byte("metadata")
warpPrefix = []byte("warp")
ethDBPrefix = []byte("ethdb")
)
var (
errEmptyBlock = errors.New("empty block")
errUnsupportedFXs = errors.New("unsupported feature extensions")
errInvalidBlock = errors.New("invalid block")
errInvalidNonce = errors.New("invalid nonce")
errUnclesUnsupported = errors.New("uncles unsupported")
errNilBaseFeeSubnetEVM = errors.New("nil base fee is invalid after subnetEVM")
errNilBlockGasCostSubnetEVM = errors.New("nil blockGasCost is invalid after subnetEVM")
errInvalidHeaderPredicateResults = errors.New("invalid header predicate results")
)
// legacyApiNames maps pre geth v1.10.20 api names to their updated counterparts.
// used in attachEthService for backward configuration compatibility.
var legacyApiNames = map[string]string{
"internal-public-eth": "internal-eth",
"internal-public-blockchain": "internal-blockchain",
"internal-public-transaction-pool": "internal-transaction",
"internal-public-tx-pool": "internal-tx-pool",
"internal-public-debug": "internal-debug",
"internal-private-debug": "internal-debug",
"internal-public-account": "internal-account",
"internal-private-personal": "internal-personal",
"public-eth": "eth",
"public-eth-filter": "eth-filter",
"private-admin": "admin",
"public-debug": "debug",
"private-debug": "debug",
}
// VM implements the snowman.ChainVM interface
type VM struct {
ctx *snow.Context
// [cancel] may be nil until [snow.NormalOp] starts
cancel context.CancelFunc
// *chain.State helps to implement the VM interface by wrapping blocks
// with an efficient caching layer.
*chain.State
config Config
networkID uint64
genesisHash common.Hash
chainConfig *params.ChainConfig
ethConfig ethconfig.Config
// pointers to eth constructs
eth *eth.Ethereum
txPool *txpool.TxPool
blockChain *core.BlockChain
miner *miner.Miner
// [db] is the VM's current database managed by ChainState
db *versiondb.Database
// metadataDB is used to store one off keys.
metadataDB database.Database
// [chaindb] is the database supplied to the Ethereum backend
chaindb ethdb.Database
// [acceptedBlockDB] is the database to store the last accepted
// block.
acceptedBlockDB database.Database
// [warpDB] is used to store warp message signatures
// set to a prefixDB with the prefix [warpPrefix]
warpDB database.Database
toEngine chan<- commonEng.Message
syntacticBlockValidator BlockValidator
builder *blockBuilder
gossiper Gossiper
clock mockable.Clock
shutdownChan chan struct{}
shutdownWg sync.WaitGroup
// Continuous Profiler
profiler profiler.ContinuousProfiler
peer.Network
client peer.NetworkClient
networkCodec codec.Manager
validators *p2p.Validators
// Metrics
multiGatherer avalanchegoMetrics.MultiGatherer
sdkMetrics *prometheus.Registry
bootstrapped bool
logger SubnetEVMLogger
// State sync server and client
StateSyncServer
StateSyncClient
// Avalanche Warp Messaging backend
// Used to serve BLS signatures of warp messages over RPC
warpBackend warp.Backend
// Initialize only sets these if nil so they can be overridden in tests
p2pSender commonEng.AppSender
ethTxGossipHandler p2p.Handler
ethTxPullGossiper gossip.Gossiper
ethTxPushGossiper gossip.Accumulator[*GossipEthTx]
}
// Initialize implements the snowman.ChainVM interface
func (vm *VM) Initialize(
_ context.Context,
chainCtx *snow.Context,
db database.Database,
genesisBytes []byte,
upgradeBytes []byte,
configBytes []byte,
toEngine chan<- commonEng.Message,
fxs []*commonEng.Fx,
appSender commonEng.AppSender,
) error {
vm.config.SetDefaults()
if len(configBytes) > 0 {
if err := json.Unmarshal(configBytes, &vm.config); err != nil {
return fmt.Errorf("failed to unmarshal config %s: %w", string(configBytes), err)
}
}
if err := vm.config.Validate(); err != nil {
return err
}
vm.ctx = chainCtx
// Create logger
alias, err := vm.ctx.BCLookup.PrimaryAlias(vm.ctx.ChainID)
if err != nil {
// fallback to ChainID string instead of erroring
alias = vm.ctx.ChainID.String()
}
subnetEVMLogger, err := InitLogger(alias, vm.config.LogLevel, vm.config.LogJSONFormat, vm.ctx.Log)
if err != nil {
return fmt.Errorf("failed to initialize logger due to: %w ", err)
}
vm.logger = subnetEVMLogger
log.Info("Initializing Subnet EVM VM", "Version", Version, "Config", vm.config)
if len(fxs) > 0 {
return errUnsupportedFXs
}
// Enable debug-level metrics that might impact runtime performance
metrics.EnabledExpensive = vm.config.MetricsExpensiveEnabled
vm.toEngine = toEngine
vm.shutdownChan = make(chan struct{}, 1)
// Use NewNested rather than New so that the structure of the database
// remains the same regardless of the provided baseDB type.
vm.chaindb = rawdb.NewDatabase(Database{prefixdb.NewNested(ethDBPrefix, db)})
vm.db = versiondb.New(db)
vm.acceptedBlockDB = prefixdb.New(acceptedPrefix, vm.db)
vm.metadataDB = prefixdb.New(metadataPrefix, vm.db)
// Note warpDB is not part of versiondb because it is not necessary
// that warp signatures are committed to the database atomically with
// the last accepted block.
vm.warpDB = prefixdb.New(warpPrefix, db)
if vm.config.InspectDatabase {
start := time.Now()
log.Info("Starting database inspection")
if err := rawdb.InspectDatabase(vm.chaindb, nil, nil); err != nil {
return err
}
log.Info("Completed database inspection", "elapsed", time.Since(start))
}
g := new(core.Genesis)
if err := json.Unmarshal(genesisBytes, g); err != nil {
return err
}
if g.Config == nil {
g.Config = params.SubnetEVMDefaultChainConfig
}
mandatoryNetworkUpgrades, enforce := getMandatoryNetworkUpgrades(chainCtx.NetworkID)
if enforce {
// We enforce network upgrades here, regardless of the chain config
// provided in the genesis file
g.Config.MandatoryNetworkUpgrades = mandatoryNetworkUpgrades
} else {
// If we are not enforcing, then apply those only if they are not
// already set in the genesis file
if g.Config.MandatoryNetworkUpgrades == (params.MandatoryNetworkUpgrades{}) {
g.Config.MandatoryNetworkUpgrades = mandatoryNetworkUpgrades
}
}
// Load airdrop file if provided
if vm.config.AirdropFile != "" {
g.AirdropData, err = os.ReadFile(vm.config.AirdropFile)
if err != nil {
return fmt.Errorf("could not read airdrop file '%s': %w", vm.config.AirdropFile, err)
}
}
// Set the Avalanche Context on the ChainConfig
g.Config.AvalancheContext = params.AvalancheContext{
SnowCtx: chainCtx,
}
vm.syntacticBlockValidator = NewBlockValidator()
if g.Config.FeeConfig == commontype.EmptyFeeConfig {
log.Info("No fee config given in genesis, setting default fee config", "DefaultFeeConfig", params.DefaultFeeConfig)
g.Config.FeeConfig = params.DefaultFeeConfig
}
// Apply upgradeBytes (if any) by unmarshalling them into [chainConfig.UpgradeConfig].
// Initializing the chain will verify upgradeBytes are compatible with existing values.
// This should be called before g.Verify().
if len(upgradeBytes) > 0 {
var upgradeConfig params.UpgradeConfig
if err := json.Unmarshal(upgradeBytes, &upgradeConfig); err != nil {
return fmt.Errorf("failed to parse upgrade bytes: %w", err)
}
g.Config.UpgradeConfig = upgradeConfig
}
if err := g.Verify(); err != nil {
return fmt.Errorf("failed to verify genesis: %w", err)
}
vm.ethConfig = ethconfig.NewDefaultConfig()
vm.ethConfig.Genesis = g
// NetworkID here is different than Avalanche's NetworkID.
// Avalanche's NetworkID represents the Avalanche network is running on
// like Fuji, Mainnet, Local, etc.
// The NetworkId here is kept same as ChainID to be compatible with
// Ethereum tooling.
vm.ethConfig.NetworkId = g.Config.ChainID.Uint64()
// Set minimum price for mining and default gas price oracle value to the min
// gas price to prevent so transactions and blocks all use the correct fees
vm.ethConfig.RPCGasCap = vm.config.RPCGasCap
vm.ethConfig.RPCEVMTimeout = vm.config.APIMaxDuration.Duration
vm.ethConfig.RPCTxFeeCap = vm.config.RPCTxFeeCap
vm.ethConfig.TxPool.Locals = vm.config.PriorityRegossipAddresses
vm.ethConfig.TxPool.NoLocals = !vm.config.LocalTxsEnabled
vm.ethConfig.TxPool.Journal = vm.config.TxPoolJournal
vm.ethConfig.TxPool.Rejournal = vm.config.TxPoolRejournal.Duration
vm.ethConfig.TxPool.PriceLimit = vm.config.TxPoolPriceLimit
vm.ethConfig.TxPool.PriceBump = vm.config.TxPoolPriceBump
vm.ethConfig.TxPool.AccountSlots = vm.config.TxPoolAccountSlots
vm.ethConfig.TxPool.GlobalSlots = vm.config.TxPoolGlobalSlots
vm.ethConfig.TxPool.AccountQueue = vm.config.TxPoolAccountQueue
vm.ethConfig.TxPool.GlobalQueue = vm.config.TxPoolGlobalQueue
vm.ethConfig.AllowUnfinalizedQueries = vm.config.AllowUnfinalizedQueries
vm.ethConfig.AllowUnprotectedTxs = vm.config.AllowUnprotectedTxs
vm.ethConfig.AllowUnprotectedTxHashes = vm.config.AllowUnprotectedTxHashes
vm.ethConfig.Preimages = vm.config.Preimages
vm.ethConfig.Pruning = vm.config.Pruning
vm.ethConfig.TrieCleanCache = vm.config.TrieCleanCache
vm.ethConfig.TrieCleanJournal = vm.config.TrieCleanJournal
vm.ethConfig.TrieCleanRejournal = vm.config.TrieCleanRejournal.Duration
vm.ethConfig.TrieDirtyCache = vm.config.TrieDirtyCache
vm.ethConfig.TrieDirtyCommitTarget = vm.config.TrieDirtyCommitTarget
vm.ethConfig.SnapshotCache = vm.config.SnapshotCache
vm.ethConfig.AcceptorQueueLimit = vm.config.AcceptorQueueLimit
vm.ethConfig.PopulateMissingTries = vm.config.PopulateMissingTries
vm.ethConfig.PopulateMissingTriesParallelism = vm.config.PopulateMissingTriesParallelism
vm.ethConfig.AllowMissingTries = vm.config.AllowMissingTries
vm.ethConfig.SnapshotDelayInit = vm.config.StateSyncEnabled
vm.ethConfig.SnapshotWait = vm.config.SnapshotWait
vm.ethConfig.SnapshotVerify = vm.config.SnapshotVerify
vm.ethConfig.OfflinePruning = vm.config.OfflinePruning
vm.ethConfig.OfflinePruningBloomFilterSize = vm.config.OfflinePruningBloomFilterSize
vm.ethConfig.OfflinePruningDataDirectory = vm.config.OfflinePruningDataDirectory
vm.ethConfig.CommitInterval = vm.config.CommitInterval
vm.ethConfig.SkipUpgradeCheck = vm.config.SkipUpgradeCheck
vm.ethConfig.AcceptedCacheSize = vm.config.AcceptedCacheSize
vm.ethConfig.TxLookupLimit = vm.config.TxLookupLimit
// Create directory for offline pruning
if len(vm.ethConfig.OfflinePruningDataDirectory) != 0 {
if err := os.MkdirAll(vm.ethConfig.OfflinePruningDataDirectory, perms.ReadWriteExecute); err != nil {
log.Error("failed to create offline pruning data directory", "error", err)
return err
}
}
// Handle custom fee recipient
if common.IsHexAddress(vm.config.FeeRecipient) {
address := common.HexToAddress(vm.config.FeeRecipient)
log.Info("Setting fee recipient", "address", address)
vm.ethConfig.Miner.Etherbase = address
} else {
log.Info("Config has not specified any coinbase address. Defaulting to the blackhole address.")
vm.ethConfig.Miner.Etherbase = constants.BlackholeAddr
}
vm.chainConfig = g.Config
vm.networkID = vm.ethConfig.NetworkId
// create genesisHash after applying upgradeBytes in case
// upgradeBytes modifies genesis.
vm.genesisHash = vm.ethConfig.Genesis.ToBlock().Hash() // must create genesis hash before [vm.readLastAccepted]
lastAcceptedHash, lastAcceptedHeight, err := vm.readLastAccepted()
if err != nil {
return err
}
log.Info(fmt.Sprintf("lastAccepted = %s", lastAcceptedHash))
if err := vm.initializeMetrics(); err != nil {
return err
}
// initialize peer network
if vm.p2pSender == nil {
vm.p2pSender = appSender
}
p2pNetwork, err := p2p.NewNetwork(vm.ctx.Log, vm.p2pSender, vm.sdkMetrics, "p2p")
if err != nil {
return fmt.Errorf("failed to initialize p2p network: %w", err)
}
vm.validators = p2p.NewValidators(p2pNetwork.Peers, vm.ctx.Log, vm.ctx.SubnetID, vm.ctx.ValidatorState, maxValidatorSetStaleness)
vm.networkCodec = message.Codec
vm.Network = peer.NewNetwork(p2pNetwork, appSender, vm.networkCodec, message.CrossChainCodec, chainCtx.NodeID, vm.config.MaxOutboundActiveRequests, vm.config.MaxOutboundActiveCrossChainRequests)
vm.client = peer.NewNetworkClient(vm.Network)
// Initialize warp backend
offchainWarpMessages := make([][]byte, len(vm.config.WarpOffChainMessages))
for i, hexMsg := range vm.config.WarpOffChainMessages {
offchainWarpMessages[i] = []byte(hexMsg)
}
vm.warpBackend, err = warp.NewBackend(vm.ctx.NetworkID, vm.ctx.ChainID, vm.ctx.WarpSigner, vm, vm.warpDB, warpSignatureCacheSize, offchainWarpMessages)
if err != nil {
return err
}
// clear warpdb on initialization if config enabled
if vm.config.PruneWarpDB {
if err := vm.warpBackend.Clear(); err != nil {
return fmt.Errorf("failed to prune warpDB: %w", err)
}
}
if err := vm.initializeChain(lastAcceptedHash, vm.ethConfig); err != nil {
return err
}
go vm.ctx.Log.RecoverAndPanic(vm.startContinuousProfiler)
vm.initializeStateSyncServer()
return vm.initializeStateSyncClient(lastAcceptedHeight)
}
func (vm *VM) initializeMetrics() error {
vm.sdkMetrics = prometheus.NewRegistry()
vm.multiGatherer = avalanchegoMetrics.NewMultiGatherer()
// If metrics are enabled, register the default metrics regitry
if metrics.Enabled {
gatherer := subnetEVMPrometheus.Gatherer(metrics.DefaultRegistry)
if err := vm.multiGatherer.Register(ethMetricsPrefix, gatherer); err != nil {
return err
}
if err := vm.multiGatherer.Register("sdk", vm.sdkMetrics); err != nil {
return err
}
// Register [multiGatherer] after registerers have been registered to it
if err := vm.ctx.Metrics.Register(vm.multiGatherer); err != nil {
return err
}
}
return nil
}
func (vm *VM) initializeChain(lastAcceptedHash common.Hash, ethConfig ethconfig.Config) error {
nodecfg := &node.Config{
SubnetEVMVersion: Version,
KeyStoreDir: vm.config.KeystoreDirectory,
ExternalSigner: vm.config.KeystoreExternalSigner,
InsecureUnlockAllowed: vm.config.KeystoreInsecureUnlockAllowed,
}
node, err := node.New(nodecfg)
if err != nil {
return err
}
vm.eth, err = eth.New(
node,
&vm.ethConfig,
vm.chaindb,
vm.config.EthBackendSettings(),
lastAcceptedHash,
&vm.clock,
)
if err != nil {
return err
}
vm.eth.SetEtherbase(ethConfig.Miner.Etherbase)
vm.txPool = vm.eth.TxPool()
vm.txPool.SetMinFee(vm.chainConfig.FeeConfig.MinBaseFee)
vm.txPool.SetGasPrice(big.NewInt(0))
vm.blockChain = vm.eth.BlockChain()
vm.miner = vm.eth.Miner()
vm.eth.Start()
return vm.initChainState(vm.blockChain.LastAcceptedBlock())
}
// initializeStateSyncClient initializes the client for performing state sync.
// If state sync is disabled, this function will wipe any ongoing summary from
// disk to ensure that we do not continue syncing from an invalid snapshot.
func (vm *VM) initializeStateSyncClient(lastAcceptedHeight uint64) error {
// parse nodeIDs from state sync IDs in vm config
var stateSyncIDs []ids.NodeID
if vm.config.StateSyncEnabled && len(vm.config.StateSyncIDs) > 0 {
nodeIDs := strings.Split(vm.config.StateSyncIDs, ",")
stateSyncIDs = make([]ids.NodeID, len(nodeIDs))
for i, nodeIDString := range nodeIDs {
nodeID, err := ids.NodeIDFromString(nodeIDString)
if err != nil {
return fmt.Errorf("failed to parse %s as NodeID: %w", nodeIDString, err)
}
stateSyncIDs[i] = nodeID
}
}
vm.StateSyncClient = NewStateSyncClient(&stateSyncClientConfig{
chain: vm.eth,
state: vm.State,
client: statesyncclient.NewClient(
&statesyncclient.ClientConfig{
NetworkClient: vm.client,
Codec: vm.networkCodec,
Stats: stats.NewClientSyncerStats(),
StateSyncNodeIDs: stateSyncIDs,
BlockParser: vm,
},
),
enabled: vm.config.StateSyncEnabled,
skipResume: vm.config.StateSyncSkipResume,
stateSyncMinBlocks: vm.config.StateSyncMinBlocks,
stateSyncRequestSize: vm.config.StateSyncRequestSize,
lastAcceptedHeight: lastAcceptedHeight, // TODO clean up how this is passed around
chaindb: vm.chaindb,
metadataDB: vm.metadataDB,
acceptedBlockDB: vm.acceptedBlockDB,
db: vm.db,
toEngine: vm.toEngine,
})
// If StateSync is disabled, clear any ongoing summary so that we will not attempt to resume
// sync using a snapshot that has been modified by the node running normal operations.
if !vm.config.StateSyncEnabled {
return vm.StateSyncClient.StateSyncClearOngoingSummary()
}
return nil
}
// initializeStateSyncServer should be called after [vm.chain] is initialized.
func (vm *VM) initializeStateSyncServer() {
vm.StateSyncServer = NewStateSyncServer(&stateSyncServerConfig{
Chain: vm.blockChain,
SyncableInterval: vm.config.StateSyncCommitInterval,
})
vm.setAppRequestHandlers()
vm.setCrossChainAppRequestHandler()
}
func (vm *VM) initChainState(lastAcceptedBlock *types.Block) error {
block := vm.newBlock(lastAcceptedBlock)
block.status = choices.Accepted
config := &chain.Config{
DecidedCacheSize: decidedCacheSize,
MissingCacheSize: missingCacheSize,
UnverifiedCacheSize: unverifiedCacheSize,
BytesToIDCacheSize: bytesToIDCacheSize,
GetBlockIDAtHeight: vm.GetBlockIDAtHeight,
GetBlock: vm.getBlock,
UnmarshalBlock: vm.parseBlock,
BuildBlock: vm.buildBlock,
BuildBlockWithContext: vm.buildBlockWithContext,
LastAcceptedBlock: block,
}
// Register chain state metrics
chainStateRegisterer := prometheus.NewRegistry()
state, err := chain.NewMeteredState(chainStateRegisterer, config)
if err != nil {
return fmt.Errorf("could not create metered state: %w", err)
}
vm.State = state
return vm.multiGatherer.Register(chainStateMetricsPrefix, chainStateRegisterer)
}
func (vm *VM) SetState(_ context.Context, state snow.State) error {
switch state {
case snow.StateSyncing:
vm.bootstrapped = false
return nil
case snow.Bootstrapping:
vm.bootstrapped = false
if err := vm.StateSyncClient.Error(); err != nil {
return err
}
return nil
case snow.NormalOp:
// Initialize goroutines related to block building once we enter normal operation as there is no need to handle mempool gossip before this point.
if err := vm.initBlockBuilding(); err != nil {
return fmt.Errorf("failed to initialize block building: %w", err)
}
vm.bootstrapped = true
return nil
default:
return snow.ErrUnknownState
}
}
// initBlockBuilding starts goroutines to manage block building
func (vm *VM) initBlockBuilding() error {
ctx, cancel := context.WithCancel(context.TODO())
vm.cancel = cancel
ethTxGossipMarshaller := GossipEthTxMarshaller{}
ethTxGossipClient := vm.Network.NewClient(ethTxGossipProtocol, p2p.WithValidatorSampling(vm.validators))
ethTxGossipMetrics, err := gossip.NewMetrics(vm.sdkMetrics, ethTxGossipNamespace)
if err != nil {
return fmt.Errorf("failed to initialize eth tx gossip metrics: %w", err)
}
if vm.ethTxPushGossiper == nil {
vm.ethTxPushGossiper = gossip.NewPushGossiper[*GossipEthTx](
ethTxGossipMarshaller,
ethTxGossipClient,
ethTxGossipMetrics,
txGossipTargetMessageSize,
)
}
// NOTE: gossip network must be initialized first otherwise ETH tx gossip will not work.
gossipStats := NewGossipStats()
vm.gossiper = vm.createGossiper(gossipStats, vm.ethTxPushGossiper)
vm.builder = vm.NewBlockBuilder(vm.toEngine)
vm.builder.awaitSubmittedTxs()
vm.Network.SetGossipHandler(NewGossipHandler(vm, gossipStats))
ethTxPool, err := NewGossipEthTxPool(vm.txPool)
if err != nil {
return err
}
vm.shutdownWg.Add(1)
go func() {
ethTxPool.Subscribe(ctx)
vm.shutdownWg.Done()
}()
if vm.ethTxGossipHandler == nil {
vm.ethTxGossipHandler = newTxGossipHandler[*GossipEthTx](
vm.ctx.Log,
ethTxGossipMarshaller,
ethTxPool,
ethTxGossipMetrics,
txGossipTargetMessageSize,
txGossipThrottlingPeriod,
txGossipThrottlingLimit,
vm.validators,
)
}
if err := vm.Network.AddHandler(ethTxGossipProtocol, vm.ethTxGossipHandler); err != nil {
return err
}
if vm.ethTxPullGossiper == nil {
ethTxPullGossiper := gossip.NewPullGossiper[*GossipEthTx](
vm.ctx.Log,
ethTxGossipMarshaller,
ethTxPool,
ethTxGossipClient,
ethTxGossipMetrics,
txGossipPollSize,
)
vm.ethTxPullGossiper = gossip.ValidatorGossiper{
Gossiper: ethTxPullGossiper,
NodeID: vm.ctx.NodeID,
Validators: vm.validators,
}
}
vm.shutdownWg.Add(1)
go func() {
gossip.Every(ctx, vm.ctx.Log, vm.ethTxPullGossiper, gossipFrequency)
vm.shutdownWg.Done()
}()
return nil
}
// setAppRequestHandlers sets the request handlers for the VM to serve state sync
// requests.
func (vm *VM) setAppRequestHandlers() {
// Create separate EVM TrieDB (read only) for serving leafs requests.
// We create a separate TrieDB here, so that it has a separate cache from the one
// used by the node when processing blocks.
evmTrieDB := trie.NewDatabaseWithConfig(
vm.chaindb,
&trie.Config{
Cache: vm.config.StateSyncServerTrieCache,
},
)
networkHandler := newNetworkHandler(vm.blockChain, vm.chaindb, evmTrieDB, vm.warpBackend, vm.networkCodec)
vm.Network.SetRequestHandler(networkHandler)
}
// setCrossChainAppRequestHandler sets the request handlers for the VM to serve cross chain
// requests.
func (vm *VM) setCrossChainAppRequestHandler() {
crossChainRequestHandler := message.NewCrossChainHandler(vm.eth.APIBackend, message.CrossChainCodec)
vm.Network.SetCrossChainRequestHandler(crossChainRequestHandler)
}
// Shutdown implements the snowman.ChainVM interface
func (vm *VM) Shutdown(context.Context) error {
if vm.ctx == nil {
return nil
}
if vm.cancel != nil {
vm.cancel()
}
vm.Network.Shutdown()
if err := vm.StateSyncClient.Shutdown(); err != nil {
log.Error("error stopping state syncer", "err", err)
}
close(vm.shutdownChan)
vm.eth.Stop()
log.Info("Ethereum backend stop completed")
vm.shutdownWg.Wait()
log.Info("Subnet-EVM Shutdown completed")
return nil
}
func (vm *VM) buildBlock(ctx context.Context) (snowman.Block, error) {
return vm.buildBlockWithContext(ctx, nil)
}
func (vm *VM) buildBlockWithContext(ctx context.Context, proposerVMBlockCtx *block.Context) (snowman.Block, error) {
if proposerVMBlockCtx != nil {
log.Debug("Building block with context", "pChainBlockHeight", proposerVMBlockCtx.PChainHeight)
} else {
log.Debug("Building block without context")
}
predicateCtx := &precompileconfig.PredicateContext{
SnowCtx: vm.ctx,
ProposerVMBlockCtx: proposerVMBlockCtx,
}
block, err := vm.miner.GenerateBlock(predicateCtx)
vm.builder.handleGenerateBlock()
if err != nil {
return nil, err
}
// Note: the status of block is set by ChainState
blk := vm.newBlock(block)
// Verify is called on a non-wrapped block here, such that this
// does not add [blk] to the processing blocks map in ChainState.
//
// TODO cache verification since Verify() will be called by the
// consensus engine as well.
//
// Note: this is only called when building a new block, so caching
// verification will only be a significant optimization for nodes
// that produce a large number of blocks.
// We call verify without writes here to avoid generating a reference
// to the blk state root in the triedb when we are going to call verify
// again from the consensus engine with writes enabled.
if err := blk.verify(predicateCtx, false /*=writes*/); err != nil {
return nil, fmt.Errorf("block failed verification due to: %w", err)
}
log.Debug(fmt.Sprintf("Built block %s", blk.ID()))
// Marks the current transactions from the mempool as being successfully issued
// into a block.
return blk, nil
}
// parseBlock parses [b] into a block to be wrapped by ChainState.
func (vm *VM) parseBlock(_ context.Context, b []byte) (snowman.Block, error) {
ethBlock := new(types.Block)
if err := rlp.DecodeBytes(b, ethBlock); err != nil {
return nil, err
}
// Note: the status of block is set by ChainState
block := vm.newBlock(ethBlock)
// Performing syntactic verification in ParseBlock allows for
// short-circuiting bad blocks before they are processed by the VM.
if err := block.syntacticVerify(); err != nil {
return nil, fmt.Errorf("syntactic block verification failed: %w", err)
}
return block, nil
}
func (vm *VM) ParseEthBlock(b []byte) (*types.Block, error) {
block, err := vm.parseBlock(context.TODO(), b)
if err != nil {
return nil, err
}
return block.(*Block).ethBlock, nil
}
// getBlock attempts to retrieve block [id] from the VM to be wrapped
// by ChainState.
func (vm *VM) getBlock(_ context.Context, id ids.ID) (snowman.Block, error) {
ethBlock := vm.blockChain.GetBlockByHash(common.Hash(id))
// If [ethBlock] is nil, return [database.ErrNotFound] here
// so that the miss is considered cacheable.
if ethBlock == nil {
return nil, database.ErrNotFound
}
// Note: the status of block is set by ChainState
return vm.newBlock(ethBlock), nil
}
// SetPreference sets what the current tail of the chain is
func (vm *VM) SetPreference(ctx context.Context, blkID ids.ID) error {
// Since each internal handler used by [vm.State] always returns a block
// with non-nil ethBlock value, GetBlockInternal should never return a
// (*Block) with a nil ethBlock value.
block, err := vm.GetBlockInternal(ctx, blkID)
if err != nil {
return fmt.Errorf("failed to set preference to %s: %w", blkID, err)
}
return vm.blockChain.SetPreference(block.(*Block).ethBlock)
}
// VerifyHeightIndex always returns a nil error since the index is maintained by
// vm.blockChain.
func (vm *VM) VerifyHeightIndex(context.Context) error {
return nil
}
// GetBlockAtHeight returns the canonical block at [blkHeight].
// If [blkHeight] is less than the height of the last accepted block, this will return
// the block accepted at that height. Otherwise, it may return a blkID that has not yet
// been accepted.
// Note: the engine assumes that if a block is not found at [blkHeight], then
// [database.ErrNotFound] will be returned. This indicates that the VM has state synced
// and does not have all historical blocks available.
func (vm *VM) GetBlockIDAtHeight(_ context.Context, blkHeight uint64) (ids.ID, error) {
ethBlock := vm.blockChain.GetBlockByNumber(blkHeight)
if ethBlock == nil {
return ids.ID{}, database.ErrNotFound
}
return ids.ID(ethBlock.Hash()), nil
}
func (vm *VM) Version(context.Context) (string, error) {
return Version, nil
}
// NewHandler returns a new Handler for a service where:
// - The handler's functionality is defined by [service]
// [service] should be a gorilla RPC service (see https://www.gorillatoolkit.org/pkg/rpc/v2)
// - The name of the service is [name]
func newHandler(name string, service interface{}) (http.Handler, error) {
server := avalancheRPC.NewServer()
server.RegisterCodec(avalancheJSON.NewCodec(), "application/json")
server.RegisterCodec(avalancheJSON.NewCodec(), "application/json;charset=UTF-8")
return server, server.RegisterService(service, name)
}
// CreateHandlers makes new http handlers that can handle API calls
func (vm *VM) CreateHandlers(context.Context) (map[string]http.Handler, error) {
handler := rpc.NewServer(vm.config.APIMaxDuration.Duration)
enabledAPIs := vm.config.EthAPIs()
if err := attachEthService(handler, vm.eth.APIs(), enabledAPIs); err != nil {
return nil, err
}
primaryAlias, err := vm.ctx.BCLookup.PrimaryAlias(vm.ctx.ChainID)
if err != nil {
return nil, fmt.Errorf("failed to get primary alias for chain due to %w", err)
}
apis := make(map[string]http.Handler)
if vm.config.AdminAPIEnabled {
adminAPI, err := newHandler("admin", NewAdminService(vm, os.ExpandEnv(fmt.Sprintf("%s_subnet_evm_performance_%s", vm.config.AdminAPIDir, primaryAlias))))
if err != nil {
return nil, fmt.Errorf("failed to register service for admin API due to %w", err)
}
apis[adminEndpoint] = adminAPI
enabledAPIs = append(enabledAPIs, "subnet-evm-admin")
}
if vm.config.SnowmanAPIEnabled {
if err := handler.RegisterName("snowman", &SnowmanAPI{vm}); err != nil {
return nil, err
}
enabledAPIs = append(enabledAPIs, "snowman")
}
if vm.config.WarpAPIEnabled {
validatorsState := warpValidators.NewState(vm.ctx)
if err := handler.RegisterName("warp", warp.NewAPI(vm.ctx.NetworkID, vm.ctx.SubnetID, vm.ctx.ChainID, validatorsState, vm.warpBackend, vm.client)); err != nil {
return nil, err
}
enabledAPIs = append(enabledAPIs, "warp")
}
log.Info(fmt.Sprintf("Enabled APIs: %s", strings.Join(enabledAPIs, ", ")))
apis[ethRPCEndpoint] = handler
apis[ethWSEndpoint] = handler.WebsocketHandlerWithDuration(
[]string{"*"},
vm.config.APIMaxDuration.Duration,
vm.config.WSCPURefillRate.Duration,
vm.config.WSCPUMaxStored.Duration,
)
return apis, nil
}
// CreateStaticHandlers makes new http handlers that can handle API calls
func (vm *VM) CreateStaticHandlers(context.Context) (map[string]http.Handler, error) {
handler := rpc.NewServer(0)
if err := handler.RegisterName("static", &StaticService{}); err != nil {
return nil, err
}
return map[string]http.Handler{
"/rpc": handler,
}, nil
}
/*
******************************************************************************
*********************************** Helpers **********************************
******************************************************************************
*/
// GetCurrentNonce returns the nonce associated with the address at the
// preferred block
func (vm *VM) GetCurrentNonce(address common.Address) (uint64, error) {
// Note: current state uses the state of the preferred block.
state, err := vm.blockChain.State()
if err != nil {