Skip to content

Commit

Permalink
tapdb: fix time zone issue with timestamps
Browse files Browse the repository at this point in the history
Because sqlc by default just uses the time.Time.String() method to turn
a timestamp into the DB value, the time zone of the timestamp has an
impact as any SQL query would basically be a string comparison that
doesn't look at the time zone at all.
For example:

	WHERE a < b

Would be true for

	a = '2023-09-26 14:00:00 GMT-8' (22:00 local time)
	a = '2023-09-26 15:00:00 UTC'   (15:00 local time)

Even though 10pm is clearly after 3pm.

To fix this, we explicitly call .UTC() on any time.Time we use in any
SQL query.

The only instances where that was not yet the case in the whole tapdb
package were the newly added queries for UTXO leases.

It seems that with a later release of the sqlite driver we could
customize this behavior of how a timestamp is being formatted. But for
now the .UTC() workaround should work as well.
  • Loading branch information
guggero committed Sep 26, 2023
1 parent 0b38323 commit 05e8ae1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions tapdb/assets_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -1682,7 +1682,7 @@ func (a *AssetStore) LeaseCoins(ctx context.Context, leaseOwner [32]byte,
err = q.UpdateUTXOLease(ctx, UpdateUTXOLease{
LeaseOwner: leaseOwner[:],
LeaseExpiry: sql.NullTime{
Time: expiry,
Time: expiry.UTC(),
Valid: true,
},
Outpoint: outpoint,
Expand Down Expand Up @@ -1991,7 +1991,7 @@ func insertAssetTransferInput(ctx context.Context, q ActiveAssetsStore,
return q.UpdateUTXOLease(ctx, UpdateUTXOLease{
LeaseOwner: finalLeaseOwner[:],
LeaseExpiry: sql.NullTime{
Time: finalLeaseExpiry,
Time: finalLeaseExpiry.UTC(),
Valid: true,
},
Outpoint: anchorPointBytes,
Expand Down
6 changes: 3 additions & 3 deletions tapdb/assets_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ func TestUTXOLeases(t *testing.T) {
// Now we lease the first asset for 1 hour. This will cause the second
// one also to be leased, since it's on the same anchor transaction.
leaseOwner := fn.ToArray[[32]byte](test.RandBytes(32))
leaseExpiry := time.Now().Add(time.Hour).UTC()
leaseExpiry := time.Now().Add(time.Hour)
err = assetsStore.LeaseCoins(
ctx, leaseOwner, leaseExpiry, assetGen.anchorPoints[0],
)
Expand All @@ -825,7 +825,7 @@ func TestUTXOLeases(t *testing.T) {
require.Len(t, selectedAssets, 1)

// Let's now update the lease.
leaseExpiry = time.Now().Add(2 * time.Hour).UTC()
leaseExpiry = time.Now().Add(2 * time.Hour)
err = assetsStore.LeaseCoins(
ctx, leaseOwner, leaseExpiry, assetGen.anchorPoints[0],
)
Expand Down Expand Up @@ -853,7 +853,7 @@ func TestUTXOLeases(t *testing.T) {

// Update the lease again, but into the past, so that it should be
// removed upon cleanup.
leaseExpiry = time.Now().Add(-time.Hour).UTC()
leaseExpiry = time.Now().Add(-time.Hour)
err = assetsStore.LeaseCoins(
ctx, leaseOwner, leaseExpiry, assetGen.anchorPoints[0],
)
Expand Down

0 comments on commit 05e8ae1

Please sign in to comment.