2017-11-13 22:56:25 +03:00
|
|
|
package chainntnfs_test
|
|
|
|
|
|
|
|
import (
|
2018-05-22 22:55:32 +03:00
|
|
|
"sync"
|
2017-11-13 22:56:25 +03:00
|
|
|
"testing"
|
|
|
|
|
2018-06-05 04:34:16 +03:00
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcutil"
|
2018-07-27 07:31:32 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
2017-11-13 22:56:25 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var zeroHash chainhash.Hash
|
|
|
|
|
2018-05-22 22:55:32 +03:00
|
|
|
type mockHintCache struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
confHints map[chainhash.Hash]uint32
|
|
|
|
spendHints map[wire.OutPoint]uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ chainntnfs.SpendHintCache = (*mockHintCache)(nil)
|
|
|
|
var _ chainntnfs.ConfirmHintCache = (*mockHintCache)(nil)
|
|
|
|
|
|
|
|
func (c *mockHintCache) CommitSpendHint(heightHint uint32, ops ...wire.OutPoint) error {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
|
|
|
for _, op := range ops {
|
|
|
|
c.spendHints[op] = heightHint
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *mockHintCache) QuerySpendHint(op wire.OutPoint) (uint32, error) {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
|
|
|
hint, ok := c.spendHints[op]
|
|
|
|
if !ok {
|
|
|
|
return 0, chainntnfs.ErrSpendHintNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return hint, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *mockHintCache) PurgeSpendHint(ops ...wire.OutPoint) error {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
|
|
|
for _, op := range ops {
|
|
|
|
delete(c.spendHints, op)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *mockHintCache) CommitConfirmHint(heightHint uint32, txids ...chainhash.Hash) error {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
|
|
|
for _, txid := range txids {
|
|
|
|
c.confHints[txid] = heightHint
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *mockHintCache) QueryConfirmHint(txid chainhash.Hash) (uint32, error) {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
|
|
|
hint, ok := c.confHints[txid]
|
|
|
|
if !ok {
|
|
|
|
return 0, chainntnfs.ErrConfirmHintNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return hint, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *mockHintCache) PurgeConfirmHint(txids ...chainhash.Hash) error {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
|
|
|
for _, txid := range txids {
|
|
|
|
delete(c.confHints, txid)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockHintCache() *mockHintCache {
|
|
|
|
return &mockHintCache{
|
|
|
|
confHints: make(map[chainhash.Hash]uint32),
|
|
|
|
spendHints: make(map[wire.OutPoint]uint32),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 12:07:55 +03:00
|
|
|
// TestTxConfFutureDispatch tests that the TxNotifier dispatches registered
|
|
|
|
// notifications when the transaction confirms after registration.
|
2017-11-13 22:56:25 +03:00
|
|
|
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-05-22 22:55:32 +03:00
|
|
|
hintCache := newMockHintCache()
|
2018-10-05 12:07:55 +03:00
|
|
|
n := chainntnfs.NewTxNotifier(10, 100, hintCache)
|
2018-03-19 21:48:44 +03:00
|
|
|
|
2018-10-05 12:07:55 +03:00
|
|
|
// Create the test transactions and register them with the TxNotifier
|
|
|
|
// before including them in a block to receive future
|
2018-03-19 22:22:44 +03:00
|
|
|
// 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
|
|
|
}
|
2018-10-05 12:07:55 +03:00
|
|
|
if _, err := n.RegisterConf(&ntfn1); err != nil {
|
2018-07-27 07:31:32 +03:00
|
|
|
t.Fatalf("unable to register ntfn: %v", err)
|
|
|
|
}
|
2017-11-13 22:56:25 +03:00
|
|
|
|
|
|
|
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
|
|
|
}
|
2018-10-05 12:07:55 +03:00
|
|
|
if _, err := n.RegisterConf(&ntfn2); err != nil {
|
2018-07-27 07:31:32 +03:00
|
|
|
t.Fatalf("unable to register ntfn: %v", err)
|
|
|
|
}
|
2017-11-13 22:56:25 +03:00
|
|
|
|
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-10-05 12:07:55 +03:00
|
|
|
// Include the transactions in a block and add it to the TxNotifier.
|
2018-03-19 22:22:44 +03:00
|
|
|
// 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-10-05 12:07:55 +03:00
|
|
|
err := n.ConnectTip(
|
2018-03-19 22:22:44 +03:00
|
|
|
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-10-05 12:07:55 +03:00
|
|
|
// Create a new block and add it to the TxNotifier 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},
|
|
|
|
})
|
|
|
|
|
2018-10-05 12:07:55 +03:00
|
|
|
err = n.ConnectTip(block2.Hash(), 12, block2.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 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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 12:07:55 +03:00
|
|
|
// TestTxConfHistoricalDispatch tests that the TxNotifier dispatches registered
|
|
|
|
// notifications when the transaction is confirmed before registration.
|
2017-11-13 22:56:25 +03:00
|
|
|
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-05-22 22:55:32 +03:00
|
|
|
hintCache := newMockHintCache()
|
2018-10-05 12:07:55 +03:00
|
|
|
n := chainntnfs.NewTxNotifier(10, 100, hintCache)
|
2018-03-19 21:48:44 +03:00
|
|
|
|
2018-10-05 12:07:55 +03:00
|
|
|
// Create the test transactions at a height before the TxNotifier's
|
2018-03-19 22:22:44 +03:00
|
|
|
// starting height so that they are confirmed once registering them.
|
2017-11-13 22:56:25 +03:00
|
|
|
tx1Hash := tx1.TxHash()
|
|
|
|
ntfn1 := chainntnfs.ConfNtfn{
|
2018-07-27 07:31:32 +03:00
|
|
|
ConfID: 0,
|
2017-11-13 22:56:25 +03:00
|
|
|
TxID: &tx1Hash,
|
2018-03-19 21:48:44 +03:00
|
|
|
NumConfirmations: tx1NumConfs,
|
|
|
|
Event: chainntnfs.NewConfirmationEvent(tx1NumConfs),
|
2017-11-13 22:56:25 +03:00
|
|
|
}
|
2018-10-05 12:07:55 +03:00
|
|
|
if _, err := n.RegisterConf(&ntfn1); err != nil {
|
2018-07-27 07:31:32 +03:00
|
|
|
t.Fatalf("unable to register ntfn: %v", err)
|
2017-11-13 22:56:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
tx2Hash := tx2.TxHash()
|
|
|
|
ntfn2 := chainntnfs.ConfNtfn{
|
2018-07-27 07:31:32 +03:00
|
|
|
ConfID: 1,
|
2017-11-13 22:56:25 +03:00
|
|
|
TxID: &tx2Hash,
|
2018-03-19 21:48:44 +03:00
|
|
|
NumConfirmations: tx2NumConfs,
|
|
|
|
Event: chainntnfs.NewConfirmationEvent(tx2NumConfs),
|
2017-11-13 22:56:25 +03:00
|
|
|
}
|
2018-10-05 12:07:55 +03:00
|
|
|
if _, err := n.RegisterConf(&ntfn2); err != nil {
|
2018-07-27 07:31:32 +03:00
|
|
|
t.Fatalf("unable to register ntfn: %v", err)
|
|
|
|
}
|
2017-11-13 22:56:25 +03:00
|
|
|
|
2018-07-27 07:31:32 +03:00
|
|
|
// Update tx1 with its confirmation details. We should only receive one
|
|
|
|
// update since it only requires one confirmation and it already met it.
|
|
|
|
txConf1 := chainntnfs.TxConfirmation{
|
|
|
|
BlockHash: &zeroHash,
|
|
|
|
BlockHeight: 9,
|
|
|
|
TxIndex: 1,
|
|
|
|
}
|
2018-10-05 12:07:55 +03:00
|
|
|
err := n.UpdateConfDetails(tx1Hash, &txConf1)
|
2018-07-27 07:31:32 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to update conf details: %v", err)
|
|
|
|
}
|
2018-03-19 22:22:44 +03:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2018-07-27 07:31:32 +03:00
|
|
|
// A confirmation notification for tx1 should also be dispatched.
|
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-07-27 07:31:32 +03:00
|
|
|
// Update tx2 with its confirmation details. This should not trigger a
|
|
|
|
// confirmation notification since it hasn't reached its required number
|
|
|
|
// of confirmations, but we should receive a confirmation update
|
|
|
|
// indicating how many confirmation are left.
|
|
|
|
txConf2 := chainntnfs.TxConfirmation{
|
|
|
|
BlockHash: &zeroHash,
|
|
|
|
BlockHeight: 9,
|
|
|
|
TxIndex: 2,
|
|
|
|
}
|
2018-10-05 12:07:55 +03:00
|
|
|
err = n.UpdateConfDetails(tx2Hash, &txConf2)
|
2018-07-27 07:31:32 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to update conf details: %v", err)
|
|
|
|
}
|
2018-03-19 22:22:44 +03:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
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-10-05 12:07:55 +03:00
|
|
|
// Create a new block and add it to the TxNotifier 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},
|
|
|
|
})
|
|
|
|
|
2018-10-05 12:07:55 +03:00
|
|
|
err = n.ConnectTip(block.Hash(), 11, block.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 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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 12:07:55 +03:00
|
|
|
// TestTxConfChainReorg tests that TxNotifier dispatches Confirmed and
|
2017-11-13 22:56:25 +03:00
|
|
|
// 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-05-22 22:55:32 +03:00
|
|
|
hintCache := newMockHintCache()
|
2018-10-05 12:07:55 +03:00
|
|
|
n := chainntnfs.NewTxNotifier(7, 100, hintCache)
|
2018-03-19 21:48:44 +03:00
|
|
|
|
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
|
|
|
}
|
2018-10-05 12:07:55 +03:00
|
|
|
if _, err := n.RegisterConf(&ntfn1); err != nil {
|
2018-07-27 07:31:32 +03:00
|
|
|
t.Fatalf("unable to register ntfn: %v", err)
|
|
|
|
}
|
2017-11-13 22:56:25 +03:00
|
|
|
|
2018-10-05 12:07:55 +03:00
|
|
|
if err := n.UpdateConfDetails(*ntfn1.TxID, nil); err != nil {
|
2018-08-25 05:23:54 +03:00
|
|
|
t.Fatalf("unable to deliver conf details: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-11-13 22:56:25 +03:00
|
|
|
// 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
|
|
|
}
|
2018-10-05 12:07:55 +03:00
|
|
|
if _, err := n.RegisterConf(&ntfn2); err != nil {
|
2018-07-27 07:31:32 +03:00
|
|
|
t.Fatalf("unable to register ntfn: %v", err)
|
|
|
|
}
|
2017-11-13 22:56:25 +03:00
|
|
|
|
2018-10-05 12:07:55 +03:00
|
|
|
if err := n.UpdateConfDetails(*ntfn2.TxID, nil); err != nil {
|
2018-08-25 05:23:54 +03:00
|
|
|
t.Fatalf("unable to deliver conf details: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-11-13 22:56:25 +03:00
|
|
|
// 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
|
|
|
}
|
2018-10-05 12:07:55 +03:00
|
|
|
if _, err := n.RegisterConf(&ntfn3); err != nil {
|
2018-07-27 07:31:32 +03:00
|
|
|
t.Fatalf("unable to register ntfn: %v", err)
|
|
|
|
}
|
2017-11-13 22:56:25 +03:00
|
|
|
|
2018-10-05 12:07:55 +03:00
|
|
|
if err := n.UpdateConfDetails(*ntfn3.TxID, nil); err != nil {
|
2018-08-25 05:23:54 +03:00
|
|
|
t.Fatalf("unable to deliver conf details: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-11-13 22:56:25 +03:00
|
|
|
// Sync chain to block 10. Txs 1 & 2 should be confirmed.
|
|
|
|
block1 := btcutil.NewBlock(&wire.MsgBlock{
|
|
|
|
Transactions: []*wire.MsgTx{&tx1},
|
|
|
|
})
|
2018-10-05 12:07:55 +03:00
|
|
|
err := n.ConnectTip(nil, 8, block1.Transactions())
|
2018-03-19 22:22:44 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to connect block: %v", err)
|
|
|
|
}
|
2018-10-05 12:07:55 +03:00
|
|
|
err = n.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},
|
|
|
|
})
|
2018-10-05 12:07:55 +03:00
|
|
|
err = n.ConnectTip(nil, 10, block2.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 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.
|
2018-10-05 12:07:55 +03:00
|
|
|
err = n.DisconnectTip(10)
|
2017-11-13 22:56:25 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to connect block: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-10-05 12:07:55 +03:00
|
|
|
err = n.ConnectTip(nil, 10, nil)
|
2017-11-13 22:56:25 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to connect block: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-10-05 12:07:55 +03:00
|
|
|
err = n.ConnectTip(nil, 11, nil)
|
2017-11-13 22:56:25 +03:00
|
|
|
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{})
|
|
|
|
|
2018-10-05 12:07:55 +03:00
|
|
|
err = n.ConnectTip(block3.Hash(), 12, block3.Transactions())
|
2017-11-13 22:56:25 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to connect block: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-10-05 12:07:55 +03:00
|
|
|
err = n.ConnectTip(block4.Hash(), 13, block4.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 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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-22 22:55:32 +03:00
|
|
|
// TestTxConfHeightHintCache ensures that the height hints for transactions are
|
2018-08-25 05:15:37 +03:00
|
|
|
// kept track of correctly with each new block connected/disconnected. This test
|
|
|
|
// also asserts that the height hints are not updated until the simulated
|
|
|
|
// historical dispatches have returned, and we know the transactions aren't
|
|
|
|
// already in the chain.
|
2018-05-22 22:55:32 +03:00
|
|
|
func TestTxConfHeightHintCache(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
const (
|
2018-08-25 05:15:37 +03:00
|
|
|
startingHeight = 200
|
|
|
|
txDummyHeight = 201
|
|
|
|
tx1Height = 202
|
|
|
|
tx2Height = 203
|
2018-05-22 22:55:32 +03:00
|
|
|
)
|
|
|
|
|
2018-10-05 12:07:55 +03:00
|
|
|
// Initialize our TxNotifier instance backed by a height hint cache.
|
2018-05-22 22:55:32 +03:00
|
|
|
hintCache := newMockHintCache()
|
2018-10-05 12:07:55 +03:00
|
|
|
n := chainntnfs.NewTxNotifier(
|
2018-05-22 22:55:32 +03:00
|
|
|
startingHeight, 100, hintCache,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Create two test transactions and register them for notifications.
|
|
|
|
tx1 := wire.MsgTx{Version: 1}
|
|
|
|
tx1Hash := tx1.TxHash()
|
|
|
|
ntfn1 := &chainntnfs.ConfNtfn{
|
|
|
|
TxID: &tx1Hash,
|
|
|
|
NumConfirmations: 1,
|
|
|
|
Event: chainntnfs.NewConfirmationEvent(1),
|
|
|
|
}
|
|
|
|
|
|
|
|
tx2 := wire.MsgTx{Version: 2}
|
|
|
|
tx2Hash := tx2.TxHash()
|
|
|
|
ntfn2 := &chainntnfs.ConfNtfn{
|
|
|
|
TxID: &tx2Hash,
|
|
|
|
NumConfirmations: 2,
|
|
|
|
Event: chainntnfs.NewConfirmationEvent(2),
|
|
|
|
}
|
|
|
|
|
2018-10-05 12:07:55 +03:00
|
|
|
if _, err := n.RegisterConf(ntfn1); err != nil {
|
2018-05-22 22:55:32 +03:00
|
|
|
t.Fatalf("unable to register tx1: %v", err)
|
|
|
|
}
|
2018-10-05 12:07:55 +03:00
|
|
|
if _, err := n.RegisterConf(ntfn2); err != nil {
|
2018-05-22 22:55:32 +03:00
|
|
|
t.Fatalf("unable to register tx2: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-10-05 12:07:55 +03:00
|
|
|
// Both transactions should not have a height hint set, as RegisterConf
|
2018-08-25 05:15:37 +03:00
|
|
|
// should not alter the cache state.
|
|
|
|
_, err := hintCache.QueryConfirmHint(tx1Hash)
|
|
|
|
if err != chainntnfs.ErrConfirmHintNotFound {
|
|
|
|
t.Fatalf("unexpected error when querying for height hint "+
|
|
|
|
"want: %v, got %v",
|
|
|
|
chainntnfs.ErrConfirmHintNotFound, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = hintCache.QueryConfirmHint(tx2Hash)
|
|
|
|
if err != chainntnfs.ErrConfirmHintNotFound {
|
|
|
|
t.Fatalf("unexpected error when querying for height hint "+
|
|
|
|
"want: %v, got %v",
|
|
|
|
chainntnfs.ErrConfirmHintNotFound, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new block that will include the dummy transaction and extend
|
|
|
|
// the chain.
|
|
|
|
txDummy := wire.MsgTx{Version: 3}
|
|
|
|
block1 := btcutil.NewBlock(&wire.MsgBlock{
|
|
|
|
Transactions: []*wire.MsgTx{&txDummy},
|
|
|
|
})
|
|
|
|
|
2018-10-05 12:07:55 +03:00
|
|
|
err = n.ConnectTip(
|
2018-08-25 05:15:37 +03:00
|
|
|
block1.Hash(), txDummyHeight, block1.Transactions(),
|
|
|
|
)
|
2018-05-22 22:55:32 +03:00
|
|
|
if err != nil {
|
2018-08-25 05:15:37 +03:00
|
|
|
t.Fatalf("Failed to connect block: %v", err)
|
2018-05-22 22:55:32 +03:00
|
|
|
}
|
2018-08-25 05:15:37 +03:00
|
|
|
|
|
|
|
// Since UpdateConfDetails has not been called for either transaction,
|
|
|
|
// the height hints should remain unchanged. This simulates blocks
|
|
|
|
// confirming while the historical dispatch is processing the
|
|
|
|
// registration.
|
|
|
|
hint, err := hintCache.QueryConfirmHint(tx1Hash)
|
|
|
|
if err != chainntnfs.ErrConfirmHintNotFound {
|
|
|
|
t.Fatalf("unexpected error when querying for height hint "+
|
|
|
|
"want: %v, got %v",
|
|
|
|
chainntnfs.ErrConfirmHintNotFound, err)
|
2018-05-22 22:55:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
hint, err = hintCache.QueryConfirmHint(tx2Hash)
|
2018-08-25 05:15:37 +03:00
|
|
|
if err != chainntnfs.ErrConfirmHintNotFound {
|
|
|
|
t.Fatalf("unexpected error when querying for height hint "+
|
|
|
|
"want: %v, got %v",
|
|
|
|
chainntnfs.ErrConfirmHintNotFound, err)
|
2018-05-22 22:55:32 +03:00
|
|
|
}
|
2018-08-25 05:15:37 +03:00
|
|
|
|
|
|
|
// Now, update the conf details reporting that the neither txn was found
|
|
|
|
// in the historical dispatch.
|
2018-10-05 12:07:55 +03:00
|
|
|
if err := n.UpdateConfDetails(tx1Hash, nil); err != nil {
|
2018-08-25 05:15:37 +03:00
|
|
|
t.Fatalf("unable to update conf details: %v", err)
|
|
|
|
}
|
2018-10-05 12:07:55 +03:00
|
|
|
if err := n.UpdateConfDetails(tx2Hash, nil); err != nil {
|
2018-08-25 05:15:37 +03:00
|
|
|
t.Fatalf("unable to update conf details: %v", err)
|
2018-05-22 22:55:32 +03:00
|
|
|
}
|
|
|
|
|
2018-08-25 05:15:37 +03:00
|
|
|
// We'll create another block that will include the first transaction
|
|
|
|
// and extend the chain.
|
|
|
|
block2 := btcutil.NewBlock(&wire.MsgBlock{
|
2018-05-22 22:55:32 +03:00
|
|
|
Transactions: []*wire.MsgTx{&tx1},
|
|
|
|
})
|
|
|
|
|
2018-10-05 12:07:55 +03:00
|
|
|
err = n.ConnectTip(
|
2018-08-25 05:15:37 +03:00
|
|
|
block2.Hash(), tx1Height, block2.Transactions(),
|
2018-05-22 22:55:32 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to connect block: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-08-25 05:15:37 +03:00
|
|
|
// Now that both notifications are waiting at tip for confirmations,
|
|
|
|
// they should have their height hints updated to the latest block
|
|
|
|
// height.
|
2018-05-22 22:55:32 +03:00
|
|
|
hint, err = hintCache.QueryConfirmHint(tx1Hash)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for hint: %v", err)
|
|
|
|
}
|
|
|
|
if hint != tx1Height {
|
2018-08-25 05:15:37 +03:00
|
|
|
t.Fatalf("expected hint %d, got %d",
|
|
|
|
tx1Height, hint)
|
2018-05-22 22:55:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
hint, err = hintCache.QueryConfirmHint(tx2Hash)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for hint: %v", err)
|
|
|
|
}
|
|
|
|
if hint != tx1Height {
|
2018-08-25 05:15:37 +03:00
|
|
|
t.Fatalf("expected hint %d, got %d",
|
|
|
|
tx2Height, hint)
|
2018-05-22 22:55:32 +03:00
|
|
|
}
|
|
|
|
|
2018-08-25 05:15:37 +03:00
|
|
|
// Next, we'll create another block that will include the second
|
2018-05-22 22:55:32 +03:00
|
|
|
// transaction and extend the chain.
|
2018-08-25 05:15:37 +03:00
|
|
|
block3 := btcutil.NewBlock(&wire.MsgBlock{
|
2018-05-22 22:55:32 +03:00
|
|
|
Transactions: []*wire.MsgTx{&tx2},
|
|
|
|
})
|
|
|
|
|
2018-10-05 12:07:55 +03:00
|
|
|
err = n.ConnectTip(
|
2018-08-25 05:15:37 +03:00
|
|
|
block3.Hash(), tx2Height, block3.Transactions(),
|
2018-05-22 22:55:32 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to connect block: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The height hint for the first transaction should remain the same.
|
|
|
|
hint, err = hintCache.QueryConfirmHint(tx1Hash)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for hint: %v", err)
|
|
|
|
}
|
|
|
|
if hint != tx1Height {
|
2018-08-25 05:15:37 +03:00
|
|
|
t.Fatalf("expected hint %d, got %d",
|
|
|
|
tx1Height, hint)
|
2018-05-22 22:55:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// The height hint for the second transaction should now be updated to
|
|
|
|
// reflect its confirmation.
|
|
|
|
hint, err = hintCache.QueryConfirmHint(tx2Hash)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for hint: %v", err)
|
|
|
|
}
|
|
|
|
if hint != tx2Height {
|
2018-08-25 05:15:37 +03:00
|
|
|
t.Fatalf("expected hint %d, got %d",
|
|
|
|
tx2Height, hint)
|
2018-05-22 22:55:32 +03:00
|
|
|
}
|
|
|
|
|
2018-08-25 05:15:37 +03:00
|
|
|
// Finally, we'll attempt do disconnect the last block in order to
|
|
|
|
// simulate a chain reorg.
|
2018-10-05 12:07:55 +03:00
|
|
|
if err := n.DisconnectTip(tx2Height); err != nil {
|
2018-05-22 22:55:32 +03:00
|
|
|
t.Fatalf("Failed to disconnect block: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should update the second transaction's height hint within the
|
|
|
|
// cache to the previous height.
|
|
|
|
hint, err = hintCache.QueryConfirmHint(tx2Hash)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for hint: %v", err)
|
|
|
|
}
|
|
|
|
if hint != tx1Height {
|
2018-08-25 05:15:37 +03:00
|
|
|
t.Fatalf("expected hint %d, got %d",
|
|
|
|
tx1Height, hint)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The first transaction's height hint should remain at the original
|
|
|
|
// confirmation height.
|
|
|
|
hint, err = hintCache.QueryConfirmHint(tx2Hash)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to query for hint: %v", err)
|
|
|
|
}
|
|
|
|
if hint != tx1Height {
|
|
|
|
t.Fatalf("expected hint %d, got %d",
|
|
|
|
tx1Height, hint)
|
2018-05-22 22:55:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-13 22:56:25 +03:00
|
|
|
func TestTxConfTearDown(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
var (
|
|
|
|
tx1 = wire.MsgTx{Version: 1}
|
|
|
|
tx2 = wire.MsgTx{Version: 2}
|
|
|
|
)
|
|
|
|
|
2018-05-22 22:55:32 +03:00
|
|
|
hintCache := newMockHintCache()
|
2018-10-05 12:07:55 +03:00
|
|
|
n := chainntnfs.NewTxNotifier(10, 100, hintCache)
|
2018-03-19 22:22:44 +03:00
|
|
|
|
2018-10-05 12:07:55 +03:00
|
|
|
// Create the test transactions and register them with the TxNotifier 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
|
|
|
}
|
2018-10-05 12:07:55 +03:00
|
|
|
if _, err := n.RegisterConf(&ntfn1); err != nil {
|
2018-07-27 07:31:32 +03:00
|
|
|
t.Fatalf("unable to register ntfn: %v", err)
|
|
|
|
}
|
2018-10-05 12:07:55 +03:00
|
|
|
if err := n.UpdateConfDetails(*ntfn1.TxID, nil); err != nil {
|
2018-08-25 05:23:54 +03:00
|
|
|
t.Fatalf("unable to update conf details: %v", err)
|
|
|
|
}
|
2017-11-13 22:56:25 +03:00
|
|
|
|
|
|
|
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
|
|
|
}
|
2018-10-05 12:07:55 +03:00
|
|
|
if _, err := n.RegisterConf(&ntfn2); err != nil {
|
2018-07-27 07:31:32 +03:00
|
|
|
t.Fatalf("unable to register ntfn: %v", err)
|
|
|
|
}
|
2018-10-05 12:07:55 +03:00
|
|
|
if err := n.UpdateConfDetails(*ntfn2.TxID, nil); err != nil {
|
2018-08-25 05:23:54 +03:00
|
|
|
t.Fatalf("unable to update conf details: %v", err)
|
|
|
|
}
|
2017-11-13 22:56:25 +03:00
|
|
|
|
2018-10-05 12:07:55 +03:00
|
|
|
// Include the transactions in a block and add it to the TxNotifier.
|
2018-03-19 22:22:44 +03:00
|
|
|
// 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},
|
|
|
|
})
|
|
|
|
|
2018-10-05 12:07:55 +03:00
|
|
|
err := n.ConnectTip(block.Hash(), 11, block.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 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.
|
2018-10-05 12:07:55 +03:00
|
|
|
n.TearDown()
|
2017-11-13 22:56:25 +03:00
|
|
|
|
2018-03-19 22:22:44 +03:00
|
|
|
// tx1 should not receive any more updates because it has already been
|
2018-10-05 12:07:55 +03:00
|
|
|
// confirmed and the TxNotifier 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
|
2018-10-05 12:07:55 +03:00
|
|
|
// channels have been closed and the TxNotifier 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)
|
|
|
|
}
|
|
|
|
}
|