Skip to content

Commit 7af1957

Browse files
authored
Merge pull request #8667 from lightningnetwork/elle-new-sweeper
Merge new sweeper branch to master
2 parents 73fd389 + 4d96f9c commit 7af1957

File tree

109 files changed

+16518
-5152
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+16518
-5152
lines changed

chainntnfs/bitcoindnotify/bitcoind.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/btcsuite/btcwallet/chain"
1616
"github.com/lightningnetwork/lnd/blockcache"
1717
"github.com/lightningnetwork/lnd/chainntnfs"
18+
"github.com/lightningnetwork/lnd/fn"
1819
"github.com/lightningnetwork/lnd/queue"
1920
)
2021

@@ -1070,3 +1071,26 @@ func (b *BitcoindNotifier) CancelMempoolSpendEvent(
10701071

10711072
b.memNotifier.UnsubscribeEvent(sub)
10721073
}
1074+
1075+
// LookupInputMempoolSpend takes an outpoint and queries the mempool to find
1076+
// its spending tx. Returns the tx if found, otherwise fn.None.
1077+
//
1078+
// NOTE: part of the MempoolWatcher interface.
1079+
func (b *BitcoindNotifier) LookupInputMempoolSpend(
1080+
op wire.OutPoint) fn.Option[wire.MsgTx] {
1081+
1082+
// Find the spending txid.
1083+
txid, found := b.chainConn.LookupInputMempoolSpend(op)
1084+
if !found {
1085+
return fn.None[wire.MsgTx]()
1086+
}
1087+
1088+
// Query the spending tx using the id.
1089+
tx, err := b.chainConn.GetRawTransaction(&txid)
1090+
if err != nil {
1091+
// TODO(yy): enable logging errors in this package.
1092+
return fn.None[wire.MsgTx]()
1093+
}
1094+
1095+
return fn.Some(*tx.MsgTx().Copy())
1096+
}

chainntnfs/btcdnotify/btcd.go

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ import (
1414
"github.com/btcsuite/btcd/rpcclient"
1515
"github.com/btcsuite/btcd/txscript"
1616
"github.com/btcsuite/btcd/wire"
17+
"github.com/btcsuite/btcwallet/chain"
1718
"github.com/lightningnetwork/lnd/blockcache"
1819
"github.com/lightningnetwork/lnd/chainntnfs"
20+
"github.com/lightningnetwork/lnd/fn"
1921
"github.com/lightningnetwork/lnd/queue"
2022
)
2123

@@ -58,7 +60,7 @@ type BtcdNotifier struct {
5860
active int32 // To be used atomically.
5961
stopped int32 // To be used atomically.
6062

61-
chainConn *rpcclient.Client
63+
chainConn *chain.RPCClient
6264
chainParams *chaincfg.Params
6365

6466
notificationCancels chan interface{}
@@ -127,21 +129,30 @@ func New(config *rpcclient.ConnConfig, chainParams *chaincfg.Params,
127129
quit: make(chan struct{}),
128130
}
129131

132+
// Disable connecting to btcd within the rpcclient.New method. We
133+
// defer establishing the connection to our .Start() method.
134+
config.DisableConnectOnNew = true
135+
config.DisableAutoReconnect = false
136+
130137
ntfnCallbacks := &rpcclient.NotificationHandlers{
131138
OnBlockConnected: notifier.onBlockConnected,
132139
OnBlockDisconnected: notifier.onBlockDisconnected,
133140
OnRedeemingTx: notifier.onRedeemingTx,
134141
}
135142

136-
// Disable connecting to btcd within the rpcclient.New method. We
137-
// defer establishing the connection to our .Start() method.
138-
config.DisableConnectOnNew = true
139-
config.DisableAutoReconnect = false
140-
chainConn, err := rpcclient.New(config, ntfnCallbacks)
143+
rpcCfg := &chain.RPCClientConfig{
144+
ReconnectAttempts: 20,
145+
Conn: config,
146+
Chain: chainParams,
147+
NotificationHandlers: ntfnCallbacks,
148+
}
149+
150+
chainRPC, err := chain.NewRPCClientWithConfig(rpcCfg)
141151
if err != nil {
142152
return nil, err
143153
}
144-
notifier.chainConn = chainConn
154+
155+
notifier.chainConn = chainRPC
145156

146157
return notifier, nil
147158
}
@@ -1127,3 +1138,26 @@ func (b *BtcdNotifier) CancelMempoolSpendEvent(
11271138

11281139
b.memNotifier.UnsubscribeEvent(sub)
11291140
}
1141+
1142+
// LookupInputMempoolSpend takes an outpoint and queries the mempool to find
1143+
// its spending tx. Returns the tx if found, otherwise fn.None.
1144+
//
1145+
// NOTE: part of the MempoolWatcher interface.
1146+
func (b *BtcdNotifier) LookupInputMempoolSpend(
1147+
op wire.OutPoint) fn.Option[wire.MsgTx] {
1148+
1149+
// Find the spending txid.
1150+
txid, found := b.chainConn.LookupInputMempoolSpend(op)
1151+
if !found {
1152+
return fn.None[wire.MsgTx]()
1153+
}
1154+
1155+
// Query the spending tx using the id.
1156+
tx, err := b.chainConn.GetRawTransaction(&txid)
1157+
if err != nil {
1158+
// TODO(yy): enable logging errors in this package.
1159+
return fn.None[wire.MsgTx]()
1160+
}
1161+
1162+
return fn.Some(*tx.MsgTx().Copy())
1163+
}

