use alloc::string::String;
use crate::{
compute_mask_goaway,
message::GoAwayMessage,
solver::{SOLVE_TYPE_MASK, Solver},
};
#[derive(serde::Deserialize, Debug)]
pub struct GoAwayConfig {
challenge: String,
difficulty: core::num::NonZeroU8,
}
impl GoAwayConfig {
pub fn challenge(&self) -> &str {
&self.challenge
}
pub fn difficulty(&self) -> core::num::NonZeroU8 {
self.difficulty
}
pub fn estimated_workload(&self) -> u64 {
2u64.pow(self.difficulty.get().try_into().unwrap())
}
pub fn solve(&self) -> (Option<(u64, [u32; 8])>, u64) {
self.solve_with_limit(u64::MAX)
}
pub fn solve_with_limit(&self, limit: u64) -> (Option<(u64, [u32; 8])>, u64) {
let mask = compute_mask_goaway(self.difficulty);
let Some(message) = self
.challenge
.as_bytes()
.try_into()
.ok()
.and_then(GoAwayMessage::new_hex)
else {
return (None, 0);
};
let mut solver = crate::GoAwaySolver::from(message);
solver.set_limit(limit);
(
solver.solve::<{ SOLVE_TYPE_MASK }>(0, mask),
solver.get_attempted_nonces(),
)
}
}