-
Notifications
You must be signed in to change notification settings - Fork 8
/
mempool.go
90 lines (78 loc) · 2.42 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
// Copyright (c) 2016 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package ticketbuyer
import (
"bytes"
"time"
"github.com/HcashOrg/hcashd/hcashjson"
"github.com/HcashOrg/hcashutil"
"github.com/HcashOrg/hcashwallet/wallet"
)
// 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.hcashdChainSvr.GetRawMempool(hcashjson.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.hcashdChainSvr.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 := hcashutil.DecodeAddress(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 *wallet.StakeInfoData
var err error
for i := 0; i < stakeInfoReqTries; i++ {
curStakeInfo, err = t.wallet.StakeInfo(false, t.hcashdChainSvr)
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.hcashdChainSvr.TicketFeeInfo(&zeroUint32, &zeroUint32)
if err != nil {
return 0, err
}
return int(tfi.FeeInfoMempool.Number), nil
}