2017-11-13 22:56:25 +03:00
|
|
|
package chainntnfs_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
2018-06-05 04:34:16 +03:00
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcutil"
|
2017-11-13 22:56:25 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var zeroHash chainhash.Hash
|
|
|
|
|
|
|
|
// TestTxConfFutureDispatch tests that the TxConfNotifier dispatches
|
|
|
|
// registered notifications when the transaction confirms after registration.
|
|
|
|
func TestTxConfFutureDispatch(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2018-03-19 21:48:44 +03:00
|
|
|
const (
|
|
|
|
tx1NumConfs uint32 = 1
|
|
|
|
tx2NumConfs uint32 = 2
|
|
|
|
)
|
2017-11-13 22:56:25 +03:00
|
|
|
|
|
|
|
var (
|
|
|
|
tx1 = wire.MsgTx{Version: 1}
|
|
|
|
tx2 = wire.MsgTx{Version: 2}
|
|
|
|
tx3 = wire.MsgTx{Version: 3}
|
|
|
|
)
|
|
|
|
|
2018-03-19 21:48:44 +03:00
|
|
|
txConfNotifier := chainntnfs.NewTxConfNotifier(10, 100)
|
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
// Create the test transactions and register them with the
|
|
|
|
// TxConfNotifier before including them in a block to receive future
|
|
|
|
// notifications.
|
2017-11-13 22:56:25 +03:00
|
|
|
tx1Hash := tx1.TxHash()
|
|
|
|
ntfn1 := chainntnfs.ConfNtfn{
|
|
|
|
TxID: &tx1Hash,
|
2018-03-19 21:48:44 +03:00
|
|
|
NumConfirmations: tx1NumConfs,
|
|
|
|
Event: chainntnfs.NewConfirmationEvent(tx1NumConfs),
|
2017-11-13 22:56:25 +03:00
|
|
|
}
|
|
|
|
txConfNotifier.Register(&ntfn1, nil)
|
|
|
|
|
|
|
|
tx2Hash := tx2.TxHash()
|
|
|
|
ntfn2 := chainntnfs.ConfNtfn{
|
|
|
|
TxID: &tx2Hash,
|
2018-03-19 21:48:44 +03:00
|
|
|
NumConfirmations: tx2NumConfs,
|
|
|
|
Event: chainntnfs.NewConfirmationEvent(tx2NumConfs),
|
2017-11-13 22:56:25 +03:00
|
|
|
}
|
|
|
|
txConfNotifier.Register(&ntfn2, nil)
|
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
// We should not receive any notifications from both transactions
|
|
|
|
// since they have not been included in a block yet.
|
2017-11-13 22:56:25 +03:00
|
|
|
select {
|
2018-03-19 22:22:44 +03:00
|
|
|
case <-ntfn1.Event.Updates:
|
|
|
|
t.Fatal("Received unexpected confirmation update for tx1")
|
2017-11-13 22:56:25 +03:00
|
|
|
case txConf := <-ntfn1.Event.Confirmed:
|
|
|
|
t.Fatalf("Received unexpected confirmation for tx1: %v", txConf)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2018-03-19 22:22:44 +03:00
|
|
|
case <-ntfn2.Event.Updates:
|
|
|
|
t.Fatal("Received unexpected confirmation update for tx2")
|
2017-11-13 22:56:25 +03:00
|
|
|
case txConf := <-ntfn2.Event.Confirmed:
|
|
|
|
t.Fatalf("Received unexpected confirmation for tx2: %v", txConf)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
// Include the transactions in a block and add it to the TxConfNotifier.
|
|
|
|
// This should confirm tx1, but not tx2.
|
2017-11-13 22:56:25 +03:00
|
|
|
block1 := btcutil.NewBlock(&wire.MsgBlock{
|
|
|
|
Transactions: []*wire.MsgTx{&tx1, &tx2, &tx3},
|
|
|
|
})
|
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
err := txConfNotifier.ConnectTip(
|
|
|
|
block1.Hash(), 11, block1.Transactions(),
|
|
|
|
)
|
2017-11-13 22:56:25 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to connect block: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
// We should only receive one update for tx1 since it only requires
|
|
|
|
// one confirmation and it already met it.
|
|
|
|
select {
|
|
|
|
case numConfsLeft := <-ntfn1.Event.Updates:
|
|
|
|
const expected = 0
|
|
|
|
if numConfsLeft != expected {
|
|
|
|
t.Fatalf("Received incorrect confirmation update: tx1 "+
|
|
|
|
"expected %d confirmations left, got %d",
|
|
|
|
expected, numConfsLeft)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Fatal("Expected confirmation update for tx1")
|
|
|
|
}
|
|
|
|
|
|
|
|
// A confirmation notification for this tranaction should be dispatched,
|
|
|
|
// as it only required one confirmation.
|
2017-11-13 22:56:25 +03:00
|
|
|
select {
|
|
|
|
case txConf := <-ntfn1.Event.Confirmed:
|
|
|
|
expectedConf := chainntnfs.TxConfirmation{
|
|
|
|
BlockHash: block1.Hash(),
|
|
|
|
BlockHeight: 11,
|
|
|
|
TxIndex: 0,
|
|
|
|
}
|
|
|
|
assertEqualTxConf(t, txConf, &expectedConf)
|
|
|
|
default:
|
|
|
|
t.Fatalf("Expected confirmation for tx1")
|
|
|
|
}
|
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
// We should only receive one update for tx2 since it only has one
|
|
|
|
// confirmation so far and it requires two.
|
|
|
|
select {
|
|
|
|
case numConfsLeft := <-ntfn2.Event.Updates:
|
|
|
|
const expected = 1
|
|
|
|
if numConfsLeft != expected {
|
|
|
|
t.Fatalf("Received incorrect confirmation update: tx2 "+
|
|
|
|
"expected %d confirmations left, got %d",
|
|
|
|
expected, numConfsLeft)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Fatal("Expected confirmation update for tx2")
|
|
|
|
}
|
|
|
|
|
|
|
|
// A confirmation notification for tx2 should not be dispatched yet, as
|
|
|
|
// it requires one more confirmation.
|
2017-11-13 22:56:25 +03:00
|
|
|
select {
|
|
|
|
case txConf := <-ntfn2.Event.Confirmed:
|
|
|
|
t.Fatalf("Received unexpected confirmation for tx2: %v", txConf)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
// Create a new block and add it to the TxConfNotifier at the next
|
|
|
|
// height. This should confirm tx2.
|
2017-11-13 22:56:25 +03:00
|
|
|
block2 := btcutil.NewBlock(&wire.MsgBlock{
|
|
|
|
Transactions: []*wire.MsgTx{&tx3},
|
|
|
|
})
|
|
|
|
|
|
|
|
err = txConfNotifier.ConnectTip(block2.Hash(), 12, block2.Transactions())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to connect block: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
// We should not receive any event notifications for tx1 since it has
|
|
|
|
// already been confirmed.
|
2017-11-13 22:56:25 +03:00
|
|
|
select {
|
2018-03-19 22:22:44 +03:00
|
|
|
case <-ntfn1.Event.Updates:
|
|
|
|
t.Fatal("Received unexpected confirmation update for tx1")
|
2017-11-13 22:56:25 +03:00
|
|
|
case txConf := <-ntfn1.Event.Confirmed:
|
|
|
|
t.Fatalf("Received unexpected confirmation for tx1: %v", txConf)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
// We should only receive one update since the last at the new height,
|
|
|
|
// indicating how many confirmations are still left.
|
|
|
|
select {
|
|
|
|
case numConfsLeft := <-ntfn2.Event.Updates:
|
|
|
|
const expected = 0
|
|
|
|
if numConfsLeft != expected {
|
|
|
|
t.Fatalf("Received incorrect confirmation update: tx2 "+
|
|
|
|
"expected %d confirmations left, got %d",
|
|
|
|
expected, numConfsLeft)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Fatal("Expected confirmation update for tx2")
|
|
|
|
}
|
|
|
|
|
|
|
|
// A confirmation notification for tx2 should be dispatched, since it
|
|
|
|
// now meets its required number of confirmations.
|
2017-11-13 22:56:25 +03:00
|
|
|
select {
|
|
|
|
case txConf := <-ntfn2.Event.Confirmed:
|
|
|
|
expectedConf := chainntnfs.TxConfirmation{
|
|
|
|
BlockHash: block1.Hash(),
|
|
|
|
BlockHeight: 11,
|
|
|
|
TxIndex: 1,
|
|
|
|
}
|
|
|
|
assertEqualTxConf(t, txConf, &expectedConf)
|
|
|
|
default:
|
|
|
|
t.Fatalf("Expected confirmation for tx2")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestTxConfHistoricalDispatch tests that the TxConfNotifier dispatches
|
|
|
|
// registered notifications when the transaction is confirmed before
|
|
|
|
// registration.
|
|
|
|
func TestTxConfHistoricalDispatch(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2018-03-19 21:48:44 +03:00
|
|
|
const (
|
|
|
|
tx1NumConfs uint32 = 1
|
|
|
|
tx2NumConfs uint32 = 3
|
|
|
|
)
|
2017-11-13 22:56:25 +03:00
|
|
|
|
|
|
|
var (
|
|
|
|
tx1 = wire.MsgTx{Version: 1}
|
|
|
|
tx2 = wire.MsgTx{Version: 2}
|
|
|
|
tx3 = wire.MsgTx{Version: 3}
|
|
|
|
)
|
|
|
|
|
2018-03-19 21:48:44 +03:00
|
|
|
txConfNotifier := chainntnfs.NewTxConfNotifier(10, 100)
|
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
// Create the test transactions at a height before the TxConfNotifier's
|
|
|
|
// starting height so that they are confirmed once registering them.
|
2017-11-13 22:56:25 +03:00
|
|
|
tx1Hash := tx1.TxHash()
|
|
|
|
ntfn1 := chainntnfs.ConfNtfn{
|
|
|
|
TxID: &tx1Hash,
|
2018-03-19 21:48:44 +03:00
|
|
|
NumConfirmations: tx1NumConfs,
|
|
|
|
Event: chainntnfs.NewConfirmationEvent(tx1NumConfs),
|
2017-11-13 22:56:25 +03:00
|
|
|
}
|
|
|
|
txConf1 := chainntnfs.TxConfirmation{
|
|
|
|
BlockHash: &zeroHash,
|
|
|
|
BlockHeight: 9,
|
|
|
|
TxIndex: 1,
|
|
|
|
}
|
|
|
|
txConfNotifier.Register(&ntfn1, &txConf1)
|
|
|
|
|
|
|
|
tx2Hash := tx2.TxHash()
|
|
|
|
txConf2 := chainntnfs.TxConfirmation{
|
|
|
|
BlockHash: &zeroHash,
|
|
|
|
BlockHeight: 9,
|
|
|
|
TxIndex: 2,
|
|
|
|
}
|
|
|
|
ntfn2 := chainntnfs.ConfNtfn{
|
|
|
|
TxID: &tx2Hash,
|
2018-03-19 21:48:44 +03:00
|
|
|
NumConfirmations: tx2NumConfs,
|
|
|
|
Event: chainntnfs.NewConfirmationEvent(tx2NumConfs),
|
2017-11-13 22:56:25 +03:00
|
|
|
}
|
|
|
|
txConfNotifier.Register(&ntfn2, &txConf2)
|
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
// We should only receive one update for tx1 since it only requires
|
|
|
|
// one confirmation and it already met it.
|
|
|
|
select {
|
|
|
|
case numConfsLeft := <-ntfn1.Event.Updates:
|
|
|
|
const expected = 0
|
|
|
|
if numConfsLeft != expected {
|
|
|
|
t.Fatalf("Received incorrect confirmation update: tx1 "+
|
|
|
|
"expected %d confirmations left, got %d",
|
|
|
|
expected, numConfsLeft)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Fatal("Expected confirmation update for tx1")
|
|
|
|
}
|
|
|
|
|
|
|
|
// A confirmation notification for tx1 should be dispatched, as it met
|
|
|
|
// its required number of confirmations.
|
2017-11-13 22:56:25 +03:00
|
|
|
select {
|
|
|
|
case txConf := <-ntfn1.Event.Confirmed:
|
|
|
|
assertEqualTxConf(t, txConf, &txConf1)
|
|
|
|
default:
|
|
|
|
t.Fatalf("Expected confirmation for tx1")
|
|
|
|
}
|
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
// We should only receive one update indicating how many confirmations
|
|
|
|
// are left for the transaction to be confirmed.
|
|
|
|
select {
|
|
|
|
case numConfsLeft := <-ntfn2.Event.Updates:
|
|
|
|
const expected = 1
|
|
|
|
if numConfsLeft != expected {
|
|
|
|
t.Fatalf("Received incorrect confirmation update: tx2 "+
|
|
|
|
"expected %d confirmations left, got %d",
|
|
|
|
expected, numConfsLeft)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Fatal("Expected confirmation update for tx2")
|
|
|
|
}
|
|
|
|
|
|
|
|
// A confirmation notification for tx2 should not be dispatched yet, as
|
|
|
|
// it requires one more confirmation.
|
2017-11-13 22:56:25 +03:00
|
|
|
select {
|
|
|
|
case txConf := <-ntfn2.Event.Confirmed:
|
|
|
|
t.Fatalf("Received unexpected confirmation for tx2: %v", txConf)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
// Create a new block and add it to the TxConfNotifier at the next
|
|
|
|
// height. This should confirm tx2.
|
2017-11-13 22:56:25 +03:00
|
|
|
block := btcutil.NewBlock(&wire.MsgBlock{
|
|
|
|
Transactions: []*wire.MsgTx{&tx3},
|
|
|
|
})
|
|
|
|
|
|
|
|
err := txConfNotifier.ConnectTip(block.Hash(), 11, block.Transactions())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to connect block: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
// We should not receive any event notifications for tx1 since it has
|
|
|
|
// already been confirmed.
|
2017-11-13 22:56:25 +03:00
|
|
|
select {
|
2018-03-19 22:22:44 +03:00
|
|
|
case <-ntfn1.Event.Updates:
|
|
|
|
t.Fatal("Received unexpected confirmation update for tx1")
|
2017-11-13 22:56:25 +03:00
|
|
|
case txConf := <-ntfn1.Event.Confirmed:
|
|
|
|
t.Fatalf("Received unexpected confirmation for tx1: %v", txConf)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
// We should only receive one update for tx2 since the last one,
|
|
|
|
// indicating how many confirmations are still left.
|
|
|
|
select {
|
|
|
|
case numConfsLeft := <-ntfn2.Event.Updates:
|
|
|
|
const expected = 0
|
|
|
|
if numConfsLeft != expected {
|
|
|
|
t.Fatalf("Received incorrect confirmation update: tx2 "+
|
|
|
|
"expected %d confirmations left, got %d",
|
|
|
|
expected, numConfsLeft)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Fatal("Expected confirmation update for tx2")
|
|
|
|
}
|
|
|
|
|
|
|
|
// A confirmation notification for tx2 should be dispatched, as it met
|
|
|
|
// its required number of confirmations.
|
2017-11-13 22:56:25 +03:00
|
|
|
select {
|
|
|
|
case txConf := <-ntfn2.Event.Confirmed:
|
|
|
|
assertEqualTxConf(t, txConf, &txConf2)
|
|
|
|
default:
|
|
|
|
t.Fatalf("Expected confirmation for tx2")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestTxConfChainReorg tests that TxConfNotifier dispatches Confirmed and
|
|
|
|
// NegativeConf notifications appropriately when there is a chain
|
|
|
|
// reorganization.
|
|
|
|
func TestTxConfChainReorg(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2018-03-19 21:48:44 +03:00
|
|
|
const (
|
|
|
|
tx1NumConfs uint32 = 2
|
|
|
|
tx2NumConfs uint32 = 1
|
|
|
|
tx3NumConfs uint32 = 2
|
|
|
|
)
|
2017-11-13 22:56:25 +03:00
|
|
|
|
|
|
|
var (
|
|
|
|
tx1 = wire.MsgTx{Version: 1}
|
|
|
|
tx2 = wire.MsgTx{Version: 2}
|
|
|
|
tx3 = wire.MsgTx{Version: 3}
|
|
|
|
)
|
|
|
|
|
2018-03-19 21:48:44 +03:00
|
|
|
txConfNotifier := chainntnfs.NewTxConfNotifier(7, 100)
|
|
|
|
|
2017-11-13 22:56:25 +03:00
|
|
|
// Tx 1 will be confirmed in block 9 and requires 2 confs.
|
|
|
|
tx1Hash := tx1.TxHash()
|
|
|
|
ntfn1 := chainntnfs.ConfNtfn{
|
|
|
|
TxID: &tx1Hash,
|
2018-03-19 21:48:44 +03:00
|
|
|
NumConfirmations: tx1NumConfs,
|
|
|
|
Event: chainntnfs.NewConfirmationEvent(tx1NumConfs),
|
2017-11-13 22:56:25 +03:00
|
|
|
}
|
|
|
|
txConfNotifier.Register(&ntfn1, nil)
|
|
|
|
|
|
|
|
// Tx 2 will be confirmed in block 10 and requires 1 conf.
|
|
|
|
tx2Hash := tx2.TxHash()
|
|
|
|
ntfn2 := chainntnfs.ConfNtfn{
|
|
|
|
TxID: &tx2Hash,
|
2018-03-19 21:48:44 +03:00
|
|
|
NumConfirmations: tx2NumConfs,
|
|
|
|
Event: chainntnfs.NewConfirmationEvent(tx2NumConfs),
|
2017-11-13 22:56:25 +03:00
|
|
|
}
|
|
|
|
txConfNotifier.Register(&ntfn2, nil)
|
|
|
|
|
|
|
|
// Tx 3 will be confirmed in block 10 and requires 2 confs.
|
|
|
|
tx3Hash := tx3.TxHash()
|
|
|
|
ntfn3 := chainntnfs.ConfNtfn{
|
|
|
|
TxID: &tx3Hash,
|
2018-03-19 21:48:44 +03:00
|
|
|
NumConfirmations: tx3NumConfs,
|
|
|
|
Event: chainntnfs.NewConfirmationEvent(tx3NumConfs),
|
2017-11-13 22:56:25 +03:00
|
|
|
}
|
|
|
|
txConfNotifier.Register(&ntfn3, nil)
|
|
|
|
|
|
|
|
// Sync chain to block 10. Txs 1 & 2 should be confirmed.
|
|
|
|
block1 := btcutil.NewBlock(&wire.MsgBlock{
|
|
|
|
Transactions: []*wire.MsgTx{&tx1},
|
|
|
|
})
|
2018-03-19 22:22:44 +03:00
|
|
|
err := txConfNotifier.ConnectTip(nil, 8, block1.Transactions())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to connect block: %v", err)
|
|
|
|
}
|
|
|
|
err = txConfNotifier.ConnectTip(nil, 9, nil)
|
2017-11-13 22:56:25 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to connect block: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
block2 := btcutil.NewBlock(&wire.MsgBlock{
|
|
|
|
Transactions: []*wire.MsgTx{&tx2, &tx3},
|
|
|
|
})
|
|
|
|
err = txConfNotifier.ConnectTip(nil, 10, block2.Transactions())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to connect block: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
// We should receive two updates for tx1 since it requires two
|
|
|
|
// confirmations and it has already met them.
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
select {
|
|
|
|
case <-ntfn1.Event.Updates:
|
|
|
|
default:
|
|
|
|
t.Fatal("Expected confirmation update for tx1")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A confirmation notification for tx1 should be dispatched, as it met
|
|
|
|
// its required number of confirmations.
|
2017-11-13 22:56:25 +03:00
|
|
|
select {
|
|
|
|
case <-ntfn1.Event.Confirmed:
|
|
|
|
default:
|
|
|
|
t.Fatalf("Expected confirmation for tx1")
|
|
|
|
}
|
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
// We should only receive one update for tx2 since it only requires
|
|
|
|
// one confirmation and it already met it.
|
|
|
|
select {
|
|
|
|
case <-ntfn2.Event.Updates:
|
|
|
|
default:
|
|
|
|
t.Fatal("Expected confirmation update for tx2")
|
|
|
|
}
|
|
|
|
|
|
|
|
// A confirmation notification for tx2 should be dispatched, as it met
|
|
|
|
// its required number of confirmations.
|
2017-11-13 22:56:25 +03:00
|
|
|
select {
|
|
|
|
case <-ntfn2.Event.Confirmed:
|
|
|
|
default:
|
|
|
|
t.Fatalf("Expected confirmation for tx2")
|
|
|
|
}
|
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
// We should only receive one update for tx3 since it only has one
|
|
|
|
// confirmation so far and it requires two.
|
|
|
|
select {
|
|
|
|
case <-ntfn3.Event.Updates:
|
|
|
|
default:
|
|
|
|
t.Fatal("Expected confirmation update for tx3")
|
|
|
|
}
|
|
|
|
|
|
|
|
// A confirmation notification for tx3 should not be dispatched yet, as
|
|
|
|
// it requires one more confirmation.
|
2017-11-13 22:56:25 +03:00
|
|
|
select {
|
|
|
|
case txConf := <-ntfn3.Event.Confirmed:
|
|
|
|
t.Fatalf("Received unexpected confirmation for tx3: %v", txConf)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
// The block that included tx2 and tx3 is disconnected and two next
|
2017-11-13 22:56:25 +03:00
|
|
|
// blocks without them are connected.
|
|
|
|
err = txConfNotifier.DisconnectTip(10)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to connect block: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = txConfNotifier.ConnectTip(nil, 10, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to connect block: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = txConfNotifier.ConnectTip(nil, 11, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to connect block: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-11-14 04:32:11 +03:00
|
|
|
select {
|
|
|
|
case reorgDepth := <-ntfn2.Event.NegativeConf:
|
|
|
|
if reorgDepth != 1 {
|
|
|
|
t.Fatalf("Incorrect value for negative conf notification: "+
|
|
|
|
"expected %d, got %d", 1, reorgDepth)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Fatalf("Expected negative conf notification for tx1")
|
|
|
|
}
|
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
// We should not receive any event notifications from all of the
|
|
|
|
// transactions because tx1 has already been confirmed and tx2 and tx3
|
|
|
|
// have not been included in the chain since the reorg.
|
2017-11-13 22:56:25 +03:00
|
|
|
select {
|
2018-03-19 22:22:44 +03:00
|
|
|
case <-ntfn1.Event.Updates:
|
|
|
|
t.Fatal("Received unexpected confirmation update for tx1")
|
2017-11-13 22:56:25 +03:00
|
|
|
case txConf := <-ntfn1.Event.Confirmed:
|
|
|
|
t.Fatalf("Received unexpected confirmation for tx1: %v", txConf)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2018-03-19 22:22:44 +03:00
|
|
|
case <-ntfn2.Event.Updates:
|
|
|
|
t.Fatal("Received unexpected confirmation update for tx2")
|
2017-11-13 22:56:25 +03:00
|
|
|
case txConf := <-ntfn2.Event.Confirmed:
|
|
|
|
t.Fatalf("Received unexpected confirmation for tx2: %v", txConf)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2018-03-19 22:22:44 +03:00
|
|
|
case <-ntfn3.Event.Updates:
|
|
|
|
t.Fatal("Received unexpected confirmation update for tx3")
|
2017-11-13 22:56:25 +03:00
|
|
|
case txConf := <-ntfn3.Event.Confirmed:
|
|
|
|
t.Fatalf("Received unexpected confirmation for tx3: %v", txConf)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now transactions 2 & 3 are re-included in a new block.
|
|
|
|
block3 := btcutil.NewBlock(&wire.MsgBlock{
|
|
|
|
Transactions: []*wire.MsgTx{&tx2, &tx3},
|
|
|
|
})
|
|
|
|
block4 := btcutil.NewBlock(&wire.MsgBlock{})
|
|
|
|
|
|
|
|
err = txConfNotifier.ConnectTip(block3.Hash(), 12, block3.Transactions())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to connect block: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = txConfNotifier.ConnectTip(block4.Hash(), 13, block4.Transactions())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to connect block: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
// We should only receive one update for tx2 since it only requires
|
|
|
|
// one confirmation and it already met it.
|
|
|
|
select {
|
|
|
|
case numConfsLeft := <-ntfn2.Event.Updates:
|
|
|
|
const expected = 0
|
|
|
|
if numConfsLeft != expected {
|
|
|
|
t.Fatalf("Received incorrect confirmation update: tx2 "+
|
|
|
|
"expected %d confirmations left, got %d",
|
|
|
|
expected, numConfsLeft)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Fatal("Expected confirmation update for tx2")
|
|
|
|
}
|
|
|
|
|
|
|
|
// A confirmation notification for tx2 should be dispatched, as it met
|
|
|
|
// its required number of confirmations.
|
2017-11-13 22:56:25 +03:00
|
|
|
select {
|
|
|
|
case txConf := <-ntfn2.Event.Confirmed:
|
|
|
|
expectedConf := chainntnfs.TxConfirmation{
|
|
|
|
BlockHash: block3.Hash(),
|
|
|
|
BlockHeight: 12,
|
|
|
|
TxIndex: 0,
|
|
|
|
}
|
|
|
|
assertEqualTxConf(t, txConf, &expectedConf)
|
|
|
|
default:
|
|
|
|
t.Fatalf("Expected confirmation for tx2")
|
|
|
|
}
|
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
// We should receive two updates for tx3 since it requires two
|
|
|
|
// confirmations and it has already met them.
|
|
|
|
for i := uint32(1); i <= 2; i++ {
|
|
|
|
select {
|
|
|
|
case numConfsLeft := <-ntfn3.Event.Updates:
|
|
|
|
expected := tx3NumConfs - i
|
|
|
|
if numConfsLeft != expected {
|
|
|
|
t.Fatalf("Received incorrect confirmation update: tx3 "+
|
|
|
|
"expected %d confirmations left, got %d",
|
|
|
|
expected, numConfsLeft)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Fatal("Expected confirmation update for tx2")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A confirmation notification for tx3 should be dispatched, as it met
|
|
|
|
// its required number of confirmations.
|
2017-11-13 22:56:25 +03:00
|
|
|
select {
|
|
|
|
case txConf := <-ntfn3.Event.Confirmed:
|
|
|
|
expectedConf := chainntnfs.TxConfirmation{
|
|
|
|
BlockHash: block3.Hash(),
|
|
|
|
BlockHeight: 12,
|
|
|
|
TxIndex: 1,
|
|
|
|
}
|
|
|
|
assertEqualTxConf(t, txConf, &expectedConf)
|
|
|
|
default:
|
|
|
|
t.Fatalf("Expected confirmation for tx3")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTxConfTearDown(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
var (
|
|
|
|
tx1 = wire.MsgTx{Version: 1}
|
|
|
|
tx2 = wire.MsgTx{Version: 2}
|
|
|
|
)
|
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
txConfNotifier := chainntnfs.NewTxConfNotifier(10, 100)
|
|
|
|
|
|
|
|
// Create the test transactions and register them with the
|
|
|
|
// TxConfNotifier to receive notifications.
|
2017-11-13 22:56:25 +03:00
|
|
|
tx1Hash := tx1.TxHash()
|
|
|
|
ntfn1 := chainntnfs.ConfNtfn{
|
|
|
|
TxID: &tx1Hash,
|
|
|
|
NumConfirmations: 1,
|
2018-03-19 22:22:44 +03:00
|
|
|
Event: chainntnfs.NewConfirmationEvent(1),
|
2017-11-13 22:56:25 +03:00
|
|
|
}
|
|
|
|
txConfNotifier.Register(&ntfn1, nil)
|
|
|
|
|
|
|
|
tx2Hash := tx2.TxHash()
|
|
|
|
ntfn2 := chainntnfs.ConfNtfn{
|
|
|
|
TxID: &tx2Hash,
|
|
|
|
NumConfirmations: 2,
|
2018-03-19 22:22:44 +03:00
|
|
|
Event: chainntnfs.NewConfirmationEvent(2),
|
2017-11-13 22:56:25 +03:00
|
|
|
}
|
|
|
|
txConfNotifier.Register(&ntfn2, nil)
|
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
// Include the transactions in a block and add it to the TxConfNotifier.
|
|
|
|
// This should confirm tx1, but not tx2.
|
2017-11-13 22:56:25 +03:00
|
|
|
block := btcutil.NewBlock(&wire.MsgBlock{
|
|
|
|
Transactions: []*wire.MsgTx{&tx1, &tx2},
|
|
|
|
})
|
|
|
|
|
|
|
|
err := txConfNotifier.ConnectTip(block.Hash(), 11, block.Transactions())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to connect block: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
// We do not care about the correctness of the notifications since they
|
|
|
|
// are tested in other methods, but we'll still attempt to retrieve them
|
|
|
|
// for the sake of not being able to later once the notification
|
|
|
|
// channels are closed.
|
|
|
|
select {
|
|
|
|
case <-ntfn1.Event.Updates:
|
|
|
|
default:
|
|
|
|
t.Fatal("Expected confirmation update for tx1")
|
|
|
|
}
|
|
|
|
|
2017-11-13 22:56:25 +03:00
|
|
|
select {
|
|
|
|
case <-ntfn1.Event.Confirmed:
|
|
|
|
default:
|
|
|
|
t.Fatalf("Expected confirmation for tx1")
|
|
|
|
}
|
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
select {
|
|
|
|
case <-ntfn2.Event.Updates:
|
|
|
|
default:
|
|
|
|
t.Fatal("Expected confirmation update for tx2")
|
|
|
|
}
|
|
|
|
|
2017-11-13 22:56:25 +03:00
|
|
|
select {
|
|
|
|
case txConf := <-ntfn2.Event.Confirmed:
|
|
|
|
t.Fatalf("Received unexpected confirmation for tx2: %v", txConf)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
// The notification channels should be closed for notifications that
|
|
|
|
// have not been dispatched yet, so we should not expect to receive any
|
|
|
|
// more updates.
|
2017-11-13 22:56:25 +03:00
|
|
|
txConfNotifier.TearDown()
|
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
// tx1 should not receive any more updates because it has already been
|
|
|
|
// confirmed and the TxConfNotifier has been shut down.
|
2017-11-13 22:56:25 +03:00
|
|
|
select {
|
2018-03-19 22:22:44 +03:00
|
|
|
case <-ntfn1.Event.Updates:
|
|
|
|
t.Fatal("Received unexpected confirmation update for tx1")
|
2017-11-13 22:56:25 +03:00
|
|
|
case txConf := <-ntfn1.Event.Confirmed:
|
|
|
|
t.Fatalf("Received unexpected confirmation for tx1: %v", txConf)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
// tx2 should not receive any more updates after the notifications
|
|
|
|
// channels have been closed and the TxConfNotifier shut down.
|
2017-11-13 22:56:25 +03:00
|
|
|
select {
|
2018-03-19 22:22:44 +03:00
|
|
|
case _, more := <-ntfn2.Event.Updates:
|
|
|
|
if more {
|
|
|
|
t.Fatal("Expected closed Updates channel for tx2")
|
|
|
|
}
|
2017-11-13 22:56:25 +03:00
|
|
|
case _, more := <-ntfn2.Event.Confirmed:
|
|
|
|
if more {
|
2018-03-19 22:22:44 +03:00
|
|
|
t.Fatalf("Expected closed Confirmed channel for tx2")
|
2017-11-13 22:56:25 +03:00
|
|
|
}
|
|
|
|
default:
|
2018-03-19 22:22:44 +03:00
|
|
|
t.Fatalf("Expected closed notification channels for tx2")
|
2017-11-13 22:56:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertEqualTxConf(t *testing.T,
|
|
|
|
actualConf, expectedConf *chainntnfs.TxConfirmation) {
|
|
|
|
|
|
|
|
if actualConf.BlockHeight != expectedConf.BlockHeight {
|
|
|
|
t.Fatalf("Incorrect block height in confirmation details: "+
|
|
|
|
"expected %d, got %d",
|
|
|
|
expectedConf.BlockHeight, actualConf.BlockHeight)
|
|
|
|
}
|
|
|
|
if !actualConf.BlockHash.IsEqual(expectedConf.BlockHash) {
|
|
|
|
t.Fatalf("Incorrect block hash in confirmation details: "+
|
|
|
|
"expected %d, got %d", expectedConf.BlockHash, actualConf.BlockHash)
|
|
|
|
}
|
|
|
|
if actualConf.TxIndex != expectedConf.TxIndex {
|
|
|
|
t.Fatalf("Incorrect tx index in confirmation details: "+
|
|
|
|
"expected %d, got %d", expectedConf.TxIndex, actualConf.TxIndex)
|
|
|
|
}
|
|
|
|
}
|