diff --git a/COVERAGE.md b/COVERAGE.md index 8b9e4d0..42a29af 100644 --- a/COVERAGE.md +++ b/COVERAGE.md @@ -148,7 +148,7 @@ No scenarios yet. |---|---|---|---|---|---|---| | M-OEMCS-1 | Ω1 | Θ1 | `.Success.request_id` doesn't echo the request | [§5.3.5](https://github.com/stratum-mining/sv2-spec/blob/main/05-Mining-Protocol.md#535-openextendedminingchannelsuccess-server---client) `request_id` "Client-specified request ID from OpenExtendedMiningChannel message, so that the client can pair responses with open channel requests" | Random `request_id`s; assert pairing | — | | M-OEMCS-2 | Ω1 | Θ1 | `channel_id` reused for a second channel | [§5.2](https://github.com/stratum-mining/sv2-spec/blob/main/05-Mining-Protocol.md#52-channel) "There MUST NOT be two Channels with the same ID" | Open N channels; assert uniqueness | — | -| M-OEMCS-3 | Ω1 | Θ1 | Extended channel: `extranonce_size` smaller than requested `min_extranonce_size`, without error | [§5.3.4](https://github.com/stratum-mining/sv2-spec/blob/main/05-Mining-Protocol.md#534-openextendedminingchannel-client---server) "Server MUST satisfy the `min_extranonce_size` or respond with `OpenMiningChannel.Error`" / [§5.3.5](https://github.com/stratum-mining/sv2-spec/blob/main/05-Mining-Protocol.md#535-openextendedminingchannelsuccess-server---client) | Assert `extranonce_size >= min_extranonce_size` (or explicit error) | `OpenMiningChannel.Error` | +| M-OEMCS-3 | Ω1 | Θ1 | Extended channel: `extranonce_size` smaller than requested `min_extranonce_size`, without error | [§5.3.4](https://github.com/stratum-mining/sv2-spec/blob/main/05-Mining-Protocol.md#534-openextendedminingchannel-client---server) "Server MUST satisfy the `min_extranonce_size` or respond with `OpenMiningChannel.Error`" / [§5.3.5](https://github.com/stratum-mining/sv2-spec/blob/main/05-Mining-Protocol.md#535-openextendedminingchannelsuccess-server---client) | Request `min_extranonce_size = 4, 10, 32` on fresh connections; assert `extranonce_size >= min_extranonce_size` on each `.Success` (or explicit error) | `OpenMiningChannel.Error` | | M-OEMCS-4 | Ω1 | Θ1 | Channels in the same group have different full extranonce sizes | [§5.2.3](https://github.com/stratum-mining/sv2-spec/blob/main/05-Mining-Protocol.md#523-group-channel) "All channels under the same Group Channel (Extended and Standard) MUST have the exact same size for the full Extended Extranonce", [§5.1.2.1](https://github.com/stratum-mining/sv2-spec/blob/main/05-Mining-Protocol.md#5121-extended-extranonce) | Cross-check full extranonce sizes within group (only if the server uses groups) | `SetGroupChannel`, `NewExtendedMiningJob` | | M-OEMCS-5 | Ω1 | Θ1 | Same extranonce space assigned to two extended channels → overlapping search space, duplicate work | [§5.1](https://github.com/stratum-mining/sv2-spec/blob/main/05-Mining-Protocol.md#51-job) "Mining servers MUST assign a unique subset of the search space to each mining device" | Open 2+ channels; compare prefixes | — | | M-OEMCS-6 | Ω1 | Θ1 | Server negotiates `extranonce_size > 32`, making a valid `SubmitSharesExtended` impossible because `extranonce` is `B0_32` | [§5.3.4](https://github.com/stratum-mining/sv2-spec/blob/main/05-Mining-Protocol.md#534-openextendedminingchannel-client---server) / [§5.3.5](https://github.com/stratum-mining/sv2-spec/blob/main/05-Mining-Protocol.md#535-openextendedminingchannelsuccess-server---client) "extranonce_size MUST be at most 32 bytes" | Request `min_extranonce_size = 33`; assert `OpenMiningChannel.Error`. For successful opens, assert `extranonce_size <= 32` | `OpenMiningChannel.Error` | diff --git a/Cargo.lock b/Cargo.lock index 535ede7..50f213c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -29,20 +29,6 @@ dependencies = [ "cpufeatures 0.2.17", ] -[[package]] -name = "aes-gcm" -version = "0.10.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "831010a0f742e1209b3bcea8fab6a8e149051ba6099432c8cb2cc117dec3ead1" -dependencies = [ - "aead", - "aes", - "cipher", - "ctr", - "ghash", - "subtle", -] - [[package]] name = "ahash" version = "0.8.12" @@ -829,8 +815,8 @@ checksum = "32637268377fc7b10a8c6d51de3e7fba1ce5dd371a96e342b34e6078db558e7f" [[package]] name = "binary_sv2" -version = "7.0.1" -source = "git+https://github.com/stratum-mining/stratum?branch=main#b1f678c46b1b7c6067dc7357e6088617c0d280ea" +version = "8.0.0" +source = "git+https://github.com/stratum-mining/stratum?branch=main#2220f62dbdc833a64c53f05bd0fbed3ae611f4d6" dependencies = [ "buffer_sv2", "derive_codec_sv2", @@ -876,6 +862,17 @@ dependencies = [ "capnpc", ] +[[package]] +name = "bitcoin-capnp-types" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d0eb36ad482005123d968a7618fe8aa72e4b95b77ce13527f96f283229e0f7" +dependencies = [ + "capnp", + "capnp-rpc", + "capnpc", +] + [[package]] name = "bitcoin-consensus-encoding" version = "1.0.0" @@ -916,11 +913,12 @@ dependencies = [ [[package]] name = "bitcoin_core_sv2" version = "0.7.0" -source = "git+https://github.com/stratum-mining/sv2-apps?branch=main#9be9d0ff8f84fa1a88031f1be35a79b405dd1dc8" +source = "git+https://github.com/stratum-mining/sv2-apps?branch=main#b6a26a418fbf8c78ddd94fa79d26acc738dec421" dependencies = [ "async-channel", "bitcoin-capnp-types 0.1.2", "bitcoin-capnp-types 0.2.1", + "bitcoin-capnp-types 0.3.0", "stratum-core", "tokio", "tokio-util", @@ -1016,11 +1014,8 @@ dependencies = [ [[package]] name = "buffer_sv2" -version = "3.0.1" -source = "git+https://github.com/stratum-mining/stratum?branch=main#b1f678c46b1b7c6067dc7357e6088617c0d280ea" -dependencies = [ - "aes-gcm", -] +version = "4.0.0" +source = "git+https://github.com/stratum-mining/stratum?branch=main#2220f62dbdc833a64c53f05bd0fbed3ae611f4d6" [[package]] name = "bumpalo" @@ -1148,7 +1143,7 @@ dependencies = [ [[package]] name = "channels_sv2" version = "9.0.0" -source = "git+https://github.com/stratum-mining/stratum?branch=main#b1f678c46b1b7c6067dc7357e6088617c0d280ea" +source = "git+https://github.com/stratum-mining/stratum?branch=main#2220f62dbdc833a64c53f05bd0fbed3ae611f4d6" dependencies = [ "binary_sv2", "bitcoin", @@ -1233,8 +1228,8 @@ dependencies = [ [[package]] name = "codec_sv2" -version = "7.0.0" -source = "git+https://github.com/stratum-mining/stratum?branch=main#b1f678c46b1b7c6067dc7357e6088617c0d280ea" +version = "8.1.0" +source = "git+https://github.com/stratum-mining/stratum?branch=main#2220f62dbdc833a64c53f05bd0fbed3ae611f4d6" dependencies = [ "binary_sv2", "buffer_sv2", @@ -1271,8 +1266,8 @@ dependencies = [ [[package]] name = "common_messages_sv2" -version = "9.0.0" -source = "git+https://github.com/stratum-mining/stratum?branch=main#b1f678c46b1b7c6067dc7357e6088617c0d280ea" +version = "10.0.0" +source = "git+https://github.com/stratum-mining/stratum?branch=main#2220f62dbdc833a64c53f05bd0fbed3ae611f4d6" dependencies = [ "binary_sv2", ] @@ -1491,15 +1486,6 @@ dependencies = [ "hybrid-array", ] -[[package]] -name = "ctr" -version = "0.9.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0369ee1ad671834580515889b80f2ea915f23b8be8d0daa4bbaf2ac5c7590835" -dependencies = [ - "cipher", -] - [[package]] name = "dashmap" version = "6.2.1" @@ -1528,7 +1514,7 @@ dependencies = [ [[package]] name = "derive_codec_sv2" version = "2.0.0" -source = "git+https://github.com/stratum-mining/stratum?branch=main#b1f678c46b1b7c6067dc7357e6088617c0d280ea" +source = "git+https://github.com/stratum-mining/stratum?branch=main#2220f62dbdc833a64c53f05bd0fbed3ae611f4d6" [[package]] name = "digest" @@ -1679,8 +1665,8 @@ checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0" [[package]] name = "extensions_sv2" -version = "0.3.0" -source = "git+https://github.com/stratum-mining/stratum?branch=main#b1f678c46b1b7c6067dc7357e6088617c0d280ea" +version = "0.4.0" +source = "git+https://github.com/stratum-mining/stratum?branch=main#2220f62dbdc833a64c53f05bd0fbed3ae611f4d6" dependencies = [ "binary_sv2", ] @@ -1737,8 +1723,8 @@ dependencies = [ [[package]] name = "framing_sv2" -version = "8.0.0" -source = "git+https://github.com/stratum-mining/stratum?branch=main#b1f678c46b1b7c6067dc7357e6088617c0d280ea" +version = "9.0.0" +source = "git+https://github.com/stratum-mining/stratum?branch=main#2220f62dbdc833a64c53f05bd0fbed3ae611f4d6" dependencies = [ "binary_sv2", "buffer_sv2", @@ -1881,20 +1867,10 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "ghash" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0d8a4362ccb29cb0b265253fb0a2728f592895ee6854fd9bc13f2ffda266ff1" -dependencies = [ - "opaque-debug", - "polyval", -] - [[package]] name = "handlers_sv2" version = "0.7.0" -source = "git+https://github.com/stratum-mining/stratum?branch=main#b1f678c46b1b7c6067dc7357e6088617c0d280ea" +source = "git+https://github.com/stratum-mining/stratum?branch=main#2220f62dbdc833a64c53f05bd0fbed3ae611f4d6" dependencies = [ "binary_sv2", "common_messages_sv2", @@ -2282,8 +2258,8 @@ dependencies = [ [[package]] name = "integration_tests_sv2" -version = "0.7.0" -source = "git+https://github.com/stratum-mining/sv2-apps?branch=main#9be9d0ff8f84fa1a88031f1be35a79b405dd1dc8" +version = "0.7.1" +source = "git+https://github.com/stratum-mining/sv2-apps?branch=main#b6a26a418fbf8c78ddd94fa79d26acc738dec421" dependencies = [ "async-channel", "clap", @@ -2342,8 +2318,8 @@ checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" [[package]] name = "jd_client_sv2" -version = "0.7.0" -source = "git+https://github.com/stratum-mining/sv2-apps?branch=main#9be9d0ff8f84fa1a88031f1be35a79b405dd1dc8" +version = "0.7.1" +source = "git+https://github.com/stratum-mining/sv2-apps?branch=main#b6a26a418fbf8c78ddd94fa79d26acc738dec421" dependencies = [ "async-channel", "clap", @@ -2358,8 +2334,8 @@ dependencies = [ [[package]] name = "jd_server_sv2" -version = "0.7.0" -source = "git+https://github.com/stratum-mining/sv2-apps?branch=main#9be9d0ff8f84fa1a88031f1be35a79b405dd1dc8" +version = "0.7.2" +source = "git+https://github.com/stratum-mining/sv2-apps?branch=main#b6a26a418fbf8c78ddd94fa79d26acc738dec421" dependencies = [ "async-channel", "async-trait", @@ -2421,8 +2397,8 @@ dependencies = [ [[package]] name = "job_declaration_sv2" -version = "10.0.0" -source = "git+https://github.com/stratum-mining/stratum?branch=main#b1f678c46b1b7c6067dc7357e6088617c0d280ea" +version = "11.0.0" +source = "git+https://github.com/stratum-mining/stratum?branch=main#2220f62dbdc833a64c53f05bd0fbed3ae611f4d6" dependencies = [ "binary_sv2", ] @@ -2636,7 +2612,7 @@ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" [[package]] name = "mining_sv2" version = "13.0.0" -source = "git+https://github.com/stratum-mining/stratum?branch=main#b1f678c46b1b7c6067dc7357e6088617c0d280ea" +source = "git+https://github.com/stratum-mining/stratum?branch=main#2220f62dbdc833a64c53f05bd0fbed3ae611f4d6" dependencies = [ "binary_sv2", ] @@ -2688,8 +2664,8 @@ dependencies = [ [[package]] name = "noise_sv2" -version = "2.0.0" -source = "git+https://github.com/stratum-mining/stratum?branch=main#b1f678c46b1b7c6067dc7357e6088617c0d280ea" +version = "2.1.0" +source = "git+https://github.com/stratum-mining/stratum?branch=main#2220f62dbdc833a64c53f05bd0fbed3ae611f4d6" dependencies = [ "chacha20poly1305", "rand 0.8.7", @@ -2829,7 +2805,7 @@ dependencies = [ [[package]] name = "parsers_sv2" version = "0.7.0" -source = "git+https://github.com/stratum-mining/stratum?branch=main#b1f678c46b1b7c6067dc7357e6088617c0d280ea" +source = "git+https://github.com/stratum-mining/stratum?branch=main#2220f62dbdc833a64c53f05bd0fbed3ae611f4d6" dependencies = [ "binary_sv2", "common_messages_sv2", @@ -2917,22 +2893,10 @@ dependencies = [ "universal-hash", ] -[[package]] -name = "polyval" -version = "0.6.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d1fe60d06143b2430aa532c94cfe9e29783047f06c0d7fd359a9a51b729fa25" -dependencies = [ - "cfg-if", - "cpufeatures 0.2.17", - "opaque-debug", - "universal-hash", -] - [[package]] name = "pool_sv2" -version = "0.8.0" -source = "git+https://github.com/stratum-mining/sv2-apps?branch=main#9be9d0ff8f84fa1a88031f1be35a79b405dd1dc8" +version = "0.8.2" +source = "git+https://github.com/stratum-mining/sv2-apps?branch=main#b6a26a418fbf8c78ddd94fa79d26acc738dec421" dependencies = [ "async-channel", "clap", @@ -3794,7 +3758,7 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" [[package]] name = "stratum-apps" version = "0.9.0" -source = "git+https://github.com/stratum-mining/sv2-apps?branch=main#9be9d0ff8f84fa1a88031f1be35a79b405dd1dc8" +source = "git+https://github.com/stratum-mining/sv2-apps?branch=main#b6a26a418fbf8c78ddd94fa79d26acc738dec421" dependencies = [ "asic-rs", "async-channel", @@ -3825,7 +3789,7 @@ dependencies = [ [[package]] name = "stratum-core" version = "0.7.0" -source = "git+https://github.com/stratum-mining/stratum?branch=main#b1f678c46b1b7c6067dc7357e6088617c0d280ea" +source = "git+https://github.com/stratum-mining/stratum?branch=main#2220f62dbdc833a64c53f05bd0fbed3ae611f4d6" dependencies = [ "binary_sv2", "bitcoin", @@ -3848,7 +3812,7 @@ dependencies = [ [[package]] name = "stratum_translation" version = "0.6.0" -source = "git+https://github.com/stratum-mining/stratum?branch=main#b1f678c46b1b7c6067dc7357e6088617c0d280ea" +source = "git+https://github.com/stratum-mining/stratum?branch=main#2220f62dbdc833a64c53f05bd0fbed3ae611f4d6" dependencies = [ "binary_sv2", "bitcoin", @@ -3894,8 +3858,8 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" [[package]] name = "sv1_api" -version = "6.0.1" -source = "git+https://github.com/stratum-mining/stratum?branch=main#b1f678c46b1b7c6067dc7357e6088617c0d280ea" +version = "7.0.0" +source = "git+https://github.com/stratum-mining/stratum?branch=main#2220f62dbdc833a64c53f05bd0fbed3ae611f4d6" dependencies = [ "binary_sv2", "bitcoin", @@ -3963,7 +3927,7 @@ dependencies = [ [[package]] name = "template_distribution_sv2" version = "8.0.0" -source = "git+https://github.com/stratum-mining/stratum?branch=main#b1f678c46b1b7c6067dc7357e6088617c0d280ea" +source = "git+https://github.com/stratum-mining/stratum?branch=main#2220f62dbdc833a64c53f05bd0fbed3ae611f4d6" dependencies = [ "binary_sv2", ] @@ -4111,6 +4075,7 @@ dependencies = [ "futures-core", "futures-io", "futures-sink", + "futures-util", "pin-project-lite", "tokio", ] @@ -4313,7 +4278,7 @@ dependencies = [ [[package]] name = "translator_sv2" version = "0.7.0" -source = "git+https://github.com/stratum-mining/sv2-apps?branch=main#9be9d0ff8f84fa1a88031f1be35a79b405dd1dc8" +source = "git+https://github.com/stratum-mining/sv2-apps?branch=main#b6a26a418fbf8c78ddd94fa79d26acc738dec421" dependencies = [ "async-channel", "clap", diff --git a/src/scenarios/mining/open_extended_mining_channel.rs b/src/scenarios/mining/open_extended_mining_channel.rs index 8f5995e..05f6198 100644 --- a/src/scenarios/mining/open_extended_mining_channel.rs +++ b/src/scenarios/mining/open_extended_mining_channel.rs @@ -6,12 +6,15 @@ use super::super::ScenarioResult; use crate::{client::TestClient, endpoint::Endpoint, scenarios::ScenarioFn}; const REQUEST_ID: u32 = 1; -const MIN_EXTRANONCE_SIZE: u16 = 4; // --------------------------------------------------------------------------- // M-OEMCS-1 — `request_id` echo on `OpenExtendedMiningChannel.Success` // --------------------------------------------------------------------------- +/// A low `min_extranonce_size` for M-OEMCS-1, which only needs a `.Success` +/// envelope to check the `request_id` echo. +const REQUEST_ID_ECHO_MIN_EXTRANONCE_SIZE: u16 = 4; + /// The endpoint MUST echo `request_id` in `OpenExtendedMiningChannel.Success`. /// /// Covers: M-OEMCS-1 @@ -23,7 +26,7 @@ pub async fn request_id_echo(endpoint: Endpoint) -> ScenarioResult { user_identity: endpoint.user_identity.clone().try_into().unwrap(), nominal_hash_rate: 1_000_000.0, max_target: [0xff_u8; 32].into(), - min_extranonce_size: MIN_EXTRANONCE_SIZE, + min_extranonce_size: REQUEST_ID_ECHO_MIN_EXTRANONCE_SIZE, }; let m = client .open_extended_channel(open) @@ -42,39 +45,51 @@ pub async fn request_id_echo(endpoint: Endpoint) -> ScenarioResult { // M-OEMCS-3 — `extranonce_size >= min_extranonce_size` // --------------------------------------------------------------------------- -/// The endpoint MUST satisfy `min_extranonce_size` or refuse the channel with -/// `OpenMiningChannel.Error` (§5.3.4, §5.3.5); both answers are compliant. +/// `min_extranonce_size` values probed by M-OEMCS-3: a common baseline (4), +/// a value above common silent caps such as 6 (10), and the protocol maximum +/// (32). A server may satisfy each request or answer with +/// `OpenMiningChannel.Error`; silently granting fewer bytes is the violation. +const MIN_EXTRANONCE_SIZE_PROBES: [u16; 3] = [4, 10, 32]; + +/// The endpoint MUST satisfy every requested `min_extranonce_size` or refuse +/// that channel with `OpenMiningChannel.Error` (§5.3.4, §5.3.5). A `.Success` +/// granting fewer bytes than requested is a violation; refusing is not. /// /// Covers: M-OEMCS-3 pub async fn extranonce_size_at_least_min(endpoint: Endpoint) -> ScenarioResult { - let client = TestClient::connect(&endpoint).await?; + let mut outcomes = Vec::with_capacity(MIN_EXTRANONCE_SIZE_PROBES.len()); - let open = OpenExtendedMiningChannelOwned { - request_id: REQUEST_ID, - user_identity: endpoint.user_identity.clone().try_into().unwrap(), - nominal_hash_rate: 1_000_000.0, - max_target: [0xff_u8; 32].into(), - min_extranonce_size: MIN_EXTRANONCE_SIZE, - }; - match client.open_extended_channel(open).await? { - Ok(m) => { - if m.extranonce_size < MIN_EXTRANONCE_SIZE { + for min_extranonce_size in MIN_EXTRANONCE_SIZE_PROBES { + let client = TestClient::connect(&endpoint).await?; + + let open = OpenExtendedMiningChannelOwned { + request_id: REQUEST_ID, + user_identity: endpoint.user_identity.clone().try_into().unwrap(), + nominal_hash_rate: 1_000_000.0, + max_target: [0xff_u8; 32].into(), + min_extranonce_size, + }; + + match client.open_extended_channel(open).await? { + Ok(m) if m.extranonce_size < min_extranonce_size => { return Err(format!( - "extranonce_size {} smaller than requested min_extranonce_size {MIN_EXTRANONCE_SIZE}", + "min_extranonce_size={min_extranonce_size}: server granted extranonce_size={}, smaller than requested", m.extranonce_size ) .into()); } - Ok(Some(format!( - "channel_id={}, group_channel_id={}, extranonce_size={}", - m.channel_id, m.group_channel_id, m.extranonce_size - ))) - } - Err(e) => Ok(Some(format!( - "refused with error_code=\"{}\"", - e.error_code.as_utf8_or_hex() - ))), + Ok(m) => outcomes.push(format!( + "min={min_extranonce_size} -> extranonce_size={}", + m.extranonce_size + )), + Err(e) => outcomes.push(format!( + "min={min_extranonce_size} -> error_code=\"{}\"", + e.error_code.as_utf8_or_hex() + )), + }; } + + Ok(Some(outcomes.join("; "))) } // --------------------------------------------------------------------------- diff --git a/tests/extranonce_size_probe.rs b/tests/extranonce_size_probe.rs new file mode 100644 index 0000000..0c814a1 --- /dev/null +++ b/tests/extranonce_size_probe.rs @@ -0,0 +1,127 @@ +//! Regression tests for https://github.com/stratum-mining/interoperability-tests-sv2/issues/18. +//! +//! A pool that answers `OpenExtendedMiningChannel` with a `.Success` carrying +//! `extranonce_size` below the requested `min_extranonce_size` violates §5.3.4 +//! and must be reported as non-compliant by M-OEMCS-3. + +use std::net::SocketAddr; + +use integration_tests_sv2::utils::{create_downstream, get_available_address, message_from_frame}; +use interoperability_tests_sv2::{ + app_type::AppType, + endpoint::Endpoint, + scenarios::{mining_server, ScenarioError}, +}; +use stratum_apps::stratum_core::{ + codec_sv2::MessageFrame, + common_messages_sv2::SetupConnectionSuccessOwned, + mining_sv2::OpenExtendedMiningChannelSuccessOwned, + parsers_sv2::{AnyMessageOwned, CommonMessagesOwned, IsSv2Message, MiningOwned}, +}; + +#[tokio::test] +async fn rejects_pool_that_caps_extranonce_size_at_thirty_one() { + assert_scenario_rejects_cap(31, 32).await; +} + +#[tokio::test] +async fn rejects_pool_that_caps_extranonce_size_at_six() { + assert_scenario_rejects_cap(6, 10).await; +} + +async fn assert_scenario_rejects_cap(cap: u16, expected_min: u16) { + let addr = get_available_address(); + start_capped_pool(addr, cap).await; + + let endpoint = Endpoint { + addr, + app_type: AppType::MiningServer, + user_identity: "interoperability_tests_sv2".to_string(), + }; + + let (_, scenario) = mining_server() + .into_iter() + .find(|(id, _)| id.starts_with("M-OEMCS-3")) + .expect("M-OEMCS-3 is registered in the mining server suite"); + + let error: ScenarioError = scenario(endpoint) + .await + .expect_err("a pool that silently caps extranonce_size must fail M-OEMCS-3"); + + let error = error.to_string(); + + assert!( + error.contains(&format!("min_extranonce_size={expected_min}")) + && error.contains(&format!("extranonce_size={cap}")), + "unexpected failure message: {error}" + ); +} + +/// Starts a Mining Protocol Server that always grants exactly `cap` bytes of +/// extranonce space, regardless of the requested `min_extranonce_size`. This +/// mirrors a server with a fixed-size extranonce. +async fn start_capped_pool(address: SocketAddr, cap: u16) { + let listener = tokio::net::TcpListener::bind(address) + .await + .expect("bind capped pool listener"); + + tokio::spawn(async move { + loop { + let Ok((stream, _)) = listener.accept().await else { + return; + }; + tokio::spawn(async move { + let Some((recv, send)) = create_downstream(stream).await else { + return; + }; + + // SetupConnection + let mut frame = recv.recv().await.expect("SetupConnection frame"); + let _ = message_from_frame(&mut frame); + let setup_success = AnyMessageOwned::Common( + CommonMessagesOwned::SetupConnectionSuccess(SetupConnectionSuccessOwned { + used_version: 2, + flags: 0, + }), + ); + let msg_type = setup_success.message_type(); + let frame = MessageFrame::from_message(setup_success, msg_type, 0, false) + .expect("frame SetupConnection.Success"); + + send.send(frame.into()) + .await + .expect("send SetupConnection.Success"); + + // OpenExtendedMiningChannel + let mut frame = recv.recv().await.expect("OpenExtendedMiningChannel frame"); + let (_, msg) = message_from_frame(&mut frame); + let AnyMessageOwned::Mining(MiningOwned::OpenExtendedMiningChannel(open)) = msg + else { + panic!("expected OpenExtendedMiningChannel, got {msg}"); + }; + + let open_success = + AnyMessageOwned::Mining(MiningOwned::OpenExtendedMiningChannelSuccess( + OpenExtendedMiningChannelSuccessOwned { + request_id: open.request_id, + channel_id: 1, + target: [0xff_u8; 32].into(), + extranonce_prefix: vec![0_u8; 4].try_into().unwrap(), + group_channel_id: 0, + extranonce_size: cap, + }, + )); + let msg_type = open_success.message_type(); + let frame = MessageFrame::from_message(open_success, msg_type, 0, false) + .expect("frame OpenExtendedMiningChannel.Success"); + + send.send(frame.into()) + .await + .expect("send OpenExtendedMiningChannel.Success"); + + // Keep the connection alive until the scenario drops the client. + while recv.recv().await.is_ok() {} + }); + } + }); +}