From 43c84c2ce5ac3cd4c9d64df536edb25c479471eb Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Mon, 20 Jun 2016 21:33:41 -0700 Subject: [PATCH] chainntfs: include height in spending+conf notifications --- chainntfs/btcdnotify/btcd.go | 17 +++++++++++++---- chainntfs/btcdnotify/btcdnotify_test.go | 8 ++++---- chainntfs/chainntfs.go | 4 ++-- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/chainntfs/btcdnotify/btcd.go b/chainntfs/btcdnotify/btcd.go index 018f30fa..9a5fd015 100644 --- a/chainntfs/btcdnotify/btcd.go +++ b/chainntfs/btcdnotify/btcd.go @@ -168,6 +168,9 @@ out: case *spendNotification: b.spendNotifications[*msg.targetOutpoint] = msg case *confirmationsNotification: + chainntnfs.Log.Infof("New confirmations "+ + "subscription: txid=%v, numconfs=%v", + *msg.txid, msg.numConfirmations) b.confNotifications[*msg.txid] = msg } case staleBlockHash := <-b.disconnectedBlockHashes: @@ -182,6 +185,9 @@ out: continue } + chainntnfs.Log.Infof("New block: height=%v, sha=%v", + connectedBlock.height, connectedBlock.sha) + newHeight := connectedBlock.height for _, tx := range newBlock.Transactions() { // Check if the inclusion of this transaction @@ -247,7 +253,7 @@ func (b *BtcdNotifier) notifyConfs(newBlockHeight int32) { // is eligible until there are no more eligible entries. nextConf := heap.Pop(b.confHeap).(*confEntry) for nextConf.triggerHeight <= uint32(newBlockHeight) { - nextConf.finConf <- struct{}{} + nextConf.finConf <- newBlockHeight if b.confHeap.Len() == 0 { return @@ -273,7 +279,10 @@ func (b *BtcdNotifier) checkConfirmationTrigger(txSha *wire.ShaHash, blockHeight if confNtfn, ok := b.confNotifications[*txSha]; ok { delete(b.confNotifications, *txSha) if confNtfn.numConfirmations == 1 { - confNtfn.finConf <- struct{}{} + chainntnfs.Log.Infof("Dispatching single conf "+ + "notification, sha=%v, height=%v", txSha, + blockHeight) + confNtfn.finConf <- blockHeight return } @@ -328,7 +337,7 @@ type confirmationsNotification struct { initialConfirmHeight uint32 numConfirmations uint32 - finConf chan struct{} + finConf chan int32 negativeConf chan int32 // TODO(roasbeef): re-org funny business } @@ -341,7 +350,7 @@ func (b *BtcdNotifier) RegisterConfirmationsNtfn(txid *wire.ShaHash, ntfn := &confirmationsNotification{ txid: txid, numConfirmations: numConfs, - finConf: make(chan struct{}, 1), + finConf: make(chan int32, 1), negativeConf: make(chan int32, 1), } diff --git a/chainntfs/btcdnotify/btcdnotify_test.go b/chainntfs/btcdnotify/btcdnotify_test.go index b5c33b8f..b6f9572b 100644 --- a/chainntfs/btcdnotify/btcdnotify_test.go +++ b/chainntfs/btcdnotify/btcdnotify_test.go @@ -6,12 +6,12 @@ import ( "time" "github.com/Roasbeef/btcd/rpctest" + "github.com/lightningnetwork/lnd/chainntfs" "github.com/roasbeef/btcd/btcec" "github.com/roasbeef/btcd/chaincfg" "github.com/roasbeef/btcd/txscript" "github.com/roasbeef/btcd/wire" "github.com/roasbeef/btcutil" - "github.com/lightningnetwork/lnd/chainntfs" ) var ( @@ -68,7 +68,7 @@ func testSingleConfirmationNotification(miner *rpctest.Harness, t.Fatalf("unable to generate single block: %v", err) } - confSent := make(chan struct{}) + confSent := make(chan int32) go func() { confSent <- <-confIntent.Confirmed }() @@ -105,7 +105,7 @@ func testMultiConfirmationNotification(miner *rpctest.Harness, t.Fatalf("unable to generate single block: %v", err) } - confSent := make(chan struct{}) + confSent := make(chan int32) go func() { confSent <- <-confIntent.Confirmed }() @@ -163,7 +163,7 @@ func testBatchConfirmationNotification(miner *rpctest.Harness, t.Fatalf("unable to generate single block: %v", err) } - confSent := make(chan struct{}) + confSent := make(chan int32) go func() { confSent <- <-confIntents[i].Confirmed }() diff --git a/chainntfs/chainntfs.go b/chainntfs/chainntfs.go index c34de0e7..50571478 100644 --- a/chainntfs/chainntfs.go +++ b/chainntfs/chainntfs.go @@ -62,8 +62,7 @@ type ChainNotifier interface { // chain, the 'NegativeConf' will be sent upon with a value representing the // depth of the re-org. type ConfirmationEvent struct { - Confirmed chan struct{} // MUST be buffered. - + Confirmed chan int32 // MUST be buffered. // TODO(roasbeef): all goroutines on ln channel updates should also // have a struct chan that's closed if funding gets re-org out. Need // to sync, to request another confirmation event ntfn, then re-open @@ -82,6 +81,7 @@ type SpendDetail struct { SpenderTxHash *wire.ShaHash SpendingTx *wire.MsgTx SpenderInputIndex uint32 + SpendingHeight int32 } // SpendEvent encapsulates a spentness notification. Its only field 'Spend' will