-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add get_version_info endpoint (#132)
* Add get_version endpoint * Add CaptiveCoreVersion Info * Add integration tests * update integration tests * remove protocol version * fetch captive core version from info endpoint * Add protocol version in response * Remove captive-core-info from makefile * Cache captive core version during bootup and add retry logic * Revert retry logic in daemon and call /info endpoint directly in handler * Fix integration tests * Isolate version population in its own test * Fix review comments
- Loading branch information
Showing
5 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
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 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) (GetVersionInfoResponse, error) { | ||
|
||
var captiveCoreVersion string | ||
info, err := coreClient.Info(ctx) | ||
if err != nil { | ||
logger.WithError(err).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).Info("Cannot create read transaction") | ||
} | ||
defer func() { | ||
_ = readTx.Done() | ||
}() | ||
|
||
latestLedger, err := readTx.GetLatestLedgerSequence() | ||
if err != nil { | ||
logger.WithError(err).Info("error occurred while getting latest ledger") | ||
} | ||
|
||
_, protocolVersion, err = getBucketListSizeAndProtocolVersion(ctx, ledgerReader, latestLedger) | ||
if err != nil { | ||
logger.WithError(err).Info("error occurred while fetching protocol version") | ||
} | ||
|
||
return GetVersionInfoResponse{ | ||
Version: config.Version, | ||
CommitHash: config.CommitHash, | ||
BuildTimestamp: config.BuildTimestamp, | ||
CaptiveCoreVersion: captiveCoreVersion, | ||
ProtocolVersion: protocolVersion, | ||
}, nil | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/config" | ||
"os/exec" | ||
"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) | ||
|
||
version, commitHash, buildTimeStamp := config.Version, config.CommitHash, config.BuildTimestamp | ||
|
||
populateVersionInfo(test) | ||
|
||
// reset to previous config values | ||
t.Cleanup(func() { | ||
config.Version = version | ||
config.CommitHash = commitHash | ||
config.BuildTimestamp = buildTimeStamp | ||
}) | ||
|
||
ch := jhttp.NewChannel(test.sorobanRPCURL(), nil) | ||
client := jrpc2.NewClient(ch, nil) | ||
|
||
var result methods.GetVersionInfoResponse | ||
err := client.CallResult(context.Background(), "getVersionInfo", nil, &result) | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, config.Version, result.Version) | ||
assert.Equal(t, config.BuildTimestamp, result.BuildTimestamp) | ||
assert.Equal(t, config.CommitHash, result.CommitHash) | ||
assert.Equal(t, test.protocolVersion, result.ProtocolVersion) | ||
assert.NotEmpty(t, result.CaptiveCoreVersion) | ||
|
||
} | ||
|
||
// Runs git commands to fetch version information | ||
func populateVersionInfo(test *Test) { | ||
|
||
execFunction := func(command string, args ...string) string { | ||
cmd := exec.Command(command, args...) | ||
test.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 { | ||
test.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") | ||
} |