diff --git a/cmd/replication/main.go b/cmd/replication/main.go index 6c61b6d2..fb40b661 100644 --- a/cmd/replication/main.go +++ b/cmd/replication/main.go @@ -22,18 +22,20 @@ var Commit string = "unknown" var options config.ServerOptions func main() { - if _, err := flags.Parse(&options); err != nil { - if err, ok := err.(*flags.Error); !ok || err.Type != flags.ErrHelp { - fatal("Could not parse options: %s", err) - } - return - } + _, err := flags.Parse(&options) if options.Version { fmt.Printf("Version: %s\n", Commit) return } + if err != nil { + if err, ok := err.(*flags.Error); !ok || err.Type != flags.ErrHelp { + fatal("Could not parse options: %s", err) + } + return + } + logger, _, err := utils.BuildLogger(options.Log) if err != nil { fatal("Could not build logger: %s", err) diff --git a/dev/run b/dev/run index f2a1751a..a9bd9279 100755 --- a/dev/run +++ b/dev/run @@ -4,4 +4,8 @@ set -eu . dev/local.env +export XMTPD_PAYER_ENABLE=true +export XMTPD_REPLICATION_ENABLE=true +export XMTPD_SYNC_ENABLE=true + go run -ldflags="-X main.Commit=$(git rev-parse HEAD)" cmd/replication/main.go "$@" \ No newline at end of file diff --git a/dev/run-2 b/dev/run-2 index 9ad3ac37..a2ea4e23 100755 --- a/dev/run-2 +++ b/dev/run-2 @@ -8,4 +8,8 @@ export XMTPD_SIGNER_PRIVATE_KEY=$ANVIL_ACC_2_PRIVATE_KEY export XMTPD_PAYER_PRIVATE_KEY=$XMTPD_SIGNER_PRIVATE_KEY export XMTPD_DB_WRITER_CONNECTION_STRING="postgres://postgres:xmtp@localhost:8766/postgres?sslmode=disable" +export XMTPD_PAYER_ENABLE=true +export XMTPD_REPLICATION_ENABLE=true +export XMTPD_SYNC_ENABLE=true + go run -ldflags="-X main.Commit=$(git rev-parse HEAD)" cmd/replication/main.go -p 5051 "$@" \ No newline at end of file diff --git a/pkg/config/options.go b/pkg/config/options.go index aedfe816..1aa9573e 100644 --- a/pkg/config/options.go +++ b/pkg/config/options.go @@ -36,7 +36,15 @@ type MetricsOptions struct { type PayerOptions struct { PrivateKey string `long:"private-key" env:"XMTPD_PAYER_PRIVATE_KEY" description:"Private key used to sign blockchain transactions" required:"true"` - EnableAPI bool `long:"enable-api" env:"XMTPD_PAYER_ENABLE_API" description:"Enable the payer API"` + Enable bool `long:"enable" env:"XMTPD_PAYER_ENABLE" description:"Enable the payer API"` +} + +type ReplicationOptions struct { + Enable bool `long:"enable" env:"XMTPD_REPLICATION_ENABLE" description:"Enable the replication API"` +} + +type SyncOptions struct { + Enable bool `long:"enable" env:"XMTPD_SYNC_ENABLE" description:"Enable the sync server"` } type MlsValidationOptions struct { @@ -90,14 +98,16 @@ type RegisterNodeOptions struct { type ServerOptions struct { API ApiOptions `group:"API Options" namespace:"api"` - DB DbOptions `group:"Database Options" namespace:"db"` Contracts ContractsOptions `group:"Contracts Options" namespace:"contracts"` + DB DbOptions `group:"Database Options" namespace:"db"` + Log LogOptions `group:"Log Options" namespace:"log"` Metrics MetricsOptions `group:"Metrics Options" namespace:"metrics"` + MlsValidation MlsValidationOptions `group:"MLS Validation Options" namespace:"mls-validation"` Payer PayerOptions `group:"Payer Options" namespace:"payer"` Reflection ReflectionOptions `group:"Reflection Options" namespace:"reflection"` - Tracing TracingOptions `group:"DD APM Tracing Options" namespace:"tracing"` - MlsValidation MlsValidationOptions `group:"MLS Validation Options" namespace:"mls-validation"` - Log LogOptions `group:"Log Options" namespace:"log"` + Replication ReplicationOptions `group:"Replication Options" namespace:"replication"` Signer SignerOptions `group:"Signer Options" namespace:"signer"` + Sync SyncOptions `group:"Sync Options" namespace:"sync"` + Tracing TracingOptions `group:"DD APM Tracing Options" namespace:"tracing"` Version bool ` short:"v" long:"version" description:"Output binary version and exit"` } diff --git a/pkg/server/server.go b/pkg/server/server.go index 767b1963..7b7d939e 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -112,18 +112,23 @@ func NewReplicationServer( } serviceRegistrationFunc := func(grpcServer *grpc.Server) error { - replicationService, err := message.NewReplicationApiService( - ctx, - log, - s.registrant, - writerDB, - ) - if err != nil { - return err + + if options.Replication.Enable { + replicationService, err := message.NewReplicationApiService( + ctx, + log, + s.registrant, + writerDB, + ) + if err != nil { + return err + } + message_api.RegisterReplicationApiServer(grpcServer, replicationService) + + log.Info("Replication service enabled") } - message_api.RegisterReplicationApiServer(grpcServer, replicationService) - if options.Payer.EnableAPI { + if options.Payer.Enable { payerPrivateKey, err := utils.ParseEcdsaPrivateKey(options.Payer.PrivateKey) if err != nil { return err @@ -139,35 +144,43 @@ func NewReplicationServer( return err } payer_api.RegisterPayerApiServer(grpcServer, payerService) + + log.Info("Payer service enabled") } return nil } - // TODO(rich): Add configuration to specify whether to run API/sync server - s.apiServer, err = api.NewAPIServer( - s.ctx, - log, - listenAddress, - options.Reflection.Enable, - serviceRegistrationFunc, - ) - if err != nil { - return nil, err + if options.Payer.Enable || options.Replication.Enable { + s.apiServer, err = api.NewAPIServer( + s.ctx, + log, + listenAddress, + options.Reflection.Enable, + serviceRegistrationFunc, + ) + if err != nil { + return nil, err + } + + log.Info("API server started", zap.Int("port", options.API.Port)) } - s.syncServer, err = sync.NewSyncServer( - s.ctx, - log, - s.nodeRegistry, - s.registrant, - s.writerDB, - ) - if err != nil { - return nil, err + if options.Sync.Enable { + s.syncServer, err = sync.NewSyncServer( + s.ctx, + log, + s.nodeRegistry, + s.registrant, + s.writerDB, + ) + if err != nil { + return nil, err + } + + log.Info("Sync server started") } - log.Info("Replication server started", zap.Int("port", options.API.Port)) return s, nil } diff --git a/pkg/server/server_test.go b/pkg/server/server_test.go index 80f274b7..40238522 100644 --- a/pkg/server/server_test.go +++ b/pkg/server/server_test.go @@ -54,6 +54,16 @@ func NewTestServer( API: config.ApiOptions{ Port: port, }, + Sync: config.SyncOptions{ + Enable: true, + }, + Replication: config.ReplicationOptions{ + Enable: true, + }, + //TODO(mkysel): this is not fully mocked yet + //Payer: config.PayerOptions{ + // Enable: true, + //}, }, registry, db, messagePublisher, fmt.Sprintf("localhost:%d", port)) require.NoError(t, err)