/
loader.go
504 lines (440 loc) · 13.8 KB
/
loader.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
// Copyright (c) 2015-2018 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package loader
import (
"os"
"path/filepath"
"sync"
"github.com/picfight/pfcd/chaincfg"
"github.com/picfight/pfcd/dcrutil"
"github.com/picfight/pfcwallet/chain"
"github.com/picfight/pfcwallet/errors"
"github.com/picfight/pfcwallet/ticketbuyer"
"github.com/picfight/pfcwallet/wallet"
_ "github.com/picfight/pfcwallet/wallet/drivers/bdb" // driver loaded during init
)
const (
walletDbName = "wallet.db"
defaultDbDriver = "bdb"
)
// Loader implements the creating of new and opening of existing wallets, while
// providing a callback system for other subsystems to handle the loading of a
// wallet. This is primarely intended for use by the RPC servers, to enable
// methods and services which require the wallet when the wallet is loaded by
// another subsystem.
//
// Loader is safe for concurrent access.
type Loader struct {
callbacks []func(*wallet.Wallet)
backend wallet.NetworkBackend
chainParams *chaincfg.Params
dbDirPath string
wallet *wallet.Wallet
db wallet.DB
dbDriver string
purchaseManager *ticketbuyer.PurchaseManager
ntfnClient wallet.MainTipChangedNotificationsClient
stakeOptions *StakeOptions
gapLimit int
accountGapLimit int
allowHighFees bool
relayFee float64
mu sync.Mutex
}
// StakeOptions contains the various options necessary for stake mining.
type StakeOptions struct {
VotingEnabled bool
TicketFee float64
AddressReuse bool
VotingAddress dcrutil.Address
PoolAddress dcrutil.Address
PoolFees float64
StakePoolColdExtKey string
}
// NewLoader constructs a Loader.
func NewLoader(chainParams *chaincfg.Params, dbDirPath string, stakeOptions *StakeOptions, gapLimit int,
allowHighFees bool, relayFee float64, accountGapLimit int) *Loader {
return &Loader{
chainParams: chainParams,
dbDirPath: dbDirPath,
dbDriver: defaultDbDriver,
stakeOptions: stakeOptions,
gapLimit: gapLimit,
accountGapLimit: accountGapLimit,
allowHighFees: allowHighFees,
relayFee: relayFee,
}
}
// SetDatabaseDriver specifies the database to be used by walletdb
func (l *Loader) SetDatabaseDriver(driver string) {
l.dbDriver = driver
}
// onLoaded executes each added callback and prevents loader from loading any
// additional wallets. Requires mutex to be locked.
func (l *Loader) onLoaded(w *wallet.Wallet, db wallet.DB) {
for _, fn := range l.callbacks {
fn(w)
}
l.wallet = w
l.db = db
l.callbacks = nil // not needed anymore
}
// RunAfterLoad adds a function to be executed when the loader creates or opens
// a wallet. Functions are executed in a single goroutine in the order they are
// added.
func (l *Loader) RunAfterLoad(fn func(*wallet.Wallet)) {
l.mu.Lock()
if l.wallet != nil {
w := l.wallet
l.mu.Unlock()
fn(w)
} else {
l.callbacks = append(l.callbacks, fn)
l.mu.Unlock()
}
}
// CreateWatchingOnlyWallet creates a new watch-only wallet using the provided
// extended public key and public passphrase.
func (l *Loader) CreateWatchingOnlyWallet(extendedPubKey string, pubPass []byte) (w *wallet.Wallet, err error) {
const op errors.Op = "loader.CreateWatchingOnlyWallet"
defer l.mu.Unlock()
l.mu.Lock()
if l.wallet != nil {
return nil, errors.E(op, errors.Exist, "wallet already loaded")
}
// Ensure that the network directory exists.
if fi, err := os.Stat(l.dbDirPath); err != nil {
if os.IsNotExist(err) {
// Attempt data directory creation
if err = os.MkdirAll(l.dbDirPath, 0700); err != nil {
return nil, errors.E(op, err)
}
} else {
return nil, errors.E(op, err)
}
} else {
if !fi.IsDir() {
return nil, errors.E(op, errors.Invalid, errors.Errorf("%q is not a directory", l.dbDirPath))
}
}
dbPath := filepath.Join(l.dbDirPath, walletDbName)
exists, err := fileExists(dbPath)
if err != nil {
return nil, errors.E(op, err)
}
if exists {
return nil, errors.E(op, errors.Exist, "wallet already exists")
}
// At this point it is asserted that there is no existing database file, and
// deleting anything won't destroy a wallet in use. Defer a function that
// attempts to remove any written database file if this function errors.
defer func() {
if err != nil {
_ = os.Remove(dbPath)
}
}()
// Create the wallet database backed by bolt db.
err = os.MkdirAll(l.dbDirPath, 0700)
if err != nil {
return nil, errors.E(op, err)
}
db, err := wallet.CreateDB(l.dbDriver, dbPath)
if err != nil {
return nil, errors.E(op, err)
}
// Initialize the watch-only database for the wallet before opening.
err = wallet.CreateWatchOnly(db, extendedPubKey, pubPass, l.chainParams)
if err != nil {
return nil, errors.E(op, err)
}
// Open the watch-only wallet.
so := l.stakeOptions
cfg := &wallet.Config{
DB: db,
PubPassphrase: pubPass,
VotingEnabled: so.VotingEnabled,
AddressReuse: so.AddressReuse,
VotingAddress: so.VotingAddress,
PoolAddress: so.PoolAddress,
PoolFees: so.PoolFees,
TicketFee: so.TicketFee,
GapLimit: l.gapLimit,
AccountGapLimit: l.accountGapLimit,
StakePoolColdExtKey: so.StakePoolColdExtKey,
AllowHighFees: l.allowHighFees,
RelayFee: l.relayFee,
Params: l.chainParams,
}
w, err = wallet.Open(cfg)
if err != nil {
return nil, errors.E(op, err)
}
w.Start()
l.onLoaded(w, db)
return w, nil
}
// CreateNewWallet creates a new wallet using the provided public and private
// passphrases. The seed is optional. If non-nil, addresses are derived from
// this seed. If nil, a secure random seed is generated.
func (l *Loader) CreateNewWallet(pubPassphrase, privPassphrase, seed []byte) (w *wallet.Wallet, err error) {
const op errors.Op = "loader.CreateNewWallet"
defer l.mu.Unlock()
l.mu.Lock()
if l.wallet != nil {
return nil, errors.E(op, errors.Exist, "wallet already opened")
}
// Ensure that the network directory exists.
if fi, err := os.Stat(l.dbDirPath); err != nil {
if os.IsNotExist(err) {
// Attempt data directory creation
if err = os.MkdirAll(l.dbDirPath, 0700); err != nil {
return nil, errors.E(op, err)
}
} else {
return nil, errors.E(op, err)
}
} else {
if !fi.IsDir() {
return nil, errors.E(op, errors.Errorf("%q is not a directory", l.dbDirPath))
}
}
dbPath := filepath.Join(l.dbDirPath, walletDbName)
exists, err := fileExists(dbPath)
if err != nil {
return nil, errors.E(op, err)
}
if exists {
return nil, errors.E(op, errors.Exist, "wallet DB exists")
}
// At this point it is asserted that there is no existing database file, and
// deleting anything won't destroy a wallet in use. Defer a function that
// attempts to remove any written database file if this function errors.
defer func() {
if err != nil {
_ = os.Remove(dbPath)
}
}()
// Create the wallet database backed by bolt db.
err = os.MkdirAll(l.dbDirPath, 0700)
if err != nil {
return nil, errors.E(op, err)
}
db, err := wallet.CreateDB(l.dbDriver, dbPath)
if err != nil {
return nil, errors.E(op, err)
}
// Initialize the newly created database for the wallet before opening.
err = wallet.Create(db, pubPassphrase, privPassphrase, seed, l.chainParams)
if err != nil {
return nil, errors.E(op, err)
}
// Open the newly-created wallet.
so := l.stakeOptions
cfg := &wallet.Config{
DB: db,
PubPassphrase: pubPassphrase,
VotingEnabled: so.VotingEnabled,
AddressReuse: so.AddressReuse,
VotingAddress: so.VotingAddress,
PoolAddress: so.PoolAddress,
PoolFees: so.PoolFees,
TicketFee: so.TicketFee,
GapLimit: l.gapLimit,
AccountGapLimit: l.accountGapLimit,
StakePoolColdExtKey: so.StakePoolColdExtKey,
AllowHighFees: l.allowHighFees,
RelayFee: l.relayFee,
Params: l.chainParams,
}
w, err = wallet.Open(cfg)
if err != nil {
return nil, errors.E(op, err)
}
w.Start()
l.onLoaded(w, db)
return w, nil
}
// OpenExistingWallet opens the wallet from the loader's wallet database path
// and the public passphrase. If the loader is being called by a context where
// standard input prompts may be used during wallet upgrades, setting
// canConsolePrompt will enable these prompts.
func (l *Loader) OpenExistingWallet(pubPassphrase []byte) (w *wallet.Wallet, rerr error) {
const op errors.Op = "loader.OpenExistingWallet"
defer l.mu.Unlock()
l.mu.Lock()
if l.wallet != nil {
return nil, errors.E(op, errors.Exist, "wallet already opened")
}
// Open the database using the boltdb backend.
dbPath := filepath.Join(l.dbDirPath, walletDbName)
l.mu.Unlock()
db, err := wallet.OpenDB(l.dbDriver, dbPath)
l.mu.Lock()
if err != nil {
log.Errorf("Failed to open database: %v", err)
return nil, errors.E(op, err)
}
// If this function does not return to completion the database must be
// closed. Otherwise, because the database is locked on opens, any
// other attempts to open the wallet will hang, and there is no way to
// recover since this db handle would be leaked.
defer func() {
if rerr != nil {
db.Close()
}
}()
so := l.stakeOptions
cfg := &wallet.Config{
DB: db,
PubPassphrase: pubPassphrase,
VotingEnabled: so.VotingEnabled,
AddressReuse: so.AddressReuse,
VotingAddress: so.VotingAddress,
PoolAddress: so.PoolAddress,
PoolFees: so.PoolFees,
TicketFee: so.TicketFee,
GapLimit: l.gapLimit,
AccountGapLimit: l.accountGapLimit,
StakePoolColdExtKey: so.StakePoolColdExtKey,
AllowHighFees: l.allowHighFees,
RelayFee: l.relayFee,
Params: l.chainParams,
}
w, err = wallet.Open(cfg)
if err != nil {
return nil, errors.E(op, err)
}
w.Start()
l.onLoaded(w, db)
return w, nil
}
// DbDirPath returns the Loader's database directory path
func (l *Loader) DbDirPath() string {
return l.dbDirPath
}
// WalletExists returns whether a file exists at the loader's database path.
// This may return an error for unexpected I/O failures.
func (l *Loader) WalletExists() (bool, error) {
const op errors.Op = "loader.WalletExists"
dbPath := filepath.Join(l.dbDirPath, walletDbName)
exists, err := fileExists(dbPath)
if err != nil {
return false, errors.E(op, err)
}
return exists, nil
}
// LoadedWallet returns the loaded wallet, if any, and a bool for whether the
// wallet has been loaded or not. If true, the wallet pointer should be safe to
// dereference.
func (l *Loader) LoadedWallet() (*wallet.Wallet, bool) {
l.mu.Lock()
w := l.wallet
l.mu.Unlock()
return w, w != nil
}
// UnloadWallet stops the loaded wallet, if any, and closes the wallet database.
// Returns with errors.Invalid if the wallet has not been loaded with
// CreateNewWallet or LoadExistingWallet. The Loader may be reused if this
// function returns without error.
func (l *Loader) UnloadWallet() error {
const op errors.Op = "loader.UnloadWallet"
defer l.mu.Unlock()
l.mu.Lock()
if l.wallet == nil {
return errors.E(op, errors.Invalid, "wallet is unopened")
}
l.stopTicketPurchase()
l.wallet.Stop()
l.wallet.WaitForShutdown()
err := l.db.Close()
if err != nil {
return errors.E(op, err)
}
l.wallet = nil
l.db = nil
return nil
}
// SetNetworkBackend associates the loader with a wallet network backend.
func (l *Loader) SetNetworkBackend(n wallet.NetworkBackend) {
l.mu.Lock()
l.backend = n
l.mu.Unlock()
}
// NetworkBackend returns the associated wallet network backend, if any, and a
// bool describing whether a non-nil network backend was set.
func (l *Loader) NetworkBackend() (n wallet.NetworkBackend, ok bool) {
l.mu.Lock()
n = l.backend
l.mu.Unlock()
return n, n != nil
}
// StartTicketPurchase launches the ticketbuyer to start purchasing tickets.
func (l *Loader) StartTicketPurchase(passphrase []byte, ticketbuyerCfg *ticketbuyer.Config) error {
const op errors.Op = "loader.StartTicketPurchase"
defer l.mu.Unlock()
l.mu.Lock()
// Already running?
if l.purchaseManager != nil {
return errors.E(op, errors.Invalid, "ticket purchaser already started")
}
if l.wallet == nil {
return errors.E(op, errors.Invalid, "wallet must be loaded")
}
c, err := chain.RPCClientFromBackend(l.backend)
if err != nil {
return errors.E(op, errors.Invalid, "pfcd RPC client must be loaded")
}
w := l.wallet
p, err := ticketbuyer.NewTicketPurchaser(ticketbuyerCfg, c, w, l.chainParams)
if err != nil {
return errors.E(op, err)
}
n := w.NtfnServer.MainTipChangedNotifications()
pm := ticketbuyer.NewPurchaseManager(w, p, n.C, passphrase)
l.ntfnClient = n
l.purchaseManager = pm
pm.Start()
return nil
}
// stopTicketPurchase stops the ticket purchaser, waiting until it has finished.
// Returns false if the ticket purchaser was not running. It must be called with
// the mutex lock held.
func (l *Loader) stopTicketPurchase() bool {
if l.purchaseManager == nil {
return false
}
l.ntfnClient.Done()
l.purchaseManager.Stop()
l.purchaseManager.WaitForShutdown()
l.purchaseManager = nil
return true
}
// StopTicketPurchase stops the ticket purchaser, waiting until it has finished.
func (l *Loader) StopTicketPurchase() error {
const op errors.Op = "loader.StopTicketPurchase"
defer l.mu.Unlock()
l.mu.Lock()
if !l.stopTicketPurchase() {
return errors.E(op, errors.Invalid, "ticket purchaser is not running")
}
return nil
}
// PurchaseManager returns the ticket purchaser instance. If ticket purchasing
// has been disabled, it returns nil.
func (l *Loader) PurchaseManager() *ticketbuyer.PurchaseManager {
l.mu.Lock()
pm := l.purchaseManager
l.mu.Unlock()
return pm
}
func fileExists(filePath string) (bool, error) {
_, err := os.Stat(filePath)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
return true, nil
}