Skip to content

Commit 6f9349a

Browse files
committed
loopd: static address loop-in support
1 parent 69ef7d8 commit 6f9349a

File tree

3 files changed

+142
-8
lines changed

3 files changed

+142
-8
lines changed

loopd/daemon.go

Lines changed: 53 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,29 @@ 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+
ValidateLoopInContract: loop.ValidateLoopInContract,
615+
}
616+
staticLoopInManager = loopin.NewManager(loopinCfg)
592617
}
593618

594619
// Now finally fully initialize the swap client RPC server instance.
@@ -607,6 +632,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
607632
staticAddressManager: staticAddressManager,
608633
depositManager: depositManager,
609634
withdrawalManager: withdrawalManager,
635+
staticLoopInManager: staticLoopInManager,
610636
}
611637

612638
// Retrieve all currently existing swaps from the database.
@@ -764,6 +790,33 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
764790
withdrawalManager.WaitInitComplete()
765791
}
766792

793+
// Start the static address loop-in manager.
794+
if staticLoopInManager != nil {
795+
d.wg.Add(1)
796+
go func() {
797+
defer d.wg.Done()
798+
799+
// Lnd's GetInfo call supplies us with the current block
800+
// height.
801+
info, err := d.lnd.Client.GetInfo(d.mainCtx)
802+
if err != nil {
803+
d.internalErrChan <- err
804+
return
805+
}
806+
807+
log.Info("Starting static address loop-in manager...")
808+
err = staticLoopInManager.Run(
809+
d.mainCtx, info.BlockHeight,
810+
)
811+
if err != nil && !errors.Is(context.Canceled, err) {
812+
d.internalErrChan <- err
813+
}
814+
log.Info("Starting static address loop-in manager " +
815+
"stopped")
816+
}()
817+
staticLoopInManager.WaitInitComplete()
818+
}
819+
767820
// Last, start our internal error handler. This will return exactly one
768821
// error or nil on the main error channel to inform the caller that
769822
// 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: 82 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,44 @@ 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+
req := &loop.StaticAddressLoopInRequest{
1480+
DepositOutpoints: in.Outpoints,
1481+
MaxSwapFee: btcutil.Amount(in.MaxSwapFee),
1482+
Label: in.Label,
1483+
Initiator: in.Initiator,
1484+
Private: in.Private,
1485+
RouteHints: routeHints,
1486+
}
1487+
1488+
if in.LastHop != nil {
1489+
lastHop, err := route.NewVertexFromBytes(in.LastHop)
1490+
if err != nil {
1491+
return nil, err
1492+
}
1493+
req.LastHop = &lastHop
1494+
}
1495+
1496+
err = s.staticLoopInManager.InitiateLoopIn(req)
1497+
if err != nil {
1498+
return nil, err
1499+
}
1500+
1501+
return &clientrpc.StaticAddressLoopInResponse{}, nil
1502+
}
1503+
14641504
func (s *swapClientServer) depositSummary(ctx context.Context,
14651505
deposits []*deposit.Deposit, stateFilter clientrpc.DepositState,
14661506
outpointsFilter []string) (*clientrpc.StaticAddressSummaryResponse,
@@ -1472,6 +1512,8 @@ func (s *swapClientServer) depositSummary(ctx context.Context,
14721512
valueDeposited int64
14731513
valueExpired int64
14741514
valueWithdrawn int64
1515+
valueLoopedIn int64
1516+
htlcTimeoutSwept int64
14751517
)
14761518

14771519
// Value unconfirmed.
@@ -1497,6 +1539,12 @@ func (s *swapClientServer) depositSummary(ctx context.Context,
14971539

14981540
case deposit.Withdrawn:
14991541
valueWithdrawn += value
1542+
1543+
case deposit.LoopedIn:
1544+
valueLoopedIn += value
1545+
1546+
case deposit.HtlcTimeoutSwept:
1547+
htlcTimeoutSwept += value
15001548
}
15011549
}
15021550

@@ -1525,7 +1573,7 @@ func (s *swapClientServer) depositSummary(ctx context.Context,
15251573
return true
15261574
}
15271575

1528-
return d.GetState() == toServerState(stateFilter)
1576+
return d.IsInState(toServerState(stateFilter))
15291577
}
15301578
clientDeposits = filter(deposits, f)
15311579
}
@@ -1543,13 +1591,15 @@ func (s *swapClientServer) depositSummary(ctx context.Context,
15431591
}
15441592

15451593
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,
1594+
StaticAddress: address.String(),
1595+
TotalNumDeposits: uint32(totalNumDeposits),
1596+
ValueUnconfirmed: valueUnconfirmed,
1597+
ValueDeposited: valueDeposited,
1598+
ValueExpired: valueExpired,
1599+
ValueWithdrawn: valueWithdrawn,
1600+
ValueLoopedIn: valueLoopedIn,
1601+
ValueHtlcTimeoutSweeps: htlcTimeoutSwept,
1602+
FilteredDeposits: clientDeposits,
15531603
}, nil
15541604
}
15551605

@@ -1592,6 +1642,18 @@ func toClientState(state fsm.StateType) clientrpc.DepositState {
15921642
case deposit.PublishExpiredDeposit:
15931643
return clientrpc.DepositState_PUBLISH_EXPIRED
15941644

1645+
case deposit.LoopingIn:
1646+
return clientrpc.DepositState_LOOPING_IN
1647+
1648+
case deposit.LoopedIn:
1649+
return clientrpc.DepositState_LOOPED_IN
1650+
1651+
case deposit.SweepHtlcTimeout:
1652+
return clientrpc.DepositState_SWEEP_HTLC_TIMEOUT
1653+
1654+
case deposit.HtlcTimeoutSwept:
1655+
return clientrpc.DepositState_HTLC_TIMEOUT_SWEPT
1656+
15951657
case deposit.WaitForExpirySweep:
15961658
return clientrpc.DepositState_WAIT_FOR_EXPIRY_SWEEP
15971659

@@ -1617,6 +1679,18 @@ func toServerState(state clientrpc.DepositState) fsm.StateType {
16171679
case clientrpc.DepositState_WITHDRAWN:
16181680
return deposit.Withdrawn
16191681

1682+
case clientrpc.DepositState_LOOPING_IN:
1683+
return deposit.LoopingIn
1684+
1685+
case clientrpc.DepositState_LOOPED_IN:
1686+
return deposit.LoopedIn
1687+
1688+
case clientrpc.DepositState_SWEEP_HTLC_TIMEOUT:
1689+
return deposit.SweepHtlcTimeout
1690+
1691+
case clientrpc.DepositState_HTLC_TIMEOUT_SWEPT:
1692+
return deposit.HtlcTimeoutSwept
1693+
16201694
case clientrpc.DepositState_PUBLISH_EXPIRED:
16211695
return deposit.PublishExpiredDeposit
16221696

0 commit comments

Comments
 (0)