2023-04-03 15:02:23 +02:00
|
|
|
package chainntnfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2023-04-05 21:39:08 +02:00
|
|
|
"sync/atomic"
|
2023-04-03 15:02:23 +02:00
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/btcutil"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/lightningnetwork/lnd/lnutils"
|
|
|
|
)
|
|
|
|
|
|
|
|
// inputsWithTx is a map of outpoints to the tx that spends them.
|
|
|
|
type inputsWithTx map[wire.OutPoint]*SpendDetail
|
|
|
|
|
|
|
|
// MempoolNotifier defines an internal mempool notifier that's used to notify
|
|
|
|
// the spending of given inputs. This is mounted to either `BitcoindNotifier`
|
|
|
|
// or `BtcdNotifier` depending on the chain backend.
|
|
|
|
type MempoolNotifier struct {
|
|
|
|
wg sync.WaitGroup
|
|
|
|
|
|
|
|
// subscribedInputs stores the inputs that we want to watch their
|
|
|
|
// spending event for.
|
2023-04-05 21:39:08 +02:00
|
|
|
subscribedInputs *lnutils.SyncMap[wire.OutPoint,
|
|
|
|
*lnutils.SyncMap[uint64, *MempoolSpendEvent]]
|
|
|
|
|
|
|
|
// sCounter is used to generate unique subscription IDs.
|
|
|
|
sCounter atomic.Uint64
|
2023-04-03 15:02:23 +02:00
|
|
|
|
|
|
|
// quit is closed when the notifier is torn down.
|
|
|
|
quit chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MempoolSpendEvent is returned to the subscriber to watch for the spending
|
|
|
|
// event for the requested input.
|
|
|
|
type MempoolSpendEvent struct {
|
|
|
|
// Spend is a receive only channel which will be sent upon once the
|
|
|
|
// target outpoint has been spent.
|
|
|
|
//
|
|
|
|
// NOTE: This channel must be buffered.
|
|
|
|
Spend <-chan *SpendDetail
|
|
|
|
|
2023-04-06 21:45:22 +02:00
|
|
|
// id is the unique identifier of this subscription.
|
|
|
|
id uint64
|
|
|
|
|
|
|
|
// outpoint is the subscribed outpoint.
|
|
|
|
outpoint wire.OutPoint
|
|
|
|
|
2023-04-03 15:02:23 +02:00
|
|
|
// event is the channel that will be sent upon once the target outpoint
|
|
|
|
// is spent.
|
|
|
|
event chan *SpendDetail
|
|
|
|
|
|
|
|
// cancel cancels the subscription.
|
|
|
|
cancel chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// newMempoolSpendEvent returns a new instance of MempoolSpendEvent.
|
2023-04-06 21:45:22 +02:00
|
|
|
func newMempoolSpendEvent(id uint64, op wire.OutPoint) *MempoolSpendEvent {
|
2023-04-03 15:02:23 +02:00
|
|
|
sub := &MempoolSpendEvent{
|
2023-04-06 21:45:22 +02:00
|
|
|
id: id,
|
|
|
|
outpoint: op,
|
|
|
|
event: make(chan *SpendDetail, 1),
|
|
|
|
cancel: make(chan struct{}),
|
2023-04-03 15:02:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mount the receive only channel to the event channel.
|
|
|
|
sub.Spend = (<-chan *SpendDetail)(sub.event)
|
|
|
|
|
|
|
|
return sub
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMempoolNotifier takes a chain connection and returns a new mempool
|
|
|
|
// notifier.
|
|
|
|
func NewMempoolNotifier() *MempoolNotifier {
|
|
|
|
return &MempoolNotifier{
|
2023-04-05 21:39:08 +02:00
|
|
|
subscribedInputs: &lnutils.SyncMap[
|
|
|
|
wire.OutPoint, *lnutils.SyncMap[
|
|
|
|
uint64, *MempoolSpendEvent,
|
|
|
|
]]{},
|
2023-04-03 15:02:23 +02:00
|
|
|
quit: make(chan struct{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubscribeInput takes an outpoint of the input and returns a channel that the
|
|
|
|
// subscriber can listen to for the spending event.
|
|
|
|
func (m *MempoolNotifier) SubscribeInput(
|
|
|
|
outpoint wire.OutPoint) *MempoolSpendEvent {
|
|
|
|
|
2023-04-05 21:39:08 +02:00
|
|
|
// Get the current subscribers for this input or create a new one.
|
|
|
|
clients := &lnutils.SyncMap[uint64, *MempoolSpendEvent]{}
|
|
|
|
clients, _ = m.subscribedInputs.LoadOrStore(outpoint, clients)
|
|
|
|
|
2023-04-06 21:45:22 +02:00
|
|
|
// Increment the subscription counter and return the new value.
|
|
|
|
subscriptionID := m.sCounter.Add(1)
|
|
|
|
|
2023-04-05 21:39:08 +02:00
|
|
|
// Create a new subscription.
|
2023-04-06 21:45:22 +02:00
|
|
|
sub := newMempoolSpendEvent(subscriptionID, outpoint)
|
2023-04-03 15:02:23 +02:00
|
|
|
|
2023-04-05 21:39:08 +02:00
|
|
|
// Add the subscriber with a unique id.
|
|
|
|
clients.Store(subscriptionID, sub)
|
|
|
|
|
|
|
|
// Update the subscribed inputs.
|
|
|
|
m.subscribedInputs.Store(outpoint, clients)
|
2023-04-03 15:02:23 +02:00
|
|
|
|
2023-04-06 21:45:22 +02:00
|
|
|
Log.Debugf("Subscribed(id=%v) mempool event for input=%s",
|
|
|
|
subscriptionID, outpoint)
|
|
|
|
|
2023-04-03 15:02:23 +02:00
|
|
|
return sub
|
|
|
|
}
|
|
|
|
|
2023-04-06 21:45:22 +02:00
|
|
|
// UnsubscribeInput removes all the subscriptions for the given outpoint.
|
|
|
|
func (m *MempoolNotifier) UnsubscribeInput(outpoint wire.OutPoint) {
|
2023-04-03 15:02:23 +02:00
|
|
|
Log.Debugf("Unsubscribing MempoolSpendEvent for input %s", outpoint)
|
|
|
|
m.subscribedInputs.Delete(outpoint)
|
|
|
|
}
|
|
|
|
|
2023-04-06 21:45:22 +02:00
|
|
|
// UnsubscribeEvent removes a given subscriber for the given MempoolSpendEvent.
|
|
|
|
func (m *MempoolNotifier) UnsubscribeEvent(sub *MempoolSpendEvent) {
|
|
|
|
Log.Debugf("Unsubscribing(id=%v) MempoolSpendEvent for input=%s",
|
|
|
|
sub.id, sub.outpoint)
|
|
|
|
|
|
|
|
// Load all the subscribers for this input.
|
|
|
|
clients, loaded := m.subscribedInputs.Load(sub.outpoint)
|
|
|
|
if !loaded {
|
|
|
|
Log.Debugf("No subscribers for input %s", sub.outpoint)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the subscriber.
|
|
|
|
subscriber, loaded := clients.Load(sub.id)
|
|
|
|
if !loaded {
|
|
|
|
Log.Debugf("No subscribers for input %s with id %v",
|
|
|
|
sub.outpoint, sub.id)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the cancel channel in case it's been used in a goroutine.
|
|
|
|
close(subscriber.cancel)
|
|
|
|
|
|
|
|
// Remove the subscriber.
|
|
|
|
clients.Delete(sub.id)
|
|
|
|
}
|
|
|
|
|
2023-04-06 22:35:27 +02:00
|
|
|
// UnsubsribeConfirmedSpentTx takes a transaction and removes the subscriptions
|
|
|
|
// identified using its inputs.
|
|
|
|
func (m *MempoolNotifier) UnsubsribeConfirmedSpentTx(tx *btcutil.Tx) {
|
|
|
|
Log.Tracef("Unsubscribe confirmed tx %s", tx.Hash())
|
|
|
|
|
|
|
|
// Get the spent inputs of interest.
|
|
|
|
spentInputs := m.findRelevantInputs(tx)
|
|
|
|
|
|
|
|
// Unsubscribe the subscribers.
|
|
|
|
for outpoint := range spentInputs {
|
|
|
|
m.UnsubscribeInput(outpoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
Log.Tracef("Finished unsubscribing confirmed tx %s, found %d inputs",
|
|
|
|
tx.Hash(), len(spentInputs))
|
|
|
|
}
|
|
|
|
|
2023-04-03 15:02:23 +02:00
|
|
|
// ProcessRelevantSpendTx takes a transaction and checks whether it spends any
|
|
|
|
// of the subscribed inputs. If so, spend notifications are sent to the
|
|
|
|
// relevant subscribers.
|
|
|
|
func (m *MempoolNotifier) ProcessRelevantSpendTx(tx *btcutil.Tx) {
|
|
|
|
Log.Tracef("Processing mempool tx %s", tx.Hash())
|
|
|
|
defer Log.Tracef("Finished processing mempool tx %s", tx.Hash())
|
|
|
|
|
|
|
|
// Get the spent inputs of interest.
|
|
|
|
spentInputs := m.findRelevantInputs(tx)
|
|
|
|
|
|
|
|
// Notify the subscribers.
|
|
|
|
m.notifySpent(spentInputs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TearDown stops the notifier and cleans up resources.
|
|
|
|
func (m *MempoolNotifier) TearDown() {
|
|
|
|
Log.Infof("Stopping mempool notifier")
|
2023-09-07 20:16:42 +02:00
|
|
|
defer Log.Debug("mempool notifier stopped")
|
|
|
|
|
2023-04-03 15:02:23 +02:00
|
|
|
close(m.quit)
|
|
|
|
m.wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// findRelevantInputs takes a transaction to find the subscribed inputs and
|
|
|
|
// returns them.
|
|
|
|
func (m *MempoolNotifier) findRelevantInputs(tx *btcutil.Tx) inputsWithTx {
|
|
|
|
txid := tx.Hash()
|
|
|
|
watchedInputs := make(inputsWithTx)
|
|
|
|
|
|
|
|
// NOTE: we may have found multiple targeted inputs in the same tx.
|
|
|
|
for i, input := range tx.MsgTx().TxIn {
|
|
|
|
op := &input.PreviousOutPoint
|
|
|
|
|
|
|
|
// Check whether this input is subscribed.
|
|
|
|
_, loaded := m.subscribedInputs.Load(*op)
|
|
|
|
if !loaded {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If found, save it to watchedInputs to notify the
|
|
|
|
// subscriber later.
|
|
|
|
Log.Infof("Found input %s, spent in %s", op, txid)
|
|
|
|
|
|
|
|
// Construct the spend details.
|
|
|
|
details := &SpendDetail{
|
|
|
|
SpentOutPoint: op,
|
|
|
|
SpenderTxHash: txid,
|
|
|
|
SpendingTx: tx.MsgTx().Copy(),
|
|
|
|
SpenderInputIndex: uint32(i),
|
|
|
|
SpendingHeight: 0,
|
|
|
|
}
|
|
|
|
watchedInputs[*op] = details
|
|
|
|
}
|
|
|
|
|
|
|
|
return watchedInputs
|
|
|
|
}
|
|
|
|
|
|
|
|
// notifySpent iterates all the spentInputs and notifies the subscribers about
|
|
|
|
// the spent details.
|
|
|
|
func (m *MempoolNotifier) notifySpent(spentInputs inputsWithTx) {
|
2023-04-05 21:39:08 +02:00
|
|
|
// notifySingle sends a notification to a single subscriber about the
|
|
|
|
// spending event.
|
2023-04-03 15:02:23 +02:00
|
|
|
//
|
|
|
|
// NOTE: must be used inside a goroutine.
|
2023-04-05 21:39:08 +02:00
|
|
|
notifySingle := func(id uint64, sub *MempoolSpendEvent,
|
|
|
|
op wire.OutPoint, detail *SpendDetail) {
|
2023-04-03 15:02:23 +02:00
|
|
|
|
2023-04-05 21:39:08 +02:00
|
|
|
defer m.wg.Done()
|
2023-04-03 15:02:23 +02:00
|
|
|
|
|
|
|
// Send the spend details to the subscriber.
|
|
|
|
select {
|
|
|
|
case sub.event <- detail:
|
2023-04-06 21:45:22 +02:00
|
|
|
Log.Debugf("Notified(id=%v) mempool spent for input %s",
|
|
|
|
sub.id, op)
|
2023-04-03 15:02:23 +02:00
|
|
|
|
|
|
|
case <-sub.cancel:
|
2023-04-06 21:45:22 +02:00
|
|
|
Log.Debugf("Subscription(id=%v) canceled, skipped "+
|
|
|
|
"notifying spent for input %s", sub.id, op)
|
2023-04-03 15:02:23 +02:00
|
|
|
|
|
|
|
case <-m.quit:
|
|
|
|
Log.Debugf("Mempool notifier quit, skipped notifying "+
|
|
|
|
"mempool spent for input %s", op)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-05 21:39:08 +02:00
|
|
|
// notifyAll is a helper closure that constructs a spend detail and
|
|
|
|
// sends it to all the subscribers of that particular input.
|
|
|
|
//
|
|
|
|
// NOTE: must be used inside a goroutine.
|
|
|
|
notifyAll := func(detail *SpendDetail, op wire.OutPoint) {
|
|
|
|
defer m.wg.Done()
|
|
|
|
|
|
|
|
txid := detail.SpendingTx.TxHash()
|
2023-04-06 21:45:22 +02:00
|
|
|
Log.Debugf("Notifying all clients for the spend of %s in tx %s",
|
|
|
|
op, txid)
|
2023-04-05 21:39:08 +02:00
|
|
|
|
|
|
|
// Load the subscriber.
|
|
|
|
subs, loaded := m.subscribedInputs.Load(op)
|
|
|
|
if !loaded {
|
|
|
|
Log.Errorf("Sub not found for %s", op)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate all the subscribers for this input and notify them.
|
|
|
|
subs.ForEach(func(id uint64, sub *MempoolSpendEvent) error {
|
|
|
|
m.wg.Add(1)
|
|
|
|
go notifySingle(id, sub, op, detail)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-04-03 15:02:23 +02:00
|
|
|
// Iterate the spent inputs to notify the subscribers concurrently.
|
|
|
|
for op, tx := range spentInputs {
|
2023-04-05 21:39:08 +02:00
|
|
|
op, tx := op, tx
|
|
|
|
|
2023-04-03 15:02:23 +02:00
|
|
|
m.wg.Add(1)
|
2023-04-05 21:39:08 +02:00
|
|
|
go notifyAll(tx, op)
|
2023-04-03 15:02:23 +02:00
|
|
|
}
|
|
|
|
}
|