Skip to content

Commit

Permalink
fixup! fix(translator): fix HexError(OddLength) in mining.subscribe h…
Browse files Browse the repository at this point in the history
…andler
  • Loading branch information
nikicat committed Apr 15, 2024
1 parent a9826b6 commit bd58c79
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 47 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
2 changes: 1 addition & 1 deletion protocols/v1/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ pub trait IsClient<'a> {
let subscribe = self
.subscribe(
configure.id,
Some(Extranonce::try_from(hex::decode("08000002")?)?),
Some(Extranonce::try_from("08000002")?),
)
.ok();
Ok(subscribe)
Expand Down
12 changes: 6 additions & 6 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 @@ -320,7 +320,7 @@ impl<'a> TryFrom<StandardRequest> for Subscribe<'a> {
// bosminer subscribe message
[JString(a), Null, JString(_), Null] => (a.into(), None),
[JString(a), JString(b)] => {
(a.into(), Some(Extranonce::try_from(hex::decode(b)?)?))
(a.into(), Some(Extranonce::try_from(b.as_str())?))
}
[JString(a)] => (a.into(), None),
[] => ("".to_string(), None),
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
38 changes: 1 addition & 37 deletions protocols/v1/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,10 @@ impl<'a> Extranonce<'a> {
}
}

fn prepend_u8_to_vec(vec: &Vec<u8>, num: u8) -> Vec<u8> {
let mut new_vec = vec![num]; // Create a new vector with the u8 integer at the beginning
new_vec.extend(vec.iter().cloned()); // Append the existing Vec<u8> to the new vector
new_vec
}

impl<'a> TryFrom<Vec<u8>> for Extranonce<'a> {
type Error = Error<'a>;
fn try_from(value: Vec<u8>) -> Result<Self, Error<'a>> {
if value.len() % 2 != 0 {
Ok(Extranonce(B032::try_from(prepend_u8_to_vec(&value, 0))?))
} else {
Ok(Extranonce(B032::try_from(value)?))
}
Ok(Extranonce(B032::try_from(value)?))
}
}

Expand Down Expand Up @@ -406,30 +396,4 @@ mod tests {

be_hex == back_to_hex && be_hex == value_to_string
}

#[test]
fn test_try_from_for_even_sized_extranonce() {
// Given a Vec<u8> suitable for conversion
let input_vec = vec![1, 2, 3, 4];

// When converting to Extranonce
let result = Extranonce::try_from(input_vec);

// Then, conversion should succeed and length should be correct
assert!(result.is_ok());
assert_eq!(result.unwrap().len(), 4);
}

#[test]
fn test_try_from_for_odd_sized_extranonce() {
// Given a Vec<u8> suitable for conversion
let input_vec = vec![1, 2, 3];

// When converting to Extranonce
let result = Extranonce::try_from(input_vec);

// Then, conversion should succeed and length should be correct
assert!(result.is_ok());
assert_eq!(result.unwrap().len(), 4);
}
}

0 comments on commit bd58c79

Please sign in to comment.