forked from breez/breez
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_init.go
292 lines (256 loc) · 7.57 KB
/
app_init.go
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package breez
import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"sync"
"github.com/breez/breez/account"
"github.com/breez/breez/backup"
"github.com/breez/breez/chainservice"
"github.com/breez/breez/config"
"github.com/breez/breez/data"
"github.com/breez/breez/db"
"github.com/breez/breez/doubleratchet"
"github.com/breez/breez/lnnode"
breezlog "github.com/breez/breez/log"
"github.com/breez/breez/services"
"github.com/breez/breez/swapfunds"
"github.com/btcsuite/btclog"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnrpc/breezbackuprpc"
"golang.org/x/net/context"
)
// App represents the breez application
type App struct {
isReady int32
started int32
stopped int32
wg sync.WaitGroup
connectionMu sync.Mutex
quitChan chan struct{}
log btclog.Logger
cfg *config.Config
breezDB *db.DB
releaseBreezDB func() error
// services passed to breez from the application layer
//appServices AppServices
//exposed sub services
AccountService *account.Service
BackupManager *backup.Manager
SwapService *swapfunds.Service
ServicesClient *services.Client
//non exposed services
lnDaemon *lnnode.Daemon
//channel for external binding events
notificationsChan chan data.NotificationEvent
lspChanStateSyncer *lspChanStateSync
}
// AppServices defined the interface needed in Breez library in order to functional
// right.
type AppServices interface {
BackupProviderName() string
BackupProviderSignIn() (string, error)
}
// AuthService is a Specific implementation for backup.Manager
type AuthService struct {
appServices AppServices
}
// SignIn is the interface function implementation needed for backup.Manager
func (a *AuthService) SignIn() (string, error) {
return a.appServices.BackupProviderSignIn()
}
// NewApp create a new application
func NewApp(workingDir string, applicationServices AppServices, startBeforeSync bool) (*App, error) {
app := &App{
quitChan: make(chan struct{}),
notificationsChan: make(chan data.NotificationEvent),
}
logger, err := breezlog.GetLogger(workingDir, "BRUI")
if err != nil {
return nil, err
}
app.log = logger
app.cfg, err = config.GetConfig(workingDir)
if err != nil {
return nil, fmt.Errorf("Failed to get config file: %v", err)
}
app.ServicesClient, err = services.NewClient(app.cfg)
if err != nil {
return nil, fmt.Errorf("Error creating services.Client: %v", err)
}
app.log.Infof("New Client")
app.breezDB, app.releaseBreezDB, err = db.Get(workingDir)
if err != nil {
return nil, fmt.Errorf("Failed to initialze breezDB: %v", err)
}
app.log.Infof("New db")
walletdbPath := app.cfg.WorkingDir + "/data/chain/bitcoin/" + app.cfg.Network + "/wallet.db"
walletDBInfo, err := os.Stat(walletdbPath)
if err == nil {
app.log.Infof("wallet db size is: %v", walletDBInfo.Size())
}
if err = compactWalletDB(app.cfg.WorkingDir+"/data/chain/bitcoin/"+app.cfg.Network, app.log); err != nil {
return nil, err
}
app.lnDaemon, err = lnnode.NewDaemon(app.cfg, app.breezDB, startBeforeSync)
if err != nil {
return nil, fmt.Errorf("Failed to create lnnode.Daemon: %v", err)
}
app.log.Infof("New daemon")
if err := doubleratchet.Start(path.Join(app.cfg.WorkingDir, "sessions_encryption.db")); err != nil {
return nil, err
}
if err != nil {
return nil, fmt.Errorf("Failed to start doubleratchet: %v", err)
}
backupLogger, err := breezlog.GetLogger(workingDir, "BCKP")
if err != nil {
return nil, err
}
app.BackupManager, err = backup.NewManager(
applicationServices.BackupProviderName(),
&AuthService{appServices: applicationServices},
app.onServiceEvent,
app.prepareBackupInfo,
app.cfg,
backupLogger,
)
if err != nil {
return nil, fmt.Errorf("Failed to start backup manager: %v", err)
}
app.log.Infof("New backup")
app.lspChanStateSyncer = newLSPChanStateSync(app)
app.AccountService, err = account.NewService(
app.cfg,
app.breezDB,
app.ServicesClient,
app.lnDaemon,
app.RequestBackup,
app.lspChanStateSyncer.unconfirmedChannelsInSync,
app.onServiceEvent,
)
app.log.Infof("New AccountService")
if err != nil {
return nil, fmt.Errorf("Failed to create AccountService: %v", err)
}
app.SwapService, err = swapfunds.NewService(
app.cfg,
app.breezDB,
app.ServicesClient,
app.lnDaemon,
app.AccountService.SendPaymentForRequestV2,
app.AccountService.AddInvoice,
app.ServicesClient.LSPList,
app.AccountService.GetGlobalMaxReceiveLimit,
app.onServiceEvent,
)
app.log.Infof("New SwapService")
if err != nil {
return nil, fmt.Errorf("Failed to create SwapService: %v", err)
}
app.log.Infof("app initialized")
return app, nil
}
// extractBackupInfo extracts the information that is needed for the external backup service:
// 1. paths - the files need to be backed up.
// 2. nodeID - the current lightning node id.
func (a *App) prepareBackupInfo() (paths []string, nodeID string, err error) {
a.log.Infof("extractBackupInfo started")
lnclient := a.lnDaemon.APIClient()
if lnclient == nil {
return nil, "", errors.New("Daemon is not ready")
}
backupClient := a.lnDaemon.BreezBackupClient()
if backupClient == nil {
return nil, "", errors.New("Daemon is not ready")
}
response, err := backupClient.GetBackup(context.Background(), &breezbackuprpc.GetBackupRequest{})
if err != nil {
a.log.Errorf("Couldn't get backup: %v", err)
return nil, "", err
}
info, err := lnclient.GetInfo(context.Background(), &lnrpc.GetInfoRequest{})
if err != nil {
return nil, "", err
}
f, err := a.breezdbCopy(a.breezDB)
if err != nil {
a.log.Errorf("Couldn't get breez backup file: %v", err)
return nil, "", err
}
files := append(response.Files, f)
chanBackupFile := a.cfg.WorkingDir + "/data/chain/bitcoin/" + a.cfg.Network + "/channel.backup"
// check if we have channel.backup file
_, err = os.Stat(chanBackupFile)
if err != nil {
a.log.Infof("Not adding channel.backup to the backup: %v", err)
} else {
dir, err := ioutil.TempDir("", "chanelbackup")
if err != nil {
return nil, "", err
}
destFile := fmt.Sprintf("%v/channel.backup", dir)
if _, err := copyFile(chanBackupFile, destFile); err != nil {
return nil, "", err
}
files = append(files, destFile)
a.log.Infof("adding channel.backup to the backup")
}
a.log.Infof("extractBackupInfo completd")
return files, info.IdentityPubkey, nil
}
func (a *App) breezdbCopy(breezDB *db.DB) (string, error) {
dir, err := ioutil.TempDir("", "backup")
if err != nil {
return "", err
}
return a.breezDB.BackupDb(dir)
}
func copyFile(src, dst string) (int64, error) {
source, err := os.Open(src)
if err != nil {
return 0, err
}
defer source.Close()
destination, err := os.Create(dst)
if err != nil {
return 0, err
}
defer destination.Close()
nBytes, err := io.Copy(destination, source)
return nBytes, err
}
func compactWalletDB(walletDBDir string, logger btclog.Logger) error {
dbName := "wallet.db"
dbPath := path.Join(walletDBDir, dbName)
f, err := os.Stat(dbPath)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
if f.Size() <= 10000000 {
return nil
}
newFile, err := ioutil.TempFile(walletDBDir, "cdb-compact")
if err != nil {
return err
}
if err = chainservice.BoltCopy(dbPath, newFile.Name(),
func(keyPath [][]byte, k []byte, v []byte) bool { return false }); err != nil {
return err
}
if err = os.Rename(dbPath, dbPath+".old"); err != nil {
return err
}
if err = os.Rename(newFile.Name(), dbPath); err != nil {
logger.Criticalf("Error when renaming the new walletdb file: %v", err)
return err
}
logger.Infof("wallet.db was compacted because it's too big")
return nil
}