Skip to content

Commit 11d8ea1

Browse files
committed
loopd: static address loop-in support
1 parent 13cfd07 commit 11d8ea1

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.
@@ -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: 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
@@ -1458,6 +1460,52 @@ func (s *swapClientServer) GetStaticAddressSummary(ctx context.Context,
14581460
)
14591461
}
14601462

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

14741524
// Value unconfirmed.
@@ -1494,6 +1544,12 @@ func (s *swapClientServer) depositSummary(ctx context.Context,
14941544

14951545
case deposit.Withdrawn:
14961546
valueWithdrawn += value
1547+
1548+
case deposit.LoopedIn:
1549+
valueLoopedIn += value
1550+
1551+
case deposit.HtlcTimeoutSwept:
1552+
htlcTimeoutSwept += value
14971553
}
14981554
}
14991555

@@ -1522,7 +1578,7 @@ func (s *swapClientServer) depositSummary(ctx context.Context,
15221578
return true
15231579
}
15241580

1525-
return d.GetState() == toServerState(stateFilter)
1581+
return d.IsInState(toServerState(stateFilter))
15261582
}
15271583
clientDeposits = filter(deposits, f)
15281584
}
@@ -1540,13 +1596,15 @@ func (s *swapClientServer) depositSummary(ctx context.Context,
15401596
}
15411597

15421598
return &looprpc.StaticAddressSummaryResponse{
1543-
StaticAddress: address.String(),
1544-
TotalNumDeposits: uint32(totalNumDeposits),
1545-
ValueUnconfirmedSatoshis: valueUnconfirmed,
1546-
ValueDepositedSatoshis: valueDeposited,
1547-
ValueExpiredSatoshis: valueExpired,
1548-
ValueWithdrawnSatoshis: valueWithdrawn,
1549-
FilteredDeposits: clientDeposits,
1599+
StaticAddress: address.String(),
1600+
TotalNumDeposits: uint32(totalNumDeposits),
1601+
ValueUnconfirmedSatoshis: valueUnconfirmed,
1602+
ValueDepositedSatoshis: valueDeposited,
1603+
ValueExpiredSatoshis: valueExpired,
1604+
ValueWithdrawnSatoshis: valueWithdrawn,
1605+
ValueLoopedInSatoshis: valueLoopedIn,
1606+
ValueHtlcTimeoutSweepsSatoshis: htlcTimeoutSwept,
1607+
FilteredDeposits: clientDeposits,
15501608
}, nil
15511609
}
15521610

@@ -1589,6 +1647,18 @@ func toClientState(state fsm.StateType) looprpc.DepositState {
15891647
case deposit.PublishExpirySweep:
15901648
return looprpc.DepositState_PUBLISH_EXPIRED
15911649

1650+
case deposit.LoopingIn:
1651+
return looprpc.DepositState_LOOPING_IN
1652+
1653+
case deposit.LoopedIn:
1654+
return looprpc.DepositState_LOOPED_IN
1655+
1656+
case deposit.SweepHtlcTimeout:
1657+
return looprpc.DepositState_SWEEP_HTLC_TIMEOUT
1658+
1659+
case deposit.HtlcTimeoutSwept:
1660+
return looprpc.DepositState_HTLC_TIMEOUT_SWEPT
1661+
15921662
case deposit.WaitForExpirySweep:
15931663
return looprpc.DepositState_WAIT_FOR_EXPIRY_SWEEP
15941664

@@ -1617,6 +1687,18 @@ func toServerState(state looprpc.DepositState) fsm.StateType {
16171687
case looprpc.DepositState_PUBLISH_EXPIRED:
16181688
return deposit.PublishExpirySweep
16191689

1690+
case looprpc.DepositState_LOOPING_IN:
1691+
return deposit.LoopingIn
1692+
1693+
case looprpc.DepositState_LOOPED_IN:
1694+
return deposit.LoopedIn
1695+
1696+
case looprpc.DepositState_SWEEP_HTLC_TIMEOUT:
1697+
return deposit.SweepHtlcTimeout
1698+
1699+
case looprpc.DepositState_HTLC_TIMEOUT_SWEPT:
1700+
return deposit.HtlcTimeoutSwept
1701+
16201702
case looprpc.DepositState_WAIT_FOR_EXPIRY_SWEEP:
16211703
return deposit.WaitForExpirySweep
16221704

0 commit comments

Comments
 (0)