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

api: Convert timezones before txn query #1543

Merged
merged 3 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 4 additions & 2 deletions idb/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,13 +564,15 @@ func buildTransactionQuery(tf idb.TransactionFilter) (query string, whereArgs []
partNumber++
}
if !tf.BeforeTime.IsZero() {
convertedTime := tf.BeforeTime.In(time.UTC)
whereParts = append(whereParts, fmt.Sprintf("h.realtime < $%d", partNumber))
whereArgs = append(whereArgs, tf.BeforeTime)
whereArgs = append(whereArgs, convertedTime)
partNumber++
}
if !tf.AfterTime.IsZero() {
convertedTime := tf.AfterTime.In(time.UTC)
whereParts = append(whereParts, fmt.Sprintf("h.realtime > $%d", partNumber))
whereArgs = append(whereArgs, tf.AfterTime)
whereArgs = append(whereArgs, convertedTime)
partNumber++
}
if tf.AssetID != 0 || tf.ApplicationID != 0 {
Expand Down
52 changes: 52 additions & 0 deletions idb/postgres/postgres_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,58 @@ func TestMultipleWriters(t *testing.T) {
assert.Equal(t, amt, balance)
}

// TestTransactionsTimestamps tests that the transactions endpoint responds to times properly.
func TestTransactionsTimestamps(t *testing.T) {
db, shutdownFunc := setupIdb(t, test.MakeGenesis())
defer shutdownFunc()

round := uint64(1)
///////////
// Given // A block at with 8 transactions at ts 1671036853.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo.

///////////
usEastTz, err := time.LoadLocation("America/New_York")
require.NoError(t, err)
usWestTz, err := time.LoadLocation("America/Los_Angeles")
require.NoError(t, err)
vb, err := test.ReadValidatedBlockFromFile("test_resources/validated_blocks/BlockWithTransactions.vb")
require.NoError(t, err)
err = db.AddBlock(&vb)
require.NoError(t, err)

//////////
// When // We call Transactions with timestamp filters
//////////
blkTime := vb.Block.BlockHeader.TimeStamp
fullRowsCh, _ := db.Transactions(
context.Background(),
idb.TransactionFilter{
Round: &round,
BeforeTime: time.Unix(blkTime+1, 0).In(usEastTz),
AfterTime: time.Unix(blkTime-1, 0)})
var txnRows0 []idb.TxnRow
for row := range fullRowsCh {
require.NoError(t, row.Error)
txnRows0 = append(txnRows0, row)
}

emptyRowsCh, _ := db.Transactions(
context.Background(),
idb.TransactionFilter{
Round: &round,
AfterTime: time.Unix(blkTime, 0).In(usWestTz)})
txnRows1 := make([]idb.TxnRow, 0)
for row := range emptyRowsCh {
require.NoError(t, row.Error)
txnRows1 = append(txnRows1, row)
}

//////////
// Then // They should have the correct number of transactions
//////////
assert.Len(t, txnRows0, len(vb.Block.Payset))
assert.Len(t, txnRows1, 0)
}

// TestBlockWithTransactions tests that the block with transactions endpoint works.
func TestBlockWithTransactions(t *testing.T) {
db, shutdownFunc := setupIdb(t, test.MakeGenesis())
Expand Down
45 changes: 45 additions & 0 deletions idb/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,48 @@ func Test_UnknownProtocol(t *testing.T) {
require.ErrorContains(t, err, protocol)
require.ErrorContains(t, err, "you need to upgrade")
}

func Test_buildTransactionQueryTime(t *testing.T) {
us_east_tz, err := time.LoadLocation("America/New_York")
require.NoError(t, err)
us_west_tz, err := time.LoadLocation("America/Los_Angeles")
require.NoError(t, err)
random_date_utc := time.Date(1000, time.December, 25, 1, 2, 3, 4, time.UTC)
tests := []struct {
name string
arg idb.TransactionFilter
whereArgs []interface{}
}{
{
"BeforeTime UTC to UTC",
idb.TransactionFilter{
BeforeTime: random_date_utc,
},
[]interface{}{random_date_utc},
},
{
"AfterTime UTC to UTC",
idb.TransactionFilter{
AfterTime: random_date_utc,
},
[]interface{}{random_date_utc},
},
{
"BeforeTime AfterTime Conversion",
idb.TransactionFilter{
BeforeTime: time.Date(1000, time.December, 25, 1, 2, 3, 4, us_east_tz),
AfterTime: time.Date(1000, time.December, 25, 1, 2, 3, 4, us_west_tz),
},
[]interface{}{
time.Date(1000, time.December, 25, 1, 2, 3, 4, us_east_tz).UTC(),
time.Date(1000, time.December, 25, 1, 2, 3, 4, us_west_tz).UTC(),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, whereArgs, _ := buildTransactionQuery(tt.arg)
require.Equal(t, whereArgs, tt.whereArgs)
})
}
}