Skip to content

Commit 1917e78

Browse files
committed
staticaddr: deposits for server and daemon
1 parent 4b012b6 commit 1917e78

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()
@@ -438,6 +432,11 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
438432
swapClient.Conn,
439433
)
440434

435+
// Create a static address server client.
436+
staticAddressClient := loop_swaprpc.NewStaticAddressServerClient(
437+
swapClient.Conn,
438+
)
439+
441440
// Both the client RPC server and the swap server client should stop
442441
// on main context cancel. So we create it early and pass it down.
443442
d.mainCtx, d.mainCtxCancel = context.WithCancel(context.Background())
@@ -498,6 +497,9 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
498497
var (
499498
reservationManager *reservation.Manager
500499
instantOutManager *instantout.Manager
500+
501+
staticAddressManager *address.Manager
502+
depositManager *deposit.Manager
501503
)
502504
// Create the reservation and instantout managers.
503505
if d.cfg.EnableExperimental {
@@ -534,43 +536,50 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
534536
instantOutManager = instantout.NewInstantOutManager(
535537
instantOutConfig,
536538
)
539+
540+
// Static address manager setup.
541+
staticAddressStore := address.NewSqlStore(baseDb)
542+
addrCfg := &address.ManagerConfig{
543+
AddressClient: staticAddressClient,
544+
FetchL402: swapClient.Server.FetchL402,
545+
Store: staticAddressStore,
546+
WalletKit: d.lnd.WalletKit,
547+
ChainParams: d.lnd.ChainParams,
548+
}
549+
staticAddressManager = address.NewManager(addrCfg)
550+
551+
// Static address deposit manager setup.
552+
depositStore := deposit.NewSqlStore(baseDb)
553+
depoCfg := &deposit.ManagerConfig{
554+
AddressClient: staticAddressClient,
555+
AddressManager: staticAddressManager,
556+
SwapClient: swapClient,
557+
Store: depositStore,
558+
WalletKit: d.lnd.WalletKit,
559+
ChainParams: d.lnd.ChainParams,
560+
ChainNotifier: d.lnd.ChainNotifier,
561+
Signer: d.lnd.Signer,
562+
}
563+
depositManager = deposit.NewManager(depoCfg)
537564
}
538565

539566
// Now finally fully initialize the swap client RPC server instance.
540567
d.swapClientServer = swapClientServer{
541-
config: d.cfg,
542-
network: lndclient.Network(d.cfg.Network),
543-
impl: swapClient,
544-
liquidityMgr: getLiquidityManager(swapClient),
545-
lnd: &d.lnd.LndServices,
546-
swaps: make(map[lntypes.Hash]loop.SwapInfo),
547-
subscribers: make(map[int]chan<- interface{}),
548-
statusChan: make(chan loop.SwapInfo),
549-
mainCtx: d.mainCtx,
550-
reservationManager: reservationManager,
551-
instantOutManager: instantOutManager,
568+
config: d.cfg,
569+
network: lndclient.Network(d.cfg.Network),
570+
impl: swapClient,
571+
liquidityMgr: getLiquidityManager(swapClient),
572+
lnd: &d.lnd.LndServices,
573+
swaps: make(map[lntypes.Hash]loop.SwapInfo),
574+
subscribers: make(map[int]chan<- interface{}),
575+
statusChan: make(chan loop.SwapInfo),
576+
mainCtx: d.mainCtx,
577+
reservationManager: reservationManager,
578+
instantOutManager: instantOutManager,
579+
staticAddressManager: staticAddressManager,
580+
depositManager: depositManager,
552581
}
553582

554-
// Create a static address server client.
555-
staticAddressClient := loop_swaprpc.NewStaticAddressServerClient(
556-
swapClient.Conn,
557-
)
558-
559-
store := staticaddr.NewSqlStore(baseDb)
560-
561-
cfg := &staticaddr.ManagerConfig{
562-
AddressClient: staticAddressClient,
563-
SwapClient: swapClient,
564-
Store: store,
565-
WalletKit: d.lnd.WalletKit,
566-
ChainParams: d.lnd.ChainParams,
567-
}
568-
staticAddressManager := staticaddr.NewAddressManager(cfg)
569-
570-
d.AddressServer = staticaddr.NewAddressServer(
571-
staticAddressClient, staticAddressManager,
572-
)
573-
574583
// Retrieve all currently existing swaps from the database.
575584
swapsList, err := d.impl.FetchSwaps(d.mainCtx)
576585
if err != nil {
@@ -662,20 +671,43 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
662671
}
663672

