peer: ensures invoices are marked as settled after redemption

This commit is contained in:
Olaoluwa Osuntokun 2016-09-20 17:00:11 -07:00
parent 5402d705ab
commit 62271768b0
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2
2 changed files with 28 additions and 4 deletions

View File

@ -6,6 +6,7 @@ import (
"time"
"github.com/btcsuite/fastsha256"
"github.com/davecgh/go-spew/spew"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/roasbeef/btcd/wire"
"github.com/roasbeef/btcutil"
@ -54,15 +55,21 @@ func newInvoiceRegistry(cdb *channeldb.DB) *invoiceRegistry {
func (i *invoiceRegistry) AddDebugInvoice(amt btcutil.Amount, preimage wire.ShaHash) {
paymentHash := wire.ShaHash(fastsha256.Sum256(preimage[:]))
i.Lock()
i.debugInvoices[paymentHash] = &channeldb.Invoice{
invoice := &channeldb.Invoice{
CreationDate: time.Now(),
Terms: channeldb.ContractTerm{
Value: amt,
PaymentPreimage: preimage,
},
}
i.Lock()
i.debugInvoices[paymentHash] = invoice
i.Unlock()
ltndLog.Debugf("Adding debug invoice %v", newLogClosure(func() string {
return spew.Sdump(invoice)
}))
}
// AddInvoice adds a regular invoice for the specified amount, identified by
@ -71,6 +78,10 @@ func (i *invoiceRegistry) AddDebugInvoice(amt btcutil.Amount, preimage wire.ShaH
// daemon add/forward HTLC's are able to obtain the proper preimage required
// for redemption in the case that we're the final destination.
func (i *invoiceRegistry) AddInvoice(invoice *channeldb.Invoice) error {
ltndLog.Debugf("Adding invoice %v", newLogClosure(func() string {
return spew.Sdump(invoice)
}))
// TODO(roasbeef): also check in memory for quick lookups/settles?
return i.cdb.AddInvoice(invoice)
}
@ -99,6 +110,8 @@ func (i *invoiceRegistry) LookupInvoice(rHash wire.ShaHash) (*channeldb.Invoice,
// dbueg invoice, then this method is a nooop as debug invoices are never fully
// settled.
func (i *invoiceRegistry) SettleInvoice(rHash wire.ShaHash) error {
ltndLog.Debugf("Setting invoice %x", rHash[:])
// First check the in-memory debug invoice index to see if this is an
// existing invoice added for debugging.
i.RLock()

15
peer.go
View File

@ -1120,8 +1120,7 @@ func (p *peer) handleUpstreamMsg(state *commitmentState, msg lnwire.Message) {
// We perform the HTLC forwarding to the switch in a distinct
// goroutine in order not to block the post-processing of
// HTLC's that are eligble for forwarding.
// TODO(roasbeef): no need to forward if have settled any of
// these.
// TODO(roasbeef): don't forward if we're going to settle them
go func() {
for _, htlc := range htlcsToForward {
// Send this fully activated HTLC to the htlc
@ -1136,6 +1135,7 @@ func (p *peer) handleUpstreamMsg(state *commitmentState, msg lnwire.Message) {
// can them from the pending set, and signal the requster (if
// existing) that the payment has been fully fulfilled.
var bandwidthUpdate btcutil.Amount
var settledPayments []wire.ShaHash
numSettled := 0
for _, htlc := range htlcsToForward {
if p, ok := state.clearedHTCLs[htlc.ParentIndex]; ok {
@ -1176,6 +1176,8 @@ func (p *peer) handleUpstreamMsg(state *commitmentState, msg lnwire.Message) {
delete(state.htlcsToSettle, htlc.Index)
bandwidthUpdate += invoice.Terms.Value
settledPayments = append(settledPayments,
wire.ShaHash(htlc.RHash))
numSettled++
}
@ -1203,6 +1205,15 @@ func (p *peer) handleUpstreamMsg(state *commitmentState, msg lnwire.Message) {
// TODO(roasbeef): wait to delete from htlcsToSettle?
state.numUnAcked += 1
}
// Notify the invoiceRegistry of the invoices we just settled
// with this latest commitment update.
for _, invoice := range settledPayments {
err := p.server.invoices.SettleInvoice(invoice)
if err != nil {
peerLog.Errorf("unable to settle invoice: %v", err)
}
}
}
}