@@ -271,13 +271,12 @@ func TestTxNotifierFutureConfDispatch(t *testing.T) {
271271 // We should only receive one update for tx1 since it only requires
272272 // one confirmation and it already met it.
273273 select {
274- case numConfsLeft := <- ntfn1 .Event .Updates :
275- const expected = 0
276- if numConfsLeft != expected {
277- t .Fatalf ("Received incorrect confirmation update: tx1 " +
278- "expected %d confirmations left, got %d" ,
279- expected , numConfsLeft )
274+ case updDetails := <- ntfn1 .Event .Updates :
275+ expected := chainntnfs.TxUpdateInfo {
276+ NumConfsLeft : 0 ,
277+ BlockHeight : 11 ,
280278 }
279+ require .Equal (t , expected , updDetails )
281280 default :
282281 t .Fatal ("Expected confirmation update for tx1" )
283282 }
@@ -300,13 +299,12 @@ func TestTxNotifierFutureConfDispatch(t *testing.T) {
300299 // We should only receive one update for tx2 since it only has one
301300 // confirmation so far and it requires two.
302301 select {
303- case numConfsLeft := <- ntfn2 .Event .Updates :
304- const expected = 1
305- if numConfsLeft != expected {
306- t .Fatalf ("Received incorrect confirmation update: tx2 " +
307- "expected %d confirmations left, got %d" ,
308- expected , numConfsLeft )
302+ case updDetails := <- ntfn2 .Event .Updates :
303+ expected := chainntnfs.TxUpdateInfo {
304+ NumConfsLeft : 1 ,
305+ BlockHeight : 11 ,
309306 }
307+ require .Equal (t , expected , updDetails )
310308 default :
311309 t .Fatal ("Expected confirmation update for tx2" )
312310 }
@@ -341,13 +339,12 @@ func TestTxNotifierFutureConfDispatch(t *testing.T) {
341339 // We should only receive one update since the last at the new height,
342340 // indicating how many confirmations are still left.
343341 select {
344- case numConfsLeft := <- ntfn2 .Event .Updates :
345- const expected = 0
346- if numConfsLeft != expected {
347- t .Fatalf ("Received incorrect confirmation update: tx2 " +
348- "expected %d confirmations left, got %d" ,
349- expected , numConfsLeft )
342+ case updDetails := <- ntfn2 .Event .Updates :
343+ expected := chainntnfs.TxUpdateInfo {
344+ NumConfsLeft : 0 ,
345+ BlockHeight : 11 ,
350346 }
347+ require .Equal (t , expected , updDetails )
351348 default :
352349 t .Fatal ("Expected confirmation update for tx2" )
353350 }
@@ -411,13 +408,12 @@ func TestTxNotifierHistoricalConfDispatch(t *testing.T) {
411408 err = n .UpdateConfDetails (ntfn1 .HistoricalDispatch .ConfRequest , & txConf1 )
412409 require .NoError (t , err , "unable to update conf details" )
413410 select {
414- case numConfsLeft := <- ntfn1 .Event .Updates :
415- const expected = 0
416- if numConfsLeft != expected {
417- t .Fatalf ("Received incorrect confirmation update: tx1 " +
418- "expected %d confirmations left, got %d" ,
419- expected , numConfsLeft )
411+ case updDetails := <- ntfn1 .Event .Updates :
412+ expected := chainntnfs.TxUpdateInfo {
413+ NumConfsLeft : 0 ,
414+ BlockHeight : 9 ,
420415 }
416+ require .Equal (t , expected , updDetails )
421417 default :
422418 t .Fatal ("Expected confirmation update for tx1" )
423419 }
@@ -443,13 +439,12 @@ func TestTxNotifierHistoricalConfDispatch(t *testing.T) {
443439 err = n .UpdateConfDetails (ntfn2 .HistoricalDispatch .ConfRequest , & txConf2 )
444440 require .NoError (t , err , "unable to update conf details" )
445441 select {
446- case numConfsLeft := <- ntfn2 .Event .Updates :
447- const expected = 1
448- if numConfsLeft != expected {
449- t .Fatalf ("Received incorrect confirmation update: tx2 " +
450- "expected %d confirmations left, got %d" ,
451- expected , numConfsLeft )
442+ case updDetails := <- ntfn2 .Event .Updates :
443+ expected := chainntnfs.TxUpdateInfo {
444+ NumConfsLeft : 1 ,
445+ BlockHeight : 9 ,
452446 }
447+ require .Equal (t , expected , updDetails )
453448 default :
454449 t .Fatal ("Expected confirmation update for tx2" )
455450 }
@@ -485,13 +480,12 @@ func TestTxNotifierHistoricalConfDispatch(t *testing.T) {
485480 // We should only receive one update for tx2 since the last one,
486481 // indicating how many confirmations are still left.
487482 select {
488- case numConfsLeft := <- ntfn2 .Event .Updates :
489- const expected = 0
490- if numConfsLeft != expected {
491- t .Fatalf ("Received incorrect confirmation update: tx2 " +
492- "expected %d confirmations left, got %d" ,
493- expected , numConfsLeft )
483+ case updDetails := <- ntfn2 .Event .Updates :
484+ expected := chainntnfs.TxUpdateInfo {
485+ NumConfsLeft : 0 ,
486+ BlockHeight : 9 ,
494487 }
488+ require .Equal (t , expected , updDetails )
495489 default :
496490 t .Fatal ("Expected confirmation update for tx2" )
497491 }
@@ -1490,13 +1484,12 @@ func TestTxNotifierConfReorg(t *testing.T) {
14901484 // We should only receive one update for tx2 since it only requires
14911485 // one confirmation and it already met it.
14921486 select {
1493- case numConfsLeft := <- ntfn2 .Event .Updates :
1494- const expected = 0
1495- if numConfsLeft != expected {
1496- t .Fatalf ("Received incorrect confirmation update: tx2 " +
1497- "expected %d confirmations left, got %d" ,
1498- expected , numConfsLeft )
1487+ case updDetails := <- ntfn2 .Event .Updates :
1488+ expected := chainntnfs.TxUpdateInfo {
1489+ NumConfsLeft : 0 ,
1490+ BlockHeight : 12 ,
14991491 }
1492+ require .Equal (t , expected , updDetails )
15001493 default :
15011494 t .Fatal ("Expected confirmation update for tx2" )
15021495 }
@@ -1520,15 +1513,14 @@ func TestTxNotifierConfReorg(t *testing.T) {
15201513 // confirmations and it has already met them.
15211514 for i := uint32 (1 ); i <= 2 ; i ++ {
15221515 select {
1523- case numConfsLeft := <- ntfn3 .Event .Updates :
1524- expected := tx3NumConfs - i
1525- if numConfsLeft != expected {
1526- t .Fatalf ("Received incorrect confirmation update: tx3 " +
1527- "expected %d confirmations left, got %d" ,
1528- expected , numConfsLeft )
1516+ case updDetails := <- ntfn3 .Event .Updates :
1517+ expected := chainntnfs.TxUpdateInfo {
1518+ NumConfsLeft : tx3NumConfs - i ,
1519+ BlockHeight : 12 ,
15291520 }
1521+ require .Equal (t , expected , updDetails )
15301522 default :
1531- t .Fatal ("Expected confirmation update for tx2 " )
1523+ t .Fatal ("Expected confirmation update for tx3 " )
15321524 }
15331525 }
15341526
@@ -1548,6 +1540,56 @@ func TestTxNotifierConfReorg(t *testing.T) {
15481540 }
15491541}
15501542
1543+ // TestTxNotifierReorgPartialConfirmation ensures that a tx with intermediate
1544+ // confirmations handles a reorg correctly and emits the appropriate reorg ntfn.
1545+ func TestTxNotifierReorgPartialConfirmation (t * testing.T ) {
1546+ t .Parallel ()
1547+
1548+ const txNumConfs uint32 = 2
1549+ hintCache := newMockHintCache ()
1550+ n := chainntnfs .NewTxNotifier (
1551+ 7 , chainntnfs .ReorgSafetyLimit , hintCache , hintCache ,
1552+ )
1553+
1554+ // Tx will be confirmed in block 9 and requires 2 confs.
1555+ tx := wire.MsgTx {Version : 1 }
1556+ tx .AddTxOut (& wire.TxOut {PkScript : testRawScript })
1557+ txHash := tx .TxHash ()
1558+ ntfn , err := n .RegisterConf (& txHash , testRawScript , txNumConfs , 1 )
1559+ require .NoError (t , err , "unable to register ntfn" )
1560+
1561+ err = n .UpdateConfDetails (ntfn .HistoricalDispatch .ConfRequest , nil )
1562+ require .NoError (t , err , "unable to deliver conf details" )
1563+
1564+ // Mine 1 block to satisfy the requirement for a partially confirmed tx.
1565+ block := btcutil .NewBlock (& wire.MsgBlock {
1566+ Transactions : []* wire.MsgTx {& tx },
1567+ })
1568+ err = n .ConnectTip (block , 8 )
1569+ require .NoError (t , err , "failed to connect block" )
1570+ err = n .NotifyHeight (8 )
1571+ require .NoError (t , err , "unable to dispatch notifications" )
1572+
1573+ // Now that the transaction is partially confirmed, reorg out those
1574+ // blocks.
1575+ err = n .DisconnectTip (8 )
1576+ require .NoError (t , err , "unable to disconnect block" )
1577+
1578+ // After the intermediate confirmation is reorged out, the tx should not
1579+ // trigger a confirmation ntfn, but should trigger a reorg ntfn.
1580+ select {
1581+ case <- ntfn .Event .Confirmed :
1582+ t .Fatal ("unexpected confirmation after reorg" )
1583+ default :
1584+ }
1585+
1586+ select {
1587+ case <- ntfn .Event .NegativeConf :
1588+ default :
1589+ t .Fatal ("expected to receive reorg notification" )
1590+ }
1591+ }
1592+
15511593// TestTxNotifierSpendReorg ensures that clients are notified of a reorg when
15521594// the spending transaction of an outpoint for which they registered a spend
15531595// notification for has been reorged out of the chain.
0 commit comments