Skip to content

Commit 2eec098

Browse files
committed
loopd: static address loop-in support
1 parent e623061 commit 2eec098

File tree

3 files changed

+157
-8
lines changed

3 files changed

+157
-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.
@@ -840,6 +867,34 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
840867
withdrawalManager.WaitInitComplete()
841868
}
842869

870+
// Start the static address loop-in manager.
871+
if staticLoopInManager != nil {
872+
d.wg.Add(1)
873+
go func() {
874+
defer d.wg.Done()
875+
876+
// Lnd's GetInfo call supplies us with the current block
877+
// height.
878+
info, err := d.lnd.Client.GetInfo(d.mainCtx)
879+
if err != nil {
880+
d.internalErrChan <- err
881+
882+
return
883+
}
884+
885+
log.Info("Starting static address loop-in manager...")
886+
err = staticLoopInManager.Run(
887+
d.mainCtx, info.BlockHeight,
888+
)
889+
if err != nil && !errors.Is(context.Canceled, err) {
890+
d.internalErrChan <- err
891+
}
892+
log.Info("Starting static address loop-in manager " +
893+
"stopped")
894+
}()
895+
staticLoopInManager.WaitInitComplete()
896+
}
897+
843898
// Last, start our internal error handler. This will return exactly one
844899
// error or nil on the main error channel to inform the caller that
845900
// 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: 95 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
@@ -1481,6 +1483,57 @@ func (s *swapClientServer) GetStaticAddressSummary(ctx context.Context,
14811483
)
14821484
}
14831485

1486+
// StaticAddressLoopIn initiates a loop-in request using static address
1487+
// deposits.
1488+
func (s *swapClientServer) StaticAddressLoopIn(ctx context.Context,
1489+
in *looprpc.StaticAddressLoopInRequest) (
1490+
*looprpc.StaticAddressLoopInResponse, error) {
1491+
1492+
log.Infof("Static loop-in request received")
1493+
1494+
routeHints, err := unmarshallRouteHints(in.RouteHints)
1495+
if err != nil {
1496+
return nil, err
1497+
}
1498+
1499+
req := &loop.StaticAddressLoopInRequest{
1500+
DepositOutpoints: in.Outpoints,
1501+
MaxSwapFee: btcutil.Amount(in.MaxSwapFeeSatoshis),
1502+
Label: in.Label,
1503+
Initiator: in.Initiator,
1504+
Private: in.Private,
1505+
RouteHints: routeHints,
1506+
PaymentTimeoutSeconds: in.PaymentTimeoutSeconds,
1507+
}
1508+
1509+
if in.LastHop != nil {
1510+
lastHop, err := route.NewVertexFromBytes(in.LastHop)
1511+
if err != nil {
1512+
return nil, err
1513+
}
1514+
req.LastHop = &lastHop
1515+
}
1516+
1517+
loopIn, err := s.staticLoopInManager.DeliverLoopInRequest(ctx, req)
1518+
if err != nil {
1519+
return nil, err
1520+
}
1521+
1522+
return &looprpc.StaticAddressLoopInResponse{
1523+
SwapHash: loopIn.SwapHash[:],
1524+
State: string(loopIn.GetState()),
1525+
Amount: uint64(loopIn.TotalDepositAmount()),
1526+
HtlcCltv: loopIn.HtlcCltvExpiry,
1527+
MaxSwapFeeSatoshis: int64(loopIn.MaxSwapFee),
1528+
InitiationHeight: loopIn.InitiationHeight,
1529+
ProtocolVersion: loopIn.ProtocolVersion.String(),
1530+
Initiator: loopIn.Initiator,
1531+
Label: loopIn.Label,
1532+
PaymentTimeoutSeconds: loopIn.PaymentTimeoutSeconds,
1533+
QuotedSwapFeeSatoshis: int64(loopIn.QuotedSwapFee),
1534+
}, nil
1535+
}
1536+
14841537
func (s *swapClientServer) depositSummary(ctx context.Context,
14851538
deposits []*deposit.Deposit, stateFilter looprpc.DepositState,
14861539
outpointsFilter []string) (*looprpc.StaticAddressSummaryResponse,
@@ -1492,6 +1545,8 @@ func (s *swapClientServer) depositSummary(ctx context.Context,
14921545
valueDeposited int64
14931546
valueExpired int64
14941547
valueWithdrawn int64
1548+
valueLoopedIn int64
1549+
htlcTimeoutSwept int64
14951550
)
14961551

14971552
// Value unconfirmed.
@@ -1517,6 +1572,12 @@ func (s *swapClientServer) depositSummary(ctx context.Context,
15171572

15181573
case deposit.Withdrawn:
15191574
valueWithdrawn += value
1575+
1576+
case deposit.LoopedIn:
1577+
valueLoopedIn += value
1578+
1579+
case deposit.HtlcTimeoutSwept:
1580+
htlcTimeoutSwept += value
15201581
}
15211582
}
15221583