chainntnfs/interface.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/btcsuite/btcd/btcutil"
1414
"github.com/btcsuite/btcd/chaincfg/chainhash"
1515
"github.com/btcsuite/btcd/wire"
16+
"github.com/lightningnetwork/lnd/fn"
1617
)
1718

1819
var (
@@ -849,4 +850,9 @@ type MempoolWatcher interface {
849850
// CancelMempoolSpendEvent allows the caller to cancel a subscription to
850851
// watch for a spend of an outpoint in the mempool.
851852
CancelMempoolSpendEvent(sub *MempoolSpendEvent)
853+
854+
// LookupInputMempoolSpend looks up the mempool to find a spending tx
855+
// which spends the given outpoint. A fn.None is returned if it's not
856+
// found.
857+
LookupInputMempoolSpend(op wire.OutPoint) fn.Option[wire.MsgTx]
852858
}

chainntnfs/mocks.go

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package chainntnfs
2+
3+
import (
4+
"github.com/btcsuite/btcd/chaincfg/chainhash"
5+
"github.com/btcsuite/btcd/wire"
6+
"github.com/lightningnetwork/lnd/fn"
7+
"github.com/stretchr/testify/mock"
8+
)
9+
10+
// MockMempoolWatcher is a mock implementation of the MempoolWatcher interface.
11+
// This is used by other subsystems to mock the behavior of the mempool
12+
// watcher.
13+
type MockMempoolWatcher struct {
14+
mock.Mock
15+
}
16+
17+
// NewMockMempoolWatcher returns a new instance of a mock mempool watcher.
18+
func NewMockMempoolWatcher() *MockMempoolWatcher {
19+
return &MockMempoolWatcher{}
20+
}
21+
22+
// Compile-time check to ensure MockMempoolWatcher implements MempoolWatcher.
23+
var _ MempoolWatcher = (*MockMempoolWatcher)(nil)
24+
25+
// SubscribeMempoolSpent implements the MempoolWatcher interface.
26+
func (m *MockMempoolWatcher) SubscribeMempoolSpent(
27+
op wire.OutPoint) (*MempoolSpendEvent, error) {
28+
29+
args := m.Called(op)
30+
31+
if args.Get(0) == nil {
32+
return nil, args.Error(1)
33+
}
34+
35+
return args.Get(0).(*MempoolSpendEvent), args.Error(1)
36+
}
37+
38+
// CancelMempoolSpendEvent implements the MempoolWatcher interface.
39+
func (m *MockMempoolWatcher) CancelMempoolSpendEvent(
40+
sub *MempoolSpendEvent) {
41+
42+
m.Called(sub)
43+
}
44+
45+
// LookupInputMempoolSpend looks up the mempool to find a spending tx which
46+
// spends the given outpoint.
47+
func (m *MockMempoolWatcher) LookupInputMempoolSpend(
48+
op wire.OutPoint) fn.Option[wire.MsgTx] {
49+
50+
args := m.Called(op)
51+
52+
return args.Get(0).(fn.Option[wire.MsgTx])
53+
}
54+
55+
// MockNotifier is a mock implementation of the ChainNotifier interface.
56+
type MockChainNotifier struct {
57+
mock.Mock
58+
}
59+
60+
// Compile-time check to ensure MockChainNotifier implements ChainNotifier.
61+
var _ ChainNotifier = (*MockChainNotifier)(nil)
62+
63+
// RegisterConfirmationsNtfn registers an intent to be notified once txid
64+
// reaches numConfs confirmations.
65+
func (m *MockChainNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash,
66+
pkScript []byte, numConfs, heightHint uint32,
67+
opts ...NotifierOption) (*ConfirmationEvent, error) {
68+
69+
args := m.Called(txid, pkScript, numConfs, heightHint)
70+
if args.Get(0) == nil {
71+
return nil, args.Error(1)
72+
}
73+
74+
return args.Get(0).(*ConfirmationEvent), args.Error(1)
75+
}
76+
77+
// RegisterSpendNtfn registers an intent to be notified once the target
78+
// outpoint is successfully spent within a transaction.
79+
func (m *MockChainNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint,
80+
pkScript []byte, heightHint uint32) (*SpendEvent, error) {
81+
82+
args := m.Called(outpoint, pkScript, heightHint)
83+
if args.Get(0) == nil {
84+
return nil, args.Error(1)
85+
}
86+
87+
return args.Get(0).(*SpendEvent), args.Error(1)
88+
}
89+
90+
// RegisterBlockEpochNtfn registers an intent to be notified of each new block
91+
// connected to the tip of the main chain.
92+
func (m *MockChainNotifier) RegisterBlockEpochNtfn(epoch *BlockEpoch) (
93+
*BlockEpochEvent, error) {
94+
95+
args := m.Called(epoch)
96+
if args.Get(0) == nil {
97+
return nil, args.Error(1)
98+
}
99+
100+
return args.Get(0).(*BlockEpochEvent), args.Error(1)
101+
}
102+
103+
// Start the ChainNotifier. Once started, the implementation should be ready,
104+
// and able to receive notification registrations from clients.
105+
func (m *MockChainNotifier) Start() error {
106+
args := m.Called()
107+
108+
return args.Error(0)
109+
}
110+
111+
// Started returns true if this instance has been started, and false otherwise.
112+
func (m *MockChainNotifier) Started() bool {
113+
args := m.Called()
114+
115+
return args.Bool(0)
116+
}
117+
118+
// Stops the concrete ChainNotifier.
119+
func (m *MockChainNotifier) Stop() error {
120+
args := m.Called()
121+
122+
return args.Error(0)
123+
}

0 commit comments

Comments
 (0)