Skip to content

Commit

Permalink
fix(translator): fix HexError(OddLength) in mining.subscribe handler
Browse files Browse the repository at this point in the history
  • Loading branch information
nikicat committed Jun 1, 2024
1 parent a545f0e commit 23a62e7
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 15 deletions.
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
14 changes: 6 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
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

0 comments on commit 23a62e7

Please sign in to comment.