forked from decred/dcrwallet
-
Notifications
You must be signed in to change notification settings - Fork 2
/
notifications.go
967 lines (882 loc) · 29.1 KB
/
notifications.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
// Copyright (c) 2015-2016 The btcsuite developers
// 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 wallet
import (
"bytes"
"context"
"sync"
"github.com/hybridnetwork/hxd/blockchain"
"github.com/hybridnetwork/hxd/blockchain/stake"
"github.com/hybridnetwork/hxd/chaincfg/chainhash"
"github.com/hybridnetwork/hxd/txscript"
"github.com/hybridnetwork/hxd/wire"
dcrutil "github.com/hybridnetwork/hxutil"
dcrrpcclient "github.com/hybridnetwork/hxrpcclient"
"github.com/hybridnetwork/hxutil/hdkeychain"
"github.com/hybridnetwork/hxwallet/apperrors"
"github.com/hybridnetwork/hxwallet/wallet/udb"
"github.com/hybridnetwork/hxwallet/walletdb"
)
// TODO: It would be good to send errors during notification creation to the rpc
// server instead of just logging them here so the client is aware that wallet
// isn't working correctly and notifications are missing.
// TODO: Anything dealing with accounts here is expensive because the database
// is not organized correctly for true account support, but do the slow thing
// instead of the easy thing since the db can be fixed later, and we want the
// api correct now.
// NotificationServer is a server that interested clients may hook into to
// receive notifications of changes in a wallet. A client is created for each
// registered notification. Clients are guaranteed to receive messages in the
// order wallet created them, but there is no guaranteed synchronization between
// different clients.
type NotificationServer struct {
transactions []chan *TransactionNotifications
// Coalesce transaction notifications since wallet previously did not add
// mined txs together. Now it does and this can be rewritten.
currentTxNtfn *TransactionNotifications
accountClients []chan *AccountNotification
tipChangedClients []chan *MainTipChangedNotification
confClients []*ConfirmationNotificationsClient
mu sync.Mutex // Only protects registered clients
wallet *Wallet // smells like hacks
}
func newNotificationServer(wallet *Wallet) *NotificationServer {
return &NotificationServer{
wallet: wallet,
}
}
func lookupInputAccount(dbtx walletdb.ReadTx, w *Wallet, details *udb.TxDetails, deb udb.DebitRecord) uint32 {
addrmgrNs := dbtx.ReadBucket(waddrmgrNamespaceKey)
txmgrNs := dbtx.ReadBucket(wtxmgrNamespaceKey)
// TODO: Debits should record which account(s?) they
// debit from so this doesn't need to be looked up.
prevOP := &details.MsgTx.TxIn[deb.Index].PreviousOutPoint
prev, err := w.TxStore.TxDetails(txmgrNs, &prevOP.Hash)
if err != nil {
log.Errorf("Cannot query previous transaction details for %v: %v", prevOP.Hash, err)
return 0
}
if prev == nil {
log.Errorf("Missing previous transaction %v", prevOP.Hash)
return 0
}
prevOut := prev.MsgTx.TxOut[prevOP.Index]
_, addrs, _, err := txscript.ExtractPkScriptAddrs(prevOut.Version, prevOut.PkScript, w.chainParams)
var inputAcct uint32
if err == nil && len(addrs) > 0 {
inputAcct, err = w.Manager.AddrAccount(addrmgrNs, addrs[0])
}
if err != nil {
log.Errorf("Cannot fetch account for previous output %v: %v", prevOP, err)
inputAcct = 0
}
return inputAcct
}
func lookupOutputChain(dbtx walletdb.ReadTx, w *Wallet, details *udb.TxDetails,
cred udb.CreditRecord) (account uint32, internal bool, address dcrutil.Address,
amount int64, outputScript []byte) {
addrmgrNs := dbtx.ReadBucket(waddrmgrNamespaceKey)
output := details.MsgTx.TxOut[cred.Index]
_, addrs, _, err := txscript.ExtractPkScriptAddrs(output.Version, output.PkScript, w.chainParams)
var ma udb.ManagedAddress
if err == nil && len(addrs) > 0 {
ma, err = w.Manager.Address(addrmgrNs, addrs[0])
}
if err != nil {
log.Errorf("Cannot fetch account for wallet output: %v", err)
} else {
account = ma.Account()
internal = ma.Internal()
address = ma.Address()
amount = output.Value
outputScript = output.PkScript
}
return
}
func makeTxSummary(dbtx walletdb.ReadTx, w *Wallet, details *udb.TxDetails) TransactionSummary {
serializedTx := details.SerializedTx
if serializedTx == nil {
var buf bytes.Buffer
buf.Grow(details.MsgTx.SerializeSize())
err := details.MsgTx.Serialize(&buf)
if err != nil {
log.Errorf("Transaction serialization: %v", err)
}
serializedTx = buf.Bytes()
}
var fee dcrutil.Amount
if len(details.Debits) == len(details.MsgTx.TxIn) {
for _, deb := range details.Debits {
fee += deb.Amount
}
for _, txOut := range details.MsgTx.TxOut {
fee -= dcrutil.Amount(txOut.Value)
}
}
var inputs []TransactionSummaryInput
if len(details.Debits) != 0 {
inputs = make([]TransactionSummaryInput, len(details.Debits))
for i, d := range details.Debits {
inputs[i] = TransactionSummaryInput{
Index: d.Index,
PreviousAccount: lookupInputAccount(dbtx, w, details, d),
PreviousAmount: d.Amount,
}
}
}
outputs := make([]TransactionSummaryOutput, 0, len(details.MsgTx.TxOut))
for i := range details.MsgTx.TxOut {
credIndex := len(outputs)
mine := len(details.Credits) > credIndex && details.Credits[credIndex].Index == uint32(i)
if !mine {
continue
}
acct, internal, address, amount, outputScript := lookupOutputChain(dbtx, w, details, details.Credits[credIndex])
output := TransactionSummaryOutput{
Index: uint32(i),
Account: acct,
Internal: internal,
Amount: dcrutil.Amount(amount),
Address: address,
OutputScript: outputScript,
}
outputs = append(outputs, output)
}
var transactionType = TxTransactionType(&details.MsgTx)
// Use earliest of receive time or block time if the transaction is mined.
receiveTime := details.Received
if details.Height() >= 0 && details.Block.Time.Before(receiveTime) {
receiveTime = details.Block.Time
}
return TransactionSummary{
Hash: &details.Hash,
Transaction: serializedTx,
MyInputs: inputs,
MyOutputs: outputs,
Fee: fee,
Timestamp: receiveTime.Unix(),
Type: transactionType,
}
}
func makeTicketSummary(chainClient *dcrrpcclient.Client, dbtx walletdb.ReadTx, w *Wallet, details *udb.TicketDetails) *TicketSummary {
var ticketStatus = TicketStatusLive
ticketTransactionDetails := makeTxSummary(dbtx, w, details.Ticket)
if details.Spender != nil {
spenderTransactionDetails := makeTxSummary(dbtx, w, details.Spender)
if details.Spender.TxType == stake.TxTypeSSGen {
ticketStatus = TicketStatusVoted
} else if details.Spender.TxType == stake.TxTypeSSRtx {
ticketStatus = TicketStatusRevoked
}
return &TicketSummary{
Ticket: &ticketTransactionDetails,
Spender: &spenderTransactionDetails,
Status: ticketStatus,
}
}
if details.Ticket.Height() == int32(-1) {
ticketStatus = TicketStatusUnmined
} else {
txmgrNs := dbtx.ReadBucket(wtxmgrNamespaceKey)
_, tipHeight := w.TxStore.MainChainTip(txmgrNs)
expiryConfs := int32(w.chainParams.TicketExpiry) +
int32(w.chainParams.TicketMaturity) + 1
// Check if ticket age is not yet mature
if !confirmed(int32(w.chainParams.TicketMaturity)+1, details.Ticket.Height(), tipHeight) {
ticketStatus = TicketStatusImmature
// Check if ticket age is over TicketExpiry limit and therefore expired
} else if confirmed(expiryConfs, details.Ticket.Height(), tipHeight) {
ticketStatus = TicketStatusExpired
} else {
// Final check to see if ticket was missed otherwise it's live
live, err := chainClient.ExistsLiveTicket(&details.Ticket.Hash)
if err != nil {
log.Errorf("Unable to check if ticket was live for ticket status: %v", &details.Ticket.Hash)
ticketStatus = TicketStatusUnknown
} else if !live {
ticketStatus = TicketStatusMissed
}
}
}
return &TicketSummary{
Ticket: &ticketTransactionDetails,
Spender: nil,
Status: ticketStatus,
}
}
func totalBalances(dbtx walletdb.ReadTx, w *Wallet, m map[uint32]dcrutil.Amount) error {
addrmgrNs := dbtx.ReadBucket(waddrmgrNamespaceKey)
unspent, err := w.TxStore.UnspentOutputs(dbtx.ReadBucket(wtxmgrNamespaceKey))
if err != nil {
return err
}
for i := range unspent {
output := unspent[i]
var outputAcct uint32
_, addrs, _, err := txscript.ExtractPkScriptAddrs(
txscript.DefaultScriptVersion, output.PkScript, w.chainParams)
if err == nil && len(addrs) > 0 {
outputAcct, err = w.Manager.AddrAccount(addrmgrNs, addrs[0])
}
if err == nil {
_, ok := m[outputAcct]
if ok {
m[outputAcct] += output.Amount
}
}
}
return nil
}
func flattenBalanceMap(m map[uint32]dcrutil.Amount) []AccountBalance {
s := make([]AccountBalance, 0, len(m))
for k, v := range m {
s = append(s, AccountBalance{Account: k, TotalBalance: v})
}
return s
}
func relevantAccounts(w *Wallet, m map[uint32]dcrutil.Amount, txs []TransactionSummary) {
for _, tx := range txs {
for _, d := range tx.MyInputs {
m[d.PreviousAccount] = 0
}
for _, c := range tx.MyOutputs {
m[c.Account] = 0
}
}
}
func (s *NotificationServer) notifyUnminedTransaction(dbtx walletdb.ReadTx, details *udb.TxDetails) {
// Sanity check: should not be currently coalescing a notification for
// mined transactions at the same time that an unmined tx is notified.
if s.currentTxNtfn != nil {
log.Tracef("Notifying unmined tx notification while creating notification for blocks")
}
defer s.mu.Unlock()
s.mu.Lock()
clients := s.transactions
if len(clients) == 0 {
return
}
unminedTxs := []TransactionSummary{makeTxSummary(dbtx, s.wallet, details)}
unminedHashes, err := s.wallet.TxStore.UnminedTxHashes(dbtx.ReadBucket(wtxmgrNamespaceKey))
if err != nil {
log.Errorf("Cannot fetch unmined transaction hashes: %v", err)
return
}
bals := make(map[uint32]dcrutil.Amount)
relevantAccounts(s.wallet, bals, unminedTxs)
err = totalBalances(dbtx, s.wallet, bals)
if err != nil {
log.Errorf("Cannot determine balances for relevant accounts: %v", err)
return
}
n := &TransactionNotifications{
UnminedTransactions: unminedTxs,
UnminedTransactionHashes: unminedHashes,
NewBalances: flattenBalanceMap(bals),
}
for _, c := range clients {
c <- n
}
}
func (s *NotificationServer) notifyDetachedBlock(hash *chainhash.Hash) {
if s.currentTxNtfn == nil {
s.currentTxNtfn = &TransactionNotifications{}
}
s.currentTxNtfn.DetachedBlocks = append(s.currentTxNtfn.DetachedBlocks, hash)
}
func (s *NotificationServer) notifyMinedTransaction(dbtx walletdb.ReadTx, details *udb.TxDetails, block *udb.BlockMeta) {
if s.currentTxNtfn == nil {
s.currentTxNtfn = &TransactionNotifications{}
}
n := len(s.currentTxNtfn.AttachedBlocks)
if n == 0 || *s.currentTxNtfn.AttachedBlocks[n-1].Hash != block.Hash {
return
}
txs := &s.currentTxNtfn.AttachedBlocks[n-1].Transactions
*txs = append(*txs, makeTxSummary(dbtx, s.wallet, details))
}
func (s *NotificationServer) notifyAttachedBlock(dbtx walletdb.ReadTx, block *wire.BlockHeader, blockHash *chainhash.Hash) {
if s.currentTxNtfn == nil {
s.currentTxNtfn = &TransactionNotifications{}
}
// Add block details if it wasn't already included for previously
// notified mined transactions.
n := len(s.currentTxNtfn.AttachedBlocks)
if n == 0 || *s.currentTxNtfn.AttachedBlocks[n-1].Hash != *blockHash {
s.currentTxNtfn.AttachedBlocks = append(s.currentTxNtfn.AttachedBlocks, Block{
Hash: blockHash,
Height: int32(block.Height),
Timestamp: block.Timestamp.Unix(),
})
}
}
func (s *NotificationServer) sendAttachedBlockNotification() {
// Avoid work if possible
s.mu.Lock()
if len(s.transactions) == 0 {
s.mu.Unlock()
s.currentTxNtfn = nil
return
}
s.mu.Unlock()
// The UnminedTransactions field is intentionally not set. Since the
// hashes of all detached blocks are reported, and all transactions
// moved from a mined block back to unconfirmed are either in the
// UnminedTransactionHashes slice or don't exist due to conflicting with
// a mined transaction in the new best chain, there is no possiblity of
// a new, previously unseen transaction appearing in unconfirmed.
var (
w = s.wallet
bals = make(map[uint32]dcrutil.Amount)
unminedHashes []*chainhash.Hash
)
err := walletdb.View(w.db, func(dbtx walletdb.ReadTx) error {
txmgrNs := dbtx.ReadBucket(wtxmgrNamespaceKey)
var err error
unminedHashes, err = w.TxStore.UnminedTxHashes(txmgrNs)
if err != nil {
return err
}
for _, b := range s.currentTxNtfn.AttachedBlocks {
relevantAccounts(w, bals, b.Transactions)
}
return totalBalances(dbtx, w, bals)
})
if err != nil {
log.Errorf("Failed to construct attached blocks notification: %v", err)
s.currentTxNtfn = nil
return
}
s.currentTxNtfn.UnminedTransactionHashes = unminedHashes
s.currentTxNtfn.NewBalances = flattenBalanceMap(bals)
s.mu.Lock()
for _, c := range s.transactions {
c <- s.currentTxNtfn
}
s.mu.Unlock()
s.currentTxNtfn = nil
}
// TransactionNotifications is a notification of changes to the wallet's
// transaction set and the current chain tip that wallet is considered to be
// synced with. All transactions added to the blockchain are organized by the
// block they were mined in.
//
// During a chain switch, all removed block hashes are included. Detached
// blocks are sorted in the reverse order they were mined. Attached blocks are
// sorted in the order mined.
//
// All newly added unmined transactions are included. Removed unmined
// transactions are not explicitly included. Instead, the hashes of all
// transactions still unmined are included.
//
// If any transactions were involved, each affected account's new total balance
// is included.
//
// TODO: Because this includes stuff about blocks and can be fired without any
// changes to transactions, it needs a better name.
type TransactionNotifications struct {
AttachedBlocks []Block
DetachedBlocks []*chainhash.Hash
UnminedTransactions []TransactionSummary
UnminedTransactionHashes []*chainhash.Hash
NewBalances []AccountBalance
}
// Block contains the properties and all relevant transactions of an attached
// block.
type Block struct {
Hash *chainhash.Hash
Height int32
Timestamp int64
Transactions []TransactionSummary
}
// TicketSummary contains the properties to describe a ticket's current status
type TicketSummary struct {
Ticket *TransactionSummary
Spender *TransactionSummary
Status TicketStatus
}
// TicketStatus describes the current status a ticket can be observed to be.
type TicketStatus int8
const (
// TicketStatusUnknown any ticket that its status was unable to be determined.
TicketStatusUnknown TicketStatus = iota
// TicketStatusUnmined any not yet mined ticket.
TicketStatusUnmined
// TicketStatusImmature any so to be live ticket.
TicketStatusImmature
// TicketStatusLive any currently live ticket.
TicketStatusLive
// TicketStatusVoted any ticket that was seen to have voted.
TicketStatusVoted
// TicketStatusRevoked any ticket that has been previously revoked.
TicketStatusRevoked
// TicketStatusMissed any ticket that has yet to be revoked, and was missed.
TicketStatusMissed
// TicketStatusExpired any ticket that has yet to be revoked, and was expired.
TicketStatusExpired
)
// TransactionSummary contains a transaction relevant to the wallet and marks
// which inputs and outputs were relevant.
type TransactionSummary struct {
Hash *chainhash.Hash
Transaction []byte
MyInputs []TransactionSummaryInput
MyOutputs []TransactionSummaryOutput
Fee dcrutil.Amount
Timestamp int64
Type TransactionType
}
// TransactionType describes the which type of transaction is has been observed to be.
// For instance, if it has a ticket as an input and a stake base reward as an ouput,
// it is known to be a vote.
type TransactionType int8
const (
// TransactionTypeRegular transaction type for all regular transactions.
TransactionTypeRegular TransactionType = iota
// TransactionTypeCoinbase is the transaction type for all coinbase transactions.
TransactionTypeCoinbase
// TransactionTypeTicketPurchase transaction type for all transactions that
// consume regular transactions as inputs and have commitments for future votes
// as outputs.
TransactionTypeTicketPurchase
// TransactionTypeVote transaction type for all transactions that consume a ticket
// and also offer a stake base reward output.
TransactionTypeVote
// TransactionTypeRevocation transaction type for all transactions that consume a
// ticket, but offer no stake base reward.
TransactionTypeRevocation
)
// TxTransactionType returns the correct TransactionType given a wire transaction
func TxTransactionType(tx *wire.MsgTx) TransactionType {
if blockchain.IsCoinBaseTx(tx) {
return TransactionTypeCoinbase
} else if ok, _ := stake.IsSStx(tx); ok {
return TransactionTypeTicketPurchase
} else if ok, _ = stake.IsSSGen(tx); ok {
return TransactionTypeVote
} else if ok, _ = stake.IsSSRtx(tx); ok {
return TransactionTypeRevocation
} else {
return TransactionTypeRegular
}
}
// TransactionSummaryInput describes a transaction input that is relevant to the
// wallet. The Index field marks the transaction input index of the transaction
// (not included here). The PreviousAccount and PreviousAmount fields describe
// how much this input debits from a wallet account.
type TransactionSummaryInput struct {
Index uint32
PreviousAccount uint32
PreviousAmount dcrutil.Amount
}
// TransactionSummaryOutput describes wallet properties of a transaction output
// controlled by the wallet. The Index field marks the transaction output index
// of the transaction (not included here).
type TransactionSummaryOutput struct {
Index uint32
Account uint32
Internal bool
Amount dcrutil.Amount
Address dcrutil.Address
OutputScript []byte
}
// AccountBalance associates a total (zero confirmation) balance with an
// account. Balances for other minimum confirmation counts require more
// expensive logic and it is not clear which minimums a client is interested in,
// so they are not included.
type AccountBalance struct {
Account uint32
TotalBalance dcrutil.Amount
}
// TransactionNotificationsClient receives TransactionNotifications from the
// NotificationServer over the channel C.
type TransactionNotificationsClient struct {
C <-chan *TransactionNotifications
server *NotificationServer
}
// TransactionNotifications returns a client for receiving
// TransactionNotifiations notifications over a channel. The channel is
// unbuffered.
//
// When finished, the Done method should be called on the client to disassociate
// it from the server.
func (s *NotificationServer) TransactionNotifications() TransactionNotificationsClient {
c := make(chan *TransactionNotifications)
s.mu.Lock()
s.transactions = append(s.transactions, c)
s.mu.Unlock()
return TransactionNotificationsClient{
C: c,
server: s,
}
}
// Done deregisters the client from the server and drains any remaining
// messages. It must be called exactly once when the client is finished
// receiving notifications.
func (c *TransactionNotificationsClient) Done() {
go func() {
// Drain notifications until the client channel is removed from
// the server and closed.
for range c.C {
}
}()
go func() {
s := c.server
s.mu.Lock()
clients := s.transactions
for i, ch := range clients {
if c.C == ch {
clients[i] = clients[len(clients)-1]
s.transactions = clients[:len(clients)-1]
close(ch)
break
}
}
s.mu.Unlock()
}()
}
// AccountNotification contains properties regarding an account, such as its
// name and the number of derived and imported keys. When any of these
// properties change, the notification is fired.
type AccountNotification struct {
AccountNumber uint32
AccountName string
ExternalKeyCount uint32
InternalKeyCount uint32
ImportedKeyCount uint32
}
func (s *NotificationServer) notifyAccountProperties(props *udb.AccountProperties) {
defer s.mu.Unlock()
s.mu.Lock()
clients := s.accountClients
if len(clients) == 0 {
return
}
n := &AccountNotification{
AccountNumber: props.AccountNumber,
AccountName: props.AccountName,
ExternalKeyCount: 0,
InternalKeyCount: 0,
ImportedKeyCount: props.ImportedKeyCount,
}
// Key counts have to be fudged for BIP0044 accounts a little bit because
// only the last used child index is saved. Add the gap limit since these
// addresses have also been generated and are being watched for transaction
// activity.
if props.AccountNumber <= udb.MaxAccountNum {
n.ExternalKeyCount = minUint32(hdkeychain.HardenedKeyStart,
props.LastUsedExternalIndex+uint32(s.wallet.gapLimit))
n.InternalKeyCount = minUint32(hdkeychain.HardenedKeyStart,
props.LastUsedInternalIndex+uint32(s.wallet.gapLimit))
}
for _, c := range clients {
c <- n
}
}
// AccountNotificationsClient receives AccountNotifications over the channel C.
type AccountNotificationsClient struct {
C chan *AccountNotification
server *NotificationServer
}
// AccountNotifications returns a client for receiving AccountNotifications over
// a channel. The channel is unbuffered. When finished, the client's Done
// method should be called to disassociate the client from the server.
func (s *NotificationServer) AccountNotifications() AccountNotificationsClient {
c := make(chan *AccountNotification)
s.mu.Lock()
s.accountClients = append(s.accountClients, c)
s.mu.Unlock()
return AccountNotificationsClient{
C: c,
server: s,
}
}
// Done deregisters the client from the server and drains any remaining
// messages. It must be called exactly once when the client is finished
// receiving notifications.
func (c *AccountNotificationsClient) Done() {
go func() {
for range c.C {
}
}()
go func() {
s := c.server
s.mu.Lock()
clients := s.accountClients
for i, ch := range clients {
if c.C == ch {
clients[i] = clients[len(clients)-1]
s.accountClients = clients[:len(clients)-1]
close(ch)
break
}
}
s.mu.Unlock()
}()
}
// MainTipChangedNotification describes processed changes to the main chain tip
// block. Attached and detached blocks are sorted by increasing heights.
//
// This is intended to be a lightweight alternative to TransactionNotifications
// when only info regarding the main chain tip block changing is needed.
type MainTipChangedNotification struct {
AttachedBlocks []*chainhash.Hash
DetachedBlocks []*chainhash.Hash
NewHeight int32
}
// MainTipChangedNotificationsClient receives MainTipChangedNotifications over
// the channel C.
type MainTipChangedNotificationsClient struct {
C chan *MainTipChangedNotification
server *NotificationServer
}
// MainTipChangedNotifications returns a client for receiving
// MainTipChangedNotification over a channel. The channel is unbuffered. When
// finished, the client's Done method should be called to disassociate the
// client from the server.
func (s *NotificationServer) MainTipChangedNotifications() MainTipChangedNotificationsClient {
c := make(chan *MainTipChangedNotification)
s.mu.Lock()
s.tipChangedClients = append(s.tipChangedClients, c)
s.mu.Unlock()
return MainTipChangedNotificationsClient{
C: c,
server: s,
}
}
// Done deregisters the client from the server and drains any remaining
// messages. It must be called exactly once when the client is finished
// receiving notifications.
func (c *MainTipChangedNotificationsClient) Done() {
go func() {
for range c.C {
}
}()
go func() {
s := c.server
s.mu.Lock()
clients := s.tipChangedClients
for i, ch := range clients {
if c.C == ch {
clients[i] = clients[len(clients)-1]
s.tipChangedClients = clients[:len(clients)-1]
close(ch)
break
}
}
s.mu.Unlock()
}()
}
func (s *NotificationServer) notifyMainChainTipChanged(n *MainTipChangedNotification) {
s.mu.Lock()
for _, c := range s.tipChangedClients {
c <- n
}
if len(s.confClients) > 0 {
var wg sync.WaitGroup
wg.Add(len(s.confClients))
for _, c := range s.confClients {
c := c
go func() {
c.process(n.NewHeight)
wg.Done()
}()
}
wg.Wait()
}
s.mu.Unlock()
}
// ConfirmationNotifications registers a client for confirmation notifications
// from the notification server.
func (s *NotificationServer) ConfirmationNotifications(ctx context.Context) *ConfirmationNotificationsClient {
c := &ConfirmationNotificationsClient{
watched: make(map[chainhash.Hash]int32),
r: make(chan *confNtfnResult),
ctx: ctx,
s: s,
}
// Register with the server
s.mu.Lock()
s.confClients = append(s.confClients, c)
s.mu.Unlock()
// Cleanup when caller signals done.
go func() {
<-ctx.Done()
// Remove item from notification server's slice
s.mu.Lock()
slice := &s.confClients
for i, sc := range *slice {
if c == sc {
(*slice)[i] = (*slice)[len(*slice)-1]
*slice = (*slice)[:len(*slice)-1]
break
}
}
s.mu.Unlock()
}()
return c
}
// ConfirmationNotificationsClient provides confirmation notifications of watched
// transactions until the caller's context signals done. Callers register for
// notifications using Watch and receive notifications by calling Recv.
type ConfirmationNotificationsClient struct {
watched map[chainhash.Hash]int32
mu sync.Mutex
r chan *confNtfnResult
ctx context.Context
s *NotificationServer
}
type confNtfnResult struct {
result []ConfirmationNotification
err error
}
// ConfirmationNotification describes the number of confirmations of a single
// transaction, or -1 if the transaction is unknown or removed from the wallet.
// If the transaction is mined (Confirmations >= 1), the block hash and height
// is included. Otherwise the block hash is nil and the block hegiht is set to
// -1.
type ConfirmationNotification struct {
TxHash *chainhash.Hash
Confirmations int32
BlockHash *chainhash.Hash // nil when unmined
BlockHeight int32 // -1 when unmined
}
// Watch adds additional transactions to watch and create confirmation results
// for. Results are immediately created with the current number of
// confirmations and are watched until stopAfter confirmations is met or the
// transaction is unknown or removed from the wallet.
func (c *ConfirmationNotificationsClient) Watch(txHashes []*chainhash.Hash, stopAfter int32) {
w := c.s.wallet
r := make([]ConfirmationNotification, 0, len(c.watched))
err := walletdb.View(w.db, func(dbtx walletdb.ReadTx) error {
txmgrNs := dbtx.ReadBucket(wtxmgrNamespaceKey)
_, tipHeight := w.TxStore.MainChainTip(txmgrNs)
// cannot range here, txHashes may be modified
for i := 0; i < len(txHashes); {
h := txHashes[i]
height, err := w.TxStore.TxBlockHeight(dbtx, h)
var confs int32
switch {
case apperrors.IsError(err, apperrors.ErrValueNoExists):
confs = -1
default:
confs = confirms(height, tipHeight)
case err != nil:
return err
}
r = append(r, ConfirmationNotification{
TxHash: h,
Confirmations: confs,
BlockHeight: -1,
})
if confs > 0 {
result := &r[len(r)-1]
height, err := w.TxStore.TxBlockHeight(dbtx, result.TxHash)
if err != nil {
return err
}
blockHash, err := w.TxStore.GetBlockHash(txmgrNs, height)
if err != nil {
return err
}
result.BlockHash = &blockHash
result.BlockHeight = height
}
if confs >= stopAfter || confs == -1 {
// Remove this hash from the slice so it is not added to the
// watch map. Do not increment i so this same index is used
// next iteration with the new hash.
s := &txHashes
(*s)[i] = (*s)[len(*s)-1]
*s = (*s)[:len(*s)-1]
} else {
i++
}
}
return nil
})
if err != nil {
r = nil
}
c.r <- &confNtfnResult{r, err}
c.mu.Lock()
for _, h := range txHashes {
c.watched[*h] = stopAfter
}
c.mu.Unlock()
}
// Recv waits for the next notification. Returns context.Canceled when the
// context is canceled.
func (c *ConfirmationNotificationsClient) Recv() ([]ConfirmationNotification, error) {
select {
case <-c.ctx.Done():
return nil, context.Canceled
case r := <-c.r:
return r.result, r.err
}
}
func (c *ConfirmationNotificationsClient) process(tipHeight int32) {
select {
case <-c.ctx.Done():
return
default:
}
c.mu.Lock()
w := c.s.wallet
r := &confNtfnResult{
result: make([]ConfirmationNotification, 0, len(c.watched)),
}
var unwatch []*chainhash.Hash
err := walletdb.View(w.db, func(dbtx walletdb.ReadTx) error {
txmgrNs := dbtx.ReadBucket(wtxmgrNamespaceKey)
for txHash, stopAfter := range c.watched {
txHash := txHash // copy
height, err := w.TxStore.TxBlockHeight(dbtx, &txHash)
var confs int32
switch {
case apperrors.IsError(err, apperrors.ErrValueNoExists):
confs = -1
default:
confs = confirms(height, tipHeight)
case err != nil:
return err
}
r.result = append(r.result, ConfirmationNotification{
TxHash: &txHash,
Confirmations: confs,
BlockHeight: -1,
})
if confs > 0 {
result := &r.result[len(r.result)-1]
height, err := w.TxStore.TxBlockHeight(dbtx, result.TxHash)
if err != nil {
return err
}
blockHash, err := w.TxStore.GetBlockHash(txmgrNs, height)
if err != nil {
return err
}
result.BlockHash = &blockHash
result.BlockHeight = height
}
if confs >= stopAfter || confs == -1 {
unwatch = append(unwatch, &txHash)
}
}
return nil
})
if err != nil {
r.result = nil
r.err = err
}
for _, h := range unwatch {
delete(c.watched, *h)
}
c.mu.Unlock()
select {
case c.r <- r:
case <-c.ctx.Done():
}
}