This commit is contained in:
+110
-6
@@ -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(¶ms[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.
|
||||
|
||||
+10
-1
@@ -3,7 +3,7 @@ use alkane_os::miner::Bm1366Miner;
|
||||
use alkane_os::pow::verify_sha256_report;
|
||||
use alkane_os::pow::VersionMask;
|
||||
use alkane_os::stratum_submit::{format_submission, Submission};
|
||||
use alkane_os::stratum_v1::{parse_notify, Subscription};
|
||||
use alkane_os::stratum_v1::{parse_control_notification, parse_notify, Subscription};
|
||||
use alkane_os::stratum_work::Sha256Job;
|
||||
use alkane_os::{power_decision, target_matrix, HardwareBackend};
|
||||
use std::hint::black_box;
|
||||
@@ -136,6 +136,15 @@ fn bench_stratum_notify_parse() {
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "microbenchmark"]
|
||||
fn bench_stratum_control_parse() {
|
||||
let line = br#"{"id":null,"method":"mining.set_difficulty","params":[2048.5]}"#;
|
||||
measure("stratum_control_parse", 20_000, || {
|
||||
black_box(parse_control_notification(black_box(line), false, false).unwrap());
|
||||
});
|
||||
}
|
||||
|
||||
struct BenchBackend;
|
||||
|
||||
impl HardwareBackend for BenchBackend {
|
||||
|
||||
+78
-1
@@ -1,5 +1,8 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
use alkane_os::stratum_v1::{parse_notify, parse_subscription, PoolMessageError};
|
||||
use alkane_os::stratum_v1::{
|
||||
parse_configure_response, parse_control_notification, parse_notify, parse_subscription,
|
||||
PoolMessageError, PoolNotification, Subscription,
|
||||
};
|
||||
|
||||
const SUBSCRIBE: &str =
|
||||
r#"{"id":1,"result":[[["mining.notify","session"]],"01020304",4],"error":null}"#;
|
||||
@@ -68,3 +71,77 @@ fn malformed_pool_messages_fail_before_work_issuance() {
|
||||
Err(PoolMessageError::TooLarge)
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn version_rolling_negotiation_and_immediate_mask_change() {
|
||||
let response = br#"{"id":3,"result":{"version-rolling":true,"version-rolling.mask":"1fffe000"},"error":null}"#;
|
||||
let mask = parse_configure_response(response, 3).unwrap().unwrap();
|
||||
assert_eq!(mask.bits(), 0x1fff_e000);
|
||||
let update = br#"{"id":null,"method":"mining.set_version_mask","params":["00006000"]}"#;
|
||||
assert_eq!(
|
||||
parse_control_notification(update, true, false).unwrap(),
|
||||
PoolNotification::SetVersionMask(alkane_os::pow::VersionMask::new(0x6000).unwrap())
|
||||
);
|
||||
assert_eq!(
|
||||
parse_control_notification(update, false, false),
|
||||
Err(PoolMessageError::Unnegotiated)
|
||||
);
|
||||
assert_eq!(
|
||||
parse_configure_response(response, 4),
|
||||
Err(PoolMessageError::WrongMessage)
|
||||
);
|
||||
let declined = br#"{"id":3,"result":{"version-rolling":false},"error":null}"#;
|
||||
assert_eq!(parse_configure_response(declined, 3), Ok(None));
|
||||
let unsafe_mask = br#"{"id":3,"result":{"version-rolling":true,"version-rolling.mask":"ffffffff"},"error":null}"#;
|
||||
assert_eq!(
|
||||
parse_configure_response(unsafe_mask, 3),
|
||||
Err(PoolMessageError::InvalidField)
|
||||
);
|
||||
let absent_mask = br#"{"id":3,"result":{"version-rolling":true},"error":null}"#;
|
||||
assert_eq!(
|
||||
parse_configure_response(absent_mask, 3),
|
||||
Err(PoolMessageError::InvalidField)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn difficulty_and_extranonce_notifications_validate_shape_and_negotiation() {
|
||||
let difficulty = br#"{"id":null,"method":"mining.set_difficulty","params":[2048.5]}"#;
|
||||
assert_eq!(
|
||||
parse_control_notification(difficulty, false, false),
|
||||
Ok(PoolNotification::SetDifficulty(2048.5))
|
||||
);
|
||||
let extranonce = br#"{"id":null,"method":"mining.set_extranonce","params":["aabbccdd",4]}"#;
|
||||
assert_eq!(
|
||||
parse_control_notification(extranonce, false, true),
|
||||
Ok(PoolNotification::SetExtranonce(Subscription {
|
||||
extranonce1: vec![0xaa, 0xbb, 0xcc, 0xdd],
|
||||
extranonce2_size: 4,
|
||||
}))
|
||||
);
|
||||
assert_eq!(
|
||||
parse_control_notification(extranonce, false, false),
|
||||
Err(PoolMessageError::Unnegotiated)
|
||||
);
|
||||
for value in ["0", "-1", "\"2\"", "null"] {
|
||||
let line =
|
||||
format!("{{\"id\":null,\"method\":\"mining.set_difficulty\",\"params\":[{value}]}}");
|
||||
assert_eq!(
|
||||
parse_control_notification(line.as_bytes(), false, false),
|
||||
Err(PoolMessageError::InvalidField)
|
||||
);
|
||||
}
|
||||
let oversize = format!(
|
||||
"{{\"id\":null,\"method\":\"mining.set_extranonce\",\"params\":[\"{}\",1]}}",
|
||||
"ab".repeat(33)
|
||||
);
|
||||
assert_eq!(
|
||||
parse_control_notification(oversize.as_bytes(), false, true),
|
||||
Err(PoolMessageError::InvalidField)
|
||||
);
|
||||
let response_id = br#"{"id":5,"method":"mining.set_difficulty","params":[2]}"#;
|
||||
assert_eq!(
|
||||
parse_control_notification(response_id, false, false),
|
||||
Err(PoolMessageError::WrongMessage)
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user