209 lines
7.8 KiB
Rust
209 lines
7.8 KiB
Rust
// SPDX-License-Identifier: MIT
|
|
use alkane_os::difficulty::PoolDifficulty;
|
|
use alkane_os::stratum_v1::{
|
|
parse_boolean_response, parse_configure_response, parse_configure_result,
|
|
parse_control_notification, parse_notify, parse_subscription, PoolMessageError,
|
|
PoolNotification, Subscription,
|
|
};
|
|
|
|
const SUBSCRIBE: &str =
|
|
r#"{"id":1,"result":[[["mining.notify","session"]],"01020304",4],"error":null}"#;
|
|
fn notify() -> String {
|
|
serde_json::json!({
|
|
"id": null,
|
|
"method": "mining.notify",
|
|
"params": [
|
|
"job-1",
|
|
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
|
|
"616263", "78797a",
|
|
["202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f"],
|
|
"20000000", "1d00ffff", "66554433", true
|
|
]
|
|
})
|
|
.to_string()
|
|
}
|
|
|
|
#[test]
|
|
fn subscribe_and_notify_construct_expected_asic_header() {
|
|
let subscription = parse_subscription(SUBSCRIBE.as_bytes(), 1).unwrap();
|
|
let line = notify();
|
|
let job = parse_notify(line.as_bytes(), &subscription).unwrap();
|
|
assert_eq!(job.id, "job-1");
|
|
assert!(job.clean_jobs);
|
|
let header = job
|
|
.work
|
|
.nonce_zero_header(&[5, 6, 7, 8], job.work.ntime)
|
|
.unwrap();
|
|
let hex: String = header.iter().map(|byte| format!("{byte:02x}")).collect();
|
|
assert_eq!(
|
|
hex,
|
|
concat!(
|
|
"0000002003020100070605040b0a09080f0e0d0c13121110171615141b1a19181f1e1d1c",
|
|
"9b9bc91f078af83ff63d00c6da09b5bc4dd26e2d8c0df8ab16b933a259a05881",
|
|
"33445566ffff001d00000000"
|
|
)
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn malformed_pool_messages_fail_before_work_issuance() {
|
|
assert!(matches!(
|
|
parse_subscription(SUBSCRIBE.as_bytes(), 2),
|
|
Err(PoolMessageError::WrongMessage)
|
|
));
|
|
let subscription = parse_subscription(SUBSCRIBE.as_bytes(), 1).unwrap();
|
|
let malformed = notify().replace("66554433", "6655443g");
|
|
assert!(matches!(
|
|
parse_notify(malformed.as_bytes(), &subscription),
|
|
Err(PoolMessageError::InvalidHex)
|
|
));
|
|
let wrong_method = notify().replace("mining.notify", "mining.submit");
|
|
assert!(matches!(
|
|
parse_notify(wrong_method.as_bytes(), &subscription),
|
|
Err(PoolMessageError::WrongMessage)
|
|
));
|
|
let too_large = vec![b' '; 131_073];
|
|
assert!(matches!(
|
|
parse_notify(&too_large, &subscription),
|
|
Err(PoolMessageError::TooLarge)
|
|
));
|
|
let too_large_coinbase = notify().replace("616263", &"aa".repeat(65_537));
|
|
assert!(matches!(
|
|
parse_notify(too_large_coinbase.as_bytes(), &subscription),
|
|
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 both_bip310_extensions_and_boolean_responses_are_checked() {
|
|
let accepted = br#"{"id":3,"result":{"version-rolling":true,"version-rolling.mask":"1fffe000","subscribe-extranonce":true},"error":null}"#;
|
|
let result = parse_configure_result(accepted, 3).unwrap();
|
|
assert_eq!(result.version_mask.unwrap().bits(), 0x1fff_e000);
|
|
assert!(result.subscribe_extranonce);
|
|
let declined =
|
|
br#"{"id":3,"result":{"version-rolling":false,"subscribe-extranonce":false},"error":null}"#;
|
|
let result = parse_configure_result(declined, 3).unwrap();
|
|
assert_eq!(result.version_mask, None);
|
|
assert!(!result.subscribe_extranonce);
|
|
let unavailable = br#"{"id":3,"result":{"version-rolling":"unavailable","subscribe-extranonce":"unsupported"},"error":null}"#;
|
|
let result = parse_configure_result(unavailable, 3).unwrap();
|
|
assert_eq!(result.version_mask, None);
|
|
assert!(!result.subscribe_extranonce);
|
|
assert_eq!(
|
|
parse_configure_result(
|
|
br#"{"id":3,"result":{"version-rolling":false},"error":null}"#,
|
|
3
|
|
),
|
|
Ok(alkane_os::stratum_v1::ConfigureResult {
|
|
version_mask: None,
|
|
subscribe_extranonce: false,
|
|
})
|
|
);
|
|
assert_eq!(
|
|
parse_configure_result(
|
|
br#"{"id":3,"result":{"version-rolling":false,"subscribe-extranonce":null},"error":null}"#,
|
|
3
|
|
),
|
|
Err(PoolMessageError::InvalidField)
|
|
);
|
|
assert_eq!(
|
|
parse_boolean_response(br#"{"id":5,"result":true,"error":null}"#, 5),
|
|
Ok(true)
|
|
);
|
|
assert_eq!(
|
|
parse_boolean_response(br#"{"id":5,"result":false,"error":[23,"stale"]}"#, 5),
|
|
Ok(false)
|
|
);
|
|
assert_eq!(
|
|
parse_boolean_response(
|
|
br#"{"id":5,"result":null,"error":[21,"Job not found",null]}"#,
|
|
5
|
|
),
|
|
Ok(false)
|
|
);
|
|
assert_eq!(
|
|
parse_boolean_response(br#"{"id":5,"result":true,"error":[23,"stale"]}"#, 5),
|
|
Err(PoolMessageError::InvalidField)
|
|
);
|
|
assert_eq!(
|
|
parse_boolean_response(br#"{"id":6,"result":true,"error":null}"#, 5),
|
|
Err(PoolMessageError::WrongMessage)
|
|
);
|
|
}
|
|
|
|
#[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(
|
|
PoolDifficulty::from_json_number(&serde_json::from_str("2048.5").unwrap()).unwrap()
|
|
))
|
|
);
|
|
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)
|
|
);
|
|
}
|