chain: extract ZMQ command strings into constants

This commit is contained in:
Wilmer Paulino 2019-11-08 12:38:28 -08:00
parent 7abdd4f8ad
commit 36ca842905
No known key found for this signature in database
GPG key ID: 6DF57B9F9514972F

View file

@ -16,6 +16,16 @@ import (
"github.com/lightninglabs/gozmq" "github.com/lightninglabs/gozmq"
) )
const (
// rawBlockZMQCommand is the command used to receive raw block
// notifications from bitcoind through ZMQ.
rawBlockZMQCommand = "rawblock"
// rawTxZMQCommand is the command used to receive raw transaction
// notifications from bitcoind through ZMQ.
rawTxZMQCommand = "rawtx"
)
// BitcoindConn represents a persistent client connection to a bitcoind node // BitcoindConn represents a persistent client connection to a bitcoind node
// that listens for events read from a ZMQ connection. // that listens for events read from a ZMQ connection.
type BitcoindConn struct { type BitcoindConn struct {
@ -79,7 +89,7 @@ func NewBitcoindConn(chainParams *chaincfg.Params,
// concern to ensure one type of event isn't dropped from the connection // concern to ensure one type of event isn't dropped from the connection
// queue due to another type of event filling it up. // queue due to another type of event filling it up.
zmqBlockConn, err := gozmq.Subscribe( zmqBlockConn, err := gozmq.Subscribe(
zmqBlockHost, []string{"rawblock"}, zmqPollInterval, zmqBlockHost, []string{rawBlockZMQCommand}, zmqPollInterval,
) )
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to subscribe for zmq block "+ return nil, fmt.Errorf("unable to subscribe for zmq block "+
@ -87,7 +97,7 @@ func NewBitcoindConn(chainParams *chaincfg.Params,
} }
zmqTxConn, err := gozmq.Subscribe( zmqTxConn, err := gozmq.Subscribe(
zmqTxHost, []string{"rawtx"}, zmqPollInterval, zmqTxHost, []string{rawTxZMQCommand}, zmqPollInterval,
) )
if err != nil { if err != nil {
zmqBlockConn.Close() zmqBlockConn.Close()
@ -190,8 +200,8 @@ func (c *BitcoindConn) blockEventHandler() {
continue continue
} }
log.Errorf("Unable to receive ZMQ rawblock message: %v", log.Errorf("Unable to receive ZMQ %v message: %v",
err) rawBlockZMQCommand, err)
continue continue
} }
@ -200,7 +210,7 @@ func (c *BitcoindConn) blockEventHandler() {
// clients. // clients.
eventType := string(msgBytes[0]) eventType := string(msgBytes[0])
switch eventType { switch eventType {
case "rawblock": case rawBlockZMQCommand:
block := &wire.MsgBlock{} block := &wire.MsgBlock{}
r := bytes.NewReader(msgBytes[1]) r := bytes.NewReader(msgBytes[1])
if err := block.Deserialize(r); err != nil { if err := block.Deserialize(r); err != nil {
@ -229,8 +239,9 @@ func (c *BitcoindConn) blockEventHandler() {
continue continue
} }
log.Warnf("Received unexpected event type from "+ log.Warnf("Received unexpected event type from %v "+
"rawblock subscription: %v", eventType) "subscription: %v", rawBlockZMQCommand,
eventType)
} }
} }
} }
@ -271,8 +282,8 @@ func (c *BitcoindConn) txEventHandler() {
continue continue
} }
log.Errorf("Unable to receive ZMQ rawtx message: %v", log.Errorf("Unable to receive ZMQ %v message: %v",
err) rawTxZMQCommand, err)
continue continue
} }
@ -281,7 +292,7 @@ func (c *BitcoindConn) txEventHandler() {
// clients. // clients.
eventType := string(msgBytes[0]) eventType := string(msgBytes[0])
switch eventType { switch eventType {
case "rawtx": case rawTxZMQCommand:
tx := &wire.MsgTx{} tx := &wire.MsgTx{}
r := bytes.NewReader(msgBytes[1]) r := bytes.NewReader(msgBytes[1])
if err := tx.Deserialize(r); err != nil { if err := tx.Deserialize(r); err != nil {
@ -310,8 +321,8 @@ func (c *BitcoindConn) txEventHandler() {
continue continue
} }
log.Warnf("Received unexpected event type from rawtx "+ log.Warnf("Received unexpected event type from %v "+
"subscription: %v", eventType) "subscription: %v", rawTxZMQCommand, eventType)
} }
} }
} }