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 claimable balance query #5200

Merged
Show file tree
Hide file tree
Changes from 1 commit
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/claimable_balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,17 @@ func applyClaimableBalancesQueriesCursor(sql sq.SelectBuilder, lCursor int64, rC
case db2.OrderAscending:
if hasPagedLimit {
sql = sql.
Where(sq.Expr("(last_modified_ledger, id) > (?, ?)", lCursor, rCursor))
Where(sq.Expr("(cb.last_modified_ledger, cb.id) > (?, ?)", lCursor, rCursor))

}
sql = sql.OrderBy("last_modified_ledger asc, id asc")
sql = sql.OrderBy("cb.last_modified_ledger asc, cb.id asc")
case db2.OrderDescending:
if hasPagedLimit {
sql = sql.
Where(sq.Expr("(last_modified_ledger, id) < (?, ?)", lCursor, rCursor))
Where(sq.Expr("(cb.last_modified_ledger, cb.id) < (?, ?)", lCursor, rCursor))
}

sql = sql.OrderBy("last_modified_ledger desc, id desc")
sql = sql.OrderBy("cb.last_modified_ledger desc, cb.id desc")
default:
return sql, errors.Errorf("invalid order: %s", order)
}
Expand Down
182 changes: 182 additions & 0 deletions services/horizon/internal/db2/history/claimable_balances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,188 @@
tt.Assert.Len(cbs, 1)
}

