chainntnfs/btcdnotify: fix historical confirmation dispatch bugs

This commit fixes to distinct bugs in the way we previously dipatched
notifications for transactions which needed a historical dispatch.

Previously we would compare transactions when scanning the block using
the `tx.Hash` field. This was incorrect has the `Hash` field is
actually the wtxid, not the txid which should be the item being
compared. We fix this within the second bug fix by actually using the
txid to find the proper transaction.

The second fix has to due with a slight race condition which led to an
off-by-one error when dispatching the historical confirmation. If while
we were dispatching the confirmation, a new block was found, then we
could calculate the wrong block height (off by one) as we were using
the ‘currentHeight’ instead the height of the block which included the
transaction.
This commit is contained in:
Olaoluwa Osuntokun 2017-05-15 17:47:03 -07:00
parent cc19695dad
commit dfd9f6d1ca
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2

@ -431,24 +431,18 @@ func (b *BtcdNotifier) attemptHistoricalDispatch(msg *confirmationsNotification,
"historical dispatch: %v", tx.BlockHash, err)
return false
}
block, err := b.chainConn.GetBlock(blockHash)
block, err := b.chainConn.GetBlockVerbose(blockHash)
if err != nil {
chainntnfs.Log.Errorf("unable to get block hash: %v", err)
return false
}
txHash, err := chainhash.NewHashFromStr(tx.Hash)
if err != nil {
chainntnfs.Log.Errorf("unable to convert to hash: %v", err)
return false
}
// If the block obtained, locate the transaction's index within the
// block so we can give the subscriber full confirmation details.
var txIndex uint32
for i, t := range block.Transactions {
h := t.TxHash()
if txHash.IsEqual(&h) {
targetTxidStr := msg.txid.String()
for i, txHash := range block.Tx {
if txHash == targetTxidStr {
txIndex = uint32(i)
break
}
@ -456,7 +450,7 @@ func (b *BtcdNotifier) attemptHistoricalDispatch(msg *confirmationsNotification,
confDetails := &chainntnfs.TxConfirmation{
BlockHash: blockHash,
BlockHeight: uint32(currentHeight) - uint32(tx.Confirmations) + 1,
BlockHeight: uint32(block.Height),
TxIndex: txIndex,
}