This repository has been archived by the owner on May 16, 2019. It is now read-only.
forked from cjepson/dcrticketbuyer
-
Notifications
You must be signed in to change notification settings - Fork 7
/
mempool.go
96 lines (83 loc) · 2.49 KB
/
mempool.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
// helpers.go
package main
import (
"bytes"
"time"
"github.com/decred/dcrd/dcrjson"
"github.com/decred/dcrutil"
)
var (
// stakeInfoReqTries is the maximum number of times to try
// GetStakeInfo before failing.
stakeInfoReqTries = 20
// stakeInfoReqTryDelay is the time in seconds to wait before
// doing another GetStakeInfo request.
stakeInfoReqTryDelay = time.Second * 1
)
// ownTicketsInMempool finds all the tickets owned by the user in the
// daemon mempool. It searches for the ticket address if it is specified,
// and otherwise uses getstakeinfo to determine this number.
func (t *ticketPurchaser) ownTicketsInMempool() (int, error) {
tickets := 0
// Ticket address is specified and may not belong to our own
// wallet. Search the mempool directly for the number of tickets.
if t.ticketAddress != nil {
tiHashes, err := t.dcrdChainSvr.GetRawMempool(dcrjson.GRMTickets)
if err != nil {
return 0, err
}
// Fetch each ticket and check the address it pays out to.
for i := range tiHashes {
raw, err := t.dcrdChainSvr.GetRawTransactionVerbose(tiHashes[i])
if err != nil {
return 0, err
}
// Tickets can only pay to a single address. Assume that
// the address is on the right network.
addrStr := raw.Vout[0].ScriptPubKey.Addresses[0]
addr, err := dcrutil.DecodeNetworkAddress(addrStr)
if err != nil {
return 0, err
}
if bytes.Equal(addr.ScriptAddress(),
t.ticketAddress.ScriptAddress()) {
tickets++
}
}
return tickets, nil
}
// The ticket address is generated by the wallet
// and is assumed to be owned by the wallet. Use
// getstakeinfo to figure out the number of tickets.
//
// It can take a little while for the wallet to sync,
// so loop this and recheck to see if we've got the
// next block attached yet.
var curStakeInfo *dcrjson.GetStakeInfoResult
var err error
for i := 0; i < stakeInfoReqTries; i++ {
curStakeInfo, err = t.dcrwChainSvr.GetStakeInfo()
if err != nil {
log.Tracef("Failed to fetch stake information "+
"on attempt %v: %v", i, err.Error())
time.Sleep(stakeInfoReqTryDelay)
continue
}
if err == nil {
break
}
}
if err != nil {
return 0, err
}
return int(curStakeInfo.OwnMempoolTix), nil
}
// allTicketsInMempool fetches the number of tickets currently in the memory
// pool.
func (t *ticketPurchaser) allTicketsInMempool() (int, error) {
tfi, err := t.dcrdChainSvr.TicketFeeInfo(&zeroUint32, &zeroUint32)
if err != nil {
return 0, err
}
return int(tfi.FeeInfoMempool.Number), nil
}