From 1f95b660b99dcc11076f641f1c95851fd7b7270b Mon Sep 17 00:00:00 2001 From: Sam Lewis Date: Thu, 9 Nov 2017 21:18:13 +1000 Subject: [PATCH] chainntfns: Fix off by 1 block height error In the historical dispatch of btcdnotify, the dispatcher checks if a transaction has been included in a block. If this check happens before the notifier has processed the update, it's possible that the currentHeight of the notifier and the currentHeight of the chain might be out of sync which causes an off by one error when calculating a target height for the transaction confirmation. This change uses the height of the block the transaction was found in, rather than the currentHeight that's known by the notifier to eliminate this. --- chainntnfs/btcdnotify/btcd.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/chainntnfs/btcdnotify/btcd.go b/chainntnfs/btcdnotify/btcd.go index 7001c59df..0ef874aab 100644 --- a/chainntnfs/btcdnotify/btcd.go +++ b/chainntnfs/btcdnotify/btcd.go @@ -295,7 +295,7 @@ out: // If the notification can be partially or // fully dispatched, then we can skip the first // phase for ntfns. - if b.attemptHistoricalDispatch(msg, currentHeight) { + if b.attemptHistoricalDispatch(msg) { continue } @@ -420,8 +420,8 @@ out: // can skip straight to the confirmations heap. // // Returns true if the transaction was either partially or completely confirmed -func (b *BtcdNotifier) attemptHistoricalDispatch(msg *confirmationsNotification, - currentHeight int32) bool { +func (b *BtcdNotifier) attemptHistoricalDispatch( + msg *confirmationsNotification) bool { chainntnfs.Log.Infof("Attempting to trigger dispatch for %v from "+ "historical chain", msg.txid) @@ -482,8 +482,8 @@ func (b *BtcdNotifier) attemptHistoricalDispatch(msg *confirmationsNotification, // Otherwise, the transaction has only been *partially* confirmed, so // we need to insert it into the confirmation heap. - confsLeft := msg.numConfirmations - uint32(tx.Confirmations) - confHeight := uint32(currentHeight) + confsLeft + // Find the block height at which this transaction will be confirmed + confHeight := uint32(block.Height) + msg.numConfirmations - 1 heapEntry := &confEntry{ msg, confDetails,