Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(translator): fix HexError(OddLength) in mining.subscribe handler #851

Merged
merged 2 commits into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions benches/benches/src/sv1/lib/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,7 @@ impl IsClient<'static> for Client {
}
}
pub fn extranonce_from_hex(hex: &str) -> Extranonce<'static> {
let data = utils::decode_hex(hex).unwrap();
Extranonce::try_from(data).expect("Failed to convert hex to U256")
Extranonce::try_from(hex).expect("Failed to convert hex to U256")
}
pub fn prevhash_from_hex<'a>(hex: &str) -> PrevHash<'a> {
let data = utils::decode_hex(hex).unwrap();
Expand Down
5 changes: 1 addition & 4 deletions protocols/v1/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,7 @@ pub trait IsClient<'a> {
// so it doesnt really matter what the server sets the extranonce to in the mining.configure handler
debug!("NOTICE: Subscribe extranonce is hardcoded by server");
let subscribe = self
.subscribe(
configure.id,
Some(Extranonce::try_from(hex::decode("08000002")?)?),
)
.subscribe(configure.id, Some(Extranonce::try_from("08000002")?))
.ok();
Ok(subscribe)
}
Expand Down
47 changes: 39 additions & 8 deletions protocols/v1/src/methods/client_to_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,31 +173,31 @@ impl<'a> TryFrom<StandardRequest> for Submit<'a> {
[JString(a), JString(b), JString(c), JNumber(d), JNumber(e), JString(f)] => (
a.into(),
b.into(),
Extranonce::try_from(hex::decode(c)?)?,
Extranonce::try_from(c.as_str())?,
HexU32Be(d.as_u64().unwrap() as u32),
HexU32Be(e.as_u64().unwrap() as u32),
Some((f.as_str()).try_into()?),
),
[JString(a), JString(b), JString(c), JString(d), JString(e), JString(f)] => (
a.into(),
b.into(),
Extranonce::try_from(hex::decode(c)?)?,
Extranonce::try_from(c.as_str())?,
(d.as_str()).try_into()?,
(e.as_str()).try_into()?,
Some((f.as_str()).try_into()?),
),
[JString(a), JString(b), JString(c), JNumber(d), JNumber(e)] => (
a.into(),
b.into(),
Extranonce::try_from(hex::decode(c)?)?,
Extranonce::try_from(c.as_str())?,
HexU32Be(d.as_u64().unwrap() as u32),
HexU32Be(e.as_u64().unwrap() as u32),
None,
),
[JString(a), JString(b), JString(c), JString(d), JString(e)] => (
a.into(),
b.into(),
Extranonce::try_from(hex::decode(c)?)?,
Extranonce::try_from(c.as_str())?,
(d.as_str()).try_into()?,
(e.as_str()).try_into()?,
None,
Expand Down Expand Up @@ -229,7 +229,7 @@ impl Arbitrary for Submit<'static> {
println!("\nEXTRA: {:?}\n", extra);
let bits = Option::<u32>::arbitrary(g);
println!("\nBITS: {:?}\n", bits);
let extra: Extranonce = extra.try_into().unwrap();
let extra: Extranonce = hex::encode(extra).as_str().try_into().unwrap();
let bits = bits.map(|x| HexU32Be(x));
println!("\nBITS: {:?}\n", bits);
Submit {
Expand Down Expand Up @@ -319,9 +319,7 @@ impl<'a> TryFrom<StandardRequest> for Subscribe<'a> {
let (agent_signature, extranonce1) = match &params[..] {
// bosminer subscribe message
[JString(a), Null, JString(_), Null] => (a.into(), None),
[JString(a), JString(b)] => {
(a.into(), Some(Extranonce::try_from(hex::decode(b)?)?))
}
[JString(a), JString(b)] => (a.into(), Some(Extranonce::try_from(b.as_str())?)),
[JString(a)] => (a.into(), None),
[] => ("".to_string(), None),
_ => return Err(ParsingMethodError::wrong_args_from_value(msg.params)),
Expand Down Expand Up @@ -716,3 +714,36 @@ fn test_version_extension_with_no_bit_count() {
_ => panic!(),
};
}

#[test]
fn test_mining_subscribe_even_sized_extranonce() {
let client_message = r#"{"id":0,
"method": "mining.subscribe",
"params": ["user agent/version", "aaeeffdd"]
}"#;
let client_message: StandardRequest = serde_json::from_str(&client_message).unwrap();
Subscribe::try_from(client_message).unwrap();
}

#[test]
fn test_mining_subscribe_odd_sized_extranonce() {
let client_message = r#"{"id":0,
"method": "mining.subscribe",
"params": ["user agent/version", "aeeffdd"]
}"#;
let client_message: StandardRequest = serde_json::from_str(&client_message).unwrap();
Subscribe::try_from(client_message).unwrap();
}

#[test]
#[should_panic(
expected = "called `Result::unwrap()` on an `Err` value: HexError(InvalidHexCharacter { c: 'z', index: 0 })"
)]
fn test_mining_subscribe_invalid_extranonce() {
let client_message = r#"{"id":0,
"method": "mining.subscribe",
"params": ["user agent/version", "zxczxc"]
}"#;
let client_message: StandardRequest = serde_json::from_str(&client_message).unwrap();
Subscribe::try_from(client_message).unwrap();
}
2 changes: 1 addition & 1 deletion protocols/v1/src/methods/server_to_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ impl<'a> TryFrom<Notification> for SetExtranonce<'a> {
.ok_or_else(|| ParsingMethodError::not_array_from_value(msg.params.clone()))?;
let (extra_nonce1, extra_nonce2_size) = match &params[..] {
[JString(a), JNumber(b)] => (
Extranonce::try_from(hex::decode(a)?)?,
Extranonce::try_from(a.as_str())?,
b.as_u64()
.ok_or_else(|| ParsingMethodError::not_unsigned_from_value(b.clone()))?
as usize,
Expand Down