mirror of
https://github.com/lightninglabs/loop.git
synced 2026-08-13 12:33:03 +02:00
staticaddr: deposits for server and daemon
This commit is contained in:
parent
042fe9f325
commit
3914ab840f
4 changed files with 147 additions and 136 deletions
134
loopd/daemon.go
134
loopd/daemon.go
|
|
@ -22,7 +22,8 @@ import (
|
|||
"github.com/lightninglabs/loop/loopdb"
|
||||
loop_looprpc "github.com/lightninglabs/loop/looprpc"
|
||||
"github.com/lightninglabs/loop/notifications"
|
||||
"github.com/lightninglabs/loop/staticaddr"
|
||||
"github.com/lightninglabs/loop/staticaddr/address"
|
||||
"github.com/lightninglabs/loop/staticaddr/deposit"
|
||||
loop_swaprpc "github.com/lightninglabs/loop/swapserverrpc"
|
||||
"github.com/lightninglabs/loop/sweepbatcher"
|
||||
"github.com/lightningnetwork/lnd/clock"
|
||||
|
|
@ -69,12 +70,6 @@ type Daemon struct {
|
|||
// same process.
|
||||
swapClientServer
|
||||
|
||||
// AddressServer is the embedded RPC server that satisfies the
|
||||
// static address client RPC interface. We embed this struct so the
|
||||
// Daemon itself can be registered to an existing grpc.Server to run as
|
||||
// a subserver in the same process.
|
||||
*staticaddr.AddressServer
|
||||
|
||||
// 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.
|
||||
|
|
@ -240,7 +235,6 @@ func (d *Daemon) startWebServers() error {
|
|||
grpc.StreamInterceptor(streamInterceptor),
|
||||
)
|
||||
loop_looprpc.RegisterSwapClientServer(d.grpcServer, d)
|
||||
loop_looprpc.RegisterStaticAddressClientServer(d.grpcServer, d)
|
||||
|
||||
// Register our debug server if it is compiled in.
|
||||
d.registerDebugServer()
|
||||
|
|
@ -423,7 +417,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// Run the costs migration.
|
||||
// Run the cost migration.
|
||||
err = loop.MigrateLoopOutCosts(
|
||||
d.mainCtx, d.lnd.LndServices, d.cfg.MigrationRPCBatchSize,
|
||||
swapDb,
|
||||
|
|
@ -458,6 +452,11 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
|
|||
swapClient.Conn,
|
||||
)
|
||||
|
||||
// Create a static address server client.
|
||||
staticAddressClient := loop_swaprpc.NewStaticAddressServerClient(
|
||||
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())
|
||||
|
|
@ -537,6 +536,9 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
|
|||
var (
|
||||
reservationManager *reservation.Manager
|
||||
instantOutManager *instantout.Manager
|
||||
|
||||
staticAddressManager *address.Manager
|
||||
depositManager *deposit.Manager
|
||||
)
|
||||
|
||||
// Create the reservation and instantout managers.
|
||||
|
|
@ -577,43 +579,50 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
|
|||
instantOutManager = instantout.NewInstantOutManager(
|
||||
instantOutConfig,
|
||||
)
|
||||
|
||||
// Static address manager setup.
|
||||
staticAddressStore := address.NewSqlStore(baseDb)
|
||||
addrCfg := &address.ManagerConfig{
|
||||
AddressClient: staticAddressClient,
|
||||
FetchL402: swapClient.Server.FetchL402,
|
||||
Store: staticAddressStore,
|
||||
WalletKit: d.lnd.WalletKit,
|
||||
ChainParams: d.lnd.ChainParams,
|
||||
}
|
||||
staticAddressManager = address.NewManager(addrCfg)
|
||||
|
||||
// Static address deposit manager setup.
|
||||
depositStore := deposit.NewSqlStore(baseDb)
|
||||
depoCfg := &deposit.ManagerConfig{
|
||||
AddressClient: staticAddressClient,
|
||||
AddressManager: staticAddressManager,
|
||||
SwapClient: swapClient,
|
||||
Store: depositStore,
|
||||
WalletKit: d.lnd.WalletKit,
|
||||
ChainParams: d.lnd.ChainParams,
|
||||
ChainNotifier: d.lnd.ChainNotifier,
|
||||
Signer: d.lnd.Signer,
|
||||
}
|
||||
depositManager = deposit.NewManager(depoCfg)
|
||||
}
|
||||
|
||||
// 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,
|
||||
reservationManager: reservationManager,
|
||||
instantOutManager: instantOutManager,
|
||||
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: reservationManager,
|
||||
instantOutManager: instantOutManager,
|
||||
staticAddressManager: staticAddressManager,
|
||||
depositManager: depositManager,
|
||||
}
|
||||
|
||||
// Create a static address server client.
|
||||
staticAddressClient := loop_swaprpc.NewStaticAddressServerClient(
|
||||
swapClient.Conn,
|
||||
)
|
||||
|
||||
store := staticaddr.NewSqlStore(baseDb)
|
||||
|
||||
cfg := &staticaddr.ManagerConfig{
|
||||
AddressClient: staticAddressClient,
|
||||
SwapClient: swapClient,
|
||||
Store: store,
|
||||
WalletKit: d.lnd.WalletKit,
|
||||
ChainParams: d.lnd.ChainParams,
|
||||
}
|
||||
staticAddressManager := staticaddr.NewAddressManager(cfg)
|
||||
|
||||
d.AddressServer = staticaddr.NewAddressServer(
|
||||
staticAddressClient, staticAddressManager,
|
||||
)
|
||||
|
||||
// Retrieve all currently existing swaps from the database.
|
||||
swapsList, err := d.impl.FetchSwaps(d.mainCtx)
|
||||
if err != nil {
|
||||
|
|
@ -757,20 +766,43 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
|
|||
}
|
||||
|
||||
// Start the static address manager.
|
||||
d.wg.Add(1)
|
||||
go func() {
|
||||
defer d.wg.Done()
|
||||
if staticAddressManager != nil {
|
||||
d.wg.Add(1)
|
||||
go func() {
|
||||
defer d.wg.Done()
|
||||
|
||||
log.Info("Starting static address manager...")
|
||||
err = staticAddressManager.Run(d.mainCtx)
|
||||
if err != nil && !errors.Is(context.Canceled, err) {
|
||||
d.internalErrChan <- err
|
||||
}
|
||||
log.Info("Starting static address manager...")
|
||||
err = staticAddressManager.Run(d.mainCtx)
|
||||
if err != nil && !errors.Is(context.Canceled, err) {
|
||||
d.internalErrChan <- err
|
||||
}
|
||||
log.Info("Static address manager stopped")
|
||||
}()
|
||||
}
|
||||
|
||||
log.Info("Static address manager stopped")
|
||||
}()
|
||||
// Start the static address deposit manager.
|
||||
if depositManager != nil {
|
||||
d.wg.Add(1)
|
||||
go func() {
|
||||
defer d.wg.Done()
|
||||
|
||||
staticAddressManager.WaitInitComplete()
|
||||
// Lnd's GetInfo call supplies us with the current block
|
||||
// height.
|
||||
info, err := d.lnd.Client.GetInfo(d.mainCtx)
|
||||
if err != nil {
|
||||
d.internalErrChan <- err
|
||||
return
|
||||
}
|
||||
|
||||
log.Info("Starting static address deposit manager...")
|
||||
err = depositManager.Run(d.mainCtx, info.BlockHeight)
|
||||
if err != nil && !errors.Is(context.Canceled, err) {
|
||||
d.internalErrChan <- err
|
||||
}
|
||||
log.Info("Static address deposit manager stopped")
|
||||
}()
|
||||
depositManager.WaitInitComplete()
|
||||
}
|
||||
|
||||
// Last, start our internal error handler. This will return exactly one
|
||||
// error or nil on the main error channel to inform the caller that
|
||||
|
|
|
|||
|
|
@ -73,14 +73,14 @@ var RequiredPermissions = map[string][]bakery.Op{
|
|||
Entity: "auth",
|
||||
Action: "read",
|
||||
}},
|
||||
"/looprpc.StaticAddressClient/NewAddress": {{
|
||||
"/looprpc.SwapClient/NewStaticAddress": {{
|
||||
Entity: "swap",
|
||||
Action: "read",
|
||||
}, {
|
||||
Entity: "loop",
|
||||
Action: "in",
|
||||
}},
|
||||
"/looprpc.StaticAddressClient/ListUnspent": {{
|
||||
"/looprpc.SwapClient/ListUnspentDeposits": {{
|
||||
Entity: "swap",
|
||||
Action: "read",
|
||||
}, {
|
||||
|
|
|
|||
|
|
@ -24,6 +24,8 @@ import (
|
|||
"github.com/lightninglabs/loop/liquidity"
|
||||
"github.com/lightninglabs/loop/loopdb"
|
||||
"github.com/lightninglabs/loop/looprpc"
|
||||
"github.com/lightninglabs/loop/staticaddr/address"
|
||||
"github.com/lightninglabs/loop/staticaddr/deposit"
|
||||
"github.com/lightninglabs/loop/swap"
|
||||
"github.com/lightninglabs/loop/swapserverrpc"
|
||||
"github.com/lightningnetwork/lnd/lnrpc/walletrpc"
|
||||
|
|
@ -76,19 +78,21 @@ type swapClientServer struct {
|
|||
looprpc.UnimplementedSwapClientServer
|
||||
looprpc.UnimplementedDebugServer
|
||||
|
||||
config *Config
|
||||
network lndclient.Network
|
||||
impl *loop.Client
|
||||
liquidityMgr *liquidity.Manager
|
||||
lnd *lndclient.LndServices
|
||||
reservationManager *reservation.Manager
|
||||
instantOutManager *instantout.Manager
|
||||
swaps map[lntypes.Hash]loop.SwapInfo
|
||||
subscribers map[int]chan<- interface{}
|
||||
statusChan chan loop.SwapInfo
|
||||
nextSubscriberID int
|
||||
swapsLock sync.Mutex
|
||||
mainCtx context.Context
|
||||
config *Config
|
||||
network lndclient.Network
|
||||
impl *loop.Client
|
||||
liquidityMgr *liquidity.Manager
|
||||
lnd *lndclient.LndServices
|
||||
reservationManager *reservation.Manager
|
||||
instantOutManager *instantout.Manager
|
||||
staticAddressManager *address.Manager
|
||||
depositManager *deposit.Manager
|
||||
swaps map[lntypes.Hash]loop.SwapInfo
|
||||
subscribers map[int]chan<- interface{}
|
||||
statusChan chan loop.SwapInfo
|
||||
nextSubscriberID int
|
||||
swapsLock sync.Mutex
|
||||
mainCtx context.Context
|
||||
}
|
||||
|
||||
// LoopOut initiates a loop out swap with the given parameters. The call returns
|
||||
|
|
@ -1314,6 +1318,51 @@ func rpcInstantOut(instantOut *instantout.InstantOut) *looprpc.InstantOut {
|
|||
}
|
||||
}
|
||||
|
||||
// NewStaticAddress is the rpc endpoint for loop clients to request a new static
|
||||
// address.
|
||||
func (s *swapClientServer) NewStaticAddress(ctx context.Context,
|
||||
_ *looprpc.NewStaticAddressRequest) (
|
||||
*looprpc.NewStaticAddressResponse, error) {
|
||||
|
||||
staticAddress, err := s.staticAddressManager.NewAddress(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &looprpc.NewStaticAddressResponse{
|
||||
Address: staticAddress.String(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ListUnspentDeposits returns a list of utxos behind the static address.
|
||||
func (s *swapClientServer) ListUnspentDeposits(ctx context.Context,
|
||||
req *looprpc.ListUnspentDepositsRequest) (
|
||||
*looprpc.ListUnspentDepositsResponse, error) {
|
||||
|
||||
// List all unspent utxos the wallet sees, regardless of the number of
|
||||
// confirmations.
|
||||
staticAddress, utxos, err := s.staticAddressManager.ListUnspentRaw(
|
||||
ctx, req.MinConfs, req.MaxConfs,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Prepare the list response.
|
||||
var respUtxos []*looprpc.Utxo
|
||||
for _, u := range utxos {
|
||||
utxo := &looprpc.Utxo{
|
||||
StaticAddress: staticAddress.String(),
|
||||
AmountSat: int64(u.Value),
|
||||
Confirmations: u.Confirmations,
|
||||
Outpoint: u.OutPoint.String(),
|
||||
}
|
||||
respUtxos = append(respUtxos, utxo)
|
||||
}
|
||||
|
||||
return &looprpc.ListUnspentDepositsResponse{Utxos: respUtxos}, nil
|
||||
}
|
||||
|
||||
func rpcAutoloopReason(reason liquidity.Reason) (looprpc.AutoReason, error) {
|
||||
switch reason {
|
||||
case liquidity.ReasonNone:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue