txnotifier test: add test for confirmation after cancellation

This test addition would cause the txnotifier to crash prior to the
previous commit.
This commit is contained in:
Johan T. Halseth 2020-09-16 10:51:50 +02:00
parent 2665836fa3
commit df6606d37f
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"sync" "sync"
"testing" "testing"
"time"
"github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
@ -1138,7 +1139,7 @@ func TestTxNotifierCancelConf(t *testing.T) {
hintCache := newMockHintCache() hintCache := newMockHintCache()
n := chainntnfs.NewTxNotifier(startingHeight, 100, hintCache, hintCache) n := chainntnfs.NewTxNotifier(startingHeight, 100, hintCache, hintCache)
// We'll register three notification requests. The last two will be // We'll register four notification requests. The last three will be
// canceled. // canceled.
tx1 := wire.NewMsgTx(1) tx1 := wire.NewMsgTx(1)
tx1.AddTxOut(&wire.TxOut{PkScript: testRawScript}) tx1.AddTxOut(&wire.TxOut{PkScript: testRawScript})
@ -1160,6 +1161,12 @@ func TestTxNotifierCancelConf(t *testing.T) {
t.Fatalf("unable to register spend ntfn: %v", err) t.Fatalf("unable to register spend ntfn: %v", err)
} }
// This request will have a three block num confs.
ntfn4, err := n.RegisterConf(&tx2Hash, testRawScript, 3, 1)
if err != nil {
t.Fatalf("unable to register spend ntfn: %v", err)
}
// Extend the chain with a block that will confirm both transactions. // Extend the chain with a block that will confirm both transactions.
// This will queue confirmation notifications to dispatch once their // This will queue confirmation notifications to dispatch once their
// respective heights have been met. // respective heights have been met.
@ -1217,6 +1224,54 @@ func TestTxNotifierCancelConf(t *testing.T) {
default: default:
t.Fatal("expected Confirmed channel to be closed") t.Fatal("expected Confirmed channel to be closed")
} }
// Connect yet another block.
block1 := btcutil.NewBlock(&wire.MsgBlock{
Transactions: []*wire.MsgTx{},
})
err = n.ConnectTip(block1.Hash(), startingHeight+2, block1.Transactions())
if err != nil {
t.Fatalf("unable to connect block: %v", err)
}
if err := n.NotifyHeight(startingHeight + 2); err != nil {
t.Fatalf("unable to dispatch notifications: %v", err)
}
// Since neither it reached the set confirmation height or was
// canceled, nothing should happen to ntfn4 in this block.
select {
case <-ntfn4.Event.Confirmed:
t.Fatal("expected nothing to happen")
case <-time.After(10 * time.Millisecond):
}
// Now cancel the notification.
ntfn4.Event.Cancel()
select {
case _, ok := <-ntfn4.Event.Confirmed:
if ok {
t.Fatal("expected Confirmed channel to be closed")
}
default:
t.Fatal("expected Confirmed channel to be closed")
}
// Finally, confirm a block that would trigger ntfn4 confirmation
// hadn't it already been canceled.
block2 := btcutil.NewBlock(&wire.MsgBlock{
Transactions: []*wire.MsgTx{},
})
err = n.ConnectTip(block2.Hash(), startingHeight+3, block2.Transactions())
if err != nil {
t.Fatalf("unable to connect block: %v", err)
}
if err := n.NotifyHeight(startingHeight + 3); err != nil {
t.Fatalf("unable to dispatch notifications: %v", err)
}
} }
// TestTxNotifierCancelSpend ensures that a spend notification after a client // TestTxNotifierCancelSpend ensures that a spend notification after a client