From 45bbeb8f8426c62d90f86b1a1da9c709a9cce303 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Wed, 10 May 2017 16:52:27 -0700 Subject: [PATCH] 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. --- chainntnfs/btcdnotify/btcd.go | 23 +++++++++++++++-------- chainntnfs/interface_test.go | 12 +++++++++++- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/chainntnfs/btcdnotify/btcd.go b/chainntnfs/btcdnotify/btcd.go index ca1bd214b..eab0a5eaa 100644 --- a/chainntnfs/btcdnotify/btcd.go +++ b/chainntnfs/btcdnotify/btcd.go @@ -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) diff --git a/chainntnfs/interface_test.go b/chainntnfs/interface_test.go index 0d2fadfb8..045510f31 100644 --- a/chainntnfs/interface_test.go +++ b/chainntnfs/interface_test.go @@ -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") }