Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion fuzz/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sv1/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sv1_api"
version = "7.0.0"
version = "7.1.0"
authors = ["The Stratum V2 Developers"]
edition = "2021"
readme = "README.md"
Expand Down
80 changes: 74 additions & 6 deletions sv1/src/methods/server_to_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,11 @@ impl TryFrom<Notification> for SetExtranonce {
let (extra_nonce1, extra_nonce2_size) = match &params[..] {
[JString(a), JNumber(b)] => (
Extranonce::try_from(hex_decode(a)?)?,
b.as_u64()
.ok_or_else(|| ParsingMethodError::not_unsigned_from_value(b.clone()))?
as usize,
extranonce2_size(
b.as_u64()
.ok_or_else(|| ParsingMethodError::not_unsigned_from_value(b.clone()))?,
b,
)?,
),
_ => return Err(ParsingMethodError::wrong_args_from_value(msg.params)),
};
Expand All @@ -282,6 +284,21 @@ impl TryFrom<Notification> for SetExtranonce {
}
}

/// Checks an extranonce2 size sent by the server, given as `size` and as the JSON `value` it was
/// read from.
///
/// The miner sends extranonce2 in [`crate::client_to_server::Submit`] as an [`Extranonce`], so a
/// size above [`Extranonce::MAX_LEN`] would leave it unable to submit any share. Bounding the
/// size first also keeps the conversion to `usize` lossless on 32-bit targets.
fn extranonce2_size(size: u64, value: &serde_json::Number) -> Result<usize, ParsingMethodError> {
if size > Extranonce::MAX_LEN as u64 {
return Err(ParsingMethodError::unexpected_value_from_value(JNumber(
value.clone(),
)));
}
Ok(size as usize)
}

#[derive(Debug, Clone)]
/// Server may arbitrarily adjust version mask
pub struct SetVersionMask {
Expand Down Expand Up @@ -503,9 +520,12 @@ impl TryFrom<&Response> for Subscribe {
a,
// infallible
b.as_str().try_into()?,
c.as_u64().ok_or_else(|| {
ParsingMethodError::ImpossibleToParseAsU64(Box::new(c.clone()))
})? as usize,
extranonce2_size(
c.as_u64().ok_or_else(|| {
ParsingMethodError::ImpossibleToParseAsU64(Box::new(c.clone()))
})?,
c,
)?,
),
_ => return Err(ParsingMethodError::UnexpectedArrayParams(params.clone())),
};
Expand Down Expand Up @@ -817,3 +837,51 @@ impl From<VersionRollingParams> for serde_json::Map<String, Value> {
params
}
}

#[test]
fn extranonce2_size_must_fit_a_submit() {
let set_extranonce = |size: serde_json::Value| {
SetExtranonce::try_from(Notification {
method: "mining.set_extranonce".to_string(),
params: serde_json::json!(["08000002", size]),
})
};
let subscribe = |size: serde_json::Value| {
let response: Response = serde_json::from_value(serde_json::json!({
"id": 1,
"error": null,
"result": [[["mining.notify", "1"]], "08000002", size],
}))
.unwrap();
Subscribe::try_from(&response)
};

assert_eq!(
set_extranonce(serde_json::json!(0))
.unwrap()
.extra_nonce2_size,
0
);
assert_eq!(
set_extranonce(serde_json::json!(32))
.unwrap()
.extra_nonce2_size,
32
);
assert_eq!(
subscribe(serde_json::json!(32)).unwrap().extra_nonce2_size,
32
);

// 2^32 + 4 would become 4 if it were cast to a 32-bit usize
for size in [33, 4_294_967_300, u64::MAX] {
assert!(matches!(
set_extranonce(serde_json::json!(size)),
Err(ParsingMethodError::UnexpectedValue(_))
));
assert!(matches!(
subscribe(serde_json::json!(size)),
Err(ParsingMethodError::UnexpectedValue(_))
));
}
}
3 changes: 3 additions & 0 deletions sv1/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ impl fmt::Display for Extranonce {
}

impl Extranonce {
/// The most bytes an extranonce can hold.
pub const MAX_LEN: usize = 32;

pub fn len(&self) -> usize {
self.0.len()
}
Expand Down
Loading