From 77a6b577ebd1b208ab61a993c340061cd78a6609 Mon Sep 17 00:00:00 2001 From: ziggie Date: Wed, 13 Aug 2025 09:14:51 +0200 Subject: [PATCH] channeldb: move helper function to codec.go This method is not only used by the payment logic so we need to move it to a generalized place because in the following commits we move payment related code into its own package. --- channeldb/codec.go | 34 ++++++++++++++++++++++++++++++++++ channeldb/mp_payment.go | 33 --------------------------------- 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/channeldb/codec.go b/channeldb/codec.go index 8c39f4d73..95434a5f6 100644 --- a/channeldb/codec.go +++ b/channeldb/codec.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "net" + "time" "github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/btcutil" @@ -466,3 +467,36 @@ func ReadElements(r io.Reader, elements ...interface{}) error { } return nil } + +// deserializeTime deserializes time as unix nanoseconds. +func deserializeTime(r io.Reader) (time.Time, error) { + var scratch [8]byte + if _, err := io.ReadFull(r, scratch[:]); err != nil { + return time.Time{}, err + } + + // Convert to time.Time. Interpret unix nano time zero as a zero + // time.Time value. + unixNano := byteOrder.Uint64(scratch[:]) + if unixNano == 0 { + return time.Time{}, nil + } + + return time.Unix(0, int64(unixNano)), nil +} + +// serializeTime serializes time as unix nanoseconds. +func serializeTime(w io.Writer, t time.Time) error { + var scratch [8]byte + + // Convert to unix nano seconds, but only if time is non-zero. Calling + // UnixNano() on a zero time yields an undefined result. + var unixNano int64 + if !t.IsZero() { + unixNano = t.UnixNano() + } + + byteOrder.PutUint64(scratch[:], uint64(unixNano)) + _, err := w.Write(scratch[:]) + return err +} diff --git a/channeldb/mp_payment.go b/channeldb/mp_payment.go index f75357afc..f4467b743 100644 --- a/channeldb/mp_payment.go +++ b/channeldb/mp_payment.go @@ -654,39 +654,6 @@ func deserializeHTLCFailInfo(r io.Reader) (*HTLCFailInfo, error) { return f, nil } -// deserializeTime deserializes time as unix nanoseconds. -func deserializeTime(r io.Reader) (time.Time, error) { - var scratch [8]byte - if _, err := io.ReadFull(r, scratch[:]); err != nil { - return time.Time{}, err - } - - // Convert to time.Time. Interpret unix nano time zero as a zero - // time.Time value. - unixNano := byteOrder.Uint64(scratch[:]) - if unixNano == 0 { - return time.Time{}, nil - } - - return time.Unix(0, int64(unixNano)), nil -} - -// serializeTime serializes time as unix nanoseconds. -func serializeTime(w io.Writer, t time.Time) error { - var scratch [8]byte - - // Convert to unix nano seconds, but only if time is non-zero. Calling - // UnixNano() on a zero time yields an undefined result. - var unixNano int64 - if !t.IsZero() { - unixNano = t.UnixNano() - } - - byteOrder.PutUint64(scratch[:], uint64(unixNano)) - _, err := w.Write(scratch[:]) - return err -} - // generateSphinxPacket generates then encodes a sphinx packet which encodes // the onion route specified by the passed layer 3 route. The blob returned // from this function can immediately be included within an HTLC add packet to