|
|
|
@ -18,7 +18,11 @@ import (
|
|
|
|
|
"github.com/lightninglabs/loop"
|
|
|
|
|
"github.com/lightninglabs/loop/loopd/perms"
|
|
|
|
|
"github.com/lightninglabs/loop/loopdb"
|
|
|
|
|
"github.com/lightninglabs/loop/looprpc"
|
|
|
|
|
|
|
|
|
|
"github.com/lightninglabs/loop/instantout/reservation"
|
|
|
|
|
loop_looprpc "github.com/lightninglabs/loop/looprpc"
|
|
|
|
|
|
|
|
|
|
loop_swaprpc "github.com/lightninglabs/loop/swapserverrpc"
|
|
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
|
|
|
|
"github.com/lightningnetwork/lnd/macaroons"
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
@ -62,6 +66,10 @@ type Daemon struct {
|
|
|
|
|
// same process.
|
|
|
|
|
swapClientServer
|
|
|
|
|
|
|
|
|
|
// reservationManager is the manager that handles all reservation state
|
|
|
|
|
// machines.
|
|
|
|
|
reservationManager *reservation.Manager
|
|
|
|
|
|
|
|
|
|
// ErrChan is an error channel that users of the Daemon struct must use
|
|
|
|
|
// to detect runtime errors and also whether a shutdown is fully
|
|
|
|
|
// completed.
|
|
|
|
@ -226,7 +234,7 @@ func (d *Daemon) startWebServers() error {
|
|
|
|
|
grpc.UnaryInterceptor(unaryInterceptor),
|
|
|
|
|
grpc.StreamInterceptor(streamInterceptor),
|
|
|
|
|
)
|
|
|
|
|
looprpc.RegisterSwapClientServer(d.grpcServer, d)
|
|
|
|
|
loop_looprpc.RegisterSwapClientServer(d.grpcServer, d)
|
|
|
|
|
|
|
|
|
|
// Register our debug server if it is compiled in.
|
|
|
|
|
d.registerDebugServer()
|
|
|
|
@ -286,7 +294,7 @@ func (d *Daemon) startWebServers() error {
|
|
|
|
|
restProxyDest, "[::]", "[::1]", 1,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
err = looprpc.RegisterSwapClientHandlerFromEndpoint(
|
|
|
|
|
err = loop_looprpc.RegisterSwapClientHandlerFromEndpoint(
|
|
|
|
|
ctx, mux, restProxyDest, proxyOpts,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
@ -399,7 +407,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
swapDb, _, err := openDatabase(d.cfg, chainParams)
|
|
|
|
|
swapDb, baseDb, err := openDatabase(d.cfg, chainParams)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
@ -413,6 +421,15 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
|
|
|
|
|
}
|
|
|
|
|
d.clientCleanup = clientCleanup
|
|
|
|
|
|
|
|
|
|
// Create a reservation server client.
|
|
|
|
|
reservationClient := loop_swaprpc.NewReservationServiceClient(
|
|
|
|
|
swapClient.Conn,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Both the client RPC server and the swap server client should stop
|
|
|
|
|
// on main context cancel. So we create it early and pass it down.
|
|
|
|
|
d.mainCtx, d.mainCtxCancel = context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
|
|
// Add our debug permissions to our main set of required permissions
|
|
|
|
|
// if compiled in.
|
|
|
|
|
for endpoint, perm := range debugRequiredPermissions {
|
|
|
|
@ -466,17 +483,32 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create the reservation rpc server.
|
|
|
|
|
reservationStore := reservation.NewSQLStore(baseDb)
|
|
|
|
|
reservationConfig := &reservation.Config{
|
|
|
|
|
Store: reservationStore,
|
|
|
|
|
Wallet: d.lnd.WalletKit,
|
|
|
|
|
ChainNotifier: d.lnd.ChainNotifier,
|
|
|
|
|
ReservationClient: reservationClient,
|
|
|
|
|
FetchL402: swapClient.Server.FetchL402,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
d.reservationManager = reservation.NewManager(
|
|
|
|
|
reservationConfig,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Now finally fully initialize the swap client RPC server instance.
|
|
|
|
|
d.swapClientServer = swapClientServer{
|
|
|
|
|
config: d.cfg,
|
|
|
|
|
network: lndclient.Network(d.cfg.Network),
|
|
|
|
|
impl: swapClient,
|
|
|
|
|
liquidityMgr: getLiquidityManager(swapClient),
|
|
|
|
|
lnd: &d.lnd.LndServices,
|
|
|
|
|
swaps: make(map[lntypes.Hash]loop.SwapInfo),
|
|
|
|
|
subscribers: make(map[int]chan<- interface{}),
|
|
|
|
|
statusChan: make(chan loop.SwapInfo),
|
|
|
|
|
mainCtx: d.mainCtx,
|
|
|
|
|
config: d.cfg,
|
|
|
|
|
network: lndclient.Network(d.cfg.Network),
|
|
|
|
|
impl: swapClient,
|
|
|
|
|
liquidityMgr: getLiquidityManager(swapClient),
|
|
|
|
|
lnd: &d.lnd.LndServices,
|
|
|
|
|
swaps: make(map[lntypes.Hash]loop.SwapInfo),
|
|
|
|
|
subscribers: make(map[int]chan<- interface{}),
|
|
|
|
|
statusChan: make(chan loop.SwapInfo),
|
|
|
|
|
mainCtx: d.mainCtx,
|
|
|
|
|
reservationManager: d.reservationManager,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Retrieve all currently existing swaps from the database.
|
|
|
|
@ -543,6 +575,30 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
|
|
|
|
|
log.Info("Liquidity manager stopped")
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
// Start the reservation manager.
|
|
|
|
|
d.wg.Add(1)
|
|
|
|
|
go func() {
|
|
|
|
|
defer d.wg.Done()
|
|
|
|
|
|
|
|
|
|
// We need to know the current block height to properly
|
|
|
|
|
// initialize the reservation manager.
|
|
|
|
|
getInfo, err := d.lnd.Client.GetInfo(d.mainCtx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
d.internalErrChan <- err
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Info("Starting reservation manager")
|
|
|
|
|
defer log.Info("Reservation manager stopped")
|
|
|
|
|
|
|
|
|
|
err = d.reservationManager.Run(
|
|
|
|
|
d.mainCtx, int32(getInfo.BlockHeight),
|
|
|
|
|
)
|
|
|
|
|
if err != nil && !errors.Is(err, context.Canceled) {
|
|
|
|
|
d.internalErrChan <- err
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
// Last, start our internal error handler. This will return exactly one
|
|
|
|
|
// error or nil on the main error channel to inform the caller that
|
|
|
|
|
// something went wrong or that shutdown is complete. We don't add to
|
|
|
|
|