From 5044cd64689f3ec0abf58c77d35b903ae7ef9ed3 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Tue, 12 Sep 2017 17:11:21 +0200 Subject: [PATCH] chainntnfs/btcdnotify: recognize JSON-RPC error in RegisterSpendNtfn MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit fixes a prior bug in the logic for registering a new spend notification. Previously, if the transaction wasn’t found in the mempool or already confirmed within the chain, then GetRawTransactionVerbose would return an error which would cause the function itself to exit with an error. This issue would then cause the server to be unable to start up as the breach arbiter would be unable to register for spend notifications for all the channels that it needed to be watching. We fix this error simply by recognizing the particular JSON-RPC error that will be returned in this scenario and treating it as a benign error. --- chainntnfs/btcdnotify/btcd.go | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/chainntnfs/btcdnotify/btcd.go b/chainntnfs/btcdnotify/btcd.go index b928def9..a6b305af 100644 --- a/chainntnfs/btcdnotify/btcd.go +++ b/chainntnfs/btcdnotify/btcd.go @@ -506,6 +506,8 @@ func (b *BtcdNotifier) notifyBlockEpochs(newHeight int32, newSha *chainhash.Hash go func(ntfnChan chan *chainntnfs.BlockEpoch, cancelChan chan struct{}, clientWg *sync.WaitGroup) { + // TODO(roasbeef): move to goroutine per client, use sync queue + defer clientWg.Done() defer b.wg.Done() @@ -664,18 +666,26 @@ func (b *BtcdNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint, if txout == nil { transaction, err := b.chainConn.GetRawTransactionVerbose(&outpoint.Hash) if err != nil { - return nil, err + jsonErr, ok := err.(*btcjson.RPCError) + switch { + case ok && jsonErr.Code == -5: + default: + return nil, err + } } - blockhash, err := chainhash.NewHashFromStr(transaction.BlockHash) - if err != nil { - return nil, err - } + if transaction != nil { + blockhash, err := chainhash.NewHashFromStr(transaction.BlockHash) + if err != nil { + return nil, err + } - ops := []*wire.OutPoint{outpoint} - if err := b.chainConn.Rescan(blockhash, nil, ops); err != nil { - chainntnfs.Log.Errorf("Rescan for spend notification txout failed: %v", err) - return nil, err + ops := []*wire.OutPoint{outpoint} + if err := b.chainConn.Rescan(blockhash, nil, ops); err != nil { + chainntnfs.Log.Errorf("Rescan for spend "+ + "notification txout failed: %v", err) + return nil, err + } } }