diff --git a/services/horizon/internal/actions/helpers.go b/services/horizon/internal/actions/helpers.go index bd9608d97a..f4ddeb5b89 100644 --- a/services/horizon/internal/actions/helpers.go +++ b/services/horizon/internal/actions/helpers.go @@ -221,18 +221,14 @@ func GetPageQuery(ledgerState *ledger.State, r *http.Request, opts ...Opt) (db2. return db2.PageQuery{}, err } - elderCursor := toid.AfterLedger( - ordered.Max(0, ledgerState.CurrentStatus().HistoryElder-1), - ).String() if defaultTOID && pageQuery.Order == db2.OrderAscending { if cursor == "" || errors.Is(validateCursorWithinHistory(ledgerState, pageQuery), &hProblem.BeforeHistory) { - pageQuery.Cursor = elderCursor + pageQuery.Cursor = toid.AfterLedger( + ordered.Max(0, ledgerState.CurrentStatus().HistoryElder-1), + ).String() } - } else if defaultTOID && pageQuery.Order == db2.OrderDescending { - pageQuery.ElderCursor = elderCursor } - return pageQuery, nil } diff --git a/services/horizon/internal/db2/history/account_signers.go b/services/horizon/internal/db2/history/account_signers.go index 8014379a4b..78afcdf06c 100644 --- a/services/horizon/internal/db2/history/account_signers.go +++ b/services/horizon/internal/db2/history/account_signers.go @@ -37,7 +37,7 @@ func (q *Q) SignersForAccounts(ctx context.Context, accounts []string) ([]Accoun // AccountsForSigner returns a list of `AccountSigner` rows for a given signer func (q *Q) AccountsForSigner(ctx context.Context, signer string, page db2.PageQuery) ([]AccountSigner, error) { sql := selectAccountSigners.Where("accounts_signers.signer = ?", signer) - sql, err := page.ApplyRawTo(sql, "accounts_signers.account_id") + sql, err := page.ApplyToUsingCursor(sql, "accounts_signers.account_id", page.Cursor) if err != nil { return nil, errors.Wrap(err, "could not apply query to page") } diff --git a/services/horizon/internal/db2/history/accounts.go b/services/horizon/internal/db2/history/accounts.go index 8f3b79a4fe..2af197da4f 100644 --- a/services/horizon/internal/db2/history/accounts.go +++ b/services/horizon/internal/db2/history/accounts.go @@ -144,7 +144,7 @@ func (q *Q) AccountsForAsset(ctx context.Context, asset xdr.Asset, page db2.Page "trust_lines.asset_code": code, }) - sql, err := page.ApplyRawTo(sql, "trust_lines.account_id") + sql, err := page.ApplyToUsingCursor(sql, "trust_lines.account_id", page.Cursor) if err != nil { return nil, errors.Wrap(err, "could not apply query to page") } @@ -168,7 +168,7 @@ func (q *Q) AccountsForLiquidityPool(ctx context.Context, poolID string, page db "trust_lines.liquidity_pool_id": poolID, }) - sql, err := page.ApplyRawTo(sql, "trust_lines.account_id") + sql, err := page.ApplyToUsingCursor(sql, "trust_lines.account_id", page.Cursor) if err != nil { return nil, errors.Wrap(err, "could not apply query to page") } @@ -189,7 +189,7 @@ func selectBySponsor(table, sponsor string, page db2.PageQuery) (sq.SelectBuilde "sponsor": sponsor, }) - sql, err := page.ApplyRawTo(sql, "account_id") + sql, err := page.ApplyToUsingCursor(sql, "account_id", page.Cursor) if err != nil { return sql, errors.Wrap(err, "could not apply query to page") } @@ -255,7 +255,7 @@ func (q *Q) AccountEntriesForSigner(ctx context.Context, signer string, page db2 "accounts_signers.signer": signer, }) - sql, err := page.ApplyRawTo(sql, "accounts_signers.account_id") + sql, err := page.ApplyToUsingCursor(sql, "accounts_signers.account_id", page.Cursor) if err != nil { return nil, errors.Wrap(err, "could not apply query to page") } diff --git a/services/horizon/internal/db2/main.go b/services/horizon/internal/db2/main.go index c497817061..5ec81eb0f3 100644 --- a/services/horizon/internal/db2/main.go +++ b/services/horizon/internal/db2/main.go @@ -5,8 +5,7 @@ package db2 // PageQuery represents a portion of a Query struct concerned with paging // through a large dataset. type PageQuery struct { - Cursor string - Order string - Limit uint64 - ElderCursor string + Cursor string + Order string + Limit uint64 } diff --git a/services/horizon/internal/db2/page_query.go b/services/horizon/internal/db2/page_query.go index 988b294366..3c458af855 100644 --- a/services/horizon/internal/db2/page_query.go +++ b/services/horizon/internal/db2/page_query.go @@ -59,7 +59,7 @@ func (p PageQuery) ApplyTo( return sql, err } - return p.applyToUsingCursor(sql, col, cursor) + return p.ApplyToUsingCursor(sql, col, cursor) } // ApplyRawTo applies the raw PageQuery.Cursor cursor to the builder @@ -67,12 +67,12 @@ func (p PageQuery) ApplyRawTo( sql sq.SelectBuilder, col string, ) (sq.SelectBuilder, error) { - return p.applyToUsingCursor(sql, col, p.Cursor) + return p.ApplyToUsingCursor(sql, col, p.Cursor) } // ApplyToUsingCursor returns a new SelectBuilder after applying the paging effects of // `p` to `sql`. This method allows any type of cursor by a single column -func (p PageQuery) applyToUsingCursor( +func (p PageQuery) ApplyToUsingCursor( sql sq.SelectBuilder, col string, cursor interface{}, @@ -81,23 +81,23 @@ func (p PageQuery) applyToUsingCursor( switch p.Order { case "asc": - if cursor != "" { + if cursor == "" { sql = sql. - Where(fmt.Sprintf("%s > ?", col), cursor) + OrderBy(fmt.Sprintf("%s asc", col)) + } else { + sql = sql. + Where(fmt.Sprintf("%s > ?", col), cursor). + OrderBy(fmt.Sprintf("%s asc", col)) } - sql = sql. - OrderBy(fmt.Sprintf("%s asc", col)) case "desc": - if cursor != "" { + if cursor == "" { sql = sql. - Where(fmt.Sprintf("%s < ?", col), cursor) - } - if p.ElderCursor != "" { + OrderBy(fmt.Sprintf("%s desc", col)) + } else { sql = sql. - Where(fmt.Sprintf("%s > ?", col), p.ElderCursor) + Where(fmt.Sprintf("%s < ?", col), cursor). + OrderBy(fmt.Sprintf("%s desc", col)) } - sql = sql. - OrderBy(fmt.Sprintf("%s desc", col)) default: return sql, errors.Errorf("invalid order: %s", p.Order) }