forked from chappjc/dcrspy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspychans.go
98 lines (83 loc) · 2.98 KB
/
spychans.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
package main
import (
"time"
"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrd/dcrutil"
"github.com/decred/dcrd/wire"
)
const (
// blockConnChanBuffer is the size of the block connected channel buffer.
blockConnChanBuffer = 8
// newTxChanBuffer is the size of the new transaction channel buffer, for
// ANY transactions are added into mempool.
newTxChanBuffer = 2000
// relevantMempoolTxChanBuffer is the size of the new transaction channel
// buffer, for relevant transactions that are added into mempool.
relevantMempoolTxChanBuffer = 512
)
// BlockWatchedTx contains, for a certain block, the transactions for certain
// watched addresses
type BlockWatchedTx struct {
BlockHeight int64
TxsForAddress map[string][]*wire.MsgTx
}
// Channels are package-level variables for simplicity
var spyChans struct {
txTicker *time.Ticker
connectChan chan *chainhash.Hash
stakeDiffChan chan int64
connectChanStkInf chan int32
spendTxBlockChan, recvTxBlockChan chan *BlockWatchedTx
relevantTxMempoolChan chan *dcrutil.Tx
newTxChan chan *chainhash.Hash
}
func makeChans(cfg *config) {
// If we're monitoring for blocks OR collecting block data, these channels
// are necessary to handle new block notifications. Otherwise, leave them
// as nil so that both a send (below) blocks and a receive (in spy.go,
// blockConnectedHandler) block. default case makes non-blocking below.
// quit channel case manages blockConnectedHandlers.
if !cfg.NoCollectBlockData && !cfg.NoMonitor {
spyChans.connectChan = make(chan *chainhash.Hash, blockConnChanBuffer)
spyChans.stakeDiffChan = make(chan int64, blockConnChanBuffer)
}
// Like connectChan for block data, connectChanStkInf is used when a new
// block is connected, but to signal the stake info monitor.
if !cfg.NoCollectStakeInfo && !cfg.NoMonitor {
spyChans.connectChanStkInf = make(chan int32, blockConnChanBuffer)
}
// watchaddress
if len(cfg.WatchAddresses) > 0 && !cfg.NoMonitor {
// recv/spendTxBlockChan come with connected blocks
spyChans.recvTxBlockChan = make(chan *BlockWatchedTx, blockConnChanBuffer)
spyChans.spendTxBlockChan = make(chan *BlockWatchedTx, blockConnChanBuffer)
spyChans.relevantTxMempoolChan = make(chan *dcrutil.Tx, relevantMempoolTxChanBuffer)
}
if cfg.MonitorMempool {
spyChans.newTxChan = make(chan *chainhash.Hash, newTxChanBuffer)
}
}
func closeChans() {
if spyChans.stakeDiffChan != nil {
close(spyChans.stakeDiffChan)
}
if spyChans.connectChan != nil {
close(spyChans.connectChan)
}
if spyChans.connectChanStkInf != nil {
close(spyChans.connectChanStkInf)
}
if spyChans.newTxChan != nil {
spyChans.txTicker.Stop()
close(spyChans.newTxChan)
}
if spyChans.relevantTxMempoolChan != nil {
close(spyChans.relevantTxMempoolChan)
}
if spyChans.spendTxBlockChan != nil {
close(spyChans.spendTxBlockChan)
}
if spyChans.recvTxBlockChan != nil {
close(spyChans.recvTxBlockChan)
}
}