2019-01-24 16:28:25 +03:00
|
|
|
package lnd
|
2018-01-17 07:02:25 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
|
|
|
"github.com/lightningnetwork/lnd/contractcourt"
|
2018-12-20 13:57:44 +03:00
|
|
|
"github.com/lightningnetwork/lnd/invoices"
|
2019-01-15 13:31:22 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2018-01-17 07:02:25 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
|
|
|
)
|
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
// preimageSubscriber reprints an active subscription to be notified once the
|
2018-01-17 07:02:25 +03:00
|
|
|
// daemon discovers new preimages, either on chain or off-chain.
|
2018-02-07 06:11:11 +03:00
|
|
|
type preimageSubscriber struct {
|
2019-02-20 04:06:00 +03:00
|
|
|
updateChan chan lntypes.Preimage
|
2018-01-17 07:02:25 +03:00
|
|
|
|
|
|
|
quit chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// preimageBeacon is an implementation of the contractcourt.WitnessBeacon
|
|
|
|
// interface, and the lnwallet.PreimageCache interface. This implementation is
|
|
|
|
// concerned with a single witness type: sha256 hahsh preimages.
|
|
|
|
type preimageBeacon struct {
|
|
|
|
sync.RWMutex
|
|
|
|
|
2018-12-20 13:57:44 +03:00
|
|
|
invoices *invoices.InvoiceRegistry
|
2018-01-17 07:02:25 +03:00
|
|
|
|
|
|
|
wCache *channeldb.WitnessCache
|
|
|
|
|
|
|
|
clientCounter uint64
|
2018-02-07 06:11:11 +03:00
|
|
|
subscribers map[uint64]*preimageSubscriber
|
2018-01-17 07:02:25 +03:00
|
|
|
}
|
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
// SubscribeUpdates returns a channel that will be sent upon *each* time a new
|
2018-01-17 07:02:25 +03:00
|
|
|
// preimage is discovered.
|
2018-02-07 06:11:11 +03:00
|
|
|
func (p *preimageBeacon) SubscribeUpdates() *contractcourt.WitnessSubscription {
|
2018-01-17 07:02:25 +03:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
|
|
|
clientID := p.clientCounter
|
2018-02-07 06:11:11 +03:00
|
|
|
client := &preimageSubscriber{
|
2019-02-20 04:06:00 +03:00
|
|
|
updateChan: make(chan lntypes.Preimage, 10),
|
2018-01-17 07:02:25 +03:00
|
|
|
quit: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
p.subscribers[p.clientCounter] = client
|
|
|
|
|
|
|
|
p.clientCounter++
|
|
|
|
|
|
|
|
srvrLog.Debugf("Creating new witness beacon subscriber, id=%v",
|
|
|
|
p.clientCounter)
|
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
return &contractcourt.WitnessSubscription{
|
2018-01-17 07:02:25 +03:00
|
|
|
WitnessUpdates: client.updateChan,
|
2018-02-07 06:11:11 +03:00
|
|
|
CancelSubscription: func() {
|
2018-01-17 07:02:25 +03:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
|
|
|
delete(p.subscribers, clientID)
|
|
|
|
|
|
|
|
close(client.quit)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LookupPreImage attempts to lookup a preimage in the global cache. True is
|
|
|
|
// returned for the second argument if the preimage is found.
|
2019-02-20 04:06:00 +03:00
|
|
|
func (p *preimageBeacon) LookupPreimage(
|
|
|
|
payHash lntypes.Hash) (lntypes.Preimage, bool) {
|
|
|
|
|
2018-01-17 07:02:25 +03:00
|
|
|
p.RLock()
|
|
|
|
defer p.RUnlock()
|
|
|
|
|
|
|
|
// First, we'll check the invoice registry to see if we already know of
|
|
|
|
// the preimage as it's on that we created ourselves.
|
2019-02-20 04:06:00 +03:00
|
|
|
invoice, _, err := p.invoices.LookupInvoice(payHash)
|
2018-01-17 07:02:25 +03:00
|
|
|
switch {
|
|
|
|
case err == channeldb.ErrInvoiceNotFound:
|
|
|
|
// If we get this error, then it simply means that this invoice
|
|
|
|
// wasn't found, so we don't treat it as a critical error.
|
|
|
|
case err != nil:
|
2019-02-20 04:06:00 +03:00
|
|
|
return lntypes.Preimage{}, false
|
2018-01-17 07:02:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we've found the invoice, then we can return the preimage
|
|
|
|
// directly.
|
2018-10-05 11:14:56 +03:00
|
|
|
if err != channeldb.ErrInvoiceNotFound &&
|
|
|
|
invoice.Terms.PaymentPreimage != channeldb.UnknownPreimage {
|
|
|
|
|
2019-02-20 04:06:00 +03:00
|
|
|
return invoice.Terms.PaymentPreimage, true
|
2018-01-17 07:02:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we'll perform a final check using the witness cache.
|
2019-02-20 04:06:42 +03:00
|
|
|
preimage, err := p.wCache.LookupSha256Witness(payHash)
|
2018-01-17 07:02:25 +03:00
|
|
|
if err != nil {
|
2019-02-20 04:06:00 +03:00
|
|
|
ltndLog.Errorf("Unable to lookup witness: %v", err)
|
|
|
|
return lntypes.Preimage{}, false
|
|
|
|
}
|
|
|
|
|
2018-01-17 07:02:25 +03:00
|
|
|
return preimage, true
|
|
|
|
}
|
|
|
|
|
2019-02-20 04:05:04 +03:00
|
|
|
// AddPreimages adds a batch of newly discovered preimages to the global cache,
|
|
|
|
// and also signals any subscribers of the newly discovered witness.
|
2019-02-20 04:06:00 +03:00
|
|
|
func (p *preimageBeacon) AddPreimages(preimages ...lntypes.Preimage) error {
|
2019-02-20 04:05:04 +03:00
|
|
|
// Exit early if no preimages are presented.
|
|
|
|
if len(preimages) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2018-01-17 07:02:25 +03:00
|
|
|
|
2019-02-20 04:05:04 +03:00
|
|
|
// Copy the preimages to ensure the backing area can't be modified by
|
|
|
|
// the caller when delivering notifications.
|
2019-02-20 04:06:00 +03:00
|
|
|
preimageCopies := make([]lntypes.Preimage, 0, len(preimages))
|
2019-02-20 04:05:04 +03:00
|
|
|
for _, preimage := range preimages {
|
2019-02-20 04:06:00 +03:00
|
|
|
srvrLog.Infof("Adding preimage=%v to witness cache", preimage)
|
2019-02-20 04:05:04 +03:00
|
|
|
preimageCopies = append(preimageCopies, preimage)
|
|
|
|
}
|
2018-01-17 07:02:25 +03:00
|
|
|
|
|
|
|
// First, we'll add the witness to the decaying witness cache.
|
2019-02-20 04:06:00 +03:00
|
|
|
err := p.wCache.AddSha256Witnesses(preimages...)
|
2018-01-17 07:02:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-20 04:05:04 +03:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
2018-01-17 07:02:25 +03:00
|
|
|
// With the preimage added to our state, we'll now send a new
|
|
|
|
// notification to all subscribers.
|
|
|
|
for _, client := range p.subscribers {
|
2018-02-07 06:11:11 +03:00
|
|
|
go func(c *preimageSubscriber) {
|
2019-02-20 04:05:04 +03:00
|
|
|
for _, preimage := range preimageCopies {
|
|
|
|
select {
|
|
|
|
case c.updateChan <- preimage:
|
|
|
|
case <-c.quit:
|
|
|
|
return
|
|
|
|
}
|
2018-01-17 07:02:25 +03:00
|
|
|
}
|
|
|
|
}(client)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ contractcourt.WitnessBeacon = (*preimageBeacon)(nil)
|
|
|
|
var _ lnwallet.PreimageCache = (*preimageBeacon)(nil)
|