Decode Stratum pool control notifications
next-build / build (push) Canceled after 0s

This commit is contained in:
2026-10-15 00:49:35 -06:00
parent e0f9b1ab50
commit e7a23faa5a
3 changed files with 198 additions and 8 deletions
+110 -6
View File
@@ -2,6 +2,7 @@
//! Bounded decoding of Bitcoin Stratum V1 subscription and job messages.
//! The network client must enforce a line limit before passing data here.
use crate::pow::VersionMask;
use crate::stratum_work::{JobError, Sha256Job};
use serde_json::Value;
@@ -12,16 +13,25 @@ pub enum PoolMessageError {
TooLarge,
Json,
WrongMessage,
Unnegotiated,
InvalidField,
InvalidHex,
InvalidWork(JobError),
}
#[derive(Debug, Eq, PartialEq)]
pub struct Subscription {
pub extranonce1: Vec<u8>,
pub extranonce2_size: usize,
}
#[derive(Debug, PartialEq)]
pub enum PoolNotification {
SetDifficulty(f64),
SetExtranonce(Subscription),
SetVersionMask(VersionMask),
}
pub struct PoolJob {
pub id: String,
pub clean_jobs: bool,
@@ -71,6 +81,19 @@ fn hex_u32(field: &Value) -> Result<u32, PoolMessageError> {
u32::from_str_radix(text, 16).map_err(|_| PoolMessageError::InvalidHex)
}
fn valid_subscription(
extranonce1: Vec<u8>,
extranonce2_size: u64,
) -> Result<Subscription, PoolMessageError> {
if extranonce1.len() > 32 || extranonce2_size > 32 {
return Err(PoolMessageError::InvalidField);
}
Ok(Subscription {
extranonce1,
extranonce2_size: extranonce2_size as usize,
})
}
/// Parse the response to a matching `mining.subscribe` request.
pub fn parse_subscription(line: &[u8], request_id: u64) -> Result<Subscription, PoolMessageError> {
let message = parse_json(line)?;
@@ -88,13 +111,94 @@ pub fn parse_subscription(line: &[u8], request_id: u64) -> Result<Subscription,
}
let extranonce1 = hex_bytes(result[1].as_str().ok_or(PoolMessageError::InvalidField)?)?;
let extranonce2_size = result[2].as_u64().ok_or(PoolMessageError::InvalidField)?;
if extranonce1.len() > 32 || extranonce2_size > 32 {
return Err(PoolMessageError::InvalidField);
valid_subscription(extranonce1, extranonce2_size)
}
/// Decode the response to a BIP310 configure request that offered version
/// rolling. A pool mask outside this ASIC's changeable bits is unusable: the
/// pool would reconstruct a different block version for submitted shares.
pub fn parse_configure_response(
line: &[u8],
request_id: u64,
) -> Result<Option<VersionMask>, PoolMessageError> {
let message = parse_json(line)?;
if message.get("id").and_then(Value::as_u64) != Some(request_id)
|| !message.get("error").is_none_or(Value::is_null)
{
return Err(PoolMessageError::WrongMessage);
}
let result = message
.get("result")
.and_then(Value::as_object)
.ok_or(PoolMessageError::InvalidField)?;
match result.get("version-rolling") {
Some(Value::Bool(false)) => Ok(None),
Some(Value::Bool(true)) => {
let mask = hex_u32(
result
.get("version-rolling.mask")
.ok_or(PoolMessageError::InvalidField)?,
)?;
VersionMask::new(mask)
.map(Some)
.ok_or(PoolMessageError::InvalidField)
}
_ => Err(PoolMessageError::InvalidField),
}
}
/// Decode pool control notifications. The caller applies them in stream order:
/// difficulty to the next job, extranonce to subsequent work, and the version
/// mask immediately to all pending submissions.
pub fn parse_control_notification(
line: &[u8],
version_rolling: bool,
subscribe_extranonce: bool,
) -> Result<PoolNotification, PoolMessageError> {
let message = parse_json(line)?;
if !message.get("id").is_none_or(Value::is_null) {
return Err(PoolMessageError::WrongMessage);
}
let method = message
.get("method")
.and_then(Value::as_str)
.ok_or(PoolMessageError::WrongMessage)?;
let params = message
.get("params")
.and_then(Value::as_array)
.ok_or(PoolMessageError::InvalidField)?;
match method {
"mining.set_difficulty" if params.len() == 1 => {
let difficulty = params[0].as_f64().ok_or(PoolMessageError::InvalidField)?;
if !difficulty.is_finite() || difficulty <= 0.0 {
return Err(PoolMessageError::InvalidField);
}
Ok(PoolNotification::SetDifficulty(difficulty))
}
"mining.set_version_mask" if params.len() == 1 => {
if !version_rolling {
return Err(PoolMessageError::Unnegotiated);
}
let mask = hex_u32(&params[0])?;
let mask = VersionMask::new(mask).ok_or(PoolMessageError::InvalidField)?;
Ok(PoolNotification::SetVersionMask(mask))
}
"mining.set_extranonce" if params.len() == 2 => {
if !subscribe_extranonce {
return Err(PoolMessageError::Unnegotiated);
}
let extranonce1 = hex_bytes(params[0].as_str().ok_or(PoolMessageError::InvalidField)?)?;
let size = params[1].as_u64().ok_or(PoolMessageError::InvalidField)?;
Ok(PoolNotification::SetExtranonce(valid_subscription(
extranonce1,
size,
)?))
}
"mining.set_difficulty" | "mining.set_version_mask" | "mining.set_extranonce" => {
Err(PoolMessageError::InvalidField)
}
_ => Err(PoolMessageError::WrongMessage),
}
Ok(Subscription {
extranonce1,
extranonce2_size: extranonce2_size as usize,
})
}
/// Parse exactly the nine Bitcoin `mining.notify` parameters into work.