diff --git a/cmd/soroban-rpc/internal/db/db.go b/cmd/soroban-rpc/internal/db/db.go index 38305d2c..f905704a 100644 --- a/cmd/soroban-rpc/internal/db/db.go +++ b/cmd/soroban-rpc/internal/db/db.go @@ -66,7 +66,8 @@ func openSQLiteDB(dbFilePath string) (*db.Session, error) { // 2. Disable WAL auto-checkpointing (we will do the checkpointing ourselves with wal_checkpoint pragmas // after every write transaction). // 3. Use synchronous=NORMAL, which is faster and still safe in WAL mode. - session, err := db.Open("sqlite3", fmt.Sprintf("file:%s?_journal_mode=WAL&_wal_autocheckpoint=0&_synchronous=NORMAL", dbFilePath)) + session, err := db.Open("sqlite3", + fmt.Sprintf("file:%s?_journal_mode=WAL&_wal_autocheckpoint=0&_synchronous=NORMAL", dbFilePath)) if err != nil { return nil, fmt.Errorf("open failed: %w", err) } @@ -78,7 +79,9 @@ func openSQLiteDB(dbFilePath string) (*db.Session, error) { return session, nil } -func OpenSQLiteDBWithPrometheusMetrics(dbFilePath string, namespace string, sub db.Subservice, registry *prometheus.Registry) (*DB, error) { +func OpenSQLiteDBWithPrometheusMetrics(dbFilePath string, namespace string, sub db.Subservice, + registry *prometheus.Registry, +) (*DB, error) { session, err := openSQLiteDB(dbFilePath) if err != nil { return nil, err @@ -133,7 +136,8 @@ func getMetaValue(ctx context.Context, q db.SessionInterface, key string) (strin case 1: // expected length on an initialized DB default: - return "", fmt.Errorf("multiple entries (%d) for key %q in table %q", len(results), latestLedgerSequenceMetaKey, metaTableName) + return "", fmt.Errorf("multiple entries (%d) for key %q in table %q", len(results), + latestLedgerSequenceMetaKey, metaTableName) } return results[0], nil } @@ -330,11 +334,11 @@ func (w writeTx) Rollback() error { // errors.New("not in transaction") is returned when rolling back a transaction which has // already been committed or rolled back. We can ignore those errors // because we allow rolling back after commits in defer statements. - if err := w.tx.Rollback(); err == nil || err.Error() == "not in transaction" { + err := w.tx.Rollback() + if err == nil || err.Error() == "not in transaction" { return nil - } else { - return err } + return err } func runSQLMigrations(db *sql.DB, dialect string) error { diff --git a/cmd/soroban-rpc/internal/integrationtest/infrastructure/test.go b/cmd/soroban-rpc/internal/integrationtest/infrastructure/test.go index c904dba1..9d309be1 100644 --- a/cmd/soroban-rpc/internal/integrationtest/infrastructure/test.go +++ b/cmd/soroban-rpc/internal/integrationtest/infrastructure/test.go @@ -20,11 +20,11 @@ import ( "testing" "time" - "github.com/stellar/go/network" "github.com/stretchr/testify/require" "github.com/stellar/go/clients/stellarcore" "github.com/stellar/go/keypair" + "github.com/stellar/go/network" proto "github.com/stellar/go/protocols/stellarcore" supportlog "github.com/stellar/go/support/log" "github.com/stellar/go/txnbuild" @@ -385,7 +385,7 @@ func (i *Test) generateCaptiveCoreCfgForContainer() { // Try the directory before the integration test refactoring // TODO: remove this hack after protocol 22 is released out, err = getOldVersionCaptiveCoreConfigVersion("../../test", captiveCoreConfigFilename) - outStr := strings.Replace(string(out), `ADDRESS="localhost"`, `ADDRESS="${CORE_HOST_PORT}"`, -1) + outStr := strings.ReplaceAll(string(out), `ADDRESS="localhost"`, `ADDRESS="${CORE_HOST_PORT}"`) out = []byte(outStr) } require.NoError(i.t, err) diff --git a/cmd/soroban-rpc/internal/jsonrpc.go b/cmd/soroban-rpc/internal/jsonrpc.go index b9234f16..54913fff 100644 --- a/cmd/soroban-rpc/internal/jsonrpc.go +++ b/cmd/soroban-rpc/internal/jsonrpc.go @@ -3,6 +3,7 @@ package internal import ( "context" "encoding/json" + "errors" "net/http" "strconv" "strings" @@ -29,7 +30,10 @@ import ( // maxHTTPRequestSize defines the largest request size that the http handler // would be willing to accept before dropping the request. The implementation // uses the default MaxBytesHandler to limit the request size. -const maxHTTPRequestSize = 512 * 1024 // half a megabyte +const ( + maxHTTPRequestSize = 512 * 1024 // half a megabyte + warningThresholdDenominator = 3 +) // Handler is the HTTP handler which serves the Soroban JSON RPC responses type Handler struct { @@ -67,7 +71,7 @@ func decorateHandlers(daemon interfaces.Daemon, logger *log.Entry, m handler.Map }, []string{"endpoint", "status"}) decorated := handler.Map{} for endpoint, h := range m { - // create copy of h so it can be used in closure bleow + // create copy of h, so it can be used in closure below h := h decorated[endpoint] = handler.New(func(ctx context.Context, r *jrpc2.Request) (interface{}, error) { reqID := strconv.FormatUint(middleware.NextRequestID(), 10) @@ -80,7 +84,8 @@ func decorateHandlers(daemon interfaces.Daemon, logger *log.Entry, m handler.Map if ok && simulateTransactionResponse.Error != "" { label["status"] = "error" } else if err != nil { - if jsonRPCErr, ok := err.(*jrpc2.Error); ok { + var jsonRPCErr *jrpc2.Error + if errors.As(err, &jsonRPCErr) { prometheusLabelReplacer := strings.NewReplacer(" ", "_", "-", "_", "(", "", ")", "") status := prometheusLabelReplacer.Replace(jsonRPCErr.Code.String()) label["status"] = status @@ -139,7 +144,7 @@ func NewJSONRPCHandler(cfg *config.Config, params HandlerParams) Handler { // Get the largest history window var ledgerRangeGetter db.LedgerRangeGetter = params.EventStore - var retentionWindow = cfg.EventLedgerRetentionWindow + retentionWindow := cfg.EventLedgerRetentionWindow if cfg.TransactionLedgerRetentionWindow > cfg.EventLedgerRetentionWindow { retentionWindow = cfg.TransactionLedgerRetentionWindow ledgerRangeGetter = params.TransactionReader @@ -176,8 +181,9 @@ func NewJSONRPCHandler(cfg *config.Config, params HandlerParams) Handler { requestDurationLimit: cfg.MaxGetNetworkExecutionDuration, }, { - methodName: "getVersionInfo", - underlyingHandler: methods.NewGetVersionInfoHandler(params.Logger, params.LedgerEntryReader, params.LedgerReader, params.Daemon), + methodName: "getVersionInfo", + underlyingHandler: methods.NewGetVersionInfoHandler(params.Logger, params.LedgerEntryReader, + params.LedgerReader, params.Daemon), longName: "get_version_info", queueLimit: cfg.RequestBacklogGetVersionInfoQueueLimit, requestDurationLimit: cfg.MaxGetVersionInfoExecutionDuration, @@ -211,8 +217,9 @@ func NewJSONRPCHandler(cfg *config.Config, params HandlerParams) Handler { requestDurationLimit: cfg.MaxGetTransactionExecutionDuration, }, { - methodName: "getTransactions", - underlyingHandler: methods.NewGetTransactionsHandler(params.Logger, params.LedgerReader, params.TransactionReader, cfg.MaxTransactionsLimit, cfg.DefaultTransactionsLimit, cfg.NetworkPassphrase), + methodName: "getTransactions", + underlyingHandler: methods.NewGetTransactionsHandler(params.Logger, params.LedgerReader, + params.TransactionReader, cfg.MaxTransactionsLimit, cfg.DefaultTransactionsLimit, cfg.NetworkPassphrase), longName: "get_transactions", queueLimit: cfg.RequestBacklogGetTransactionsQueueLimit, requestDurationLimit: cfg.MaxGetTransactionsExecutionDuration, @@ -260,8 +267,10 @@ func NewJSONRPCHandler(cfg *config.Config, params HandlerParams) Handler { durationWarnCounterName := handler.longName + "_execution_threshold_warning" durationLimitCounterName := handler.longName + "_execution_threshold_limit" - durationWarnCounterHelp := "The metric measures the count of " + handler.methodName + " requests that surpassed the warning threshold for execution time" - durationLimitCounterHelp := "The metric measures the count of " + handler.methodName + " requests that surpassed the limit threshold for execution time" + durationWarnCounterHelp := "The metric measures the count of " + handler.methodName + + " requests that surpassed the warning threshold for execution time" + durationLimitCounterHelp := "The metric measures the count of " + handler.methodName + + " requests that surpassed the limit threshold for execution time" requestDurationWarnCounter := prometheus.NewCounter(prometheus.CounterOpts{ Namespace: params.Daemon.MetricsNamespace(), Subsystem: "network", @@ -274,7 +283,7 @@ func NewJSONRPCHandler(cfg *config.Config, params HandlerParams) Handler { Help: durationLimitCounterHelp, }) // set the warning threshold to be one third of the limit. - requestDurationWarn := handler.requestDurationLimit / 3 + requestDurationWarn := handler.requestDurationLimit / warningThresholdDenominator durationLimiter := network.MakeJrpcRequestDurationLimiter( queueLimiter.Handle, requestDurationWarn, @@ -303,12 +312,16 @@ func NewJSONRPCHandler(cfg *config.Config, params HandlerParams) Handler { params.Logger) globalQueueRequestExecutionDurationWarningCounter := prometheus.NewCounter(prometheus.CounterOpts{ - Namespace: params.Daemon.MetricsNamespace(), Subsystem: "network", Name: "global_request_execution_duration_threshold_warning", - Help: "The metric measures the count of requests that surpassed the warning threshold for execution time", + Namespace: params.Daemon.MetricsNamespace(), + Subsystem: "network", + Name: "global_request_execution_duration_threshold_warning", + Help: "The metric measures the count of requests that surpassed the warning threshold for execution time", }) globalQueueRequestExecutionDurationLimitCounter := prometheus.NewCounter(prometheus.CounterOpts{ - Namespace: params.Daemon.MetricsNamespace(), Subsystem: "network", Name: "global_request_execution_duration_threshold_limit", - Help: "The metric measures the count of requests that surpassed the limit threshold for execution time", + Namespace: params.Daemon.MetricsNamespace(), + Subsystem: "network", + Name: "global_request_execution_duration_threshold_limit", + Help: "The metric measures the count of requests that surpassed the limit threshold for execution time", }) handler := network.MakeHTTPRequestDurationLimiter( queueLimitedBridge,