Skip to content

Commit 7eaecaf

Browse files
committed
loopd: static address loop-in support
1 parent 23506d9 commit 7eaecaf

File tree

3 files changed

+142
-8
lines changed

3 files changed

+142
-8
lines changed

loopd/daemon.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
loop_looprpc "github.com/lightninglabs/loop/looprpc"
2424
"github.com/lightninglabs/loop/staticaddr/address"
2525
"github.com/lightninglabs/loop/staticaddr/deposit"
26+
"github.com/lightninglabs/loop/staticaddr/loopin"
2627
"github.com/lightninglabs/loop/staticaddr/withdraw"
2728
loop_swaprpc "github.com/lightninglabs/loop/swapserverrpc"
2829
"github.com/lightninglabs/loop/sweepbatcher"
@@ -513,6 +514,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
513514
staticAddressManager *address.Manager
514515
depositManager *deposit.Manager
515516
withdrawalManager *withdraw.Manager
517+
staticLoopInManager *loopin.Manager
516518
)
517519
// Create the reservation and instantout managers.
518520
if d.cfg.EnableExperimental {
@@ -589,6 +591,28 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
589591
Signer: d.lnd.Signer,
590592
}
591593
withdrawalManager = withdraw.NewManager(withdrawalCfg)
594+
595+
// Static address loop-in manager setup.
596+
staticAddressLoopInStore := loopin.NewSqlStore(
597+
loopdb.NewTypedStore[loopin.Querier](baseDb),
598+
clock.NewDefaultClock(), d.lnd.ChainParams,
599+
)
600+
601+
loopinCfg := &loopin.Config{
602+
StaticAddressServerClient: staticAddressClient,
603+
SwapClient: swapClient,
604+
LndClient: d.lnd.Client,
605+
InvoicesClient: d.lnd.Invoices,
606+
NodePubkey: d.lnd.NodePubkey,
607+
AddressManager: staticAddressManager,
608+
DepositManager: depositManager,
609+
Store: staticAddressLoopInStore,
610+
WalletKit: d.lnd.WalletKit,
611+
ChainNotifier: d.lnd.ChainNotifier,
612+
ChainParams: d.lnd.ChainParams,
613+
Signer: d.lnd.Signer,
614+
}
615+
staticLoopInManager = loopin.NewManager(loopinCfg)
592616
}
593617

594618
// Now finally fully initialize the swap client RPC server instance.
@@ -607,6 +631,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
607631
staticAddressManager: staticAddressManager,
608632
depositManager: depositManager,
609633
withdrawalManager: withdrawalManager,
634+
staticLoopInManager: staticLoopInManager,
610635
}
611636

612637
// Retrieve all currently existing swaps from the database.
@@ -764,6 +789,33 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
764789
withdrawalManager.WaitInitComplete()
765790
}
766791

792+
// Start the static address loop-in manager.
793+
if staticLoopInManager != nil {
794+
d.wg.Add(1)
795+
go func() {
796+
defer d.wg.Done()
797+
798+
// Lnd's GetInfo call supplies us with the current block
799+
// height.
800+
info, err := d.lnd.Client.GetInfo(d.mainCtx)
801+
if err != nil {
802+
d.internalErrChan <- err
803+
return
804+
}
805+
806+
log.Info("Starting static address loop-in manager...")
807+
err = staticLoopInManager.Run(
808+
d.mainCtx, info.BlockHeight,
809+
)
810+
if err != nil && !errors.Is(context.Canceled, err) {
811+
d.internalErrChan <- err
812+
}
813+
log.Info("Starting static address loop-in manager " +
814+
"stopped")
815+
}()
816+
staticLoopInManager.WaitInitComplete()
817+
}
818+
767819
// Last, start our internal error handler. This will return exactly one
768820
// error or nil on the main error channel to inform the caller that
769821
// 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: 83 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
clientrpc "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
looprpc "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,45 @@ 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(_ context.Context,
1469+
in *clientrpc.StaticAddressLoopInRequest) (
1470+
*clientrpc.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+
var lastHop route.Vertex
1480+
if in.LastHop != nil {
1481+
lastHop, err = route.NewVertexFromBytes(in.LastHop)
1482+
if err != nil {
1483+
return nil, err
1484+
}
1485+
}
1486+
1487+
req := &loop.StaticAddressLoopInRequest{
1488+
DepositOutpoints: in.Outpoints,
1489+
MaxSwapFee: btcutil.Amount(in.MaxSwapFee),
1490+
LastHop: &lastHop,
1491+
Label: in.Label,
1492+
Initiator: in.Initiator,
1493+
Private: in.Private,
1494+
RouteHints: routeHints,
1495+
}
1496+
1497+
err = s.staticLoopInManager.InitiateLoopIn(req)
1498+
if err != nil {
1499+
return nil, err
1500+
}
1501+
1502+
return &clientrpc.StaticAddressLoopInResponse{}, nil
1503+
}
1504+
14641505
func (s *swapClientServer) depositSummary(ctx context.Context,
14651506
deposits []*deposit.Deposit, stateFilter clientrpc.DepositState,
14661507
outpointsFilter []string) (*clientrpc.StaticAddressSummaryResponse,
@@ -1472,6 +1513,8 @@ func (s *swapClientServer) depositSummary(ctx context.Context,
14721513
valueDeposited int64
14731514
valueExpired int64
14741515
valueWithdrawn int64
1516+
valueLoopedIn int64
1517+
htlcTimeoutSwept int64
14751518
)
14761519

14771520
// Value unconfirmed.
@@ -1497,6 +1540,12 @@ func (s *swapClientServer) depositSummary(ctx context.Context,
14971540

14981541
case deposit.Withdrawn:
14991542
valueWithdrawn += value
1543+
1544+
case deposit.LoopedIn:
1545+
valueLoopedIn += value
1546+
1547+
case deposit.HtlcTimeoutSwept:
1548+
htlcTimeoutSwept += value
15001549
}
15011550
}
15021551

