Skip to content

Commit

Permalink
Remove GetLedgerRange from meta table and use ledgerRangeGetter for g…
Browse files Browse the repository at this point in the history
…etHealth and getFeeStats
  • Loading branch information
aditya1702 committed Jun 17, 2024
1 parent 1d9140c commit 1b35c07
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 80 deletions.
56 changes: 0 additions & 56 deletions cmd/soroban-rpc/internal/db/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import (
"fmt"

sq "github.com/Masterminds/squirrel"
"github.com/stellar/go/support/errors"

"github.com/stellar/go/xdr"

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

const (
Expand All @@ -21,7 +17,6 @@ type StreamLedgerFn func(xdr.LedgerCloseMeta) error
type LedgerReader interface {
GetLedger(ctx context.Context, sequence uint32) (xdr.LedgerCloseMeta, bool, error)
StreamAllLedgers(ctx context.Context, f StreamLedgerFn) error
LedgerRangeReader
}

type LedgerWriter interface {
Expand Down Expand Up @@ -73,57 +68,6 @@ func (r ledgerReader) GetLedger(ctx context.Context, sequence uint32) (xdr.Ledge
}
}

// GetLedgerRange pulls the min/max ledger sequence numbers from the ledgers table.
func (r ledgerReader) GetLedgerRange(ctx context.Context) (ledgerbucketwindow.LedgerRange, error) {
var ledgerRange ledgerbucketwindow.LedgerRange
//
// We use subqueries alongside a UNION ALL stitch in order to select the min
// and max from the ledger table in a single query and get around sqlite's
// limitations with parentheses (see https://stackoverflow.com/a/22609948).
//
newestQ := sq.
Select("m1.meta").
FromSelect(
sq.
Select("meta").
From(ledgerCloseMetaTableName).
OrderBy("sequence ASC").
Limit(1),
"m1",
)
sql, args, err := sq.
Select("m2.meta").
FromSelect(
sq.
Select("meta").
From(ledgerCloseMetaTableName).
OrderBy("sequence DESC").
Limit(1),
"m2",
).ToSql()
if err != nil {
return ledgerRange, errors.Wrap(err, "couldn't build ledger range query")
}

var lcms []xdr.LedgerCloseMeta
if err = r.db.Select(ctx, &lcms, newestQ.Suffix("UNION ALL "+sql, args...)); err != nil {
return ledgerRange, errors.Wrap(err, "couldn't query ledger range")
} else if len(lcms) < 2 {
// There is almost certainly a row, but we want to avoid a race condition
// with ingestion as well as support test cases from an empty DB, so we need
// to sanity check that there is in fact a result. Note that no ledgers in
// the database isn't an error, it's just an empty range.
return ledgerRange, nil
}

lcm1, lcm2 := lcms[0], lcms[1]
ledgerRange.FirstLedger.Sequence = lcm1.LedgerSequence()
ledgerRange.FirstLedger.CloseTime = lcm1.LedgerCloseTime()
ledgerRange.LastLedger.Sequence = lcm2.LedgerSequence()
ledgerRange.LastLedger.CloseTime = lcm2.LedgerCloseTime()
return ledgerRange, nil
}

type ledgerWriter struct {
stmtCache *sq.StmtCache
}
Expand Down
6 changes: 0 additions & 6 deletions cmd/soroban-rpc/internal/db/ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,6 @@ func assertLedgerRange(t *testing.T, reader LedgerReader, start, end uint32) {
allLedgers = allLedgers[1:]
}
assert.Empty(t, allLedgers)

ledgerRange, err := reader.GetLedgerRange(context.Background())
assert.Equal(t, start, ledgerRange.FirstLedger.Sequence)
assert.Equal(t, int64(start+10), ledgerRange.FirstLedger.CloseTime)
assert.Equal(t, end, ledgerRange.LastLedger.Sequence)
assert.Equal(t, int64(end+10), ledgerRange.LastLedger.CloseTime)
}

func TestLedgers(t *testing.T) {
Expand Down
4 changes: 0 additions & 4 deletions cmd/soroban-rpc/internal/db/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,6 @@ func (m *mockLedgerReader) StreamAllLedgers(ctx context.Context, f StreamLedgerF
return nil
}

func (m *mockLedgerReader) GetLedgerRange(ctx context.Context) (ledgerbucketwindow.LedgerRange, error) {
return ledgerbucketwindow.LedgerRange{}, nil
}

var _ TransactionReader = &mockTransactionHandler{}
var _ TransactionWriter = &mockTransactionHandler{}
var _ LedgerReader = &mockLedgerReader{}
3 changes: 2 additions & 1 deletion cmd/soroban-rpc/internal/events/events.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package events

import (
"context"
"errors"
"io"
"sort"
Expand Down Expand Up @@ -267,7 +268,7 @@ func readEvents(networkPassphrase string, ledgerCloseMeta xdr.LedgerCloseMeta) (
}

// GetLedgerRange returns the first and latest ledger available in the store.
func (m *MemoryStore) GetLedgerRange() (ledgerbucketwindow.LedgerRange, error) {
func (m *MemoryStore) GetLedgerRange(ctx context.Context) (ledgerbucketwindow.LedgerRange, error) {
m.lock.RLock()
defer m.lock.RUnlock()
return m.eventsByLedger.GetLedgerRange(), nil
Expand Down
15 changes: 9 additions & 6 deletions cmd/soroban-rpc/internal/jsonrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,13 @@ func NewJSONRPCHandler(cfg *config.Config, params HandlerParams) Handler {
},
}

// While we transition from in-memory to database-oriented history storage,
// the on-disk (transaction) retention window will always be larger than the
// in-memory (events) one.
var retentionWindow = cfg.TransactionLedgerRetentionWindow
// Get the largest history window
var ledgerRangeGetter db.LedgerRangeReader = params.EventStore
var retentionWindow = cfg.EventLedgerRetentionWindow
if cfg.TransactionLedgerRetentionWindow > cfg.EventLedgerRetentionWindow {
retentionWindow = cfg.TransactionLedgerRetentionWindow
ledgerRangeGetter = params.TransactionReader
}

handlers := []struct {
methodName string
Expand All @@ -151,7 +154,7 @@ func NewJSONRPCHandler(cfg *config.Config, params HandlerParams) Handler {
{
methodName: "getHealth",
underlyingHandler: methods.NewHealthCheck(
retentionWindow, params.LedgerReader, cfg.MaxHealthyLedgerLatency),
retentionWindow, ledgerRangeGetter, cfg.MaxHealthyLedgerLatency),
longName: "get_health",
queueLimit: cfg.RequestBacklogGetHealthQueueLimit,
requestDurationLimit: cfg.MaxGetHealthExecutionDuration,
Expand Down Expand Up @@ -232,7 +235,7 @@ func NewJSONRPCHandler(cfg *config.Config, params HandlerParams) Handler {
},
{
methodName: "getFeeStats",
underlyingHandler: methods.NewGetFeeStatsHandler(params.FeeStatWindows, params.LedgerReader, params.Logger),
underlyingHandler: methods.NewGetFeeStatsHandler(params.FeeStatWindows, ledgerRangeGetter, params.Logger),
longName: "get_fee_stats",
queueLimit: cfg.RequestBacklogGetFeeStatsTransactionQueueLimit,
requestDurationLimit: cfg.MaxGetFeeStatsExecutionDuration,
Expand Down
2 changes: 1 addition & 1 deletion cmd/soroban-rpc/internal/methods/get_fee_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type GetFeeStatsResult struct {
}

// NewGetFeeStatsHandler returns a handler obtaining fee statistics
func NewGetFeeStatsHandler(windows *feewindow.FeeWindows, reader db.LedgerReader, logger *log.Entry) jrpc2.Handler {
func NewGetFeeStatsHandler(windows *feewindow.FeeWindows, reader db.LedgerRangeReader, logger *log.Entry) jrpc2.Handler {
return NewHandler(func(ctx context.Context) (GetFeeStatsResult, error) {
ledgerRange, err := reader.GetLedgerRange(ctx)
if err != nil { // still not fatal
Expand Down
5 changes: 0 additions & 5 deletions cmd/soroban-rpc/internal/methods/get_latest_ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/stretchr/testify/assert"

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

const (
Expand Down Expand Up @@ -59,10 +58,6 @@ func (ledgerReader *ConstantLedgerReader) StreamAllLedgers(ctx context.Context,
return nil
}

func (ledgerReader *ConstantLedgerReader) GetLedgerRange(ctx context.Context) (ledgerbucketwindow.LedgerRange, error) {
return ledgerbucketwindow.LedgerRange{}, nil
}

func createLedger(ledgerSequence uint32, protocolVersion uint32, hash byte) xdr.LedgerCloseMeta {
return xdr.LedgerCloseMeta{
V: 1,
Expand Down
2 changes: 1 addition & 1 deletion cmd/soroban-rpc/internal/methods/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type HealthCheckResult struct {
// NewHealthCheck returns a health check json rpc handler
func NewHealthCheck(
retentionWindow uint32,
reader db.LedgerReader,
reader db.LedgerRangeReader,
maxHealthyLedgerLatency time.Duration,
) jrpc2.Handler {
return NewHandler(func(ctx context.Context) (HealthCheckResult, error) {
Expand Down

0 comments on commit 1b35c07

Please sign in to comment.