Skip to content

Commit 3802428

Browse files
chainntnfs: notify BlockHeight in Updates event
Enhance the ConfirmationEvent's Updates channel by including the BlockHeight alongside NumConfsLeft. Signed-off-by: Nishant Bansal <[email protected]>
1 parent ee25c22 commit 3802428

File tree

3 files changed

+147
-49
lines changed

3 files changed

+147
-49
lines changed

chainntnfs/interface.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,20 @@ type TxConfirmation struct {
203203
Block *wire.MsgBlock
204204
}
205205

206+
// TxUpdateInfo contains information about a transaction before it has reached
207+
// its required number of confirmations. Transactions are registered for
208+
// notification for a specific number of "required" confirmations, this struct
209+
// will update the caller incrementally as long as the transaction is not yet
210+
// fully regarded as confirmed.
211+
type TxUpdateInfo struct {
212+
// BlockHeight is the height of the block that contains the transaction.
213+
BlockHeight uint32
214+
215+
// NumConfsLeft is the number of confirmations left for the transaction
216+
// to be regarded as fully confirmed.
217+
NumConfsLeft uint32
218+
}
219+
206220
// ConfirmationEvent encapsulates a confirmation notification. With this struct,
207221
// callers can be notified of: the instance the target txid reaches the targeted
208222
// number of confirmations, how many confirmations are left for the target txid
@@ -229,11 +243,12 @@ type ConfirmationEvent struct {
229243

230244
// Updates is a channel that will sent upon, at every incremental
231245
// confirmation, how many confirmations are left to declare the
232-
// transaction as fully confirmed.
246+
// transaction as fully confirmed, along with the height of the block
247+
// that contains the transaction.
233248
//
234249
// NOTE: This channel must be buffered with the number of required
235250
// confirmations.
236-
Updates chan uint32
251+
Updates chan TxUpdateInfo
237252

238253
// NegativeConf is a channel that will be sent upon if the transaction
239254
// confirms, but is later reorged out of the chain. The integer sent
@@ -262,7 +277,7 @@ func NewConfirmationEvent(numConfs uint32, cancel func()) *ConfirmationEvent {
262277
// the channel so we need to create a larger buffer to avoid
263278
// blocking the notifier.
264279
Confirmed: make(chan *TxConfirmation, 1),
265-
Updates: make(chan uint32, numConfs),
280+
Updates: make(chan TxUpdateInfo, numConfs),
266281
NegativeConf: make(chan int32, 1),
267282
Done: make(chan struct{}, 1),
268283
Cancel: cancel,

chainntnfs/txnotifier.go

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -947,8 +947,12 @@ func (n *TxNotifier) dispatchConfDetails(
947947

948948
// We'll send a 0 value to the Updates channel,
949949
// indicating that the transaction/output script has already
950-
// been confirmed.
951-
err := n.notifyNumConfsLeft(ntfn, 0)
950+
// been confirmed, and include the block height at which the
951+
// transaction was included.
952+
err := n.notifyNumConfsLeft(ntfn, TxUpdateInfo{
953+
NumConfsLeft: 0,
954+
BlockHeight: details.BlockHeight,
955+
})
952956
if err != nil {
953957
return err
954958
}
@@ -977,7 +981,10 @@ func (n *TxNotifier) dispatchConfDetails(
977981
// confirmations are left for the transaction/output script to
978982
// be confirmed.
979983
numConfsLeft := confHeight - n.currentHeight
980-
err := n.notifyNumConfsLeft(ntfn, numConfsLeft)
984+
err := n.notifyNumConfsLeft(ntfn, TxUpdateInfo{
985+
NumConfsLeft: numConfsLeft,
986+
BlockHeight: details.BlockHeight,
987+
})
981988
if err != nil {
982989
return err
983990
}
@@ -1731,7 +1738,10 @@ func (n *TxNotifier) NotifyHeight(height uint32) error {
17311738
for confRequest := range confRequests {
17321739
confSet := n.confNotifications[confRequest]
17331740
for _, ntfn := range confSet.ntfns {
1734-
txConfHeight := confSet.details.BlockHeight +
1741+
// blockHeight is the height of the block which
1742+
// contains the transaction.
1743+
blockHeight := confSet.details.BlockHeight
1744+
txConfHeight := blockHeight +
17351745
ntfn.NumConfirmations - 1
17361746
numConfsLeft := txConfHeight - height
17371747

@@ -1744,7 +1754,10 @@ func (n *TxNotifier) NotifyHeight(height uint32) error {
17441754
continue
17451755
}
17461756

1747-
err := n.notifyNumConfsLeft(ntfn, numConfsLeft)
1757+
err := n.notifyNumConfsLeft(ntfn, TxUpdateInfo{
1758+
NumConfsLeft: numConfsLeft,
1759+
BlockHeight: blockHeight,
1760+
})
17481761
if err != nil {
17491762
return err
17501763
}
@@ -2099,25 +2112,28 @@ func (n *TxNotifier) TearDown() {
20992112
}
21002113

21012114
// notifyNumConfsLeft sends the number of confirmations left to the
2102-
// notification subscriber through the Event.Updates channel.
2115+
// notification subscriber through the Event.Updates channel, along with the
2116+
// block height in which the transaction was included.
21032117
//
21042118
// NOTE: must be used with the TxNotifier's lock held.
2105-
func (n *TxNotifier) notifyNumConfsLeft(ntfn *ConfNtfn, num uint32) error {
2119+
func (n *TxNotifier) notifyNumConfsLeft(ntfn *ConfNtfn,
2120+
info TxUpdateInfo) error {
2121+
21062122
// If the number left is no less than the recorded value, we can skip
21072123
// sending it as it means this same value has already been sent before.
2108-
if num >= ntfn.numConfsLeft {
2124+
if info.NumConfsLeft >= ntfn.numConfsLeft {
21092125
Log.Debugf("Skipped dispatched update (numConfsLeft=%v) for "+
2110-
"request %v conf_id=%v", num, ntfn.ConfRequest,
2111-
ntfn.ConfID)
2126+
"request %v conf_id=%v", info.NumConfsLeft,
2127+
ntfn.ConfRequest, ntfn.ConfID)
21122128

21132129
return nil
21142130
}
21152131

21162132
// Update the number of confirmations left to the notification.
2117-
ntfn.numConfsLeft = num
2133+
ntfn.numConfsLeft = info.NumConfsLeft
21182134

21192135
select {
2120-
case ntfn.Event.Updates <- num:
2136+
case ntfn.Event.Updates <- info:
21212137
case <-n.quit:
21222138
return ErrTxNotifierExiting
21232139
}

chainntnfs/txnotifier_test.go

Lines changed: 101 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -271,12 +271,20 @@ func TestTxNotifierFutureConfDispatch(t *testing.T) {
271271
// We should only receive one update for tx1 since it only requires
272272
// one confirmation and it already met it.
273273
select {
274-
case numConfsLeft := <-ntfn1.Event.Updates:
275-
const expected = 0
276-
if numConfsLeft != expected {
274+
case updDetails := <-ntfn1.Event.Updates:
275+
expected := chainntnfs.TxUpdateInfo{
276+
NumConfsLeft: 0,
277+
BlockHeight: 11,
278+
}
279+
if updDetails.NumConfsLeft != expected.NumConfsLeft {
277280
t.Fatalf("Received incorrect confirmation update: tx1 "+
278281
"expected %d confirmations left, got %d",
279-
expected, numConfsLeft)
282+
expected.NumConfsLeft, updDetails.NumConfsLeft)
283+
}
284+
if updDetails.BlockHeight != expected.BlockHeight {
285+
t.Fatalf("Received incorrect confirmation update: tx1 "+
286+
"expected %d block height, got %d",
287+
expected.BlockHeight, updDetails.BlockHeight)
280288
}
281289
default:
282290
t.Fatal("Expected confirmation update for tx1")
@@ -300,12 +308,20 @@ func TestTxNotifierFutureConfDispatch(t *testing.T) {
300308
// We should only receive one update for tx2 since it only has one
301309
// confirmation so far and it requires two.
302310
select {
303-
case numConfsLeft := <-ntfn2.Event.Updates:
304-
const expected = 1
305-
if numConfsLeft != expected {
311+
case updDetails := <-ntfn2.Event.Updates:
312+
expected := chainntnfs.TxUpdateInfo{
313+
NumConfsLeft: 1,
314+
BlockHeight: 11,
315+
}
316+
if updDetails.NumConfsLeft != expected.NumConfsLeft {
306317
t.Fatalf("Received incorrect confirmation update: tx2 "+
307318
"expected %d confirmations left, got %d",
308-
expected, numConfsLeft)
319+
expected.NumConfsLeft, updDetails.NumConfsLeft)
320+
}
321+
if updDetails.BlockHeight != expected.BlockHeight {
322+
t.Fatalf("Received incorrect confirmation update: tx2 "+
323+
"expected %d block height, got %d",
324+
expected.BlockHeight, updDetails.BlockHeight)
309325
}
310326
default:
311327
t.Fatal("Expected confirmation update for tx2")
@@ -341,12 +357,20 @@ func TestTxNotifierFutureConfDispatch(t *testing.T) {
341357
// We should only receive one update since the last at the new height,
342358
// indicating how many confirmations are still left.
343359
select {
344-
case numConfsLeft := <-ntfn2.Event.Updates:
345-
const expected = 0
346-
if numConfsLeft != expected {
360+
case updDetails := <-ntfn2.Event.Updates:
361+
expected := chainntnfs.TxUpdateInfo{
362+
NumConfsLeft: 0,
363+
BlockHeight: 11,
364+
}
365+
if updDetails.NumConfsLeft != expected.NumConfsLeft {
347366
t.Fatalf("Received incorrect confirmation update: tx2 "+
348367
"expected %d confirmations left, got %d",
349-
expected, numConfsLeft)
368+
expected.NumConfsLeft, updDetails.NumConfsLeft)
369+
}
370+
if updDetails.BlockHeight != expected.BlockHeight {
371+
t.Fatalf("Received incorrect confirmation update: tx2 "+
372+
"expected %d block height, got %d",
373+
expected.BlockHeight, updDetails.BlockHeight)
350374
}
351375
default:
352376
t.Fatal("Expected confirmation update for tx2")
@@ -411,12 +435,20 @@ func TestTxNotifierHistoricalConfDispatch(t *testing.T) {
411435
err = n.UpdateConfDetails(ntfn1.HistoricalDispatch.ConfRequest, &txConf1)
412436
require.NoError(t, err, "unable to update conf details")
413437
select {
414-
case numConfsLeft := <-ntfn1.Event.Updates:
415-
const expected = 0
416-
if numConfsLeft != expected {
438+
case updDetails := <-ntfn1.Event.Updates:
439+
expected := chainntnfs.TxUpdateInfo{
440+
NumConfsLeft: 0,
441+
BlockHeight: 9,
442+
}
443+
if updDetails.NumConfsLeft != expected.NumConfsLeft {
417444
t.Fatalf("Received incorrect confirmation update: tx1 "+
418445
"expected %d confirmations left, got %d",
419-
expected, numConfsLeft)
446+
expected.NumConfsLeft, updDetails.NumConfsLeft)
447+
}
448+
if updDetails.BlockHeight != expected.BlockHeight {
449+
t.Fatalf("Received incorrect confirmation update: tx1 "+
450+
"expected %d block height, got %d",
451+
expected.BlockHeight, updDetails.BlockHeight)
420452
}
421453
default:
422454
t.Fatal("Expected confirmation update for tx1")
@@ -443,12 +475,20 @@ func TestTxNotifierHistoricalConfDispatch(t *testing.T) {
443475
err = n.UpdateConfDetails(ntfn2.HistoricalDispatch.ConfRequest, &txConf2)
444476
require.NoError(t, err, "unable to update conf details")
445477
select {
446-
case numConfsLeft := <-ntfn2.Event.Updates:
447-
const expected = 1
448-
if numConfsLeft != expected {
478+
case updDetails := <-ntfn2.Event.Updates:
479+
expected := chainntnfs.TxUpdateInfo{
480+
NumConfsLeft: 1,
481+
BlockHeight: 9,
482+
}
483+
if updDetails.NumConfsLeft != expected.NumConfsLeft {
449484
t.Fatalf("Received incorrect confirmation update: tx2 "+
450485
"expected %d confirmations left, got %d",
451-
expected, numConfsLeft)
486+
expected.NumConfsLeft, updDetails.NumConfsLeft)
487+
}
488+
if updDetails.BlockHeight != expected.BlockHeight {
489+
t.Fatalf("Received incorrect confirmation update: tx2 "+
490+
"expected %d block height, got %d",
491+
expected.BlockHeight, updDetails.BlockHeight)
452492
}
453493
default:
454494
t.Fatal("Expected confirmation update for tx2")
@@ -485,12 +525,20 @@ func TestTxNotifierHistoricalConfDispatch(t *testing.T) {
485525
// We should only receive one update for tx2 since the last one,
486526
// indicating how many confirmations are still left.
487527
select {
488-
case numConfsLeft := <-ntfn2.Event.Updates:
489-
const expected = 0
490-
if numConfsLeft != expected {
528+
case updDetails := <-ntfn2.Event.Updates:
529+
expected := chainntnfs.TxUpdateInfo{
530+
NumConfsLeft: 0,
531+
BlockHeight: 9,
532+
}
533+
if updDetails.NumConfsLeft != expected.NumConfsLeft {
491534
t.Fatalf("Received incorrect confirmation update: tx2 "+
492535
"expected %d confirmations left, got %d",
493-
expected, numConfsLeft)
536+
expected.NumConfsLeft, updDetails.NumConfsLeft)
537+
}
538+
if updDetails.BlockHeight != expected.BlockHeight {
539+
t.Fatalf("Received incorrect confirmation update: tx2 "+
540+
"expected %d block height, got %d",
541+
expected.BlockHeight, updDetails.BlockHeight)
494542
}
495543
default:
496544
t.Fatal("Expected confirmation update for tx2")
@@ -1490,12 +1538,20 @@ func TestTxNotifierConfReorg(t *testing.T) {
14901538
// We should only receive one update for tx2 since it only requires
14911539
// one confirmation and it already met it.
14921540
select {
1493-
case numConfsLeft := <-ntfn2.Event.Updates:
1494-
const expected = 0
1495-
if numConfsLeft != expected {
1541+
case updDetails := <-ntfn2.Event.Updates:
1542+
expected := chainntnfs.TxUpdateInfo{
1543+
NumConfsLeft: 0,
1544+
BlockHeight: 12,
1545+
}
1546+
if updDetails.NumConfsLeft != expected.NumConfsLeft {
14961547
t.Fatalf("Received incorrect confirmation update: tx2 "+
14971548
"expected %d confirmations left, got %d",
1498-
expected, numConfsLeft)
1549+
expected.NumConfsLeft, updDetails.NumConfsLeft)
1550+
}
1551+
if updDetails.BlockHeight != expected.BlockHeight {
1552+
t.Fatalf("Received incorrect confirmation update: tx2 "+
1553+
"expected %d block height, got %d",
1554+
expected.BlockHeight, updDetails.BlockHeight)
14991555
}
15001556
default:
15011557
t.Fatal("Expected confirmation update for tx2")
@@ -1520,12 +1576,23 @@ func TestTxNotifierConfReorg(t *testing.T) {
15201576
// confirmations and it has already met them.
15211577
for i := uint32(1); i <= 2; i++ {
15221578
select {
1523-
case numConfsLeft := <-ntfn3.Event.Updates:
1524-
expected := tx3NumConfs - i
1525-
if numConfsLeft != expected {
1526-
t.Fatalf("Received incorrect confirmation update: tx3 "+
1527-
"expected %d confirmations left, got %d",
1528-
expected, numConfsLeft)
1579+
case updDetails := <-ntfn3.Event.Updates:
1580+
expected := chainntnfs.TxUpdateInfo{
1581+
NumConfsLeft: tx3NumConfs - i,
1582+
BlockHeight: 12,
1583+
}
1584+
if updDetails.NumConfsLeft != expected.NumConfsLeft {
1585+
t.Fatalf("Received incorrect confirmation "+
1586+
"update: tx3 expected %d "+
1587+
"confirmations left, got %d",
1588+
expected.NumConfsLeft,
1589+
updDetails.NumConfsLeft)
1590+
}
1591+
if updDetails.BlockHeight != expected.BlockHeight {
1592+
t.Fatalf("Received incorrect confirmation "+
1593+
"update: tx3 expected %d block "+
1594+
"height, got %d", expected.BlockHeight,
1595+
updDetails.BlockHeight)
15291596
}
15301597
default:
15311598
t.Fatal("Expected confirmation update for tx2")

0 commit comments

Comments
 (0)