Skip to content

Commit

Permalink
Decide biggest range and startup and address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
2opremio committed Apr 11, 2024
1 parent 1a74baa commit e9dda73
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 34 deletions.
9 changes: 8 additions & 1 deletion cmd/soroban-rpc/internal/jsonrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ func NewJSONRPCHandler(cfg *config.Config, params HandlerParams) Handler {
Logger: func(text string) { params.Logger.Debug(text) },
},
}

// Get the largest history window
var ledgerRangeGetter methods.LedgerRangeGetter = params.EventStore
if cfg.TransactionLedgerRetentionWindow > cfg.EventLedgerRetentionWindow {
ledgerRangeGetter = params.TransactionStore
}

handlers := []struct {
methodName string
underlyingHandler jrpc2.Handler
Expand All @@ -143,7 +150,7 @@ func NewJSONRPCHandler(cfg *config.Config, params HandlerParams) Handler {
}{
{
methodName: "getHealth",
underlyingHandler: methods.NewHealthCheck(params.TransactionStore, params.EventStore, cfg.MaxHealthyLedgerLatency),
underlyingHandler: methods.NewHealthCheck(ledgerRangeGetter, cfg.MaxHealthyLedgerLatency),
longName: "get_health",
queueLimit: cfg.RequestBacklogGetHealthQueueLimit,
requestDurationLimit: cfg.MaxGetHealthExecutionDuration,
Expand Down
3 changes: 2 additions & 1 deletion cmd/soroban-rpc/internal/methods/get_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/creachadair/jrpc2/handler"
"github.com/stellar/go/xdr"

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

Expand Down Expand Up @@ -67,7 +68,7 @@ type GetTransactionRequest struct {
}

type transactionGetter interface {
GetTransaction(hash xdr.Hash) (transactions.Transaction, bool, transactions.StoreRange)
GetTransaction(hash xdr.Hash) (transactions.Transaction, bool, ledgerbucketwindow.LedgerRange)
}

func GetTransaction(getter transactionGetter, request GetTransactionRequest) (GetTransactionResponse, error) {
Expand Down
29 changes: 12 additions & 17 deletions cmd/soroban-rpc/internal/methods/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,31 @@ import (
"github.com/creachadair/jrpc2"
"github.com/creachadair/jrpc2/handler"

"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/events"
"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/transactions"
"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/ledgerbucketwindow"
)

type HealthCheckResult struct {
Status string `json:"status"`
LatestLedger uint32 `json:"latestLedger"`
FirstLedger uint32 `json:"firstLedger"`
OldestLedger uint32 `json:"oldestLedger"`
}

type LedgerRangeGetter interface {
GetLedgerRange() ledgerbucketwindow.LedgerRange
}

// NewHealthCheck returns a health check json rpc handler
func NewHealthCheck(txStore *transactions.MemoryStore, evStore *events.MemoryStore, maxHealthyLedgerLatency time.Duration) jrpc2.Handler {
func NewHealthCheck(ledgerRangeGetter LedgerRangeGetter, maxHealthyLedgerLatency time.Duration) jrpc2.Handler {
return handler.New(func(ctx context.Context) (HealthCheckResult, error) {
txLedgerRange := txStore.GetLedgerRange()
evLedgerRange := evStore.GetLedgerRange()
if txLedgerRange.FirstLedger.Sequence < 1 || evLedgerRange.FirstLedger.Sequence < 1 {
ledgerRange := ledgerRangeGetter.GetLedgerRange()
if ledgerRange.LastLedger.Sequence < 1 {
return HealthCheckResult{}, jrpc2.Error{
Code: jrpc2.InternalError,
Message: "data stores are not initialized",
}
}
mergedRange := evLedgerRange
if txLedgerRange.FirstLedger.Sequence < mergedRange.FirstLedger.Sequence {
mergedRange.FirstLedger = txLedgerRange.FirstLedger
}
if txLedgerRange.LastLedger.Sequence > mergedRange.LastLedger.Sequence {
mergedRange.LastLedger = txLedgerRange.LastLedger
}

lastKnownLedgerCloseTime := time.Unix(mergedRange.LastLedger.CloseTime, 0)
lastKnownLedgerCloseTime := time.Unix(ledgerRange.LastLedger.CloseTime, 0)
lastKnownLedgerLatency := time.Since(lastKnownLedgerCloseTime)
if lastKnownLedgerLatency > maxHealthyLedgerLatency {
roundedLatency := lastKnownLedgerLatency.Round(time.Second)
Expand All @@ -49,8 +44,8 @@ func NewHealthCheck(txStore *transactions.MemoryStore, evStore *events.MemorySto
}
result := HealthCheckResult{
Status: "healthy",
LatestLedger: mergedRange.LastLedger.Sequence,
FirstLedger: mergedRange.FirstLedger.Sequence,
LatestLedger: ledgerRange.LastLedger.Sequence,
OldestLedger: ledgerRange.FirstLedger.Sequence,
}
return result, nil
})
Expand Down
19 changes: 6 additions & 13 deletions cmd/soroban-rpc/internal/methods/send_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/stellar/go/xdr"

"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/daemon/interfaces"
"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/transactions"
)

// SendTransactionResponse represents the transaction submission response returned Soroban-RPC
Expand Down Expand Up @@ -45,14 +44,8 @@ type SendTransactionRequest struct {
Transaction string `json:"transaction"`
}

// LatestLedgerStore is a store which returns the latest ingested ledger.
type LatestLedgerStore interface {
// GetLatestLedger returns the latest ingested ledger.
GetLatestLedger() transactions.LedgerInfo
}

// NewSendTransactionHandler returns a submit transaction json rpc handler
func NewSendTransactionHandler(daemon interfaces.Daemon, logger *log.Entry, store LatestLedgerStore, passphrase string) jrpc2.Handler {
func NewSendTransactionHandler(daemon interfaces.Daemon, logger *log.Entry, ledgerRangeGetter LedgerRangeGetter, passphrase string) jrpc2.Handler {
submitter := daemon.CoreClient()
return handler.New(func(ctx context.Context, request SendTransactionRequest) (SendTransactionResponse, error) {
var envelope xdr.TransactionEnvelope
Expand All @@ -74,7 +67,7 @@ func NewSendTransactionHandler(daemon interfaces.Daemon, logger *log.Entry, stor
}
txHash := hex.EncodeToString(hash[:])

ledgerInfo := store.GetLatestLedger()
latestLedgerInfo := ledgerRangeGetter.GetLedgerRange().LastLedger
resp, err := submitter.SubmitTransaction(ctx, request.Transaction)
if err != nil {
logger.WithError(err).
Expand Down Expand Up @@ -110,15 +103,15 @@ func NewSendTransactionHandler(daemon interfaces.Daemon, logger *log.Entry, stor
DiagnosticEventsXDR: events,
Status: resp.Status,
Hash: txHash,
LatestLedger: ledgerInfo.Sequence,
LatestLedgerCloseTime: ledgerInfo.CloseTime,
LatestLedger: latestLedgerInfo.Sequence,
LatestLedgerCloseTime: latestLedgerInfo.CloseTime,
}, nil
case proto.TXStatusPending, proto.TXStatusDuplicate, proto.TXStatusTryAgainLater:
return SendTransactionResponse{
Status: resp.Status,
Hash: txHash,
LatestLedger: ledgerInfo.Sequence,
LatestLedgerCloseTime: ledgerInfo.CloseTime,
LatestLedger: latestLedgerInfo.Sequence,
LatestLedgerCloseTime: latestLedgerInfo.CloseTime,
}, nil
default:
logger.WithField("status", resp.Status).
Expand Down
8 changes: 6 additions & 2 deletions cmd/soroban-rpc/internal/test/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (

"github.com/creachadair/jrpc2"
"github.com/creachadair/jrpc2/jhttp"
"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/methods"
"github.com/stretchr/testify/assert"

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

func TestHealth(t *testing.T) {
Expand All @@ -20,5 +21,8 @@ func TestHealth(t *testing.T) {
if err := client.CallResult(context.Background(), "getHealth", nil, &result); err != nil {
t.Fatalf("rpc call failed: %v", err)
}
assert.Equal(t, methods.HealthCheckResult{Status: "healthy"}, result)
assert.Equal(t, "healthy", result.Status)
assert.Greater(t, result.OldestLedger, uint32(0))
assert.Greater(t, result.LatestLedger, uint32(0))
assert.GreaterOrEqual(t, result.LatestLedger, result.OldestLedger)
}

0 comments on commit e9dda73

Please sign in to comment.