diff --git a/CHANGELOG.md b/CHANGELOG.md index 2976ef21..4b1e58c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ go get -u github.com/stellar/go-stellar-sdk/protocols/rpc ``` ### Added +- `getNetwork` endpoint now returns core supported protocol version ([#563](https://github.com/stellar/stellar-rpc/pull/563)). - Expanded `getLatestLedger` endpoint to also return `closeTime`, `headerXdr`, and `metadataXdr` ([#554](https://github.com/stellar/stellar-rpc/pull/554)). - Added `soroban-env-host` info to `version` command ([#550](https://github.com/stellar/stellar-rpc/pull/550)). - Added a new `--network` configuration parameter, allowing users to specify a default Stellar network (`testnet`, `pubnet`, or `futurenet`) ([#540](https://github.com/stellar/stellar-rpc/pull/540), [#543](https://github.com/stellar/stellar-rpc/pull/543)). diff --git a/cmd/stellar-rpc/internal/integrationtest/get_network_test.go b/cmd/stellar-rpc/internal/integrationtest/get_network_test.go index 2c78baeb..b60d2cdd 100644 --- a/cmd/stellar-rpc/internal/integrationtest/get_network_test.go +++ b/cmd/stellar-rpc/internal/integrationtest/get_network_test.go @@ -18,5 +18,6 @@ func TestGetNetworkSucceeds(t *testing.T) { assert.NoError(t, err) assert.Equal(t, infrastructure.FriendbotURL, result.FriendbotURL) assert.Equal(t, infrastructure.StandaloneNetworkPassphrase, result.Passphrase) - assert.GreaterOrEqual(t, result.ProtocolVersion, 20) + assert.GreaterOrEqual(t, result.ProtocolVersion, 24) + assert.GreaterOrEqual(t, result.CoreSupportedProtocolVersion, 25) } diff --git a/cmd/stellar-rpc/internal/jsonrpc.go b/cmd/stellar-rpc/internal/jsonrpc.go index d5b05981..71095c52 100644 --- a/cmd/stellar-rpc/internal/jsonrpc.go +++ b/cmd/stellar-rpc/internal/jsonrpc.go @@ -189,9 +189,11 @@ func NewJSONRPCHandler(cfg *config.Config, params HandlerParams) Handler { { methodName: protocol.GetNetworkMethodName, underlyingHandler: methods.NewGetNetworkHandler( + params.Logger, cfg.NetworkPassphrase, cfg.FriendbotURL, params.LedgerReader, + cfg.StellarCoreBinaryPath, ), longName: toSnakeCase(protocol.GetNetworkMethodName), queueLimit: cfg.RequestBacklogGetNetworkQueueLimit, diff --git a/cmd/stellar-rpc/internal/methods/get_network.go b/cmd/stellar-rpc/internal/methods/get_network.go index 2cc420d3..b0e19f4c 100644 --- a/cmd/stellar-rpc/internal/methods/get_network.go +++ b/cmd/stellar-rpc/internal/methods/get_network.go @@ -1,20 +1,30 @@ package methods import ( + "bytes" "context" + "fmt" + "os/exec" + "regexp" + "strconv" "github.com/creachadair/jrpc2" protocol "github.com/stellar/go-stellar-sdk/protocols/rpc" + "github.com/stellar/go-stellar-sdk/support/log" "github.com/stellar/stellar-rpc/cmd/stellar-rpc/internal/db" ) +var coreVersion int //nolint:gochecknoglobals // allow global cache for core supported protocol version + // NewGetNetworkHandler returns a json rpc handler to for the getNetwork method func NewGetNetworkHandler( + logger *log.Entry, networkPassphrase string, friendbotURL string, ledgerReader db.LedgerReader, + coreBinaryPath string, ) jrpc2.Handler { return NewHandler(func(ctx context.Context, _ protocol.GetNetworkRequest) (protocol.GetNetworkResponse, error) { protocolVersion, err := getProtocolVersion(ctx, ledgerReader) @@ -25,10 +35,55 @@ func NewGetNetworkHandler( } } + if coreVersion == 0 { + coreVersionResponse, err := getCoreSupportedProtocolVersions(ctx, coreBinaryPath) + if err != nil { + logger.Warnf("failed to get supported protocol versions: %v", err) + } else { + coreVersion = coreVersionResponse + } + } + return protocol.GetNetworkResponse{ - FriendbotURL: friendbotURL, - Passphrase: networkPassphrase, - ProtocolVersion: int(protocolVersion), + FriendbotURL: friendbotURL, + Passphrase: networkPassphrase, + ProtocolVersion: int(protocolVersion), + CoreSupportedProtocolVersion: coreVersion, }, nil }) } + +func getCoreSupportedProtocolVersions(ctx context.Context, coreBinaryPath string) (int, error) { + // Exec `stellar-core version` to get supported protocol versions + cmd := exec.CommandContext(ctx, coreBinaryPath, "version") + var out, stderr bytes.Buffer + cmd.Stdout = &out + cmd.Stderr = &stderr + if err := cmd.Run(); err != nil { + return 0, &jrpc2.Error{ + Code: jrpc2.InternalError, + Message: fmt.Sprintf("failed to exec `stellar-core version`: %v, stderr: %s", err, stderr.String()), + } + } + + // Find all matches for protocol versions across hosts in stdout + outStr := out.String() + re := regexp.MustCompile(`ledger protocol version:\s*(\d+)`) + match := re.FindStringSubmatch(outStr) + if match == nil { + return 0, &jrpc2.Error{ + Code: jrpc2.InternalError, + Message: "failed to parse protocol versions from `stellar-core version` output: " + outStr, + } + } + + version, err := strconv.Atoi(match[1]) + if err != nil { + return 0, &jrpc2.Error{ + Code: jrpc2.InternalError, + Message: "failed to parse protocol version from `stellar-core version` output: " + err.Error(), + } + } + + return version, nil +} diff --git a/go.mod b/go.mod index 1e45d072..b51502c6 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,7 @@ require ( github.com/sirupsen/logrus v1.9.3 github.com/spf13/cobra v1.7.0 github.com/spf13/pflag v1.0.5 - github.com/stellar/go-stellar-sdk v0.0.0-20251208182759-7568ee53f4fd + github.com/stellar/go-stellar-sdk v0.0.0-20251210212453-0b8bc9bdc885 github.com/stretchr/testify v1.11.1 ) diff --git a/go.sum b/go.sum index 4162620f..e11782b7 100644 --- a/go.sum +++ b/go.sum @@ -429,8 +429,8 @@ github.com/spf13/viper v1.17.0 h1:I5txKw7MJasPL/BrfkbA0Jyo/oELqVmux4pR/UxOMfI= github.com/spf13/viper v1.17.0/go.mod h1:BmMMMLQXSbcHK6KAOiFLz0l5JHrU89OdIRHvsk0+yVI= github.com/spiffe/go-spiffe/v2 v2.6.0 h1:l+DolpxNWYgruGQVV0xsfeya3CsC7m8iBzDnMpsbLuo= github.com/spiffe/go-spiffe/v2 v2.6.0/go.mod h1:gm2SeUoMZEtpnzPNs2Csc0D/gX33k1xIx7lEzqblHEs= -github.com/stellar/go-stellar-sdk v0.0.0-20251208182759-7568ee53f4fd h1:90j0mtcO8J6v2v/EVgvyfK+L1WlLW9Fg2Vn4/zAv/0Q= -github.com/stellar/go-stellar-sdk v0.0.0-20251208182759-7568ee53f4fd/go.mod h1:fZPcxQZw1I0zZ+X76uFcVPqmQCaYbWc87lDFW/kQJaY= +github.com/stellar/go-stellar-sdk v0.0.0-20251210212453-0b8bc9bdc885 h1:ZE6zXQanqXbUG60SJc7IYrcPuW4BsHgoAjcfh/C50hY= +github.com/stellar/go-stellar-sdk v0.0.0-20251210212453-0b8bc9bdc885/go.mod h1:fZPcxQZw1I0zZ+X76uFcVPqmQCaYbWc87lDFW/kQJaY= github.com/stellar/go-xdr v0.0.0-20231122183749-b53fb00bcac2 h1:OzCVd0SV5qE3ZcDeSFCmOWLZfEWZ3Oe8KtmSOYKEVWE= github.com/stellar/go-xdr v0.0.0-20231122183749-b53fb00bcac2/go.mod h1:yoxyU/M8nl9LKeWIoBrbDPQ7Cy+4jxRcWcOayZ4BMps= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=