-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathrebroadcaster_test.go
More file actions
200 lines (159 loc) · 4.88 KB
/
Copy pathrebroadcaster_test.go
File metadata and controls
200 lines (159 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package lnwallet
import (
"sync/atomic"
"testing"
"time"
"github.com/btcsuite/btcd/chainhash/v2"
"github.com/btcsuite/btcd/wire/v2"
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/lnutils"
"github.com/stretchr/testify/require"
)
type mockRebroadcaster struct {
started atomic.Bool
rebroadcastAttempt chan struct{}
falseStart bool
confSignal chan struct{}
startSignal chan struct{}
}
func newMockRebroadcaster(falseStart bool) *mockRebroadcaster {
return &mockRebroadcaster{
rebroadcastAttempt: make(chan struct{}, 1),
falseStart: falseStart,
confSignal: make(chan struct{}, 1),
startSignal: make(chan struct{}),
}
}
func (m *mockRebroadcaster) Start() error {
if !m.falseStart {
defer m.started.Store(true)
defer close(m.startSignal)
}
return nil
}
func (m *mockRebroadcaster) Started() bool {
return m.started.Load()
}
func (m *mockRebroadcaster) Stop() {
}
// Broadcast enqueues a transaction to be rebroadcast until it's been
// confirmed.
func (m *mockRebroadcaster) Broadcast(tx *wire.MsgTx) error {
m.rebroadcastAttempt <- struct{}{}
return nil
}
func (m *mockRebroadcaster) MarkAsConfirmed(txid chainhash.Hash) {
m.confSignal <- struct{}{}
}
func assertBroadcasterBypass(t *testing.T, wallet *LightningWallet,
rebroadcaster *mockRebroadcaster,
walletController *mockWalletController) {
testTx := wire.NewMsgTx(2)
timeout := time.Second * 1
require.NoError(t, wallet.PublishTransaction(testTx, ""))
// The tx should go to the backend.
_, err := lnutils.RecvOrTimeout(
walletController.PublishedTransactions, timeout,
)
require.NoError(t, err)
// It shouldn't go to the rebroadcaster.
select {
case <-time.After(timeout):
case <-rebroadcaster.rebroadcastAttempt:
t.Fatal("tx sent to rebroadcaster")
}
}
func assertBroadcasterSend(t *testing.T, wallet *LightningWallet,
rebroadcaster *mockRebroadcaster,
walletController *mockWalletController) {
testTx := wire.NewMsgTx(2)
testTx.AddTxOut(&wire.TxOut{})
timeout := time.Second * 1
require.NoError(t, wallet.PublishTransaction(testTx, ""))
// The tx should go to the backend.
_, err := lnutils.RecvOrTimeout(
walletController.PublishedTransactions, timeout,
)
require.NoError(t, err)
// It should also go to the rebroadcaster.
select {
case <-time.After(timeout):
t.Fatal("tx not sent to rebroadcaster")
case <-rebroadcaster.rebroadcastAttempt:
}
}
// TestWalletRebroadcaster tests that the wallet properly manages the existence
// or lack of existence of the rebroadcaster, and also properly marks the
// transaction as confirmed.
func TestWalletRebroadcaster(t *testing.T) {
t.Parallel()
rebroadcaster := newMockRebroadcaster(false)
walletController := &mockWalletController{
PublishedTransactions: make(chan *wire.MsgTx, 1),
}
chainIO := &mockChainIO{}
notifier := &mockChainNotifier{
SpendChan: make(chan *chainntnfs.SpendDetail, 1),
EpochChan: make(chan *chainntnfs.BlockEpoch, 1),
ConfChan: make(chan *chainntnfs.TxConfirmation, 1),
}
cfg := &Config{
Rebroadcaster: rebroadcaster,
WalletController: walletController,
Notifier: notifier,
ChainIO: chainIO,
}
t.Run("rebroadcast bypass", func(t *testing.T) {
// We'll make a copy of the config, but without the
// broadcaster.
testCfg := *cfg
testCfg.Rebroadcaster = nil
wallet, err := NewLightningWallet(testCfg)
require.NoError(t, err)
require.NoError(t, wallet.Startup())
// If we try to broadcast, it should go straight to the wallet
// backend and skip the broadcaster.
assertBroadcasterBypass(
t, wallet, rebroadcaster, walletController,
)
err = wallet.Shutdown()
require.NoError(t, err)
// If we make a new wallet, that has the broadcaster, but
// hasn't started yet, we should see the same behavior.
testCfg.Rebroadcaster = newMockRebroadcaster(true)
wallet, err = NewLightningWallet(testCfg)
require.NoError(t, err)
require.NoError(t, wallet.Startup())
assertBroadcasterBypass(
t, wallet, rebroadcaster, walletController,
)
err = wallet.Shutdown()
require.NoError(t, err)
})
t.Run("rebroadcast normal", func(t *testing.T) {
wallet, err := NewLightningWallet(*cfg)
require.NoError(t, err)
require.NoError(t, wallet.Startup())
defer func() {
err = wallet.Shutdown()
require.NoError(t, err)
}()
// Wait for the broadcaster to start.
_, err = lnutils.RecvOrTimeout(
rebroadcaster.startSignal, time.Second,
)
require.NoError(t, err)
// We'll now broadcast a new test transaction, asserting that
// it goes to both the backend and the rebroadcaster.
assertBroadcasterSend(
t, wallet, rebroadcaster, walletController,
)
// We'll now mark the transaction as confirmed, and assert that
// the rebroadcaster was notified.
notifier.ConfChan <- &chainntnfs.TxConfirmation{}
_, err = lnutils.RecvOrTimeout(
rebroadcaster.confSignal, time.Second,
)
require.NoError(t, err)
})
}