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.
This commit is contained in:
Wilmer Paulino 2018-03-19 14:48:44 -04:00
parent 706cc02639
commit 486694a84e
No known key found for this signature in database
GPG Key ID: 6DF57B9F9514972F
6 changed files with 48 additions and 23 deletions

@ -605,7 +605,7 @@ func (b *BitcoindNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash,
chainntnfs.ConfNtfn{
TxID: txid,
NumConfirmations: numConfs,
Event: chainntnfs.NewConfirmationEvent(),
Event: chainntnfs.NewConfirmationEvent(numConfs),
},
}

@ -607,7 +607,7 @@ func (b *BtcdNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash,
chainntnfs.ConfNtfn{
TxID: txid,
NumConfirmations: numConfs,
Event: chainntnfs.NewConfirmationEvent(),
Event: chainntnfs.NewConfirmationEvent(numConfs),
},
}

@ -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

@ -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,
}

@ -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)
}
}

@ -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)