invoices: refactor - rename invoiceExpiry to invoiceExpiryTs

We're going to add block based expiry, so we clarify now.
This commit is contained in:
carla 2021-04-23 08:19:52 +02:00
parent 2e39edd6bd
commit f5f1e9e6c7
No known key found for this signature in database
GPG Key ID: 4CA7FE54A6213C91
3 changed files with 15 additions and 15 deletions

@ -12,9 +12,9 @@ import (
"github.com/lightningnetwork/lnd/zpay32" "github.com/lightningnetwork/lnd/zpay32"
) )
// invoiceExpiry holds and invoice's payment hash and its expiry. This // invoiceExpiryTs holds and invoice's payment hash and its expiry. This
// is used to order invoices by their expiry for cancellation. // is used to order invoices by their expiry time for cancellation.
type invoiceExpiry struct { type invoiceExpiryTs struct {
PaymentHash lntypes.Hash PaymentHash lntypes.Hash
Expiry time.Time Expiry time.Time
Keysend bool Keysend bool
@ -22,8 +22,8 @@ type invoiceExpiry struct {
// Less implements PriorityQueueItem.Less such that the top item in the // Less implements PriorityQueueItem.Less such that the top item in the
// priorty queue will be the one that expires next. // priorty queue will be the one that expires next.
func (e invoiceExpiry) Less(other queue.PriorityQueueItem) bool { func (e invoiceExpiryTs) Less(other queue.PriorityQueueItem) bool {
return e.Expiry.Before(other.(*invoiceExpiry).Expiry) return e.Expiry.Before(other.(*invoiceExpiryTs).Expiry)
} }
// InvoiceExpiryWatcher handles automatic invoice cancellation of expried // InvoiceExpiryWatcher handles automatic invoice cancellation of expried
@ -50,7 +50,7 @@ type InvoiceExpiryWatcher struct {
// newInvoices channel is used to wake up the main loop when a new // newInvoices channel is used to wake up the main loop when a new
// invoices is added. // invoices is added.
newInvoices chan []*invoiceExpiry newInvoices chan []*invoiceExpiryTs
wg sync.WaitGroup wg sync.WaitGroup
@ -62,7 +62,7 @@ type InvoiceExpiryWatcher struct {
func NewInvoiceExpiryWatcher(clock clock.Clock) *InvoiceExpiryWatcher { func NewInvoiceExpiryWatcher(clock clock.Clock) *InvoiceExpiryWatcher {
return &InvoiceExpiryWatcher{ return &InvoiceExpiryWatcher{
clock: clock, clock: clock,
newInvoices: make(chan []*invoiceExpiry), newInvoices: make(chan []*invoiceExpiryTs),
quit: make(chan struct{}), quit: make(chan struct{}),
} }
} }
@ -107,7 +107,7 @@ func (ew *InvoiceExpiryWatcher) Stop() {
// the expiry time and creates a slimmer invoiceExpiry object with the hash and // the expiry time and creates a slimmer invoiceExpiry object with the hash and
// expiry time. // expiry time.
func makeInvoiceExpiry(paymentHash lntypes.Hash, func makeInvoiceExpiry(paymentHash lntypes.Hash,
invoice *channeldb.Invoice) *invoiceExpiry { invoice *channeldb.Invoice) *invoiceExpiryTs {
if invoice.State != channeldb.ContractOpen { if invoice.State != channeldb.ContractOpen {
log.Debugf("Invoice not added to expiry watcher: %v", log.Debugf("Invoice not added to expiry watcher: %v",
@ -121,7 +121,7 @@ func makeInvoiceExpiry(paymentHash lntypes.Hash,
} }
expiry := invoice.CreationDate.Add(realExpiry) expiry := invoice.CreationDate.Add(realExpiry)
return &invoiceExpiry{ return &invoiceExpiryTs{
PaymentHash: paymentHash, PaymentHash: paymentHash,
Expiry: expiry, Expiry: expiry,
Keysend: len(invoice.PaymentRequest) == 0, Keysend: len(invoice.PaymentRequest) == 0,
@ -129,7 +129,7 @@ func makeInvoiceExpiry(paymentHash lntypes.Hash,
} }
// AddInvoices adds invoices to the InvoiceExpiryWatcher. // AddInvoices adds invoices to the InvoiceExpiryWatcher.
func (ew *InvoiceExpiryWatcher) AddInvoices(invoices ...*invoiceExpiry) { func (ew *InvoiceExpiryWatcher) AddInvoices(invoices ...*invoiceExpiryTs) {
if len(invoices) > 0 { if len(invoices) > 0 {
select { select {
case ew.newInvoices <- invoices: case ew.newInvoices <- invoices:
@ -147,7 +147,7 @@ func (ew *InvoiceExpiryWatcher) AddInvoices(invoices ...*invoiceExpiry) {
// If there are no active invoices, then it'll simply wait indefinitely. // If there are no active invoices, then it'll simply wait indefinitely.
func (ew *InvoiceExpiryWatcher) nextExpiry() <-chan time.Time { func (ew *InvoiceExpiryWatcher) nextExpiry() <-chan time.Time {
if !ew.expiryQueue.Empty() { if !ew.expiryQueue.Empty() {
top := ew.expiryQueue.Top().(*invoiceExpiry) top := ew.expiryQueue.Top().(*invoiceExpiryTs)
return ew.clock.TickAfter(top.Expiry.Sub(ew.clock.Now())) return ew.clock.TickAfter(top.Expiry.Sub(ew.clock.Now()))
} }
@ -158,7 +158,7 @@ func (ew *InvoiceExpiryWatcher) nextExpiry() <-chan time.Time {
// it from the expiry queue. // it from the expiry queue.
func (ew *InvoiceExpiryWatcher) cancelNextExpiredInvoice() { func (ew *InvoiceExpiryWatcher) cancelNextExpiredInvoice() {
if !ew.expiryQueue.Empty() { if !ew.expiryQueue.Empty() {
top := ew.expiryQueue.Top().(*invoiceExpiry) top := ew.expiryQueue.Top().(*invoiceExpiryTs)
if !top.Expiry.Before(ew.clock.Now()) { if !top.Expiry.Before(ew.clock.Now()) {
return return
} }
@ -190,7 +190,7 @@ func (ew *InvoiceExpiryWatcher) mainLoop() {
// Cancel any invoices that may have expired. // Cancel any invoices that may have expired.
ew.cancelNextExpiredInvoice() ew.cancelNextExpiredInvoice()
pushInvoices := func(invoicesWithExpiry []*invoiceExpiry) { pushInvoices := func(invoicesWithExpiry []*invoiceExpiryTs) {
for _, invoiceWithExpiry := range invoicesWithExpiry { for _, invoiceWithExpiry := range invoicesWithExpiry {
// Avoid pushing nil object to the heap. // Avoid pushing nil object to the heap.
if invoiceWithExpiry != nil { if invoiceWithExpiry != nil {

@ -157,7 +157,7 @@ func TestInvoiceExpiryWhenAddingMultipleInvoices(t *testing.T) {
t.Parallel() t.Parallel()
test := newInvoiceExpiryWatcherTest(t, testTime, 5, 5) test := newInvoiceExpiryWatcherTest(t, testTime, 5, 5)
var invoices []*invoiceExpiry var invoices []*invoiceExpiryTs
for hash, invoice := range test.testData.expiredInvoices { for hash, invoice := range test.testData.expiredInvoices {
invoices = append(invoices, makeInvoiceExpiry(hash, invoice)) invoices = append(invoices, makeInvoiceExpiry(hash, invoice))

@ -160,7 +160,7 @@ func NewRegistry(cdb *channeldb.DB, expiryWatcher *InvoiceExpiryWatcher,
// invoices. // invoices.
func (i *InvoiceRegistry) scanInvoicesOnStart() error { func (i *InvoiceRegistry) scanInvoicesOnStart() error {
var ( var (
pending []*invoiceExpiry pending []*invoiceExpiryTs
removable []channeldb.InvoiceDeleteRef removable []channeldb.InvoiceDeleteRef
) )