Skip to content

Commit

Permalink
populate default cursor value for history endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
tamirms committed Aug 5, 2024
1 parent 81dbb68 commit 7819376
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 5 deletions.
2 changes: 1 addition & 1 deletion services/horizon/internal/actions/effects.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type GetEffectsHandler struct {
}

func (handler GetEffectsHandler) GetResourcePage(w HeaderWriter, r *http.Request) ([]hal.Pageable, error) {
pq, err := GetPageQuery(handler.LedgerState, r)
pq, err := GetPageQuery(handler.LedgerState, r, DefaultTOID)
if err != nil {
return nil, err
}
Expand Down
24 changes: 24 additions & 0 deletions services/horizon/internal/actions/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/stellar/go/services/horizon/internal/ledger"
hProblem "github.com/stellar/go/services/horizon/internal/render/problem"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/support/ordered"
"github.com/stellar/go/support/render/problem"
"github.com/stellar/go/toid"
"github.com/stellar/go/xdr"
Expand All @@ -44,6 +45,10 @@ type Opt int
const (
// DisableCursorValidation disables cursor validation in GetPageQuery
DisableCursorValidation Opt = iota
// DefaultTOID sets a default cursor value in GetPageQuery based on the ledger state
DefaultTOID Opt = iota

defaultLedgerCursorBuffer = 100
)

// HeaderWriter is an interface for setting HTTP response headers
Expand Down Expand Up @@ -182,10 +187,14 @@ func getLimit(r *http.Request, name string, def uint64, max uint64) (uint64, err
// using the results from a call to GetPagingParams()
func GetPageQuery(ledgerState *ledger.State, r *http.Request, opts ...Opt) (db2.PageQuery, error) {
disableCursorValidation := false
defaultTOID := false
for _, opt := range opts {
if opt == DisableCursorValidation {
disableCursorValidation = true
}
if opt == DefaultTOID {
defaultTOID = true
}
}

cursor, err := getCursor(ledgerState, r, ParamCursor)
Expand Down Expand Up @@ -214,6 +223,21 @@ func GetPageQuery(ledgerState *ledger.State, r *http.Request, opts ...Opt) (db2.

return db2.PageQuery{}, err
}
if cursor == "" && defaultTOID {
if pageQuery.Order == db2.OrderAscending {
pageQuery.Cursor = toid.AfterLedger(
ordered.Max(1, ledgerState.CurrentStatus().HistoryElder-1),
).String()
} else if pageQuery.Order == db2.OrderDescending {
pageQuery.Cursor = toid.AfterLedger(
// add an extra amount to the latest ledger in case the
// ledger state is out of date
ledgerState.CurrentStatus().HistoryLatest + defaultLedgerCursorBuffer,
).String()
} else {
return db2.PageQuery{}, problem.BadRequest
}
}

return pageQuery, nil
}
Expand Down
58 changes: 58 additions & 0 deletions services/horizon/internal/actions/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/url"
"strings"
"testing"
"time"

"github.com/go-chi/chi"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -290,6 +291,63 @@ func TestGetPageQuery(t *testing.T) {
tt.Assert.Error(err)
}

func TestGetPageQueryCursorDefaultTOID(t *testing.T) {
ascReq := makeTestActionRequest("/foo-bar/blah?limit=2", testURLParams())
descReq := makeTestActionRequest("/foo-bar/blah?limit=2&order=desc", testURLParams())

ledgerState := &ledger.State{}
ledgerState.SetHorizonStatus(ledger.HorizonStatus{
HistoryLatest: 7000,
HistoryLatestClosedAt: time.Now(),
HistoryElder: 300,
ExpHistoryLatest: 7000,
})

pq, err := GetPageQuery(ledgerState, ascReq, DefaultTOID)
assert.NoError(t, err)
assert.Equal(t, toid.AfterLedger(299).String(), pq.Cursor)
assert.Equal(t, uint64(2), pq.Limit)
assert.Equal(t, "asc", pq.Order)

pq, err = GetPageQuery(ledgerState, descReq, DefaultTOID)
assert.NoError(t, err)
assert.Equal(t, toid.AfterLedger(7000+defaultLedgerCursorBuffer).String(), pq.Cursor)
assert.Equal(t, uint64(2), pq.Limit)
assert.Equal(t, "desc", pq.Order)

pq, err = GetPageQuery(ledgerState, ascReq)
assert.NoError(t, err)
assert.Empty(t, pq.Cursor)
assert.Equal(t, uint64(2), pq.Limit)
assert.Equal(t, "asc", pq.Order)

pq, err = GetPageQuery(ledgerState, descReq)
assert.NoError(t, err)
assert.Empty(t, pq.Cursor)
assert.Equal(t, uint64(2), pq.Limit)
assert.Equal(t, "desc", pq.Order)

ledgerState.SetHorizonStatus(ledger.HorizonStatus{
HistoryLatest: 7000,
HistoryLatestClosedAt: time.Now(),
HistoryElder: 0,
ExpHistoryLatest: 7000,
})

pq, err = GetPageQuery(ledgerState, ascReq, DefaultTOID)
assert.NoError(t, err)
assert.Equal(t, toid.AfterLedger(1).String(), pq.Cursor)
assert.Equal(t, uint64(2), pq.Limit)
assert.Equal(t, "asc", pq.Order)

pq, err = GetPageQuery(ledgerState, descReq, DefaultTOID)
assert.NoError(t, err)
assert.Equal(t, toid.AfterLedger(7000+defaultLedgerCursorBuffer).String(), pq.Cursor)
assert.Equal(t, uint64(2), pq.Limit)
assert.Equal(t, "desc", pq.Order)

}

func TestGetString(t *testing.T) {
tt := test.Start(t)
defer tt.Finish()
Expand Down
2 changes: 1 addition & 1 deletion services/horizon/internal/actions/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type GetLedgersHandler struct {
}

func (handler GetLedgersHandler) GetResourcePage(w HeaderWriter, r *http.Request) ([]hal.Pageable, error) {
pq, err := GetPageQuery(handler.LedgerState, r)
pq, err := GetPageQuery(handler.LedgerState, r, DefaultTOID)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion services/horizon/internal/actions/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type GetOperationsHandler struct {
func (handler GetOperationsHandler) GetResourcePage(w HeaderWriter, r *http.Request) ([]hal.Pageable, error) {
ctx := r.Context()

pq, err := GetPageQuery(handler.LedgerState, r)
pq, err := GetPageQuery(handler.LedgerState, r, DefaultTOID)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion services/horizon/internal/actions/trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ type GetTradesHandler struct {
func (handler GetTradesHandler) GetResourcePage(w HeaderWriter, r *http.Request) ([]hal.Pageable, error) {
ctx := r.Context()

pq, err := GetPageQuery(handler.LedgerState, r)
pq, err := GetPageQuery(handler.LedgerState, r, DefaultTOID)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion services/horizon/internal/actions/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ type GetTransactionsHandler struct {
func (handler GetTransactionsHandler) GetResourcePage(w HeaderWriter, r *http.Request) ([]hal.Pageable, error) {
ctx := r.Context()

pq, err := GetPageQuery(handler.LedgerState, r)
pq, err := GetPageQuery(handler.LedgerState, r, DefaultTOID)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 7819376

Please sign in to comment.