Skip to content

Commit

Permalink
services/horizon: Properly allow nullable Protocol 19 account fields (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaptic authored Apr 29, 2022
1 parent c9d3b4a commit 453586e
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 83 deletions.
17 changes: 9 additions & 8 deletions services/horizon/internal/actions/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/guregu/null"
"github.com/guregu/null/zero"

"github.com/stretchr/testify/assert"

Expand All @@ -32,8 +33,8 @@ var (
AccountID: accountOne,
Balance: 20000,
SequenceNumber: 223456789,
SequenceLedger: 2345,
SequenceTime: 1647265533,
SequenceLedger: zero.IntFrom(0),
SequenceTime: zero.IntFrom(0),
NumSubEntries: 10,
Flags: 1,
HomeDomain: "stellar.org",
Expand All @@ -50,8 +51,8 @@ var (
AccountID: accountTwo,
Balance: 50000,
SequenceNumber: 648736,
SequenceLedger: 3456,
SequenceTime: 1647365533,
SequenceLedger: zero.IntFrom(3456),
SequenceTime: zero.IntFrom(1647365533),
NumSubEntries: 10,
Flags: 2,
HomeDomain: "meridian.stellar.org",
Expand All @@ -68,8 +69,8 @@ var (
AccountID: signer,
Balance: 50000,
SequenceNumber: 648736,
SequenceLedger: 4567,
SequenceTime: 1647465533,
SequenceLedger: zero.IntFrom(4567),
SequenceTime: zero.IntFrom(1647465533),
NumSubEntries: 10,
Flags: 2,
MasterWeight: 5,
Expand Down Expand Up @@ -176,8 +177,8 @@ func TestAccountInfo(t *testing.T) {
AccountID: accountID,
Balance: 9999999900,
SequenceNumber: 8589934593,
SequenceLedger: 4567,
SequenceTime: 1647465533,
SequenceLedger: zero.IntFrom(4567),
SequenceTime: zero.IntFrom(1647465533),
NumSubEntries: 1,
InflationDestination: "",
HomeDomain: "",
Expand Down
2 changes: 2 additions & 0 deletions services/horizon/internal/codes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ func String(code interface{}) (string, error) {
return "tx_internal_error", nil
case xdr.TransactionResultCodeTxBadSponsorship:
return "tx_bad_sponsorship", nil
case xdr.TransactionResultCodeTxBadMinSeqAgeOrGap:
return "tx_bad_minseq_age_or_gap", nil
case xdr.TransactionResultCodeTxMalformed:
return "tx_malformed", nil
}
Expand Down
24 changes: 13 additions & 11 deletions services/horizon/internal/db2/history/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/guregu/null"
"github.com/guregu/null/zero"
"github.com/stellar/go/services/horizon/internal/db2"
"github.com/stellar/go/services/horizon/internal/test"
"github.com/stellar/go/xdr"
Expand All @@ -19,8 +20,8 @@ var (
AccountID: "GAOQJGUAB7NI7K7I62ORBXMN3J4SSWQUQ7FOEPSDJ322W2HMCNWPHXFB",
Balance: 20000,
SequenceNumber: 223456789,
SequenceLedger: 2345,
SequenceTime: 1647265533,
SequenceLedger: zero.IntFrom(0),
SequenceTime: zero.IntFrom(0),
NumSubEntries: 10,
InflationDestination: inflationDest,
Flags: 1,
Expand All @@ -38,8 +39,8 @@ var (
AccountID: "GCT2NQM5KJJEF55NPMY444C6M6CA7T33HRNCMA6ZFBIIXKNCRO6J25K7",
Balance: 50000,
SequenceNumber: 648736,
SequenceLedger: 3456,
SequenceTime: 1647365533,
SequenceLedger: zero.IntFrom(3456),
SequenceTime: zero.IntFrom(1647365533),
NumSubEntries: 10,
InflationDestination: inflationDest,
Flags: 2,
Expand All @@ -60,8 +61,8 @@ var (
AccountID: "GDPGOMFSP4IF7A4P7UBKA4UC4QTRLEHGBD6IMDIS3W3KBDNBFAQ7FXDY",
Balance: 50000,
SequenceNumber: 648736,
SequenceLedger: 4567,
SequenceTime: 1647465533,
SequenceLedger: zero.IntFrom(4567),
SequenceTime: zero.IntFrom(1647465533),
NumSubEntries: 10,
InflationDestination: inflationDest,
Flags: 2,
Expand Down Expand Up @@ -93,8 +94,8 @@ 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)
assert.Equal(t, uint64(1647265533), accounts[0].SequenceTime)
assert.Equal(t, zero.IntFrom(0), accounts[0].SequenceLedger)
assert.Equal(t, zero.IntFrom(0), accounts[0].SequenceTime)
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 @@ -129,8 +130,8 @@ func TestUpsertAccount(t *testing.T) {
AccountID: "GAOQJGUAB7NI7K7I62ORBXMN3J4SSWQUQ7FOEPSDJ322W2HMCNWPHXFB",
Balance: 32847893,
SequenceNumber: 223456789,
SequenceTime: 223456789223456789,
SequenceLedger: 2345,
SequenceTime: zero.IntFrom(223456789223456789),
SequenceLedger: zero.IntFrom(2345),
NumSubEntries: 10,
InflationDestination: inflationDest,
Flags: 1,
Expand Down Expand Up @@ -458,7 +459,8 @@ 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, zero.IntFrom(0), resultAccount.SequenceLedger)
assert.Equal(t, zero.IntFrom(0), resultAccount.SequenceTime)
assert.Equal(t, uint32(10), resultAccount.NumSubEntries)
assert.Equal(t, "GBUH7T6U36DAVEKECMKN5YEBQYZVRBPNSZAAKBCO6P5HBMDFSQMQL4Z4", resultAccount.InflationDestination)
assert.Equal(t, uint32(1), resultAccount.Flags)
Expand Down
5 changes: 3 additions & 2 deletions services/horizon/internal/db2/history/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

sq "github.com/Masterminds/squirrel"
"github.com/guregu/null"
"github.com/guregu/null/zero"
"github.com/jmoiron/sqlx"
"github.com/lib/pq"

Expand Down Expand Up @@ -221,8 +222,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 uint64 `db:"sequence_time"`
SequenceLedger zero.Int `db:"sequence_ledger"`
SequenceTime zero.Int `db:"sequence_time"`
NumSubEntries uint32 `db:"num_subentries"`
InflationDestination string `db:"inflation_destination"`
HomeDomain string `db:"home_domain"`
Expand Down
4 changes: 2 additions & 2 deletions services/horizon/internal/ingest/processor_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/guregu/null"
"github.com/guregu/null/zero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

Expand All @@ -30,7 +31,7 @@ func TestProcessorRunnerRunHistoryArchiveIngestionGenesis(t *testing.T) {
AccountID: "GAAZI4TCR3TY5OJHCTJC2A4QSY6CJWJH5IAJTGKIN2ER7LBNVKOCCWN7",
Balance: int64(1000000000000000000),
SequenceNumber: 0,
SequenceTime: 0,
SequenceTime: zero.IntFrom(0),
MasterWeight: 1,
},
}).Return(nil).Once()
Expand Down Expand Up @@ -96,7 +97,6 @@ func TestProcessorRunnerRunHistoryArchiveIngestionHistoryArchive(t *testing.T) {
AccountID: "GAAZI4TCR3TY5OJHCTJC2A4QSY6CJWJH5IAJTGKIN2ER7LBNVKOCCWN7",
Balance: int64(1000000000000000000),
SequenceNumber: 0,
SequenceTime: 0,
MasterWeight: 1,
},
}).Return(nil).Once()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package processors
import (
"context"

"github.com/guregu/null/zero"
"github.com/stellar/go/ingest"
"github.com/stellar/go/services/horizon/internal/db2/history"
"github.com/stellar/go/support/errors"
Expand Down Expand Up @@ -117,8 +118,8 @@ func (p *AccountsProcessor) ledgerEntryToRow(entry xdr.LedgerEntry) history.Acco
BuyingLiabilities: int64(liabilities.Buying),
SellingLiabilities: int64(liabilities.Selling),
SequenceNumber: int64(account.SeqNum),
SequenceLedger: uint32(account.SeqLedger()),
SequenceTime: uint64(account.SeqTime()),
SequenceLedger: zero.IntFrom(int64(account.SeqLedger())),
SequenceTime: zero.IntFrom(int64(account.SeqTime())),
NumSubEntries: uint32(account.NumSubEntries),
InflationDestination: inflationDestination,
Flags: uint32(account.Flags),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"testing"

"github.com/guregu/null/zero"
"github.com/stellar/go/ingest"
"github.com/stellar/go/services/horizon/internal/db2/history"
"github.com/stellar/go/xdr"
Expand Down Expand Up @@ -47,7 +48,6 @@ func (s *AccountsProcessorTestSuiteState) TestCreatesAccounts() {
{
LastModifiedLedger: 123,
AccountID: "GC3C4AKRBQLHOJ45U4XG35ESVWRDECWO5XLDGYADO6DPR3L7KIDVUMML",
SequenceTime: 0,
MasterWeight: 1,
ThresholdLow: 1,
ThresholdMedium: 1,
Expand Down Expand Up @@ -152,7 +152,6 @@ func (s *AccountsProcessorTestSuiteLedger) TestNewAccount() {
[]history.AccountEntry{
{
AccountID: "GC3C4AKRBQLHOJ45U4XG35ESVWRDECWO5XLDGYADO6DPR3L7KIDVUMML",
SequenceTime: 0,
MasterWeight: 0,
ThresholdLow: 1,
ThresholdMedium: 2,
Expand All @@ -164,24 +163,16 @@ func (s *AccountsProcessorTestSuiteLedger) TestNewAccount() {
).Return(nil).Once()
}

func (s *AccountsProcessorTestSuiteLedger) TestNewAccountWithExtension() {
func (s *AccountsProcessorTestSuiteLedger) TestNewAccountUpgrade() {
account := xdr.AccountEntry{
AccountId: xdr.MustAddress("GC3C4AKRBQLHOJ45U4XG35ESVWRDECWO5XLDGYADO6DPR3L7KIDVUMML"),
Thresholds: [4]byte{1, 1, 1, 1},
Ext: xdr.AccountEntryExt{
V: 1,
V1: &xdr.AccountEntryExtensionV1{
Ext: xdr.AccountEntryExtensionV1Ext{
V: 2,
V2: &xdr.AccountEntryExtensionV2{
Ext: xdr.AccountEntryExtensionV2Ext{
V: 3,
V3: &xdr.AccountEntryExtensionV3{
SeqLedger: 2345,
SeqTime: 1647265533,
},
},
},
V: 2,
V2: &xdr.AccountEntryExtensionV2{},
},
},
},
Expand Down Expand Up @@ -250,8 +241,8 @@ func (s *AccountsProcessorTestSuiteLedger) TestNewAccountWithExtension() {
[]history.AccountEntry{
{
AccountID: "GC3C4AKRBQLHOJ45U4XG35ESVWRDECWO5XLDGYADO6DPR3L7KIDVUMML",
SequenceLedger: 2346,
SequenceTime: 1647265534,
SequenceLedger: zero.IntFrom(2346),
SequenceTime: zero.IntFrom(1647265534),
MasterWeight: 0,
ThresholdLow: 1,
ThresholdMedium: 2,
Expand Down Expand Up @@ -338,8 +329,8 @@ func (s *AccountsProcessorTestSuiteLedger) TestProcessUpgradeChange() {
{
LastModifiedLedger: uint32(lastModifiedLedgerSeq) + 1,
AccountID: "GC3C4AKRBQLHOJ45U4XG35ESVWRDECWO5XLDGYADO6DPR3L7KIDVUMML",
SequenceTime: 0,
SequenceLedger: 0,
SequenceTime: zero.IntFrom(0),
SequenceLedger: zero.IntFrom(0),
MasterWeight: 0,
ThresholdLow: 1,
ThresholdMedium: 2,
Expand Down Expand Up @@ -405,8 +396,6 @@ func (s *AccountsProcessorTestSuiteLedger) TestFeeProcessedBeforeEverythingElse(
LastModifiedLedger: 0,
AccountID: "GAHK7EEG2WWHVKDNT4CEQFZGKF2LGDSW2IVM4S5DP42RBW3K6BTODB4A",
Balance: 300,
SequenceTime: 0,
SequenceLedger: 0,
},
},
).Return(nil).Once()
Expand Down
20 changes: 13 additions & 7 deletions services/horizon/internal/ingest/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,18 @@ func addAccountsToStateVerifier(ctx context.Context, verifier *verify.StateVerif
}
}

// Accounts that haven't done anything since Protocol 19 will not have a
// V3 extension, so we need to check whether or not this extension needs
// to be filled out.
v3extension := xdr.AccountEntryExtensionV2Ext{V: 0}
if row.SequenceLedger.Valid && row.SequenceTime.Valid {
v3extension.V = 3
v3extension.V3 = &xdr.AccountEntryExtensionV3{
SeqLedger: xdr.Uint32(row.SequenceLedger.Int64),
SeqTime: xdr.TimePoint(row.SequenceTime.Int64),
}
}

account := &xdr.AccountEntry{
AccountId: xdr.MustAddress(row.AccountID),
Balance: xdr.Int64(row.Balance),
Expand Down Expand Up @@ -422,13 +434,7 @@ func addAccountsToStateVerifier(ctx context.Context, verifier *verify.StateVerif
NumSponsored: xdr.Uint32(row.NumSponsored),
NumSponsoring: xdr.Uint32(row.NumSponsoring),
SignerSponsoringIDs: signerSponsoringIDs,
Ext: xdr.AccountEntryExtensionV2Ext{
V: 3,
V3: &xdr.AccountEntryExtensionV3{
SeqLedger: xdr.Uint32(row.SequenceLedger),
SeqTime: xdr.TimePoint(row.SequenceTime),
},
},
Ext: v3extension,
},
},
},
Expand Down
8 changes: 5 additions & 3 deletions services/horizon/internal/ingest/verify_range_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
"database/sql"
"fmt"
"io"
"math"
"testing"

"github.com/guregu/null"
"github.com/guregu/null/zero"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"

Expand Down Expand Up @@ -334,7 +336,7 @@ func (s *VerifyRangeStateTestSuite) TestSuccessWithVerify() {
Ext: xdr.AccountEntryExtensionV2Ext{
V: 3,
V3: &xdr.AccountEntryExtensionV3{
SeqTime: xdr.TimePoint(18446744011573954816),
SeqTime: xdr.TimePoint(math.MaxInt64),
SeqLedger: xdr.Uint32(12345678),
},
},
Expand Down Expand Up @@ -485,8 +487,8 @@ func (s *VerifyRangeStateTestSuite) TestSuccessWithVerify() {
AccountID: mockAccountID,
Balance: 600,
LastModifiedLedger: 62,
SequenceTime: 18446744011573954816,
SequenceLedger: 12345678,
SequenceTime: zero.IntFrom(9223372036854775807),
SequenceLedger: zero.IntFrom(12345678),
MasterWeight: 1,
NumSponsored: 0,
NumSponsoring: 2,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package integration

import (
"fmt"
"strconv"
"testing"

Expand Down Expand Up @@ -54,8 +53,6 @@ func TestProtocol19Upgrade(t *testing.T) {
tt.Error(err)
if prob := horizonclient.GetError(err); prob != nil {
if results, ok := prob.Problem.Extras["result_codes"].(map[string]interface{}); ok {
fmt.Println("RESULTS ARE HERE")
fmt.Println(results)
tt.Equal("tx_not_supported", results["transaction"])
} else {
tt.FailNow("result_codes couldn't be parsed: %+v", results)
Expand All @@ -78,7 +75,7 @@ func TestProtocol19Upgrade(t *testing.T) {

// Check that the account response has V3 fields omitted.
tt.EqualValues(0, account.SequenceLedger)
tt.Equal("0", account.SequenceTime)
tt.Equal("", account.SequenceTime)

itest.UpgradeProtocol(19)

Expand Down
Loading

0 comments on commit 453586e

Please sign in to comment.