forked from decred/dcrwallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stake.go
597 lines (505 loc) · 18.8 KB
/
stake.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
// Copyright (c) 2015-2017 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package udb
import (
"bytes"
"fmt"
"sync"
"time"
"github.com/decred/dcrd/blockchain/stake"
"github.com/decred/dcrd/chaincfg"
"github.com/decred/dcrd/chaincfg/chainec"
"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrd/wire"
"github.com/decred/dcrutil"
"github.com/decred/dcrwallet/apperrors"
walletchain "github.com/decred/dcrwallet/chain"
"github.com/decred/dcrwallet/walletdb"
)
func stakeStoreError(code apperrors.Code, str string, err error) error {
return apperrors.E{ErrorCode: code, Description: str, Err: err}
}
// sstxRecord is the structure for a stored SStx.
type sstxRecord struct {
tx *dcrutil.Tx
ts time.Time
voteBitsSet bool // Removed in version 3
voteBits uint16 // Removed in version 3
voteBitsExt []byte // Removed in version 3
}
// ssgenRecord is the structure for a stored SSGen tx. There's no
// real reason to store the actual transaction I don't think,
// the inputs and outputs are all predetermined from the block
// height and the original SStx it references.
//
// TODO Store the extended votebits, too.
type ssgenRecord struct {
blockHash chainhash.Hash
blockHeight uint32
txHash chainhash.Hash
voteBits uint16
ts time.Time
}
// ssrtxRecord is the structure for a stored SSRtx. While the
// ssrtx itself does not include the block hash or block height,
// we still preserve that so that we know the block ntfn that
// informed us that the sstx was missed.
type ssrtxRecord struct {
blockHash chainhash.Hash
blockHeight uint32
txHash chainhash.Hash
ts time.Time
}
// TicketStatus is the current status of a stake pool ticket.
type TicketStatus uint8
const (
// TSImmatureOrLive indicates that the ticket is either
// live or immature.
TSImmatureOrLive = iota
// TSVoted indicates that the ticket was spent as a vote.
TSVoted
// TSMissed indicates that the ticket was spent as a
// revocation.
TSMissed
)
// PoolTicket is a 73-byte struct that is used to preserve user's
// ticket information when they have an account at the stake pool.
type PoolTicket struct {
Ticket chainhash.Hash
HeightTicket uint32
Status TicketStatus
HeightSpent uint32
SpentBy chainhash.Hash
}
// StakePoolUser is a list of tickets for a given user (P2SH
// address) in the stake pool.
type StakePoolUser struct {
Tickets []*PoolTicket
InvalidTickets []*chainhash.Hash
}
// StakeStore represents a safely accessible database of
// stake transactions.
type StakeStore struct {
Params *chaincfg.Params
Manager *Manager
chainSvr *walletchain.RPCClient
ownedSStxs map[chainhash.Hash]struct{}
mtx sync.RWMutex // only protects ownedSStxs
}
// checkHashInStore checks if a hash exists in ownedSStxs.
func (s *StakeStore) checkHashInStore(hash *chainhash.Hash) bool {
_, exists := s.ownedSStxs[*hash]
return exists
}
// OwnTicket returns whether the ticket is tracked by the stake manager.
func (s *StakeStore) OwnTicket(hash *chainhash.Hash) bool {
s.mtx.RLock()
owned := s.checkHashInStore(hash)
s.mtx.RUnlock()
return owned
}
// addHashToStore adds a hash into ownedSStxs.
func (s *StakeStore) addHashToStore(hash *chainhash.Hash) {
s.ownedSStxs[*hash] = struct{}{}
}
// insertSStx inserts an SStx into the store.
func (s *StakeStore) insertSStx(ns walletdb.ReadWriteBucket, sstx *dcrutil.Tx) error {
// If we already have the SStx, no need to
// try to include twice.
exists := s.checkHashInStore(sstx.Hash())
if exists {
log.Tracef("Attempted to insert SStx %v into the stake store, "+
"but the SStx already exists.", sstx.Hash())
return nil
}
record := &sstxRecord{
tx: sstx,
ts: time.Now(),
}
// Add the SStx to the database.
err := putSStxRecord(ns, record, DBVersion)
if err != nil {
return err
}
// Add the SStx's hash to the internal list in the store.
s.addHashToStore(sstx.Hash())
return nil
}
// InsertSStx is the exported version of insertSStx that is safe for concurrent
// access.
func (s *StakeStore) InsertSStx(ns walletdb.ReadWriteBucket, sstx *dcrutil.Tx) error {
s.mtx.Lock()
err := s.insertSStx(ns, sstx)
s.mtx.Unlock()
return err
}
// dumpSStxHashes dumps the hashes of all owned SStxs. Note
// that this doesn't use the DB.
func (s *StakeStore) dumpSStxHashes() []chainhash.Hash {
// Copy the hash list of sstxs. You could pass the pointer
// directly but you risk that the size of the internal
// ownedSStxs is later modified while the end user is
// working with the returned list.
ownedSStxs := make([]chainhash.Hash, len(s.ownedSStxs))
itr := 0
for hash := range s.ownedSStxs {
ownedSStxs[itr] = hash
itr++
}
return ownedSStxs
}
// DumpSStxHashes returns the hashes of all wallet ticket purchase transactions.
func (s *StakeStore) DumpSStxHashes() []chainhash.Hash {
defer s.mtx.RUnlock()
s.mtx.RLock()
return s.dumpSStxHashes()
}
// dumpSStxHashes dumps the hashes of all owned SStxs for some address.
func (s *StakeStore) dumpSStxHashesForAddress(ns walletdb.ReadBucket, addr dcrutil.Address) ([]chainhash.Hash, error) {
// Extract the HASH160 script hash; if it's not 20 bytes
// long, return an error.
hash160 := addr.ScriptAddress()
if len(hash160) != 20 {
str := "stake store is closed"
return nil, stakeStoreError(apperrors.ErrInput, str, nil)
}
_, addrIsP2SH := addr.(*dcrutil.AddressScriptHash)
allTickets := s.dumpSStxHashes()
var ticketsForAddr []chainhash.Hash
// Access the database and store the result locally.
for _, h := range allTickets {
thisHash160, p2sh, err := fetchSStxRecordSStxTicketHash160(ns, &h, DBVersion)
if err != nil {
str := "failure getting ticket 0th out script hashes from db"
return nil, stakeStoreError(apperrors.ErrDatabase, str, err)
}
if addrIsP2SH != p2sh {
continue
}
if bytes.Equal(hash160, thisHash160) {
ticketsForAddr = append(ticketsForAddr, h)
}
}
return ticketsForAddr, nil
}
// DumpSStxHashesForAddress returns the hashes of all wallet ticket purchase
// transactions for an address.
func (s *StakeStore) DumpSStxHashesForAddress(ns walletdb.ReadBucket, addr dcrutil.Address) ([]chainhash.Hash, error) {
defer s.mtx.RUnlock()
s.mtx.RLock()
return s.dumpSStxHashesForAddress(ns, addr)
}
// sstxAddress returns the address for a given ticket.
func (s *StakeStore) sstxAddress(ns walletdb.ReadBucket, hash *chainhash.Hash) (dcrutil.Address, error) {
// Access the database and store the result locally.
thisHash160, p2sh, err := fetchSStxRecordSStxTicketHash160(ns, hash, DBVersion)
if err != nil {
str := "failure getting ticket 0th out script hashes from db"
return nil, stakeStoreError(apperrors.ErrDatabase, str, err)
}
var addr dcrutil.Address
if p2sh {
addr, err = dcrutil.NewAddressScriptHashFromHash(thisHash160, s.Params)
} else {
addr, err = dcrutil.NewAddressPubKeyHash(thisHash160, s.Params, chainec.ECTypeSecp256k1)
}
if err != nil {
str := "failure getting ticket 0th out script hashes from db"
return nil, stakeStoreError(apperrors.ErrDatabase, str, err)
}
return addr, nil
}
// SStxAddress is the exported, concurrency safe version of sstxAddress.
func (s *StakeStore) SStxAddress(ns walletdb.ReadBucket, hash *chainhash.Hash) (dcrutil.Address, error) {
return s.sstxAddress(ns, hash)
}
// dumpSSGenHashes fetches and returns the entire list of votes generated by
// this wallet, including votes that were produced but were never included in
// the blockchain.
func (s *StakeStore) dumpSSGenHashes(ns walletdb.ReadBucket) ([]chainhash.Hash, error) {
var voteList []chainhash.Hash
// Open the vite records database.
bucket := ns.NestedReadBucket(ssgenRecordsBucketName)
// Store each hash sequentially.
err := bucket.ForEach(func(k []byte, v []byte) error {
recs, errDeser := deserializeSSGenRecords(v)
if errDeser != nil {
return errDeser
}
for _, rec := range recs {
voteList = append(voteList, rec.txHash)
}
return nil
})
return voteList, err
}
// DumpSSGenHashes is the exported version of dumpSSGenHashes that is safe
// for concurrent access.
func (s *StakeStore) DumpSSGenHashes(ns walletdb.ReadBucket) ([]chainhash.Hash, error) {
return s.dumpSSGenHashes(ns)
}
// dumpSSRtxTickets fetches the entire list of tickets spent as revocations
// by this wallet.
func (s *StakeStore) dumpSSRtxTickets(ns walletdb.ReadBucket) ([]chainhash.Hash, error) {
var ticketList []chainhash.Hash
// Open the revocation records database.
bucket := ns.NestedReadBucket(ssrtxRecordsBucketName)
// Store each hash sequentially.
err := bucket.ForEach(func(k []byte, v []byte) error {
ticket, errDeser := chainhash.NewHash(k)
if errDeser != nil {
return errDeser
}
ticketList = append(ticketList, *ticket)
return nil
})
return ticketList, err
}
// DumpSSRtxTickets is the exported version of dumpSSRtxTickets that is safe
// for concurrent access.
func (s *StakeStore) DumpSSRtxTickets(ns walletdb.ReadBucket) ([]chainhash.Hash, error) {
return s.dumpSSRtxTickets(ns)
}
// insertSSGen inserts an SSGen record into the DB (keyed to the SStx it
// spends.
func insertSSGen(ns walletdb.ReadWriteBucket, blockHash *chainhash.Hash, blockHeight int64,
ssgenHash *chainhash.Hash, voteBits uint16, sstxHash *chainhash.Hash) error {
if blockHeight <= 0 {
return fmt.Errorf("invalid SSGen block height")
}
record := &ssgenRecord{
*blockHash,
uint32(blockHeight),
*ssgenHash,
voteBits,
time.Now(),
}
// Add the SSGen to the database.
return putSSGenRecord(ns, sstxHash, record)
}
// InsertSSGen is the exported version of insertSSGen that is safe for
// concurrent access.
func (s *StakeStore) InsertSSGen(ns walletdb.ReadWriteBucket, blockHash *chainhash.Hash, blockHeight int64, ssgenHash *chainhash.Hash, voteBits uint16, sstxHash *chainhash.Hash) error {
return insertSSGen(ns, blockHash, blockHeight, ssgenHash, voteBits, sstxHash)
}
// TicketPurchase returns the ticket purchase transaction recorded in the "stake
// manager" portion of the DB.
//
// TODO: This is redundant and should be looked up in from the transaction
// manager. Left for now for compatibility.
func (s *StakeStore) TicketPurchase(dbtx walletdb.ReadTx, hash *chainhash.Hash) (*wire.MsgTx, error) {
ns := dbtx.ReadBucket(wstakemgrBucketKey)
ticketRecord, err := fetchSStxRecord(ns, hash, DBVersion)
if err != nil {
return nil, err
}
return ticketRecord.tx.MsgTx(), nil
}
// StoreVoteInfo records information about a vote transaction.
//
// TODO: Much of this is redundant and we probably don't want to track it here
// anyways. Would be better to handle this in the transaction manager.
func (s *StakeStore) StoreVoteInfo(dbtx walletdb.ReadWriteTx, ticketHash, voteHash, blockHash *chainhash.Hash,
blockHeight int32, voteBits stake.VoteBits) error {
ns := dbtx.ReadWriteBucket(wstakemgrBucketKey)
return insertSSGen(ns, blockHash, int64(blockHeight), voteHash, voteBits.Bits,
ticketHash)
}
// insertSSRtx inserts an SSRtx record into the DB (keyed to the SStx it
// spends.
func (s *StakeStore) insertSSRtx(ns walletdb.ReadWriteBucket, blockHash *chainhash.Hash, blockHeight int64, ssrtxHash *chainhash.Hash, sstxHash *chainhash.Hash) error {
if blockHeight <= 0 {
return fmt.Errorf("invalid SSRtx block height")
}
record := &ssrtxRecord{
*blockHash,
uint32(blockHeight),
*ssrtxHash,
time.Now(),
}
// Add the SSRtx to the database.
return putSSRtxRecord(ns, sstxHash, record)
}
// StoreRevocationInfo records information about a revocation transaction.
//
// TODO: Much of this is redundant and we probably don't want to track it here
// anyways. Would be better to handle this in the transaction manager.
func (s *StakeStore) StoreRevocationInfo(dbtx walletdb.ReadWriteTx, ticketHash, revocationHash *chainhash.Hash,
blockHash *chainhash.Hash, blockHeight int32) error {
ns := dbtx.ReadWriteBucket(wstakemgrBucketKey)
return s.insertSSRtx(ns, blockHash, int64(blockHeight), revocationHash,
ticketHash)
}
// updateStakePoolUserTickets updates a stake pool ticket for a given user.
// If the ticket does not currently exist in the database, it adds it. If it
// does exist (the ticket hash exists), it replaces the old record.
func (s *StakeStore) updateStakePoolUserTickets(ns walletdb.ReadWriteBucket, user dcrutil.Address, ticket *PoolTicket) error {
_, isScriptHash := user.(*dcrutil.AddressScriptHash)
_, isP2PKH := user.(*dcrutil.AddressPubKeyHash)
if !(isScriptHash || isP2PKH) {
str := fmt.Sprintf("user %v is invalid", user.EncodeAddress())
return stakeStoreError(apperrors.ErrBadPoolUserAddr, str, nil)
}
scriptHashB := user.ScriptAddress()
scriptHash := new([20]byte)
copy(scriptHash[:], scriptHashB)
return updateStakePoolUserTickets(ns, *scriptHash, ticket)
}
// UpdateStakePoolUserTickets is the exported and concurrency safe form of
// updateStakePoolUserTickets.
func (s *StakeStore) UpdateStakePoolUserTickets(ns walletdb.ReadWriteBucket, user dcrutil.Address, ticket *PoolTicket) error {
return s.updateStakePoolUserTickets(ns, user, ticket)
}
// removeStakePoolUserInvalTickets prepares the user.Address and asks stakedb
// to remove the formerly invalid tickets.
func (s *StakeStore) removeStakePoolUserInvalTickets(ns walletdb.ReadWriteBucket, user dcrutil.Address,
ticket *chainhash.Hash) error {
_, isScriptHash := user.(*dcrutil.AddressScriptHash)
_, isP2PKH := user.(*dcrutil.AddressPubKeyHash)
if !(isScriptHash || isP2PKH) {
str := fmt.Sprintf("user %v is invalid", user.EncodeAddress())
return stakeStoreError(apperrors.ErrBadPoolUserAddr, str, nil)
}
scriptHashB := user.ScriptAddress()
scriptHash := new([20]byte)
copy(scriptHash[:], scriptHashB)
return removeStakePoolInvalUserTickets(ns, *scriptHash, ticket)
}
// RemoveStakePoolUserInvalTickets is the exported and concurrency safe form of
// removetStakePoolUserInvalTickets.
func (s *StakeStore) RemoveStakePoolUserInvalTickets(ns walletdb.ReadWriteBucket, user dcrutil.Address,
ticket *chainhash.Hash) error {
return s.removeStakePoolUserInvalTickets(ns, user, ticket)
}
// updateStakePoolUserInvalTickets updates the list of invalid stake pool
// tickets for a given user. If the ticket does not currently exist in the
// database, it adds it.
func (s *StakeStore) updateStakePoolUserInvalTickets(ns walletdb.ReadWriteBucket, user dcrutil.Address, ticket *chainhash.Hash) error {
_, isScriptHash := user.(*dcrutil.AddressScriptHash)
_, isP2PKH := user.(*dcrutil.AddressPubKeyHash)
if !(isScriptHash || isP2PKH) {
str := fmt.Sprintf("user %v is invalid", user.EncodeAddress())
return stakeStoreError(apperrors.ErrBadPoolUserAddr, str, nil)
}
scriptHashB := user.ScriptAddress()
scriptHash := new([20]byte)
copy(scriptHash[:], scriptHashB)
return updateStakePoolInvalUserTickets(ns, *scriptHash, ticket)
}
// UpdateStakePoolUserInvalTickets is the exported and concurrency safe form of
// updateStakePoolUserInvalTickets.
func (s *StakeStore) UpdateStakePoolUserInvalTickets(ns walletdb.ReadWriteBucket, user dcrutil.Address, ticket *chainhash.Hash) error {
return s.updateStakePoolUserInvalTickets(ns, user, ticket)
}
func stakePoolUserInfo(ns walletdb.ReadBucket, user dcrutil.Address) (*StakePoolUser, error) {
_, isScriptHash := user.(*dcrutil.AddressScriptHash)
_, isP2PKH := user.(*dcrutil.AddressPubKeyHash)
if !(isScriptHash || isP2PKH) {
str := fmt.Sprintf("user %v is invalid", user.EncodeAddress())
return nil, stakeStoreError(apperrors.ErrBadPoolUserAddr, str, nil)
}
scriptHashB := user.ScriptAddress()
scriptHash := new([20]byte)
copy(scriptHash[:], scriptHashB)
stakePoolUser := new(StakePoolUser)
// Catch missing user errors below and blank out the stake
// pool user information for the section if the user has
// no entries.
missingValidTickets, missingInvalidTickets := false, false
userTickets, fetchErrVal := fetchStakePoolUserTickets(ns, *scriptHash)
if fetchErrVal != nil {
stakeMgrErr, is := fetchErrVal.(apperrors.E)
if is {
missingValidTickets = stakeMgrErr.ErrorCode ==
apperrors.ErrPoolUserTicketsNotFound
} else {
return nil, fetchErrVal
}
}
if missingValidTickets {
userTickets = make([]*PoolTicket, 0)
}
invalTickets, fetchErrInval := fetchStakePoolUserInvalTickets(ns,
*scriptHash)
if fetchErrInval != nil {
stakeMgrErr, is := fetchErrInval.(apperrors.E)
if is {
missingInvalidTickets = stakeMgrErr.ErrorCode ==
apperrors.ErrPoolUserInvalTcktsNotFound
} else {
return nil, fetchErrInval
}
}
if missingInvalidTickets {
invalTickets = make([]*chainhash.Hash, 0)
}
stakePoolUser.Tickets = userTickets
stakePoolUser.InvalidTickets = invalTickets
return stakePoolUser, nil
}
// StakePoolUserInfo returns the stake pool user information for a given stake
// pool user, keyed to their P2SH voting address.
func (s *StakeStore) StakePoolUserInfo(ns walletdb.ReadBucket, user dcrutil.Address) (*StakePoolUser, error) {
return stakePoolUserInfo(ns, user)
}
// loadManager returns a new stake manager that results from loading it from
// the passed opened database. The public passphrase is required to decrypt the
// public keys.
func (s *StakeStore) loadOwnedSStxs(ns walletdb.ReadBucket) error {
// Regenerate the list of tickets.
// Perform all database lookups in a read-only view.
ticketList := make(map[chainhash.Hash]struct{})
// Open the sstx records database.
bucket := ns.NestedReadBucket(sstxRecordsBucketName)
// Store each key sequentially.
err := bucket.ForEach(func(k []byte, v []byte) error {
var errNewHash error
var hash *chainhash.Hash
hash, errNewHash = chainhash.NewHash(k)
if errNewHash != nil {
return errNewHash
}
ticketList[*hash] = struct{}{}
return nil
})
if err != nil {
return err
}
s.ownedSStxs = ticketList
return nil
}
// SetChainSvr is used to set the chainSvr to a given pointer. Should
// be called after chainSvr is initialized in wallet.
func (s *StakeStore) SetChainSvr(chainSvr *walletchain.RPCClient) {
s.chainSvr = chainSvr
}
// newStakeStore initializes a new stake store with the given parameters.
func newStakeStore(params *chaincfg.Params, manager *Manager) *StakeStore {
return &StakeStore{
Params: params,
Manager: manager,
chainSvr: nil,
ownedSStxs: make(map[chainhash.Hash]struct{}),
}
}
// openStakeStore loads an existing stake manager from the given namespace,
// waddrmgr, and network parameters.
//
// A ManagerError with an error code of ErrNoExist will be returned if the
// passed manager does not exist in the specified namespace.
func openStakeStore(ns walletdb.ReadBucket, manager *Manager, params *chaincfg.Params) (*StakeStore, error) {
// Return an error if the manager has NOT already been created in the
// given database namespace.
exists := stakeStoreExists(ns)
if !exists {
str := "the specified stake store/manager does not exist in db"
return nil, stakeStoreError(apperrors.ErrNoExist, str, nil)
}
ss := newStakeStore(params, manager)
err := ss.loadOwnedSStxs(ns)
if err != nil {
return nil, err
}
return ss, nil
}