forked from decred/dcrd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ticketlookup.go
646 lines (555 loc) · 19.8 KB
/
ticketlookup.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
641
642
643
644
645
646
// Copyright (c) 2013-2014 Conformal Systems LLC.
// Copyright (c) 2015 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package blockchain
import (
"errors"
"fmt"
"sort"
"github.com/decred/dcrd/blockchain/stake"
"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrutil"
)
// TicketStatus is used to indicate the state of a ticket in the ticket store and
// the ticket database. Non-existing is included because it's possible to have
// a ticket not exist in a sidechain that exists in the mainchain (and thus exists
// in the ticket database), and so you need to indicate this in the ticket store.
// It could also point to a ticket that's been missed and eliminated from the
// ticket db by SSRtx.
type TicketStatus int
// Possible values for TicketStatus
const (
TiNonexisting = iota
TiSpent
TiAvailable
TiMissed
TiRevoked
TiError
)
// TicketPatchData contains contextual information about tickets, namely their
// ticket data and whether or not they are spent.
type TicketPatchData struct {
td *stake.TicketData
ts TicketStatus
err error
}
// NewTicketPatchData creates a new TicketPatchData struct.
func NewTicketPatchData(td *stake.TicketData,
ts TicketStatus,
err error) *TicketPatchData {
return &TicketPatchData{td, ts, err}
}
// TicketStore is used to store a patch of the ticket db for use in validating the
// block header and subsequently the block reward. It allows you to observe the
// ticket db from the point-of-view of different points in the chain.
// TicketStore is basically like an extremely inefficient version of the ticket
// database that isn't designed to be able to be easily rolled back, which is fine
// because we're only going to use it in ephermal cases.
type TicketStore map[chainhash.Hash]*TicketPatchData
// GenerateLiveTicketBucket takes ticket patch data and a bucket number as input,
// then recreates a ticket bucket from the patch and the current database state.
func (b *BlockChain) GenerateLiveTicketBucket(ticketStore TicketStore,
tpdBucketMap map[uint8][]*TicketPatchData, bucket uint8) (stake.SStxMemMap,
error) {
bucketTickets := make(stake.SStxMemMap)
// Check the ticketstore for live tickets and add these to the bucket if
// their ticket number matches.
for _, tpd := range tpdBucketMap[bucket] {
if tpd.ts == TiAvailable {
bucketTickets[tpd.td.SStxHash] = tpd.td
}
}
// Check the ticket database for live tickets; prune live tickets that
// have been spent/missed/were otherwise accounted for.
liveTicketsFromDb, err := b.tmdb.DumpLiveTickets(bucket)
if err != nil {
return nil, err
}
for hash, td := range liveTicketsFromDb {
if _, exists := ticketStore[hash]; exists {
continue
}
bucketTickets[hash] = td
}
return bucketTickets, nil
}
// GenerateMissedTickets takes ticket patch data as input, then recreates the
// missed tickets bucket from the patch and the current database state.
func (b *BlockChain) GenerateMissedTickets(tixStore TicketStore) (stake.SStxMemMap,
error) {
missedTickets := make(stake.SStxMemMap)
// Check the ticketstore for live tickets and add these to the bucket if
// their ticket number matches.
for hash, tpd := range tixStore {
if tpd.ts == TiMissed {
missedTickets[hash] = tpd.td
}
}
// Check the ticket database for live tickets; prune live tickets that
// have been spent/missed/were otherwise accounted for.
missedTicketsFromDb, err := b.tmdb.DumpMissedTickets()
if err != nil {
return nil, err
}
for hash, td := range missedTicketsFromDb {
if _, exists := tixStore[hash]; exists {
continue
}
missedTickets[hash] = td
}
return missedTickets, nil
}
// connectTickets updates the passed map by removing removing any tickets
// from the ticket pool that have been considered spent or missed in this block
// according to the block header. Then, it connects all the newly mature tickets
// to the passed map.
func (b *BlockChain) connectTickets(tixStore TicketStore,
node *blockNode,
block *dcrutil.Block) error {
if tixStore == nil {
return fmt.Errorf("nil ticket store")
}
// Nothing to do if tickets haven't yet possibly matured.
height := node.height
if height < b.chainParams.StakeEnabledHeight {
return nil
}
parentBlock, err := b.GetBlockFromHash(node.parentHash)
if err != nil {
return err
}
revocations := node.header.Revocations
tM := int64(b.chainParams.TicketMaturity)
// Skip a number of validation steps before we requiring chain
// voting.
if node.height >= b.chainParams.StakeValidationHeight {
regularTxTreeValid := dcrutil.IsFlagSet16(node.header.VoteBits,
dcrutil.BlockValid)
thisNodeStakeViewpoint := ViewpointPrevInvalidStake
if regularTxTreeValid {
thisNodeStakeViewpoint = ViewpointPrevValidStake
}
// We need the missed tickets bucket from the original perspective of
// the node.
missedTickets, err := b.GenerateMissedTickets(tixStore)
if err != nil {
return err
}
// TxStore at blockchain HEAD + TxTreeRegular of prevBlock (if
// validated) for this node.
txInputStoreStake, err := b.fetchInputTransactions(node, block,
thisNodeStakeViewpoint)
if err != nil {
errStr := fmt.Sprintf("fetchInputTransactions failed for incoming "+
"node %v; error given: %v", node.hash, err)
return errors.New(errStr)
}
// PART 1: Spend/miss winner tickets
// Iterate through all the SSGen (vote) tx in the block and add them to
// a map of tickets that were actually used.
spentTicketsFromBlock := make(map[chainhash.Hash]bool)
numberOfSSgen := 0
for _, staketx := range block.STransactions() {
if is, _ := stake.IsSSGen(staketx); is {
msgTx := staketx.MsgTx()
sstxIn := msgTx.TxIn[1] // sstx input
sstxHash := sstxIn.PreviousOutPoint.Hash
originTx, exists := txInputStoreStake[sstxHash]
if !exists {
str := fmt.Sprintf("unable to find input transaction "+
"%v for transaction %v", sstxHash, staketx.Sha())
return ruleError(ErrMissingTx, str)
}
sstxHeight := originTx.BlockHeight
// Check maturity of ticket; we can only spend the ticket after it
// hits maturity at height + tM + 1.
if (height - sstxHeight) < (tM + 1) {
blockSha := block.Sha()
errStr := fmt.Sprintf("Error: A ticket spend as an SSGen in "+
"block height %v was immature! Block sha %v",
height,
blockSha)
return errors.New(errStr)
}
// Fill out the ticket data.
spentTicketsFromBlock[sstxHash] = true
numberOfSSgen++
}
}
// Obtain the TicketsPerBlock many tickets that were selected this round,
// then check these against the tickets that were actually used to make
// sure that any SSGen actually match the selected tickets. Commit the
// spent or missed tickets to the ticket store after.
spentAndMissedTickets := make(TicketStore)
tixSpent := 0
tixMissed := 0
// Sort the entire list of tickets lexicographically by sorting
// each bucket and then appending it to the list. Start by generating
// a prefix matched map of tickets to speed up the lookup.
tpdBucketMap := make(map[uint8][]*TicketPatchData)
for _, tpd := range tixStore {
// Bucket does not exist.
if _, ok := tpdBucketMap[tpd.td.Prefix]; !ok {
tpdBucketMap[tpd.td.Prefix] = make([]*TicketPatchData, 1)
tpdBucketMap[tpd.td.Prefix][0] = tpd
} else {
// Bucket exists.
data := tpdBucketMap[tpd.td.Prefix]
tpdBucketMap[tpd.td.Prefix] = append(data, tpd)
}
}
totalTickets := 0
var sortedSlice []*stake.TicketData
for i := 0; i < stake.BucketsSize; i++ {
ltb, err := b.GenerateLiveTicketBucket(tixStore, tpdBucketMap,
uint8(i))
if err != nil {
h := node.hash
str := fmt.Sprintf("Failed to generate live ticket bucket "+
"%v for node %v, height %v! Error: %v",
i,
h,
node.height,
err.Error())
return fmt.Errorf(str)
}
mapLen := len(ltb)
tempTdSlice := stake.NewTicketDataSlice(mapLen)
itr := 0 // Iterator
for _, td := range ltb {
tempTdSlice[itr] = td
itr++
totalTickets++
}
sort.Sort(tempTdSlice)
sortedSlice = append(sortedSlice, tempTdSlice...)
}
// Use the parent block's header to seed a PRNG that picks the
// lottery winners.
ticketsPerBlock := int(b.chainParams.TicketsPerBlock)
pbhB, err := parentBlock.MsgBlock().Header.Bytes()
if err != nil {
return err
}
prng := stake.NewHash256PRNG(pbhB)
ts, err := stake.FindTicketIdxs(int64(totalTickets), ticketsPerBlock, prng)
if err != nil {
return err
}
ticketsToSpendOrMiss := make([]*stake.TicketData, ticketsPerBlock,
ticketsPerBlock)
for i, idx := range ts {
ticketsToSpendOrMiss[i] = sortedSlice[idx]
}
// Spend or miss these tickets by checking for their existence in the
// passed spentTicketsFromBlock map.
for _, ticket := range ticketsToSpendOrMiss {
// Move the ticket from active tickets map into the used tickets
// map if the ticket was spent.
wasSpent, _ := spentTicketsFromBlock[ticket.SStxHash]
if wasSpent {
tpd := NewTicketPatchData(ticket, TiSpent, nil)
spentAndMissedTickets[ticket.SStxHash] = tpd
tixSpent++
} else { // Ticket missed being spent and --> false or nil
tpd := NewTicketPatchData(ticket, TiMissed, nil)
spentAndMissedTickets[ticket.SStxHash] = tpd
tixMissed++
}
}
// This error is thrown if for some reason there exists an SSGen in
// the block that doesn't spend a ticket from the eligible list of
// tickets, thus making it invalid.
if tixSpent != numberOfSSgen {
errStr := fmt.Sprintf("an invalid number %v "+
"tickets was spent, but %v many tickets should "+
"have been spent!", tixSpent, numberOfSSgen)
return errors.New(errStr)
}
if tixMissed != (ticketsPerBlock - numberOfSSgen) {
errStr := fmt.Sprintf("an invalid number %v "+
"tickets was missed, but %v many tickets should "+
"have been missed!", tixMissed,
ticketsPerBlock-numberOfSSgen)
return errors.New(errStr)
}
if (tixSpent + tixMissed) != int(b.chainParams.TicketsPerBlock) {
errStr := fmt.Sprintf("an invalid number %v "+
"tickets was spent and missed, but TicketsPerBlock %v many "+
"tickets should have been spent!", tixSpent,
ticketsPerBlock)
return errors.New(errStr)
}
// Calculate all the tickets expiring this block and mark them as missed.
tpdBucketMap = make(map[uint8][]*TicketPatchData)
for _, tpd := range tixStore {
// Bucket does not exist.
if _, ok := tpdBucketMap[tpd.td.Prefix]; !ok {
tpdBucketMap[tpd.td.Prefix] = make([]*TicketPatchData, 1)
tpdBucketMap[tpd.td.Prefix][0] = tpd
} else {
// Bucket exists.
data := tpdBucketMap[tpd.td.Prefix]
tpdBucketMap[tpd.td.Prefix] = append(data, tpd)
}
}
toExpireHeight := node.height - int64(b.chainParams.TicketExpiry)
if !(toExpireHeight < int64(b.chainParams.StakeEnabledHeight)) {
for i := 0; i < stake.BucketsSize; i++ {
// Generate the live ticket bucket.
ltb, err := b.GenerateLiveTicketBucket(tixStore,
tpdBucketMap, uint8(i))
if err != nil {
return err
}
for _, ticket := range ltb {
if ticket.BlockHeight == toExpireHeight {
tpd := NewTicketPatchData(ticket, TiMissed, nil)
spentAndMissedTickets[ticket.SStxHash] = tpd
}
}
}
}
// Merge the ticket store patch containing the spent and missed tickets
// with the ticket store.
for hash, tpd := range spentAndMissedTickets {
tixStore[hash] = tpd
}
// At this point our tixStore now contains all the spent and missed tx
// as per this block.
// PART 2: Remove tickets that were missed and are now revoked.
// Iterate through all the SSGen (vote) tx in the block and add them to
// a map of tickets that were actually used.
revocationsFromBlock := make(map[chainhash.Hash]struct{})
numberOfSSRtx := 0
for _, staketx := range block.STransactions() {
if is, _ := stake.IsSSRtx(staketx); is {
msgTx := staketx.MsgTx()
sstxIn := msgTx.TxIn[0] // sstx input
sstxHash := sstxIn.PreviousOutPoint.Hash
// Fill out the ticket data.
revocationsFromBlock[sstxHash] = struct{}{}
numberOfSSRtx++
}
}
if numberOfSSRtx != int(revocations) {
errStr := fmt.Sprintf("an invalid revocations %v was calculated "+
"the block header indicates %v instead", numberOfSSRtx,
revocations)
return errors.New(errStr)
}
// Lookup the missed ticket. If we find it in the patch data,
// modify the patch data so that it doesn't exist.
// Otherwise, just modify load the missed ticket data from
// the ticket db and create patch data based on that.
for hash := range revocationsFromBlock {
ticketWasMissed := false
if td, is := missedTickets[hash]; is {
maturedHeight := td.BlockHeight
// Check maturity of ticket; we can only spend the ticket after it
// hits maturity at height + tM + 2.
if height < maturedHeight+2 {
blockSha := block.Sha()
errStr := fmt.Sprintf("Error: A ticket spend as an "+
"SSRtx in block height %v was immature! Block sha %v",
height,
blockSha)
return errors.New(errStr)
}
ticketWasMissed = true
}
if !ticketWasMissed {
errStr := fmt.Sprintf("SSRtx spent missed sstx %v, "+
"but that missed sstx could not be found!",
hash)
return errors.New(errStr)
}
}
}
// PART 3: Add newly maturing tickets
// This is the only chunk we need to do for blocks appearing before
// stake validation height.
// Calculate block number for where new tickets are maturing from and retrieve
// this block from db.
// Get the block that is maturing.
matureNode, err := b.getNodeAtHeightFromTopNode(node, tM)
if err != nil {
return err
}
matureBlock, errBlock := b.getBlockFromHash(matureNode.hash)
if errBlock != nil {
return errBlock
}
// Maturing tickets are from the maturingBlock; fill out the ticket patch data
// and then push them to the tixStore.
for _, stx := range matureBlock.STransactions() {
if is, _ := stake.IsSStx(stx); is {
// Calculate the prefix for pre-sort.
sstxHash := *stx.Sha()
prefix := uint8(sstxHash[0])
// Fill out the ticket data.
td := stake.NewTicketData(sstxHash,
prefix,
chainhash.Hash{},
height,
false, // not missed
false) // not expired
tpd := NewTicketPatchData(td,
TiAvailable,
nil)
tixStore[*stx.Sha()] = tpd
}
}
return nil
}
// disconnectTransactions updates the passed map by undoing transaction and
// spend information for all transactions in the passed block. Only
// transactions in the passed map are updated.
// This function should only ever have to disconnect transactions from the main
// chain, so most of the calls are directly the the tmdb which contains all this
// data in an organized bucket.
func (b *BlockChain) disconnectTickets(tixStore TicketStore,
node *blockNode,
block *dcrutil.Block) error {
tM := int64(b.chainParams.TicketMaturity)
height := node.height
// Nothing to do if tickets haven't yet possibly matured.
if height < b.chainParams.StakeEnabledHeight {
return nil
}
// PART 1: Remove newly maturing tickets
// Calculate block number for where new tickets matured from and retrieve
// this block from db.
matureNode, err := b.getNodeAtHeightFromTopNode(node, tM)
if err != nil {
return err
}
matureBlock, errBlock := b.getBlockFromHash(matureNode.hash)
if errBlock != nil {
return errBlock
}
// Store pointers to empty ticket data in the ticket store and mark them as
// non-existing.
for _, stx := range matureBlock.STransactions() {
if is, _ := stake.IsSStx(stx); is {
// Leave this pointing to nothing, as the ticket technically does not
// exist. It may exist when we add blocks later, but we can fill it
// out then.
td := &stake.TicketData{}
tpd := NewTicketPatchData(td,
TiNonexisting,
nil)
tixStore[*stx.Sha()] = tpd
}
}
// PART 2: Unrevoke any SSRtx in this block and restore them as
// missed tickets.
for _, stx := range block.STransactions() {
if is, _ := stake.IsSSRtx(stx); is {
// Move the revoked ticket to missed tickets. Obtain the
// revoked ticket data from the ticket database.
msgTx := stx.MsgTx()
sstxIn := msgTx.TxIn[0] // sstx input
sstxHash := sstxIn.PreviousOutPoint.Hash
td := b.tmdb.GetRevokedTicket(sstxHash)
if td == nil {
return fmt.Errorf("Failed to find revoked ticket %v in tmdb",
sstxHash)
}
tpd := NewTicketPatchData(td,
TiMissed,
nil)
tixStore[sstxHash] = tpd
}
}
// PART 3: Unspend or unmiss all tickets spent/missed/expired at this block.
// Query the stake db for used tickets (spentTicketDb), which includes all of
// the spent and missed tickets.
spentTickets, errDump := b.tmdb.DumpSpentTickets(height)
if errDump != nil {
return errDump
}
// Move all of these tickets into the ticket store as available tickets.
for hash, td := range spentTickets {
tpd := NewTicketPatchData(td,
TiAvailable,
nil)
tixStore[hash] = tpd
}
return nil
}
// fetchTicketStore fetches ticket data from the point of view of the given node.
// For example, a given node might be down a side chain where a ticket hasn't been
// spent from its point of view even though it might have been spent in the main
// chain (or another side chain). Another scenario is where a ticket exists from
// the point of view of the main chain, but doesn't exist in a side chain that
// branches before the block that contains the ticket on the main chain.
func (b *BlockChain) fetchTicketStore(node *blockNode) (TicketStore, error) {
tixStore := make(TicketStore)
// Get the previous block node. This function is used over simply
// accessing node.parent directly as it will dynamically create previous
// block nodes as needed. This helps allow only the pieces of the chain
// that are needed to remain in memory.
prevNode, err := b.getPrevNodeFromNode(node)
if err != nil {
return nil, err
}
// If we haven't selected a best chain yet or we are extending the main
// (best) chain with a new block, just use the ticket database we already
// have.
if b.bestChain == nil || (prevNode != nil &&
prevNode.hash.IsEqual(b.bestChain.hash)) {
return nil, nil
}
// We don't care about nodes before stake enabled height.
if node.height < b.chainParams.StakeEnabledHeight {
return nil, nil
}
// The requested node is either on a side chain or is a node on the main
// chain before the end of it. In either case, we need to undo the
// transactions and spend information for the blocks which would be
// disconnected during a reorganize to the point of view of the
// node just before the requested node.
detachNodes, attachNodes, err := b.getReorganizeNodes(prevNode)
if err != nil {
return nil, err
}
for e := detachNodes.Front(); e != nil; e = e.Next() {
n := e.Value.(*blockNode)
block, err := b.db.FetchBlockBySha(n.hash)
if err != nil {
return nil, err
}
err = b.disconnectTickets(tixStore, n, block)
if err != nil {
return nil, err
}
}
// The ticket store is now accurate to either the node where the
// requested node forks off the main chain (in the case where the
// requested node is on a side chain), or the requested node itself if
// the requested node is an old node on the main chain. Entries in the
// attachNodes list indicate the requested node is on a side chain, so
// if there are no nodes to attach, we're done.
if attachNodes.Len() == 0 {
return tixStore, nil
}
// The requested node is on a side chain, so we need to apply the
// transactions and spend information from each of the nodes to attach.
for e := attachNodes.Front(); e != nil; e = e.Next() {
n := e.Value.(*blockNode)
block, exists := b.blockCache[*n.hash]
if !exists {
return nil, fmt.Errorf("unable to find block %v in "+
"side chain cache for ticket db patch construction",
n.hash)
}
// The number of blocks below this block but above the root of the fork
err = b.connectTickets(tixStore, n, block)
if err != nil {
return nil, err
}
}
return tixStore, nil
}