Skip to content

Commit 4f4e1c0

Browse files
committed
loopd: static address loop-in support
1 parent a675c8f commit 4f4e1c0

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
@@ -1466,6 +1468,57 @@ func (s *swapClientServer) GetStaticAddressSummary(ctx context.Context,
14661468
)
14671469
}
14681470

1471+
// StaticAddressLoopIn initiates a loop-in request using static address
1472+
// deposits.
1473+
func (s *swapClientServer) StaticAddressLoopIn(ctx context.Context,
1474+
in *looprpc.StaticAddressLoopInRequest) (
1475+
*looprpc.StaticAddressLoopInResponse, error) {
1476+
1477+
log.Infof("Static loop-in request received")
1478+
1479+
routeHints, err := unmarshallRouteHints(in.RouteHints)
1480+
if err != nil {
1481+
return nil, err
1482+
}
1483+
1484+
req := &loop.StaticAddressLoopInRequest{
1485+
DepositOutpoints: in.Outpoints,
1486+
MaxSwapFee: btcutil.Amount(in.MaxSwapFeeSatoshis),
1487+
Label: in.Label,
1488+
Initiator: in.Initiator,
1489+
Private: in.Private,
1490+
RouteHints: routeHints,
1491+
PaymentTimeoutSeconds: in.PaymentTimeoutSeconds,
1492+
}
1493+
1494+
if in.LastHop != nil {
1495+
lastHop, err := route.NewVertexFromBytes(in.LastHop)
1496+
if err != nil {
1497+
return nil, err
1498+
}
1499+
req.LastHop = &lastHop
1500+
}
1501+
1502+
loopIn, err := s.staticLoopInManager.DeliverLoopInRequest(ctx, req)
1503+
if err != nil {
1504+
return nil, err
1505+
}
1506+
1507+
return &looprpc.StaticAddressLoopInResponse{
1508+
SwapHash: loopIn.SwapHash[:],
1509+
State: string(loopIn.GetState()),
1510+
Amount: uint64(loopIn.TotalDepositAmount()),
1511+
HtlcCltv: loopIn.HtlcCltvExpiry,
1512+
MaxSwapFeeSatoshis: int64(loopIn.MaxSwapFee),
1513+
InitiationHeight: loopIn.InitiationHeight,
1514+
ProtocolVersion: loopIn.ProtocolVersion.String(),
1515+
Initiator: loopIn.Initiator,
1516+
Label: loopIn.Label,
1517+
PaymentTimeoutSeconds: loopIn.PaymentTimeoutSeconds,
1518+
QuotedSwapFeeSatoshis: int64(loopIn.QuotedSwapFee),
1519+
}, nil
1520+
}
1521+
14691522
func (s *swapClientServer) depositSummary(ctx context.Context,
14701523
deposits []*deposit.Deposit, stateFilter looprpc.DepositState,
14711524
outpointsFilter []string) (*looprpc.StaticAddressSummaryResponse,
@@ -1477,6 +1530,8 @@ func (s *swapClientServer) depositSummary(ctx context.Context,
14771530
valueDeposited int64
14781531
valueExpired int64
14791532
valueWithdrawn int64
1533+
valueLoopedIn int64
1534+
htlcTimeoutSwept int64
14801535
)
14811536

14821537
// Value unconfirmed.
@@ -1502,6 +1557,12 @@ func (s *swapClientServer) depositSummary(ctx context.Context,
15021557

15031558
case deposit.Withdrawn:
15041559
valueWithdrawn += value
1560+
1561+
case deposit.LoopedIn:
1562+
valueLoopedIn += value
1563+
1564+
case deposit.HtlcTimeoutSwept:
1565+
htlcTimeoutSwept += value
15051566
}
15061567
}
15071568

@@ -1530,7 +1591,7 @@ func (s *swapClientServer) depositSummary(ctx context.Context,
15301591
return true
15311592
}
15321593

1533-
return d.GetState() == toServerState(stateFilter)
1594+
return d.IsInState(toServerState(stateFilter))
15341595
}
15351596
clientDeposits = filter(deposits, f)
15361597
}
@@ -1548,13 +1609,15 @@ func (s *swapClientServer) depositSummary(ctx context.Context,
15481609
}
15491610

15501611
return &looprpc.StaticAddressSummaryResponse{
1551-
StaticAddress: address.String(),
1552-
TotalNumDeposits: uint32(totalNumDeposits),
1553-
ValueUnconfirmedSatoshis: valueUnconfirmed,
1554-
ValueDepositedSatoshis: valueDeposited,
1555-
ValueExpiredSatoshis: valueExpired,
1556-
ValueWithdrawnSatoshis: valueWithdrawn,
1557-
FilteredDeposits: clientDeposits,
1612+
StaticAddress: address.String(),
1613+
TotalNumDeposits: uint32(totalNumDeposits),
1614+
ValueUnconfirmedSatoshis: valueUnconfirmed,
1615+
ValueDepositedSatoshis: valueDeposited,
1616+
ValueExpiredSatoshis: valueExpired,
1617+
ValueWithdrawnSatoshis: valueWithdrawn,
1618+
ValueLoopedInSatoshis: valueLoopedIn,
1619+
ValueHtlcTimeoutSweepsSatoshis: htlcTimeoutSwept,
1620+
FilteredDeposits: clientDeposits,
15581621
}, nil
15591622
}
15601623

@@ -1597,6 +1660,18 @@ func toClientState(state fsm.StateType) looprpc.DepositState {
15971660
case deposit.PublishExpirySweep:
15981661
return looprpc.DepositState_PUBLISH_EXPIRED
15991662

1663+
case deposit.LoopingIn:
1664+
return looprpc.DepositState_LOOPING_IN
1665+
1666+
case deposit.LoopedIn:
1667+
return looprpc.DepositState_LOOPED_IN
1668+
1669+
case deposit.SweepHtlcTimeout:
1670+
return looprpc.DepositState_SWEEP_HTLC_TIMEOUT
1671+
1672+
case deposit.HtlcTimeoutSwept:
1673+
return looprpc.DepositState_HTLC_TIMEOUT_SWEPT
1674+
16001675
case deposit.WaitForExpirySweep:
16011676
return looprpc.DepositState_WAIT_FOR_EXPIRY_SWEEP
16021677

@@ -1622,6 +1697,18 @@ func toServerState(state looprpc.DepositState) fsm.StateType {
16221697
case looprpc.DepositState_PUBLISH_EXPIRED:
16231698
return deposit.PublishExpirySweep
16241699

1700+
case looprpc.DepositState_LOOPING_IN:
1701+
return deposit.LoopingIn
1702+
1703+
case looprpc.DepositState_LOOPED_IN:
1704+
return deposit.LoopedIn
1705+
1706+
case looprpc.DepositState_SWEEP_HTLC_TIMEOUT:
1707+
return deposit.SweepHtlcTimeout
1708+
1709+
case looprpc.DepositState_HTLC_TIMEOUT_SWEPT:
1710+
return deposit.HtlcTimeoutSwept
1711+
16251712
case looprpc.DepositState_WAIT_FOR_EXPIRY_SWEEP:
16261713
return deposit.WaitForExpirySweep
16271714

0 commit comments

Comments
 (0)