From 53eea09b639d03fdbed88d133b424310624800a8 Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Fri, 30 Aug 2019 13:10:29 +0200 Subject: [PATCH] channeldb: add now function Needed for time control in unit tests. --- channeldb/db.go | 2 ++ channeldb/invoice_test.go | 1 + channeldb/invoices.go | 12 +++++++----- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/channeldb/db.go b/channeldb/db.go index c53b7a231..fcb81aa8b 100644 --- a/channeldb/db.go +++ b/channeldb/db.go @@ -129,6 +129,7 @@ type DB struct { *bbolt.DB dbPath string graph *ChannelGraph + now func() time.Time } // Open opens an existing channeldb. Any necessary schemas migrations due to @@ -162,6 +163,7 @@ func Open(dbPath string, modifiers ...OptionModifier) (*DB, error) { chanDB := &DB{ DB: bdb, dbPath: dbPath, + now: time.Now, } chanDB.graph = newChannelGraph( chanDB, opts.RejectCacheSize, opts.ChannelCacheSize, diff --git a/channeldb/invoice_test.go b/channeldb/invoice_test.go index 0e5afd1e2..3ed1f8ebc 100644 --- a/channeldb/invoice_test.go +++ b/channeldb/invoice_test.go @@ -354,6 +354,7 @@ func TestDuplicateSettleInvoice(t *testing.T) { if err != nil { t.Fatalf("unable to make test db: %v", err) } + db.now = func() time.Time { return time.Unix(1, 0) } // We'll start out by creating an invoice and writing it to the DB. amt := lnwire.NewMSatFromSatoshis(1000) diff --git a/channeldb/invoices.go b/channeldb/invoices.go index ff4d60f63..df218af16 100644 --- a/channeldb/invoices.go +++ b/channeldb/invoices.go @@ -742,7 +742,7 @@ func (d *DB) UpdateInvoice(paymentHash lntypes.Hash, return ErrInvoiceNotFound } - updatedInvoice, err = updateInvoice( + updatedInvoice, err = d.updateInvoice( paymentHash, invoices, settleIndex, invoiceNum, callback, ) @@ -1176,7 +1176,7 @@ func copyInvoice(src *Invoice) *Invoice { // updateInvoice fetches the invoice, obtains the update descriptor from the // callback and applies the updates in a single db transaction. -func updateInvoice(hash lntypes.Hash, invoices, settleIndex *bbolt.Bucket, +func (d *DB) updateInvoice(hash lntypes.Hash, invoices, settleIndex *bbolt.Bucket, invoiceNum []byte, callback InvoiceUpdateCallback) (*Invoice, error) { invoice, err := fetchInvoice(invoiceNum, invoices) @@ -1210,7 +1210,9 @@ func updateInvoice(hash lntypes.Hash, invoices, settleIndex *bbolt.Bucket, } invoice.Terms.PaymentPreimage = update.Preimage - err := setSettleFields(settleIndex, invoiceNum, &invoice) + err := setSettleFields( + settleIndex, invoiceNum, &invoice, d.now(), + ) if err != nil { return nil, err } @@ -1229,7 +1231,7 @@ func updateInvoice(hash lntypes.Hash, invoices, settleIndex *bbolt.Bucket, } func setSettleFields(settleIndex *bbolt.Bucket, invoiceNum []byte, - invoice *Invoice) error { + invoice *Invoice, now time.Time) error { // Now that we know the invoice hasn't already been settled, we'll // update the settle index so we can place this settle event in the @@ -1246,7 +1248,7 @@ func setSettleFields(settleIndex *bbolt.Bucket, invoiceNum []byte, } invoice.Terms.State = ContractSettled - invoice.SettleDate = time.Now() + invoice.SettleDate = now invoice.SettleIndex = nextSettleSeqNo return nil