From 7c0d1e0093611ec93fa41c229f46bc96adb29551 Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Wed, 3 Mar 2021 09:56:57 -0800 Subject: [PATCH] channeldb/invoice: map zero-value timestamps to 0 Mainly affects ResolveTime which can be 0 before its settled. --- channeldb/invoices.go | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/channeldb/invoices.go b/channeldb/invoices.go index 9f867ea8..48f01b83 100644 --- a/channeldb/invoices.go +++ b/channeldb/invoices.go @@ -1304,8 +1304,8 @@ func serializeHtlcs(w io.Writer, htlcs map[CircuitKey]*InvoiceHTLC) error { chanID := key.ChanID.ToUint64() amt := uint64(htlc.Amt) mppTotalAmt := uint64(htlc.MppTotalAmt) - acceptTime := uint64(htlc.AcceptTime.UnixNano()) - resolveTime := uint64(htlc.ResolveTime.UnixNano()) + acceptTime := putNanoTime(htlc.AcceptTime) + resolveTime := putNanoTime(htlc.ResolveTime) state := uint8(htlc.State) var records []tlv.Record @@ -1379,6 +1379,25 @@ func serializeHtlcs(w io.Writer, htlcs map[CircuitKey]*InvoiceHTLC) error { return nil } +// putNanoTime returns the unix nano time for the passed timestamp. A zero-value +// timestamp will be mapped to 0, since calling UnixNano in that case is +// undefined. +func putNanoTime(t time.Time) uint64 { + if t.IsZero() { + return 0 + } + return uint64(t.UnixNano()) +} + +// getNanoTime returns a timestamp for the given number of nano seconds. If zero +// is provided, an zero-value time stamp is returned. +func getNanoTime(ns uint64) time.Time { + if ns == 0 { + return time.Time{} + } + return time.Unix(0, int64(ns)) +} + func fetchInvoice(invoiceNum []byte, invoices kvdb.RBucket) (Invoice, error) { invoiceBytes := invoices.Get(invoiceNum) if invoiceBytes == nil { @@ -1564,8 +1583,8 @@ func deserializeHtlcs(r io.Reader) (map[CircuitKey]*InvoiceHTLC, error) { } key.ChanID = lnwire.NewShortChanIDFromInt(chanID) - htlc.AcceptTime = time.Unix(0, int64(acceptTime)) - htlc.ResolveTime = time.Unix(0, int64(resolveTime)) + htlc.AcceptTime = getNanoTime(acceptTime) + htlc.ResolveTime = getNanoTime(resolveTime) htlc.State = HtlcState(state) htlc.Amt = lnwire.MilliSatoshi(amt) htlc.MppTotalAmt = lnwire.MilliSatoshi(mppTotalAmt)