Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor getVersionInfo and getNetwork RPC to not use HTTP endpoint /info on core #198

Merged
merged 9 commits into from
Jul 18, 2024
2 changes: 2 additions & 0 deletions cmd/soroban-rpc/internal/daemon/interfaces/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/prometheus/client_golang/prometheus"

"github.com/stellar/go/ingest/ledgerbackend"
proto "github.com/stellar/go/protocols/stellarcore"
)

Expand All @@ -15,6 +16,7 @@ type Daemon interface {
MetricsRegistry() *prometheus.Registry
MetricsNamespace() string
CoreClient() CoreClient
GetCore() *ledgerbackend.CaptiveStellarCore
}

type CoreClient interface {
Expand Down
6 changes: 6 additions & 0 deletions cmd/soroban-rpc/internal/daemon/interfaces/noOpDaemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/prometheus/client_golang/prometheus"

"github.com/stellar/go/ingest/ledgerbackend"
proto "github.com/stellar/go/protocols/stellarcore"
)

Expand All @@ -14,6 +15,7 @@ type NoOpDaemon struct {
metricsRegistry *prometheus.Registry
metricsNamespace string
coreClient noOpCoreClient
core *ledgerbackend.CaptiveStellarCore
}

func MakeNoOpDeamon() *NoOpDaemon {
Expand All @@ -36,6 +38,10 @@ func (d *NoOpDaemon) CoreClient() CoreClient {
return d.coreClient
}

func (d *NoOpDaemon) GetCore() *ledgerbackend.CaptiveStellarCore {
return d.core
}

type noOpCoreClient struct{}

func (s noOpCoreClient) Info(context.Context) (*proto.InfoResponse, error) {
Expand Down
5 changes: 5 additions & 0 deletions cmd/soroban-rpc/internal/daemon/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/prometheus/client_golang/prometheus/collectors"

"github.com/stellar/go/clients/stellarcore"
"github.com/stellar/go/ingest/ledgerbackend"
proto "github.com/stellar/go/protocols/stellarcore"
supportlog "github.com/stellar/go/support/log"
"github.com/stellar/go/support/logmetrics"
Expand Down Expand Up @@ -106,6 +107,10 @@ func (d *Daemon) CoreClient() interfaces.CoreClient {
return d.coreClient
}

func (d *Daemon) GetCore() *ledgerbackend.CaptiveStellarCore {
return d.core
}

func (d *Daemon) Logger() *supportlog.Entry {
return d.logger
}
10 changes: 8 additions & 2 deletions cmd/soroban-rpc/internal/jsonrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,14 @@ func NewJSONRPCHandler(cfg *config.Config, params HandlerParams) Handler {
requestDurationLimit: cfg.MaxGetEventsExecutionDuration,
},
{
methodName: "getNetwork",
underlyingHandler: methods.NewGetNetworkHandler(params.Daemon, cfg.NetworkPassphrase, cfg.FriendbotURL),
methodName: "getNetwork",
underlyingHandler: methods.NewGetNetworkHandler(
params.Logger,
cfg.NetworkPassphrase,
cfg.FriendbotURL,
params.LedgerEntryReader,
params.LedgerReader,
),
longName: "get_network",
queueLimit: cfg.RequestBacklogGetNetworkQueueLimit,
requestDurationLimit: cfg.MaxGetNetworkExecutionDuration,
Expand Down
23 changes: 14 additions & 9 deletions cmd/soroban-rpc/internal/methods/get_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (

"github.com/creachadair/jrpc2"

"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/daemon/interfaces"
"github.com/stellar/go/support/log"

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

type GetNetworkRequest struct{}
Expand All @@ -17,20 +19,23 @@ type GetNetworkResponse struct {
}

// NewGetNetworkHandler returns a json rpc handler to for the getNetwork method
func NewGetNetworkHandler(daemon interfaces.Daemon, networkPassphrase, friendbotURL string) jrpc2.Handler {
coreClient := daemon.CoreClient()
func NewGetNetworkHandler(
logger *log.Entry,
networkPassphrase string,
friendbotURL string,
ledgerEntryReader db.LedgerEntryReader,
ledgerReader db.LedgerReader,
) jrpc2.Handler {
return NewHandler(func(ctx context.Context, request GetNetworkRequest) (GetNetworkResponse, error) {
info, err := coreClient.Info(ctx)
protocolVersion, err := getProtocolVersion(ctx, ledgerEntryReader, ledgerReader)
if err != nil {
return GetNetworkResponse{}, (&jrpc2.Error{
Code: jrpc2.InternalError,
Message: err.Error(),
})
logger.WithError(err).Error("failed to fetch protocol version")
psheth9 marked this conversation as resolved.
Show resolved Hide resolved
}

return GetNetworkResponse{
FriendbotURL: friendbotURL,
Passphrase: networkPassphrase,
ProtocolVersion: info.Info.ProtocolVersion,
ProtocolVersion: int(protocolVersion),
}, nil
})
}
38 changes: 11 additions & 27 deletions cmd/soroban-rpc/internal/methods/get_version_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,19 @@ type GetVersionInfoResponse struct {
ProtocolVersion uint32 `json:"protocol_version"` //nolint:tagliatelle
}

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()
}()
func NewGetVersionInfoHandler(
logger *log.Entry,
ledgerEntryReader db.LedgerEntryReader,
ledgerReader db.LedgerReader,
daemon interfaces.Daemon,
) jrpc2.Handler {
core := daemon.GetCore()
psheth9 marked this conversation as resolved.
Show resolved Hide resolved

latestLedger, err := readTx.GetLatestLedgerSequence()
if err != nil {
logger.WithError(err).Info("error occurred while getting latest ledger")
}

_, protocolVersion, err = getBucketListSizeAndProtocolVersion(ctx, ledgerReader, latestLedger)
return handler.New(func(ctx context.Context) (GetVersionInfoResponse, error) {
captiveCoreVersion := core.GetCoreVersion()
protocolVersion, err := getProtocolVersion(ctx, ledgerEntryReader, ledgerReader)
if err != nil {
logger.WithError(err).Info("error occurred while fetching protocol version")
logger.WithError(err).Error("failed to fetch protocol version")
}

return GetVersionInfoResponse{
Expand Down
15 changes: 0 additions & 15 deletions cmd/soroban-rpc/internal/methods/simulate_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,18 +285,3 @@ func base64EncodeSlice(in [][]byte) []string {
}
return result
}

func getBucketListSizeAndProtocolVersion(ctx context.Context, ledgerReader db.LedgerReader, latestLedger uint32) (uint64, uint32, error) {
// obtain bucket size
closeMeta, ok, err := ledgerReader.GetLedger(ctx, latestLedger)
if err != nil {
return 0, 0, err
}
if !ok {
return 0, 0, fmt.Errorf("missing meta for latest ledger (%d)", latestLedger)
}
if closeMeta.V != 1 {
return 0, 0, fmt.Errorf("latest ledger (%d) meta has unexpected verion (%d)", latestLedger, closeMeta.V)
}
return uint64(closeMeta.V1.TotalByteSizeOfBucketList), uint32(closeMeta.V1.LedgerHeader.Header.LedgerVersion), nil
}
53 changes: 53 additions & 0 deletions cmd/soroban-rpc/internal/methods/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package methods

import (
"context"
"fmt"

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

func getProtocolVersion(
ctx context.Context,
ledgerEntryReader db.LedgerEntryReader,
ledgerReader db.LedgerReader,
) (uint32, error) {
var protocolVersion uint32
readTx, err := ledgerEntryReader.NewCachedTx(ctx)
if err != nil {
return 0, err
}
psheth9 marked this conversation as resolved.
Show resolved Hide resolved
defer func() {
_ = readTx.Done()
}()
psheth9 marked this conversation as resolved.
Show resolved Hide resolved

latestLedger, err := readTx.GetLatestLedgerSequence()
if err != nil {
return 0, err
}
psheth9 marked this conversation as resolved.
Show resolved Hide resolved

_, protocolVersion, err = getBucketListSizeAndProtocolVersion(ctx, ledgerReader, latestLedger)
if err != nil {
return 0, err
}
return protocolVersion, nil
}

func getBucketListSizeAndProtocolVersion(
psheth9 marked this conversation as resolved.
Show resolved Hide resolved
ctx context.Context,
ledgerReader db.LedgerReader,
latestLedger uint32,
) (uint64, uint32, error) {
// obtain bucket size
closeMeta, ok, err := ledgerReader.GetLedger(ctx, latestLedger)
if err != nil {
return 0, 0, err
}
if !ok {
return 0, 0, fmt.Errorf("missing meta for latest ledger (%d)", latestLedger)
}
if closeMeta.V != 1 {
return 0, 0, fmt.Errorf("latest ledger (%d) meta has unexpected verion (%d)", latestLedger, closeMeta.V)
}
return uint64(closeMeta.V1.TotalByteSizeOfBucketList), uint32(closeMeta.V1.LedgerHeader.Header.LedgerVersion), nil
}
Loading