lntest: add new wait for conf helper method to ChainNotifier
What changed, and why it matters
This commit only adds a new helper method to a test mock and improves an error message in test code. It does not change any production code, network behavior, or security-sensitive logic. There is no security issue here.
No security action needed. This is a routine test-infrastructure change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff modifies two files under lntest/ (test infrastructure only). It adds a WaitForConfRegistrationAndSend helper to the mock ChainNotifier so tests can wait for a confirmation registration before sending a mock confirmation. It also changes an assertion error message in harness_assertion.go to print the concrete type and a detailed dump of the unexpected update. No production code, cryptographic operations, or network protocol handling are affected.
Changed components
lntest/mock/chainnotifier.golntest/harness_assertion.goInspect captured patch +42 / −5
diff --git a/lntest/harness_assertion.go b/lntest/harness_assertion.go
index 9ae4251..8c60a27 100644
--- a/lntest/harness_assertion.go
+++ b/lntest/harness_assertion.go
@@ -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",
diff --git a/lntest/mock/chainnotifier.go b/lntest/mock/chainnotifier.go
index ddce8de..9a9e125 100644
--- a/lntest/mock/chainnotifier.go
+++ b/lntest/mock/chainnotifier.go
@@ -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"
@@ -8,9 +11,10 @@ import (
// ChainNotifier is a mock implementation of the ChainNotifier interface.
type ChainNotifier struct {
- SpendChan chan *chainntnfs.SpendDetail
- EpochChan chan *chainntnfs.BlockEpoch
- ConfChan chan *chainntnfs.TxConfirmation
+ 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")
+ }
+}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.