From 698593a43c972293c084ce8ad0bea99b95b6dd9b Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Mon, 27 Apr 2020 21:24:52 +0200 Subject: [PATCH] Sort trustlines by asset code and issuer (#2516) --- services/horizon/internal/actions/account.go | 2 +- .../horizon/internal/actions/account_test.go | 49 +++++++++++-------- .../internal/db2/history/trust_lines.go | 6 +-- .../internal/db2/history/trust_lines_test.go | 16 ++++-- 4 files changed, 44 insertions(+), 29 deletions(-) diff --git a/services/horizon/internal/actions/account.go b/services/horizon/internal/actions/account.go index 782d817c01..4b58e093f0 100644 --- a/services/horizon/internal/actions/account.go +++ b/services/horizon/internal/actions/account.go @@ -401,7 +401,7 @@ func (handler GetAccountsHandler) loadData(historyQ *history.Q, accounts []strin func (handler GetAccountsHandler) loadTrustlines(historyQ *history.Q, accounts []string) (map[string][]history.TrustLine, error) { trustLines := make(map[string][]history.TrustLine) - records, err := historyQ.GetTrustLinesByAccountsID(accounts) + records, err := historyQ.GetSortedTrustLinesByAccountIDs(accounts) if err != nil { return trustLines, errors.Wrap(err, "loading trustline records by accounts") } diff --git a/services/horizon/internal/actions/account_test.go b/services/horizon/internal/actions/account_test.go index a16d216240..5dc5757a14 100644 --- a/services/horizon/internal/actions/account_test.go +++ b/services/horizon/internal/actions/account_test.go @@ -128,27 +128,27 @@ var ( } accountSigners = []history.AccountSigner{ - history.AccountSigner{ + { Account: accountOne, Signer: accountOne, Weight: 1, }, - history.AccountSigner{ + { Account: accountTwo, Signer: accountTwo, Weight: 1, }, - history.AccountSigner{ + { Account: accountOne, Signer: signer, Weight: 1, }, - history.AccountSigner{ + { Account: accountTwo, Signer: signer, Weight: 2, }, - history.AccountSigner{ + { Account: signer, Signer: signer, Weight: 3, @@ -198,6 +198,18 @@ func TestAccountInfo(t *testing.T) { }, 6) assert.NoError(t, err) + _, err = q.InsertTrustLine(xdr.TrustLineEntry{ + AccountId: accountID, + Asset: xdr.MustNewCreditAsset( + "EUR", + "GC23QF2HUE52AMXUFUH3AYJAXXGXXV2VHXYYR6EYXETPKDXZSAW67XO4", + ), + Balance: 0, + Limit: 9223372036854775807, + Flags: 1, + }, 6) + assert.NoError(t, err) + account, err := AccountInfo( tt.Ctx, &core.Q{tt.CoreSession()}, @@ -208,20 +220,17 @@ func TestAccountInfo(t *testing.T) { tt.Assert.Equal("8589934593", account.Sequence) tt.Assert.Equal(uint32(4), account.LastModifiedLedger) - tt.Assert.Len(account.Balances, 2) - - for _, balance := range account.Balances { - if balance.Type == "native" { - tt.Assert.Equal(uint32(0), balance.LastModifiedLedger) - } else { - tt.Assert.Equal("USD", balance.Code) - tt.Assert.Equal( - "GC23QF2HUE52AMXUFUH3AYJAXXGXXV2VHXYYR6EYXETPKDXZSAW67XO4", - balance.Issuer, - ) - tt.Assert.NotEqual(uint32(0), balance.LastModifiedLedger) - } - } + tt.Assert.Len(account.Balances, 3) + + tt.Assert.Equal(account.Balances[0].Code, "EUR") + tt.Assert.Equal(account.Balances[1].Code, "USD") + tt.Assert.Equal( + "GC23QF2HUE52AMXUFUH3AYJAXXGXXV2VHXYYR6EYXETPKDXZSAW67XO4", + account.Balances[1].Issuer, + ) + tt.Assert.NotEqual(uint32(0), account.Balances[1].LastModifiedLedger) + tt.Assert.Equal(account.Balances[2].Type, "native") + tt.Assert.Equal(uint32(0), account.Balances[2].LastModifiedLedger) // core account and horizon ingestion account differ // horizon ingestion account has a signer whereas core account @@ -244,7 +253,7 @@ func TestAccountInfo(t *testing.T) { // even though horizon ingestion account differs from core account, // no error is returned because they have different last modified ledgers err = q.UpsertAccounts([]xdr.LedgerEntry{ - xdr.LedgerEntry{ + { LastModifiedLedgerSeq: 5, Data: xdr.LedgerEntryData{ Type: xdr.LedgerEntryTypeAccount, diff --git a/services/horizon/internal/db2/history/trust_lines.go b/services/horizon/internal/db2/history/trust_lines.go index d5dff67f91..cf1303b823 100644 --- a/services/horizon/internal/db2/history/trust_lines.go +++ b/services/horizon/internal/db2/history/trust_lines.go @@ -205,10 +205,10 @@ func (q *Q) RemoveTrustLine(ledgerKey xdr.LedgerKeyTrustLine) (int64, error) { return result.RowsAffected() } -// GetTrustLinesByAccountsID loads trust lines for a list of accounts ID -func (q *Q) GetTrustLinesByAccountsID(id []string) ([]TrustLine, error) { +// GetSortedTrustLinesByAccountIDs loads trust lines for a list of accounts ID, ordered by asset and issuer +func (q *Q) GetSortedTrustLinesByAccountIDs(id []string) ([]TrustLine, error) { var data []TrustLine - sql := selectTrustLines.Where(sq.Eq{"account_id": id}) + sql := selectTrustLines.Where(sq.Eq{"account_id": id}).OrderBy("asset_code", "asset_issuer") err := q.Select(&data, sql) return data, err } diff --git a/services/horizon/internal/db2/history/trust_lines_test.go b/services/horizon/internal/db2/history/trust_lines_test.go index e04d1e65b8..657d23130f 100644 --- a/services/horizon/internal/db2/history/trust_lines_test.go +++ b/services/horizon/internal/db2/history/trust_lines_test.go @@ -160,14 +160,14 @@ func TestUpsertTrustLines(t *testing.T) { assert.NoError(t, err) ledgerEntries := []xdr.LedgerEntry{ - xdr.LedgerEntry{ + { LastModifiedLedgerSeq: 1, Data: xdr.LedgerEntryData{ Type: xdr.LedgerEntryTypeTrustline, TrustLine: &eurTrustLine, }, }, - xdr.LedgerEntry{ + { LastModifiedLedgerSeq: 2, Data: xdr.LedgerEntryData{ Type: xdr.LedgerEntryTypeTrustline, @@ -197,7 +197,7 @@ func TestUpsertTrustLines(t *testing.T) { modifiedTrustLine.Balance = 30000 ledgerEntries = []xdr.LedgerEntry{ - xdr.LedgerEntry{ + { LastModifiedLedgerSeq: 1000, Data: xdr.LedgerEntryData{ Type: xdr.LedgerEntryTypeTrustline, @@ -298,7 +298,7 @@ func TestRemoveTrustLine(t *testing.T) { assert.NoError(t, err) assert.Equal(t, int64(0), rows) } -func TestGetTrustLinesByAccountsID(t *testing.T) { +func TestGetSortedTrustLinesByAccountsID(t *testing.T) { tt := test.Start(t) defer tt.Finish() test.ResetHorizonDB(t, tt.HorizonDB) @@ -316,7 +316,7 @@ func TestGetTrustLinesByAccountsID(t *testing.T) { usdTrustLine.AccountId.Address(), } - records, err := q.GetTrustLinesByAccountsID(ids) + records, err := q.GetSortedTrustLinesByAccountIDs(ids) tt.Assert.NoError(err) tt.Assert.Len(records, 2) @@ -325,7 +325,13 @@ func TestGetTrustLinesByAccountsID(t *testing.T) { usdTrustLine.AccountId.Address(): usdTrustLine, } + lastAssetCode := "" + lastIssuer := records[0].AssetIssuer for _, record := range records { + tt.Assert.LessOrEqual(lastAssetCode, record.AssetCode) + lastAssetCode = record.AssetCode + tt.Assert.LessOrEqual(lastIssuer, record.AssetIssuer) + lastIssuer = record.AssetIssuer xtl, ok := m[record.AccountID] tt.Assert.True(ok) asset := xdr.MustNewCreditAsset(record.AssetCode, record.AssetIssuer)