664673
// Start the static address manager.
665-
d.wg.Add(1)
666-
go func() {
667-
defer d.wg.Done()
674+
if staticAddressManager != nil {
675+
d.wg.Add(1)
676+
go func() {
677+
defer d.wg.Done()
668678

669-
log.Info("Starting static address manager...")
670-
err = staticAddressManager.Run(d.mainCtx)
671-
if err != nil && !errors.Is(context.Canceled, err) {
672-
d.internalErrChan <- err
673-
}
679+
log.Info("Starting static address manager...")
680+
err = staticAddressManager.Run(d.mainCtx)
681+
if err != nil && !errors.Is(context.Canceled, err) {
682+
d.internalErrChan <- err
683+
}
684+
log.Info("Static address manager stopped")
685+
}()
686+
}
674687

675-
log.Info("Static address manager stopped")
676-
}()
688+
// Start the static address deposit manager.
689+
if depositManager != nil {
690+
d.wg.Add(1)
691+
go func() {
692+
defer d.wg.Done()
677693

678-
staticAddressManager.WaitInitComplete()
694+
// Lnd's GetInfo call supplies us with the current block
695+
// height.
696+
info, err := d.lnd.Client.GetInfo(d.mainCtx)
697+
if err != nil {
698+
d.internalErrChan <- err
699+
return
700+
}
701+
702+
log.Info("Starting static address deposit manager...")
703+
err = depositManager.Run(d.mainCtx, info.BlockHeight)
704+
if err != nil && !errors.Is(context.Canceled, err) {
705+
d.internalErrChan <- err
706+
}
707+
log.Info("Static address deposit manager stopped")
708+
}()
709+
depositManager.WaitInitComplete()
710+
}
679711

680712
// Last, start our internal error handler. This will return exactly one
681713
// 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
@@ -69,14 +69,14 @@ var RequiredPermissions = map[string][]bakery.Op{
6969
Entity: "loop",
7070
Action: "in",
7171
}},
72-
"/looprpc.StaticAddressClient/NewAddress": {{
72+
"/looprpc.SwapClient/NewStaticAddress": {{
7373
Entity: "swap",
7474
Action: "read",
7575
}, {
7676
Entity: "loop",
7777
Action: "in",
7878
}},
79-
"/looprpc.StaticAddressClient/ListUnspent": {{
79+
"/looprpc.SwapClient/ListUnspentDeposits": {{
8080
Entity: "swap",
8181
Action: "read",
8282
}, {

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
@@ -1272,6 +1276,51 @@ func rpcInstantOut(instantOut *instantout.InstantOut) *clientrpc.InstantOut {
12721276
}
12731277
}
12741278

1279+
// NewStaticAddress is the rpc endpoint for loop clients to request a new static
1280+
// address.
1281+
func (s *swapClientServer) NewStaticAddress(ctx context.Context,
1282+
_ *clientrpc.NewStaticAddressRequest) (
1283+
*clientrpc.NewStaticAddressResponse, error) {
1284+
1285+
staticAddress, err := s.staticAddressManager.NewAddress(ctx)
1286+
if err != nil {
1287+
return nil, err
1288+
}
1289+
1290+
return &clientrpc.NewStaticAddressResponse{
1291+
Address: staticAddress.String(),
1292+
}, nil
1293+
}
1294+
1295+
// ListUnspentDeposits returns a list of utxos behind the static address.
1296+
func (s *swapClientServer) ListUnspentDeposits(ctx context.Context,
1297+
req *clientrpc.ListUnspentDepositsRequest) (
1298+
*clientrpc.ListUnspentDepositsResponse, error) {
1299+
1300+
// List all unspent utxos the wallet sees, regardless of the number of
1301+
// confirmations.
1302+
staticAddress, utxos, err := s.staticAddressManager.ListUnspentRaw(
1303+
ctx, req.MinConfs, req.MaxConfs,
1304+
)
1305+
if err != nil {
1306+
return nil, err
1307+
}
1308+
1309+
// Prepare the list response.
1310+
var respUtxos []*clientrpc.Utxo
1311+
for _, u := range utxos {
1312+
utxo := &clientrpc.Utxo{
1313+
StaticAddress: staticAddress.String(),
1314+
AmountSat: int64(u.Value),
1315+
Confirmations: u.Confirmations,
1316+
Outpoint: u.OutPoint.String(),
1317+
}
1318+
respUtxos = append(respUtxos, utxo)
1319+
}
1320+
1321+
return &clientrpc.ListUnspentDepositsResponse{Utxos: respUtxos}, nil
1322+
}
1323+
12751324
func rpcAutoloopReason(reason liquidity.Reason) (clientrpc.AutoReason, error) {
12761325
switch reason {
12771326
case liquidity.ReasonNone:

staticaddr/server.go

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

0 commit comments

Comments
 (0)