Skip to content

Commit

Permalink
migrate from hex to faster-hex
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesFoundation committed Jul 31, 2024
1 parent 1642b9b commit 8ecabbe
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 41 deletions.
2 changes: 1 addition & 1 deletion stratum-v1/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ license = "GPL-3.0-or-later AND GPL-3.0-only"

[dependencies]
anyhow = { workspace = true }
faster-hex = { version = "0.9", default-features = false, git = "https://github.com/Georges760/faster-hex.git", branch = "no-alloc" }
heapless = { workspace = true, features = ["serde"] }
hex = { version = "0.4.3", default-features = false, features = ["serde", "heapless"], git = "https://github.com/Georges760/rust-hex.git", branch = "heapless" }
json-rpc-types = { workspace = true, features = ["id-number-only"] }
serde = { workspace = true }
serde-json-core = { workspace = true, features = ["custom-error-messages"] }
20 changes: 10 additions & 10 deletions stratum-v1/src/client_not.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: GPL-3.0-or-later

use anyhow::{Error, Result};
use faster_hex::hex_decode;
use heapless::{String, Vec};
use serde::Deserialize;

Expand Down Expand Up @@ -51,26 +52,25 @@ pub fn parse_notification_method(resp: &[u8]) -> Result<String<32>> {
/// ```
/// use stratum_v1::client_not::parse_notification_set_version_mask;
///
/// let resp = br#"{"params":["00003000"], "id":null, "method": "mining.set_version_mask"}"#;
/// let resp = br#"{"params":["1fffe000"], "id":null, "method": "mining.set_version_mask"}"#;
/// let r = parse_notification_set_version_mask(resp);
/// println!("{:?}", r);
/// assert_eq!(parse_notification_set_version_mask(resp).unwrap(), 0x00003000);
/// assert_eq!(parse_notification_set_version_mask(resp).unwrap(), 0x1fff_e000);
/// ```
pub fn parse_notification_set_version_mask(resp: &[u8]) -> Result<u32> {
#[derive(Deserialize)]
struct SetVersionMaskNotificationParams(
// mask: The meaning is the same as the "version-rolling.mask" return parameter.
#[serde(deserialize_with = "hex::deserialize")] Vec<u8, 8>,
);
let v: Vec<u8, 4> = hex::decode_heapless(
let mut v: Vec<u8, 4> = Vec::new();
v.resize(4, 0).unwrap();
hex_decode(
serde_json_core::from_slice::<json_rpc_types::Request<Vec<String<8>, 1>, String<64>>>(resp)
.map_err(Error::msg)?
.0
.params
.unwrap()
.pop()
.unwrap(),
.unwrap()
.as_bytes(),
&mut v,
)
.expect("decode error");
Ok(u32::from_be_bytes(v[0..4].try_into().unwrap()))
Ok(u32::from_be_bytes(v.into_array::<4>().unwrap()))
}
36 changes: 9 additions & 27 deletions stratum-v1/src/client_req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

use anyhow::{Error, Result};
use core::str::FromStr;
use faster_hex::hex_string;
use heapless::{String, Vec};
use hex::ToHex;
use json_rpc_types::{Id, Version};
use serde::Serialize;

Expand Down Expand Up @@ -139,7 +139,7 @@ pub fn configure_request(id: u64, exts: Extensions, buf: &mut [u8]) -> Result<us
.push(String::from_str("version-rolling").unwrap())
.unwrap();
if let Some(mask) = &version_rolling.mask {
ext_params.version_rolling_mask = Some(mask.to_be_bytes().encode_hex());
ext_params.version_rolling_mask = Some(hex_string::<8>(&mask.to_be_bytes()));
}
ext_params.version_rolling_min_bit_count = Some(version_rolling.min_bit_count);
}
Expand Down Expand Up @@ -335,32 +335,14 @@ pub fn submit_request(id: u64, share: Share, buf: &mut [u8]) -> Result<usize> {
let mut vec = Vec::<String<32>, 6>::new();
vec.push(share.user).map_err(Error::msg)?;
vec.push(share.job_id).map_err(Error::msg)?;
vec.push(
share
.extranonce2
.to_be_bytes()
.as_ref()
.encode_hex::<String<32>>(),
)
.map_err(Error::msg)?;
vec.push(
share
.ntime
.to_be_bytes()
.as_ref()
.encode_hex::<String<32>>(),
)
.map_err(Error::msg)?;
vec.push(
share
.nonce
.to_be_bytes()
.as_ref()
.encode_hex::<String<32>>(),
)
.map_err(Error::msg)?;
vec.push(hex_string::<32>(&share.extranonce2.to_be_bytes()))
.map_err(Error::msg)?;
vec.push(hex_string::<32>(&share.ntime.to_be_bytes()))
.map_err(Error::msg)?;
vec.push(hex_string::<32>(&share.nonce.to_be_bytes()))
.map_err(Error::msg)?;
if let Some(v) = share.version_bits {
vec.push(v.to_be_bytes().as_ref().encode_hex::<String<32>>())
vec.push(hex_string::<32>(&v.to_be_bytes()))
.map_err(Error::msg)?;
}
let params = Some(vec);
Expand Down
9 changes: 6 additions & 3 deletions stratum-v1/src/client_resp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: GPL-3.0-or-later

use anyhow::{Error, Result};
use faster_hex::hex_decode;
use heapless::{String, Vec};
use serde::Deserialize;

Expand Down Expand Up @@ -130,7 +131,7 @@ pub fn parse_response_configure(resp: &[u8]) -> Result<ConfigureResp> {

#[derive(Debug, Deserialize, PartialEq)]
pub struct ConnectResp(
// Subscriptions details - 2-tuple with name of subscribed notification and subscription ID. Teoretically it may be used for unsubscribing, but obviously miners won't use it.
// Subscriptions details - 2-tuple with name of subscribed notification and subscription ID. Theoretically it may be used for unsubscribing, but obviously miners won't use it.
Vec<Vec<String<32>, 2>, 2>,
// Extranonce1 - Hex-encoded, per-connection unique string which will be used for coinbase serialization later. Keep it safe!
String<8>,
Expand All @@ -144,8 +145,10 @@ impl ConnectResp {
}

pub fn extranonce1(&self) -> u32 {
let v: Vec<u8, 4> = hex::decode_heapless(&self.1).expect("decode error");
u32::from_be_bytes(v[0..4].try_into().unwrap())
let mut v: Vec<u8, 4> = Vec::new();
v.resize(4, 0).unwrap();
hex_decode(self.1.as_bytes(), &mut v).expect("decode error");
u32::from_be_bytes(v.into_array::<4>().unwrap())
}

pub fn extranonce2_size(&self) -> usize {
Expand Down

0 comments on commit 8ecabbe

Please sign in to comment.