From aebb26066380512e987e91506ad20c5dc8d77c43 Mon Sep 17 00:00:00 2001 From: rian Date: Fri, 18 Oct 2024 16:40:22 +0300 Subject: [PATCH] integrate plugin into sequencer (reverts??) --- Makefile | 13 +++++++++++++ builder/builder.go | 15 ++++++++++++++- node/node.go | 25 +++++++++++++------------ 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index b6967ee2c2..d8b8e6ed8c 100644 --- a/Makefile +++ b/Makefile @@ -169,6 +169,19 @@ sequencer: --network sequencer \ --rpc-call-max-steps=4123000 +sequencer-plugin: + ./build/juno \ + --http \ + --http-port=6060 \ + --http-host=0.0.0.0 \ + --db-path=./seq-db \ + --log-level=debug \ + --seq-enable \ + --seq-block-time=1 \ + --network sequencer \ + --plugin-path="myplugin.so" \ + --rpc-call-max-steps=4123000 + sequencer-with-accounts: ./build/juno \ --http \ diff --git a/builder/builder.go b/builder/builder.go index 50b4e71de1..91d256c7e7 100644 --- a/builder/builder.go +++ b/builder/builder.go @@ -18,6 +18,7 @@ import ( "github.com/NethermindEth/juno/db" "github.com/NethermindEth/juno/feed" "github.com/NethermindEth/juno/mempool" + "github.com/NethermindEth/juno/plugin" "github.com/NethermindEth/juno/rpc" "github.com/NethermindEth/juno/service" "github.com/NethermindEth/juno/starknetdata" @@ -62,6 +63,8 @@ type Builder struct { chanFinalised chan struct{} blockHashToBeRevealed *felt.Felt + + plugin plugin.JunoPlugin } func New(privKey *ecdsa.PrivateKey, ownAddr *felt.Felt, bc *blockchain.Blockchain, builderVM vm.VM, @@ -115,6 +118,11 @@ func (b *Builder) WithJunoEndpoint(endpoint string) *Builder { return b } +func (b *Builder) WithPlugin(plugin plugin.JunoPlugin) *Builder { + b.plugin = plugin + return b +} + func (b *Builder) WithSyncToBlock(syncTo uint64) *Builder { b.shadowSyncToBlock = syncTo return b @@ -319,7 +327,12 @@ func (b *Builder) Finalise(signFunc blockchain.BlockSignFunc) error { b.log.Infow("Finalised block", "number", b.pendingBlock.Block.Number, "hash", b.pendingBlock.Block.Hash.ShortString(), "state", b.pendingBlock.Block.GlobalStateRoot.ShortString()) b.listener.OnBlockFinalised(b.pendingBlock.Block.Header) - + if b.plugin != nil { + err := b.plugin.NewBlock(b.pendingBlock.Block, b.pendingBlock.StateUpdate, b.pendingBlock.NewClasses) + if err != nil { + b.log.Errorw("error sending new block to plugin", err) + } + } if err := b.ClearPending(); err != nil { return err } diff --git a/node/node.go b/node/node.go index 761da373d4..2c1cdfd4c5 100644 --- a/node/node.go +++ b/node/node.go @@ -169,8 +169,17 @@ func New(cfg *Config, version string) (*Node, error) { //nolint:gocyclo,funlen client := feeder.NewClient(cfg.Network.FeederURL).WithUserAgent(ua).WithLogger(log). WithTimeout(cfg.GatewayTimeout).WithAPIKey(cfg.GatewayAPIKey) starknetData := adaptfeeder.New(client) + var junoPlugin plugin.JunoPlugin var rpcHandler *rpc.Handler var synchronizer *sync.Synchronizer + var sequencer *builder.Builder + if cfg.PluginPath != "" { + junoPlugin, err = plugin.Load(cfg.PluginPath) + if err != nil { + return nil, err + } + services = append(services, plugin.NewService(junoPlugin)) + } if cfg.Sequencer { if cfg.SeqShadowMode && chain.Network().L2ChainID != utils.Sepolia.L2ChainID { return nil, fmt.Errorf("the sequencers shadow mode can only be used for %v network. Provided network: %v", utils.Sepolia, cfg.Network) @@ -181,17 +190,17 @@ func New(cfg *Config, version string) (*Node, error) { //nolint:gocyclo,funlen } poolDB, _ := pebble.NewMem() p := mempool.New(poolDB) - sequencer := builder.New(pKey, new(felt.Felt).SetUint64(1337), chain, nodeVM, //nolint:mnd - time.Second*time.Duration(cfg.SeqBlockTime), p, log) + sequencer = builder.New(pKey, new(felt.Felt).SetUint64(1337), chain, nodeVM, //nolint:mnd + time.Second*time.Duration(cfg.SeqBlockTime), p, log).WithPlugin(junoPlugin) if cfg.SeqShadowMode { sequencer = builder.NewShadow(pKey, new(felt.Felt).SetUint64(1337), chain, nodeVM, time.Second*time.Duration(cfg.SeqBlockTime), p, //nolint: gomnd,lll,mnd - log, starknetData).WithJunoEndpoint(cfg.SeqRPCEndpoint).WithSyncToBlock(cfg.SeqShadowModeSyncTo) + log, starknetData).WithJunoEndpoint(cfg.SeqRPCEndpoint).WithSyncToBlock(cfg.SeqShadowModeSyncTo).WithPlugin(junoPlugin) } rpcHandler = rpc.New(chain, sequencer, throttledVM, version, log).WithMempool(p).WithCallMaxSteps(uint64(cfg.RPCCallMaxSteps)) services = append(services, sequencer) } else { - synchronizer = sync.New(chain, starknetData, log, cfg.PendingPollInterval, dbIsRemote) + synchronizer = sync.New(chain, starknetData, log, cfg.PendingPollInterval, dbIsRemote).WithPlugin(junoPlugin) gatewayClient := gateway.NewClient(cfg.Network.GatewayURL, log).WithUserAgent(ua).WithAPIKey(cfg.GatewayAPIKey) var p2pService *p2p.Service @@ -235,14 +244,6 @@ func New(cfg *Config, version string) (*Node, error) { //nolint:gocyclo,funlen rpcHandler = rpc.New(chain, syncReader, throttledVM, version, log).WithGateway(gatewayClient).WithFeeder(client) rpcHandler.WithFilterLimit(cfg.RPCMaxBlockScan).WithCallMaxSteps(uint64(cfg.RPCCallMaxSteps)) } - if cfg.PluginPath != "" { - p, err := plugin.Load(cfg.PluginPath) - if err != nil { - return nil, err - } - synchronizer.WithPlugin(p) - services = append(services, plugin.NewService(p)) - } services = append(services, rpcHandler) // to improve RPC throughput we double GOMAXPROCS maxGoroutines := 2 * runtime.GOMAXPROCS(0)