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

Add get_version_info endpoint #132

Merged
merged 17 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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
2 changes: 2 additions & 0 deletions cmd/soroban-rpc/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Config struct {
RequestBacklogGetHealthQueueLimit uint
RequestBacklogGetEventsQueueLimit uint
RequestBacklogGetNetworkQueueLimit uint
RequestBacklogGetVersionInfoQueueLimit uint
RequestBacklogGetLatestLedgerQueueLimit uint
RequestBacklogGetLedgerEntriesQueueLimit uint
RequestBacklogGetTransactionQueueLimit uint
Expand All @@ -54,6 +55,7 @@ type Config struct {
MaxGetHealthExecutionDuration time.Duration
MaxGetEventsExecutionDuration time.Duration
MaxGetNetworkExecutionDuration time.Duration
MaxGetVersionInfoExecutionDuration time.Duration
MaxGetLatestLedgerExecutionDuration time.Duration
MaxGetLedgerEntriesExecutionDuration time.Duration
MaxGetTransactionExecutionDuration time.Duration
Expand Down
13 changes: 13 additions & 0 deletions cmd/soroban-rpc/internal/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,13 @@ func (cfg *Config) options() ConfigOptions {
DefaultValue: uint(1000),
Validate: positive,
},
{
TomlKey: strutils.KebabToConstantCase("request-backlog-get-version-info-queue-limit"),
Usage: "Maximum number of outstanding GetVersionInfo requests",
ConfigKey: &cfg.RequestBacklogGetVersionInfoQueueLimit,
DefaultValue: uint(1000),
Validate: positive,
},
{
TomlKey: strutils.KebabToConstantCase("request-backlog-get-latest-ledger-queue-limit"),
Usage: "Maximum number of outstanding GetLatestsLedger requests",
Expand Down Expand Up @@ -367,6 +374,12 @@ func (cfg *Config) options() ConfigOptions {
ConfigKey: &cfg.MaxGetNetworkExecutionDuration,
DefaultValue: 5 * time.Second,
},
{
TomlKey: strutils.KebabToConstantCase("max-get-version-info-execution-duration"),
Usage: "The maximum duration of time allowed for processing a getVersionInfo request. When that time elapses, the rpc server would return -32001 and abort the request's execution",
ConfigKey: &cfg.MaxGetVersionInfoExecutionDuration,
DefaultValue: 5 * time.Second,
},
{
TomlKey: strutils.KebabToConstantCase("max-get-latest-ledger-execution-duration"),
Usage: "The maximum duration of time allowed for processing a getLatestLedger request. When that time elapses, the rpc server would return -32001 and abort the request's execution",
Expand Down
3 changes: 3 additions & 0 deletions cmd/soroban-rpc/internal/config/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ var (

// Branch is the git branch from which the soroban-rpc was built, injected during build time.
Branch = ""

// CaptiveCoreVersion is the Build value from /Info endpoint of core, injected during daemon.run()
CaptiveCoreVersion = ""
)
19 changes: 19 additions & 0 deletions cmd/soroban-rpc/internal/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import (
"context"
"errors"
"github.com/cenkalti/backoff/v4"
"net/http"
"net/http/pprof" //nolint:gosec
"os"
Expand Down Expand Up @@ -185,6 +186,24 @@
}, metricsRegistry),
}

fetchCaptiveCoreVersion := func() error {
coreClient := daemon.CoreClient()
info, err := coreClient.Info(context.Background())
if err != nil {
return err
}
config.CaptiveCoreVersion = info.Info.Build
return nil
}

backoffStrategy := backoff.NewExponentialBackOff()

Check failure on line 199 in cmd/soroban-rpc/internal/daemon/daemon.go

View workflow job for this annotation

GitHub Actions / golangci

undefined: backoff (typecheck)
backoffStrategy.MaxElapsedTime = 4 * time.Second
err = backoff.Retry(fetchCaptiveCoreVersion, backoffStrategy)

Check failure on line 201 in cmd/soroban-rpc/internal/daemon/daemon.go

View workflow job for this annotation

GitHub Actions / golangci

undefined: backoff (typecheck)
if err != nil {
psheth9 marked this conversation as resolved.
Show resolved Hide resolved
logger.WithError(err).
Info("error occurred while calling Info endpoint of core")
}

eventStore := events.NewMemoryStore(
daemon,
cfg.NetworkPassphrase,
Expand Down
7 changes: 7 additions & 0 deletions cmd/soroban-rpc/internal/jsonrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,13 @@ func NewJSONRPCHandler(cfg *config.Config, params HandlerParams) Handler {
queueLimit: cfg.RequestBacklogGetNetworkQueueLimit,
requestDurationLimit: cfg.MaxGetNetworkExecutionDuration,
},
{
methodName: "getVersionInfo",
underlyingHandler: methods.NewGetVersionInfoHandler(params.Logger, params.LedgerEntryReader, params.LedgerReader, params.Daemon),
longName: "get_version_info",
queueLimit: cfg.RequestBacklogGetVersionInfoQueueLimit,
requestDurationLimit: cfg.MaxGetVersionInfoExecutionDuration,
},
{
methodName: "getLatestLedger",
underlyingHandler: methods.NewGetLatestLedgerHandler(params.LedgerEntryReader, params.LedgerReader),
Expand Down
59 changes: 59 additions & 0 deletions cmd/soroban-rpc/internal/methods/get_version_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package methods

import (
"context"
"github.com/creachadair/jrpc2"
"github.com/creachadair/jrpc2/handler"
"github.com/stellar/go/support/log"
"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/config"
"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/daemon/interfaces"
"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/db"
)

type GetVersionInfoRequest struct {
psheth9 marked this conversation as resolved.
Show resolved Hide resolved
}

type GetVersionInfoResponse struct {
Version string `json:"version"`
CommitHash string `json:"commit_hash"`
BuildTimestamp string `json:"build_time_stamp"`
CaptiveCoreVersion string `json:"captive_core_version"`
ProtocolVersion uint32 `json:"protocol_version"`
}

func NewGetVersionInfoHandler(logger *log.Entry, ledgerEntryReader db.LedgerEntryReader, ledgerReader db.LedgerReader, daemon interfaces.Daemon) jrpc2.Handler {
//coreClient := daemon.CoreClient()
return handler.New(func(ctx context.Context, request GetVersionInfoRequest) (GetVersionInfoResponse, error) {

// Fetch Protocol version
var protocolVersion uint32
readTx, err := ledgerEntryReader.NewCachedTx(ctx)
if err != nil {
logger.WithError(err).WithField("request", request).
Info("Cannot create read transaction")
}
defer func() {
_ = readTx.Done()
}()

latestLedger, err := readTx.GetLatestLedgerSequence()
if err != nil {
logger.WithError(err).WithField("request", request).
Info("error occurred while getting latest ledger")
}

_, protocolVersion, err = getBucketListSizeAndProtocolVersion(ctx, ledgerReader, latestLedger)
if err != nil {
logger.WithError(err).WithField("request", request).
Info("error occurred while fetching protocol version")
}

return GetVersionInfoResponse{
Version: config.Version,
CommitHash: config.CommitHash,
BuildTimestamp: config.BuildTimestamp,
CaptiveCoreVersion: config.CaptiveCoreVersion,
ProtocolVersion: protocolVersion,
}, nil
})
}
30 changes: 30 additions & 0 deletions cmd/soroban-rpc/internal/test/get_version_info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package test

import (
"context"
"testing"

"github.com/creachadair/jrpc2"
"github.com/creachadair/jrpc2/jhttp"
"github.com/stretchr/testify/assert"

"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/methods"
)

func TestGetVersionInfoSucceeds(t *testing.T) {
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
request := methods.GetVersionInfoRequest{}

var result methods.GetVersionInfoResponse
err := client.CallResult(context.Background(), "getVersionInfo", request, &result)
assert.NoError(t, err)

assert.NotEmpty(t, result.Version)
assert.NotEmpty(t, result.BuildTimestamp)
assert.NotEmpty(t, result.CommitHash)
psheth9 marked this conversation as resolved.
Show resolved Hide resolved
assert.NotEmpty(t, result.CaptiveCoreVersion)
assert.NotEmpty(t, result.ProtocolVersion)
psheth9 marked this conversation as resolved.
Show resolved Hide resolved
}
24 changes: 24 additions & 0 deletions cmd/soroban-rpc/internal/test/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func NewTest(t *testing.T, cfg *TestConfig) *Test {
}))

i.runComposeCommand("up", "--detach", "--quiet-pull", "--no-color")
i.populateVersionInfo()
i.prepareShutdownHandlers()
i.coreClient = &stellarcore.Client{URL: "http://localhost:" + strconv.Itoa(stellarCorePort)}
i.waitForCore()
Expand Down Expand Up @@ -224,6 +225,29 @@ func (i *Test) runComposeCommand(args ...string) {
}
}

// Runs git commands to fetch version information
func (i *Test) populateVersionInfo() {

execFunction := func(command string, args ...string) string {
cmd := exec.Command(command, args...)
i.t.Log("Running", cmd.Env, cmd.Args)
out, innerErr := cmd.Output()
if exitErr, ok := innerErr.(*exec.ExitError); ok {
fmt.Printf("stdout:\n%s\n", string(out))
fmt.Printf("stderr:\n%s\n", string(exitErr.Stderr))
}

if innerErr != nil {
i.t.Fatalf("Command %s failed: %v", cmd.Env, innerErr)
}
return string(out)
}

config.Version = execFunction("git", "describe", "--tags", "--always", "--abbrev=0", "--match='v[0-9]*.[0-9]*.[0-9]*'")
config.CommitHash = execFunction("git", "rev-parse", "HEAD")
config.BuildTimestamp = execFunction("date", "+%Y-%m-%dT%H:%M:%S")
}

func (i *Test) prepareShutdownHandlers() {
i.shutdownCalls = append(i.shutdownCalls,
func() {
Expand Down
Loading