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 6 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
2 changes: 2 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,6 @@ var (

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

CaptiveCoreVersionInfo = ""
)
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(),
longName: "get_version_info",
queueLimit: cfg.RequestBacklogGetVersionInfoQueueLimit,
requestDurationLimit: cfg.MaxGetVersionInfoExecutionDuration,
},
{
methodName: "getLatestLedger",
underlyingHandler: methods.NewGetLatestLedgerHandler(params.LedgerEntryReader, params.LedgerReader),
Expand Down
30 changes: 30 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,30 @@
package methods

import (
"context"
"github.com/creachadair/jrpc2"
"github.com/creachadair/jrpc2/handler"
"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/config"
)

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"`
CaptiveCoreVersionInfo string `json:"captive_core_version"`
}

func NewGetVersionInfoHandler() jrpc2.Handler {
return handler.New(func(ctx context.Context, request GetVersionInfoRequest) (GetVersionInfoResponse, error) {

return GetVersionInfoResponse{
Version: config.Version,
CommitHash: config.CommitHash,
BuildTimestamp: config.BuildTimestamp,
CaptiveCoreVersionInfo: config.CaptiveCoreVersionInfo,
psheth9 marked this conversation as resolved.
Show resolved Hide resolved
}, nil
})
}
29 changes: 29 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,29 @@
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.CaptiveCoreVersionInfo)
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
}
25 changes: 25 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,30 @@ 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.CaptiveCoreVersionInfo = execFunction("stellar-core", "version")
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