Skip to content
Closed
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

### Added
- `getNetwork` endpoint now returns core supported protocol version ([#563](https://github.com/stellar/stellar-rpc/pull/563)).

### Breaking Changes
* The RPC client and schema definitions have been moved to the unified Stellar Go SDK. Please install them from there, instead:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ 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)
}
2 changes: 2 additions & 0 deletions cmd/stellar-rpc/internal/jsonrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
61 changes: 57 additions & 4 deletions cmd/stellar-rpc/internal/methods/get_network.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
package methods

import (
"bytes"
"context"
"os/exec"
"regexp"
"strconv"

"github.com/creachadair/jrpc2"
"github.com/stellar/go-stellar-sdk/support/log"

protocol "github.com/stellar/go-stellar-sdk/protocols/rpc"

"github.com/stellar/stellar-rpc/cmd/stellar-rpc/internal/db"
)

var coreVersion int

// 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)
Expand All @@ -25,10 +33,55 @@ func NewGetNetworkHandler(
}
}

if coreVersion == 0 {
coreVersionResponse, err := getCoreSupportedProtocolVersions(ctx, coreBinaryPath)
if err != nil {
logger.WithError(err).Warn("failed to get supported protocol versions: %v")
} 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: "failed to exec `stellar-core version`: " + err.Error() + " stderr: " + 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
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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-20251203191521-a04f63bf3455
github.com/stellar/go-stellar-sdk v0.0.0-20251210212453-0b8bc9bdc885
github.com/stretchr/testify v1.11.1
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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-20251203191521-a04f63bf3455 h1:cP/z5b8FF0PcMhnRxAq6AECCRgWhENTjgw5tHVWcZcs=
github.com/stellar/go-stellar-sdk v0.0.0-20251203191521-a04f63bf3455/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=
Expand Down