Skip to content

Commit 942b0dc

Browse files
committed
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.
1 parent 64bdef9 commit 942b0dc

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

loopdb/sql_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,10 @@ func TestIssue615(t *testing.T) {
328328

329329
// Create a faulty loopout swap.
330330
destAddr := test.GetDestAddr(t, 0)
331-
faultyTime, err := parseSqliteTimeStamp("55563-06-27 02:09:24 +0000 UTC")
332-
require.NoError(t, err)
331+
// Corresponds to 55563-06-27 02:09:24 +0000 UTC.
332+
faultyTime := time.Unix(1691247002964, 0)
333+
334+
t.Log(faultyTime.Unix())
333335

334336
unrestrictedSwap := LoopOutContract{
335337
SwapContract: SwapContract{
@@ -362,7 +364,7 @@ func TestIssue615(t *testing.T) {
362364
SwapPublicationDeadline: faultyTime,
363365
}
364366

365-
err = sqlDB.CreateLoopOut(ctxb, testPreimage.Hash(), &unrestrictedSwap)
367+
err := sqlDB.CreateLoopOut(ctxb, testPreimage.Hash(), &unrestrictedSwap)
366368
require.NoError(t, err)
367369

368370
// This should fail because of the faulty timestamp.
@@ -441,7 +443,7 @@ func TestTimeConversions(t *testing.T) {
441443
}
442444

443445
for _, test := range tests {
444-
time, err := parseTimeStamp(test.timeString)
446+
time, err := fixTimeStamp(test.timeString)
445447
require.NoError(t, err)
446448
require.Equal(t, test.expectedTime, time)
447449
}

loopdb/sqlite.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -248,19 +248,25 @@ func (b *BaseDB) FixFaultyTimestamps(ctx context.Context) error {
248248
defer tx.Rollback() //nolint: errcheck
249249

250250
for _, swap := range loopOutSwaps {
251-
faultyTime, err := parseTimeStamp(swap.PublicationDeadline)
251+
252+
// Get the year of the timestamp.
253+
year, err := getTimeStampYear(swap.PublicationDeadline)
252254
if err != nil {
253255
return err
254256
}
255257

256-
// Skip if the time is not faulty.
257-
if !isMilisecondsTime(faultyTime.Unix()) {
258+
// Skip if the year is not in the future.
259+
thisYear := time.Now().Year()
260+
if year <= thisYear {
258261
continue
259262
}
260263

264+
fixedTime, err := fixTimeStamp(swap.PublicationDeadline)
265+
if err != nil {
266+
return err
267+
}
268+
261269
// Update the faulty time to a valid time.
262-
secs := faultyTime.Unix() / 1000
263-
correctTime := time.Unix(secs, 0)
264270
_, err = tx.ExecContext(
265271
ctx, `
266272
UPDATE
@@ -270,7 +276,7 @@ func (b *BaseDB) FixFaultyTimestamps(ctx context.Context) error {
270276
WHERE
271277
swap_hash = $2;
272278
`,
273-
correctTime, swap.Hash,
279+
fixedTime, swap.Hash,
274280
)
275281
if err != nil {
276282
return err
@@ -309,7 +315,7 @@ func (r *SqliteTxOptions) ReadOnly() bool {
309315
return r.readOnly
310316
}
311317

312-
// parseTimeStamp tries to parse a timestamp string with both the
318+
// fixTimeStamp tries to parse a timestamp string with both the
313319
// parseSqliteTimeStamp and parsePostgresTimeStamp functions.
314320
// If both fail, it returns an error.
315321
func fixTimeStamp(dateTimeStr string) (time.Time, error) {

0 commit comments

Comments
 (0)