forked from chappjc/dcrspy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathntfnhandlers.go
159 lines (147 loc) · 4.87 KB
/
ntfnhandlers.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
package main
import (
"bytes"
"os/exec"
"strconv"
"strings"
"time"
"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrd/dcrutil"
"github.com/decred/dcrd/rpcclient"
"github.com/decred/dcrd/wire"
"github.com/decred/dcrwallet/wallet/udb"
)
// Arbitrary command execution
// cfg.CmdName // e.g. "ping"
// cfg.CmdArgs // e.g. "127.0.0.1,-n-8"
// Define notification handlers
func getNodeNtfnHandlers(cfg *config) *rpcclient.NotificationHandlers {
return &rpcclient.NotificationHandlers{
OnBlockConnected: func(blockHeaderSerialized []byte, transactions [][]byte) {
// OnBlockConnected: func(hash *chainhash.Hash, height int32,
// time time.Time, vb uint16) {
blockHeader := new(wire.BlockHeader)
err := blockHeader.FromBytes(blockHeaderSerialized)
if err != nil {
log.Error("Failed to serialize blockHeader in new block notification.")
}
height := int32(blockHeader.Height)
hash := blockHeader.BlockHash()
select {
case spyChans.connectChan <- &hash:
// Past this point in this case is command execution. Block
// height was sent on connectChan, so move on if no command.
cmdName := cfg.CmdName
if len(cmdName) == 0 {
break
}
// replace %h and %n with hash and block height, resp.
rep := strings.NewReplacer("%h", hash.String(), "%n",
strconv.Itoa(int(height)))
var argSubst bytes.Buffer
rep.WriteString(&argSubst, cfg.CmdArgs)
// Split the argument string by comma
argsSplit := strings.Split(argSubst.String(), ",")
// Create command, with substituted args
cmd := exec.Command(cmdName, argsSplit...)
// Get a pipe for stdout
outpipe, err := cmd.StdoutPipe()
if err != nil {
log.Critical(err)
}
// Send stderr to the same place
cmd.Stderr = cmd.Stdout
// Display the full command being executed
execLog.Debugf("Full command line to be executed: %s %s",
cmd.Path, strings.Join(argsSplit, " "))
// Channel for logger and command execution routines to talk
cmdDone := make(chan error)
go execLogger(outpipe, cmdDone)
// Start command and return from handler without waiting
go func() {
if err := cmd.Run(); err != nil {
execLog.Errorf("Failed to start system command %v. Error: %v",
cmdName, err)
}
// Signal the logger goroutine, and clean up
cmdDone <- err
close(cmdDone)
}()
// send to nil channel blocks
default:
}
// Also send on stake info channel, if enabled.
select {
case spyChans.connectChanStkInf <- height:
// send to nil channel blocks
default:
}
},
// Not too useful since this notifies on every block
OnStakeDifficulty: func(hash *chainhash.Hash, height int64,
stakeDiff int64) {
select {
case spyChans.stakeDiffChan <- stakeDiff:
default:
}
},
// TODO
OnWinningTickets: func(blockHash *chainhash.Hash, blockHeight int64,
tickets []*chainhash.Hash) {
var txstr []string
for _, t := range tickets {
txstr = append(txstr, t.String())
}
log.Debugf("Winning tickets: %v", strings.Join(txstr, ", "))
},
// maturing tickets
// BUG: rpcclient/notify.go (parseNewTicketsNtfnParams) is unable to
// Unmarshal fourth parameter as a map[hash]hash.
OnNewTickets: func(hash *chainhash.Hash, height int64, stakeDiff int64,
tickets []*chainhash.Hash) {
for _, tick := range tickets {
log.Debugf("Mined new ticket: %v", tick.String())
}
},
// OnRelevantTxAccepted is invoked when a transaction containing a
// registered address is inserted into mempool.
OnRelevantTxAccepted: func(transaction []byte) {
rec, err := udb.NewTxRecord(transaction, time.Now())
if err != nil {
return
}
tx := dcrutil.NewTx(&rec.MsgTx)
txHash := rec.Hash
select {
case spyChans.relevantTxMempoolChan <- tx:
log.Debugf("Detected transaction %v in mempool containing registered address.",
txHash.String())
default:
}
},
// OnTxAccepted is invoked when a transaction is accepted into the
// memory pool. It will only be invoked if a preceding call to
// NotifyNewTransactions with the verbose flag set to false has been
// made to register for the notification and the function is non-nil.
OnTxAccepted: func(hash *chainhash.Hash, amount dcrutil.Amount) {
// Just send the tx hash and let the goroutine handle everything.
select {
case spyChans.newTxChan <- hash:
default:
}
//log.Trace("Transaction accepted to mempool: ", hash, amount)
},
// Note: dcrjson.TxRawResult is from getrawtransaction
//OnTxAcceptedVerbose: func(txDetails *dcrjson.TxRawResult) {
//txDetails.Hex
//log.Info("Transaction accepted to mempool: ", txDetails.Txid)
//},
}
}
func getWalletNtfnHandlers(cfg *config) *rpcclient.NotificationHandlers {
return &rpcclient.NotificationHandlers{
OnAccountBalance: func(account string, balance dcrutil.Amount, confirmed bool) {
log.Debug("OnAccountBalance")
},
}
}