Skip to content

Commit

Permalink
services/horizon/internal/db2/history: Improve effects for liquidity …
Browse files Browse the repository at this point in the history
…pool query (#4065)

* Improve effects for liquidity pool query

* Update changelog
  • Loading branch information
tamirms authored Nov 12, 2021
1 parent e9d8dbd commit c7b9e2f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
1 change: 1 addition & 0 deletions services/horizon/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ file. This project adheres to [Semantic Versioning](http://semver.org/).
* Add a new horizon flag `--max-assets-per-path-request` (`15` by default) that sets the number of assets to consider for strict-send and strict-recieve ([4046](https://github.com/stellar/go/pull/4046))
* Add an endpoint that allows querying for which liquidity pools an account is participating in [4043](https://github.com/stellar/go/pull/4043)
* Add a new horizon command `horizon db fill-gaps` which fills any gaps in history in the horizon db. The command takes optional start and end ledger parameters. If the start and end ledger is provided then horizon will only fill the gaps found within the given ledger range [4060](https://github.com/stellar/go/pull/4060)
* Improve performance of `/liquidity_pools/{liquidity_pool_id}/effects` endpoint by optimizing the db query to fetch effects for a liquidity pool [4065](https://github.com/stellar/go/pull/4065)

## v2.10.0

Expand Down
2 changes: 1 addition & 1 deletion services/horizon/internal/actions/effects.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func loadEffectRecords(ctx context.Context, hq *history.Q, qp EffectsQuery, pq d
case qp.AccountID != "":
effects.ForAccount(ctx, qp.AccountID)
case qp.LiquidityPoolID != "":
effects.ForLiquidityPool(qp.LiquidityPoolID)
effects.ForLiquidityPool(ctx, pq, qp.LiquidityPoolID)
case qp.OperationID > 0:
effects.ForOperation(int64(qp.OperationID))
case qp.LedgerID > 0:
Expand Down
41 changes: 36 additions & 5 deletions services/horizon/internal/db2/history/effect.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,43 @@ func (q *EffectsQ) ForOperation(id int64) *EffectsQ {
return q
}

// ForLiquidityPoolID filters the query to only effects in a specific liquidity pool,
// ForLiquidityPool filters the query to only effects in a specific liquidity pool,
// specified by its id.
func (q *EffectsQ) ForLiquidityPool(id string) *EffectsQ {
q.sql = q.sql.InnerJoin("history_operation_liquidity_pools holp ON holp.history_operation_id = heff.history_operation_id")
q.sql = q.sql.InnerJoin("history_liquidity_pools hlp ON hlp.id = holp.history_liquidity_pool_id")
q.sql = q.sql.Where("hlp.liquidity_pool_id = ?", id)
func (q *EffectsQ) ForLiquidityPool(ctx context.Context, page db2.PageQuery, id string) *EffectsQ {
if q.Err != nil {
return q
}

op, _, err := page.CursorInt64Pair(db2.DefaultPairSep)
if err != nil {
q.Err = err
return q
}

query := `SELECT holp.history_operation_id
FROM history_operation_liquidity_pools holp
WHERE holp.history_liquidity_pool_id = (SELECT id FROM history_liquidity_pools WHERE liquidity_pool_id = ?)
`
switch page.Order {
case "asc":
query += "AND holp.history_operation_id >= ? ORDER BY holp.history_operation_id asc LIMIT ?"
case "desc":
query += "AND holp.history_operation_id <= ? ORDER BY holp.history_operation_id desc LIMIT ?"
default:
q.Err = errors.Errorf("invalid paging order: %s", page.Order)
return q
}

var liquidityPoolOperationIDs []int64
err = q.parent.SelectRaw(ctx, &liquidityPoolOperationIDs, query, id, op, page.Limit)
if err != nil {
q.Err = err
return q
}

q.sql = q.sql.Where(map[string]interface{}{
"heff.history_operation_id": liquidityPoolOperationIDs,
})
return q
}

Expand Down
7 changes: 6 additions & 1 deletion services/horizon/internal/db2/history/effect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/guregu/null"
"github.com/stellar/go/protocols/horizon/effects"
"github.com/stellar/go/services/horizon/internal/db2"
"github.com/stellar/go/services/horizon/internal/test"
"github.com/stellar/go/services/horizon/internal/toid"
)
Expand Down Expand Up @@ -57,7 +58,11 @@ func TestEffectsForLiquidityPool(t *testing.T) {
tt.Assert.NoError(err)

var result []Effect
err = q.Effects().ForLiquidityPool(liquidityPoolID).Select(tt.Ctx, &result)
err = q.Effects().ForLiquidityPool(tt.Ctx, db2.PageQuery{
Cursor: "0-0",
Order: "asc",
Limit: 10,
}, liquidityPoolID).Select(tt.Ctx, &result)
tt.Assert.NoError(err)

tt.Assert.Len(result, 1)
Expand Down

0 comments on commit c7b9e2f

Please sign in to comment.