loopin/test: move invoice updates to loopin test context

pull/396/head
carla 3 years ago
parent 9d3d9ce680
commit cae72b5848
No known key found for this signature in database
GPG Key ID: 4CA7FE54A6213C91

@ -4,7 +4,6 @@ import (
"context" "context"
"testing" "testing"
"github.com/lightninglabs/lndclient"
"github.com/lightninglabs/loop/loopdb" "github.com/lightninglabs/loop/loopdb"
"github.com/lightninglabs/loop/swap" "github.com/lightninglabs/loop/swap"
"github.com/lightninglabs/loop/test" "github.com/lightninglabs/loop/test"
@ -89,17 +88,11 @@ func TestLoopInSuccess(t *testing.T) {
<-ctx.lnd.RegisterSpendChannel <-ctx.lnd.RegisterSpendChannel
// Client starts listening for swap invoice updates. // Client starts listening for swap invoice updates.
subscription := <-ctx.lnd.SingleInvoiceSubcribeChannel ctx.assertSubscribeInvoice(ctx.server.swapHash)
if subscription.Hash != ctx.server.swapHash {
t.Fatal("client subscribing to wrong invoice")
}
// Server has already paid invoice before spending the htlc. Signal // Server has already paid invoice before spending the htlc. Signal
// settled. // settled.
subscription.Update <- lndclient.InvoiceUpdate{ ctx.updateInvoiceState(49000, channeldb.ContractSettled)
State: channeldb.ContractSettled,
AmtPaid: 49000,
}
// Swap is expected to move to the state InvoiceSettled // Swap is expected to move to the state InvoiceSettled
ctx.assertState(loopdb.StateInvoiceSettled) ctx.assertState(loopdb.StateInvoiceSettled)
@ -257,10 +250,7 @@ func testLoopInTimeout(t *testing.T,
<-ctx.lnd.RegisterSpendChannel <-ctx.lnd.RegisterSpendChannel
// Client starts listening for swap invoice updates. // Client starts listening for swap invoice updates.
subscription := <-ctx.lnd.SingleInvoiceSubcribeChannel ctx.assertSubscribeInvoice(ctx.server.swapHash)
if subscription.Hash != ctx.server.swapHash {
t.Fatal("client subscribing to wrong invoice")
}
// Let htlc expire. // Let htlc expire.
ctx.blockEpochChan <- s.LoopInContract.CltvExpiry ctx.blockEpochChan <- s.LoopInContract.CltvExpiry
@ -294,10 +284,8 @@ func testLoopInTimeout(t *testing.T,
// safely cancel the swap invoice. // safely cancel the swap invoice.
<-ctx.lnd.FailInvoiceChannel <-ctx.lnd.FailInvoiceChannel
// Signal the the invoice was canceled. // Signal that the invoice was canceled.
subscription.Update <- lndclient.InvoiceUpdate{ ctx.updateInvoiceState(0, channeldb.ContractCanceled)
State: channeldb.ContractCanceled,
}
ctx.assertState(loopdb.StateFailTimeout) ctx.assertState(loopdb.StateFailTimeout)
state := ctx.store.assertLoopInState(loopdb.StateFailTimeout) state := ctx.store.assertLoopInState(loopdb.StateFailTimeout)
@ -501,18 +489,12 @@ func testLoopInResume(t *testing.T, state loopdb.SwapState, expired bool,
<-ctx.lnd.RegisterSpendChannel <-ctx.lnd.RegisterSpendChannel
// Client starts listening for swap invoice updates. // Client starts listening for swap invoice updates.
subscription := <-ctx.lnd.SingleInvoiceSubcribeChannel ctx.assertSubscribeInvoice(testPreimage.Hash())
if subscription.Hash != testPreimage.Hash() {
t.Fatal("client subscribing to wrong invoice")
}
// Server has already paid invoice before spending the htlc. Signal // Server has already paid invoice before spending the htlc. Signal
// settled. // settled.
invoiceUpdate := lndclient.InvoiceUpdate{ amtPaid := btcutil.Amount(49000)
State: channeldb.ContractSettled, ctx.updateInvoiceState(amtPaid, channeldb.ContractSettled)
AmtPaid: 49000,
}
subscription.Update <- invoiceUpdate
// Swap is expected to move to the state InvoiceSettled // Swap is expected to move to the state InvoiceSettled
ctx.assertState(loopdb.StateInvoiceSettled) ctx.assertState(loopdb.StateInvoiceSettled)
@ -537,7 +519,6 @@ func testLoopInResume(t *testing.T, state loopdb.SwapState, expired bool,
// We expect our server fee to reflect as the difference between htlc // We expect our server fee to reflect as the difference between htlc
// value and invoice amount paid. We use our original on-chain cost, set // value and invoice amount paid. We use our original on-chain cost, set
// earlier in the test, because we expect this value to be unchanged. // earlier in the test, because we expect this value to be unchanged.
cost.Server = btcutil.Amount(htlcTx.TxOut[0].Value) - cost.Server = btcutil.Amount(htlcTx.TxOut[0].Value) - amtPaid
invoiceUpdate.AmtPaid
require.Equal(t, cost, finalState.Cost) require.Equal(t, cost, finalState.Cost)
} }

@ -4,9 +4,14 @@ import (
"testing" "testing"
"time" "time"
"github.com/btcsuite/btcutil"
"github.com/lightninglabs/lndclient"
"github.com/lightninglabs/loop/loopdb" "github.com/lightninglabs/loop/loopdb"
"github.com/lightninglabs/loop/sweep" "github.com/lightninglabs/loop/sweep"
"github.com/lightninglabs/loop/test" "github.com/lightninglabs/loop/test"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/stretchr/testify/require"
) )
type loopInTestContext struct { type loopInTestContext struct {
@ -18,6 +23,8 @@ type loopInTestContext struct {
cfg *executeConfig cfg *executeConfig
statusChan chan SwapInfo statusChan chan SwapInfo
blockEpochChan chan interface{} blockEpochChan chan interface{}
swapInvoiceSubscription *test.SingleInvoiceSubscription
} }
func newLoopInTestContext(t *testing.T) *loopInTestContext { func newLoopInTestContext(t *testing.T) *loopInTestContext {
@ -61,3 +68,20 @@ func (c *loopInTestContext) assertState(expectedState loopdb.SwapState) {
state.State) state.State)
} }
} }
// 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,
}
}

Loading…
Cancel
Save