From 05e8ae124912f39035f989bbba679f1fcd837d97 Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Mon, 25 Sep 2023 19:39:05 +0200 Subject: [PATCH] tapdb: fix time zone issue with timestamps 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. --- tapdb/assets_store.go | 4 ++-- tapdb/assets_store_test.go | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tapdb/assets_store.go b/tapdb/assets_store.go index 400377aff..47f7f9d10 100644 --- a/tapdb/assets_store.go +++ b/tapdb/assets_store.go @@ -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, @@ -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, diff --git a/tapdb/assets_store_test.go b/tapdb/assets_store_test.go index c4f60524a..44eb4008d 100644 --- a/tapdb/assets_store_test.go +++ b/tapdb/assets_store_test.go @@ -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], ) @@ -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], ) @@ -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], )