func TestFindClaimableBalancesByCursor(t *testing.T) {

Check failure on line 222 in services/horizon/internal/db2/history/claimable_balances_test.go

View workflow job for this annotation

GitHub Actions / golangci

Function 'TestFindClaimableBalancesByCursor' has too many statements (101 > 50) (funlen)
tt := test.Start(t)
defer tt.Finish()
test.ResetHorizonDB(t, tt.HorizonDB)
q := &Q{tt.HorizonSession()}

tt.Assert.NoError(q.BeginTx(tt.Ctx, &sql.TxOptions{}))
defer func() {
_ = q.Rollback()
}()

balanceInsertBuilder := q.NewClaimableBalanceBatchInsertBuilder()
claimantsInsertBuilder := q.NewClaimableBalanceClaimantBatchInsertBuilder()

dest1 := "GC3C4AKRBQLHOJ45U4XG35ESVWRDECWO5XLDGYADO6DPR3L7KIDVUMML"
dest2 := "GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H"

sponsor1 := "GA25GQLHJU3LPEJXEIAXK23AWEA5GWDUGRSHTQHDFT6HXHVMRULSQJUJ"

Check failure on line 239 in services/horizon/internal/db2/history/claimable_balances_test.go

View workflow job for this annotation

GitHub Actions / golangci

string `GA25GQLHJU3LPEJXEIAXK23AWEA5GWDUGRSHTQHDFT6HXHVMRULSQJUJ` has 3 occurrences, make it a constant (goconst)
sponsor2 := "GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H"

asset := xdr.MustNewCreditAsset("USD", dest1)
balanceID := xdr.ClaimableBalanceId{
Type: xdr.ClaimableBalanceIdTypeClaimableBalanceIdTypeV0,
V0: &xdr.Hash{1, 2, 3},
}
id, err := xdr.MarshalHex(balanceID)
tt.Assert.NoError(err)
cBalance := ClaimableBalance{
BalanceID: id,
Claimants: []Claimant{
{
Destination: dest1,
Predicate: xdr.ClaimPredicate{
Type: xdr.ClaimPredicateTypeClaimPredicateUnconditional,
},
},
},
Asset: asset,
LastModifiedLedger: 123,
Amount: 10,
Sponsor: null.StringFrom(sponsor1),
}

tt.Assert.NoError(balanceInsertBuilder.Add(cBalance))
tt.Assert.NoError(insertClaimants(claimantsInsertBuilder, cBalance))

balanceID = xdr.ClaimableBalanceId{
Type: xdr.ClaimableBalanceIdTypeClaimableBalanceIdTypeV0,
V0: &xdr.Hash{3, 2, 1},
}
id, err = xdr.MarshalHex(balanceID)
tt.Assert.NoError(err)
cBalance = ClaimableBalance{
BalanceID: id,
Claimants: []Claimant{
{
Destination: dest1,
Predicate: xdr.ClaimPredicate{
Type: xdr.ClaimPredicateTypeClaimPredicateUnconditional,
},
},
{
Destination: dest2,
Predicate: xdr.ClaimPredicate{
Type: xdr.ClaimPredicateTypeClaimPredicateUnconditional,
},
},
},
Asset: asset,
LastModifiedLedger: 300,
Amount: 10,
Sponsor: null.StringFrom(sponsor2),
}

tt.Assert.NoError(balanceInsertBuilder.Add(cBalance))
tt.Assert.NoError(insertClaimants(claimantsInsertBuilder, cBalance))

tt.Assert.NoError(claimantsInsertBuilder.Exec(tt.Ctx))
tt.Assert.NoError(balanceInsertBuilder.Exec(tt.Ctx))

query := ClaimableBalancesQuery{
PageQuery: db2.MustPageQuery("", false, "", 10),
}

cbs, err := q.GetClaimableBalances(tt.Ctx, query)
tt.Assert.NoError(err)
tt.Assert.Len(cbs, 2)

order := "" // default is "asc"
// this validates the cb query with claimant and cb.id/ledger cursor parameters
query.PageQuery = db2.MustPageQuery(fmt.Sprintf("%v-%s", 150, cbs[0].BalanceID), false, order, 10)
query.Claimant = xdr.MustAddressPtr(dest1)
cbs, err = q.GetClaimableBalances(tt.Ctx, query)
tt.Assert.NoError(err)
tt.Assert.Len(cbs, 1)
tt.Assert.Equal(dest2, cbs[0].Claimants[1].Destination)

// this validates the cb query with claimant, asset, sponsor and cb.id/ledger cursor parameters
query.PageQuery = db2.MustPageQuery(fmt.Sprintf("%v-%s", 150, cbs[0].BalanceID), false, order, 10)
query.Claimant = xdr.MustAddressPtr(dest1)
query.Asset = &asset
query.Sponsor = xdr.MustAddressPtr(sponsor2)

cbs, err = q.GetClaimableBalances(tt.Ctx, query)
tt.Assert.NoError(err)
tt.Assert.Len(cbs, 1)
tt.Assert.Equal(dest2, cbs[0].Claimants[1].Destination)

// this validates the cb query with no claimant, asset, sponsor and cb.id/ledger cursor parameters
query.PageQuery = db2.MustPageQuery(fmt.Sprintf("%v-%s", 150, cbs[0].BalanceID), false, order, 10)
query.Claimant = nil
query.Asset = &asset
query.Sponsor = xdr.MustAddressPtr(sponsor2)

cbs, err = q.GetClaimableBalances(tt.Ctx, query)
tt.Assert.NoError(err)
tt.Assert.Len(cbs, 1)
tt.Assert.Equal(dest2, cbs[0].Claimants[1].Destination)

order = "desc"
// claimant and cb.id/ledger cursor parameters
query.PageQuery = db2.MustPageQuery(fmt.Sprintf("%v-%s", 301, cbs[0].BalanceID), false, order, 10)
query.Claimant = xdr.MustAddressPtr(dest1)
cbs, err = q.GetClaimableBalances(tt.Ctx, query)
tt.Assert.NoError(err)
tt.Assert.Len(cbs, 1)
tt.Assert.Equal(dest2, cbs[0].Claimants[1].Destination)

// claimant, asset, sponsor and cb.id/ledger cursor parameters
query.PageQuery = db2.MustPageQuery(fmt.Sprintf("%v-%s", 301, cbs[0].BalanceID), false, order, 10)
query.Claimant = xdr.MustAddressPtr(dest1)
query.Asset = &asset
query.Sponsor = xdr.MustAddressPtr(sponsor2)

cbs, err = q.GetClaimableBalances(tt.Ctx, query)
tt.Assert.NoError(err)
tt.Assert.Len(cbs, 1)
tt.Assert.Equal(dest2, cbs[0].Claimants[1].Destination)

// no claimant, asset, sponsor and cb.id/ledger cursor parameters
query.PageQuery = db2.MustPageQuery(fmt.Sprintf("%v-%s", 301, cbs[0].BalanceID), false, order, 10)
query.Claimant = nil
query.Asset = &asset
query.Sponsor = xdr.MustAddressPtr(sponsor2)

cbs, err = q.GetClaimableBalances(tt.Ctx, query)
tt.Assert.NoError(err)
tt.Assert.Len(cbs, 1)
tt.Assert.Equal(dest2, cbs[0].Claimants[1].Destination)

order = "asc"
// claimant and cb.id/ledger cursor parameters
query.PageQuery = db2.MustPageQuery(fmt.Sprintf("%v-%s", 150, cbs[0].BalanceID), false, order, 10)
query.Claimant = xdr.MustAddressPtr(dest1)
cbs, err = q.GetClaimableBalances(tt.Ctx, query)
tt.Assert.NoError(err)
tt.Assert.Len(cbs, 1)
tt.Assert.Equal(dest2, cbs[0].Claimants[1].Destination)

// claimant, asset, sponsor and cb.id/ledger cursor parameters
query.PageQuery = db2.MustPageQuery(fmt.Sprintf("%v-%s", 150, cbs[0].BalanceID), false, order, 10)
query.Claimant = xdr.MustAddressPtr(dest1)
query.Asset = &asset
query.Sponsor = xdr.MustAddressPtr(sponsor2)

cbs, err = q.GetClaimableBalances(tt.Ctx, query)
tt.Assert.NoError(err)
tt.Assert.Len(cbs, 1)
tt.Assert.Equal(dest2, cbs[0].Claimants[1].Destination)

// no claimant, asset, sponsor and cb.id/ledger cursor parameters
query.PageQuery = db2.MustPageQuery(fmt.Sprintf("%v-%s", 150, cbs[0].BalanceID), false, order, 10)
query.Claimant = nil
query.Asset = &asset
query.Sponsor = xdr.MustAddressPtr(sponsor2)

cbs, err = q.GetClaimableBalances(tt.Ctx, query)
tt.Assert.NoError(err)
tt.Assert.Len(cbs, 1)
tt.Assert.Equal(dest2, cbs[0].Claimants[1].Destination)
}

func insertClaimants(claimantsInsertBuilder ClaimableBalanceClaimantBatchInsertBuilder, cBalance ClaimableBalance) error {
for _, claimant := range cBalance.Claimants {
claimant := ClaimableBalanceClaimant{
Expand Down
Loading