Skip to content

Commit 1aafb48

Browse files
committed
staticaddr: deposits for server and daemon
1 parent 50ad732 commit 1aafb48

File tree

4 files changed

+146
-135
lines changed

4 files changed

+146
-135
lines changed

loopd/daemon.go

Lines changed: 82 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import (
2121
"github.com/lightninglabs/loop/loopd/perms"
2222
"github.com/lightninglabs/loop/loopdb"
2323
loop_looprpc "github.com/lightninglabs/loop/looprpc"
24-
"github.com/lightninglabs/loop/staticaddr"
24+
"github.com/lightninglabs/loop/staticaddr/address"
25+
"github.com/lightninglabs/loop/staticaddr/deposit"
2526
loop_swaprpc "github.com/lightninglabs/loop/swapserverrpc"
2627
"github.com/lightninglabs/loop/sweepbatcher"
2728
"github.com/lightningnetwork/lnd/clock"
@@ -68,12 +69,6 @@ type Daemon struct {
6869
// same process.
6970
swapClientServer
7071

71-
// AddressServer is the embedded RPC server that satisfies the
72-
// static address client RPC interface. We embed this struct so the
73-
// Daemon itself can be registered to an existing grpc.Server to run as
74-
// a subserver in the same process.
75-
*staticaddr.AddressServer
76-
7772
// ErrChan is an error channel that users of the Daemon struct must use
7873
// to detect runtime errors and also whether a shutdown is fully
7974
// completed.
@@ -239,7 +234,6 @@ func (d *Daemon) startWebServers() error {
239234
grpc.StreamInterceptor(streamInterceptor),
240235
)
241236
loop_looprpc.RegisterSwapClientServer(d.grpcServer, d)
242-
loop_looprpc.RegisterStaticAddressClientServer(d.grpcServer, d)
243237

244238
// Register our debug server if it is compiled in.
245239
d.registerDebugServer()
@@ -446,6 +440,11 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
446440
swapClient.Conn,
447441
)
448442

443+
// Create a static address server client.
444+
staticAddressClient := loop_swaprpc.NewStaticAddressServerClient(
445+
swapClient.Conn,
446+
)
447+
449448
// Both the client RPC server and the swap server client should stop
450449
// on main context cancel. So we create it early and pass it down.
451450
d.mainCtx, d.mainCtxCancel = context.WithCancel(context.Background())
@@ -506,6 +505,9 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
506505
var (
507506
reservationManager *reservation.Manager
508507
instantOutManager *instantout.Manager
508+
509+
staticAddressManager *address.Manager
510+
depositManager *deposit.Manager
509511
)
510512
// Create the reservation and instantout managers.
511513
if d.cfg.EnableExperimental {
@@ -542,43 +544,50 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
542544
instantOutManager = instantout.NewInstantOutManager(
543545
instantOutConfig,
544546
)
547+
548+
// Static address manager setup.
549+
staticAddressStore := address.NewSqlStore(baseDb)
550+
addrCfg := &address.ManagerConfig{
551+
AddressClient: staticAddressClient,
552+
FetchL402: swapClient.Server.FetchL402,
553+
Store: staticAddressStore,
554+
WalletKit: d.lnd.WalletKit,
555+
ChainParams: d.lnd.ChainParams,
556+
}
557+
staticAddressManager = address.NewManager(addrCfg)
558+
559+
// Static address deposit manager setup.
560+
depositStore := deposit.NewSqlStore(baseDb)
561+
depoCfg := &deposit.ManagerConfig{
562+
AddressClient: staticAddressClient,
563+
AddressManager: staticAddressManager,
564+
SwapClient: swapClient,
565+
Store: depositStore,
566+
WalletKit: d.lnd.WalletKit,
567+
ChainParams: d.lnd.ChainParams,
568+
ChainNotifier: d.lnd.ChainNotifier,
569+
Signer: d.lnd.Signer,
570+
}
571+
depositManager = deposit.NewManager(depoCfg)
545572
}
546573

547574
// Now finally fully initialize the swap client RPC server instance.
548575
d.swapClientServer = swapClientServer{
549-
config: d.cfg,
550-
network: lndclient.Network(d.cfg.Network),
551-
impl: swapClient,
552-
liquidityMgr: getLiquidityManager(swapClient),
553-
lnd: &d.lnd.LndServices,
554-
swaps: make(map[lntypes.Hash]loop.SwapInfo),
555-
subscribers: make(map[int]chan<- interface{}),
556-
statusChan: make(chan loop.SwapInfo),
557-
mainCtx: d.mainCtx,
558-
reservationManager: reservationManager,
559-
instantOutManager: instantOutManager,
576+
config: d.cfg,
577+
network: lndclient.Network(d.cfg.Network),
578+
impl: swapClient,
579+
liquidityMgr: getLiquidityManager(swapClient),
580+
lnd: &d.lnd.LndServices,
581+
swaps: make(map[lntypes.Hash]loop.SwapInfo),
582+
subscribers: make(map[int]chan<- interface{}),
583+
statusChan: make(chan loop.SwapInfo),
584+
mainCtx: d.mainCtx,
585+
reservationManager: reservationManager,
586+
instantOutManager: instantOutManager,
587+
staticAddressManager: staticAddressManager,
588+
depositManager: depositManager,
560589
}
561590

562-
// Create a static address server client.
563-
staticAddressClient := loop_swaprpc.NewStaticAddressServerClient(
564-
swapClient.Conn,
565-
)
566-
567-
store := staticaddr.NewSqlStore(baseDb)
568-
569-
cfg := &staticaddr.ManagerConfig{
570-
AddressClient: staticAddressClient,
571-
SwapClient: swapClient,
572-
Store: store,
573-
WalletKit: d.lnd.WalletKit,
574-
ChainParams: d.lnd.ChainParams,
575-
}
576-
staticAddressManager := staticaddr.NewAddressManager(cfg)
577-
578-
d.AddressServer = staticaddr.NewAddressServer(
579-
staticAddressClient, staticAddressManager,
580-
)
581-
582591
// Retrieve all currently existing swaps from the database.
583592
swapsList, err := d.impl.FetchSwaps(d.mainCtx)
584593
if err != nil {
@@ -670,20 +679,43 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
670679
}
671680

672681
// Start the static address manager.
673-
d.wg.Add(1)
674-
go func() {
675-
defer d.wg.Done()
682+
if staticAddressManager != nil {
683+
d.wg.Add(1)
684+
go func() {
685+
defer d.wg.Done()
676686

677-
log.Info("Starting static address manager...")
678-
err = staticAddressManager.Run(d.mainCtx)
679-
if err != nil && !errors.Is(context.Canceled, err) {
680-
d.internalErrChan <- err
681-
}
687+
log.Info("Starting static address manager...")
688+
err = staticAddressManager.Run(d.mainCtx)
689+
if err != nil && !errors.Is(context.Canceled, err) {
690+
d.internalErrChan <- err
691+
}
692+
log.Info("Static address manager stopped")
693+
}()
694+
}
682695

683-
log.Info("Static address manager stopped")
684-
}()
696+
// Start the static address deposit manager.
697+
if depositManager != nil {
698+
d.wg.Add(1)
699+
go func() {
700+
defer d.wg.Done()
685701

686-
staticAddressManager.WaitInitComplete()
702+
// Lnd's GetInfo call supplies us with the current block
703+
// height.
704+
info, err := d.lnd.Client.GetInfo(d.mainCtx)
705+
if err != nil {
706+
d.internalErrChan <- err
707+
return
708+
}
709+
710+
log.Info("Starting static address deposit manager...")
711+
err = depositManager.Run(d.mainCtx, info.BlockHeight)
712+
if err != nil && !errors.Is(context.Canceled, err) {
713+
d.internalErrChan <- err
714+
}
715+
log.Info("Static address deposit manager stopped")
716+
}()
717+
depositManager.WaitInitComplete()
718+
}
687719

688720
// Last, start our internal error handler. This will return exactly one
689721
// error or nil on the main error channel to inform the caller that

loopd/perms/perms.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ var RequiredPermissions = map[string][]bakery.Op{
7373
Entity: "auth",
7474
Action: "read",
7575
}},
76-
"/looprpc.StaticAddressClient/NewAddress": {{
76+
"/looprpc.SwapClient/NewStaticAddress": {{
7777
Entity: "swap",
7878
Action: "read",
7979
}, {
8080
Entity: "loop",
8181
Action: "in",
8282
}},
83-
"/looprpc.StaticAddressClient/ListUnspent": {{
83+
"/looprpc.SwapClient/ListUnspentDeposits": {{
8484
Entity: "swap",
8585
Action: "read",
8686
}, {

loopd/swapclient_server.go

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
"github.com/lightninglabs/loop/liquidity"
2525
"github.com/lightninglabs/loop/loopdb"
2626
clientrpc "github.com/lightninglabs/loop/looprpc"
27+
"github.com/lightninglabs/loop/staticaddr/address"
28+
"github.com/lightninglabs/loop/staticaddr/deposit"
2729
"github.com/lightninglabs/loop/swap"
2830
looprpc "github.com/lightninglabs/loop/swapserverrpc"
2931
"github.com/lightningnetwork/lnd/lnrpc/walletrpc"
@@ -76,19 +78,21 @@ type swapClientServer struct {
7678
clientrpc.UnimplementedSwapClientServer
7779
clientrpc.UnimplementedDebugServer
7880

79-
config *Config
80-
network lndclient.Network
81-
impl *loop.Client
82-
liquidityMgr *liquidity.Manager
83-
lnd *lndclient.LndServices
84-
reservationManager *reservation.Manager
85-
instantOutManager *instantout.Manager
86-
swaps map[lntypes.Hash]loop.SwapInfo
87-
subscribers map[int]chan<- interface{}
88-
statusChan chan loop.SwapInfo
89-
nextSubscriberID int
90-
swapsLock sync.Mutex
91-
mainCtx context.Context
81+
config *Config
82+
network lndclient.Network
83+
impl *loop.Client
84+
liquidityMgr *liquidity.Manager
85+
lnd *lndclient.LndServices
86+
reservationManager *reservation.Manager
87+
instantOutManager *instantout.Manager
88+
staticAddressManager *address.Manager
89+
depositManager *deposit.Manager
90+
swaps map[lntypes.Hash]loop.SwapInfo
91+
subscribers map[int]chan<- interface{}
92+
statusChan chan loop.SwapInfo
93+
nextSubscriberID int
94+
swapsLock sync.Mutex
95+
mainCtx context.Context
9296
}
9397

9498
// LoopOut initiates a loop out swap with the given parameters. The call returns
@@ -1299,6 +1303,51 @@ func rpcInstantOut(instantOut *instantout.InstantOut) *clientrpc.InstantOut {
12991303
}
13001304
}
13011305

1306+
// NewStaticAddress is the rpc endpoint for loop clients to request a new static
1307+
// address.
1308+
func (s *swapClientServer) NewStaticAddress(ctx context.Context,
1309+
_ *clientrpc.NewStaticAddressRequest) (
1310+
*clientrpc.NewStaticAddressResponse, error) {
1311+
1312+
staticAddress, err := s.staticAddressManager.NewAddress(ctx)
1313+
if err != nil {
1314+
return nil, err
1315+
}
1316+
1317+
return &clientrpc.NewStaticAddressResponse{
1318+
Address: staticAddress.String(),
1319+
}, nil
1320+
}
1321+
1322+
// ListUnspentDeposits returns a list of utxos behind the static address.
1323+
func (s *swapClientServer) ListUnspentDeposits(ctx context.Context,
1324+
req *clientrpc.ListUnspentDepositsRequest) (
1325+
*clientrpc.ListUnspentDepositsResponse, error) {
1326+
1327+
// List all unspent utxos the wallet sees, regardless of the number of
1328+
// confirmations.
1329+
staticAddress, utxos, err := s.staticAddressManager.ListUnspentRaw(
1330+
ctx, req.MinConfs, req.MaxConfs,
1331+
)
1332+
if err != nil {
1333+
return nil, err
1334+
}
1335+
1336+
// Prepare the list response.
1337+
var respUtxos []*clientrpc.Utxo
1338+
for _, u := range utxos {
1339+
utxo := &clientrpc.Utxo{
1340+
StaticAddress: staticAddress.String(),
1341+
AmountSat: int64(u.Value),
1342+
Confirmations: u.Confirmations,
1343+
Outpoint: u.OutPoint.String(),
1344+
}
1345+
respUtxos = append(respUtxos, utxo)
1346+
}
1347+
1348+
return &clientrpc.ListUnspentDepositsResponse{Utxos: respUtxos}, nil
1349+
}
1350+
13021351
func rpcAutoloopReason(reason liquidity.Reason) (clientrpc.AutoReason, error) {
13031352
switch reason {
13041353
case liquidity.ReasonNone:

staticaddr/server.go

Lines changed: 0 additions & 70 deletions
This file was deleted.

0 commit comments

Comments
 (0)