@@ -18,7 +18,11 @@ import (
1818 "github.com/lightninglabs/loop"
1919 "github.com/lightninglabs/loop/loopd/perms"
2020 "github.com/lightninglabs/loop/loopdb"
21- "github.com/lightninglabs/loop/looprpc"
21+
22+ loop_looprpc "github.com/lightninglabs/loop/looprpc"
23+ "github.com/lightninglabs/loop/reservation"
24+
25+ loop_swaprpc "github.com/lightninglabs/loop/swapserverrpc"
2226 "github.com/lightningnetwork/lnd/lntypes"
2327 "github.com/lightningnetwork/lnd/macaroons"
2428 "google.golang.org/grpc"
@@ -62,6 +66,10 @@ type Daemon struct {
6266 // same process.
6367 swapClientServer
6468
69+ // reservationManager is the manager that handles all reservation state
70+ // machines.
71+ reservationManager * reservation.Manager
72+
6573 // ErrChan is an error channel that users of the Daemon struct must use
6674 // to detect runtime errors and also whether a shutdown is fully
6775 // completed.
@@ -226,7 +234,7 @@ func (d *Daemon) startWebServers() error {
226234 grpc .UnaryInterceptor (unaryInterceptor ),
227235 grpc .StreamInterceptor (streamInterceptor ),
228236 )
229- looprpc .RegisterSwapClientServer (d .grpcServer , d )
237+ loop_looprpc .RegisterSwapClientServer (d .grpcServer , d )
230238
231239 // Register our debug server if it is compiled in.
232240 d .registerDebugServer ()
@@ -286,7 +294,7 @@ func (d *Daemon) startWebServers() error {
286294 restProxyDest , "[::]" , "[::1]" , 1 ,
287295 )
288296 }
289- err = looprpc .RegisterSwapClientHandlerFromEndpoint (
297+ err = loop_looprpc .RegisterSwapClientHandlerFromEndpoint (
290298 ctx , mux , restProxyDest , proxyOpts ,
291299 )
292300 if err != nil {
@@ -399,7 +407,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
399407 return err
400408 }
401409
402- swapDb , _ , err := openDatabase (d .cfg , chainParams )
410+ swapDb , baseDb , err := openDatabase (d .cfg , chainParams )
403411 if err != nil {
404412 return err
405413 }
@@ -413,6 +421,15 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
413421 }
414422 d .clientCleanup = clientCleanup
415423
424+ // Create a reservation server client.
425+ reservationClient := loop_swaprpc .NewReservationServiceClient (
426+ swapClient .Conn ,
427+ )
428+
429+ // Both the client RPC server and the swap server client should stop
430+ // on main context cancel. So we create it early and pass it down.
431+ d .mainCtx , d .mainCtxCancel = context .WithCancel (context .Background ())
432+
416433 // Add our debug permissions to our main set of required permissions
417434 // if compiled in.
418435 for endpoint , perm := range debugRequiredPermissions {
@@ -466,17 +483,31 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
466483 }
467484 }
468485
486+ // Create the reservation rpc server.
487+ reservationStore := reservation .NewSQLStore (baseDb )
488+ reservationConfig := & reservation.Config {
489+ Store : reservationStore ,
490+ Wallet : d .lnd .WalletKit ,
491+ ChainNotifier : d .lnd .ChainNotifier ,
492+ ReservationClient : reservationClient ,
493+ }
494+
495+ d .reservationManager = reservation .NewReservationManager (
496+ reservationConfig ,
497+ )
498+
469499 // Now finally fully initialize the swap client RPC server instance.
470500 d .swapClientServer = swapClientServer {
471- config : d .cfg ,
472- network : lndclient .Network (d .cfg .Network ),
473- impl : swapClient ,
474- liquidityMgr : getLiquidityManager (swapClient ),
475- lnd : & d .lnd .LndServices ,
476- swaps : make (map [lntypes.Hash ]loop.SwapInfo ),
477- subscribers : make (map [int ]chan <- interface {}),
478- statusChan : make (chan loop.SwapInfo ),
479- mainCtx : d .mainCtx ,
501+ config : d .cfg ,
502+ network : lndclient .Network (d .cfg .Network ),
503+ impl : swapClient ,
504+ liquidityMgr : getLiquidityManager (swapClient ),
505+ lnd : & d .lnd .LndServices ,
506+ swaps : make (map [lntypes.Hash ]loop.SwapInfo ),
507+ subscribers : make (map [int ]chan <- interface {}),
508+ statusChan : make (chan loop.SwapInfo ),
509+ mainCtx : d .mainCtx ,
510+ reservationManager : d .reservationManager ,
480511 }
481512
482513 // Retrieve all currently existing swaps from the database.
@@ -543,6 +574,29 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
543574 log .Info ("Liquidity manager stopped" )
544575 }()
545576
577+ // Start the reservation manager.
578+ d .wg .Add (1 )
579+ go func () {
580+ defer d .wg .Done ()
581+
582+ getInfo , err := d .lnd .Client .GetInfo (d .mainCtx )
583+ if err != nil {
584+ d .internalErrChan <- err
585+ return
586+ }
587+
588+ log .Info ("Starting reservation manager" )
589+ defer log .Info ("Reservation manager stopped" )
590+
591+ err = d .reservationManager .Run (
592+ d .mainCtx , int32 (getInfo .BlockHeight ),
593+ )
594+ if err != nil && ! errors .Is (err , context .Canceled ) {
595+ d .internalErrChan <- err
596+ }
597+
598+ }()
599+
546600 // Last, start our internal error handler. This will return exactly one
547601 // error or nil on the main error channel to inform the caller that
548602 // something went wrong or that shutdown is complete. We don't add to
0 commit comments