Skip to content

Commit

Permalink
services/horizon: Fix operation ordering when querying by claimable b…
Browse files Browse the repository at this point in the history
…alance and liq pools (#3872)
  • Loading branch information
2opremio authored Aug 31, 2021
1 parent 5ea473b commit e0beb7a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
8 changes: 4 additions & 4 deletions services/horizon/internal/db2/history/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ func (q *OperationsQ) ForClaimableBalance(ctx context.Context, cbID xdr.Claimabl
"hocb.history_operation_id = hop.id",
).Where("hocb.history_claimable_balance_id = ?", hCB.InternalID)

// in order to use hocb.history_claimable_balance_id index
q.opIdCol = "hocb.history_claimable_balance_id"
// in order to use hocb.history_operation_id index
q.opIdCol = "hocb.history_operation_id"

return q
}
Expand All @@ -187,8 +187,8 @@ func (q *OperationsQ) ForLiquidityPool(ctx context.Context, lpID string) *Operat
"holp.history_operation_id = hop.id",
).Where("holp.history_liquidity_pool_id = ?", hLP.InternalID)

// in order to use holp.history_liquidity_pool_id index
q.opIdCol = "holp.history_liquidity_pool_id"
// in order to use holp.history_operation_id index
q.opIdCol = "holp.history_operation_id"

return q
}
Expand Down
46 changes: 39 additions & 7 deletions services/horizon/internal/db2/history/operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ func TestOperationByLiquidityPool(t *testing.T) {
txIndex := int32(1)
sequence := int32(56)
txID := toid.New(sequence, txIndex, 0).ToInt64()
opID := toid.New(sequence, txIndex, 1).ToInt64()
opID1 := toid.New(sequence, txIndex, 1).ToInt64()
opID2 := toid.New(sequence, txIndex, 2).ToInt64()

// Insert a phony transaction
transactionBuilder := q.NewTransactionBatchInsertBuilder(2)
Expand All @@ -91,11 +92,25 @@ func TestOperationByLiquidityPool(t *testing.T) {
err = transactionBuilder.Exec(tt.Ctx)
tt.Assert.NoError(err)

// Insert a phony operation
// Insert a two phony operations
operationBuilder := q.NewOperationBatchInsertBuilder(2)
err = operationBuilder.Add(
tt.Ctx,
opID,
opID1,
txID,
1,
xdr.OperationTypeEndSponsoringFutureReserves,
[]byte("{}"),
"GAUJETIZVEP2NRYLUESJ3LS66NVCEGMON4UDCBCSBEVPIID773P2W6AY",
null.String{},
)
tt.Assert.NoError(err)
err = operationBuilder.Exec(tt.Ctx)
tt.Assert.NoError(err)

err = operationBuilder.Add(
tt.Ctx,
opID2,
txID,
1,
xdr.OperationTypeEndSponsoringFutureReserves,
Expand All @@ -111,19 +126,36 @@ func TestOperationByLiquidityPool(t *testing.T) {
liquidityPoolID := "a2f38836a839de008cf1d782c81f45e1253cc5d3dad9110b872965484fec0a49"
toInternalID, err := q.CreateHistoryLiquidityPools(tt.Ctx, []string{liquidityPoolID}, 2)
tt.Assert.NoError(err)
lpOperationBuilder := q.NewOperationLiquidityPoolBatchInsertBuilder(2)
lpOperationBuilder := q.NewOperationLiquidityPoolBatchInsertBuilder(3)
tt.Assert.NoError(err)
internalID, ok := toInternalID[liquidityPoolID]
tt.Assert.True(ok)
err = lpOperationBuilder.Add(tt.Ctx, opID, internalID)
err = lpOperationBuilder.Add(tt.Ctx, opID1, internalID)
tt.Assert.NoError(err)
err = lpOperationBuilder.Add(tt.Ctx, opID2, internalID)
tt.Assert.NoError(err)
err = lpOperationBuilder.Exec(tt.Ctx)
tt.Assert.NoError(err)

ops, _, err := q.Operations().ForLiquidityPool(tt.Ctx, liquidityPoolID).Fetch(tt.Ctx)
// Check ascending order
pq := db2.PageQuery{
Cursor: "",
Order: "asc",
Limit: 2,
}
ops, _, err := q.Operations().ForLiquidityPool(tt.Ctx, liquidityPoolID).Page(pq).Fetch(tt.Ctx)
tt.Assert.NoError(err)
tt.Assert.Len(ops, 1)
tt.Assert.Len(ops, 2)
tt.Assert.Equal(ops[0].ID, opID1)
tt.Assert.Equal(ops[1].ID, opID2)

// Check descending order
pq.Order = "desc"
ops, _, err = q.Operations().ForLiquidityPool(tt.Ctx, liquidityPoolID).Page(pq).Fetch(tt.Ctx)
tt.Assert.NoError(err)
tt.Assert.Len(ops, 2)
tt.Assert.Equal(ops[0].ID, opID2)
tt.Assert.Equal(ops[1].ID, opID1)
}

func TestOperationQueryBuilder(t *testing.T) {
Expand Down

0 comments on commit e0beb7a

Please sign in to comment.