Skip to content

Commit c6b2f1f

Browse files
committed
loopd: static address loop-in support
1 parent ccd8cc1 commit c6b2f1f

File tree

3 files changed

+152
-8
lines changed

3 files changed

+152
-8
lines changed

loopd/daemon.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/lightninglabs/loop/notifications"
2525
"github.com/lightninglabs/loop/staticaddr/address"
2626
"github.com/lightninglabs/loop/staticaddr/deposit"
27+
"github.com/lightninglabs/loop/staticaddr/loopin"
2728
"github.com/lightninglabs/loop/staticaddr/withdraw"
2829
loop_swaprpc "github.com/lightninglabs/loop/swapserverrpc"
2930
"github.com/lightninglabs/loop/sweepbatcher"
@@ -536,6 +537,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
536537
staticAddressManager *address.Manager
537538
depositManager *deposit.Manager
538539
withdrawalManager *withdraw.Manager
540+
staticLoopInManager *loopin.Manager
539541
)
540542

541543
// Create the reservation and instantout managers.
@@ -613,6 +615,30 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
613615
Signer: d.lnd.Signer,
614616
}
615617
withdrawalManager = withdraw.NewManager(withdrawalCfg)
618+
619+
// Static address loop-in manager setup.
620+
staticAddressLoopInStore := loopin.NewSqlStore(
621+
loopdb.NewTypedStore[loopin.Querier](baseDb),
622+
clock.NewDefaultClock(), d.lnd.ChainParams,
623+
)
624+
625+
staticLoopInManager = loopin.NewManager(&loopin.Config{
626+
Server: staticAddressClient,
627+
QuoteGetter: swapClient.Server,
628+
LndClient: d.lnd.Client,
629+
InvoicesClient: d.lnd.Invoices,
630+
NodePubkey: d.lnd.NodePubkey,
631+
AddressManager: staticAddressManager,
632+
DepositManager: depositManager,
633+
Store: staticAddressLoopInStore,
634+
WalletKit: d.lnd.WalletKit,
635+
ChainNotifier: d.lnd.ChainNotifier,
636+
ChainParams: d.lnd.ChainParams,
637+
Signer: d.lnd.Signer,
638+
ValidateLoopInContract: loop.ValidateLoopInContract,
639+
MaxStaticAddrHtlcFeePercentage: d.cfg.MaxStaticAddrHtlcFeePercentage,
640+
MaxStaticAddrHtlcBackupFeePercentage: d.cfg.MaxStaticAddrHtlcBackupFeePercentage,
641+
})
616642
}
617643

618644
// Now finally fully initialize the swap client RPC server instance.
@@ -631,6 +657,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
631657
staticAddressManager: staticAddressManager,
632658
depositManager: depositManager,
633659
withdrawalManager: withdrawalManager,
660+
staticLoopInManager: staticLoopInManager,
634661
}
635662

636663
// Retrieve all currently existing swaps from the database.
@@ -825,6 +852,34 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
825852
withdrawalManager.WaitInitComplete()
826853
}
827854

855+
// Start the static address loop-in manager.
856+
if staticLoopInManager != nil {
857+
d.wg.Add(1)
858+
go func() {
859+
defer d.wg.Done()
860+
861+
// Lnd's GetInfo call supplies us with the current block
862+
// height.
863+
info, err := d.lnd.Client.GetInfo(d.mainCtx)
864+
if err != nil {
865+
d.internalErrChan <- err
866+
867+
return
868+
}
869+
870+
log.Info("Starting static address loop-in manager...")
871+
err = staticLoopInManager.Run(
872+
d.mainCtx, info.BlockHeight,
873+
)
874+
if err != nil && !errors.Is(context.Canceled, err) {
875+
d.internalErrChan <- err
876+
}
877+
log.Info("Starting static address loop-in manager " +
878+
"stopped")
879+
}()
880+
staticLoopInManager.WaitInitComplete()
881+
}
882+
828883
// Last, start our internal error handler. This will return exactly one
829884
// error or nil on the main error channel to inform the caller that
830885
// something went wrong or that shutdown is complete. We don't add to