@@ -1525,7 +1574,7 @@ func (s *swapClientServer) depositSummary(ctx context.Context,
15251574
return true
15261575
}
15271576

1528-
return d.GetState() == toServerState(stateFilter)
1577+
return d.IsInState(toServerState(stateFilter))
15291578
}
15301579
clientDeposits = filter(deposits, f)
15311580
}
@@ -1543,13 +1592,15 @@ func (s *swapClientServer) depositSummary(ctx context.Context,
15431592
}
15441593

15451594
return &clientrpc.StaticAddressSummaryResponse{
1546-
StaticAddress: address.String(),
1547-
TotalNumDeposits: uint32(totalNumDeposits),
1548-
ValueUnconfirmed: valueUnconfirmed,
1549-
ValueDeposited: valueDeposited,
1550-
ValueExpired: valueExpired,
1551-
ValueWithdrawn: valueWithdrawn,
1552-
FilteredDeposits: clientDeposits,
1595+
StaticAddress: address.String(),
1596+
TotalNumDeposits: uint32(totalNumDeposits),
1597+
ValueUnconfirmed: valueUnconfirmed,
1598+
ValueDeposited: valueDeposited,
1599+
ValueExpired: valueExpired,
1600+
ValueWithdrawn: valueWithdrawn,
1601+
ValueLoopedIn: valueLoopedIn,
1602+
ValueHtlcTimeoutSweeps: htlcTimeoutSwept,
1603+
FilteredDeposits: clientDeposits,
15531604
}, nil
15541605
}
15551606

@@ -1592,6 +1643,18 @@ func toClientState(state fsm.StateType) clientrpc.DepositState {
15921643
case deposit.PublishExpiredDeposit:
15931644
return clientrpc.DepositState_PUBLISH_EXPIRED
15941645

1646+
case deposit.LoopingIn:
1647+
return clientrpc.DepositState_LOOPING_IN
1648+
1649+
case deposit.LoopedIn:
1650+
return clientrpc.DepositState_LOOPED_IN
1651+
1652+
case deposit.SweepHtlcTimout:
1653+
return clientrpc.DepositState_SWEEP_HTLC_TIMEOUT
1654+
1655+
case deposit.HtlcTimeoutSwept:
1656+
return clientrpc.DepositState_HTLC_TIMEOUT_SWEPT
1657+
15951658
case deposit.WaitForExpirySweep:
15961659
return clientrpc.DepositState_WAIT_FOR_EXPIRY_SWEEP
15971660

@@ -1617,6 +1680,18 @@ func toServerState(state clientrpc.DepositState) fsm.StateType {
16171680
case clientrpc.DepositState_WITHDRAWN:
16181681
return deposit.Withdrawn
16191682

1683+
case clientrpc.DepositState_LOOPING_IN:
1684+
return deposit.LoopingIn
1685+
1686+
case clientrpc.DepositState_LOOPED_IN:
1687+
return deposit.LoopedIn
1688+
1689+
case clientrpc.DepositState_SWEEP_HTLC_TIMEOUT:
1690+
return deposit.SweepHtlcTimout
1691+
1692+
case clientrpc.DepositState_HTLC_TIMEOUT_SWEPT:
1693+
return deposit.HtlcTimeoutSwept
1694+
16201695
case clientrpc.DepositState_PUBLISH_EXPIRED:
16211696
return deposit.PublishExpiredDeposit
16221697

0 commit comments

Comments
 (0)