Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

soroban-rpc: separate the daemon from the main function #267

Merged
merged 3 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions cmd/soroban-rpc/internal/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package config

import "github.com/sirupsen/logrus"

type LocalConfig struct {
EndPoint string
HorizonURL string
StellarCoreURL string
NetworkPassphrase string
LogLevel logrus.Level
TxConcurrency int
TxQueueSize int
}
58 changes: 58 additions & 0 deletions cmd/soroban-rpc/internal/daemon/daemon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package daemon

import (
"net/http"
"time"

"github.com/stellar/go/clients/horizonclient"
"github.com/stellar/go/clients/stellarcore"
supporthttp "github.com/stellar/go/support/http"
supportlog "github.com/stellar/go/support/log"
"github.com/stellar/soroban-tools/cmd/soroban-rpc/internal"
"github.com/stellar/soroban-tools/cmd/soroban-rpc/internal/config"
"github.com/stellar/soroban-tools/cmd/soroban-rpc/internal/methods"
)

func Start(cfg config.LocalConfig) (exitCode int) {
logger := supportlog.New()
logger.SetLevel(cfg.LogLevel)

hc := &horizonclient.Client{
HorizonURL: cfg.HorizonURL,
HTTP: &http.Client{
Timeout: horizonclient.HorizonTimeout,
},
AppName: "Soroban RPC",
}
hc.SetHorizonTimeout(horizonclient.HorizonTimeout)

transactionProxy := methods.NewTransactionProxy(
hc,
cfg.TxConcurrency,
cfg.TxQueueSize,
cfg.NetworkPassphrase,
5*time.Minute,
)

handler, err := internal.NewJSONRPCHandler(internal.HandlerParams{
AccountStore: methods.AccountStore{Client: hc},
Logger: logger,
TransactionProxy: transactionProxy,
CoreClient: &stellarcore.Client{URL: cfg.StellarCoreURL},
})
if err != nil {
logger.Fatalf("could not create handler: %v", err)
}
supporthttp.Run(supporthttp.Config{
ListenAddr: cfg.EndPoint,
Handler: handler,
OnStarting: func() {
logger.Infof("Starting Soroban JSON RPC server on %v", cfg.EndPoint)
handler.Start()
},
OnStopping: func() {
handler.Close()
},
})
return 0
}
64 changes: 17 additions & 47 deletions cmd/soroban-rpc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,23 @@ package main
import (
"fmt"
"go/types"
"net/http"
"time"
"os"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/stellar/go/clients/horizonclient"
"github.com/stellar/go/clients/stellarcore"
"github.com/stellar/go/network"
"github.com/stellar/go/support/config"
supporthttp "github.com/stellar/go/support/http"
supportlog "github.com/stellar/go/support/log"
"github.com/stellar/soroban-tools/cmd/soroban-rpc/internal"
"github.com/stellar/soroban-tools/cmd/soroban-rpc/internal/methods"
localConfig "github.com/stellar/soroban-tools/cmd/soroban-rpc/internal/config"
"github.com/stellar/soroban-tools/cmd/soroban-rpc/internal/daemon"
)

func main() {
var endpoint, horizonURL, stellarCoreURL, networkPassphrase string
var txConcurrency, txQueueSize int
var logLevel logrus.Level
logger := supportlog.New()

configOpts := config.ConfigOptions{
{
Expand Down Expand Up @@ -97,53 +92,28 @@ func main() {
Run: func(_ *cobra.Command, _ []string) {
configOpts.Require()
configOpts.SetValues()
logger.SetLevel(logLevel)

hc := &horizonclient.Client{
HorizonURL: horizonURL,
HTTP: &http.Client{
Timeout: horizonclient.HorizonTimeout,
},
AppName: "Soroban RPC",
config := localConfig.LocalConfig{
EndPoint: endpoint,
HorizonURL: horizonURL,
StellarCoreURL: stellarCoreURL,
NetworkPassphrase: networkPassphrase,
LogLevel: logLevel,
TxConcurrency: txConcurrency,
TxQueueSize: txQueueSize,
}
hc.SetHorizonTimeout(horizonclient.HorizonTimeout)

transactionProxy := methods.NewTransactionProxy(
hc,
txConcurrency,
txQueueSize,
networkPassphrase,
5*time.Minute,
)

handler, err := internal.NewJSONRPCHandler(internal.HandlerParams{
AccountStore: methods.AccountStore{Client: hc},
Logger: logger,
TransactionProxy: transactionProxy,
CoreClient: &stellarcore.Client{URL: stellarCoreURL},
})
if err != nil {
logger.Fatalf("could not create handler: %v", err)
}
supporthttp.Run(supporthttp.Config{
ListenAddr: endpoint,
Handler: handler,
OnStarting: func() {
logger.Infof("Starting Soroban JSON RPC server on %v", endpoint)
handler.Start()
},
OnStopping: func() {
handler.Close()
},
})
exitCode := daemon.Start(config)
os.Exit(exitCode)
},
}

if err := configOpts.Init(cmd); err != nil {
logger.WithError(err).Fatal("could not parse config options")
supportlog.New().WithError(err).Fatal("could not parse config options")
os.Exit(-1)
}

if err := cmd.Execute(); err != nil {
logger.WithError(err).Fatal("could not run")
supportlog.New().WithError(err).Fatal("could not run")
os.Exit(-1)
}
}