Skip to content

Commit f94a0b7

Browse files
committed
loopd: static address loop-in support
1 parent dcede40 commit f94a0b7

File tree

3 files changed

+142
-8
lines changed

3 files changed

+142
-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.
@@ -788,6 +815,34 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
788815
withdrawalManager.WaitInitComplete()
789816
}
790817

818+
// Start the static address loop-in manager.
819+
if staticLoopInManager != nil {
820+
d.wg.Add(1)
821+
go func() {
822+
defer d.wg.Done()
823+
824+
// Lnd's GetInfo call supplies us with the current block
825+
// height.
826+
info, err := d.lnd.Client.GetInfo(d.mainCtx)
827+
if err != nil {
828+
d.internalErrChan <- err
829+
830+
return
831+
}
832+
833+
log.Info("Starting static address loop-in manager...")
834+
err = staticLoopInManager.Run(
835+
d.mainCtx, info.BlockHeight,
836+
)
837+
if err != nil && !errors.Is(context.Canceled, err) {
838+
d.internalErrChan <- err
839+
}
840+
log.Info("Starting static address loop-in manager " +
841+
"stopped")
842+
}()
843+
staticLoopInManager.WaitInitComplete()
844+
}
845+
791846
// Last, start our internal error handler. This will return exactly one
792847
// error or nil on the main error channel to inform the caller that
793848
// 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: 80 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
@@ -1461,6 +1463,42 @@ func (s *swapClientServer) GetStaticAddressSummary(ctx context.Context,
14611463
)
14621464
}
14631465

1466+
// StaticAddressLoopIn initiates a loop-in request using static address
1467+
// deposits.
1468+
func (s *swapClientServer) StaticAddressLoopIn(ctx context.Context,
1469+
in *looprpc.StaticAddressLoopInRequest) (
1470+
*looprpc.StaticAddressLoopInResponse, error) {
1471+
1472+
log.Infof("Static loop-in request received")
1473+
1474+
routeHints, err := unmarshallRouteHints(in.RouteHints)
1475+
if err != nil {
1476+
return nil, err
1477+
}
1478+
1479+
req := &loop.StaticAddressLoopInRequest{
1480+
DepositOutpoints: in.Outpoints,
1481+
MaxSwapFee: btcutil.Amount(in.MaxSwapFeeSatoshis),
1482+
Label: in.Label,
1483+
Initiator: in.Initiator,
1484+
Private: in.Private,
1485+
RouteHints: routeHints,
1486+
PaymentTimeoutSeconds: in.PaymentTimeoutSeconds,
1487+
}
1488+
1489+
if in.LastHop != nil {
1490+
lastHop, err := route.NewVertexFromBytes(in.LastHop)
1491+
if err != nil {
1492+
return nil, err
1493+
}
1494+
req.LastHop = &lastHop
1495+
}
1496+
1497+
s.staticLoopInManager.DeliverLoopInRequest(ctx, req)
1498+
1499+
return &looprpc.StaticAddressLoopInResponse{}, nil
1500+
}
1501+
14641502
func (s *swapClientServer) depositSummary(ctx context.Context,
14651503
deposits []*deposit.Deposit, stateFilter looprpc.DepositState,
14661504
outpointsFilter []string) (*looprpc.StaticAddressSummaryResponse,
@@ -1472,6 +1510,8 @@ func (s *swapClientServer) depositSummary(ctx context.Context,
14721510
valueDeposited int64
14731511
valueExpired int64
14741512
valueWithdrawn int64
1513+
valueLoopedIn int64
1514+
htlcTimeoutSwept int64
14751515
)
14761516

14771517
// Value unconfirmed.
@@ -1497,6 +1537,12 @@ func (s *swapClientServer) depositSummary(ctx context.Context,
14971537

14981538
case deposit.Withdrawn:
14991539
valueWithdrawn += value
1540+
1541+
case deposit.LoopedIn:
1542+
valueLoopedIn += value
1543+
1544+
case deposit.HtlcTimeoutSwept:
1545+
htlcTimeoutSwept += value
15001546
}
15011547
}
15021548

@@ -1525,7 +1571,7 @@ func (s *swapClientServer) depositSummary(ctx context.Context,
15251571
return true
15261572
}
15271573

1528-
return d.GetState() == toServerState(stateFilter)
1574+
return d.IsInState(toServerState(stateFilter))
15291575
}
15301576
clientDeposits = filter(deposits, f)
15311577
}
@@ -1543,13 +1589,15 @@ func (s *swapClientServer) depositSummary(ctx context.Context,
15431589
}
15441590

15451591
return &looprpc.StaticAddressSummaryResponse{
1546-
StaticAddress: address.String(),
1547-
TotalNumDeposits: uint32(totalNumDeposits),
1548-
ValueUnconfirmedSatoshis: valueUnconfirmed,
1549-
ValueDepositedSatoshis: valueDeposited,
1550-
ValueExpiredSatoshis: valueExpired,
1551-
ValueWithdrawnSatoshis: valueWithdrawn,
1552-
FilteredDeposits: clientDeposits,
1592+
StaticAddress: address.String(),
1593+
TotalNumDeposits: uint32(totalNumDeposits),
1594+
ValueUnconfirmedSatoshis: valueUnconfirmed,
1595+
ValueDepositedSatoshis: valueDeposited,
1596+
ValueExpiredSatoshis: valueExpired,
1597+
ValueWithdrawnSatoshis: valueWithdrawn,
1598+
ValueLoopedInSatoshis: valueLoopedIn,
1599+
ValueHtlcTimeoutSweepsSatoshis: htlcTimeoutSwept,
1600+
FilteredDeposits: clientDeposits,
15531601
}, nil
15541602
}
15551603

@@ -1592,6 +1640,18 @@ func toClientState(state fsm.StateType) looprpc.DepositState {
15921640
case deposit.PublishExpirySweep:
15931641
return looprpc.DepositState_PUBLISH_EXPIRED
15941642

1643+
case deposit.LoopingIn:
1644+
return looprpc.DepositState_LOOPING_IN
1645+
1646+
case deposit.LoopedIn:
1647+
return looprpc.DepositState_LOOPED_IN
1648+
1649+
case deposit.SweepHtlcTimeout:
1650+
return looprpc.DepositState_SWEEP_HTLC_TIMEOUT
1651+
1652+
case deposit.HtlcTimeoutSwept:
1653+
return looprpc.DepositState_HTLC_TIMEOUT_SWEPT
1654+
15951655
case deposit.WaitForExpirySweep:
15961656
return looprpc.DepositState_WAIT_FOR_EXPIRY_SWEEP
15971657

@@ -1620,6 +1680,18 @@ func toServerState(state looprpc.DepositState) fsm.StateType {
16201680
case looprpc.DepositState_PUBLISH_EXPIRED:
16211681
return deposit.PublishExpirySweep
16221682

1683+
case looprpc.DepositState_LOOPING_IN:
1684+
return deposit.LoopingIn
1685+
1686+
case looprpc.DepositState_LOOPED_IN:
1687+
return deposit.LoopedIn
1688+
1689+
case looprpc.DepositState_SWEEP_HTLC_TIMEOUT:
1690+
return deposit.SweepHtlcTimeout
1691+
1692+
case looprpc.DepositState_HTLC_TIMEOUT_SWEPT:
1693+
return deposit.HtlcTimeoutSwept
1694+
16231695
case looprpc.DepositState_WAIT_FOR_EXPIRY_SWEEP:
16241696
return deposit.WaitForExpirySweep
16251697

0 commit comments

Comments
 (0)