forked from chappjc/dcrspy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatchaddr.go
290 lines (259 loc) · 8.54 KB
/
watchaddr.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
// Watch for transactions receiving to or sending from certain addresses.
// Receiving works, sending is probably messed up.
package main
import (
"fmt"
"sync"
"time"
"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrd/dcrjson"
"github.com/decred/dcrd/dcrutil"
"github.com/decred/dcrd/rpcclient"
"github.com/decred/dcrd/txscript"
)
// tryGetTransaction and tryGetRawTransactionVerbose are hacks while I figure
// out the issue with getting the block hash from a transaction that is
// supposedly mined.
func tryGetTransaction(c *rpcclient.Client, txh *chainhash.Hash,
maxTries int) (*dcrjson.GetTransactionResult, error) {
numTries := 0
for {
txRes, err := c.GetTransaction(txh)
if err != nil {
if numTries == maxTries {
return nil, err
}
//log.Error("Unable to get transaction for", txh)
numTries++
time.Sleep(300 * time.Millisecond)
continue
}
return txRes, nil
}
}
func tryGetRawTransactionVerbose(c *rpcclient.Client, txh *chainhash.Hash,
maxTries int) (*dcrjson.TxRawResult, error) {
numTries := 0
for {
txRes, err := c.GetRawTransactionVerbose(txh)
if err != nil {
if numTries == maxTries {
return nil, err
}
//log.Error("Unable to get transaction for", txh)
numTries++
time.Sleep(300 * time.Millisecond)
continue
}
return txRes, nil
}
}
// handleReceivingTx should be run as a go routine, and handles notification of
// transactions receiving to a registered address. If no email notification is
// required, emailConf may be a nil pointer. addrs is a map of addresses as
// strings with TxAction values indicating if email should be sent in response
// to transactions involving the keyed address.
func handleReceivingTx(c *rpcclient.Client, addrs map[string]TxAction,
emailConf *EmailConfig, wg *sync.WaitGroup,
quit <-chan struct{}) {
defer wg.Done()
//out:
for {
receive:
select {
// The message with all tx for watched addresses in new block
case blockWatchedTxs, ok := <-spyChans.recvTxBlockChan:
txsByAddr := blockWatchedTxs.TxsForAddress
// map[string][]*dcrutil.Tx is a map of addresses to slices of
// transactions using that address.
if !ok {
log.Infof("Receive-Tx-in-block watch channel closed")
return
}
if len(txsByAddr) == 0 {
break receive
}
// Height is now in the message
height := blockWatchedTxs.BlockHeight
// For each address in map, process each tx
for addr, txs := range txsByAddr {
if len(txs) == 0 {
continue
}
for _, tx := range txs {
txHash := tx.TxHash().String()
// Check the addresses associated with the PkScript of each TxOut
for outID, txOut := range tx.TxOut {
scriptClass, txAddrs, _, err :=
txscript.ExtractPkScriptAddrs(txOut.Version,
txOut.PkScript, activeChain)
if err != nil {
log.Infof("ExtractPkScriptAddrs: %v", err.Error())
// Next TxOut
continue
}
value := dcrutil.Amount(txOut.Value).ToCoin()
// Check if this is a TxOut for the address
for _, txAddr := range txAddrs {
//log.Debug(addr, ", ", txAddr.EncodeAddress())
if addr != txAddr.EncodeAddress() {
// Next address for this TxOut
continue
}
if addrActn, ok := addrs[addr]; ok {
recvString := fmt.Sprintf("Mined in block %d: "+
"%s receiving %.6f DCR, type: %s "+
"(%s[out:%d])",
height, addr, value, scriptClass.String(),
txHash, outID)
log.Infof(recvString)
// Email notification if watchaddress has a
// suffix with the TxMined bit AND emailConf is
// non-nil.
if (addrActn&TxMined) > 0 && emailConf != nil {
EmailMsgChan <- recvString
}
}
}
}
}
}
case tx, ok := <-spyChans.relevantTxMempoolChan:
if !ok {
log.Infof("Receive-Tx watch channel closed")
return
}
// Make like notifyForTxOuts and screen the transactions TxOuts for
// addresses we are watching for.
_, height, err := c.GetBestBlock()
if err != nil {
log.Error("Unable to get best block.")
break
}
// TODO also make this function handle mined tx again, with a
// gettransaction to see if it's in a block
txHash := tx.Hash().String()
// Check the addresses associated with the PkScript of each TxOut
for _, txOut := range tx.MsgTx().TxOut {
_, txAddrs, _, err := txscript.ExtractPkScriptAddrs(txOut.Version,
txOut.PkScript, activeChain)
if err != nil {
log.Infof("ExtractPkScriptAddrs: %v", err.Error())
continue
}
value := dcrutil.Amount(txOut.Value).ToCoin()
// Check if we are watching any address for this TxOut
for _, txAddr := range txAddrs {
addrstr := txAddr.EncodeAddress()
if addrActn, ok := addrs[addrstr]; ok {
recvString := fmt.Sprintf("Inserted into mempool: %s "+
"receiving %.6f, best block: %d (%s)",
addrstr, value, height, txHash)
log.Infof(recvString)
// Email notification if watchaddress has a suffix with
// the TxInserted bit AND we have a non-nil *emailConfig
if (addrActn&TxInserted) > 0 && emailConf != nil {
EmailMsgChan <- recvString
}
continue
}
}
}
case <-quit:
mempoolLog.Debugf("Quitting OnRecvTx handler.")
return
}
}
}
// handleSendingTx is DEAD
// Rather than watching for the sending address, which isn't known ahead of
// time, watch for a transaction with an input (source) whos previous outpoint
// is one of the watched addresses.
// But I am not sure we can do that here with the Tx and BlockDetails provided.
func handleSendingTx(c *rpcclient.Client, addrs map[string]TxAction,
spendTxChan <-chan *watchedAddrTx, wg *sync.WaitGroup,
quit <-chan struct{}) {
defer wg.Done()
//out:
for {
//keepon:
select {
case addrTx, ok := <-spendTxChan:
if !ok {
log.Infof("Send Tx watch channel closed")
return
}
// Unfortunately, can't make like notifyForTxOuts because we are
// not watching outpoints. For the tx we are given, we need to
// search through each TxIn's PreviousOutPoints, requesting the raw
// transaction from each PreviousOutPoint's tx hash, and check each
// TxOut in the result for each watched address. Phew! There is
// surely a better way, but I don't know it.
height, _, err := c.GetBestBlock()
if err != nil {
log.Error("Unable to get best block.")
break
}
tx := addrTx.transaction
var action string
if addrTx.details != nil {
action = fmt.Sprintf("mined into block %d.", height)
} else {
action = "inserted into mempool."
}
log.Debugf("Transaction with watched address as previous outpoint (spending) %s. Hash: %v",
action, tx.Hash().String())
for _, txIn := range tx.MsgTx().TxIn {
prevOut := &txIn.PreviousOutPoint
// uh, now what?
// For each TxIn, we need to check the indicated vout index in the
// txid of the previous outpoint.
//txrr, err := c.GetRawTransactionVerbose(&prevOut.Hash)
Tx, err := c.GetRawTransaction(&prevOut.Hash)
if err != nil {
log.Error("Unable to get raw transaction for", Tx)
continue
}
// prevOut.Index should tell us which one, right? Check all anyway.
wireMsg := Tx.MsgTx()
if wireMsg == nil {
log.Debug("No wire Msg? Hmm.")
continue
}
for _, txOut := range wireMsg.TxOut {
_, txAddrs, _, err := txscript.ExtractPkScriptAddrs(
txOut.Version, txOut.PkScript, activeChain)
if err != nil {
log.Infof("ExtractPkScriptAddrs: %v", err.Error())
continue
}
for _, txAddr := range txAddrs {
addrstr := txAddr.EncodeAddress()
if _, ok := addrs[addrstr]; ok {
log.Infof("Transaction with watched address %v as previous outpoint (spending), value %.6f, %v",
addrstr, dcrutil.Amount(txOut.Value).ToCoin(), action)
continue
}
}
}
// That's not what I'm doing here, but I'm looking anyway...
// log.Debug(txscript.GetScriptClass(txscript.DefaultScriptVersion, txIn.SignatureScript))
// log.Debug(txscript.GetPkScriptFromP2SHSigScript(txIn.SignatureScript))
// sclass, txAddrs, nreqsigs, err := txscript.ExtractPkScriptAddrs(txscript.DefaultScriptVersion, txIn.SignatureScript, activeChain)
// log.Debug(sclass, txAddrs, nreqsigs, err, action)
// addresses := make([]string, len(txAddrs))
// for i, addr := range txAddrs {
// addresses[i] = addr.EncodeAddress()
// }
// log.Debug(addresses)
}
case <-quit:
mempoolLog.Debugf("Quitting OnRedeemingTx handler.")
return
}
}
}
type watchedAddrTx struct {
transaction *dcrutil.Tx
details *int
}