lntest: add new wait for conf helper method to ChainNotifier
This commit is contained in:
1 parent
12668b8af6
commit
0f59756b40
2 files changed
+42
-5
No files matched your search
@@ -18,6 +18,7 @@ import (
|
|||||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
||||||
"github.com/btcsuite/btcd/txscript"
|
"github.com/btcsuite/btcd/txscript"
|
||||||
"github.com/btcsuite/btcd/wire"
|
"github.com/btcsuite/btcd/wire"
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
"github.com/lightningnetwork/lnd/channeldb"
|
"github.com/lightningnetwork/lnd/channeldb"
|
||||||
"github.com/lightningnetwork/lnd/lnrpc"
|
"github.com/lightningnetwork/lnd/lnrpc"
|
||||||
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
|
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
|
||||||
@@ -552,8 +553,10 @@ func (h HarnessTest) WaitForChannelCloseEvent(
|
|||||||
require.NoError(h, err)
|
require.NoError(h, err)
|
||||||
|
|
||||||
resp, ok := event.Update.(*lnrpc.CloseStatusUpdate_ChanClose)
|
resp, ok := event.Update.(*lnrpc.CloseStatusUpdate_ChanClose)
|
||||||
require.Truef(h, ok, "expected channel close update, instead got %v",
|
require.Truef(
|
||||||
event.Update)
|
h, ok, "expected channel close update, instead got %T: %v",
|
||||||
|
event.Update, spew.Sdump(event.Update),
|
||||||
|
)
|
||||||
|
|
||||||
txid, err := chainhash.NewHash(resp.ChanClose.ClosingTxid)
|
txid, err := chainhash.NewHash(resp.ChanClose.ClosingTxid)
|
||||||
require.NoErrorf(h, err, "wrong format found in closing txid: %v",
|
require.NoErrorf(h, err, "wrong format found in closing txid: %v",
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package mock
|
package mock
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
||||||
"github.com/btcsuite/btcd/wire"
|
"github.com/btcsuite/btcd/wire"
|
||||||
"github.com/lightningnetwork/lnd/chainntnfs"
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
||||||
@@ -8,9 +11,10 @@ import (
|
|||||||
|
|
||||||
// ChainNotifier is a mock implementation of the ChainNotifier interface.
|
// ChainNotifier is a mock implementation of the ChainNotifier interface.
|
||||||
type ChainNotifier struct {
|
type ChainNotifier struct {
|
||||||
SpendChan chan *chainntnfs.SpendDetail
|
SpendChan chan *chainntnfs.SpendDetail
|
||||||
EpochChan chan *chainntnfs.BlockEpoch
|
EpochChan chan *chainntnfs.BlockEpoch
|
||||||
ConfChan chan *chainntnfs.TxConfirmation
|
ConfChan chan *chainntnfs.TxConfirmation
|
||||||
|
ConfRegistered chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// RegisterConfirmationsNtfn returns a ConfirmationEvent that contains a channel
|
// RegisterConfirmationsNtfn returns a ConfirmationEvent that contains a channel
|
||||||
@@ -19,6 +23,14 @@ func (c *ChainNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash,
|
|||||||
pkScript []byte, numConfs, heightHint uint32,
|
pkScript []byte, numConfs, heightHint uint32,
|
||||||
opts ...chainntnfs.NotifierOption) (*chainntnfs.ConfirmationEvent, error) {
|
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{
|
return &chainntnfs.ConfirmationEvent{
|
||||||
Confirmed: c.ConfChan,
|
Confirmed: c.ConfChan,
|
||||||
Cancel: func() {},
|
Cancel: func() {},
|
||||||
@@ -61,3 +73,25 @@ func (c *ChainNotifier) Started() bool {
|
|||||||
func (c *ChainNotifier) Stop() error {
|
func (c *ChainNotifier) Stop() error {
|
||||||
return nil
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user