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.
pull/630/head
sputn1ck 9 months ago
parent 64bdef9238
commit 942b0dc1bb
No known key found for this signature in database
GPG Key ID: 671103D881A5F0E4

@ -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)
}

@ -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) {

Loading…
Cancel
Save