From 486694a84e1c0dde60345174650adedbe78dff52 Mon Sep 17 00:00:00 2001 From: Wilmer Paulino Date: Mon, 19 Mar 2018 14:48:44 -0400 Subject: [PATCH] chainntnfs: add Updates channel field to ConfirmationEvent In this commit, we add a new Updates channel to our ConfirmationEvent struct. This channel will be used to deliver updates to a subscriber of a confirmation notification. Updates will be delivered at every incremental height of the chain with the number of confirmations remaining for the transaction to be considered confirmed by the subscriber. --- chainntnfs/bitcoindnotify/bitcoind.go | 2 +- chainntnfs/btcdnotify/btcd.go | 2 +- chainntnfs/interface.go | 11 ++++-- chainntnfs/neutrinonotify/neutrino.go | 2 +- chainntnfs/txconfnotifier.go | 4 ++- chainntnfs/txconfnotifier_test.go | 50 ++++++++++++++++++--------- 6 files changed, 48 insertions(+), 23 deletions(-) diff --git a/chainntnfs/bitcoindnotify/bitcoind.go b/chainntnfs/bitcoindnotify/bitcoind.go index 5897d06f..7233389a 100644 --- a/chainntnfs/bitcoindnotify/bitcoind.go +++ b/chainntnfs/bitcoindnotify/bitcoind.go @@ -605,7 +605,7 @@ func (b *BitcoindNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash, chainntnfs.ConfNtfn{ TxID: txid, NumConfirmations: numConfs, - Event: chainntnfs.NewConfirmationEvent(), + Event: chainntnfs.NewConfirmationEvent(numConfs), }, } diff --git a/chainntnfs/btcdnotify/btcd.go b/chainntnfs/btcdnotify/btcd.go index a93726e2..a7c87c65 100644 --- a/chainntnfs/btcdnotify/btcd.go +++ b/chainntnfs/btcdnotify/btcd.go @@ -607,7 +607,7 @@ func (b *BtcdNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash, chainntnfs.ConfNtfn{ TxID: txid, NumConfirmations: numConfs, - Event: chainntnfs.NewConfirmationEvent(), + Event: chainntnfs.NewConfirmationEvent(numConfs), }, } diff --git a/chainntnfs/interface.go b/chainntnfs/interface.go index d589578b..9854d30a 100644 --- a/chainntnfs/interface.go +++ b/chainntnfs/interface.go @@ -85,8 +85,10 @@ type TxConfirmation struct { // ConfirmationEvent encapsulates a confirmation notification. With this struct, // callers can be notified of: the instance the target txid reaches the targeted -// number of confirmations, and also in the event that the original txid becomes -// disconnected from the blockchain as a result of a re-org. +// number of confirmations, how many confirmations are left for the target txid +// to be fully confirmed at every new block height, and also in the event that +// the original txid becomes disconnected from the blockchain as a result of a +// re-org. // // Once the txid reaches the specified number of confirmations, the 'Confirmed' // channel will be sent upon fulfilling the notification. @@ -100,6 +102,11 @@ type ConfirmationEvent struct { // details of the channel's confirmation. Confirmed chan *TxConfirmation // MUST be buffered. + // Updates is a channel that will sent upon, at every incremental + // confirmation, how many confirmations are left to declare the + // transaction as fully confirmed. + Updates chan uint32 // MUST be buffered. + // TODO(roasbeef): all goroutines on ln channel updates should also // have a struct chan that's closed if funding gets re-org out. Need // to sync, to request another confirmation event ntfn, then re-open diff --git a/chainntnfs/neutrinonotify/neutrino.go b/chainntnfs/neutrinonotify/neutrino.go index e347e6d8..d9b0828e 100644 --- a/chainntnfs/neutrinonotify/neutrino.go +++ b/chainntnfs/neutrinonotify/neutrino.go @@ -698,7 +698,7 @@ func (n *NeutrinoNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash, ConfNtfn: chainntnfs.ConfNtfn{ TxID: txid, NumConfirmations: numConfs, - Event: chainntnfs.NewConfirmationEvent(), + Event: chainntnfs.NewConfirmationEvent(numConfs), }, heightHint: heightHint, } diff --git a/chainntnfs/txconfnotifier.go b/chainntnfs/txconfnotifier.go index 6d941553..f5d37e46 100644 --- a/chainntnfs/txconfnotifier.go +++ b/chainntnfs/txconfnotifier.go @@ -33,9 +33,10 @@ type ConfNtfn struct { // NewConfirmationEvent constructs a new ConfirmationEvent with newly opened // channels. -func NewConfirmationEvent() *ConfirmationEvent { +func NewConfirmationEvent(numConfs uint32) *ConfirmationEvent { return &ConfirmationEvent{ Confirmed: make(chan *TxConfirmation, 1), + Updates: make(chan uint32, numConfs), NegativeConf: make(chan int32, 1), } } @@ -290,6 +291,7 @@ func (tcn *TxConfNotifier) TearDown() { } close(ntfn.Event.Confirmed) + close(ntfn.Event.Updates) close(ntfn.Event.NegativeConf) } } diff --git a/chainntnfs/txconfnotifier_test.go b/chainntnfs/txconfnotifier_test.go index d60b5227..2677671f 100644 --- a/chainntnfs/txconfnotifier_test.go +++ b/chainntnfs/txconfnotifier_test.go @@ -16,7 +16,10 @@ var zeroHash chainhash.Hash func TestTxConfFutureDispatch(t *testing.T) { t.Parallel() - txConfNotifier := chainntnfs.NewTxConfNotifier(10, 100) + const ( + tx1NumConfs uint32 = 1 + tx2NumConfs uint32 = 2 + ) var ( tx1 = wire.MsgTx{Version: 1} @@ -24,19 +27,21 @@ func TestTxConfFutureDispatch(t *testing.T) { tx3 = wire.MsgTx{Version: 3} ) + txConfNotifier := chainntnfs.NewTxConfNotifier(10, 100) + tx1Hash := tx1.TxHash() ntfn1 := chainntnfs.ConfNtfn{ TxID: &tx1Hash, - NumConfirmations: 1, - Event: chainntnfs.NewConfirmationEvent(), + NumConfirmations: tx1NumConfs, + Event: chainntnfs.NewConfirmationEvent(tx1NumConfs), } txConfNotifier.Register(&ntfn1, nil) tx2Hash := tx2.TxHash() ntfn2 := chainntnfs.ConfNtfn{ TxID: &tx2Hash, - NumConfirmations: 2, - Event: chainntnfs.NewConfirmationEvent(), + NumConfirmations: tx2NumConfs, + Event: chainntnfs.NewConfirmationEvent(tx2NumConfs), } txConfNotifier.Register(&ntfn2, nil) @@ -113,7 +118,10 @@ func TestTxConfFutureDispatch(t *testing.T) { func TestTxConfHistoricalDispatch(t *testing.T) { t.Parallel() - txConfNotifier := chainntnfs.NewTxConfNotifier(10, 100) + const ( + tx1NumConfs uint32 = 1 + tx2NumConfs uint32 = 3 + ) var ( tx1 = wire.MsgTx{Version: 1} @@ -121,11 +129,13 @@ func TestTxConfHistoricalDispatch(t *testing.T) { tx3 = wire.MsgTx{Version: 3} ) + txConfNotifier := chainntnfs.NewTxConfNotifier(10, 100) + tx1Hash := tx1.TxHash() ntfn1 := chainntnfs.ConfNtfn{ TxID: &tx1Hash, - NumConfirmations: 1, - Event: chainntnfs.NewConfirmationEvent(), + NumConfirmations: tx1NumConfs, + Event: chainntnfs.NewConfirmationEvent(tx1NumConfs), } txConf1 := chainntnfs.TxConfirmation{ BlockHash: &zeroHash, @@ -142,8 +152,8 @@ func TestTxConfHistoricalDispatch(t *testing.T) { } ntfn2 := chainntnfs.ConfNtfn{ TxID: &tx2Hash, - NumConfirmations: 3, - Event: chainntnfs.NewConfirmationEvent(), + NumConfirmations: tx2NumConfs, + Event: chainntnfs.NewConfirmationEvent(tx2NumConfs), } txConfNotifier.Register(&ntfn2, &txConf2) @@ -189,7 +199,11 @@ func TestTxConfHistoricalDispatch(t *testing.T) { func TestTxConfChainReorg(t *testing.T) { t.Parallel() - txConfNotifier := chainntnfs.NewTxConfNotifier(8, 100) + const ( + tx1NumConfs uint32 = 2 + tx2NumConfs uint32 = 1 + tx3NumConfs uint32 = 2 + ) var ( tx1 = wire.MsgTx{Version: 1} @@ -197,12 +211,14 @@ func TestTxConfChainReorg(t *testing.T) { tx3 = wire.MsgTx{Version: 3} ) + txConfNotifier := chainntnfs.NewTxConfNotifier(7, 100) + // Tx 1 will be confirmed in block 9 and requires 2 confs. tx1Hash := tx1.TxHash() ntfn1 := chainntnfs.ConfNtfn{ TxID: &tx1Hash, - NumConfirmations: 2, - Event: chainntnfs.NewConfirmationEvent(), + NumConfirmations: tx1NumConfs, + Event: chainntnfs.NewConfirmationEvent(tx1NumConfs), } txConfNotifier.Register(&ntfn1, nil) @@ -210,8 +226,8 @@ func TestTxConfChainReorg(t *testing.T) { tx2Hash := tx2.TxHash() ntfn2 := chainntnfs.ConfNtfn{ TxID: &tx2Hash, - NumConfirmations: 1, - Event: chainntnfs.NewConfirmationEvent(), + NumConfirmations: tx2NumConfs, + Event: chainntnfs.NewConfirmationEvent(tx2NumConfs), } txConfNotifier.Register(&ntfn2, nil) @@ -219,8 +235,8 @@ func TestTxConfChainReorg(t *testing.T) { tx3Hash := tx3.TxHash() ntfn3 := chainntnfs.ConfNtfn{ TxID: &tx3Hash, - NumConfirmations: 2, - Event: chainntnfs.NewConfirmationEvent(), + NumConfirmations: tx3NumConfs, + Event: chainntnfs.NewConfirmationEvent(tx3NumConfs), } txConfNotifier.Register(&ntfn3, nil)