Skip to content

Commit

Permalink
pass rpcHandler to plugin (+refactor for import cycle)
Browse files Browse the repository at this point in the history
  • Loading branch information
rian committed Aug 16, 2024
1 parent 626cf58 commit 109fa90
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
21 changes: 13 additions & 8 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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

Check warning on line 198 in node/node.go

View check run for this annotation

Codecov / codecov/patch

node/node.go#L196-L198

Added lines #L196 - L198 were not covered by tests
}
err = plugin.Init(rpcHandler)
if err != nil {
return nil, err

Check warning on line 202 in node/node.go

View check run for this annotation

Codecov / codecov/patch

node/node.go#L200-L202

Added lines #L200 - L202 were not covered by tests
}
synchronizer.WithPlugin(plugin)

Check warning on line 204 in node/node.go

View check run for this annotation

Codecov / codecov/patch

node/node.go#L204

Added line #L204 was not covered by tests
}

// to improve RPC throughput we double GOMAXPROCS
maxGoroutines := 2 * runtime.GOMAXPROCS(0)
jsonrpcServer := jsonrpc.NewServer(maxGoroutines, log).WithValidator(validator.Validator())
Expand Down
20 changes: 6 additions & 14 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -39,5 +31,5 @@ func Load(pluginPath string) (JunoPlugin, error) {
return nil, fmt.Errorf("the plugin does not staisfy the required interface")

Check warning on line 31 in plugin/plugin.go

View check run for this annotation

Codecov / codecov/patch

plugin/plugin.go#L29-L31

Added lines #L29 - L31 were not covered by tests
}

return pluginInstance, pluginInstance.Init()
return pluginInstance, nil

Check warning on line 34 in plugin/plugin.go

View check run for this annotation

Codecov / codecov/patch

plugin/plugin.go#L34

Added line #L34 was not covered by tests
}
18 changes: 18 additions & 0 deletions plugin/sync/pluginsync.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 109fa90

Please sign in to comment.