loopd/perms/perms.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ var RequiredPermissions = map[string][]bakery.Op{
101101
Entity: "loop",
102102
Action: "in",
103103
}},
104+
"/looprpc.SwapClient/StaticAddressLoopIn": {{
105+
Entity: "swap",
106+
Action: "read",
107+
}, {
108+
Entity: "loop",
109+
Action: "in",
110+
}},
104111
"/looprpc.SwapClient/GetLsatTokens": {{
105112
Entity: "auth",
106113
Action: "read",

loopd/swapclient_server.go

Lines changed: 90 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/lightninglabs/loop/looprpc"
2929
"github.com/lightninglabs/loop/staticaddr/address"
3030
"github.com/lightninglabs/loop/staticaddr/deposit"
31+
"github.com/lightninglabs/loop/staticaddr/loopin"
3132
"github.com/lightninglabs/loop/staticaddr/withdraw"
3233
"github.com/lightninglabs/loop/swap"
3334
"github.com/lightninglabs/loop/swapserverrpc"
@@ -91,6 +92,7 @@ type swapClientServer struct {
9192
staticAddressManager *address.Manager
9293
depositManager *deposit.Manager
9394
withdrawalManager *withdraw.Manager
95+
staticLoopInManager *loopin.Manager
9496
swaps map[lntypes.Hash]loop.SwapInfo
9597
subscribers map[int]chan<- interface{}
9698
statusChan chan loop.SwapInfo
@@ -1464,6 +1466,52 @@ func (s *swapClientServer) GetStaticAddressSummary(ctx context.Context,
14641466
)
14651467
}
14661468

1469+
// StaticAddressLoopIn initiates a loop-in request using static address
1470+
// deposits.
1471+
func (s *swapClientServer) StaticAddressLoopIn(ctx context.Context,
1472+
in *looprpc.StaticAddressLoopInRequest) (
1473+
*looprpc.StaticAddressLoopInResponse, error) {
1474+
1475+
log.Infof("Static loop-in request received")
1476+
1477+
routeHints, err := unmarshallRouteHints(in.RouteHints)
1478+
if err != nil {
1479+
return nil, err
1480+
}
1481+
1482+
req := &loop.StaticAddressLoopInRequest{
1483+
DepositOutpoints: in.Outpoints,
1484+
MaxSwapFee: btcutil.Amount(in.MaxSwapFeeSatoshis),
1485+
Label: in.Label,
1486+
Initiator: in.Initiator,
1487+
Private: in.Private,
1488+
RouteHints: routeHints,
1489+
PaymentTimeoutSeconds: in.PaymentTimeoutSeconds,
1490+
}
1491+
1492+
if in.LastHop != nil {
1493+
lastHop, err := route.NewVertexFromBytes(in.LastHop)
1494+
if err != nil {
1495+
return nil, err
1496+
}
1497+
req.LastHop = &lastHop
1498+
}
1499+
1500+
errChan := s.staticLoopInManager.DeliverLoopInRequest(ctx, req)
1501+
select {
1502+
case err = <-errChan:
1503+
1504+
case <-ctx.Done():
1505+
err = ctx.Err()
1506+
}
1507+
1508+
if err != nil {
1509+
return nil, err
1510+
}
1511+
1512+
return &looprpc.StaticAddressLoopInResponse{}, nil
1513+
}
1514+
14671515
func (s *swapClientServer) depositSummary(ctx context.Context,
14681516
deposits []*deposit.Deposit, stateFilter looprpc.DepositState,
14691517
outpointsFilter []string) (*looprpc.StaticAddressSummaryResponse,
@@ -1475,6 +1523,8 @@ func (s *swapClientServer) depositSummary(ctx context.Context,
14751523
valueDeposited int64
14761524
valueExpired int64
14771525
valueWithdrawn int64
1526+
valueLoopedIn int64
1527+
htlcTimeoutSwept int64
14781528
)
14791529

14801530
// Value unconfirmed.
@@ -1500,6 +1550,12 @@ func (s *swapClientServer) depositSummary(ctx context.Context,
15001550

15011551
case deposit.Withdrawn:
15021552
valueWithdrawn += value
1553+
1554+
case deposit.LoopedIn:
1555+
valueLoopedIn += value
1556+
1557+
case deposit.HtlcTimeoutSwept:
1558+
htlcTimeoutSwept += value
15031559
}
15041560
}
15051561

@@ -1528,7 +1584,7 @@ func (s *swapClientServer) depositSummary(ctx context.Context,
15281584
return true
15291585
}
15301586

1531-
return d.GetState() == toServerState(stateFilter)
1587+
return d.IsInState(toServerState(stateFilter))
15321588
}
15331589
clientDeposits = filter(deposits, f)
15341590
}
@@ -1546,13 +1602,15 @@ func (s *swapClientServer) depositSummary(ctx context.Context,
15461602
}
15471603

