From 1aaa24189b16835865bcb967f53c2ff00b7b1c85 Mon Sep 17 00:00:00 2001 From: joanestebanr <129153821+joanestebanr@users.noreply.github.com> Date: Mon, 16 Dec 2024 15:19:12 +0100 Subject: [PATCH] feat: RPC for aggsender,previous component RPC is called BRIDGE --- aggsender/aggsender.go | 21 +++++++++++++++++ aggsender/config.go | 5 +++++ cmd/main.go | 2 +- cmd/run.go | 51 +++++++++++++++++++++++++----------------- common/components.go | 4 ++-- config/default.go | 2 ++ 6 files changed, 61 insertions(+), 24 deletions(-) diff --git a/aggsender/aggsender.go b/aggsender/aggsender.go index ff0dab0a..e8965009 100644 --- a/aggsender/aggsender.go +++ b/aggsender/aggsender.go @@ -9,8 +9,10 @@ import ( "os" "time" + jRPC "github.com/0xPolygon/cdk-rpc/rpc" "github.com/0xPolygon/cdk/agglayer" "github.com/0xPolygon/cdk/aggsender/db" + aggsenderrpc "github.com/0xPolygon/cdk/aggsender/rpc" "github.com/0xPolygon/cdk/aggsender/types" "github.com/0xPolygon/cdk/bridgesync" cdkcommon "github.com/0xPolygon/cdk/common" @@ -83,6 +85,21 @@ func New( }, nil } +// GetRPCServices returns the list of services that the RPC provider exposes +func (a *AggSender) GetRPCServices() []jRPC.Service { + if !a.cfg.EnableRPC { + return []jRPC.Service{} + } + + logger := log.WithFields("aggsender-rpc", cdkcommon.BRIDGE) + return []jRPC.Service{ + { + Name: "aggsender", + Service: aggsenderrpc.NewAggsenderRPC(logger), + }, + } +} + // Start starts the AggSender func (a *AggSender) Start(ctx context.Context) { a.log.Info("AggSender started") @@ -209,6 +226,10 @@ func (a *AggSender) sendCertificate(ctx context.Context) (*agglayer.SignedCertif a.saveCertificateToFile(signedCertificate) a.log.Infof("certificate ready to be send to AggLayer: %s", signedCertificate.Brief()) + if a.cfg.DryRun { + a.log.Warn("dry run mode enabled, skipping sending certificate") + return signedCertificate, nil + } certificateHash, err := a.aggLayerClient.SendCertificate(signedCertificate) if err != nil { return nil, fmt.Errorf("error sending certificate: %w", err) diff --git a/aggsender/config.go b/aggsender/config.go index 552c44c5..a81c1299 100644 --- a/aggsender/config.go +++ b/aggsender/config.go @@ -39,6 +39,11 @@ type Config struct { MaxCertSize uint `mapstructure:"MaxCertSize"` // BridgeMetadataAsHash is a flag to import the bridge metadata as hash BridgeMetadataAsHash bool `mapstructure:"BridgeMetadataAsHash"` + // DryRun is a flag to enable the dry run mode + // in this mode the AggSender will not send the certificates to Agglayer + DryRun bool `mapstructure:"DryRun"` + // EnableRPC is a flag to enable the RPC for aggsender + EnableRPC bool `mapstructure:"EnableRPC"` } // String returns a string representation of the Config diff --git a/cmd/main.go b/cmd/main.go index 15b0fdc6..3fdb6a8f 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -42,7 +42,7 @@ var ( Usage: "List of components to run", Required: false, Value: cli.NewStringSlice(common.SEQUENCE_SENDER, common.AGGREGATOR, - common.AGGORACLE, common.RPC, common.AGGSENDER), + common.AGGORACLE, common.BRIDGE, common.AGGSENDER), } saveConfigFlag = cli.StringFlag{ Name: config.FlagSaveConfigPath, diff --git a/cmd/run.go b/cmd/run.go index 0900c2fa..5fb51693 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -85,7 +85,7 @@ func start(cliCtx *cli.Context) error { lastGERSync := runLastGERSyncIfNeeded( cliCtx.Context, components, cfg.LastGERSync, reorgDetectorL2, l2Client, l1InfoTreeSync, ) - + var rpcServices []jRPC.Service for _, component := range components { switch component { case cdkcommon.SEQUENCE_SENDER: @@ -106,8 +106,8 @@ func start(cliCtx *cli.Context) error { case cdkcommon.AGGORACLE: aggOracle := createAggoracle(*cfg, l1Client, l2Client, l1InfoTreeSync) go aggOracle.Start(cliCtx.Context) - case cdkcommon.RPC: - server := createRPC( + case cdkcommon.BRIDGE: + rpcBridge := createBridgeRPC( cfg.RPC, cfg.Common.NetworkID, claimSponsor, @@ -116,11 +116,8 @@ func start(cliCtx *cli.Context) error { l1BridgeSync, l2BridgeSync, ) - go func() { - if err := server.Start(); err != nil { - log.Fatal(err) - } - }() + rpcServices = append(rpcServices, rpcBridge...) + case cdkcommon.AGGSENDER: aggsender, err := createAggSender( cliCtx.Context, @@ -132,11 +129,19 @@ func start(cliCtx *cli.Context) error { if err != nil { log.Fatal(err) } + rpcServices = append(rpcServices, aggsender.GetRPCServices()...) go aggsender.Start(cliCtx.Context) } } - + if len(rpcServices) > 0 { + rpcServer := createRPC(cfg.RPC, rpcServices) + go func() { + if err := rpcServer.Start(); err != nil { + log.Fatal(err) + } + }() + } waitSignal(nil) return nil @@ -504,7 +509,7 @@ func runL1InfoTreeSyncerIfNeeded( l1Client *ethclient.Client, reorgDetector *reorgdetector.ReorgDetector, ) *l1infotreesync.L1InfoTreeSync { - if !isNeeded([]string{cdkcommon.AGGORACLE, cdkcommon.RPC, + if !isNeeded([]string{cdkcommon.AGGORACLE, cdkcommon.BRIDGE, cdkcommon.SEQUENCE_SENDER, cdkcommon.AGGSENDER, cdkcommon.L1INFOTREESYNC}, components) { return nil } @@ -534,7 +539,7 @@ func runL1InfoTreeSyncerIfNeeded( func runL1ClientIfNeeded(components []string, urlRPCL1 string) *ethclient.Client { if !isNeeded([]string{ cdkcommon.SEQUENCE_SENDER, cdkcommon.AGGREGATOR, - cdkcommon.AGGORACLE, cdkcommon.RPC, + cdkcommon.AGGORACLE, cdkcommon.BRIDGE, cdkcommon.AGGSENDER, cdkcommon.L1INFOTREESYNC, }, components) { @@ -564,7 +569,7 @@ func getRollUpIDIfNeeded(components []string, networkConfig ethermanconfig.L1Con } func runL2ClientIfNeeded(components []string, urlRPCL2 string) *ethclient.Client { - if !isNeeded([]string{cdkcommon.AGGORACLE, cdkcommon.RPC, cdkcommon.AGGSENDER}, components) { + if !isNeeded([]string{cdkcommon.AGGORACLE, cdkcommon.BRIDGE, cdkcommon.AGGSENDER}, components) { return nil } @@ -585,7 +590,7 @@ func runReorgDetectorL1IfNeeded( ) (*reorgdetector.ReorgDetector, chan error) { if !isNeeded([]string{ cdkcommon.SEQUENCE_SENDER, cdkcommon.AGGREGATOR, - cdkcommon.AGGORACLE, cdkcommon.RPC, cdkcommon.AGGSENDER, + cdkcommon.AGGORACLE, cdkcommon.BRIDGE, cdkcommon.AGGSENDER, cdkcommon.L1INFOTREESYNC}, components) { return nil, nil @@ -609,7 +614,7 @@ func runReorgDetectorL2IfNeeded( l2Client *ethclient.Client, cfg *reorgdetector.Config, ) (*reorgdetector.ReorgDetector, chan error) { - if !isNeeded([]string{cdkcommon.AGGORACLE, cdkcommon.RPC, cdkcommon.AGGSENDER}, components) { + if !isNeeded([]string{cdkcommon.AGGORACLE, cdkcommon.BRIDGE, cdkcommon.AGGSENDER}, components) { return nil, nil } rd := newReorgDetector(cfg, l2Client) @@ -631,7 +636,7 @@ func runClaimSponsorIfNeeded( l2Client *ethclient.Client, cfg claimsponsor.EVMClaimSponsorConfig, ) *claimsponsor.ClaimSponsor { - if !isNeeded([]string{cdkcommon.RPC}, components) || !cfg.Enabled { + if !isNeeded([]string{cdkcommon.BRIDGE}, components) || !cfg.Enabled { return nil } @@ -673,7 +678,7 @@ func runLastGERSyncIfNeeded( l2Client *ethclient.Client, l1InfoTreeSync *l1infotreesync.L1InfoTreeSync, ) *lastgersync.LastGERSync { - if !isNeeded([]string{cdkcommon.RPC}, components) { + if !isNeeded([]string{cdkcommon.BRIDGE}, components) { return nil } lastGERSync, err := lastgersync.New( @@ -705,7 +710,7 @@ func runBridgeSyncL1IfNeeded( l1Client *ethclient.Client, rollupID uint32, ) *bridgesync.BridgeSync { - if !isNeeded([]string{cdkcommon.RPC}, components) { + if !isNeeded([]string{cdkcommon.BRIDGE}, components) { return nil } @@ -739,7 +744,7 @@ func runBridgeSyncL2IfNeeded( l2Client *ethclient.Client, rollupID uint32, ) *bridgesync.BridgeSync { - if !isNeeded([]string{cdkcommon.RPC, cdkcommon.AGGSENDER}, components) { + if !isNeeded([]string{cdkcommon.BRIDGE, cdkcommon.AGGSENDER}, components) { return nil } @@ -765,7 +770,7 @@ func runBridgeSyncL2IfNeeded( return bridgeSyncL2 } -func createRPC( +func createBridgeRPC( cfg jRPC.Config, cdkNetworkID uint32, sponsor *claimsponsor.ClaimSponsor, @@ -773,8 +778,8 @@ func createRPC( injectedGERs *lastgersync.LastGERSync, bridgeL1 *bridgesync.BridgeSync, bridgeL2 *bridgesync.BridgeSync, -) *jRPC.Server { - logger := log.WithFields("module", cdkcommon.RPC) +) []jRPC.Service { + logger := log.WithFields("module", cdkcommon.BRIDGE) services := []jRPC.Service{ { Name: rpc.BRIDGE, @@ -791,7 +796,11 @@ func createRPC( ), }, } + return services +} +func createRPC(cfg jRPC.Config, services []jRPC.Service) *jRPC.Server { + logger := log.WithFields("module", "RPC") return jRPC.NewServer(cfg, services, jRPC.WithLogger(logger.GetSugaredLogger())) } diff --git a/common/components.go b/common/components.go index 2c8ab188..2b562cff 100644 --- a/common/components.go +++ b/common/components.go @@ -7,8 +7,8 @@ const ( AGGREGATOR = "aggregator" // AGGORACLE name to identify the aggoracle component AGGORACLE = "aggoracle" - // RPC name to identify the rpc component - RPC = "rpc" + // BRIDGE name to identify the bridge component (have RPC) + BRIDGE = "bridge" // CLAIM_SPONSOR name to identify the claim sponsor component CLAIM_SPONSOR = "claim-sponsor" //nolint:stylecheck // PROVER name to identify the prover component diff --git a/config/default.go b/config/default.go index 82cc2c91..1ff737b6 100644 --- a/config/default.go +++ b/config/default.go @@ -341,4 +341,6 @@ KeepCertificatesHistory = true # MaxSize of the certificate to 8Mb MaxCertSize = 8388608 BridgeMetadataAsHash = true +DryRun = false +EnableRPC = true `