Skip to content

Commit

Permalink
Improve bound and cache update checks
Browse files Browse the repository at this point in the history
  • Loading branch information
2opremio committed Dec 18, 2024
1 parent 69d7895 commit 1acc9af
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 8 deletions.
4 changes: 2 additions & 2 deletions cmd/stellar-rpc/internal/db/cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ var (
//nolint:gochecknoglobals
MaxCursor = Cursor{
Ledger: math.MaxInt32,
Tx: math.MaxInt32,
Op: math.MaxInt32,
Tx: toid.TransactionMask,
Op: toid.OperationMask,
Event: math.MaxUint32,
}
)
7 changes: 7 additions & 0 deletions cmd/stellar-rpc/internal/db/cursor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,10 @@ func TestCursorCmp(t *testing.T) {
}
}
}

func TestMaxCursor(t *testing.T) {
str := MaxCursor.String()
cursor, err := ParseCursor(str)
require.NoError(t, err)
assert.Equal(t, MaxCursor, cursor)
}
4 changes: 2 additions & 2 deletions cmd/stellar-rpc/internal/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ func getLatestLedgerSequence(ctx context.Context, ledgerReader LedgerReader, cac
// Add missing ledger sequence and close time to the top cache.
// Otherwise, the write-through cache won't get updated until the first ingestion commit
cache.Lock()
if cache.latestLedgerSeq == 0 {
// Only update the cache if the value is missing (0), otherwise
if cache.latestLedgerSeq == 0 || cache.latestLedgerSeq < ledgerRange.LastLedger.Sequence {
// Only update the cache if the value is missing or lower, otherwise
// we may end up overwriting the entry with an older version
cache.latestLedgerSeq = ledgerRange.LastLedger.Sequence
cache.latestLedgerCloseTime = ledgerRange.LastLedger.CloseTime
Expand Down
4 changes: 4 additions & 0 deletions cmd/stellar-rpc/internal/db/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package db
import (
"context"
"database/sql"
"errors"
"fmt"

sq "github.com/Masterminds/squirrel"
Expand Down Expand Up @@ -63,6 +64,9 @@ func (l ledgerReaderTx) GetLedgerRange(ctx context.Context) (ledgerbucketwindow.
func (l ledgerReaderTx) BatchGetLedgers(ctx context.Context, sequence uint32,
batchSize uint,
) ([]xdr.LedgerCloseMeta, error) {
if batchSize < 1 {
return nil, errors.New("batch size must be greater than zero")
}
sql := sq.Select("meta").
From(ledgerCloseMetaTableName).
Where(sq.And{
Expand Down
13 changes: 10 additions & 3 deletions cmd/stellar-rpc/internal/methods/get_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"math"
"strings"
"time"

Expand Down Expand Up @@ -124,10 +123,16 @@ func (g *GetEventsRequest) Valid(maxLimit uint) error {
if g.StartLedger != 0 || g.EndLedger != 0 {
return errors.New("ledger ranges and cursor cannot both be set")
}
} else if g.StartLedger <= 0 {
}

if g.StartLedger <= 0 {
return errors.New("startLedger must be positive")
}

if g.EndLedger < g.StartLedger {
return errors.New("startLedger must be >= endLedger")
}

if g.Pagination != nil && g.Pagination.Limit > maxLimit {
return fmt.Errorf("limit must not exceed %d", maxLimit)
}
Expand Down Expand Up @@ -524,7 +529,9 @@ func (h eventsRPCHandler) getEvents(ctx context.Context, request GetEventsReques
// cursor represents end of the search window if events does not reach limit
// here endLedger is always exclusive when fetching events
// so search window is max Cursor value with endLedger - 1
cursor = db.Cursor{Ledger: endLedger - 1, Tx: math.MaxUint32, Event: math.MaxUint32 - 1}.String()
maxCursor := db.MaxCursor
maxCursor.Event = endLedger - 1
cursor = maxCursor.String()
}

return GetEventsResponse{
Expand Down
3 changes: 2 additions & 1 deletion cmd/stellar-rpc/internal/methods/get_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ func (req GetTransactionsRequest) isValid(maxLimit uint, ledgerRange ledgerbucke
if req.StartLedger != 0 {
return errors.New("startLedger and cursor cannot both be set")
}
} else if req.StartLedger < ledgerRange.FirstLedger.Sequence || req.StartLedger > ledgerRange.LastLedger.Sequence {
}
if req.StartLedger < ledgerRange.FirstLedger.Sequence || req.StartLedger > ledgerRange.LastLedger.Sequence {
return fmt.Errorf(
"start ledger must be between the oldest ledger: %d and the latest ledger: %d for this rpc instance",
ledgerRange.FirstLedger.Sequence,
Expand Down

0 comments on commit 1acc9af

Please sign in to comment.