15481604
return &looprpc.StaticAddressSummaryResponse{
1549-
StaticAddress: address.String(),
1550-
TotalNumDeposits: uint32(totalNumDeposits),
1551-
ValueUnconfirmedSatoshis: valueUnconfirmed,
1552-
ValueDepositedSatoshis: valueDeposited,
1553-
ValueExpiredSatoshis: valueExpired,
1554-
ValueWithdrawnSatoshis: valueWithdrawn,
1555-
FilteredDeposits: clientDeposits,
1605+
StaticAddress: address.String(),
1606+
TotalNumDeposits: uint32(totalNumDeposits),
1607+
ValueUnconfirmedSatoshis: valueUnconfirmed,
1608+
ValueDepositedSatoshis: valueDeposited,
1609+
ValueExpiredSatoshis: valueExpired,
1610+
ValueWithdrawnSatoshis: valueWithdrawn,
1611+
ValueLoopedInSatoshis: valueLoopedIn,
1612+
ValueHtlcTimeoutSweepsSatoshis: htlcTimeoutSwept,
1613+
FilteredDeposits: clientDeposits,
15561614
}, nil
15571615
}
15581616

@@ -1595,6 +1653,18 @@ func toClientState(state fsm.StateType) looprpc.DepositState {
15951653
case deposit.PublishExpirySweep:
15961654
return looprpc.DepositState_PUBLISH_EXPIRED
15971655

1656+
case deposit.LoopingIn:
1657+
return looprpc.DepositState_LOOPING_IN
1658+
1659+
case deposit.LoopedIn:
1660+
return looprpc.DepositState_LOOPED_IN
1661+
1662+
case deposit.SweepHtlcTimeout:
1663+
return looprpc.DepositState_SWEEP_HTLC_TIMEOUT
1664+
1665+
case deposit.HtlcTimeoutSwept:
1666+
return looprpc.DepositState_HTLC_TIMEOUT_SWEPT
1667+
15981668
case deposit.WaitForExpirySweep:
15991669
return looprpc.DepositState_WAIT_FOR_EXPIRY_SWEEP
16001670

@@ -1623,6 +1693,18 @@ func toServerState(state looprpc.DepositState) fsm.StateType {
16231693
case looprpc.DepositState_PUBLISH_EXPIRED:
16241694
return deposit.PublishExpirySweep
16251695

1696+
case looprpc.DepositState_LOOPING_IN:
1697+
return deposit.LoopingIn
1698+
1699+
case looprpc.DepositState_LOOPED_IN:
1700+
return deposit.LoopedIn
1701+
1702+
case looprpc.DepositState_SWEEP_HTLC_TIMEOUT:
1703+
return deposit.SweepHtlcTimeout
1704+
1705+
case looprpc.DepositState_HTLC_TIMEOUT_SWEPT:
1706+
return deposit.HtlcTimeoutSwept
1707+
16261708
case looprpc.DepositState_WAIT_FOR_EXPIRY_SWEEP:
16271709
return deposit.WaitForExpirySweep
16281710

0 commit comments

Comments
 (0)