Skip to content

Commit

Permalink
feat: RPC for aggsender,previous component RPC is called BRIDGE
Browse files Browse the repository at this point in the history
  • Loading branch information
joanestebanr committed Dec 16, 2024
1 parent b3c5b64 commit 1aaa241
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 24 deletions.
21 changes: 21 additions & 0 deletions aggsender/aggsender.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Check failure on line 15 in aggsender/aggsender.go

View workflow job for this annotation

GitHub Actions / test-unit (1.22.4, amd64)

no required module provides package github.com/0xPolygon/cdk/aggsender/rpc; to add it:
"github.com/0xPolygon/cdk/aggsender/types"
"github.com/0xPolygon/cdk/bridgesync"
cdkcommon "github.com/0xPolygon/cdk/common"
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions aggsender/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
51 changes: 30 additions & 21 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}

Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
}

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand All @@ -765,16 +770,16 @@ func runBridgeSyncL2IfNeeded(
return bridgeSyncL2
}

func createRPC(
func createBridgeRPC(
cfg jRPC.Config,
cdkNetworkID uint32,
sponsor *claimsponsor.ClaimSponsor,
l1InfoTree *l1infotreesync.L1InfoTreeSync,
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,
Expand All @@ -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()))
}

Expand Down
4 changes: 2 additions & 2 deletions common/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,4 +341,6 @@ KeepCertificatesHistory = true
# MaxSize of the certificate to 8Mb
MaxCertSize = 8388608
BridgeMetadataAsHash = true
DryRun = false
EnableRPC = true
`

0 comments on commit 1aaa241

Please sign in to comment.