44package extension
55
66import (
7+ "context"
78 "errors"
89
10+ "github.com/ava-labs/avalanchego/database"
11+ "github.com/ava-labs/avalanchego/database/versiondb"
12+ "github.com/ava-labs/avalanchego/ids"
13+ "github.com/ava-labs/avalanchego/network/p2p"
14+ "github.com/ava-labs/avalanchego/snow/consensus/snowman"
15+ "github.com/ava-labs/avalanchego/snow/engine/snowman/block"
916 "github.com/ava-labs/avalanchego/utils/timer/mockable"
17+ "github.com/ava-labs/libevm/common"
18+ "github.com/ava-labs/libevm/core/types"
19+ "github.com/prometheus/client_golang/prometheus"
1020
21+ "github.com/ava-labs/subnet-evm/consensus/dummy"
22+ "github.com/ava-labs/subnet-evm/core"
23+ "github.com/ava-labs/subnet-evm/params"
24+ "github.com/ava-labs/subnet-evm/params/extras"
25+ "github.com/ava-labs/subnet-evm/plugin/evm/config"
1126 "github.com/ava-labs/subnet-evm/plugin/evm/message"
1227 "github.com/ava-labs/subnet-evm/plugin/evm/sync"
1328 "github.com/ava-labs/subnet-evm/sync/handlers"
29+
30+ avalanchecommon "github.com/ava-labs/avalanchego/snow/engine/common"
1431)
1532
1633var (
2037 errNilClock = errors .New ("nil clock" )
2138)
2239
40+ type ExtensibleVM interface {
41+ // SetExtensionConfig sets the configuration for the VM extension
42+ // Should be called before any other method and only once
43+ SetExtensionConfig (config * Config ) error
44+ // NewClient returns a client to send messages with for the given protocol
45+ NewClient (protocol uint64 ) * p2p.Client
46+ // AddHandler registers a server handler for an application protocol
47+ AddHandler (protocol uint64 , handler p2p.Handler ) error
48+ // GetExtendedBlock returns the VMBlock for the given ID or an error if the block is not found
49+ GetExtendedBlock (context.Context , ids.ID ) (ExtendedBlock , error )
50+ // LastAcceptedExtendedBlock returns the last accepted VM block
51+ LastAcceptedExtendedBlock () ExtendedBlock
52+ // ChainConfig returns the chain config for the VM
53+ ChainConfig () * params.ChainConfig
54+ // P2PValidators returns the validators for the network
55+ P2PValidators () * p2p.Validators
56+ // Blockchain returns the blockchain client
57+ Blockchain () * core.BlockChain
58+ // Config returns the configuration for the VM
59+ Config () config.Config
60+ // MetricRegistry returns the metric registry for the VM
61+ MetricRegistry () * prometheus.Registry
62+ // ReadLastAccepted returns the last accepted block hash and height
63+ ReadLastAccepted () (common.Hash , uint64 , error )
64+ // VersionDB returns the versioned database for the VM
65+ VersionDB () * versiondb.Database
66+ }
67+
68+ // InnerVM is the interface that must be implemented by the VM
69+ // that's being wrapped by the extension
70+ type InnerVM interface {
71+ ExtensibleVM
72+ avalanchecommon.VM
73+ block.ChainVM
74+ block.BuildBlockWithContextChainVM
75+ block.StateSyncableVM
76+ }
77+
78+ // ExtendedBlock is a block that can be used by the extension
79+ type ExtendedBlock interface {
80+ snowman.Block
81+ GetEthBlock () * types.Block
82+ GetBlockExtension () BlockExtension
83+ }
84+
85+ type BlockExtender interface {
86+ // NewBlockExtension is called when a new block is created
87+ NewBlockExtension (b ExtendedBlock ) (BlockExtension , error )
88+ }
89+
90+ // BlockExtension allows the VM extension to handle block processing events.
91+ type BlockExtension interface {
92+ // SyntacticVerify verifies the block syntactically
93+ // it can be implemented to extend inner block verification
94+ SyntacticVerify (rules extras.Rules ) error
95+ // SemanticVerify verifies the block semantically
96+ // it can be implemented to extend inner block verification
97+ SemanticVerify () error
98+ // CleanupVerified is called when a block has passed SemanticVerify and SynctacticVerify,
99+ // and should be cleaned up due to error or verification runs under non-write mode. This
100+ // does not return an error because the block has already been verified.
101+ CleanupVerified ()
102+ // Accept is called when a block is accepted by the block manager. Accept takes a
103+ // database.Batch that contains the changes that were made to the database as a result
104+ // of accepting the block. The changes in the batch should be flushed to the database in this method.
105+ Accept (acceptedBatch database.Batch ) error
106+ // Reject is called when a block is rejected by the block manager
107+ Reject () error
108+ }
109+
110+ // BuilderMempool is a mempool that's used in the block builder
111+ type BuilderMempool interface {
112+ // PendingLen returns the number of pending transactions
113+ // that are waiting to be included in a block
114+ PendingLen () int
115+ // SubscribePendingTxs returns a channel that's signaled when there are pending transactions
116+ SubscribePendingTxs () <- chan struct {}
117+ }
118+
23119// LeafRequestConfig is the configuration to handle leaf requests
24120// in the network and syncer
25121type LeafRequestConfig struct {
@@ -33,13 +129,29 @@ type LeafRequestConfig struct {
33129
34130// Config is the configuration for the VM extension
35131type Config struct {
132+ // ConsensusCallbacks is the consensus callbacks to use
133+ // for the VM to be used in consensus engine.
134+ // Callback functions can be nil.
135+ ConsensusCallbacks dummy.ConsensusCallbacks
36136 // SyncSummaryProvider is the sync summary provider to use
37137 // for the VM to be used in syncer.
38138 // It's required and should be non-nil
39139 SyncSummaryProvider sync.SummaryProvider
140+ // SyncExtender can extend the syncer to handle custom sync logic.
141+ // It's optional and can be nil
142+ SyncExtender sync.Extender
40143 // SyncableParser is to parse summary messages from the network.
41144 // It's required and should be non-nil
42145 SyncableParser message.SyncableParser
146+ // BlockExtender allows the VM extension to create an extension to handle block processing events.
147+ // It's optional and can be nil
148+ BlockExtender BlockExtender
149+ // ExtraSyncLeafHandlerConfig is the extra configuration to handle leaf requests
150+ // in the network and syncer. It's optional and can be nil
151+ ExtraSyncLeafHandlerConfig * LeafRequestConfig
152+ // ExtraMempool is the mempool to be used in the block builder.
153+ // It's optional and can be nil
154+ ExtraMempool BuilderMempool
43155 // Clock is the clock to use for time related operations.
44156 // It's optional and can be nil
45157 Clock * mockable.Clock
0 commit comments