Skip to content

Commit 8ae8163

Browse files
committed
loopd: add assets manager/server
1 parent 27e89d9 commit 8ae8163

File tree

5 files changed

+86
-8
lines changed

5 files changed

+86
-8
lines changed

loopd/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ type Config struct {
200200
Tapd *assets.TapdConfig `group:"tapd" namespace:"tapd"`
201201

202202
View viewParameters `command:"view" alias:"v" description:"View all swaps in the database. This command can only be executed when loopd is not running."`
203+
204+
TapdConfig *assets.TapdConfig `group:"tapd" namespace:"tapd"`
203205
}
204206

205207
const (
@@ -247,6 +249,7 @@ func DefaultConfig() Config {
247249
MacaroonPath: DefaultLndMacaroonPath,
248250
RPCTimeout: DefaultLndRPCTimeout,
249251
},
252+
TapdConfig: assets.DefaultTapdConfig(),
250253
}
251254
}
252255

loopd/daemon.go

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ func (d *Daemon) startWebServers() error {
249249
)
250250
loop_looprpc.RegisterSwapClientServer(d.grpcServer, d)
251251

252+
loop_looprpc.RegisterAssetsClientServer(d.grpcServer, d.assetsServer)
253+
252254
// Register our debug server if it is compiled in.
253255
d.registerDebugServer()
254256

@@ -332,7 +334,7 @@ func (d *Daemon) startWebServers() error {
332334
d.wg.Add(1)
333335
go func() {
334336
defer d.wg.Done()
335-
337+
defer log.Info("REST proxy stopped")
336338
log.Infof("REST proxy listening on %s",
337339
d.restListener.Addr())
338340
err := d.restServer.Serve(d.restListener)
@@ -354,7 +356,7 @@ func (d *Daemon) startWebServers() error {
354356
d.wg.Add(1)
355357
go func() {
356358
defer d.wg.Done()
357-
359+
defer log.Info("RPC server stopped")
358360
log.Infof("RPC server listening on %s", d.grpcListener.Addr())
359361
err = d.grpcServer.Serve(d.grpcListener)
360362
if err != nil && !errors.Is(err, grpc.ErrServerStopped) {
@@ -487,6 +489,11 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
487489
swapClient.Conn,
488490
)
489491

492+
// Create a assets server client.
493+
assetsClient := loop_swaprpc.NewAssetsSwapServerClient(
494+
swapClient.Conn,
495+
)
496+
490497
// Both the client RPC server and the swap server client should stop
491498
// on main context cancel. So we create it early and pass it down.
492499
d.mainCtx, d.mainCtxCancel = context.WithCancel(context.Background())
@@ -636,6 +643,8 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
636643
var (
637644
reservationManager *reservation.Manager
638645
instantOutManager *instantout.Manager
646+
assetManager *assets.AssetsSwapManager
647+
assetClientServer *assets.AssetsClientServer
639648
)
640649

641650
// Create the reservation and instantout managers.
@@ -676,6 +685,27 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
676685
instantOutManager = instantout.NewInstantOutManager(
677686
instantOutConfig,
678687
)
688+
689+
tapdClient, err := assets.NewTapdClient(
690+
d.cfg.TapdConfig,
691+
)
692+
if err != nil {
693+
return err
694+
}
695+
assetsStore := assets.NewPostgresStore(baseDb)
696+
assetsConfig := &assets.Config{
697+
ServerClient: assetsClient,
698+
Store: assetsStore,
699+
AssetClient: tapdClient,
700+
LndClient: d.lnd.Client,
701+
Router: d.lnd.Router,
702+
ChainNotifier: d.lnd.ChainNotifier,
703+
Signer: d.lnd.Signer,
704+
Wallet: d.lnd.WalletKit,
705+
ExchangeRateProvider: assets.NewFixedExchangeRateProvider(),
706+
}
707+
assetManager = assets.NewAssetSwapServer(assetsConfig)
708+
assetClientServer = assets.NewAssetsServer(assetManager)
679709
}
680710

681711
// Now finally fully initialize the swap client RPC server instance.
@@ -696,6 +726,8 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
696726
withdrawalManager: withdrawalManager,
697727
staticLoopInManager: staticLoopInManager,
698728
assetClient: d.assetClient,
729+
assetManager: assetManager,
730+
assetsServer: assetClientServer,
699731
}
700732

701733
// Retrieve all currently existing swaps from the database.
@@ -801,6 +833,10 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
801833
cancel()
802834
}
803835
}
836+
getInfo, err := d.lnd.Client.GetInfo(d.mainCtx)
837+
if err != nil {
838+
return err
839+
}
804840

