Skip to content

Commit

Permalink
services/horizon: Change min_account_sequence_age column from `bigi…
Browse files Browse the repository at this point in the history
…nt` to string (#4339)

* `minSeqAge` can be unsigned in the XDR
* Update codebase to conform to new data type: null.Int -> null.String
* Fix time-based tests to be tz-agnostic + work w/ uint64
  • Loading branch information
Shaptic authored Apr 15, 2022
1 parent 55ab073 commit 4dd082b
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ type TransactionWithoutLedger struct {
TimeBounds TimeBounds `db:"time_bounds"`
LedgerBounds LedgerBounds `db:"ledger_bounds"`
MinAccountSequence null.Int `db:"min_account_sequence"`
MinAccountSequenceAge null.Int `db:"min_account_sequence_age"`
MinAccountSequenceAge null.String `db:"min_account_sequence_age"`
MinAccountSequenceLedgerGap null.Int `db:"min_account_sequence_ledger_gap"`
ExtraSigners pq.StringArray `db:"extra_signers"`
CreatedAt time.Time `db:"created_at"`
Expand Down Expand Up @@ -234,11 +234,11 @@ func formatMinSequenceNumber(minSeqNum *int64) null.Int {
return null.IntFrom(int64(*minSeqNum))
}

func formatDuration(d *xdr.Duration) null.Int {
func formatDuration(d *xdr.Duration) null.String {
if d == nil {
return null.Int{}
return null.String{}
}
return null.IntFrom(int64(*d))
return null.StringFrom(fmt.Sprint(uint64(*d)))
}

func formatUint32(u *xdr.Uint32) null.Int {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package history

import (
"fmt"
"math"
"testing"

"github.com/guregu/null"
Expand Down Expand Up @@ -188,7 +190,7 @@ func TestTransactionToMap_Preconditions(t *testing.T) {
Type: xdr.CryptoKeyTypeKeyTypeEd25519,
Ed25519: &xdr.Uint256{3, 2, 1},
}
minSeqNum := xdr.SequenceNumber(24)
minSeqNum := xdr.SequenceNumber(math.MaxInt64)
signerKey := xdr.SignerKey{
Type: xdr.SignerKeyTypeSignerKeyTypeEd25519,
Ed25519: source.Ed25519,
Expand Down Expand Up @@ -226,7 +228,7 @@ func TestTransactionToMap_Preconditions(t *testing.T) {
MaxLedger: 10,
},
MinSeqNum: &minSeqNum,
MinSeqAge: xdr.Duration(1024),
MinSeqAge: xdr.Duration(math.MaxUint64),
MinSeqLedgerGap: xdr.Uint32(3),
ExtraSigners: []xdr.SignerKey{signerKey},
},
Expand Down Expand Up @@ -272,8 +274,8 @@ func TestTransactionToMap_Preconditions(t *testing.T) {
assert.Equal(t, null.IntFrom(5), row.LedgerBounds.MinLedger)
assert.Equal(t, null.IntFrom(10), row.LedgerBounds.MaxLedger)

assert.Equal(t, null.IntFrom(24), row.MinAccountSequence)
assert.Equal(t, null.IntFrom(1024), row.MinAccountSequenceAge)
assert.Equal(t, null.IntFrom(int64(minSeqNum)), row.MinAccountSequence)
assert.Equal(t, null.StringFrom(fmt.Sprint(uint64(math.MaxUint64))), row.MinAccountSequenceAge)
assert.Equal(t, null.IntFrom(3), row.MinAccountSequenceLedgerGap)
assert.Equal(t, pq.StringArray{signerKey.Address()}, row.ExtraSigners)
}
2 changes: 1 addition & 1 deletion services/horizon/internal/db2/history/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ func TestInsertTransaction(t *testing.T) {
TimeBounds: v2TimeboundsWithMinAndMax,
LedgerBounds: v2LedgerboundsWithMinAndMax,
MinAccountSequence: null.Int{},
MinAccountSequenceAge: null.IntFrom(10),
MinAccountSequenceAge: null.StringFrom("10"),
MinAccountSequenceLedgerGap: null.IntFrom(2),
ExtraSigners: pq.StringArray{},
Successful: success,
Expand Down
6 changes: 3 additions & 3 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
@@ -1,8 +1,8 @@
-- +migrate Up
ALTER TABLE history_transactions ADD ledger_bounds int8range;
ALTER TABLE history_transactions ADD min_account_sequence bigint;
ALTER TABLE history_transactions ADD min_account_sequence_age bigint;
ALTER TABLE history_transactions ADD min_account_sequence_ledger_gap bigint;
ALTER TABLE history_transactions ADD ledger_bounds int8range; -- xdr.Uint32s
ALTER TABLE history_transactions ADD min_account_sequence bigint; -- xdr.SequenceNumber -> int64
ALTER TABLE history_transactions ADD min_account_sequence_age varchar(20); -- xdr.TimePoint -> uint64 -> longest uint64 number
ALTER TABLE history_transactions ADD min_account_sequence_ledger_gap bigint; -- xdr.Int32
ALTER TABLE history_transactions ADD extra_signers text[];

ALTER TABLE accounts ADD sequence_ledger integer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package integration

import (
"fmt"
"strconv"
"sync"
"testing"
Expand Down Expand Up @@ -218,9 +219,11 @@ func TestTransactionPreconditionsMinSequenceNumberAge(t *testing.T) {
tt.Len(ledgers.Embedded.Records, 1)

// gather up the current sequence times
acctSeqTime, err := strconv.ParseInt(latestMasterAccount.SequenceTime, 10, 64)
signedAcctSeqTime, err := strconv.ParseInt(latestMasterAccount.SequenceTime, 10, 64)
tt.NoError(err)
networkSeqTime := ledgers.Embedded.Records[0].ClosedAt.UTC().Unix()
tt.GreaterOrEqual(signedAcctSeqTime, int64(0))
acctSeqTime := uint64(signedAcctSeqTime)
networkSeqTime := uint64(ledgers.Embedded.Records[0].ClosedAt.UTC().Unix())

// build a tx with seqnum based on master.seqNum+1 as source account
txParams := buildTXParams(master, masterAccount, currentAccountSeq+1)
Expand All @@ -241,7 +244,8 @@ func TestTransactionPreconditionsMinSequenceNumberAge(t *testing.T) {
//verify roundtrip to network and back through the horizon api returns same precondition values
txHistory, err := itest.Client().TransactionDetail(tx.Hash)
assert.NoError(t, err)
assert.Equal(t, txHistory.Preconditions.MinAccountSequenceAge, strconv.FormatInt(txParams.Preconditions.MinSequenceNumberAge, 10))
assert.EqualValues(t, txHistory.Preconditions.MinAccountSequenceAge,
fmt.Sprint(uint64(txParams.Preconditions.MinSequenceNumberAge)))
}

func TestTransactionPreconditionsMinSequenceNumberLedgerGap(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion services/horizon/internal/resourceadapter/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func PopulateTransaction(
}

if row.MinAccountSequenceAge.Valid {
dest.Preconditions.MinAccountSequenceAge = fmt.Sprint(row.MinAccountSequenceAge.Int64)
dest.Preconditions.MinAccountSequenceAge = row.MinAccountSequenceAge.String
}

if row.MinAccountSequenceLedgerGap.Valid {
Expand Down
10 changes: 5 additions & 5 deletions services/horizon/internal/resourceadapter/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@ func TestPopulateTransaction_Preconditions(t *testing.T) {
row history.Transaction
)

validAfter := time.Now().Add(-1 * time.Hour)
validBefore := time.Now().Add(1 * time.Hour)
validAfter := time.Now().UTC().Add(-1 * time.Hour)
validBefore := time.Now().UTC().Add(1 * time.Hour)
minLedger := uint32(40071006 - 1024)
maxLedger := uint32(40071006 + 1024)
minAccountSequence := int64(10)
minSequenceAge := 30 * time.Second * 1000
minSequenceAge := uint64(30 * 1000)
minSequenceLedgerGap := uint32(5)

dest = Transaction{}
Expand All @@ -184,7 +184,7 @@ func TestPopulateTransaction_Preconditions(t *testing.T) {
MaxLedger: null.IntFrom(int64(maxLedger)),
},
MinAccountSequence: null.IntFrom(minAccountSequence),
MinAccountSequenceAge: null.IntFrom(int64(minSequenceAge)),
MinAccountSequenceAge: null.StringFrom(fmt.Sprint(minSequenceAge)),
MinAccountSequenceLedgerGap: null.IntFrom(int64(minSequenceLedgerGap)),
ExtraSigners: pq.StringArray{"D34DB33F", "8BADF00D"},
},
Expand All @@ -199,7 +199,7 @@ func TestPopulateTransaction_Preconditions(t *testing.T) {
assert.Equal(t, minLedger, p.Ledgerbounds.MinLedger)
assert.Equal(t, maxLedger, p.Ledgerbounds.MaxLedger)
assert.Equal(t, fmt.Sprint(minAccountSequence), p.MinAccountSequence)
assert.Equal(t, fmt.Sprint(int64(minSequenceAge)), p.MinAccountSequenceAge)
assert.Equal(t, fmt.Sprint(uint64(minSequenceAge)), p.MinAccountSequenceAge)
assert.Equal(t, minSequenceLedgerGap, p.MinAccountSequenceLedgerGap)
assert.Equal(t, []string{"D34DB33F", "8BADF00D"}, p.ExtraSigners)
}
Expand Down
4 changes: 2 additions & 2 deletions txnbuild/preconditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Preconditions struct {
// Transaction is valid if the current ledger time is at least
// minSequenceNumberAge greater than the source account's seqTime (units are
// seconds).
MinSequenceNumberAge int64
MinSequenceNumberAge uint64
// Transaction is valid if the current ledger number is at least
// minSequenceNumberLedgerGap greater than the source account's seqLedger.
MinSequenceNumberLedgerGap uint32
Expand Down Expand Up @@ -135,7 +135,7 @@ func (cond *Preconditions) FromXDR(precondXdr xdr.Preconditions) error {
cond.MinSequenceNumber = &minSeqNum
}

cond.MinSequenceNumberAge = int64(inner.MinSeqAge)
cond.MinSequenceNumberAge = uint64(inner.MinSeqAge)
cond.MinSequenceNumberLedgerGap = uint32(inner.MinSeqLedgerGap)
if len(inner.ExtraSigners) > 0 {
cond.ExtraSigners = make([]string, len(inner.ExtraSigners))
Expand Down

0 comments on commit 4dd082b

Please sign in to comment.