Skip to content

Commit

Permalink
Sort trustlines by asset code and issuer (#2516)
Browse files Browse the repository at this point in the history
  • Loading branch information
2opremio authored Apr 27, 2020
1 parent 04ac85c commit 698593a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 29 deletions.
2 changes: 1 addition & 1 deletion services/horizon/internal/actions/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
49 changes: 29 additions & 20 deletions services/horizon/internal/actions/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()},
Expand All @@ -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
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions services/horizon/internal/db2/history/trust_lines.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 11 additions & 5 deletions services/horizon/internal/db2/history/trust_lines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand All @@ -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)
Expand Down

0 comments on commit 698593a

Please sign in to comment.