Skip to content

Commit f5fcad6

Browse files
author
Paul Bellamy
authored
soroban-rpc: Implement getNetwork method (#366)
* Add friendbotURL and networkPassphrase getNetwork method * Get the current protocol version directly from core This sucks. It would be good to cache it when we see new ledgers? * Add a test for getNetwork * Remove coreclient failing test. we can't really integration test that easily * Set expected/actual correctly * Fix test * Update description * privatize test constants where possible
1 parent 9ab61aa commit f5fcad6

File tree

7 files changed

+87
-7
lines changed

7 files changed

+87
-7
lines changed

Diff for: cmd/soroban-rpc/internal/config/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type LocalConfig struct {
1414
CaptiveCoreStoragePath string
1515
CaptiveCoreHTTPPort uint16
1616
CaptiveCoreUseDB bool
17+
FriendbotURL string
1718
NetworkPassphrase string
1819
HistoryArchiveURLs []string
1920
LogLevel logrus.Level

Diff for: cmd/soroban-rpc/internal/daemon/daemon.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,14 @@ func MustNew(cfg config.LocalConfig) *Daemon {
125125
)
126126

127127
handler, err := internal.NewJSONRPCHandler(internal.HandlerParams{
128-
AccountStore: methods.AccountStore{Client: hc},
129-
EventStore: methods.EventStore{Client: hc},
130-
Logger: logger,
131-
TransactionProxy: transactionProxy,
132-
CoreClient: &stellarcore.Client{URL: cfg.StellarCoreURL},
133-
DB: db,
128+
AccountStore: methods.AccountStore{Client: hc},
129+
EventStore: methods.EventStore{Client: hc},
130+
FriendbotURL: cfg.FriendbotURL,
131+
NetworkPassphrase: cfg.NetworkPassphrase,
132+
Logger: logger,
133+
TransactionProxy: transactionProxy,
134+
CoreClient: &stellarcore.Client{URL: cfg.StellarCoreURL},
135+
DB: db,
134136
})
135137
if err != nil {
136138
logger.Fatalf("could not create handler: %v", err)

Diff for: cmd/soroban-rpc/internal/jsonrpc.go

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func (h Handler) Close() {
4040
type HandlerParams struct {
4141
AccountStore methods.AccountStore
4242
EventStore methods.EventStore
43+
FriendbotURL string
4344
TransactionProxy *methods.TransactionProxy
4445
CoreClient *stellarcore.Client
4546
DB db.DB
@@ -54,6 +55,7 @@ func NewJSONRPCHandler(params HandlerParams) (Handler, error) {
5455
"getAccount": methods.NewAccountHandler(params.AccountStore),
5556
"getEvents": methods.NewGetEventsHandler(params.EventStore),
5657
"getLedgerEntry": methods.NewGetLedgerEntryHandler(params.Logger, params.DB),
58+
"getNetwork": methods.NewGetNetworkHandler(params.NetworkPassphrase, params.FriendbotURL, params.CoreClient),
5759
"getTransactionStatus": methods.NewGetTransactionStatusHandler(params.TransactionProxy),
5860
"sendTransaction": methods.NewSendTransactionHandler(params.TransactionProxy),
5961
"simulateTransaction": methods.NewSimulateTransactionHandler(params.Logger, params.NetworkPassphrase, params.DB),

Diff for: cmd/soroban-rpc/internal/methods/get_network.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package methods
2+
3+
import (
4+
"context"
5+
6+
"github.com/creachadair/jrpc2"
7+
"github.com/creachadair/jrpc2/code"
8+
"github.com/creachadair/jrpc2/handler"
9+
10+
"github.com/stellar/go/clients/stellarcore"
11+
)
12+
13+
type GetNetworkRequest struct{}
14+
15+
type GetNetworkResponse struct {
16+
FriendbotURL string `json:"friendbotUrl,omitempty"`
17+
Passphrase string `json:"passphrase"`
18+
ProtocolVersion int `json:"protocolVersion,string"`
19+
}
20+
21+
// NewGetNetworkHandler returns a json rpc handler to for the getNetwork method
22+
func NewGetNetworkHandler(networkPassphrase, friendbotURL string, coreClient *stellarcore.Client) jrpc2.Handler {
23+
return handler.New(func(ctx context.Context, request GetNetworkRequest) (GetNetworkResponse, error) {
24+
info, err := coreClient.Info(ctx)
25+
if err != nil {
26+
return GetNetworkResponse{}, (&jrpc2.Error{
27+
Code: code.InternalError,
28+
Message: err.Error(),
29+
})
30+
}
31+
return GetNetworkResponse{
32+
FriendbotURL: friendbotURL,
33+
Passphrase: networkPassphrase,
34+
ProtocolVersion: info.Info.ProtocolVersion,
35+
}, nil
36+
})
37+
}

Diff for: cmd/soroban-rpc/internal/test/get_network_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/creachadair/jrpc2"
8+
"github.com/creachadair/jrpc2/jhttp"
9+
"github.com/stretchr/testify/assert"
10+
11+
"github.com/stellar/soroban-tools/cmd/soroban-rpc/internal/methods"
12+
)
13+
14+
func TestGetNetworkSucceeds(t *testing.T) {
15+
test := NewTest(t)
16+
17+
ch := jhttp.NewChannel(test.server.URL, nil)
18+
client := jrpc2.NewClient(ch, nil)
19+
20+
request := methods.GetNetworkRequest{}
21+
22+
var result methods.GetNetworkResponse
23+
err := client.CallResult(context.Background(), "getNetwork", request, &result)
24+
assert.NoError(t, err)
25+
assert.Equal(t, friendbotURL, result.FriendbotURL)
26+
assert.Equal(t, StandaloneNetworkPassphrase, result.Passphrase)
27+
assert.Equal(t, stellarCoreProtocolVersion, result.ProtocolVersion)
28+
}

Diff for: cmd/soroban-rpc/internal/test/integration.go

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const (
3939
stellarCorePort = 11626
4040
goModFile = "go.mod"
4141
goMonorepoGithubPath = "github.com/stellar/go"
42+
friendbotURL = "http://localhost:8000/friendbot"
4243
)
4344

4445
type Test struct {
@@ -103,6 +104,7 @@ func (i *Test) launchDaemon() {
103104
CaptiveCoreConfigPath: path.Join(i.composePath, "captive-core-integration-tests.cfg"),
104105
CaptiveCoreHTTPPort: 0,
105106
CaptiveCoreStoragePath: i.t.TempDir(),
107+
FriendbotURL: friendbotURL,
106108
NetworkPassphrase: StandaloneNetworkPassphrase,
107109
HistoryArchiveURLs: []string{"http://localhost:1570"},
108110
LogLevel: logrus.DebugLevel,

Diff for: cmd/soroban-rpc/main.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
)
2121

2222
func main() {
23-
var endpoint, horizonURL, stellarCoreURL, binaryPath, configPath, networkPassphrase, dbPath, captivecoreStoragePath string
23+
var endpoint, horizonURL, stellarCoreURL, binaryPath, configPath, friendbotURL, networkPassphrase, dbPath, captivecoreStoragePath string
2424
var captiveCoreHTTPPort, ledgerEntryStorageTimeoutMinutes uint
2525
var checkpointFrequency uint32
2626
var useDB bool
@@ -134,6 +134,13 @@ func main() {
134134
},
135135
Usage: "comma-separated list of stellar history archives to connect with",
136136
},
137+
{
138+
Name: "friendbot-url",
139+
Usage: "The friendbot URL to be returned by getNetwork endpoint",
140+
OptType: types.String,
141+
ConfigKey: &friendbotURL,
142+
Required: false,
143+
},
137144
{
138145
Name: "network-passphrase",
139146
Usage: "Network passphrase of the Stellar network transactions should be signed for",
@@ -201,6 +208,7 @@ func main() {
201208
CaptiveCoreUseDB: useDB,
202209
CaptiveCoreHTTPPort: uint16(captiveCoreHTTPPort),
203210
CaptiveCoreStoragePath: captivecoreStoragePath,
211+
FriendbotURL: friendbotURL,
204212
NetworkPassphrase: networkPassphrase,
205213
HistoryArchiveURLs: historyArchiveURLs,
206214
LogLevel: logLevel,

0 commit comments

Comments
 (0)