Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

services/horizon: Fix operation ordering when querying by claimable balance and liq pools #3872

Merged
merged 2 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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