/
tickets.go
345 lines (311 loc) · 10.1 KB
/
tickets.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
// Copyright (c) 2016-2020 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wallet
import (
"context"
"time"
"github.com/EXCCoin/exccwallet/v2/errors"
"github.com/EXCCoin/exccwallet/v2/rpc/client/dcrd"
"github.com/EXCCoin/exccwallet/v2/wallet/udb"
"github.com/EXCCoin/exccwallet/v2/wallet/walletdb"
"github.com/EXCCoin/exccd/blockchain/stake/v4"
"github.com/EXCCoin/exccd/chaincfg/chainhash"
dcrdtypes "github.com/EXCCoin/exccd/rpc/jsonrpc/types/v3"
"github.com/EXCCoin/exccd/txscript/v4/stdaddr"
"github.com/EXCCoin/exccd/wire"
"golang.org/x/sync/errgroup"
)
// GenerateVoteTx creates a vote transaction for a chosen ticket purchase hash
// using the provided votebits. The ticket purchase transaction must be stored
// by the wallet.
//
// Deprecated: This method will not produce the proper vote subsidy after
// DCP0010 activation.
func (w *Wallet) GenerateVoteTx(ctx context.Context, blockHash *chainhash.Hash, height int32,
ticketHash *chainhash.Hash, voteBits stake.VoteBits) (*wire.MsgTx, error) {
const op errors.Op = "wallet.GenerateVoteTx"
var vote *wire.MsgTx
const dcp0010Active = false
err := walletdb.View(ctx, w.db, func(dbtx walletdb.ReadTx) error {
addrmgrNs := dbtx.ReadBucket(waddrmgrNamespaceKey)
txmgrNs := dbtx.ReadBucket(wtxmgrNamespaceKey)
ticketPurchase, err := w.txStore.Tx(txmgrNs, ticketHash)
if err != nil {
return err
}
vote, err = createUnsignedVote(ticketHash, ticketPurchase,
height, blockHash, voteBits, w.subsidyCache, w.chainParams,
dcp0010Active)
if err != nil {
return errors.E(op, err)
}
err = w.signVote(addrmgrNs, ticketPurchase, vote)
if err != nil {
return errors.E(op, err)
}
return err
})
if err != nil {
return nil, errors.E(op, err)
}
return vote, nil
}
// LiveTicketHashes returns the hashes of live tickets that the wallet has
// purchased or has voting authority for.
func (w *Wallet) LiveTicketHashes(ctx context.Context, rpcCaller Caller, includeImmature bool) ([]chainhash.Hash, error) {
const op errors.Op = "wallet.LiveTicketHashes"
var ticketHashes []chainhash.Hash
var maybeLive []*chainhash.Hash
extraTickets := w.stakeMgr.DumpSStxHashes()
var tipHeight int32 // Assigned in view below.
err := walletdb.View(ctx, w.db, func(dbtx walletdb.ReadTx) error {
txmgrNs := dbtx.ReadBucket(wtxmgrNamespaceKey)
// Remove tickets from the extraTickets slice if they will appear in the
// ticket iteration below.
hashes := extraTickets
extraTickets = hashes[:0]
for i := range hashes {
h := &hashes[i]
if !w.txStore.ExistsTx(txmgrNs, h) {
extraTickets = append(extraTickets, *h)
}
}
_, tipHeight = w.txStore.MainChainTip(dbtx)
it := w.txStore.IterateTickets(dbtx)
defer it.Close()
for it.Next() {
// Tickets that are mined at a height beyond the expiry height can
// not be live.
if ticketExpired(w.chainParams, it.Block.Height, tipHeight) {
continue
}
// Tickets that have not reached ticket maturity are immature.
// Exclude them unless the caller requested to include immature
// tickets.
if !ticketMatured(w.chainParams, it.Block.Height, tipHeight) {
if includeImmature {
ticketHashes = append(ticketHashes, it.Hash)
}
continue
}
// The ticket may be live. Because the selected state of tickets is
// not yet known by the wallet, this must be queried over RPC. Add
// this hash to a slice of ticket purchase hashes to check later.
hash := it.Hash
maybeLive = append(maybeLive, &hash)
}
return it.Err()
})
if err != nil {
return nil, errors.E(op, err)
}
// Determine if the extra tickets are immature or possibly live. Because
// these transactions are not part of the wallet's transaction history, dcrd
// must be queried for their blockchain height. This functionality requires
// the dcrd transaction index to be enabled.
var g errgroup.Group
type extraTicketResult struct {
valid bool // unspent with known height
height int32
}
extraTicketResults := make([]extraTicketResult, len(extraTickets))
for i := range extraTickets {
i := i
g.Go(func() error {
// gettxout is used first as an optimization to check that output 0
// of the ticket is unspent.
var txOut *dcrdtypes.GetTxOutResult
const index = 0
const tree = 1
err := rpcCaller.Call(ctx, "gettxout", &txOut, extraTickets[i].String(), index, tree)
if err != nil || txOut == nil {
return nil
}
var grt struct {
BlockHeight int32 `json:"blockheight"`
}
err = rpcCaller.Call(ctx, "getrawtransaction", &grt, extraTickets[i].String(), 1)
if err != nil {
return nil
}
extraTicketResults[i] = extraTicketResult{true, grt.BlockHeight}
return nil
})
}
err = g.Wait()
if err != nil {
return nil, err
}
for i := range extraTickets {
r := &extraTicketResults[i]
if !r.valid {
continue
}
// Same checks as above in the db view.
if ticketExpired(w.chainParams, r.height, tipHeight) {
continue
}
if !ticketMatured(w.chainParams, r.height, tipHeight) {
if includeImmature {
ticketHashes = append(ticketHashes, extraTickets[i])
}
continue
}
maybeLive = append(maybeLive, &extraTickets[i])
}
// If there are no possibly live tickets to check, ticketHashes contains all
// of the results.
if len(maybeLive) == 0 {
return ticketHashes, nil
}
// Use RPC to query which of the possibly-live tickets are really live.
rpc := dcrd.New(rpcCaller)
live, err := rpc.ExistsLiveTickets(ctx, maybeLive)
if err != nil {
return nil, errors.E(op, err)
}
for i, h := range maybeLive {
if live.Get(i) {
ticketHashes = append(ticketHashes, *h)
}
}
return ticketHashes, nil
}
// TicketHashesForVotingAddress returns the hashes of all tickets with voting
// rights delegated to votingAddr. This function does not return the hashes of
// pruned tickets.
func (w *Wallet) TicketHashesForVotingAddress(ctx context.Context, votingAddr stdaddr.Address) ([]chainhash.Hash, error) {
const op errors.Op = "wallet.TicketHashesForVotingAddress"
var ticketHashes []chainhash.Hash
err := walletdb.View(ctx, w.db, func(tx walletdb.ReadTx) error {
stakemgrNs := tx.ReadBucket(wstakemgrNamespaceKey)
txmgrNs := tx.ReadBucket(wtxmgrNamespaceKey)
dump, err := w.stakeMgr.DumpSStxHashesForAddress(
stakemgrNs, votingAddr)
if err != nil {
return err
}
// Exclude hashes for unsaved transactions.
ticketHashes = dump[:0]
for i := range dump {
h := &dump[i]
if w.txStore.ExistsTx(txmgrNs, h) {
ticketHashes = append(ticketHashes, *h)
}
}
return nil
})
if err != nil {
return nil, errors.E(op, err)
}
return ticketHashes, nil
}
// RevokeTickets creates and sends revocation transactions for any unrevoked
// missed and expired tickets. The wallet must be unlocked to generate any
// revocations.
func (w *Wallet) RevokeTickets(ctx context.Context, rpcCaller Caller) error {
const op errors.Op = "wallet.RevokeTickets"
var ticketHashes []chainhash.Hash
err := walletdb.View(ctx, w.db, func(dbtx walletdb.ReadTx) error {
var err error
_, tipHeight := w.txStore.MainChainTip(dbtx)
ticketHashes, err = w.txStore.UnspentTickets(dbtx, tipHeight, false)
return err
})
if err != nil {
return errors.E(op, err)
}
ticketHashPtrs := make([]*chainhash.Hash, len(ticketHashes))
for i := range ticketHashes {
ticketHashPtrs[i] = &ticketHashes[i]
}
rpc := dcrd.New(rpcCaller)
expired, missed, err := rpc.ExistsExpiredMissedTickets(ctx, ticketHashPtrs)
if err != nil {
return errors.E(op, err)
}
revokableTickets := make([]*chainhash.Hash, 0, len(ticketHashes))
for i, p := range ticketHashPtrs {
if !(expired.Get(i) || missed.Get(i)) {
continue
}
revokableTickets = append(revokableTickets, p)
}
feePerKb := w.RelayFee()
revocations := make([]*wire.MsgTx, 0, len(revokableTickets))
err = walletdb.View(ctx, w.db, func(dbtx walletdb.ReadTx) error {
for _, ticketHash := range revokableTickets {
addrmgrNs := dbtx.ReadBucket(waddrmgrNamespaceKey)
txmgrNs := dbtx.ReadBucket(wtxmgrNamespaceKey)
ticketPurchase, err := w.txStore.Tx(txmgrNs, ticketHash)
if err != nil {
return err
}
// Don't create revocations when this wallet doesn't have voting
// authority or the private key to revoke.
owned, haveKey, err := w.hasVotingAuthority(addrmgrNs, ticketPurchase)
if err != nil {
return err
}
if !(owned && haveKey) {
continue
}
revocation, err := createUnsignedRevocation(ticketHash,
ticketPurchase, feePerKb, w.chainParams)
if err != nil {
return err
}
err = w.signRevocation(addrmgrNs, ticketPurchase, revocation)
if err != nil {
return err
}
revocations = append(revocations, revocation)
}
return nil
})
if err != nil {
return errors.E(op, err)
}
for i, revocation := range revocations {
rec, err := udb.NewTxRecordFromMsgTx(revocation, time.Now())
if err != nil {
return errors.E(op, err)
}
var watch []wire.OutPoint
//w.lockedOutpointMu intentionally not locked.
err = walletdb.Update(ctx, w.db, func(dbtx walletdb.ReadWriteTx) error {
// Could be more efficient by avoiding processTransaction, as we
// know it is a revocation.
watch, err = w.processTransactionRecord(ctx, dbtx, rec, nil, nil)
if err != nil {
return errors.E(op, err)
}
return rpc.PublishTransaction(ctx, revocation)
})
if err != nil {
return errors.E(op, err)
}
log.Infof("Revoked ticket %v with revocation %v", revokableTickets[i],
&rec.Hash)
err = rpc.LoadTxFilter(ctx, false, nil, watch)
if err != nil {
log.Errorf("Failed to watch outpoints: %v", err)
}
}
return nil
}
// RevokeExpiredTickets no longer revokes any tickets since revocations are now
// automatically created per DCP0009.
//
// Deprecated: this method will be removed in the next major version.
func (w *Wallet) RevokeExpiredTickets(ctx context.Context, p Peer) (err error) {
return nil
}
// RevokeTicket no longer revokes any tickets since revocations are now
// automatically created per DCP0009.
//
// Deprecated: this method will be removed in the next major version.
func (w *Wallet) RevokeTicket(ctx context.Context, ticketHash *chainhash.Hash, p Peer) error {
return nil
}