From 942b0dc1bbc932f4bf1e5b6c5ba811d492994b4c Mon Sep 17 00:00:00 2001 From: sputn1ck Date: Tue, 22 Aug 2023 20:08:37 +0200 Subject: [PATCH] loopdb: change faulty year migration logic This commit changes how the faulty year migration works, by just checking if the deadline is far in the future and then fixing it. --- loopdb/sql_test.go | 10 ++++++---- loopdb/sqlite.go | 20 +++++++++++++------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/loopdb/sql_test.go b/loopdb/sql_test.go index 1de9b32..831cafd 100644 --- a/loopdb/sql_test.go +++ b/loopdb/sql_test.go @@ -328,8 +328,10 @@ func TestIssue615(t *testing.T) { // Create a faulty loopout swap. destAddr := test.GetDestAddr(t, 0) - faultyTime, err := parseSqliteTimeStamp("55563-06-27 02:09:24 +0000 UTC") - require.NoError(t, err) + // Corresponds to 55563-06-27 02:09:24 +0000 UTC. + faultyTime := time.Unix(1691247002964, 0) + + t.Log(faultyTime.Unix()) unrestrictedSwap := LoopOutContract{ SwapContract: SwapContract{ @@ -362,7 +364,7 @@ func TestIssue615(t *testing.T) { SwapPublicationDeadline: faultyTime, } - err = sqlDB.CreateLoopOut(ctxb, testPreimage.Hash(), &unrestrictedSwap) + err := sqlDB.CreateLoopOut(ctxb, testPreimage.Hash(), &unrestrictedSwap) require.NoError(t, err) // This should fail because of the faulty timestamp. @@ -441,7 +443,7 @@ func TestTimeConversions(t *testing.T) { } for _, test := range tests { - time, err := parseTimeStamp(test.timeString) + time, err := fixTimeStamp(test.timeString) require.NoError(t, err) require.Equal(t, test.expectedTime, time) } diff --git a/loopdb/sqlite.go b/loopdb/sqlite.go index 90db0a1..87bf9fa 100644 --- a/loopdb/sqlite.go +++ b/loopdb/sqlite.go @@ -248,19 +248,25 @@ func (b *BaseDB) FixFaultyTimestamps(ctx context.Context) error { defer tx.Rollback() //nolint: errcheck for _, swap := range loopOutSwaps { - faultyTime, err := parseTimeStamp(swap.PublicationDeadline) + + // Get the year of the timestamp. + year, err := getTimeStampYear(swap.PublicationDeadline) if err != nil { return err } - // Skip if the time is not faulty. - if !isMilisecondsTime(faultyTime.Unix()) { + // Skip if the year is not in the future. + thisYear := time.Now().Year() + if year <= thisYear { continue } + fixedTime, err := fixTimeStamp(swap.PublicationDeadline) + if err != nil { + return err + } + // Update the faulty time to a valid time. - secs := faultyTime.Unix() / 1000 - correctTime := time.Unix(secs, 0) _, err = tx.ExecContext( ctx, ` UPDATE @@ -270,7 +276,7 @@ func (b *BaseDB) FixFaultyTimestamps(ctx context.Context) error { WHERE swap_hash = $2; `, - correctTime, swap.Hash, + fixedTime, swap.Hash, ) if err != nil { return err @@ -309,7 +315,7 @@ func (r *SqliteTxOptions) ReadOnly() bool { return r.readOnly } -// parseTimeStamp tries to parse a timestamp string with both the +// fixTimeStamp tries to parse a timestamp string with both the // parseSqliteTimeStamp and parsePostgresTimeStamp functions. // If both fail, it returns an error. func fixTimeStamp(dateTimeStr string) (time.Time, error) {