/
chainntfns.go
1051 lines (937 loc) · 32 KB
/
chainntfns.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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2015-2021 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"
"math/big"
"time"
"decred.org/dcrwallet/v2/deployments"
"decred.org/dcrwallet/v2/errors"
"decred.org/dcrwallet/v2/wallet/txrules"
"decred.org/dcrwallet/v2/wallet/udb"
"decred.org/dcrwallet/v2/wallet/walletdb"
"github.com/decred/dcrd/blockchain/stake/v4"
blockchain "github.com/decred/dcrd/blockchain/standalone/v2"
"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrd/dcrutil/v4"
gcs2 "github.com/decred/dcrd/gcs/v3"
"github.com/decred/dcrd/txscript/v4"
"github.com/decred/dcrd/txscript/v4/stdaddr"
"github.com/decred/dcrd/txscript/v4/stdscript"
"github.com/decred/dcrd/wire"
)
func (w *Wallet) extendMainChain(ctx context.Context, op errors.Op, dbtx walletdb.ReadWriteTx,
header *wire.BlockHeader, f *gcs2.FilterV2, transactions []*wire.MsgTx) ([]wire.OutPoint, error) {
txmgrNs := dbtx.ReadWriteBucket(wtxmgrNamespaceKey)
blockHash := header.BlockHash()
// Enforce checkpoints
height := int32(header.Height)
ckpt := CheckpointHash(w.chainParams.Net, height)
if ckpt != nil && blockHash != *ckpt {
err := errors.Errorf("block hash %v does not satisify "+
"checkpoint hash %v for height %v", blockHash,
ckpt, height)
return nil, errors.E(errors.Consensus, err)
}
// Propagate the error unless this block is already included in the main
// chain.
err := w.txStore.ExtendMainChain(txmgrNs, header, f)
if err != nil && !errors.Is(err, errors.Exist) {
return nil, errors.E(op, err)
}
// Notify interested clients of the connected block.
w.NtfnServer.notifyAttachedBlock(dbtx, header, &blockHash)
blockMeta, err := w.txStore.GetBlockMetaForHash(txmgrNs, &blockHash)
if err != nil {
return nil, errors.E(op, err)
}
var watch []wire.OutPoint
for _, tx := range transactions {
// In manual ticket mode, tickets are only ever added to the
// wallet using AddTransaction. Skip over any relevant tickets
// seen in this block unless they already exist in the wallet.
if w.manualTickets && stake.IsSStx(tx) {
txHash := tx.TxHash()
if !w.txStore.ExistsTx(txmgrNs, &txHash) {
continue
}
}
rec, err := udb.NewTxRecordFromMsgTx(tx, time.Now())
if err != nil {
return nil, errors.E(op, err)
}
ops, err := w.processTransactionRecord(ctx, dbtx, rec, header, &blockMeta)
if err != nil {
return nil, errors.E(op, err)
}
watch = append(watch, ops...)
}
return watch, nil
}
// ChainSwitch updates the wallet's main chain, either by extending the chain
// with new blocks, or switching to a better sidechain. A sidechain for removed
// blocks (if any) is returned. If relevantTxs is non-nil, the block marker for
// the latest block with processed transactions is updated for the new tip
// block.
func (w *Wallet) ChainSwitch(ctx context.Context, forest *SidechainForest, chain []*BlockNode,
relevantTxs map[chainhash.Hash][]*wire.MsgTx) ([]*BlockNode, error) {
const op errors.Op = "wallet.ChainSwitch"
if len(chain) == 0 {
return nil, errors.E(op, errors.Invalid, "zero-length chain")
}
chainTipChanges := &MainTipChangedNotification{
AttachedBlocks: make([]*chainhash.Hash, 0, len(chain)),
DetachedBlocks: nil,
NewHeight: int32(chain[len(chain)-1].Header.Height),
}
sideChainForkHeight := int32(chain[0].Header.Height)
var prevChain []*BlockNode
newWork := chain[len(chain)-1].workSum
oldWork := new(big.Int)
w.lockedOutpointMu.Lock()
var watchOutPoints []wire.OutPoint
err := walletdb.Update(ctx, w.db, func(dbtx walletdb.ReadWriteTx) error {
txmgrNs := dbtx.ReadWriteBucket(wtxmgrNamespaceKey)
tipHash, tipHeight := w.txStore.MainChainTip(dbtx)
if tipHash == *chain[len(chain)-1].Hash {
return nil
}
if sideChainForkHeight <= tipHeight {
chainTipChanges.DetachedBlocks = make([]*chainhash.Hash, tipHeight-sideChainForkHeight+1)
prevChain = make([]*BlockNode, tipHeight-sideChainForkHeight+1)
for i := tipHeight; i >= sideChainForkHeight; i-- {
hash, err := w.txStore.GetMainChainBlockHashForHeight(txmgrNs, i)
if err != nil {
return err
}
header, err := w.txStore.GetBlockHeader(dbtx, &hash)
if err != nil {
return err
}
_, filter, err := w.txStore.CFilterV2(dbtx, &hash)
if err != nil {
return err
}
// DetachedBlocks and prevChain are sorted in order of increasing heights.
chainTipChanges.DetachedBlocks[i-sideChainForkHeight] = &hash
prevChain[i-sideChainForkHeight] = NewBlockNode(header, &hash, filter)
// For transaction notifications, the blocks are notified in reverse
// height order.
w.NtfnServer.notifyDetachedBlock(header)
oldWork.Add(oldWork, blockchain.CalcWork(header.Bits))
}
if newWork.Cmp(oldWork) != 1 {
return errors.Errorf("failed reorganize: sidechain ending at block %v has less total work "+
"than the main chain tip block %v", chain[len(chain)-1].Hash, &tipHash)
}
// Remove blocks on the current main chain that are at or above the
// height of the block that begins the side chain.
err := w.txStore.Rollback(dbtx, sideChainForkHeight)
if err != nil {
return err
}
}
for _, n := range chain {
if voteVersion(w.chainParams) < n.Header.StakeVersion {
log.Warnf("Old vote version detected (v%v), please update your "+
"wallet to the latest version.", voteVersion(w.chainParams))
}
watch, err := w.extendMainChain(ctx, op, dbtx, n.Header, n.FilterV2, relevantTxs[*n.Hash])
if err != nil {
return err
}
watchOutPoints = append(watchOutPoints, watch...)
// Add the block hash to the notification.
chainTipChanges.AttachedBlocks = append(chainTipChanges.AttachedBlocks, n.Hash)
}
if relevantTxs != nil {
// To avoid skipped blocks, the marker is not advanced if there is a
// gap between the existing rescan point (main chain fork point of
// the current marker) and the first block attached in this chain
// switch.
r, err := w.rescanPoint(dbtx)
if err != nil {
return err
}
rHeader, err := w.txStore.GetBlockHeader(dbtx, r)
if err != nil {
return err
}
if !(rHeader.Height+1 < chain[0].Header.Height) {
marker := chain[len(chain)-1].Hash
log.Debugf("Updating processed txs block marker to %v", marker)
err := w.txStore.UpdateProcessedTxsBlockMarker(dbtx, marker)
if err != nil {
return err
}
}
}
// Prune unmined transactions that don't belong on the extended chain.
// An error here is not fatal and should just be logged.
//
// TODO: The stake difficulty passed here is not correct. This must be
// the difficulty of the next block, not the tip block.
tip := chain[len(chain)-1]
hashes, err := w.txStore.PruneUnmined(dbtx, tip.Header.SBits)
if err != nil {
log.Errorf("Failed to prune unmined transactions when "+
"connecting block height %v: %v", tip.Header.Height, err)
}
for _, hash := range hashes {
w.NtfnServer.notifyRemovedTransaction(*hash)
}
return nil
})
w.lockedOutpointMu.Unlock()
if err != nil {
return nil, errors.E(op, err)
}
if len(chainTipChanges.AttachedBlocks) != 0 {
w.recentlyPublishedMu.Lock()
for _, node := range chain {
for _, tx := range relevantTxs[*node.Hash] {
txHash := tx.TxHash()
delete(w.recentlyPublished, txHash)
}
}
w.recentlyPublishedMu.Unlock()
}
if n, err := w.NetworkBackend(); err == nil {
_, err = w.watchHDAddrs(ctx, false, n)
if err != nil {
return nil, errors.E(op, err)
}
if len(watchOutPoints) > 0 {
err = n.LoadTxFilter(ctx, false, nil, watchOutPoints)
if err != nil {
log.Errorf("Failed to watch outpoints: %v", err)
}
}
}
forest.PruneTree(chain[0].Hash)
forest.Prune(int32(chain[len(chain)-1].Header.Height), w.chainParams)
w.NtfnServer.notifyMainChainTipChanged(chainTipChanges)
w.NtfnServer.sendAttachedBlockNotification(ctx)
return prevChain, nil
}
// evaluateStakePoolTicket evaluates a stake pool ticket to see if it's
// acceptable to the stake pool. The ticket must pay out to the stake
// pool cold wallet, and must have a sufficient fee.
func (w *Wallet) evaluateStakePoolTicket(rec *udb.TxRecord, blockHeight int32, poolUser stdaddr.Address) bool {
tx := rec.MsgTx
// Check the first commitment output (txOuts[1])
// and ensure that the address found there exists
// in the list of approved addresses. Also ensure
// that the fee exists and is of the amount
// requested by the pool.
commitmentOut := tx.TxOut[1]
commitAddr, err := stake.AddrFromSStxPkScrCommitment(
commitmentOut.PkScript, w.chainParams)
if err != nil {
log.Warnf("Cannot parse commitment address from ticket %v: %v",
&rec.Hash, err)
return false
}
// Extract the fee from the ticket.
in := dcrutil.Amount(0)
for i := range tx.TxOut {
if i%2 != 0 {
commitAmt, err := stake.AmountFromSStxPkScrCommitment(
tx.TxOut[i].PkScript)
if err != nil {
log.Warnf("Cannot parse commitment amount for output %i from ticket %v: %v",
i, &rec.Hash, err)
return false
}
in += commitAmt
}
}
out := dcrutil.Amount(0)
for i := range tx.TxOut {
out += dcrutil.Amount(tx.TxOut[i].Value)
}
fees := in - out
_, exists := w.stakePoolColdAddrs[commitAddr.String()]
if exists {
commitAmt, err := stake.AmountFromSStxPkScrCommitment(
commitmentOut.PkScript)
if err != nil {
log.Warnf("Cannot parse commitment amount from ticket %v: %v", &rec.Hash, err)
return false
}
// Calculate the fee required based on the current
// height and the required amount from the pool.
feeNeeded := txrules.StakePoolTicketFee(dcrutil.Amount(
tx.TxOut[0].Value), fees, blockHeight, w.poolFees,
w.chainParams, false)
if commitAmt < feeNeeded {
log.Warnf("User %s submitted ticket %v which "+
"has less fees than are required to use this "+
"stake pool and is being skipped (required: %v"+
", found %v)", commitAddr,
tx.TxHash(), feeNeeded, commitAmt)
// Reject the entire transaction if it didn't
// pay the pool server fees.
return false
}
} else {
log.Warnf("Unknown pool commitment address %s for ticket %v",
commitAddr, tx.TxHash())
return false
}
log.Debugf("Accepted valid stake pool ticket %v committing %v in fees",
tx.TxHash(), tx.TxOut[0].Value)
return true
}
// AddTransaction stores tx, marking it as mined in the block described by
// blockHash, or recording it to the wallet's mempool when nil.
//
// This method will always add ticket transactions to the wallet, even when
// configured in manual ticket mode. It is up to network syncers to avoid
// calling this method on unmined tickets.
func (w *Wallet) AddTransaction(ctx context.Context, tx *wire.MsgTx, blockHash *chainhash.Hash) error {
const op errors.Op = "wallet.AddTransaction"
w.recentlyPublishedMu.Lock()
_, recent := w.recentlyPublished[tx.TxHash()]
w.recentlyPublishedMu.Unlock()
if recent {
return nil
}
// Prevent recording unmined tspends since they need to go through
// voting for potentially a long time.
if isTreasurySpend(tx) && blockHash == nil {
log.Debugf("Ignoring unmined TSPend %s", tx.TxHash())
return nil
}
w.lockedOutpointMu.Lock()
var watchOutPoints []wire.OutPoint
err := walletdb.Update(ctx, w.db, func(dbtx walletdb.ReadWriteTx) error {
txmgrNs := dbtx.ReadBucket(wtxmgrNamespaceKey)
rec, err := udb.NewTxRecordFromMsgTx(tx, time.Now())
if err != nil {
return err
}
// Prevent orphan votes from entering the wallet's unmined transaction
// set.
if isVote(&rec.MsgTx) && blockHash == nil {
votedBlock, _ := stake.SSGenBlockVotedOn(&rec.MsgTx)
tipBlock, _ := w.txStore.MainChainTip(dbtx)
if votedBlock != tipBlock {
log.Debugf("Rejected unmined orphan vote %v which votes on block %v",
&rec.Hash, &votedBlock)
return nil
}
}
var header *wire.BlockHeader
var meta *udb.BlockMeta
switch {
case blockHash != nil:
inChain, _ := w.txStore.BlockInMainChain(dbtx, blockHash)
if !inChain {
break
}
header, err = w.txStore.GetBlockHeader(dbtx, blockHash)
if err != nil {
return err
}
meta = new(udb.BlockMeta)
*meta, err = w.txStore.GetBlockMetaForHash(txmgrNs, blockHash)
if err != nil {
return err
}
}
watchOutPoints, err = w.processTransactionRecord(ctx, dbtx, rec, header, meta)
return err
})
w.lockedOutpointMu.Unlock()
if err != nil {
return errors.E(op, err)
}
if n, err := w.NetworkBackend(); err == nil && len(watchOutPoints) > 0 {
_, err := w.watchHDAddrs(ctx, false, n)
if err != nil {
return errors.E(op, err)
}
if len(watchOutPoints) > 0 {
err = n.LoadTxFilter(ctx, false, nil, watchOutPoints)
if err != nil {
log.Errorf("Failed to watch outpoints: %v", err)
}
}
}
return nil
}
func (w *Wallet) processTransactionRecord(ctx context.Context, dbtx walletdb.ReadWriteTx, rec *udb.TxRecord,
header *wire.BlockHeader, blockMeta *udb.BlockMeta) (watchOutPoints []wire.OutPoint, err error) {
const op errors.Op = "wallet.processTransactionRecord"
addrmgrNs := dbtx.ReadWriteBucket(waddrmgrNamespaceKey)
stakemgrNs := dbtx.ReadWriteBucket(wstakemgrNamespaceKey)
txmgrNs := dbtx.ReadWriteBucket(wtxmgrNamespaceKey)
height := int32(-1)
if header != nil {
height = int32(header.Height)
}
// At the moment all notified transactions are assumed to actually be
// relevant. This assumption will not hold true when SPV support is
// added, but until then, simply insert the transaction because there
// should either be one or more relevant inputs or outputs.
if header == nil {
err = w.txStore.InsertMemPoolTx(dbtx, rec)
if errors.Is(err, errors.Exist) {
log.Warnf("Refusing to add unmined transaction %v since same "+
"transaction already exists mined", &rec.Hash)
return nil, nil
}
} else {
err = w.txStore.InsertMinedTx(dbtx, rec, &blockMeta.Hash)
}
if err != nil {
return nil, errors.E(op, err)
}
// Handle incoming SStx; store them in the stake manager if we own
// the OP_SSTX tagged out, except if we're operating as a stake pool
// server. In that case, additionally consider the first commitment
// output as well.
if w.stakePoolEnabled && header != nil && rec.TxType == stake.TxTypeSStx {
// Errors don't matter here. If addrs is nil, the range below
// does nothing.
txOut := rec.MsgTx.TxOut[0]
_, addrs := stdscript.ExtractAddrs(txOut.Version, txOut.PkScript, w.chainParams)
insert := false
for _, addr := range addrs {
switch addr := addr.(type) {
case stdaddr.Hash160er:
if !w.manager.ExistsHash160(addrmgrNs, addr.Hash160()[:]) {
continue
}
default:
continue
}
// We are operating as a stake pool. The below
// function will ONLY add the ticket into the
// stake pool if it has been found within a
// block.
if header == nil {
break
}
if w.evaluateStakePoolTicket(rec, height, addr) {
// Be sure to insert this into the user's stake
// pool entry into the stake manager.
poolTicket := &udb.PoolTicket{
Ticket: rec.Hash,
HeightTicket: uint32(height),
Status: udb.TSImmatureOrLive,
}
err := w.stakeMgr.UpdateStakePoolUserTickets(
stakemgrNs, addr, poolTicket)
if err != nil {
log.Warnf("Failed to insert stake pool "+
"user ticket: %v", err)
}
log.Debugf("Inserted stake pool ticket %v for user %v "+
"into the stake store database", &rec.Hash, addr)
insert = true
break
}
// At this point the ticket must be invalid, so insert it into the
// list of invalid user tickets.
err := w.stakeMgr.UpdateStakePoolUserInvalTickets(
stakemgrNs, addr, &rec.Hash)
if err != nil {
log.Warnf("Failed to update pool user %v with "+
"invalid ticket %v", addr, rec.Hash)
}
}
if insert {
err := w.stakeMgr.InsertSStx(stakemgrNs, dcrutil.NewTx(&rec.MsgTx))
if err != nil {
log.Errorf("Failed to insert SStx %v"+
"into the stake store.", &rec.Hash)
}
}
}
// Handle incoming mined votes (only in stakepool mode)
if w.stakePoolEnabled && rec.TxType == stake.TxTypeSSGen && header != nil {
ticketHash := &rec.MsgTx.TxIn[1].PreviousOutPoint.Hash
txInHeight := rec.MsgTx.TxIn[1].BlockHeight
poolTicket := &udb.PoolTicket{
Ticket: *ticketHash,
HeightTicket: txInHeight,
Status: udb.TSVoted,
SpentBy: rec.Hash,
HeightSpent: uint32(height),
}
poolUser, err := w.stakeMgr.SStxAddress(stakemgrNs, ticketHash)
if err != nil {
log.Warnf("Failed to fetch stake pool user for "+
"ticket %v (voted ticket): %v", ticketHash, err)
} else {
err = w.stakeMgr.UpdateStakePoolUserTickets(
stakemgrNs, poolUser, poolTicket)
if err != nil {
log.Warnf("Failed to update stake pool ticket for "+
"stake pool user %s after voting",
poolUser)
} else {
log.Debugf("Updated voted stake pool ticket %v "+
"for user %v into the stake store database ("+
"vote hash: %v)", ticketHash, poolUser, &rec.Hash)
}
}
}
// Handle incoming mined revocations (only in stakepool mode)
if w.stakePoolEnabled && rec.TxType == stake.TxTypeSSRtx && header != nil {
txInHash := &rec.MsgTx.TxIn[0].PreviousOutPoint.Hash
txInHeight := rec.MsgTx.TxIn[0].BlockHeight
poolTicket := &udb.PoolTicket{
Ticket: *txInHash,
HeightTicket: txInHeight,
Status: udb.TSMissed,
SpentBy: rec.Hash,
HeightSpent: uint32(height),
}
poolUser, err := w.stakeMgr.SStxAddress(stakemgrNs, txInHash)
if err != nil {
log.Warnf("failed to fetch stake pool user for "+
"ticket %v (missed ticket)", txInHash)
} else {
err = w.stakeMgr.UpdateStakePoolUserTickets(
stakemgrNs, poolUser, poolTicket)
if err != nil {
log.Warnf("failed to update stake pool ticket for "+
"stake pool user %s after revoking",
poolUser)
} else {
log.Debugf("Updated missed stake pool ticket %v "+
"for user %v into the stake store database ("+
"revocation hash: %v)", txInHash, poolUser, &rec.Hash)
}
}
}
// Skip unlocking outpoints if the transaction is a vote or revocation as the lock
// is not held.
skipOutpoints := rec.TxType == stake.TxTypeSSGen || rec.TxType == stake.TxTypeSSRtx
// Handle input scripts that contain P2PKs that we care about.
for i, input := range rec.MsgTx.TxIn {
if !skipOutpoints {
prev := input.PreviousOutPoint
delete(w.lockedOutpoints, outpoint{prev.Hash, prev.Index})
}
// TODO: the prevout's actual pkScript version is needed.
if stdscript.IsMultiSigSigScript(scriptVersionAssumed, input.SignatureScript) {
rs := stdscript.MultiSigRedeemScriptFromScriptSigV0(input.SignatureScript)
class, addrs := stdscript.ExtractAddrs(scriptVersionAssumed, rs, w.chainParams)
if class != stdscript.STMultiSig {
// This should never happen, but be paranoid.
continue
}
isRelevant := false
for _, addr := range addrs {
ma, err := w.manager.Address(addrmgrNs, addr)
if err != nil {
// Missing addresses are skipped. Other errors should be
// propagated.
if errors.Is(err, errors.NotExist) {
continue
}
return nil, errors.E(op, err)
}
isRelevant = true
err = w.markUsedAddress(op, dbtx, ma)
if err != nil {
return nil, err
}
log.Debugf("Marked address %v used", addr)
}
// Add the script to the script databases.
// TODO Markused script address? cj
if isRelevant {
n, _ := w.NetworkBackend()
addr, err := w.manager.ImportScript(addrmgrNs, rs)
switch {
case errors.Is(err, errors.Exist):
case err != nil:
return nil, errors.E(op, err)
case n != nil:
addrs := []stdaddr.Address{addr.Address()}
err := n.LoadTxFilter(ctx, false, addrs, nil)
if err != nil {
return nil, errors.E(op, err)
}
}
}
// If we're spending a multisig outpoint we know about,
// update the outpoint. Inefficient because you deserialize
// the entire multisig output info. Consider a specific
// exists function in udb. The error here is skipped
// because the absence of an multisignature output for
// some script can not always be considered an error. For
// example, the wallet might be rescanning as called from
// the above function and so does not have the output
// included yet.
mso, err := w.txStore.GetMultisigOutput(txmgrNs, &input.PreviousOutPoint)
if mso != nil && err == nil {
err = w.txStore.SpendMultisigOut(txmgrNs, &input.PreviousOutPoint,
rec.Hash, uint32(i))
if err != nil {
return nil, errors.E(op, err)
}
}
}
}
// Check every output to determine whether it is controlled by a
// wallet key. If so, mark the output as a credit and mark
// outpoints to watch.
for i, output := range rec.MsgTx.TxOut {
class, addrs := stdscript.ExtractAddrs(output.Version, output.PkScript, w.chainParams)
if class == stdscript.STNonStandard {
// Non-standard outputs are skipped.
continue
}
subClass, isStakeType := txrules.StakeSubScriptType(class)
if isStakeType {
class = subClass
}
isTicketCommit := rec.TxType == stake.TxTypeSStx && i%2 == 1
watchOutPoint := true
if isTicketCommit {
// For ticket commitments, decode the address stored in the pkscript
// and evaluate ownership of that.
addr, err := stake.AddrFromSStxPkScrCommitment(output.PkScript,
w.chainParams)
if err != nil {
log.Warnf("failed to decode ticket commitment script of %s:%d",
rec.Hash, i)
continue
}
addrs = []stdaddr.Address{addr}
watchOutPoint = false
} else if output.Value == 0 {
// The only case of outputs with 0 value that we need to handle are
// ticket commitments. All other outputs can be ignored.
continue
}
var tree int8
if isStakeType {
tree = 1
}
outpoint := wire.OutPoint{Hash: rec.Hash, Tree: tree}
for _, addr := range addrs {
ma, err := w.manager.Address(addrmgrNs, addr)
// Missing addresses are skipped. Other errors should
// be propagated.
if errors.Is(err, errors.NotExist) {
continue
}
if err != nil {
return nil, errors.E(op, err)
}
if isTicketCommit {
err = w.txStore.AddTicketCommitment(txmgrNs, rec, uint32(i),
ma.Account())
} else {
err = w.txStore.AddCredit(dbtx, rec, blockMeta,
uint32(i), ma.Internal(), ma.Account())
}
if err != nil {
return nil, errors.E(op, err)
}
err = w.markUsedAddress(op, dbtx, ma)
if err != nil {
return nil, err
}
if watchOutPoint {
outpoint.Index = uint32(i)
watchOutPoints = append(watchOutPoints, outpoint)
}
log.Debugf("Marked address %v used", addr)
}
// Handle P2SH addresses that are multisignature scripts
// with keys that we own.
if class == stdscript.STScriptHash {
var expandedScript []byte
for _, addr := range addrs {
expandedScript, err = w.manager.RedeemScript(addrmgrNs, addr)
if err != nil {
log.Debugf("failed to find redeemscript for "+
"address %v in address manager: %v",
addr, err)
continue
}
}
// Otherwise, extract the actual addresses and see if any are ours.
expClass, multisigAddrs := stdscript.ExtractAddrs(scriptVersionAssumed, expandedScript, w.chainParams)
// Skip non-multisig scripts.
if expClass != stdscript.STMultiSig {
continue
}
for _, maddr := range multisigAddrs {
_, err := w.manager.Address(addrmgrNs, maddr)
// An address we own; handle accordingly.
if err == nil {
err := w.txStore.AddMultisigOut(
dbtx, rec, blockMeta, uint32(i))
if err != nil {
// This will throw if there are multiple private keys
// for this multisignature output owned by the wallet,
// so it's routed to debug.
log.Debugf("unable to add multisignature output: %v", err)
}
}
}
}
}
if (rec.TxType == stake.TxTypeSSGen) || (rec.TxType == stake.TxTypeSSRtx) {
err = w.txStore.RedeemTicketCommitments(txmgrNs, rec, blockMeta)
if err != nil {
log.Errorf("Error redeeming ticket commitments: %v", err)
}
}
// Send notification of mined or unmined transaction to any interested
// clients.
//
// TODO: Avoid the extra db hits.
if header == nil {
details, err := w.txStore.UniqueTxDetails(txmgrNs, &rec.Hash, nil)
if err != nil {
log.Errorf("Cannot query transaction details for notifiation: %v", err)
} else {
w.NtfnServer.notifyUnminedTransaction(dbtx, details)
}
} else {
details, err := w.txStore.UniqueTxDetails(txmgrNs, &rec.Hash, &blockMeta.Block)
if err != nil {
log.Errorf("Cannot query transaction details for notifiation: %v", err)
} else {
w.NtfnServer.notifyMinedTransaction(dbtx, details, blockMeta)
}
}
return watchOutPoints, nil
}
// selectOwnedTickets returns a slice of tickets hashes from the tickets
// argument that are owned by the wallet.
//
// Because votes must be created for tickets tracked by both the transaction
// manager and the stake manager, this function checks both.
func selectOwnedTickets(w *Wallet, dbtx walletdb.ReadTx, tickets []*chainhash.Hash) []*chainhash.Hash {
var owned []*chainhash.Hash
for _, ticketHash := range tickets {
if w.txStore.OwnTicket(dbtx, ticketHash) || w.stakeMgr.OwnTicket(ticketHash) {
owned = append(owned, ticketHash)
}
}
return owned
}
// VoteOnOwnedTickets creates and publishes vote transactions for all owned
// tickets in the winningTicketHashes slice if wallet voting is enabled. The
// vote is only valid when voting on the block described by the passed block
// hash and height. When a network backend is associated with the wallet,
// relevant commitment outputs are loaded as watched data.
func (w *Wallet) VoteOnOwnedTickets(ctx context.Context, winningTicketHashes []*chainhash.Hash, blockHash *chainhash.Hash, blockHeight int32) error {
const op errors.Op = "wallet.VoteOnOwnedTickets"
if !w.votingEnabled || blockHeight < int32(w.chainParams.StakeValidationHeight)-1 {
return nil
}
n, err := w.NetworkBackend()
if err != nil {
return errors.E(op, err)
}
dcp0010Active, err := deployments.DCP0010Active(ctx, blockHeight,
w.chainParams, n)
if err != nil {
return errors.E(op, err)
}
// TODO The behavior of this is not quite right if tons of blocks
// are coming in quickly, because the transaction store will end up
// out of sync with the voting channel here. This should probably
// be fixed somehow, but this should be stable for networks that
// are voting at normal block speeds.
var ticketHashes []*chainhash.Hash
var votes []*wire.MsgTx
var usedVoteBits []stake.VoteBits
defaultVoteBits := w.VoteBits()
var watchOutPoints []wire.OutPoint
err = walletdb.View(ctx, w.db, func(dbtx walletdb.ReadTx) error {
txmgrNs := dbtx.ReadBucket(wtxmgrNamespaceKey)
// Only consider tickets owned by this wallet.
ticketHashes = selectOwnedTickets(w, dbtx, winningTicketHashes)
if len(ticketHashes) == 0 {
return nil
}
votes = make([]*wire.MsgTx, len(ticketHashes))
usedVoteBits = make([]stake.VoteBits, len(ticketHashes))
addrmgrNs := dbtx.ReadBucket(waddrmgrNamespaceKey)
for i, ticketHash := range ticketHashes {
ticketPurchase, err := w.txStore.Tx(txmgrNs, ticketHash)
if err != nil && errors.Is(err, errors.NotExist) {
ticketPurchase, err = w.stakeMgr.TicketPurchase(dbtx, ticketHash)
}
if err != nil {
log.Errorf("Failed to read ticket purchase transaction for "+
"owned winning ticket %v: %v", ticketHash, err)
continue
}
// Don't create votes when this wallet doesn't have voting
// authority or the private key to vote.
owned, haveKey, err := w.hasVotingAuthority(addrmgrNs, ticketPurchase)
if err != nil {
return err
}
if !(owned && haveKey) {
continue
}
ticketVoteBits := defaultVoteBits
// Check for and use per-ticket votebits if set for this ticket.
if tvb, found := w.readDBTicketVoteBits(dbtx, ticketHash); found {
ticketVoteBits = tvb
}
// When not on mainnet, randomly disapprove blocks based
// on the disapprove percent.
dp := w.DisapprovePercent()
if dp > 0 {
if w.chainParams.Net == wire.MainNet {
log.Warnf("block disapprove percent set on mainnet")
} else {
if int64(dp) > randInt63n(100) {
log.Infof("Disapproving block %v voted with ticket %v",
blockHash, ticketHash)
// Set the BlockValid bit to zero,
// disapproving the block.
const blockIsValidBit = uint16(0x01)
ticketVoteBits.Bits &= ^blockIsValidBit
}
}
}
// Deal with treasury votes
tspends := w.GetAllTSpends(ctx)
// Dealwith consensus votes
vote, err := createUnsignedVote(ticketHash, ticketPurchase,
blockHeight, blockHash, ticketVoteBits, w.subsidyCache,
w.chainParams, dcp0010Active)
if err != nil {
log.Errorf("Failed to create vote transaction for ticket "+
"hash %v: %v", ticketHash, err)
continue
}
// Iterate over all tpends and determine if they are
// within the voting window.
tVotes := make([]byte, 0, 256)
tVotes = append(tVotes[:], 'T', 'V')
for _, v := range tspends {
if !blockchain.InsideTSpendWindow(int64(blockHeight),
v.Expiry, w.chainParams.TreasuryVoteInterval,
w.chainParams.TreasuryVoteIntervalMultiplier) {
continue
}
// Get policy for tspend, falling back to any
// policy for the Pi key.
tspendHash := v.TxHash()
tspendVote := w.TSpendPolicy(&tspendHash, ticketHash)
if tspendVote == stake.TreasuryVoteInvalid {
continue
}
// Append tspend hash and vote bits
tVotes = append(tVotes[:], tspendHash[:]...)
tVotes = append(tVotes[:], byte(tspendVote))
}
if len(tVotes) > 2 {
// Vote was appended. Create output and flip
// script version.
var b txscript.ScriptBuilder
b.AddOp(txscript.OP_RETURN)
b.AddData(tVotes)
tspendVoteScript, err := b.Script()
if err != nil {
// Log error and continue.
log.Errorf("Failed to create treasury "+
"vote for ticket hash %v: %v",
ticketHash, err)
} else {
// Success.
vote.AddTxOut(wire.NewTxOut(0, tspendVoteScript))
vote.Version = 3
}
}
// Sign vote and sumit.
err = w.signVote(addrmgrNs, ticketPurchase, vote)
if err != nil {
log.Errorf("Failed to sign vote for ticket hash %v: %v",
ticketHash, err)
continue
}
votes[i] = vote
usedVoteBits[i] = ticketVoteBits
watchOutPoints = w.appendRelevantOutpoints(watchOutPoints, dbtx, vote)
}
return nil
})
if err != nil {
log.Errorf("View failed: %v", errors.E(op, err))
}
// Remove nil votes without preserving order.
for i := 0; i < len(votes); {
if votes[i] == nil {
votes[i], votes[len(votes)-1] = votes[len(votes)-1], votes[i]
votes = votes[:len(votes)-1]
continue
}
i++
}
voteRecords := make([]*udb.TxRecord, 0, len(votes))
for i := range votes {
rec, err := udb.NewTxRecordFromMsgTx(votes[i], time.Now())
if err != nil {
log.Errorf("Failed to create transaction record: %v", err)
continue
}
voteRecords = append(voteRecords, rec)
}
w.recentlyPublishedMu.Lock()
for i := range voteRecords {