channeldb: add now function

Needed for time control in unit tests.
This commit is contained in:
Joost Jager 2019-08-30 13:10:29 +02:00
parent c8fa51f865
commit 53eea09b63
No known key found for this signature in database
GPG Key ID: A61B9D4C393C59C7
3 changed files with 10 additions and 5 deletions

@ -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,

@ -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)

@ -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