Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions channeldb/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"net"
"time"

"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil"
Expand Down Expand Up @@ -182,11 +183,6 @@ func WriteElement(w io.Writer, element interface{}) error {
return err
}

case paymentIndexType:
if err := binary.Write(w, byteOrder, e); err != nil {
return err
}

case lnwire.FundingFlag:
if err := binary.Write(w, byteOrder, e); err != nil {
return err
Expand Down Expand Up @@ -415,11 +411,6 @@ func ReadElement(r io.Reader, element interface{}) error {
return err
}

case *paymentIndexType:
if err := binary.Read(r, byteOrder, e); err != nil {
return err
}

case *lnwire.FundingFlag:
if err := binary.Read(r, byteOrder, e); err != nil {
return err
Expand Down Expand Up @@ -466,3 +457,37 @@ 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
}
Comment on lines +461 to +493

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The functions deserializeTime and serializeTime are added here, but they also exist in the new payments/db/codec.go file. This appears to be code duplication introduced during the refactoring. Since these functions are related to payment serialization and are used within the new payments package, they should likely only reside in payments/db/codec.go. Please remove the duplicated functions from this file.

13 changes: 7 additions & 6 deletions channeldb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,13 @@ var (
migration: mig.CreateTLB(payAddrIndexBucket),
},
{
// Initialize payment index bucket which will be used
// to index payments by sequence number. This index will
// be used to allow more efficient ListPayments queries.
number: 15,
migration: mig.CreateTLB(paymentsIndexBucket),
// This used to be create payment related top-level
// buckets, however this is now done by the payment
// package.
number: 15,
migration: func(tx kvdb.RwTx) error {
return nil
},
},
{
// Add our existing payments to the index bucket created
Expand Down Expand Up @@ -450,7 +452,6 @@ var dbTopLevelBuckets = [][]byte{
invoiceBucket,
payAddrIndexBucket,
setIDIndexBucket,
paymentsIndexBucket,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it mean we no longer init the payments db in initChannelDB or remove it in Wipe?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exactly we have a separate init method now in the kv store => initKVStore

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And the wipe method was only used in tests, which did not affect the payment related data and I don't think we currently need a wipe test on the paymentsdb package side, because the test seems not really necessary imo.

peersBucket,
nodeInfoBucket,
metaBucket,
Expand Down
4 changes: 2 additions & 2 deletions channeldb/invoices.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ func (d *DB) QueryInvoices(_ context.Context, q invpkg.InvoiceQuery) (

// Create a paginator which reads from our add index bucket with
// the parameters provided by the invoice query.
paginator := newPaginator(
paginator := NewPaginator(
invoiceAddIndex.ReadCursor(), q.Reversed, q.IndexOffset,
q.NumMaxInvoices,
)
Expand Down Expand Up @@ -603,7 +603,7 @@ func (d *DB) QueryInvoices(_ context.Context, q invpkg.InvoiceQuery) (

// Query our paginator using accumulateInvoices to build up a
// set of invoices.
if err := paginator.query(accumulateInvoices); err != nil {
if err := paginator.Query(accumulateInvoices); err != nil {
return err
}

Expand Down
8 changes: 4 additions & 4 deletions channeldb/paginate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ type paginator struct {
totalItems uint64
}

// newPaginator returns a struct which can be used to query an indexed bucket
// NewPaginator returns a struct which can be used to query an indexed bucket
// in pages.
func newPaginator(c kvdb.RCursor, reversed bool,
func NewPaginator(c kvdb.RCursor, reversed bool,
indexOffset, totalItems uint64) paginator {

return paginator{
Expand Down Expand Up @@ -105,14 +105,14 @@ func (p paginator) cursorStart() ([]byte, []byte) {
return indexKey, indexValue
}

// query gets the start point for our index offset and iterates through keys
// Query gets the start point for our index offset and iterates through keys
// in our index until we reach the total number of items required for the query
// or we run out of cursor values. This function takes a fetchAndAppend function
// which is responsible for looking up the entry at that index, adding the entry
// to its set of return items (if desired) and return a boolean which indicates
// whether the item was added. This is required to allow the paginator to
// determine when the response has the maximum number of required items.
func (p paginator) query(fetchAndAppend func(k, v []byte) (bool, error)) error {
func (p paginator) Query(fetchAndAppend func(k, v []byte) (bool, error)) error {
indexKey, indexValue := p.cursorStart()

var totalItems int
Expand Down
162 changes: 0 additions & 162 deletions channeldb/payments.go

This file was deleted.

Loading
Loading