805841
// Start the instant out manager.
806842
if d.instantOutManager != nil {
@@ -809,12 +845,6 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
809845
go func() {
810846
defer d.wg.Done()
811847

812-
getInfo, err := d.lnd.Client.GetInfo(d.mainCtx)
813-
if err != nil {
814-
d.internalErrChan <- err
815-
return
816-
}
817-
818848
log.Info("Starting instantout manager")
819849
defer log.Info("Instantout manager stopped")
820850

@@ -933,6 +963,20 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
933963
staticLoopInManager.WaitInitComplete()
934964
}
935965

966+
// Start the asset manager.
967+
if d.assetManager != nil {
968+
d.wg.Add(1)
969+
go func() {
970+
defer d.wg.Done()
971+
log.Infof("Starting asset manager")
972+
defer log.Infof("Asset manager stopped")
973+
err := d.assetManager.Run(d.mainCtx, int32(getInfo.BlockHeight))
974+
if err != nil && !errors.Is(err, context.Canceled) {
975+
d.internalErrChan <- err
976+
}
977+
}()
978+
}
979+
936980
// Last, start our internal error handler. This will return exactly one
937981
// error or nil on the main error channel to inform the caller that
938982
// something went wrong or that shutdown is complete. We don't add to
@@ -978,6 +1022,9 @@ func (d *Daemon) Stop() {
9781022

9791023
// stop does the actual shutdown and blocks until all goroutines have exit.
9801024
func (d *Daemon) stop() {
1025+
// Sleep a second in order to fix a blocking issue when having a
1026+
// startup error.
1027+
<-time.After(time.Second)
9811028
// First of all, we can cancel the main context that all event handlers
9821029
// are using. This should stop all swap activity and all event handlers
9831030
// should exit.
@@ -995,6 +1042,7 @@ func (d *Daemon) stop() {
9951042
if d.restServer != nil {
9961043
// Don't return the error here, we first want to give everything
9971044
// else a chance to shut down cleanly.
1045+
9981046
err := d.restServer.Close()
9991047
if err != nil {
10001048
log.Errorf("Error stopping REST server: %v", err)

loopd/log.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/lightninglabs/aperture/l402"
66
"github.com/lightninglabs/lndclient"
77
"github.com/lightninglabs/loop"
8+
"github.com/lightninglabs/loop/assets"
89
"github.com/lightninglabs/loop/fsm"
910
"github.com/lightninglabs/loop/instantout"
1011
"github.com/lightninglabs/loop/instantout/reservation"
@@ -14,6 +15,7 @@ import (
1415
"github.com/lightninglabs/loop/staticaddr"
1516
"github.com/lightninglabs/loop/sweep"
1617
"github.com/lightninglabs/loop/sweepbatcher"
18+
"github.com/lightninglabs/loop/utils"
1719
"github.com/lightningnetwork/lnd"
1820
"github.com/lightningnetwork/lnd/build"
1921
"github.com/lightningnetwork/lnd/signal"
@@ -58,6 +60,13 @@ func SetupLoggers(root *build.RotatingLogWriter, intercept signal.Interceptor) {
5860
lnd.AddSubLogger(
5961
root, sweep.Subsystem, intercept, sweep.UseLogger,
6062
)
63+
64+
lnd.AddSubLogger(
65+
root, assets.Subsystem, intercept, assets.UseLogger,
66+
)
67+
lnd.AddSubLogger(
68+
root, utils.Subsystem, intercept, utils.UseLogger,
69+
)
6170
}
6271

6372
// genSubLogger creates a logger for a subsystem. We provide an instance of

loopd/perms/perms.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,20 @@ var RequiredPermissions = map[string][]bakery.Op{
169169
Entity: "swap",
170170
Action: "read",
171171
}},
172+
"/looprpc.AssetsClient/SwapOut": {{
173+
Entity: "swap",
174+
Action: "execute",
175+
}},
176+
"/looprpc.AssetsClient/ListAssetSwaps": {{
177+
Entity: "swap",
178+
Action: "read",
179+
}},
180+
"/looprpc.AssetsClient/ClientListAvailableAssets": {{
181+
Entity: "swap",
182+
Action: "read",
183+
}},
184+
"/looprpc.AssetsClient/ClientGetAssetSwapOutQuote": {{
185+
Entity: "swap",
186+
Action: "read",
187+
}},
172188
}

loopd/swapclient_server.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ type swapClientServer struct {
9696
withdrawalManager *withdraw.Manager
9797
staticLoopInManager *loopin.Manager
9898
assetClient *assets.TapdClient
99+
assetManager *assets.AssetsSwapManager
100+
assetsServer *assets.AssetsClientServer
99101
swaps map[lntypes.Hash]loop.SwapInfo
100102
subscribers map[int]chan<- interface{}
101103
statusChan chan loop.SwapInfo

0 commit comments

Comments
 (0)