Skip to content

Commit 14e540f

Browse files
committed
loopd: test static address swap rpc fields
1 parent c6b4362 commit 14e540f

1 file changed

Lines changed: 190 additions & 1 deletion

File tree

loopd/swapclient_server_test.go

Lines changed: 190 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/lightninglabs/loop/staticaddr/script"
2525
"github.com/lightninglabs/loop/swap"
2626
mock_lnd "github.com/lightninglabs/loop/test"
27+
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
2728
"github.com/lightningnetwork/lnd/lntypes"
2829
"github.com/lightningnetwork/lnd/lnwallet"
2930
"github.com/lightningnetwork/lnd/lnwire"
@@ -373,6 +374,188 @@ func TestStaticAddressLoopInSwapServerCost(t *testing.T) {
373374
}
374375
}
375376

377+
// TestListStaticAddressSwapsPopulatesTimingAndCosts verifies that the RPC
378+
// response maps stored static loop-in timing fields and cost fields.
379+
func TestListStaticAddressSwapsPopulatesTimingAndCosts(t *testing.T) {
380+
ctx := t.Context()
381+
lnd := mock_lnd.NewMockLnd()
382+
383+
const (
384+
paymentRequestAmount = btcutil.Amount(50_000)
385+
quotedSwapFee = btcutil.Amount(1_234)
386+
depositValue = btcutil.Amount(51_234)
387+
depositConfHeight = int64(590)
388+
staticAddressExpiry = uint32(25)
389+
)
390+
391+
_, swapInvoice, err := lnd.Client.AddInvoice(
392+
ctx, &invoicesrpc.AddInvoiceData{
393+
Value: lnwire.NewMSatFromSatoshis(paymentRequestAmount),
394+
},
395+
)
396+
require.NoError(t, err)
397+
398+
swapHash := lntypes.Hash{1, 2, 3}
399+
depositID := deposit.ID{4, 5, 6}
400+
depositOutpoint := wire.OutPoint{
401+
Hash: chainhash.Hash{7, 8, 9},
402+
Index: 1,
403+
}
404+
testDeposit := &deposit.Deposit{
405+
ID: depositID,
406+
OutPoint: depositOutpoint,
407+
Value: depositValue,
408+
ConfirmationHeight: depositConfHeight,
409+
SwapHash: &swapHash,
410+
}
411+
testDeposit.SetState(deposit.LoopedIn)
412+
413+
initiationTime := time.Unix(1_234, 567).UTC()
414+
lastUpdateTime := time.Unix(2_345, 678).UTC()
415+
staticLoopIn := &loopin.StaticAddressLoopIn{
416+
SwapHash: swapHash,
417+
SwapInvoice: swapInvoice,
418+
InitiationTime: initiationTime,
419+
LastUpdateTime: lastUpdateTime,
420+
QuotedSwapFee: quotedSwapFee,
421+
DepositOutpoints: []string{depositOutpoint.String()},
422+
Deposits: []*deposit.Deposit{testDeposit},
423+
}
424+
staticLoopIn.SetState(loopin.Succeeded)
425+
426+
depositStore := &mockDepositStore{
427+
byOutpoint: map[string]*deposit.Deposit{
428+
depositOutpoint.String(): testDeposit,
429+
},
430+
}
431+
depositMgr := deposit.NewManager(&deposit.ManagerConfig{
432+
Store: depositStore,
433+
})
434+
435+
staticLoopInMgr, err := loopin.NewManager(&loopin.Config{
436+
Store: &mockStaticAddressLoopInStore{
437+
swaps: []*loopin.StaticAddressLoopIn{staticLoopIn},
438+
},
439+
DepositManager: depositMgr,
440+
}, 1)
441+
require.NoError(t, err)
442+
443+
_, clientPubkey := mock_lnd.CreateKey(1)
444+
_, serverPubkey := mock_lnd.CreateKey(2)
445+
addrStore := &mockAddressStore{
446+
params: []*script.Parameters{{
447+
ClientPubkey: clientPubkey,
448+
ServerPubkey: serverPubkey,
449+
Expiry: staticAddressExpiry,
450+
PkScript: []byte("pkscript"),
451+
}},
452+
}
453+
addrMgr, err := address.NewManager(&address.ManagerConfig{
454+
Store: addrStore,
455+
WalletKit: lnd.WalletKit,
456+
ChainParams: lnd.ChainParams,
457+
}, 1)
458+
require.NoError(t, err)
459+
460+
server := &swapClientServer{
461+
network: lndclient.NetworkTestnet,
462+
lnd: &lnd.LndServices,
463+
staticAddressManager: addrMgr,
464+
depositManager: depositMgr,
465+
staticLoopInManager: staticLoopInMgr,
466+
}
467+
resp, err := server.ListStaticAddressSwaps(
468+
ctx, &looprpc.ListStaticAddressSwapsRequest{},
469+
)
470+
require.NoError(t, err)
471+
require.Len(t, resp.Swaps, 1)
472+
473+
swap := resp.Swaps[0]
474+
require.Equal(t, swapHash[:], swap.SwapHash)
475+
require.Equal(t, []string{depositOutpoint.String()}, swap.DepositOutpoints)
476+
require.Equal(
477+
t, looprpc.StaticAddressLoopInSwapState_SUCCEEDED, swap.State,
478+
)
479+
require.Equal(t, int64(depositValue), swap.SwapAmountSatoshis)
480+
require.Equal(
481+
t, int64(paymentRequestAmount), swap.PaymentRequestAmountSatoshis,
482+
)
483+
require.Equal(t, initiationTime.UnixNano(), swap.InitiationTime)
484+
require.Equal(t, lastUpdateTime.UnixNano(), swap.LastUpdateTime)
485+
require.Equal(t, int64(quotedSwapFee), swap.CostServer)
486+
require.Zero(t, swap.CostOnchain)
487+
require.Zero(t, swap.CostOffchain)
488+
require.Len(t, swap.Deposits, 1)
489+
490+
rpcDeposit := swap.Deposits[0]
491+
require.Equal(t, depositID[:], rpcDeposit.Id)
492+
require.Equal(t, depositOutpoint.String(), rpcDeposit.Outpoint)
493+
require.Equal(t, int64(depositValue), rpcDeposit.Value)
494+
require.Equal(t, depositConfHeight, rpcDeposit.ConfirmationHeight)
495+
require.Equal(t, swapHash[:], rpcDeposit.SwapHash)
496+
require.Equal(t, looprpc.DepositState_LOOPED_IN, rpcDeposit.State)
497+
require.Equal(
498+
t, depositConfHeight+int64(staticAddressExpiry)-600,
499+
rpcDeposit.BlocksUntilExpiry,
500+
)
501+
}
502+
503+
// mockStaticAddressLoopInStore is a minimal in-memory loop-in store for RPC
504+
// response mapping tests.
505+
type mockStaticAddressLoopInStore struct {
506+
swaps []*loopin.StaticAddressLoopIn
507+
}
508+
509+
// CreateLoopIn satisfies the static loop-in store interface.
510+
func (s *mockStaticAddressLoopInStore) CreateLoopIn(_ context.Context,
511+
_ *loopin.StaticAddressLoopIn) error {
512+
513+
return nil
514+
}
515+
516+
// UpdateLoopIn satisfies the static loop-in store interface.
517+
func (s *mockStaticAddressLoopInStore) UpdateLoopIn(_ context.Context,
518+
_ *loopin.StaticAddressLoopIn) error {
519+
520+
return nil
521+
}
522+
523+
// GetStaticAddressLoopInSwapsByStates returns the configured loop-ins.
524+
func (s *mockStaticAddressLoopInStore) GetStaticAddressLoopInSwapsByStates(
525+
_ context.Context, _ []fsm.StateType) ([]*loopin.StaticAddressLoopIn,
526+
error) {
527+
528+
return s.swaps, nil
529+
}
530+
531+
// IsStored satisfies the static loop-in store interface.
532+
func (s *mockStaticAddressLoopInStore) IsStored(_ context.Context,
533+
_ lntypes.Hash) (bool, error) {
534+
535+
return false, nil
536+
}
537+
538+
// GetLoopInByHash returns the configured loop-in with the given hash.
539+
func (s *mockStaticAddressLoopInStore) GetLoopInByHash(_ context.Context,
540+
swapHash lntypes.Hash) (*loopin.StaticAddressLoopIn, error) {
541+
542+
for _, swp := range s.swaps {
543+
if swp.SwapHash == swapHash {
544+
return swp, nil
545+
}
546+
}
547+
548+
return nil, nil
549+
}
550+
551+
// SwapHashesForDepositIDs satisfies the static loop-in store interface.
552+
func (s *mockStaticAddressLoopInStore) SwapHashesForDepositIDs(
553+
_ context.Context, _ []deposit.ID) (map[lntypes.Hash][]deposit.ID,
554+
error) {
555+
556+
return nil, nil
557+
}
558+
376559
// TestRPCAutoloopReasonStaticLoopInNoCandidate verifies that the new planner
377560
// reason is exposed over rpc.
378561
func TestRPCAutoloopReasonStaticLoopInNoCandidate(t *testing.T) {
@@ -1140,10 +1323,16 @@ func (s *mockDepositStore) DepositForOutpoint(_ context.Context,
11401323
}
11411324
return nil, nil
11421325
}
1326+
11431327
func (s *mockDepositStore) AllDeposits(_ context.Context) ([]*deposit.Deposit,
11441328
error) {
11451329

1146-
return nil, nil
1330+
deposits := make([]*deposit.Deposit, 0, len(s.byOutpoint))
1331+
for _, d := range s.byOutpoint {
1332+
deposits = append(deposits, d)
1333+
}
1334+
1335+
return deposits, nil
11471336
}
11481337

11491338
// TestListUnspentDeposits tests filtering behavior of ListUnspentDeposits.

0 commit comments

Comments
 (0)