Skip to content

Commit 8fa841d

Browse files
committed
loopd: add assets manager/server
1 parent 3fdf6c5 commit 8fa841d

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-0
lines changed

loopd/config.go

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

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

206208
const (

loopd/daemon.go

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

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

@@ -494,6 +496,11 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
494496
swapClient.Conn,
495497
)
496498

499+
// Create a assets server client.
500+
assetsClient := loop_swaprpc.NewAssetsSwapServerClient(
501+
swapClient.Conn,
502+
)
503+
497504
// Both the client RPC server and the swap server client should stop
498505
// on main context cancel. So we create it early and pass it down.
499506
d.mainCtx, d.mainCtxCancel = context.WithCancel(context.Background())
@@ -643,6 +650,8 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
643650
var (
644651
reservationManager *reservation.Manager
645652
instantOutManager *instantout.Manager
653+
assetManager *assets.AssetsSwapManager
654+
assetClientServer *assets.AssetsClientServer
646655
)
647656

648657
// Create the reservation and instantout managers.
@@ -683,6 +692,27 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
683692
instantOutManager = instantout.NewInstantOutManager(
684693
instantOutConfig, int32(blockHeight),
685694
)
695+
696+
tapdClient, err := assets.NewTapdClient(
697+
d.cfg.TapdConfig,
698+
)
699+
if err != nil {
700+
return err
701+
}
702+
assetsStore := assets.NewPostgresStore(baseDb)
703+
assetsConfig := &assets.Config{
704+
ServerClient: assetsClient,
705+
Store: assetsStore,
706+
AssetClient: tapdClient,
707+
LndClient: d.lnd.Client,
708+
Router: d.lnd.Router,
709+
ChainNotifier: d.lnd.ChainNotifier,
710+
Signer: d.lnd.Signer,
711+
Wallet: d.lnd.WalletKit,
712+
ExchangeRateProvider: assets.NewFixedExchangeRateProvider(),
713+
}
714+
assetManager = assets.NewAssetSwapServer(assetsConfig)
715+
assetClientServer = assets.NewAssetsServer(assetManager)
686716
}
687717

688718
// Now finally fully initialize the swap client RPC server instance.
@@ -703,6 +733,8 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
703733
withdrawalManager: withdrawalManager,
704734
staticLoopInManager: staticLoopInManager,
705735
assetClient: d.assetClient,
736+
assetManager: assetManager,
737+
assetsServer: assetClientServer,
706738
}
707739

708740
// Retrieve all currently existing swaps from the database.
@@ -897,6 +929,20 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
897929
staticLoopInManager.WaitInitComplete()
898930
}
899931

932+
// Start the asset manager.
933+
if d.assetManager != nil {
934+
d.wg.Add(1)
935+
go func() {
936+
defer d.wg.Done()
937+
infof("Starting asset manager")
938+
defer infof("Asset manager stopped")
939+
err := d.assetManager.Run(d.mainCtx, int32(getInfo.BlockHeight))
940+
if err != nil && !errors.Is(err, context.Canceled) {
941+
d.internalErrChan <- err
942+
}
943+
}()
944+
}
945+
900946
// Last, start our internal error handler. This will return exactly one
901947
// error or nil on the main error channel to inform the caller that
902948
// something went wrong or that shutdown is complete. We don't add to
@@ -942,6 +988,9 @@ func (d *Daemon) Stop() {
942988

943989
// stop does the actual shutdown and blocks until all goroutines have exit.
944990
func (d *Daemon) stop() {
991+
// Sleep a second in order to fix a blocking issue when having a
992+
// startup error.
993+
<-time.After(time.Second)
945994
// First of all, we can cancel the main context that all event handlers
946995
// are using. This should stop all swap activity and all event handlers
947996
// should exit.
@@ -959,6 +1008,7 @@ func (d *Daemon) stop() {
9591008
if d.restServer != nil {
9601009
// Don't return the error here, we first want to give everything
9611010
// else a chance to shut down cleanly.
1011+
9621012
err := d.restServer.Close()
9631013
if err != nil {
9641014
errorf("Error stopping REST server: %v", err)

loopd/log.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/lightninglabs/aperture/l402"
88
"github.com/lightninglabs/lndclient"
99
"github.com/lightninglabs/loop"
10+
"github.com/lightninglabs/loop/assets"
1011
"github.com/lightninglabs/loop/fsm"
1112
"github.com/lightninglabs/loop/instantout"
1213
"github.com/lightninglabs/loop/instantout/reservation"
@@ -16,6 +17,7 @@ import (
1617
"github.com/lightninglabs/loop/staticaddr"
1718
"github.com/lightninglabs/loop/sweep"
1819
"github.com/lightninglabs/loop/sweepbatcher"
20+
"github.com/lightninglabs/loop/utils"
1921
"github.com/lightningnetwork/lnd"
2022
"github.com/lightningnetwork/lnd/build"
2123
"github.com/lightningnetwork/lnd/signal"
@@ -92,6 +94,13 @@ func SetupLoggers(root *build.SubLoggerManager, intercept signal.Interceptor) {
9294
lnd.AddSubLogger(
9395
root, sweep.Subsystem, intercept, sweep.UseLogger,
9496
)
97+
98+
lnd.AddSubLogger(
99+
root, assets.Subsystem, intercept, assets.UseLogger,
100+
)
101+
lnd.AddSubLogger(
102+
root, utils.Subsystem, intercept, utils.UseLogger,
103+
)
95104
}
96105

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

loopd/swapclient_server.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ type swapClientServer struct {
9898
withdrawalManager *withdraw.Manager
9999
staticLoopInManager *loopin.Manager
100100
assetClient *assets.TapdClient
101+
assetManager *assets.AssetsSwapManager
102+
assetsServer *assets.AssetsClientServer
101103
swaps map[lntypes.Hash]loop.SwapInfo
102104
subscribers map[int]chan<- interface{}
103105
statusChan chan loop.SwapInfo

looprpc/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
}

0 commit comments

Comments
 (0)