@@ -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+ "github.com/lightninglabs/loop/instantout/reservation"
23+ loop_looprpc "github.com/lightninglabs/loop/looprpc"
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,32 @@ 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+ FetchL402 : swapClient .Server .FetchL402 ,
494+ }
495+
496+ d .reservationManager = reservation .NewManager (
497+ reservationConfig ,
498+ )
499+
469500 // Now finally fully initialize the swap client RPC server instance.
470501 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 ,
502+ config : d .cfg ,
503+ network : lndclient .Network (d .cfg .Network ),
504+ impl : swapClient ,
505+ liquidityMgr : getLiquidityManager (swapClient ),
506+ lnd : & d .lnd .LndServices ,
507+ swaps : make (map [lntypes.Hash ]loop.SwapInfo ),
508+ subscribers : make (map [int ]chan <- interface {}),
509+ statusChan : make (chan loop.SwapInfo ),
510+ mainCtx : d .mainCtx ,
511+ reservationManager : d .reservationManager ,
480512 }
481513
482514 // Retrieve all currently existing swaps from the database.
@@ -543,6 +575,30 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
543575 log .Info ("Liquidity manager stopped" )
544576 }()
545577
578+ // Start the reservation manager.
579+ d .wg .Add (1 )
580+ go func () {
581+ defer d .wg .Done ()
582+
583+ // We need to know the current block height to properly
584+ // initialize the reservation manager.
585+ getInfo , err := d .lnd .Client .GetInfo (d .mainCtx )
586+ if err != nil {
587+ d .internalErrChan <- err
588+ return
589+ }
590+
591+ log .Info ("Starting reservation manager" )
592+ defer log .Info ("Reservation manager stopped" )
593+
594+ err = d .reservationManager .Run (
595+ d .mainCtx , int32 (getInfo .BlockHeight ),
596+ )
597+ if err != nil && ! errors .Is (err , context .Canceled ) {
598+ d .internalErrChan <- err
599+ }
600+ }()
601+
546602 // Last, start our internal error handler. This will return exactly one
547603 // error or nil on the main error channel to inform the caller that
548604 // something went wrong or that shutdown is complete. We don't add to
0 commit comments