/
bitcoind_zmq_events.go
430 lines (363 loc) · 11.7 KB
/
bitcoind_zmq_events.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
package chain
import (
"bytes"
"fmt"
"io"
"math/rand"
"net"
"sync"
"time"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/rpcclient"
"github.com/btcsuite/btcd/wire"
"github.com/lightninglabs/gozmq"
)
// ZMQConfig holds all the config values needed to set up a ZMQ connection to
// bitcoind.
type ZMQConfig struct {
// ZMQBlockHost is the IP address and port of the bitcoind's rawblock
// listener.
ZMQBlockHost string
// ZMQTxHost is the IP address and port of the bitcoind's rawtx
// listener.
ZMQTxHost string
// ZMQReadDeadline represents the read deadline we'll apply when reading
// ZMQ messages from either subscription.
ZMQReadDeadline time.Duration
// MempoolPollingInterval is the interval that will be used to poll
// bitcoind to update the local mempool. If a jitter factor is
// configed, it will be
// applied to this value to provide randomness in the range,
// - max: MempoolPollingInterval * (1 + PollingIntervalJitter)
// - min: MempoolPollingInterval * (1 - PollingIntervalJitter)
//
// TODO(yy): replace this temp config with SEQUENCE check.
MempoolPollingInterval time.Duration
// PollingIntervalJitter a factor that's used to simulates jitter by
// scaling MempoolPollingInterval with it. This value must be no less
// than 0. Default to 0, meaning no jitter will be applied.
//
// TODO(yy): replace this temp config with SEQUENCE check.
PollingIntervalJitter float64
}
// bitcoindZMQEvents delivers block and transaction notifications that it gets
// from ZMQ connections to bitcoind.
type bitcoindZMQEvents struct {
cfg *ZMQConfig
// blockConn is the ZMQ connection we'll use to read raw block events.
blockConn *gozmq.Conn
// txConn is the ZMQ connection we'll use to read raw transaction
// events.
txConn *gozmq.Conn
// blockNtfns is a channel to which any new blocks will be sent.
blockNtfns chan *wire.MsgBlock
// txNtfns is a channel to which any new transactions will be sent.
txNtfns chan *wire.MsgTx
// mempool holds all the transactions that we currently see as being in
// the mempool. This is used so that we know which transactions we have
// already sent notifications for.
mempool *mempool
wg sync.WaitGroup
quit chan struct{}
}
// Ensure bitcoindZMQEvent implements the BitcoinEvents interface at compile
// time.
var _ BitcoindEvents = (*bitcoindZMQEvents)(nil)
// newBitcoindZMQEvents initialises the necessary zmq connections to bitcoind.
func newBitcoindZMQEvents(cfg *ZMQConfig,
client *rpcclient.Client) (*bitcoindZMQEvents, error) {
// Check polling config.
if cfg.MempoolPollingInterval == 0 {
cfg.MempoolPollingInterval = defaultTxPollInterval
}
// Floor the jitter value to be 0.
if cfg.PollingIntervalJitter < 0 {
log.Warnf("Jitter value(%v) must be positive, setting to 0",
cfg.PollingIntervalJitter)
cfg.PollingIntervalJitter = 0
}
// Establish two different ZMQ connections to bitcoind to retrieve block
// and transaction event notifications. We'll use two as a separation of
// concern to ensure one type of event isn't dropped from the connection
// queue due to another type of event filling it up.
zmqBlockConn, err := gozmq.Subscribe(
cfg.ZMQBlockHost, []string{rawBlockZMQCommand},
cfg.ZMQReadDeadline,
)
if err != nil {
return nil, fmt.Errorf("unable to subscribe for zmq block "+
"events: %v", err)
}
zmqTxConn, err := gozmq.Subscribe(
cfg.ZMQTxHost, []string{rawTxZMQCommand}, cfg.ZMQReadDeadline,
)
if err != nil {
// Ensure that the block zmq connection is closed in the case
// that it succeeded but the tx zmq connection failed.
if err := zmqBlockConn.Close(); err != nil {
log.Errorf("could not close zmq block conn: %v", err)
}
return nil, fmt.Errorf("unable to subscribe for zmq tx "+
"events: %v", err)
}
return &bitcoindZMQEvents{
cfg: cfg,
blockConn: zmqBlockConn,
txConn: zmqTxConn,
blockNtfns: make(chan *wire.MsgBlock),
txNtfns: make(chan *wire.MsgTx),
mempool: newMempool(client),
quit: make(chan struct{}),
}, nil
}
// Start spins off the bitcoindZMQEvent goroutines.
func (b *bitcoindZMQEvents) Start() error {
// Load the mempool so we don't miss transactions.
if err := b.mempool.LoadMempool(); err != nil {
return err
}
b.wg.Add(3)
go b.blockEventHandler()
go b.txEventHandler()
go b.mempoolPoller()
return nil
}
// Stop cleans up any of the resources and goroutines held by bitcoindZMQEvents.
func (b *bitcoindZMQEvents) Stop() error {
var returnErr error
if err := b.txConn.Close(); err != nil {
returnErr = err
}
if err := b.blockConn.Close(); err != nil {
returnErr = err
}
close(b.quit)
b.wg.Wait()
return returnErr
}
// TxNotifications returns a channel which will deliver new transactions.
func (b *bitcoindZMQEvents) TxNotifications() <-chan *wire.MsgTx {
return b.txNtfns
}
// BlockNotifications returns a channel which will deliver new blocks.
func (b *bitcoindZMQEvents) BlockNotifications() <-chan *wire.MsgBlock {
return b.blockNtfns
}
// LookupInputSpend returns the transaction that spends the given outpoint
// found in the mempool.
func (b *bitcoindZMQEvents) LookupInputSpend(
op wire.OutPoint) (chainhash.Hash, bool) {
b.mempool.RLock()
defer b.mempool.RUnlock()
// Check whether the input is in mempool.
return b.mempool.containsInput(op)
}
// blockEventHandler reads raw blocks events from the ZMQ block socket and
// forwards them along to the current rescan clients.
//
// NOTE: This must be run as a goroutine.
func (b *bitcoindZMQEvents) blockEventHandler() {
defer b.wg.Done()
log.Info("Started listening for bitcoind block notifications via ZMQ "+
"on", b.blockConn.RemoteAddr())
// Set up the buffers we expect our messages to consume. ZMQ
// messages from bitcoind include three parts: the command, the
// data, and the sequence number.
//
// We'll allocate a fixed data slice that we'll reuse when reading
// blocks from bitcoind through ZMQ. There's no need to recycle this
// slice (zero out) after using it, as further reads will overwrite the
// slice and we'll only be deserializing the bytes needed.
var (
command [len(rawBlockZMQCommand)]byte
seqNum [seqNumLen]byte
data = make([]byte, maxRawBlockSize)
)
for {
// Before attempting to read from the ZMQ socket, we'll make
// sure to check if we've been requested to shut down.
select {
case <-b.quit:
return
default:
}
// Poll an event from the ZMQ socket.
var (
bufs = [][]byte{command[:], data, seqNum[:]}
err error
)
bufs, err = b.blockConn.Receive(bufs)
if err != nil {
// EOF should only be returned if the connection was
// explicitly closed, so we can exit at this point.
if err == io.EOF {
return
}
// It's possible that the connection to the socket
// continuously times out, so we'll prevent logging this
// error to prevent spamming the logs.
netErr, ok := err.(net.Error)
if ok && netErr.Timeout() {
log.Trace("Re-establishing timed out ZMQ " +
"block connection")
continue
}
log.Errorf("Unable to receive ZMQ %v message: %v",
rawBlockZMQCommand, err)
continue
}
// We have an event! We'll now ensure it is a block event,
// deserialize it, and report it to the different rescan
// clients.
eventType := string(bufs[0])
switch eventType {
case rawBlockZMQCommand:
block := &wire.MsgBlock{}
r := bytes.NewReader(bufs[1])
if err := block.Deserialize(r); err != nil {
log.Errorf("Unable to deserialize block: %v",
err)
continue
}
select {
case b.blockNtfns <- block:
case <-b.quit:
return
}
default:
// It's possible that the message wasn't fully read if
// bitcoind shuts down, which will produce an unreadable
// event type. To prevent from logging it, we'll make
// sure it conforms to the ASCII standard.
if eventType == "" || !isASCII(eventType) {
continue
}
log.Warnf("Received unexpected event type from %v "+
"subscription: %v", rawBlockZMQCommand,
eventType)
}
}
}
// txEventHandler reads raw blocks events from the ZMQ block socket and forwards
// them along to the current rescan clients.
//
// NOTE: This must be run as a goroutine.
func (b *bitcoindZMQEvents) txEventHandler() {
defer b.wg.Done()
log.Info("Started listening for bitcoind transaction notifications "+
"via ZMQ on", b.txConn.RemoteAddr())
// Set up the buffers we expect our messages to consume. ZMQ
// messages from bitcoind include three parts: the command, the
// data, and the sequence number.
//
// We'll allocate a fixed data slice that we'll reuse when reading
// transactions from bitcoind through ZMQ. There's no need to recycle
// this slice (zero out) after using it, as further reads will overwrite
// the slice and we'll only be deserializing the bytes needed.
var (
command [len(rawTxZMQCommand)]byte
seqNum [seqNumLen]byte
data = make([]byte, maxRawTxSize)
)
for {
// Before attempting to read from the ZMQ socket, we'll make
// sure to check if we've been requested to shut down.
select {
case <-b.quit:
return
default:
}
// Poll an event from the ZMQ socket.
var (
bufs = [][]byte{command[:], data, seqNum[:]}
err error
)
bufs, err = b.txConn.Receive(bufs)
if err != nil {
// EOF should only be returned if the connection was
// explicitly closed, so we can exit at this point.
if err == io.EOF {
return
}
// It's possible that the connection to the socket
// continuously times out, so we'll prevent logging this
// error to prevent spamming the logs.
netErr, ok := err.(net.Error)
if ok && netErr.Timeout() {
log.Trace("Re-establishing timed out ZMQ " +
"transaction connection")
continue
}
log.Errorf("Unable to receive ZMQ %v message: %v",
rawTxZMQCommand, err)
continue
}
// We have an event! We'll now ensure it is a transaction event,
// deserialize it, and report it to the different rescan
// clients.
eventType := string(bufs[0])
switch eventType {
case rawTxZMQCommand:
tx := &wire.MsgTx{}
r := bytes.NewReader(bufs[1])
if err := tx.Deserialize(r); err != nil {
log.Errorf("Unable to deserialize "+
"transaction: %v", err)
continue
}
// Add the tx to mempool.
b.mempool.Add(tx)
select {
case b.txNtfns <- tx:
case <-b.quit:
return
}
default:
// It's possible that the message wasn't fully read if
// bitcoind shuts down, which will produce an unreadable
// event type. To prevent from logging it, we'll make
// sure it conforms to the ASCII standard.
if eventType == "" || !isASCII(eventType) {
continue
}
log.Warnf("Received unexpected event type from %v "+
"subscription: %v", rawTxZMQCommand, eventType)
}
}
}
// NOTE: This must be run as a goroutine.
func (b *bitcoindZMQEvents) mempoolPoller() {
defer b.wg.Done()
// We'll wait to start the main reconciliation loop until we're doing
// the initial mempool load.
b.mempool.WaitForInit()
log.Info("Started polling mempool to cache new transactions")
// Create a ticker that fires randomly.
rand.Seed(time.Now().UnixNano())
ticker := NewJitterTicker(
b.cfg.MempoolPollingInterval, b.cfg.PollingIntervalJitter,
)
defer ticker.Stop()
for {
select {
case <-ticker.C:
log.Debugf("Reconciling mempool spends with node " +
"mempool...")
now := time.Now()
// After each ticker interval, we poll the mempool to
// check for transactions we haven't seen yet.
txs, err := b.mempool.client.GetRawMempool()
if err != nil {
log.Errorf("Unable to retrieve mempool txs: "+
"%v", err)
continue
}
// Update our local mempool with the new mempool.
b.mempool.UpdateMempoolTxes(txs)
log.Debugf("Reconciled mempool spends in %v",
time.Since(now))
case <-b.quit:
return
}
}
}