-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathapplication.go
More file actions
210 lines (191 loc) · 7.77 KB
/
application.go
File metadata and controls
210 lines (191 loc) · 7.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package lps
import (
"context"
"errors"
"fmt"
"os"
"syscall"
"github.com/rsksmart/liquidity-provider-server/internal/adapters/dataproviders"
"github.com/rsksmart/liquidity-provider-server/internal/adapters/entrypoints/rest/server"
"github.com/rsksmart/liquidity-provider-server/internal/adapters/entrypoints/watcher"
"github.com/rsksmart/liquidity-provider-server/internal/configuration/bootstrap"
"github.com/rsksmart/liquidity-provider-server/internal/configuration/bootstrap/btc_bootstrap"
"github.com/rsksmart/liquidity-provider-server/internal/configuration/bootstrap/wallet"
"github.com/rsksmart/liquidity-provider-server/internal/configuration/environment"
"github.com/rsksmart/liquidity-provider-server/internal/configuration/environment/secrets"
"github.com/rsksmart/liquidity-provider-server/internal/configuration/registry"
"github.com/rsksmart/liquidity-provider-server/internal/entities"
"github.com/rsksmart/liquidity-provider-server/internal/entities/blockchain"
"github.com/rsksmart/liquidity-provider-server/internal/usecases"
log "github.com/sirupsen/logrus"
)
type Application struct {
env environment.Environment
timeouts environment.ApplicationTimeouts
liquidityProvider *dataproviders.LocalLiquidityProvider
useCaseRegistry *registry.UseCaseRegistry
watcherRegistry *registry.WatcherRegistry
rskRegistry *registry.Rootstock
btcRegistry *registry.Bitcoin
dbRegistry *registry.Database
messagingRegistry *registry.Messaging
runningServices []entities.Closeable
doneChannel chan os.Signal
}
func NewApplication(initCtx context.Context, env environment.Environment, timeouts environment.ApplicationTimeouts) *Application {
secretLoader, err := secrets.GetSecretLoader(initCtx, env)
if err != nil {
log.Fatal("Error getting secret loader:", err)
}
rskClient, err := bootstrap.Rootstock(initCtx, env)
if err != nil {
log.Fatal("Error connecting to RSK node: ", err)
}
log.Debug("Connected to RSK node")
walletFactory, err := wallet.NewFactory(env, wallet.FactoryCreationArgs{
Ctx: initCtx, Env: env, SecretLoader: secretLoader, RskClient: rskClient, Timeouts: timeouts,
})
if err != nil {
log.Fatal("Error creating wallet factory: ", err)
}
btcConnection, err := btc_bootstrap.Bitcoin(env.Btc)
if err != nil {
log.Fatal("Error connecting to the bitcoin node: ", err)
}
log.Debug("Connected to BTC node RPC server")
dbConnection, err := bootstrap.Mongo(initCtx, env.Mongo, timeouts)
if err != nil {
log.Fatal("Error connecting to MongoDB:", err)
}
log.Debug("Connected to MongoDB")
externalClients, err := createExternalRpc(initCtx, env)
if err != nil {
log.Fatal(err)
}
btcRegistry, err := registry.NewBitcoinRegistry(walletFactory, btcConnection)
if err != nil {
log.Fatal("Error creating BTC registry:", err)
}
dbRegistry := registry.NewDatabaseRegistry(dbConnection)
rootstockRegistry, err := registry.NewRootstockRegistry(env, rskClient, walletFactory, timeouts)
if err != nil {
log.Fatal("Error creating Rootstock registry:", err)
}
messagingRegistry := registry.NewMessagingRegistry(initCtx, env, rskClient, btcConnection, externalClients)
liquidityProvider := registry.NewLiquidityProvider(dbRegistry, rootstockRegistry, btcRegistry, messagingRegistry)
mutexes := environment.NewApplicationMutexes()
useCaseRegistry, err := registry.NewUseCaseRegistry(env, rootstockRegistry, btcRegistry, dbRegistry, liquidityProvider, messagingRegistry, mutexes)
if err != nil {
log.Fatal("Error creating use case registry:", err)
}
watcherRegistry := registry.NewWatcherRegistry(env, useCaseRegistry, rootstockRegistry, btcRegistry, liquidityProvider, messagingRegistry, watcher.NewApplicationTickers(), timeouts)
return &Application{
env: env,
timeouts: timeouts,
liquidityProvider: liquidityProvider,
useCaseRegistry: useCaseRegistry,
rskRegistry: rootstockRegistry,
btcRegistry: btcRegistry,
dbRegistry: dbRegistry,
messagingRegistry: messagingRegistry,
watcherRegistry: watcherRegistry,
runningServices: make([]entities.Closeable, 0),
}
}
func createExternalRpc(ctx context.Context, env environment.Environment) (registry.ExternalRpc, error) {
externalRskSources, err := bootstrap.ExternalRskSources(ctx, env)
if err != nil {
return registry.ExternalRpc{}, fmt.Errorf("error connecting to external RSK clients: %w", err)
} else if len(externalRskSources) == 0 {
log.Warn("No external RSK clients configured")
}
externalBtcSources, err := btc_bootstrap.ExternalBitcoinSources(env)
if err != nil {
return registry.ExternalRpc{}, fmt.Errorf("error connecting to external BTC clients: %w", err)
} else if len(externalBtcSources) == 0 {
log.Warn("No external BTC sources configured")
}
return registry.ExternalRpc{
RskExternalRpc: externalRskSources,
BtcExternalRpc: externalBtcSources,
}, nil
}
func (app *Application) Run(env environment.Environment, logLevel log.Level) {
app.addRunningService(app.dbRegistry.Connection)
app.addRunningService(app.rskRegistry.Client)
app.addRunningService(app.btcRegistry.RpcConnection)
app.addRunningService(app.btcRegistry.PaymentWallet)
app.addRunningService(app.btcRegistry.MonitoringWallet)
app.addRunningService(app.messagingRegistry.EventBus)
registerParams := blockchain.NewProviderRegistrationParams(app.env.Provider.Name, app.env.Provider.ApiBaseUrl, true, app.env.Provider.ProviderType())
id, err := app.useCaseRegistry.GetRegistrationUseCase().Run(registerParams)
if errors.Is(err, usecases.AlreadyRegisteredError) {
log.Info("Provider already registered")
} else if err != nil {
log.Fatal("Error registering provider: ", err)
} else {
log.Info("Provider registered with ID ", id)
}
err = app.useCaseRegistry.GenerateDefaultCredentialsUseCase().Run(context.Background(), os.TempDir())
if err != nil {
log.Fatal("Error generating default password for management interface: ", err)
}
watchers, err := app.prepareWatchers()
if err != nil {
log.Fatal("Error initializing watchers: ", err)
}
for _, w := range watchers {
go w.Start()
}
applicationServer, done := server.NewServer(env, app.useCaseRegistry, logLevel, app.timeouts)
app.doneChannel = done
app.addRunningService(applicationServer)
go applicationServer.Start()
<-done
}
func (app *Application) addRunningService(service entities.Closeable) {
app.runningServices = append(app.runningServices, service)
}
func (app *Application) prepareWatchers() ([]watcher.Watcher, error) {
var err error
watchers := []watcher.Watcher{
app.watcherRegistry.PeginDepositAddressWatcher,
app.watcherRegistry.PeginBridgeWatcher,
app.watcherRegistry.PegoutRskDepositWatcher,
app.watcherRegistry.PegoutBtcTransferWatcher,
app.watcherRegistry.LiquidityCheckWatcher,
app.watcherRegistry.PenalizationAlertWatcher,
app.watcherRegistry.PegoutBridgeWatcher,
app.watcherRegistry.BtcReleaseWatcher,
app.watcherRegistry.QuoteMetricsWatcher,
app.watcherRegistry.AssetReportWatcher,
}
if app.env.Eclipse.Enabled {
watchers = append(watchers, app.watcherRegistry.RskEclipseWatcher)
watchers = append(watchers, app.watcherRegistry.BitcoinEclipseWatcher)
}
ctx, cancel := context.WithTimeout(context.Background(), app.timeouts.WatcherPreparation.Seconds())
defer cancel()
for _, w := range watchers {
if err = w.Prepare(ctx); err != nil {
return nil, err
}
app.addRunningService(w)
}
return watchers, nil
}
func (app *Application) ShutdownServices() {
log.Info("Starting graceful shutdown...")
numberOfServices := len(app.runningServices)
closeChannel := make(chan bool, numberOfServices)
for _, service := range app.runningServices {
service.Shutdown(closeChannel)
}
for i := 0; i < numberOfServices; i++ {
<-closeChannel
}
log.Info("Shutdown completed")
}
func (app *Application) ForceShutdown() {
app.doneChannel <- syscall.SIGINT
}