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

Support new account fields for protocol-19. #4294

Merged
merged 1 commit into from
Mar 23, 2022
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
2 changes: 2 additions & 0 deletions protocols/horizon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type Account struct {
ID string `json:"id"`
AccountID string `json:"account_id"`
Sequence string `json:"sequence"`
SequenceLedger uint32 `json:"sequence_ledger,omitempty"`
SequenceTime string `json:"sequence_time,omitempty"`
SubentryCount int32 `json:"subentry_count"`
InflationDestination string `json:"inflation_destination,omitempty"`
HomeDomain string `json:"home_domain,omitempty"`
Expand Down
8 changes: 8 additions & 0 deletions services/horizon/internal/actions/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ var (
AccountID: accountOne,
Balance: 20000,
SequenceNumber: 223456789,
SequenceLedger: 2345,
SequenceTime: time.Unix(1647265533, 0),
NumSubEntries: 10,
Flags: 1,
HomeDomain: "stellar.org",
Expand All @@ -48,6 +50,8 @@ var (
AccountID: accountTwo,
Balance: 50000,
SequenceNumber: 648736,
SequenceLedger: 3456,
SequenceTime: time.Unix(1647365533, 0),
NumSubEntries: 10,
Flags: 2,
HomeDomain: "meridian.stellar.org",
Expand All @@ -64,6 +68,8 @@ var (
AccountID: signer,
Balance: 50000,
SequenceNumber: 648736,
SequenceLedger: 4567,
SequenceTime: time.Unix(1647465533, 0),
NumSubEntries: 10,
Flags: 2,
MasterWeight: 5,
Expand Down Expand Up @@ -170,6 +176,8 @@ func TestAccountInfo(t *testing.T) {
AccountID: accountID,
Balance: 9999999900,
SequenceNumber: 8589934593,
SequenceLedger: 4567,
SequenceTime: time.Unix(1647465533, 0),
NumSubEntries: 1,
InflationDestination: "",
HomeDomain: "",
Expand Down
12 changes: 9 additions & 3 deletions services/horizon/internal/db2/history/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,18 @@ func (q *Q) GetAccountsByIDs(ctx context.Context, ids []string) ([]AccountEntry,
// for each ledger with the current limits.
func (q *Q) UpsertAccounts(ctx context.Context, accounts []AccountEntry) error {
var accountID, inflationDestination, homeDomain, balance, buyingLiabilities,
sellingLiabilities, sequenceNumber, numSubEntries, flags, lastModifiedLedger,
numSponsored, numSponsoring, masterWeight, thresholdLow, thresholdMedium,
thresholdHigh, sponsor []interface{}
sellingLiabilities, sequenceNumber, sequenceLedger, sequenceTime, numSubEntries,
flags, lastModifiedLedger, numSponsored, numSponsoring, masterWeight, thresholdLow,
thresholdMedium, thresholdHigh, sponsor []interface{}

for _, account := range accounts {
accountID = append(accountID, account.AccountID)
balance = append(balance, account.Balance)
buyingLiabilities = append(buyingLiabilities, account.BuyingLiabilities)
sellingLiabilities = append(sellingLiabilities, account.SellingLiabilities)
sequenceNumber = append(sequenceNumber, account.SequenceNumber)
sequenceLedger = append(sequenceLedger, account.SequenceLedger)
sequenceTime = append(sequenceTime, account.SequenceTime)
numSubEntries = append(numSubEntries, account.NumSubEntries)
inflationDestination = append(inflationDestination, account.InflationDestination)
homeDomain = append(homeDomain, account.HomeDomain)
Expand All @@ -95,6 +97,8 @@ func (q *Q) UpsertAccounts(ctx context.Context, accounts []AccountEntry) error {
{"buying_liabilities", "bigint", buyingLiabilities},
{"selling_liabilities", "bigint", sellingLiabilities},
{"sequence_number", "bigint", sequenceNumber},
{"sequence_ledger", "int", sequenceLedger},
{"sequence_time", "timestamp", sequenceTime},
{"num_subentries", "int", numSubEntries},
{"inflation_destination", "text", inflationDestination},
{"flags", "int", flags},
Expand Down Expand Up @@ -270,6 +274,8 @@ var selectAccounts = sq.Select(`
buying_liabilities,
selling_liabilities,
sequence_number,
sequence_ledger,
sequence_time,
num_subentries,
inflation_destination,
flags,
Expand Down
18 changes: 17 additions & 1 deletion services/horizon/internal/db2/history/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package history

import (
"testing"
"time"

"github.com/guregu/null"
"github.com/stellar/go/services/horizon/internal/db2"
Expand All @@ -19,6 +20,8 @@ var (
AccountID: "GAOQJGUAB7NI7K7I62ORBXMN3J4SSWQUQ7FOEPSDJ322W2HMCNWPHXFB",
Balance: 20000,
SequenceNumber: 223456789,
SequenceLedger: 2345,
SequenceTime: time.Unix(1647265533, 0).UTC(),
NumSubEntries: 10,
InflationDestination: inflationDest,
Flags: 1,
Expand All @@ -36,6 +39,8 @@ var (
AccountID: "GCT2NQM5KJJEF55NPMY444C6M6CA7T33HRNCMA6ZFBIIXKNCRO6J25K7",
Balance: 50000,
SequenceNumber: 648736,
SequenceLedger: 3456,
SequenceTime: time.Unix(1647365533, 0).UTC(),
NumSubEntries: 10,
InflationDestination: inflationDest,
Flags: 2,
Expand All @@ -56,6 +61,8 @@ var (
AccountID: "GDPGOMFSP4IF7A4P7UBKA4UC4QTRLEHGBD6IMDIS3W3KBDNBFAQ7FXDY",
Balance: 50000,
SequenceNumber: 648736,
SequenceLedger: 4567,
SequenceTime: time.Unix(1647465533, 0).UTC(),
NumSubEntries: 10,
InflationDestination: inflationDest,
Flags: 2,
Expand Down Expand Up @@ -87,6 +94,10 @@ func TestInsertAccount(t *testing.T) {
assert.Equal(t, "GAOQJGUAB7NI7K7I62ORBXMN3J4SSWQUQ7FOEPSDJ322W2HMCNWPHXFB", accounts[0].AccountID)
assert.Equal(t, int64(20000), accounts[0].Balance)
assert.Equal(t, int64(223456789), accounts[0].SequenceNumber)
assert.Equal(t, uint32(2345), accounts[0].SequenceLedger)
Shaptic marked this conversation as resolved.
Show resolved Hide resolved
// Convert actual result to UTC timezone for comparison;
// query results from DB for SequenceTime supply a blank timezone.
assert.Equal(t, time.Unix(1647265533, 0).UTC(), accounts[0].SequenceTime.UTC())
assert.Equal(t, uint32(10), accounts[0].NumSubEntries)
assert.Equal(t, "GBUH7T6U36DAVEKECMKN5YEBQYZVRBPNSZAAKBCO6P5HBMDFSQMQL4Z4", accounts[0].InflationDestination)
assert.Equal(t, uint32(1), accounts[0].Flags)
Expand Down Expand Up @@ -121,6 +132,7 @@ func TestUpsertAccount(t *testing.T) {
AccountID: "GAOQJGUAB7NI7K7I62ORBXMN3J4SSWQUQ7FOEPSDJ322W2HMCNWPHXFB",
Balance: 32847893,
SequenceNumber: 223456789,
SequenceLedger: 2345,
NumSubEntries: 10,
InflationDestination: inflationDest,
Flags: 1,
Expand Down Expand Up @@ -156,14 +168,17 @@ func TestUpsertAccount(t *testing.T) {
assert.NoError(t, err)
assert.Len(t, accounts, 1)

accounts[0].SequenceTime = modifiedAccount.SequenceTime
assert.Equal(t, modifiedAccount, accounts[0])
assert.Equal(t, uint32(1234), accounts[0].LastModifiedLedger)

accounts, err = q.GetAccountsByIDs(tt.Ctx, []string{account2.AccountID})
assert.NoError(t, err)
assert.Len(t, accounts, 1)

assert.Equal(t, account2, accounts[0])
expectedAccount := account2
expectedAccount.SequenceTime = accounts[0].SequenceTime
assert.Equal(t, expectedAccount, accounts[0])
assert.Equal(t, uint32(1235), accounts[0].LastModifiedLedger)
}

Expand Down Expand Up @@ -445,6 +460,7 @@ func TestGetAccountByID(t *testing.T) {
assert.Equal(t, "GAOQJGUAB7NI7K7I62ORBXMN3J4SSWQUQ7FOEPSDJ322W2HMCNWPHXFB", resultAccount.AccountID)
assert.Equal(t, int64(20000), resultAccount.Balance)
assert.Equal(t, int64(223456789), resultAccount.SequenceNumber)
assert.Equal(t, uint32(2345), resultAccount.SequenceLedger)
assert.Equal(t, uint32(10), resultAccount.NumSubEntries)
assert.Equal(t, "GBUH7T6U36DAVEKECMKN5YEBQYZVRBPNSZAAKBCO6P5HBMDFSQMQL4Z4", resultAccount.InflationDestination)
assert.Equal(t, uint32(1), resultAccount.Flags)
Expand Down
2 changes: 2 additions & 0 deletions services/horizon/internal/db2/history/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ type AccountEntry struct {
BuyingLiabilities int64 `db:"buying_liabilities"`
SellingLiabilities int64 `db:"selling_liabilities"`
SequenceNumber int64 `db:"sequence_number"`
SequenceLedger uint32 `db:"sequence_ledger"`
SequenceTime time.Time `db:"sequence_time"`
NumSubEntries uint32 `db:"num_subentries"`
InflationDestination string `db:"inflation_destination"`
HomeDomain string `db:"home_domain"`
Expand Down
23 changes: 23 additions & 0 deletions services/horizon/internal/db2/schema/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- +migrate Up
ALTER TABLE accounts ADD sequence_ledger integer null;
ALTER TABLE accounts ADD sequence_time timestamp without time zone null;

-- +migrate Down
ALTER TABLE accounts DROP sequence_ledger;
ALTER TABLE accounts DROP sequence_time;
3 changes: 3 additions & 0 deletions services/horizon/internal/ingest/processor_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"reflect"
"testing"
"time"

"github.com/guregu/null"
"github.com/stretchr/testify/assert"
Expand All @@ -29,6 +30,7 @@ func TestProcessorRunnerRunHistoryArchiveIngestionGenesis(t *testing.T) {
AccountID: "GAAZI4TCR3TY5OJHCTJC2A4QSY6CJWJH5IAJTGKIN2ER7LBNVKOCCWN7",
Balance: int64(1000000000000000000),
SequenceNumber: 0,
SequenceTime: time.Unix(0, 0).UTC(),
MasterWeight: 1,
},
}).Return(nil).Once()
Expand Down Expand Up @@ -94,6 +96,7 @@ func TestProcessorRunnerRunHistoryArchiveIngestionHistoryArchive(t *testing.T) {
AccountID: "GAAZI4TCR3TY5OJHCTJC2A4QSY6CJWJH5IAJTGKIN2ER7LBNVKOCCWN7",
Balance: int64(1000000000000000000),
SequenceNumber: 0,
SequenceTime: time.Unix(0, 0).UTC(),
MasterWeight: 1,
},
}).Return(nil).Once()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package processors

import (
"context"

"github.com/stellar/go/ingest"
"github.com/stellar/go/services/horizon/internal/db2/history"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/xdr"
"time"
)

type AccountsProcessor struct {
Expand Down Expand Up @@ -111,12 +111,17 @@ func (p *AccountsProcessor) ledgerEntryToRow(entry xdr.LedgerEntry) history.Acco
inflationDestination = account.InflationDest.Address()
}

seqTime := account.SeqTime()
seqLedger := account.SeqLedger()

return history.AccountEntry{
AccountID: account.AccountId.Address(),
Balance: int64(account.Balance),
BuyingLiabilities: int64(liabilities.Buying),
SellingLiabilities: int64(liabilities.Selling),
SequenceNumber: int64(account.SeqNum),
SequenceLedger: uint32(seqLedger),
SequenceTime: time.Unix(int64(seqTime), 0).UTC(),
NumSubEntries: uint32(account.NumSubEntries),
InflationDestination: inflationDestination,
Flags: uint32(account.Flags),
Expand Down
Loading