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 9 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
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ all: check build test

export RUSTFLAGS=-Dwarnings -Dclippy::all -Dclippy::pedantic


CAPTIVE_CORE_VERSION_INFO := $(shell stellar-core version)

psheth9 marked this conversation as resolved.
Show resolved Hide resolved
REPOSITORY_COMMIT_HASH := "$(shell git rev-parse HEAD)"
ifeq (${REPOSITORY_COMMIT_HASH},"")
$(error failed to retrieve git head commit hash)
Expand All @@ -17,7 +20,8 @@ BUILD_TIMESTAMP ?= $(shell date '+%Y-%m-%dT%H:%M:%S')
GOLDFLAGS := -X 'github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/config.Version=${REPOSITORY_VERSION}' \
-X 'github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/config.CommitHash=${REPOSITORY_COMMIT_HASH}' \
-X 'github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/config.BuildTimestamp=${BUILD_TIMESTAMP}' \
-X 'github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/config.Branch=${REPOSITORY_BRANCH}'
-X 'github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/config.Branch=${REPOSITORY_BRANCH}' \
psheth9 marked this conversation as resolved.
Show resolved Hide resolved
-X 'github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/config.CaptiveCoreVersionInfo=${CAPTIVE_CORE_VERSION_INFO}'
psheth9 marked this conversation as resolved.
Show resolved Hide resolved


# The following works around incompatibility between the rust and the go linkers -
Expand Down
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
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
68 changes: 68 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,68 @@
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) {

var captiveCoreVersion string
info, err := coreClient.Info(ctx)
psheth9 marked this conversation as resolved.
Show resolved Hide resolved
psheth9 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
logger.WithError(err).WithField("request", request).
Info("error occurred while calling Info endpoint of core")
} else {
captiveCoreVersion = info.Info.Build
}

// 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: 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