From cadc8d0fba8a3ea379ec3c5856dbf283aa47850d Mon Sep 17 00:00:00 2001 From: ziggie Date: Wed, 16 Apr 2025 15:11:18 +0200 Subject: [PATCH] btcdnotify: change order of rpc calls. We have to make sure we register the block notifier before we fetch the best block for block notifications. --- chainntnfs/btcdnotify/btcd.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/chainntnfs/btcdnotify/btcd.go b/chainntnfs/btcdnotify/btcd.go index b89cde9ac..3c7843133 100644 --- a/chainntnfs/btcdnotify/btcd.go +++ b/chainntnfs/btcdnotify/btcd.go @@ -223,6 +223,17 @@ func (b *BtcdNotifier) startNotifier() error { return err } + // Before we fetch the best block/block height we need to register the + // notifications for connected blocks, otherwise we might think we are + // at an earlier block height because during block notification + // registration we might have already mined some new blocks. Hence we + // will not get notified accordingly. + if err := b.chainConn.NotifyBlocks(); err != nil { + b.txUpdates.Stop() + b.chainUpdates.Stop() + return err + } + currentHash, currentHeight, err := b.chainConn.GetBestBlock() if err != nil { b.txUpdates.Stop() @@ -248,12 +259,6 @@ func (b *BtcdNotifier) startNotifier() error { BlockHeader: &bestBlock.Header, } - if err := b.chainConn.NotifyBlocks(); err != nil { - b.txUpdates.Stop() - b.chainUpdates.Stop() - return err - } - b.wg.Add(1) go b.notificationDispatcher()