Stratum V2 mining protocol module for blvm-node.
This module implements a complete Stratum V2 mining protocol server for blvm-node. It provides:
- Stratum V2 Server: Full protocol implementation for miner connections
- Mining Pool Management: Miner registration, channel management, and job distribution
- Share Validation: Proof-of-work validation using the consensus layer
- Block Template Generation: Integration with node's block template generation
- Automatic Block Submission: Submits valid blocks to the node when found
The module integrates with blvm-node through the NodeAPI interface:
┌─────────────────┐
│ blvm-node │
│ (Core Node) │
└────────┬────────┘
│ NodeAPI
│ (get_block_template, submit_block)
│
▼
┌─────────────────┐
│ blvm-stratum-v2 │
│ (Module) │
│ │
│ ┌─────────────┐ │
│ │ SV2 Server │ │◄─── Stratum V2 Miners
│ └─────────────┘ │ (Encrypted Protocol)
└─────────────────┘
- Message Format: Binary encoding (bincode) with TLV framing (tag + length + payload)
- Setup Connection: Protocol version negotiation and capability exchange
- Mining Channels: Multiple channels per miner with configurable difficulty
- Job Distribution: Automatic job distribution to all active channels
- Share Submission: Share validation with PoW checking
- Error Handling: Comprehensive error responses with proper error codes
- Block Template Generation: Uses node's consensus-verified template generation
- Merkle Path Calculation: Proper Stratum V2 merkle path from coinbase to root
- Coinbase Splitting: Splits coinbase at correct point for miner customization
- Share Validation: Validates shares against channel targets and network difficulty
- Block Detection: Automatically detects and submits valid blocks
- Miner Registration: Tracks connected miners and their channels
- Job Management: Maintains job history with automatic cleanup
- Connection Management: Automatic cleanup of disconnected miners
- Statistics: Tracks shares, acceptance rates, and miner activity
The module is part of the blvm-node module system. It is loaded automatically when configured in the node's module configuration.
Pin in node blvm.toml:
[modules]
blvm-stratum-v2 = "0.1.*"Module config.toml (flat keys at <modules.data_dir>/blvm-stratum-v2/config.toml):
listen_addr = "0.0.0.0:3333"
difficulty_target = 1
max_connections = 100Node overrides: [modules.blvm-stratum-v2] with the same keys (MODULE_CONFIG_* on spawn). There is no module enabled key — load via the [modules] pin or loadmodule.
listen_addr(default:0.0.0.0:3333) — miner TCP bind (module-owned)difficulty_target(default:1) — default channel difficultymax_connections(default:100)pool_name,extra_extranonce— optional
The node does not bind miner TCP. [stratum_v2] in blvm.toml controls P2P Stratum TLV demux (p2p_stratum_demux, default true) and merge-mining-related fields — see blvm-docs Stratum V2 + Merge Mining.
See module.toml in this repo and registry/modules.json.
name = "blvm-stratum-v2"
description = "Stratum V2 mining protocol module"
entry_point = "blvm-stratum-v2"
capabilities = [
"read_blockchain",
"read_chain_state",
"subscribe_events",
"submit_block",
"call_module",
"discover_modules",
"publish_events",
]- SetupConnection: Initial connection setup with protocol version and capabilities
- OpenMiningChannel: Request to open a mining channel with minimum difficulty
- SubmitShares: Submit mining shares for validation
- SetupConnectionSuccess: Connection established successfully
- SetupConnectionError: Connection failed with error code and message
- OpenMiningChannelSuccess: Channel opened with target and max jobs
- OpenMiningChannelError: Channel open failed with error details
- NewMiningJob: New mining job with coinbase parts and merkle path
- SetNewPrevHash: Previous block hash update (chain reorganization)
- SubmitSharesSuccess: Shares accepted with last job ID
- SubmitSharesError: Shares rejected with error code and message
BlockMined,BlockTemplateUpdated,MiningDifficultyChangedMiningJobCreated,ShareSubmitted(coordination with other modules)StratumV2MessageReceived(P2P Stratum TLV when node feature enabled)
StratumClientConnected,StratumClientDisconnectedShareSubmitted(valid share accepted)
Shares are validated using a two-stage process:
- Channel Target Validation: Checks if share meets the channel's target difficulty (easier than network difficulty)
- Network Difficulty Validation: Checks if share meets network difficulty (valid block)
Shares that pass channel validation are accepted. Shares that also pass network validation trigger automatic block submission.
- Each channel maintains a history of active jobs (configurable
max_jobs, default: 10) - Old jobs are automatically cleaned up when the limit is exceeded
- Jobs are distributed to all active channels when a new block template is available
- Miners are automatically registered on successful connection setup
- Channels are opened per miner request with configurable difficulty
- Disconnected miners (no activity for 5+ minutes) are automatically removed
- Periodic cleanup runs every 5 minutes
All protocol errors return appropriate error messages:
- Error Code 1: Setup connection failed (unsupported protocol version, etc.)
- Error Code 2: Channel open failed (miner not registered, invalid parameters)
- Error Code 3: Share submission failed (invalid job, channel not found, etc.)
blvm-node: Module system and NodeAPI integrationblvm-consensus: Proof-of-work validationblvm-protocol: Protocol types and structurestokio: Async runtimeserde: Message serializationsha2: Cryptographic hashing
The module includes comprehensive test coverage:
- Unit tests for all core components
- Integration tests for protocol flows
- Concurrent operation tests for thread safety
- Error handling tests for edge cases
See TEST_COVERAGE.md for detailed test coverage information.
MIT License - see LICENSE file for details.