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

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

15
peer.go

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