lntest: add new wait for conf helper method to ChainNotifier

This commit is contained in:
Olaoluwa Osuntokun committed 2026-01-15 16:22:26 -08:00
1 parent 12668b8af6
commit 0f59756b40
2 files changed
+39 -2

No files matched your search

+5 -2
View File
@@ -18,6 +18,7 @@ import (
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/davecgh/go-spew/spew"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
@@ -552,8 +553,10 @@ func (h HarnessTest) WaitForChannelCloseEvent(
require.NoError(h, err)
resp, ok := event.Update.(*lnrpc.CloseStatusUpdate_ChanClose)
require.Truef(h, ok, "expected channel close update, instead got %v",
event.Update)
require.Truef(
h, ok, "expected channel close update, instead got %T: %v",
event.Update, spew.Sdump(event.Update),
)
txid, err := chainhash.NewHash(resp.ChanClose.ClosingTxid)
require.NoErrorf(h, err, "wrong format found in closing txid: %v",
+34
View File
@@ -1,6 +1,9 @@
package mock
import (
"testing"
"time"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/chainntnfs"
@@ -11,6 +14,7 @@ type ChainNotifier struct {
SpendChan chan *chainntnfs.SpendDetail
EpochChan chan *chainntnfs.BlockEpoch
ConfChan chan *chainntnfs.TxConfirmation
ConfRegistered chan struct{}
}
// RegisterConfirmationsNtfn returns a ConfirmationEvent that contains a channel
@@ -19,6 +23,14 @@ func (c *ChainNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash,
pkScript []byte, numConfs, heightHint uint32,
opts ...chainntnfs.NotifierOption) (*chainntnfs.ConfirmationEvent, error) {
// Signal that a confirmation registration occurred.
if c.ConfRegistered != nil {
select {
case c.ConfRegistered <- struct{}{}:
default:
}
}
return &chainntnfs.ConfirmationEvent{
Confirmed: c.ConfChan,
Cancel: func() {},
@@ -61,3 +73,25 @@ func (c *ChainNotifier) Started() bool {
func (c *ChainNotifier) Stop() error {
return nil
}
// WaitForConfRegistrationAndSend waits for a confirmation registration to
// occur and then sends a confirmation notification. This is a helper function
// for tests that need to ensure the chain watcher has registered for
// confirmations before sending the confirmation.
func (c *ChainNotifier) WaitForConfRegistrationAndSend(t *testing.T) {
t.Helper()
// Wait for the chain watcher to register for confirmations.
select {
case <-c.ConfRegistered:
case <-time.After(time.Second * 2):
t.Fatalf("timeout waiting for conf registration")
}
// Send the confirmation to satisfy the confirmation requirement.
select {
case c.ConfChan <- &chainntnfs.TxConfirmation{}:
case <-time.After(time.Second * 1):
t.Fatalf("unable to send confirmation")
}
}