@@ -72,7 +72,7 @@ class ZmqWatcherSpec extends TestKitBaseClass with AnyFunSuiteLike with Bitcoind
7272 case class Fixture (blockHeight : AtomicLong , bitcoinClient : BitcoinCoreClient , watcher : typed.ActorRef [ZmqWatcher .Command ], probe : TestProbe , listener : TestProbe )
7373
7474 // NB: we can't use ScalaTest's fixtures, they would see uninitialized bitcoind fields because they sandbox each test.
75- private def withWatcher (testFun : Fixture => Any , scanPastBlock : Boolean = false , useTxoSpenderIndex : Boolean = false ): Unit = {
75+ private def withWatcher (testFun : Fixture => Any , scanPastBlock : Boolean = false ): Unit = {
7676 val blockCount = new AtomicLong ()
7777 val probe = TestProbe ()
7878 val listener = TestProbe ()
@@ -85,7 +85,6 @@ class ZmqWatcherSpec extends TestKitBaseClass with AnyFunSuiteLike with Bitcoind
8585 // We enable it and use a faster (randomized) delay when requested.
8686 .modify(_.channelConf.scanPreviousBlocksDepth).setToIf(scanPastBlock)(6 )
8787 .modify(_.channelConf.maxBlockProcessingDelay).setToIf(scanPastBlock)(10 millis)
88- // .modify(_.channelConf.useTxoSpenderIndex).setTo(useTxoSpenderIndex)
8988 val watcher = system.spawn(ZmqWatcher (nodeParams, blockCount, bitcoinClient), UUID .randomUUID().toString)
9089 try {
9190 testFun(Fixture (blockCount, bitcoinClient, watcher, probe, listener))
@@ -246,86 +245,80 @@ class ZmqWatcherSpec extends TestKitBaseClass with AnyFunSuiteLike with Bitcoind
246245 })
247246 }
248247
249- def watchForSpentTransactions (f : Fixture ): Unit = {
250- import f ._
251-
252- val (priv, address) = createExternalAddress()
253- val tx = sendToAddress(address, Btc (1 ), probe)
254- val outputIndex = tx.txOut.indexWhere(_.publicKeyScript == Script .write(Script .pay2wpkh(priv.publicKey)))
255- val (tx1, tx2) = createUnspentTxChain(tx, priv)
256-
257- watcher ! WatchExternalChannelSpent (probe.ref, tx.txid, outputIndex, RealShortChannelId (5 ))
258- watcher ! WatchFundingSpent (probe.ref, tx.txid, outputIndex, Set .empty)
259- probe.expectNoMessage(100 millis)
260-
261- watcher ! ListWatches (probe.ref)
262- assert(probe.expectMsgType[Set [Watch [_]]].size == 2 )
263-
264- bitcoinClient.publishTransaction(tx1)
265- // tx and tx1 aren't confirmed yet, but we trigger the WatchSpentTriggered event when we see tx1 in the mempool.
266- probe.expectMsgAllOf(
267- WatchExternalChannelSpentTriggered (RealShortChannelId (5 ), Some (tx1)),
268- WatchFundingSpentTriggered (tx1)
269- )
270- // Let's confirm tx and tx1: seeing tx1 in a block should trigger both WatchSpentTriggered events again.
271- bitcoinClient.getBlockHeight().pipeTo(probe.ref)
272- val initialBlockHeight = probe.expectMsgType[BlockHeight ]
273- generateBlocks(1 )
274- probe.expectMsgAllOf(
275- WatchExternalChannelSpentTriggered (RealShortChannelId (5 ), Some (tx1)),
276- WatchFundingSpentTriggered (tx1)
277- )
278- probe.expectNoMessage(100 millis)
279-
280- watcher ! ListWatches (probe.ref)
281- val watches1 = probe.expectMsgType[Set [Watch [_]]]
282- assert(watches1.size == 2 )
283- assert(watches1.forall(_.isInstanceOf [WatchSpent [_]]))
284-
285- // Let's submit tx2, and set a watch after it has been confirmed this time.
286- bitcoinClient.publishTransaction(tx2)
287- probe.expectNoMessage(100 millis)
288-
289- system.eventStream.subscribe(probe.ref, classOf [CurrentBlockHeight ])
290- generateBlocks(1 )
291- awaitCond(probe.expectMsgType[CurrentBlockHeight ].blockHeight >= initialBlockHeight + 2 )
292-
293- watcher ! ListWatches (probe.ref)
294- val watches2 = probe.expectMsgType[Set [Watch [_]]]
295- assert(watches2.size == 2 )
296- assert(watches2.forall(_.isInstanceOf [WatchSpent [_]]))
297- watcher ! StopWatching (probe.ref)
298-
299- // We use hints and see if we can find tx2
300- watcher ! WatchFundingSpent (probe.ref, tx1.txid, 0 , Set (tx2.txid))
301- probe.expectMsg(WatchFundingSpentTriggered (tx2))
302- watcher ! StopWatching (probe.ref)
303-
304- // We should still find tx2 if the provided hint is wrong
305- watcher ! WatchOutputSpent (probe.ref, tx1.txid, 0 , tx1.txOut(0 ).amount, Set (randomTxId()))
306- probe.fishForMessage() { case m : WatchOutputSpentTriggered => m.spendingTx.txid == tx2.txid }
307- watcher ! StopWatching (probe.ref)
308-
309- // We should find txs that have already been confirmed
310- watcher ! WatchOutputSpent (probe.ref, tx.txid, outputIndex, tx.txOut(outputIndex).amount, Set .empty)
311- probe.fishForMessage() { case m : WatchOutputSpentTriggered => m.spendingTx.txid == tx1.txid }
312- watcher ! StopWatching (probe.ref)
313-
314- // If we watch after being spent by a confirmed transaction, we immediately trigger the watch without fetching
315- // the spending transaction.
316- watcher ! WatchExternalChannelSpent (probe.ref, tx1.txid, 0 , RealShortChannelId (1 ))
317- probe.expectMsg(WatchExternalChannelSpentTriggered (RealShortChannelId (1 ), None ))
318- watcher ! StopWatching (probe.ref)
319- watcher ! WatchFundingSpent (probe.ref, tx1.txid, 0 , Set .empty)
320- probe.expectMsg(WatchFundingSpentTriggered (tx2))
321- }
322-
323248 test(" watch for spent transactions" ) {
324- withWatcher(f => watchForSpentTransactions(f))
325- }
249+ withWatcher(f => {
250+ import f ._
251+
252+ val (priv, address) = createExternalAddress()
253+ val tx = sendToAddress(address, Btc (1 ), probe)
254+ val outputIndex = tx.txOut.indexWhere(_.publicKeyScript == Script .write(Script .pay2wpkh(priv.publicKey)))
255+ val (tx1, tx2) = createUnspentTxChain(tx, priv)
256+
257+ watcher ! WatchExternalChannelSpent (probe.ref, tx.txid, outputIndex, RealShortChannelId (5 ))
258+ watcher ! WatchFundingSpent (probe.ref, tx.txid, outputIndex, Set .empty)
259+ probe.expectNoMessage(100 millis)
260+
261+ watcher ! ListWatches (probe.ref)
262+ assert(probe.expectMsgType[Set [Watch [_]]].size == 2 )
263+
264+ bitcoinClient.publishTransaction(tx1)
265+ // tx and tx1 aren't confirmed yet, but we trigger the WatchSpentTriggered event when we see tx1 in the mempool.
266+ probe.expectMsgAllOf(
267+ WatchExternalChannelSpentTriggered (RealShortChannelId (5 ), Some (tx1)),
268+ WatchFundingSpentTriggered (tx1)
269+ )
270+ // Let's confirm tx and tx1: seeing tx1 in a block should trigger both WatchSpentTriggered events again.
271+ bitcoinClient.getBlockHeight().pipeTo(probe.ref)
272+ val initialBlockHeight = probe.expectMsgType[BlockHeight ]
273+ generateBlocks(1 )
274+ probe.expectMsgAllOf(
275+ WatchExternalChannelSpentTriggered (RealShortChannelId (5 ), Some (tx1)),
276+ WatchFundingSpentTriggered (tx1)
277+ )
278+ probe.expectNoMessage(100 millis)
326279
327- test(" watch for spent transactions with txospenderindex" ) {
328- withWatcher(f => watchForSpentTransactions(f), useTxoSpenderIndex = true )
280+ watcher ! ListWatches (probe.ref)
281+ val watches1 = probe.expectMsgType[Set [Watch [_]]]
282+ assert(watches1.size == 2 )
283+ assert(watches1.forall(_.isInstanceOf [WatchSpent [_]]))
284+
285+ // Let's submit tx2, and set a watch after it has been confirmed this time.
286+ bitcoinClient.publishTransaction(tx2)
287+ probe.expectNoMessage(100 millis)
288+
289+ system.eventStream.subscribe(probe.ref, classOf [CurrentBlockHeight ])
290+ generateBlocks(1 )
291+ awaitCond(probe.expectMsgType[CurrentBlockHeight ].blockHeight >= initialBlockHeight + 2 )
292+
293+ watcher ! ListWatches (probe.ref)
294+ val watches2 = probe.expectMsgType[Set [Watch [_]]]
295+ assert(watches2.size == 2 )
296+ assert(watches2.forall(_.isInstanceOf [WatchSpent [_]]))
297+ watcher ! StopWatching (probe.ref)
298+
299+ // We use hints and see if we can find tx2
300+ watcher ! WatchFundingSpent (probe.ref, tx1.txid, 0 , Set (tx2.txid))
301+ probe.expectMsg(WatchFundingSpentTriggered (tx2))
302+ watcher ! StopWatching (probe.ref)
303+
304+ // We should still find tx2 if the provided hint is wrong
305+ watcher ! WatchOutputSpent (probe.ref, tx1.txid, 0 , tx1.txOut(0 ).amount, Set (randomTxId()))
306+ probe.fishForMessage() { case m : WatchOutputSpentTriggered => m.spendingTx.txid == tx2.txid }
307+ watcher ! StopWatching (probe.ref)
308+
309+ // We should find txs that have already been confirmed
310+ watcher ! WatchOutputSpent (probe.ref, tx.txid, outputIndex, tx.txOut(outputIndex).amount, Set .empty)
311+ probe.fishForMessage() { case m : WatchOutputSpentTriggered => m.spendingTx.txid == tx1.txid }
312+ watcher ! StopWatching (probe.ref)
313+
314+ // If we watch after being spent by a confirmed transaction, we immediately trigger the watch without fetching
315+ // the spending transaction.
316+ watcher ! WatchExternalChannelSpent (probe.ref, tx1.txid, 0 , RealShortChannelId (1 ))
317+ probe.expectMsg(WatchExternalChannelSpentTriggered (RealShortChannelId (1 ), None ))
318+ watcher ! StopWatching (probe.ref)
319+ watcher ! WatchFundingSpent (probe.ref, tx1.txid, 0 , Set .empty)
320+ probe.expectMsg(WatchFundingSpentTriggered (tx2))
321+ })
329322 }
330323
331324 test(" unwatch external channel" ) {
0 commit comments