From 109fa90470432ff4754651d4d81f1348c6370ffd Mon Sep 17 00:00:00 2001 From: rian Date: Fri, 16 Aug 2024 15:53:49 +0300 Subject: [PATCH] pass rpcHandler to plugin (+refactor for import cycle) --- node/node.go | 21 +++++++++++++-------- plugin/plugin.go | 20 ++++++-------------- plugin/sync/pluginsync.go | 18 ++++++++++++++++++ sync/sync.go | 2 +- 4 files changed, 38 insertions(+), 23 deletions(-) create mode 100644 plugin/sync/pluginsync.go diff --git a/node/node.go b/node/node.go index b4c77eebcf..e05caced76 100644 --- a/node/node.go +++ b/node/node.go @@ -158,14 +158,6 @@ func New(cfg *Config, version string) (*Node, error) { //nolint:gocyclo,funlen synchronizer := sync.New(chain, adaptfeeder.New(client), log, cfg.PendingPollInterval, dbIsRemote) gatewayClient := gateway.NewClient(cfg.Network.GatewayURL, log).WithUserAgent(ua).WithAPIKey(cfg.GatewayAPIKey) - if cfg.PluginPath != "" { - plugin, err := junoplugin.Load(cfg.PluginPath) - if err != nil { - return nil, err - } - synchronizer.WithPlugin(plugin) - } - var p2pService *p2p.Service if cfg.P2P { if cfg.Network != utils.Sepolia { @@ -199,6 +191,19 @@ func New(cfg *Config, version string) (*Node, error) { //nolint:gocyclo,funlen rpcHandler := rpc.New(chain, syncReader, throttledVM, version, log).WithGateway(gatewayClient).WithFeeder(client) rpcHandler = rpcHandler.WithFilterLimit(cfg.RPCMaxBlockScan).WithCallMaxSteps(uint64(cfg.RPCCallMaxSteps)) services = append(services, rpcHandler) + + if cfg.PluginPath != "" { + plugin, err := junoplugin.Load(cfg.PluginPath) + if err != nil { + return nil, err + } + err = plugin.Init(rpcHandler) + if err != nil { + return nil, err + } + synchronizer.WithPlugin(plugin) + } + // to improve RPC throughput we double GOMAXPROCS maxGoroutines := 2 * runtime.GOMAXPROCS(0) jsonrpcServer := jsonrpc.NewServer(maxGoroutines, log).WithValidator(validator.Validator()) diff --git a/plugin/plugin.go b/plugin/plugin.go index 5ccf984e2d..9cc5572462 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -4,23 +4,15 @@ import ( "fmt" "plugin" - "github.com/NethermindEth/juno/core" - "github.com/NethermindEth/juno/core/felt" + junopluginsync "github.com/NethermindEth/juno/plugin/sync" + "github.com/NethermindEth/juno/rpc" ) //go:generate mockgen -destination=../mocks/mock_plugin.go -package=mocks github.com/NethermindEth/juno/plugin JunoPlugin type JunoPlugin interface { - Init() error - Shutdown() error // Todo: Currently this function will never be called. - NewBlock(block *core.Block, stateUpdate *core.StateUpdate, newClasses map[felt.Felt]core.Class) error - // The state is reverted by applying a write operation with the reverseStateDiff's StorageDiffs, Nonces, and ReplacedClasses, - // and a delete option with its DeclaredV0Classes, DeclaredV1Classes, and ReplacedClasses. - RevertBlock(from, to *BlockAndStateUpdate, reverseStateDiff *core.StateDiff) error -} - -type BlockAndStateUpdate struct { - Block *core.Block - StateUpdate *core.StateUpdate + Init(rpcHandler *rpc.Handler) error + ShutDown() // Todo: Currently this function will never be called. + junopluginsync.JunoPlugin } func Load(pluginPath string) (JunoPlugin, error) { @@ -39,5 +31,5 @@ func Load(pluginPath string) (JunoPlugin, error) { return nil, fmt.Errorf("the plugin does not staisfy the required interface") } - return pluginInstance, pluginInstance.Init() + return pluginInstance, nil } diff --git a/plugin/sync/pluginsync.go b/plugin/sync/pluginsync.go new file mode 100644 index 0000000000..c57cfb3215 --- /dev/null +++ b/plugin/sync/pluginsync.go @@ -0,0 +1,18 @@ +package junopluginsync + +import ( + "github.com/NethermindEth/juno/core" + "github.com/NethermindEth/juno/core/felt" +) + +type BlockAndStateUpdate struct { + Block *core.Block + StateUpdate *core.StateUpdate +} + +type JunoPlugin interface { + NewBlock(block *core.Block, stateUpdate *core.StateUpdate, newClasses map[felt.Felt]core.Class) error + // The state is reverted by applying a write operation with the reverseStateDiff's StorageDiffs, Nonces, and ReplacedClasses, + // and a delete option with its DeclaredV0Classes, DeclaredV1Classes, and ReplacedClasses. + RevertBlock(from, to *BlockAndStateUpdate, reverseStateDiff *core.StateDiff) error +} diff --git a/sync/sync.go b/sync/sync.go index fb067b9110..6e243cf4c4 100644 --- a/sync/sync.go +++ b/sync/sync.go @@ -12,7 +12,7 @@ import ( "github.com/NethermindEth/juno/core/felt" "github.com/NethermindEth/juno/db" "github.com/NethermindEth/juno/feed" - junoplugin "github.com/NethermindEth/juno/plugin" + junoplugin "github.com/NethermindEth/juno/plugin/sync" "github.com/NethermindEth/juno/service" "github.com/NethermindEth/juno/starknetdata" "github.com/NethermindEth/juno/utils"