B++ Logo

Blockchain Monitoring

Real-time blockchain monitoring allows you to detect new blocks instantly, track mining pools, analyze transactions, and monitor network activity.

Explore the blockchain live! Try RPC commands like getblockchaininfo and getmempoolinfo in the Bitcoin CLI Terminal.

ZMQ Notifications

ZeroMQ (ZMQ) provides real-time notifications for blockchain events without polling. It's much more efficient than repeatedly calling RPC commands.

Benefits

  • Instant notifications: No polling delays
  • Lower resource usage: No constant RPC calls
  • Better reliability: Catches blocks even after node restarts
  • Real-time monitoring: Perfect for applications

Configuration

Add to your bitcoin.conf:

# ZMQ Notifications
zmqpubhashblock=tcp://127.0.0.1:28332
zmqpubhashtx=tcp://127.0.0.1:28333
zmqpubrawblock=tcp://127.0.0.1:28334
zmqpubrawtx=tcp://127.0.0.1:28335

Important: Bitcoin Core must be built with ZMQ enabled.

Verification

# Check if ZMQ is enabled in Bitcoin logs
grep -i zmq ~/.bitcoin/debug.log

# Check Bitcoin help for ZMQ options
bitcoind -h | grep zmq

Block Detection

Real-Time Block Monitoring with ZMQ

Using Polling (Fallback)

Block Information

Get Block Details:

# Get block by hash
block = rpc.getblock(block_hash)

# Get block by height
block_hash = rpc.getblockhash(height)
block = rpc.getblock(block_hash)

Key Information:

  • Block height
  • Block hash
  • Previous block hash
  • Merkle root
  • Timestamp
  • Transaction count
  • Block size

Mining Pool Identification

Coinbase Transaction Analysis

Mining pools often embed their name or identifier in the coinbase transaction.

Extract Pool Information:

def identify_pool(block):
    coinbase_tx = block['tx'][0]
    coinbase_hex = rpc.getrawtransaction(coinbase_tx)
    
    # Parse coinbase script
    # Look for pool identifiers
    # Common patterns:
    # - Pool names in ASCII
    # - Pool URLs
    # - Pool signatures
    
    return pool_name

OP_RETURN Analysis

Extracting OP_RETURN Data

Use Cases

  • Timestamping: Document timestamps
  • Asset Protocols: Counterparty, Omni Layer
  • Messages: Encoded messages
  • Metadata: Transaction metadata

Transaction Monitoring

Mempool Monitoring with ZMQ

Transaction Analysis


Statistics and Logging

Block Statistics

Track Block Metrics:

def log_block_stats(block):
    stats = {
        'height': block['height'],
        'hash': block['hash'],
        'tx_count': len(block['tx']),
        'size': block['size'],
        'pool': identify_pool(block),
        'timestamp': block['time']
    }
    
    # Log to CSV or database
    log_to_csv(stats)

CSV Logging

Log to CSV:

import csv

def log_to_csv(stats):
    with open('blocks.csv', 'a') as f:
        writer = csv.DictWriter(f, fieldnames=stats.keys())
        writer.writerow(stats)

Error Handling

ZMQ Connection Failures

Handle ZMQ Errors:

try:
    socket = context.socket(zmq.SUB)
    socket.connect("tcp://127.0.0.1:28332")
except zmq.ZMQError as e:
    print(f"ZMQ connection failed: {e}")
    # Fall back to polling
    use_polling_fallback()

RPC Failures

Handle RPC Errors:

try:
    block = rpc.getblock(block_hash)
except Exception as e:
    print(f"RPC error: {e}")
    # Retry or log error

Best Practices

For Monitoring Applications

  1. Use ZMQ First: Try ZMQ, fall back to polling
  2. Error Handling: Handle all error cases
  3. Logging: Log important events
  4. Rate Limiting: Don't overwhelm RPC
  5. Caching: Cache frequently accessed data

For Performance

  1. Async Operations: Use async/await for I/O
  2. Batch Operations: Batch RPC calls when possible
  3. Connection Pooling: Reuse connections
  4. Efficient Parsing: Parse only needed data