2018-01-17 07:02:25 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
|
|
|
"github.com/lightningnetwork/lnd/contractcourt"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
2018-06-05 04:34:16 +03:00
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
2018-01-17 07:02:25 +03:00
|
|
|
)
|
|
|
|
|
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 {
|
2018-01-17 07:02:25 +03:00
|
|
|
updateChan chan []byte
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
invoices *invoiceRegistry
|
|
|
|
|
|
|
|
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{
|
2018-01-17 07:02:25 +03:00
|
|
|
updateChan: make(chan []byte, 10),
|
|
|
|
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.
|
|
|
|
func (p *preimageBeacon) LookupPreimage(payHash []byte) ([]byte, bool) {
|
|
|
|
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.
|
|
|
|
var invoiceKey chainhash.Hash
|
|
|
|
copy(invoiceKey[:], payHash)
|
2018-06-30 02:03:46 +03:00
|
|
|
invoice, _, err := p.invoices.LookupInvoice(invoiceKey)
|
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:
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we've found the invoice, then we can return the preimage
|
|
|
|
// directly.
|
|
|
|
if err != channeldb.ErrInvoiceNotFound {
|
|
|
|
return invoice.Terms.PaymentPreimage[:], true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we'll perform a final check using the witness cache.
|
|
|
|
preimage, err := p.wCache.LookupWitness(
|
|
|
|
channeldb.Sha256HashWitness, payHash,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
ltndLog.Errorf("unable to lookup witness: %v", err)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return preimage, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddPreImage adds a newly discovered preimage to the global cache, and also
|
|
|
|
// signals any subscribers of the newly discovered witness.
|
|
|
|
func (p *preimageBeacon) AddPreimage(pre []byte) error {
|
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
|
|
|
srvrLog.Infof("Adding preimage=%x to witness cache", pre[:])
|
|
|
|
|
|
|
|
// First, we'll add the witness to the decaying witness cache.
|
|
|
|
err := p.wCache.AddWitness(channeldb.Sha256HashWitness, pre)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2018-01-17 07:02:25 +03:00
|
|
|
select {
|
|
|
|
case c.updateChan <- pre:
|
|
|
|
case <-c.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}(client)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ contractcourt.WitnessBeacon = (*preimageBeacon)(nil)
|
|
|
|
var _ lnwallet.PreimageCache = (*preimageBeacon)(nil)
|