From 1200b75546753cfc45acb3da07cb29b6fae37291 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Sun, 10 Nov 2024 17:41:48 +0800 Subject: [PATCH] chainntnfs: always notify txns before block This commit changes the order of notifications when a relevant tx is found in a block and now we will always notify the tx subscribers before notifying the block, which has implications in the upcoming blockbeat. When a block notification is subscribed via `RegisterBlockEpochNtfn` and a confirm or spend is subscribed via `RegisterConfirmationsNtfn` or `RegisterSpendNtfn`, we would always notify the block first before the tx, causing the subsystem to think there's no relevant txns found in this block, while the notifications are sent later. We now fix it by always sending the txns notifications first, so the subsystem can receive the txns, process them, then attempt to advance its state based on the block received. --- chainntnfs/bitcoindnotify/bitcoind.go | 8 +++++++- chainntnfs/btcdnotify/btcd.go | 7 ++++++- chainntnfs/neutrinonotify/neutrino.go | 8 +++++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/chainntnfs/bitcoindnotify/bitcoind.go b/chainntnfs/bitcoindnotify/bitcoind.go index 8bcf8872b..91c38d8d6 100644 --- a/chainntnfs/bitcoindnotify/bitcoind.go +++ b/chainntnfs/bitcoindnotify/bitcoind.go @@ -665,8 +665,14 @@ func (b *BitcoindNotifier) handleBlockConnected(block chainntnfs.BlockEpoch) err // satisfy any client requests based upon the new block. b.bestBlock = block + err = b.txNotifier.NotifyHeight(uint32(block.Height)) + if err != nil { + return fmt.Errorf("unable to notify height: %w", err) + } + b.notifyBlockEpochs(block.Height, block.Hash, block.BlockHeader) - return b.txNotifier.NotifyHeight(uint32(block.Height)) + + return nil } // notifyBlockEpochs notifies all registered block epoch clients of the newly diff --git a/chainntnfs/btcdnotify/btcd.go b/chainntnfs/btcdnotify/btcd.go index bcbfa571a..ff91b5aee 100644 --- a/chainntnfs/btcdnotify/btcd.go +++ b/chainntnfs/btcdnotify/btcd.go @@ -725,11 +725,16 @@ func (b *BtcdNotifier) handleBlockConnected(epoch chainntnfs.BlockEpoch) error { // satisfy any client requests based upon the new block. b.bestBlock = epoch + err = b.txNotifier.NotifyHeight(uint32(epoch.Height)) + if err != nil { + return fmt.Errorf("unable to notify height: %w", err) + } + b.notifyBlockEpochs( epoch.Height, epoch.Hash, epoch.BlockHeader, ) - return b.txNotifier.NotifyHeight(uint32(epoch.Height)) + return nil } // notifyBlockEpochs notifies all registered block epoch clients of the newly diff --git a/chainntnfs/neutrinonotify/neutrino.go b/chainntnfs/neutrinonotify/neutrino.go index 55d923577..7b93bcd80 100644 --- a/chainntnfs/neutrinonotify/neutrino.go +++ b/chainntnfs/neutrinonotify/neutrino.go @@ -689,10 +689,16 @@ func (n *NeutrinoNotifier) handleBlockConnected(newBlock *filteredBlock) error { n.bestBlock.Height = int32(newBlock.height) n.bestBlock.BlockHeader = newBlock.header + err = n.txNotifier.NotifyHeight(newBlock.height) + if err != nil { + return fmt.Errorf("unable to notify height: %w", err) + } + n.notifyBlockEpochs( int32(newBlock.height), &newBlock.hash, newBlock.header, ) - return n.txNotifier.NotifyHeight(newBlock.height) + + return nil } // getFilteredBlock is a utility to retrieve the full filtered block from a block epoch.