@@ -1545,7 +1606,7 @@ func (s *swapClientServer) depositSummary(ctx context.Context,
15451606
return true
15461607
}
15471608

1548-
return d.GetState() == toServerState(stateFilter)
1609+
return d.IsInState(toServerState(stateFilter))
15491610
}
15501611
clientDeposits = filter(deposits, f)
15511612
}
@@ -1563,13 +1624,15 @@ func (s *swapClientServer) depositSummary(ctx context.Context,
15631624
}
15641625

15651626
return &looprpc.StaticAddressSummaryResponse{
1566-
StaticAddress: address.String(),
1567-
TotalNumDeposits: uint32(totalNumDeposits),
1568-
ValueUnconfirmedSatoshis: valueUnconfirmed,
1569-
ValueDepositedSatoshis: valueDeposited,
1570-
ValueExpiredSatoshis: valueExpired,
1571-
ValueWithdrawnSatoshis: valueWithdrawn,
1572-
FilteredDeposits: clientDeposits,
1627+
StaticAddress: address.String(),
1628+
TotalNumDeposits: uint32(totalNumDeposits),
1629+
ValueUnconfirmedSatoshis: valueUnconfirmed,
1630+
ValueDepositedSatoshis: valueDeposited,
1631+
ValueExpiredSatoshis: valueExpired,
1632+
ValueWithdrawnSatoshis: valueWithdrawn,
1633+
ValueLoopedInSatoshis: valueLoopedIn,
1634+
ValueHtlcTimeoutSweepsSatoshis: htlcTimeoutSwept,
1635+
FilteredDeposits: clientDeposits,
15731636
}, nil
15741637
}
15751638

@@ -1612,6 +1675,18 @@ func toClientState(state fsm.StateType) looprpc.DepositState {
16121675
case deposit.PublishExpirySweep:
16131676
return looprpc.DepositState_PUBLISH_EXPIRED
16141677

1678+
case deposit.LoopingIn:
1679+
return looprpc.DepositState_LOOPING_IN
1680+
1681+
case deposit.LoopedIn:
1682+
return looprpc.DepositState_LOOPED_IN
1683+
1684+
case deposit.SweepHtlcTimeout:
1685+
return looprpc.DepositState_SWEEP_HTLC_TIMEOUT
1686+
1687+
case deposit.HtlcTimeoutSwept:
1688+
return looprpc.DepositState_HTLC_TIMEOUT_SWEPT
1689+
16151690
case deposit.WaitForExpirySweep:
16161691
return looprpc.DepositState_WAIT_FOR_EXPIRY_SWEEP
16171692

@@ -1637,6 +1712,18 @@ func toServerState(state looprpc.DepositState) fsm.StateType {
16371712
case looprpc.DepositState_PUBLISH_EXPIRED:
16381713
return deposit.PublishExpirySweep
16391714

1715+
case looprpc.DepositState_LOOPING_IN:
1716+
return deposit.LoopingIn
1717+
1718+
case looprpc.DepositState_LOOPED_IN:
1719+
return deposit.LoopedIn
1720+
1721+
case looprpc.DepositState_SWEEP_HTLC_TIMEOUT:
1722+
return deposit.SweepHtlcTimeout
1723+
1724+
case looprpc.DepositState_HTLC_TIMEOUT_SWEPT:
1725+
return deposit.HtlcTimeoutSwept
1726+
16401727
case looprpc.DepositState_WAIT_FOR_EXPIRY_SWEEP:
16411728
return deposit.WaitForExpirySweep
16421729

0 commit comments

Comments
 (0)