forked from chappjc/dcrspy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspy.go
178 lines (158 loc) · 4.57 KB
/
spy.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
// spy.go defines the chainMonitor and stakeMonitor, which handle the block
// connected notifications via blockConnChan. They are separate because we
// might want to run without a wallet, just monitoring dcrd data.
//
// chappjc
package main
import (
"strings"
"sync"
"time"
)
// for getblock, ticketfeeinfo, estimatestakediff, etc.
type chainMonitor struct {
collector *blockDataCollector
dataSavers []BlockDataSaver
quit chan struct{}
wg *sync.WaitGroup
noTicketPool bool
watchaddrs map[string]TxAction
}
// newChainMonitor creates a new chainMonitor
func newChainMonitor(collector *blockDataCollector,
savers []BlockDataSaver,
quit chan struct{}, wg *sync.WaitGroup, noPoolValue bool,
addrs map[string]TxAction) *chainMonitor {
return &chainMonitor{
collector: collector,
dataSavers: savers,
quit: quit,
wg: wg,
noTicketPool: noPoolValue,
watchaddrs: addrs,
}
}
// blockConnectedHandler handles block connected notifications, which trigger
// data collection and storage.
func (p *chainMonitor) blockConnectedHandler() {
defer p.wg.Done()
out:
for {
keepon:
select {
case hash, ok := <-spyChans.connectChan:
if !ok {
log.Warnf("Block connected channel closed.")
break out
}
block, _ := p.collector.dcrdChainSvr.GetBlock(hash)
height := int64(block.Header.Height)
daemonLog.Infof("Block height %v connected", height)
if len(p.watchaddrs) > 0 {
// txsForOutpoints := blockConsumesOutpointWithAddresses(block, p.watchaddrs,
// p.collector.dcrdChainSvr)
// if len(txsForOutpoints) > 0 {
// p.spendTxBlockChan <- &BlockWatchedTx{height, txsForOutpoints}
// }
txsForAddrs := BlockReceivesToAddresses(block, p.watchaddrs)
if len(txsForAddrs) > 0 {
spyChans.recvTxBlockChan <- &BlockWatchedTx{height,
txsForAddrs}
}
}
// data collection with timeout
bdataChan := make(chan *blockData)
// fire it off and get the blockData pointer back through the channel
go func() {
BlockData, err := p.collector.collect(p.noTicketPool)
if err != nil {
log.Errorf("Block data collection failed: %v", err.Error())
// BlockData is nil when err != nil
}
bdataChan <- BlockData
}()
// Wait for X seconds before giving up on collect()
var BlockData *blockData
select {
case BlockData = <-bdataChan:
if BlockData == nil {
break keepon
}
case <-time.After(time.Second * 20):
log.Errorf("Block data collection TIMEOUT after 20 seconds.")
break keepon
}
// Store block data with each saver
for _, s := range p.dataSavers {
if s != nil {
// save data to wherever the saver wants to put it
go s.Store(BlockData)
}
}
case _, ok := <-p.quit:
if !ok {
log.Debugf("Got quit signal. Exiting block connected handler for BLOCK monitor.")
break out
}
}
}
}
// for getstakeinfo, etc.
type stakeMonitor struct {
collector *stakeInfoDataCollector
dataSavers []StakeInfoDataSaver
quit chan struct{}
wg *sync.WaitGroup
}
// newStakeMonitor creates a new stakeMonitor
func newStakeMonitor(collector *stakeInfoDataCollector,
savers []StakeInfoDataSaver,
quit chan struct{}, wg *sync.WaitGroup) *stakeMonitor {
return &stakeMonitor{
collector: collector,
dataSavers: savers,
quit: quit,
wg: wg,
}
}
// blockConnectedHandler handles block connected notifications, which trigger
// data collection and storage.
func (p *stakeMonitor) blockConnectedHandler() {
defer p.wg.Done()
out:
for {
select {
case height, ok := <-spyChans.connectChanStkInf:
if !ok {
log.Warnf("Block connected channel closed.")
break out
}
// Let the wallet process the new block (too bad no wallet ntfns!)
time.Sleep(time.Millisecond * 300)
// Try to collect the data, retry if wallet says to
collect:
stakeInfo, err := p.collector.collect(uint32(height))
if err != nil {
log.Errorf("Stake info data collection failed: %v", err)
// Look for that -4 message from wallet that says: "the wallet is
// currently syncing to the best block, please try again later"
if strings.Contains(err.Error(), "try again later") {
time.Sleep(time.Millisecond * 700)
goto collect // mmm, feel so dirty! maybe make this "cleaner" later
}
break out
}
for _, s := range p.dataSavers {
if s != nil {
// save data to wherever the saver wants to put it
go s.Store(stakeInfo)
}
}
case _, ok := <-p.quit:
if !ok {
log.Debugf("Got quit signal. Exiting block connected handler for STAKE monitor.")
break out
}
}
}
}