Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 63 additions & 23 deletions staticaddr/deposit/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,6 @@ var (
defaultExpiry = uint32(100)

defaultDepositConfirmations = uint32(3)

confChan = make(chan *chainntnfs.TxConfirmation)

confErrChan = make(chan error)

blockChan = make(chan int32)

blockErrChan = make(chan error)

finalizedDepositChan = make(chan wire.OutPoint)
)

type mockStaticAddressClient struct {
Expand Down Expand Up @@ -229,48 +219,87 @@ func (m *MockChainNotifier) RegisterSpendNtfn(ctx context.Context,
// TestManager checks that the manager processes the right channel notifications
// while a deposit is expiring.
func TestManager(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(t.Context())
defer cancel()

// Create the test context with required mocks.
testContext := newManagerTestContext(t)

// Start the deposit manager.
initChan := make(chan struct{})
runErrChan := make(chan error, 1)
go func() {
require.NoError(t, testContext.manager.Run(ctx, initChan))
runErrChan <- testContext.manager.Run(ctx, initChan)
}()

// Ensure that the manager has been initialized.
<-initChan
select {
case <-initChan:
case err := <-runErrChan:
require.NoError(t, err, "manager failed to start")

case <-time.After(test.Timeout):
t.Fatal("manager timed out starting")
}

// Notify about the last block before the expiry.
blockChan <- int32(defaultDepositConfirmations + defaultExpiry)
testContext.blockChan <- int32(
defaultDepositConfirmations + defaultExpiry - 1,
)

// Ensure that the deposit state machine didn't sign for the expiry tx.
select {
case <-testContext.mockLnd.SignOutputRawChannel:
t.Fatal("received unexpected sign request")

default:
case <-time.After(test.Timeout / 10):
}

// Mine the expiry tx height.
blockChan <- int32(defaultDepositConfirmations + defaultExpiry)
testContext.blockChan <- int32(
defaultDepositConfirmations + defaultExpiry,
)

// Ensure that the deposit state machine didn't sign for the expiry tx.
<-testContext.mockLnd.SignOutputRawChannel
// Ensure that the deposit state machine signed the expiry tx.
select {
case <-testContext.mockLnd.SignOutputRawChannel:

case <-time.After(test.Timeout):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using test.Timeout here and elsewhere we could use a shorter one, eg. defaultTimeout (30 secs)?

t.Fatal("did not receive sign request")
}

// Ensure that the signed expiry transaction is published.
expiryTx := <-testContext.mockLnd.TxPublishChannel
var expiryTx *wire.MsgTx
select {
case expiryTx = <-testContext.mockLnd.TxPublishChannel:

case <-time.After(test.Timeout):
t.Fatal("did not receive published expiry tx")
}

// Ensure that the deposit is waiting for a confirmation notification.
confChan <- &chainntnfs.TxConfirmation{
testContext.confChan <- &chainntnfs.TxConfirmation{
BlockHeight: defaultDepositConfirmations + defaultExpiry + 3,
Tx: expiryTx,
}

// Ensure that the deposit is finalized.
<-finalizedDepositChan
// Ensure that the manager observed the finalization and removed the
// deposit from its active set.
require.Eventually(t, func() bool {
testContext.manager.mu.Lock()
defer testContext.manager.mu.Unlock()

return len(testContext.manager.activeDeposits) == 0
}, test.Timeout, 10*time.Millisecond)

cancel()
select {
case err := <-runErrChan:
require.ErrorIs(t, err, context.Canceled)

case <-time.After(test.Timeout):
t.Fatal("manager did not stop")
}
}

// ManagerTestContext is a helper struct that contains all the necessary
Expand All @@ -281,6 +310,10 @@ type ManagerTestContext struct {
mockLnd *test.LndMockServices
mockStaticAddressClient *mockStaticAddressClient
mockAddressManager *mockAddressManager
confChan chan *chainntnfs.TxConfirmation
confErrChan chan error
blockChan chan int32
blockErrChan chan error
}

// newManagerTestContext creates a new test context for the reservation manager.
Expand All @@ -292,6 +325,10 @@ func newManagerTestContext(t *testing.T) *ManagerTestContext {
mockAddressManager := new(mockAddressManager)
mockStore := new(mockStore)
mockChainNotifier := new(MockChainNotifier)
confChan := make(chan *chainntnfs.TxConfirmation)
confErrChan := make(chan error)
blockChan := make(chan int32)
blockErrChan := make(chan error)

ID, err := GetRandomDepositID()
utxo := &lnwallet.Utxo{
Expand Down Expand Up @@ -353,14 +390,17 @@ func newManagerTestContext(t *testing.T) *ManagerTestContext {
}

manager := NewManager(cfg)
manager.finalizedDepositChan = finalizedDepositChan

testContext := &ManagerTestContext{
manager: manager,
context: lndContext,
mockLnd: mockLnd,
mockStaticAddressClient: mockStaticAddressClient,
mockAddressManager: mockAddressManager,
confChan: confChan,
confErrChan: confErrChan,
blockChan: blockChan,
blockErrChan: blockErrChan,
}

staticAddress := generateStaticAddress(
Expand Down
Loading