chainntnfs/btcdnotify: properly include SpendingHeight in SpendDetails

This commit modifies the btcdnotify implementation of the ChainNotifier
interface to properly include the height in which the watched output
was spent in the SpendDetail sent as a notification.

The set of tests have also been updated to assert that the proper
spending height is included in received notification.
This commit is contained in:
Olaoluwa Osuntokun 2017-05-10 16:52:27 -07:00
parent a5113c2439
commit 45bbeb8f84
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2
2 changed files with 26 additions and 9 deletions

@ -369,15 +369,22 @@ out:
// the notification subscriber.
if clients, ok := b.spendNotifications[prevOut]; ok {
spenderSha := newSpend.tx.Hash()
for _, ntfn := range clients {
spendDetails := &chainntnfs.SpendDetail{
SpentOutPoint: ntfn.targetOutpoint,
SpenderTxHash: spenderSha,
// TODO(roasbeef): copy tx?
SpendingTx: spendingTx.MsgTx(),
SpenderInputIndex: uint32(i),
}
spendDetails := &chainntnfs.SpendDetail{
SpentOutPoint: &prevOut,
SpenderTxHash: spenderSha,
SpendingTx: spendingTx.MsgTx(),
SpenderInputIndex: uint32(i),
}
// TODO(roasbeef): after change to
// loadfilter, only notify on block
// inclusion?
if newSpend.details != nil {
spendDetails.SpendingHeight = newSpend.details.Height
} else {
spendDetails.SpendingHeight = currentHeight
}
for _, ntfn := range clients {
chainntnfs.Log.Infof("Dispatching "+
"spend notification for "+
"outpoint=%v", ntfn.targetOutpoint)

@ -287,6 +287,11 @@ func testSpendNotification(miner *rpctest.Harness,
t.Fatalf("unable to brodacst tx: %v", err)
}
_, currentHeight, err = miner.Node.GetBestBlock()
if err != nil {
t.Fatalf("unable to get current height: %v", err)
}
// Now we mine a single block, which should include our spend. The
// notification should also be sent off.
if _, err := miner.Node.Generate(1); err != nil {
@ -308,7 +313,7 @@ func testSpendNotification(miner *rpctest.Harness,
case ntfn := <-spentNtfn:
// We've received the spend nftn. So now verify all the
// fields have been set properly.
if ntfn.SpentOutPoint != outpoint {
if *ntfn.SpentOutPoint != *outpoint {
t.Fatalf("ntfn includes wrong output, reports "+
"%v instead of %v",
ntfn.SpentOutPoint, outpoint)
@ -323,6 +328,11 @@ func testSpendNotification(miner *rpctest.Harness,
"index, reports %v, should be %v",
ntfn.SpenderInputIndex, 0)
}
if ntfn.SpendingHeight != currentHeight {
t.Fatalf("ntfn has wrong spending height: "+
"expected %v, got %v", currentHeight,
ntfn.SpendingHeight)
}
case <-time.After(2 * time.Second):
t.Fatalf("spend ntfn never received")
}