2019-03-07 02:22:46 +00:00
|
|
|
package loop
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-03-06 23:29:44 +00:00
|
|
|
"github.com/lightninglabs/loop/lndclient"
|
2019-03-07 04:32:24 +00:00
|
|
|
"github.com/lightninglabs/loop/loopdb"
|
2019-03-06 23:29:44 +00:00
|
|
|
"github.com/lightninglabs/loop/sweep"
|
|
|
|
"github.com/lightninglabs/loop/test"
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestLateHtlcPublish tests that the client is not revealing the preimage if
|
|
|
|
// there are not enough blocks left.
|
|
|
|
func TestLateHtlcPublish(t *testing.T) {
|
|
|
|
defer test.Guard(t)()
|
|
|
|
|
|
|
|
lnd := test.NewMockLnd()
|
|
|
|
|
|
|
|
ctx := test.NewContext(t, lnd)
|
|
|
|
|
|
|
|
server := newServerMock()
|
|
|
|
|
|
|
|
store := newStoreMock(t)
|
|
|
|
|
|
|
|
expiryChan := make(chan time.Time)
|
|
|
|
timerFactory := func(expiry time.Duration) <-chan time.Time {
|
|
|
|
return expiryChan
|
|
|
|
}
|
|
|
|
|
|
|
|
height := int32(600)
|
|
|
|
|
|
|
|
cfg := &swapConfig{
|
|
|
|
lnd: &lnd.LndServices,
|
|
|
|
store: store,
|
|
|
|
server: server,
|
|
|
|
}
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
swap, err := newLoopOutSwap(
|
2019-03-06 20:13:50 +00:00
|
|
|
context.Background(), cfg, height, testRequest,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sweeper := &sweep.Sweeper{Lnd: &lnd.LndServices}
|
|
|
|
|
|
|
|
blockEpochChan := make(chan interface{})
|
|
|
|
statusChan := make(chan SwapInfo)
|
|
|
|
|
|
|
|
errChan := make(chan error)
|
|
|
|
go func() {
|
|
|
|
err := swap.execute(context.Background(), &executeConfig{
|
|
|
|
statusChan: statusChan,
|
|
|
|
sweeper: sweeper,
|
|
|
|
blockEpochChan: blockEpochChan,
|
|
|
|
timerFactory: timerFactory,
|
|
|
|
}, height)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err)
|
|
|
|
}
|
|
|
|
errChan <- err
|
|
|
|
}()
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
store.assertLoopOutStored()
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
state := <-statusChan
|
2019-03-07 04:32:24 +00:00
|
|
|
if state.State != loopdb.StateInitiated {
|
2019-03-06 20:13:50 +00:00
|
|
|
t.Fatal("unexpected state")
|
|
|
|
}
|
|
|
|
|
|
|
|
signalSwapPaymentResult := ctx.AssertPaid(swapInvoiceDesc)
|
|
|
|
signalPrepaymentResult := ctx.AssertPaid(prepayInvoiceDesc)
|
|
|
|
|
|
|
|
// Expect client to register for conf
|
|
|
|
ctx.AssertRegisterConf()
|
|
|
|
|
|
|
|
// // Wait too long before publishing htlc.
|
|
|
|
blockEpochChan <- int32(swap.CltvExpiry - 10)
|
|
|
|
|
|
|
|
signalSwapPaymentResult(
|
|
|
|
errors.New(lndclient.PaymentResultUnknownPaymentHash),
|
|
|
|
)
|
|
|
|
signalPrepaymentResult(
|
|
|
|
errors.New(lndclient.PaymentResultUnknownPaymentHash),
|
|
|
|
)
|
|
|
|
|
2019-03-07 04:32:24 +00:00
|
|
|
store.assertStoreFinished(loopdb.StateFailTimeout)
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
status := <-statusChan
|
2019-03-07 04:32:24 +00:00
|
|
|
if status.State != loopdb.StateFailTimeout {
|
2019-03-06 20:13:50 +00:00
|
|
|
t.Fatal("unexpected state")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = <-errChan
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|