forked from planetdecred/dcrlibwallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ticket.go
640 lines (548 loc) · 18.6 KB
/
ticket.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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
package dcrlibwallet
import (
"context"
"fmt"
"runtime/trace"
"sync"
"time"
"decred.org/dcrwallet/v2/errors"
w "decred.org/dcrwallet/v2/wallet"
"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrd/dcrutil/v4"
"github.com/decred/dcrd/wire"
"github.com/planetdecred/dcrlibwallet/internal/vsp"
"github.com/planetdecred/dcrlibwallet/utils"
)
func (wallet *Wallet) TotalStakingRewards() (int64, error) {
voteTransactions, err := wallet.GetTransactionsRaw(0, 0, TxFilterVoted, true)
if err != nil {
return 0, err
}
var totalRewards int64
for _, tx := range voteTransactions {
totalRewards += tx.VoteReward
}
return totalRewards, nil
}
func (mw *MultiWallet) TotalStakingRewards() (int64, error) {
var totalRewards int64
for _, wal := range mw.wallets {
walletTotalRewards, err := wal.TotalStakingRewards()
if err != nil {
return 0, err
}
totalRewards += walletTotalRewards
}
return totalRewards, nil
}
func (mw *MultiWallet) TicketMaturity() int32 {
return int32(mw.chainParams.TicketMaturity)
}
func (mw *MultiWallet) TicketExpiry() int32 {
return int32(mw.chainParams.TicketExpiry)
}
func (wallet *Wallet) StakingOverview() (stOverview *StakingOverview, err error) {
stOverview = &StakingOverview{}
stOverview.Voted, err = wallet.CountTransactions(TxFilterVoted)
if err != nil {
return nil, err
}
stOverview.Revoked, err = wallet.CountTransactions(TxFilterRevoked)
if err != nil {
return nil, err
}
stOverview.Live, err = wallet.CountTransactions(TxFilterLive)
if err != nil {
return nil, err
}
stOverview.Immature, err = wallet.CountTransactions(TxFilterImmature)
if err != nil {
return nil, err
}
stOverview.Expired, err = wallet.CountTransactions(TxFilterExpired)
if err != nil {
return nil, err
}
stOverview.Unmined, err = wallet.CountTransactions(TxFilterUnmined)
if err != nil {
return nil, err
}
stOverview.All = stOverview.Unmined + stOverview.Immature + stOverview.Live + stOverview.Voted +
stOverview.Revoked + stOverview.Expired
return stOverview, nil
}
func (mw *MultiWallet) StakingOverview() (stOverview *StakingOverview, err error) {
stOverview = &StakingOverview{}
for _, wallet := range mw.wallets {
st, err := wallet.StakingOverview()
if err != nil {
return nil, err
}
stOverview.Unmined += st.Unmined
stOverview.Immature += st.Immature
stOverview.Live += st.Live
stOverview.Voted += st.Voted
stOverview.Revoked += st.Revoked
stOverview.Expired += st.Expired
}
stOverview.All = stOverview.Unmined + stOverview.Immature + stOverview.Live + stOverview.Voted +
stOverview.Revoked + stOverview.Expired
return stOverview, nil
}
// TicketPrice returns the price of a ticket for the next block, also known as
// the stake difficulty. May be incorrect if blockchain sync is ongoing or if
// blockchain is not up-to-date.
func (wallet *Wallet) TicketPrice() (*TicketPriceResponse, error) {
ctx := wallet.shutdownContext()
sdiff, err := wallet.Internal().NextStakeDifficulty(ctx)
if err != nil {
return nil, err
}
_, tipHeight := wallet.Internal().MainChainTip(ctx)
resp := &TicketPriceResponse{
TicketPrice: int64(sdiff),
Height: tipHeight,
}
return resp, nil
}
func (mw *MultiWallet) TicketPrice() (*TicketPriceResponse, error) {
bestBlock := mw.GetBestBlock()
for _, wal := range mw.wallets {
resp, err := wal.TicketPrice()
if err != nil {
return nil, err
}
if resp.Height == bestBlock.Height {
return resp, nil
}
}
return nil, errors.New(ErrWalletNotFound)
}
// PurchaseTickets purchases tickets from the wallet.
// Returns a slice of hashes for tickets purchased.
func (wallet *Wallet) PurchaseTickets(account, numTickets int32, vspHost string, vspPubKey []byte, passphrase []byte) ([]*chainhash.Hash, error) {
vspClient, err := wallet.VSPClient(vspHost, vspPubKey)
if err != nil {
return nil, fmt.Errorf("VSP Server instance failed to start: %v", err)
}
networkBackend, err := wallet.Internal().NetworkBackend()
if err != nil {
return nil, err
}
err = wallet.UnlockWallet(passphrase)
if err != nil {
return nil, translateError(err)
}
defer wallet.LockWallet()
// Use the user-specified instructions for processing fee payments
// for this ticket, rather than some default policy.
vspPolicy := vsp.Policy{
MaxFee: 0.2e8,
FeeAcct: uint32(account),
ChangeAcct: uint32(account),
}
request := &w.PurchaseTicketsRequest{
Count: int(numTickets),
SourceAccount: uint32(account),
MinConf: wallet.RequiredConfirmations(),
VSPFeeProcess: vspClient.FeePercentage,
VSPFeePaymentProcess: func(ctx context.Context, ticketHash *chainhash.Hash, feeTx *wire.MsgTx) error {
return vspClient.Process(ctx, ticketHash, feeTx, vspPolicy)
},
}
// Mixed split buying through CoinShuffle++, if configured.
if csppCfg := wallet.readCSPPConfig(); csppCfg != nil {
request.CSPPServer = csppCfg.CSPPServer
request.DialCSPPServer = csppCfg.DialCSPPServer
request.MixedAccount = csppCfg.MixedAccount
request.MixedAccountBranch = csppCfg.MixedAccountBranch
request.ChangeAccount = csppCfg.ChangeAccount
request.MixedSplitAccount = csppCfg.TicketSplitAccount
}
ctx := wallet.shutdownContext()
ticketsResponse, err := wallet.Internal().PurchaseTickets(ctx, networkBackend, request)
if err != nil {
return nil, err
}
return ticketsResponse.TicketHashes, err
}
// VSPTicketInfo returns vsp-related info for a given ticket. Returns an error
// if the ticket is not yet assigned to a VSP.
func (mw *MultiWallet) VSPTicketInfo(walletID int, hash string) (*VSPTicketInfo, error) {
wallet := mw.WalletWithID(walletID)
if wallet == nil {
return nil, fmt.Errorf("no wallet with ID %d", walletID)
}
ticketHash, err := chainhash.NewHashFromStr(hash)
if err != nil {
return nil, err
}
// Read the VSP info for this ticket from the wallet db.
ctx := wallet.shutdownContext()
walletTicketInfo, err := wallet.Internal().VSPTicketInfo(ctx, ticketHash)
if err != nil {
return nil, err
}
ticketInfo := &VSPTicketInfo{
VSP: walletTicketInfo.Host,
FeeTxHash: walletTicketInfo.FeeHash.String(),
FeeTxStatus: VSPFeeStatus(walletTicketInfo.FeeTxStatus),
}
// Cannot submit a ticketstatus api request to the VSP if
// the wallet is locked. Return just the wallet info.
if wallet.IsLocked() {
return ticketInfo, nil
}
vspClient, err := wallet.VSPClient(walletTicketInfo.Host, walletTicketInfo.PubKey)
if err != nil {
log.Warnf("unable to get vsp ticket info for %s: %v", hash, err)
return ticketInfo, nil
}
vspTicketStatus, err := vspClient.TicketStatus(ctx, ticketHash)
if err != nil {
log.Warnf("unable to get vsp ticket info for %s: %v", hash, err)
return ticketInfo, nil
}
// Parse the fee status returned by the vsp.
var vspFeeStatus VSPFeeStatus
switch vspTicketStatus.FeeTxStatus {
case "received": // received but not broadcast
vspFeeStatus = VSPFeeProcessStarted
case "broadcast": // broadcast but not confirmed
vspFeeStatus = VSPFeeProcessPaid
case "confirmed": // broadcast and confirmed
vspFeeStatus = VSPFeeProcessConfirmed
case "error":
vspFeeStatus = VSPFeeProcessErrored
default:
vspFeeStatus = VSPFeeProcessErrored
log.Warnf("VSP responded with %v for %v", vspTicketStatus.FeeTxStatus, ticketHash)
}
// Sanity check and log any observed discrepancies.
if ticketInfo.FeeTxHash != vspTicketStatus.FeeTxHash {
log.Warnf("wallet fee tx hash %s differs from vsp fee tx hash %s for ticket %s",
ticketInfo.FeeTxHash, vspTicketStatus.FeeTxHash, ticketHash)
ticketInfo.FeeTxHash = vspTicketStatus.FeeTxHash
}
if ticketInfo.FeeTxStatus != vspFeeStatus {
log.Warnf("wallet fee status %q differs from vsp fee status %q for ticket %s",
ticketInfo.FeeTxStatus, vspFeeStatus, ticketHash)
ticketInfo.FeeTxStatus = vspFeeStatus
}
return ticketInfo, nil
}
// StartTicketBuyer starts the automatic ticket buyer. The wallet
// should already be configured with the required parameters using
// wallet.SetAutoTicketsBuyerConfig().
func (wallet *Wallet) StartTicketBuyer(passphrase []byte) error {
cfg := wallet.AutoTicketsBuyerConfig()
if cfg.VspHost == "" {
return errors.New("ticket buyer config not set for this wallet")
}
if cfg.BalanceToMaintain < 0 {
return errors.New("Negative balance to maintain in ticket buyer config")
}
wallet.cancelAutoTicketBuyerMu.Lock()
if wallet.cancelAutoTicketBuyer != nil {
wallet.cancelAutoTicketBuyerMu.Unlock()
return errors.New("Ticket buyer already running")
}
ctx, cancel := wallet.shutdownContextWithCancel()
wallet.cancelAutoTicketBuyer = cancel
wallet.cancelAutoTicketBuyerMu.Unlock()
// Validate the passphrase.
if len(passphrase) > 0 && wallet.IsLocked() {
err := wallet.UnlockWallet(passphrase)
if err != nil {
return translateError(err)
}
}
// Check the VSP.
vspInfo, err := vspInfo(cfg.VspHost)
if err == nil {
cfg.vspClient, err = wallet.VSPClient(cfg.VspHost, vspInfo.PubKey)
}
if err != nil {
return fmt.Errorf("error setting up vsp client: %v", err)
}
go func() {
log.Infof("[%d] Running ticket buyer", wallet.ID)
err := wallet.runTicketBuyer(ctx, passphrase, cfg)
if err != nil {
if ctx.Err() != nil {
log.Errorf("[%d] Ticket buyer instance canceled", wallet.ID)
} else {
log.Errorf("[%d] Ticket buyer instance errored: %v", wallet.ID, err)
}
}
wallet.cancelAutoTicketBuyerMu.Lock()
wallet.cancelAutoTicketBuyer = nil
wallet.cancelAutoTicketBuyerMu.Unlock()
}()
return nil
}
// runTicketBuyer executes the ticket buyer. If the private passphrase is
// incorrect, or ever becomes incorrect due to a wallet passphrase change,
// runTicketBuyer exits with an errors.Passphrase error.
func (wallet *Wallet) runTicketBuyer(ctx context.Context, passphrase []byte, cfg *TicketBuyerConfig) error {
if len(passphrase) > 0 && wallet.IsLocked() {
err := wallet.UnlockWallet(passphrase)
if err != nil {
return translateError(err)
}
}
c := wallet.Internal().NtfnServer.MainTipChangedNotifications()
defer c.Done()
ctx, outerCancel := context.WithCancel(ctx)
defer outerCancel()
var fatal error
var fatalMu sync.Mutex
var nextIntervalStart, expiry int32
var cancels []func()
for {
select {
case <-ctx.Done():
defer outerCancel()
fatalMu.Lock()
err := fatal
fatalMu.Unlock()
if err != nil {
return err
}
return ctx.Err()
case n := <-c.C:
if len(n.AttachedBlocks) == 0 {
continue
}
tip := n.AttachedBlocks[len(n.AttachedBlocks)-1]
w := wallet.Internal()
// Don't perform any actions while transactions are not synced through
// the tip block.
rp, err := w.RescanPoint(ctx)
if err != nil {
return err
}
if rp != nil {
log.Debugf("[%d] Skipping autobuyer actions: transactions are not synced", wallet.ID)
continue
}
tipHeader, err := w.BlockHeader(ctx, tip)
if err != nil {
log.Error(err)
continue
}
height := int32(tipHeader.Height)
// Cancel any ongoing ticket purchases which are buying
// at an old ticket price or are no longer able to
// create mined tickets the window.
if height+2 >= nextIntervalStart {
for i, cancel := range cancels {
cancel()
cancels[i] = nil
}
cancels = cancels[:0]
intervalSize := int32(w.ChainParams().StakeDiffWindowSize)
currentInterval := height / intervalSize
nextIntervalStart = (currentInterval + 1) * intervalSize
// Skip this purchase when no more tickets may be purchased in the interval and
// the next sdiff is unknown. The earliest any ticket may be mined is two
// blocks from now, with the next block containing the split transaction
// that the ticket purchase spends.
if height+2 == nextIntervalStart {
log.Debugf("[%d] Skipping purchase: next sdiff interval starts soon", wallet.ID)
continue
}
// Set expiry to prevent tickets from being mined in the next
// sdiff interval. When the next block begins the new interval,
// the ticket is being purchased for the next interval; therefore
// increment expiry by a full sdiff window size to prevent it
// being mined in the interval after the next.
expiry = nextIntervalStart
if height+1 == nextIntervalStart {
expiry += intervalSize
}
}
// Get the account balance to determine how many tickets to buy
bal, err := wallet.GetAccountBalance(cfg.PurchaseAccount)
if err != nil {
return err
}
spendable := bal.Spendable
if spendable < cfg.BalanceToMaintain {
log.Debugf("[%d] Skipping purchase: low available balance", wallet.ID)
return nil
}
spendable -= cfg.BalanceToMaintain
sdiff, err := wallet.Internal().NextStakeDifficultyAfterHeader(ctx, tipHeader)
if err != nil {
return err
}
buy := int(dcrutil.Amount(spendable) / sdiff)
if buy == 0 {
log.Debugf("[%d] Skipping purchase: low available balance", wallet.ID)
return nil
}
cancelCtx, cancel := context.WithCancel(ctx)
cancels = append(cancels, cancel)
buyTicket := func() {
err := wallet.buyTicket(cancelCtx, passphrase, sdiff, expiry, cfg)
if err != nil {
switch {
// silence these errors
case errors.Is(err, errors.InsufficientBalance):
case errors.Is(err, context.Canceled):
case errors.Is(err, context.DeadlineExceeded):
default:
log.Errorf("[%d] Ticket purchasing failed: %v", wallet.ID, err)
}
if errors.Is(err, errors.Passphrase) {
fatalMu.Lock()
fatal = err
fatalMu.Unlock()
outerCancel()
}
}
}
// start separate ticket purchase for as many tickets that can be purchased
// each purchase only buy 1 ticket.
for i := 0; i < buy; i++ {
go buyTicket()
}
}
}
}
// buyTicket purchases one ticket with the wallet.
func (wallet *Wallet) buyTicket(ctx context.Context, passphrase []byte, sdiff dcrutil.Amount, expiry int32, cfg *TicketBuyerConfig) error {
ctx, task := trace.NewTask(ctx, "ticketbuyer.buy")
defer task.End()
if len(passphrase) > 0 && wallet.IsLocked() {
err := wallet.UnlockWallet(passphrase)
if err != nil {
return translateError(err)
}
}
networkBackend, err := wallet.Internal().NetworkBackend()
if err != nil {
return err
}
// Count is 1 to prevent combining multiple split outputs in one tx,
// which can be used to link the tickets eventually purchased with the
// split outputs.
vspPolicy := vsp.Policy{
MaxFee: 0.2e8,
FeeAcct: uint32(cfg.PurchaseAccount),
ChangeAcct: uint32(cfg.PurchaseAccount),
}
request := &w.PurchaseTicketsRequest{
Count: 1,
SourceAccount: uint32(cfg.PurchaseAccount),
Expiry: expiry,
MinConf: wallet.RequiredConfirmations(),
VSPFeeProcess: cfg.vspClient.FeePercentage,
VSPFeePaymentProcess: func(ctx context.Context, ticketHash *chainhash.Hash, feeTx *wire.MsgTx) error {
return cfg.vspClient.Process(ctx, ticketHash, feeTx, vspPolicy)
},
}
// Mixed split buying through CoinShuffle++, if configured.
if csppCfg := wallet.readCSPPConfig(); csppCfg != nil {
request.CSPPServer = csppCfg.CSPPServer
request.DialCSPPServer = csppCfg.DialCSPPServer
request.MixedAccount = csppCfg.MixedAccount
request.MixedAccountBranch = csppCfg.MixedAccountBranch
request.ChangeAccount = csppCfg.ChangeAccount
request.MixedSplitAccount = csppCfg.TicketSplitAccount
}
tix, err := wallet.Internal().PurchaseTickets(ctx, networkBackend, request)
if tix != nil {
for _, hash := range tix.TicketHashes {
log.Infof("[%d] Purchased ticket %v at stake difficulty %v", wallet.ID, hash, sdiff)
}
}
return err
}
// IsAutoTicketsPurchaseActive returns true if ticket buyer is active.
func (wallet *Wallet) IsAutoTicketsPurchaseActive() bool {
wallet.cancelAutoTicketBuyerMu.Lock()
defer wallet.cancelAutoTicketBuyerMu.Unlock()
return wallet.cancelAutoTicketBuyer != nil
}
// StopAutoTicketsPurchase stops the automatic ticket buyer.
func (mw *MultiWallet) StopAutoTicketsPurchase(walletID int) error {
wallet := mw.WalletWithID(walletID)
if wallet == nil {
return errors.New(ErrNotExist)
}
wallet.cancelAutoTicketBuyerMu.Lock()
defer wallet.cancelAutoTicketBuyerMu.Unlock()
if wallet.cancelAutoTicketBuyer == nil {
return errors.New(ErrInvalid)
}
wallet.cancelAutoTicketBuyer()
wallet.cancelAutoTicketBuyer = nil
return nil
}
// SetAutoTicketsBuyerConfig sets ticket buyer config for the wallet.
func (wallet *Wallet) SetAutoTicketsBuyerConfig(vspHost string, purchaseAccount int32, amountToMaintain int64) {
wallet.SetLongConfigValueForKey(TicketBuyerATMConfigKey, amountToMaintain)
wallet.SetInt32ConfigValueForKey(TicketBuyerAccountConfigKey, purchaseAccount)
wallet.SetStringConfigValueForKey(TicketBuyerVSPHostConfigKey, vspHost)
}
// AutoTicketsBuyerConfig returns the previously set ticket buyer config for
// the wallet.
func (wallet *Wallet) AutoTicketsBuyerConfig() *TicketBuyerConfig {
btm := wallet.ReadLongConfigValueForKey(TicketBuyerATMConfigKey, -1)
accNum := wallet.ReadInt32ConfigValueForKey(TicketBuyerAccountConfigKey, -1)
vspHost := wallet.ReadStringConfigValueForKey(TicketBuyerVSPHostConfigKey, "")
return &TicketBuyerConfig{
VspHost: vspHost,
PurchaseAccount: accNum,
BalanceToMaintain: btm,
}
}
// TicketBuyerConfigIsSet checks if ticket buyer config is set for the wallet.
func (wallet *Wallet) TicketBuyerConfigIsSet() bool {
return wallet.ReadStringConfigValueForKey(TicketBuyerVSPHostConfigKey, "") != ""
}
// ClearTicketBuyerConfig clears the wallet's ticket buyer config.
func (mw *MultiWallet) ClearTicketBuyerConfig(walletID int) error {
wallet := mw.WalletWithID(walletID)
if wallet == nil {
return errors.New(ErrNotExist)
}
mw.SetLongConfigValueForKey(TicketBuyerATMConfigKey, -1)
mw.SetInt32ConfigValueForKey(TicketBuyerAccountConfigKey, -1)
mw.SetStringConfigValueForKey(TicketBuyerVSPHostConfigKey, "")
return nil
}
// NextTicketPriceRemaining returns the remaning time in seconds of a ticket for the next block,
// if secs equal 0 is imminent
func (mw *MultiWallet) NextTicketPriceRemaining() (secs int64, err error) {
params, er := utils.ChainParams(mw.chainParams.Name)
if er != nil {
secs, err = -1, er
return
}
bestBestBlock := mw.GetBestBlock()
idxBlockInWindow := int(int64(bestBestBlock.Height)%params.StakeDiffWindowSize) + 1
blockTime := params.TargetTimePerBlock.Nanoseconds()
windowSize := params.StakeDiffWindowSize
x := (windowSize - int64(idxBlockInWindow)) * blockTime
if x == 0 {
secs, err = 0, nil
return
}
secs, err = int64(time.Duration(x).Seconds()), nil
return
}
// UnspentUnexpiredTickets returns all Unmined, Immature and Live tickets.
func (wallet *Wallet) UnspentUnexpiredTickets() ([]Transaction, error) {
var tickets []Transaction
for _, filter := range []int32{TxFilterUnmined, TxFilterImmature, TxFilterLive} {
tx, err := wallet.GetTransactionsRaw(0, 0, filter, true)
if err != nil {
return nil, err
}
tickets = append(tickets, tx...)
}
return tickets, nil
}