Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)).
Expand Down
3 changes: 2 additions & 1 deletion cmd/stellar-rpc/internal/integrationtest/get_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
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: 58 additions & 3 deletions cmd/stellar-rpc/internal/methods/get_network.go
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
}
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-20251208182759-7568ee53f4fd
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-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=
Expand Down
Loading