Skip to content

Commit be9ca71

Browse files
committed
loopd: convert unix millisecond timestamps to seconds
1 parent f25b5e9 commit be9ca71

File tree

3 files changed

+60
-26
lines changed

3 files changed

+60
-26
lines changed

loopd/swapclient_server.go

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -156,21 +156,22 @@ func (s *swapClientServer) LoopOut(ctx context.Context,
156156
return nil, err
157157
}
158158

159+
// Infer if the publication deadline is set in milliseconds.
160+
publicationDeadline := getPublicationDeadline(in.SwapPublicationDeadline)
161+
159162
req := &loop.OutRequest{
160-
Amount: btcutil.Amount(in.Amt),
161-
DestAddr: sweepAddr,
162-
MaxMinerFee: btcutil.Amount(in.MaxMinerFee),
163-
MaxPrepayAmount: btcutil.Amount(in.MaxPrepayAmt),
164-
MaxPrepayRoutingFee: btcutil.Amount(in.MaxPrepayRoutingFee),
165-
MaxSwapRoutingFee: btcutil.Amount(in.MaxSwapRoutingFee),
166-
MaxSwapFee: btcutil.Amount(in.MaxSwapFee),
167-
SweepConfTarget: sweepConfTarget,
168-
HtlcConfirmations: in.HtlcConfirmations,
169-
SwapPublicationDeadline: time.Unix(
170-
int64(in.SwapPublicationDeadline), 0,
171-
),
172-
Label: in.Label,
173-
Initiator: in.Initiator,
163+
Amount: btcutil.Amount(in.Amt),
164+
DestAddr: sweepAddr,
165+
MaxMinerFee: btcutil.Amount(in.MaxMinerFee),
166+
MaxPrepayAmount: btcutil.Amount(in.MaxPrepayAmt),
167+
MaxPrepayRoutingFee: btcutil.Amount(in.MaxPrepayRoutingFee),
168+
MaxSwapRoutingFee: btcutil.Amount(in.MaxSwapRoutingFee),
169+
MaxSwapFee: btcutil.Amount(in.MaxSwapFee),
170+
SweepConfTarget: sweepConfTarget,
171+
HtlcConfirmations: in.HtlcConfirmations,
172+
SwapPublicationDeadline: publicationDeadline,
173+
Label: in.Label,
174+
Initiator: in.Initiator,
174175
}
175176

176177
switch {
@@ -538,13 +539,14 @@ func (s *swapClientServer) LoopOutQuote(ctx context.Context,
538539
if err != nil {
539540
return nil, err
540541
}
542+
543+
publicactionDeadline := getPublicationDeadline(req.SwapPublicationDeadline)
544+
541545
quote, err := s.impl.LoopOutQuote(ctx, &loop.LoopOutQuoteRequest{
542-
Amount: btcutil.Amount(req.Amt),
543-
SweepConfTarget: confTarget,
544-
SwapPublicationDeadline: time.Unix(
545-
int64(req.SwapPublicationDeadline), 0,
546-
),
547-
Initiator: defaultLoopdInitiator,
546+
Amount: btcutil.Amount(req.Amt),
547+
SweepConfTarget: confTarget,
548+
SwapPublicationDeadline: publicactionDeadline,
549+
Initiator: defaultLoopdInitiator,
548550
})
549551
if err != nil {
550552
return nil, err
@@ -1249,3 +1251,19 @@ func hasBandwidth(channels []lndclient.ChannelInfo, amt btcutil.Amount,
12491251

12501252
return false, 0
12511253
}
1254+
1255+
// getPublicationDeadline returns the publication deadline for a swap given the
1256+
// unix timestamp. If the timestamp is believed to be in milliseconds, then it
1257+
// is converted to seconds.
1258+
func getPublicationDeadline(unixTimestamp uint64) time.Time {
1259+
length := len(fmt.Sprintf("%d", unixTimestamp))
1260+
if length >= 13 {
1261+
// Likely a millisecond timestamp
1262+
secs := unixTimestamp / 1000
1263+
nsecs := (unixTimestamp % 1000) * 1e6
1264+
return time.Unix(int64(secs), int64(nsecs))
1265+
} else {
1266+
// Likely a second timestamp
1267+
return time.Unix(int64(unixTimestamp), 0)
1268+
}
1269+
}

loopdb/sql_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,11 @@ func TestIssue615(t *testing.T) {
346346
Index: 2,
347347
},
348348
},
349-
MaxMinerFee: 10,
350-
MaxSwapFee: 20,
351-
349+
MaxMinerFee: 10,
350+
MaxSwapFee: 20,
352351
InitiationHeight: 99,
353-
354-
InitiationTime: time.Now(),
355-
ProtocolVersion: ProtocolVersionMuSig2,
352+
InitiationTime: time.Now(),
353+
ProtocolVersion: ProtocolVersionMuSig2,
356354
},
357355
MaxPrepayRoutingFee: 40,
358356
PrepayInvoice: "prepayinvoice",

loopdb/sqlite.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,11 @@ func (r *SqliteTxOptions) ReadOnly() bool {
316316
func parseSqliteTimeStamp(dateTimeStr string) (time.Time, error) {
317317
// Split the date and time parts.
318318
parts := strings.Fields(strings.TrimSpace(dateTimeStr))
319+
if len(parts) <= 2 {
320+
return time.Time{}, fmt.Errorf("invalid timestamp format: %v",
321+
dateTimeStr)
322+
}
323+
319324
datePart, timePart := parts[0], parts[1]
320325

321326
return parseTimeParts(datePart, timePart)
@@ -328,6 +333,11 @@ func parseSqliteTimeStamp(dateTimeStr string) (time.Time, error) {
328333
func parsePostgresTimeStamp(dateTimeStr string) (time.Time, error) {
329334
// Split the date and time parts.
330335
parts := strings.Split(dateTimeStr, "T")
336+
if len(parts) != 2 {
337+
return time.Time{}, fmt.Errorf("invalid timestamp format: %v",
338+
dateTimeStr)
339+
}
340+
331341
datePart, timePart := parts[0], strings.TrimSuffix(parts[1], "Z")
332342

333343
return parseTimeParts(datePart, timePart)
@@ -338,6 +348,10 @@ func parsePostgresTimeStamp(dateTimeStr string) (time.Time, error) {
338348
func parseTimeParts(datePart, timePart string) (time.Time, error) {
339349
// Parse the date.
340350
dateParts := strings.Split(datePart, "-")
351+
if len(dateParts) != 3 {
352+
return time.Time{}, fmt.Errorf("invalid date format: %v",
353+
datePart)
354+
}
341355

342356
year, err := strconv.Atoi(dateParts[0])
343357
if err != nil {
@@ -356,6 +370,10 @@ func parseTimeParts(datePart, timePart string) (time.Time, error) {
356370

357371
// Parse the time.
358372
timeParts := strings.Split(timePart, ":")
373+
if len(timeParts) != 3 {
374+
return time.Time{}, fmt.Errorf("invalid time format: %v",
375+
timePart)
376+
}
359377

360378
hour, err := strconv.Atoi(timeParts[0])
361379
if err != nil {

0 commit comments

Comments
 (0)