2019-03-12 15:10:37 +00:00
|
|
|
package loop
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-06-07 08:56:34 +00:00
|
|
|
"github.com/btcsuite/btcutil"
|
|
|
|
"github.com/lightninglabs/lndclient"
|
2019-03-12 15:10:37 +00:00
|
|
|
"github.com/lightninglabs/loop/loopdb"
|
|
|
|
"github.com/lightninglabs/loop/sweep"
|
|
|
|
"github.com/lightninglabs/loop/test"
|
2021-06-07 08:56:34 +00:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
|
|
|
"github.com/stretchr/testify/require"
|
2019-03-12 15:10:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type loopInTestContext struct {
|
|
|
|
t *testing.T
|
|
|
|
lnd *test.LndMockServices
|
|
|
|
server *serverMock
|
|
|
|
store *storeMock
|
|
|
|
sweeper *sweep.Sweeper
|
|
|
|
cfg *executeConfig
|
|
|
|
statusChan chan SwapInfo
|
|
|
|
blockEpochChan chan interface{}
|
2021-06-07 08:56:34 +00:00
|
|
|
|
|
|
|
swapInvoiceSubscription *test.SingleInvoiceSubscription
|
2019-03-12 15:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newLoopInTestContext(t *testing.T) *loopInTestContext {
|
|
|
|
lnd := test.NewMockLnd()
|
2020-05-29 09:27:47 +00:00
|
|
|
server := newServerMock(lnd)
|
2019-03-12 15:10:37 +00:00
|
|
|
store := newStoreMock(t)
|
|
|
|
sweeper := sweep.Sweeper{Lnd: &lnd.LndServices}
|
|
|
|
|
|
|
|
blockEpochChan := make(chan interface{})
|
|
|
|
statusChan := make(chan SwapInfo)
|
|
|
|
|
|
|
|
expiryChan := make(chan time.Time)
|
|
|
|
timerFactory := func(expiry time.Duration) <-chan time.Time {
|
|
|
|
return expiryChan
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := executeConfig{
|
|
|
|
statusChan: statusChan,
|
|
|
|
sweeper: &sweeper,
|
|
|
|
blockEpochChan: blockEpochChan,
|
|
|
|
timerFactory: timerFactory,
|
2021-05-24 06:40:14 +00:00
|
|
|
cancelSwap: server.CancelLoopOutSwap,
|
2019-03-12 15:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &loopInTestContext{
|
|
|
|
t: t,
|
|
|
|
lnd: lnd,
|
|
|
|
server: server,
|
|
|
|
store: store,
|
|
|
|
sweeper: &sweeper,
|
|
|
|
cfg: &cfg,
|
|
|
|
statusChan: statusChan,
|
|
|
|
blockEpochChan: blockEpochChan,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *loopInTestContext) assertState(expectedState loopdb.SwapState) {
|
|
|
|
state := <-c.statusChan
|
|
|
|
if state.State != expectedState {
|
|
|
|
c.t.Fatalf("expected state %v but got %v", expectedState,
|
|
|
|
state.State)
|
|
|
|
}
|
|
|
|
}
|
2021-06-07 08:56:34 +00:00
|
|
|
|
|
|
|
// assertSubscribeInvoice asserts that the client subscribes to invoice updates
|
|
|
|
// for our swap invoice.
|
|
|
|
func (c *loopInTestContext) assertSubscribeInvoice(hash lntypes.Hash) {
|
|
|
|
c.swapInvoiceSubscription = <-c.lnd.SingleInvoiceSubcribeChannel
|
|
|
|
require.Equal(c.t, hash, c.swapInvoiceSubscription.Hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateInvoiceState mocks an update to our swap invoice state.
|
|
|
|
func (c *loopInTestContext) updateInvoiceState(amount btcutil.Amount,
|
|
|
|
state channeldb.ContractState) {
|
|
|
|
|
|
|
|
c.swapInvoiceSubscription.Update <- lndclient.InvoiceUpdate{
|
|
|
|
AmtPaid: amount,
|
|
|
|
State: state,
|
|
|
|
}
|
2021-06-07 08:56:36 +00:00
|
|
|
|
|
|
|
// If we're in a final state, close our update channels as lndclient
|
|
|
|
// would.
|
|
|
|
if state == channeldb.ContractCanceled ||
|
|
|
|
state == channeldb.ContractSettled {
|
|
|
|
|
|
|
|
close(c.swapInvoiceSubscription.Update)
|
|
|
|
close(c.swapInvoiceSubscription.Err)
|
|
|
|
}
|
2021-06-07 08:56:34 +00:00
|
|
|
}
|