1-
2-
31// src/data_types.rs
42
5- use std:: borrow:: Cow ;
6- use std:: hash:: { Hash , Hasher , DefaultHasher } ;
7- use std:: path:: PathBuf ;
8- use std:: io:: Write ;
93use reqwest:: blocking;
104use serde:: { Deserialize , Serialize } ;
5+ use std:: borrow:: Cow ;
6+ use std:: hash:: { DefaultHasher , Hash , Hasher } ;
7+ use std:: io:: Write ;
8+ use std:: path:: PathBuf ;
119
1210// ===============================================
1311// API RESPONSE STRUCTS (Minimal subset)
@@ -67,7 +65,6 @@ pub struct DonateResponse {
6765 pub donation_id : String ,
6866}
6967
70-
7168#[ derive( Debug , Deserialize ) ]
7269pub struct ApiErrorResponse {
7370 pub message : String ,
@@ -143,7 +140,6 @@ pub struct MiningContext {
143140 pub data_dir : Option < String > ,
144141}
145142
146-
147143// Holds the data needed to submit a solution later.
148144#[ derive( Debug , Deserialize , Serialize , Clone ) ]
149145pub struct PendingSolution {
@@ -152,7 +148,7 @@ pub struct PendingSolution {
152148 pub nonce : String ,
153149 pub donation_address : Option < String > ,
154150 // FIX: Add fields for error logging and identification
155- pub preimage : String , // The full string used for hashing
151+ pub preimage : String , // The full string used for hashing
156152 pub hash_output : String , // The final Blake2b hash output (hex encoded)
157153}
158154
@@ -168,7 +164,6 @@ pub struct FailedSolution {
168164 pub hash_output : String ,
169165}
170166
171-
172167// Define a result type for the mining cycle
173168#[ derive( Debug , PartialEq ) ]
174169pub enum MiningResult {
@@ -197,7 +192,10 @@ pub enum SubmitterCommand {
197192 SaveState ( String , String ) , // Key, Value
198193 /// Command to retrieve data from SLED (used for synchronous lookups like next index).
199194 /// Value is sent back on the provided response channel.
200- GetState ( String , std:: sync:: mpsc:: Sender < Result < Option < String > , String > > ) ,
195+ GetState (
196+ String ,
197+ std:: sync:: mpsc:: Sender < Result < Option < String > , String > > ,
198+ ) ,
201199 /// Command to initiate solution submission (used in non-WS mode).
202200 SubmitSolution ( PendingSolution ) ,
203201 /// Signal to gracefully shut down the submitter.
@@ -217,14 +215,12 @@ pub struct BackupEntry {
217215 pub value : String ,
218216}
219217
220-
221218// --- DataDir Structures and Constants (Kept for Migration/Compatibility) ---
222219pub const FILE_NAME_CHALLENGE : & str = "challenge.json" ;
223220pub const FILE_NAME_RECEIPT : & str = "receipt.json" ;
224221pub const FILE_NAME_FOUND_SOLUTION : & str = "found.json" ;
225222pub const SLED_KEY_FAILED_SOLUTION : & str = "failed_solution" ; // FIX: Added new Sled key prefix
226223
227-
228224#[ derive( Debug , Clone , Copy ) ]
229225pub enum DataDir < ' a > {
230226 Persistent ( & ' a str ) ,
@@ -239,15 +235,15 @@ pub struct DataDirMnemonic<'a> {
239235 pub deriv_index : u32 ,
240236}
241237
242- fn normalize_challenge_id ( challenge_id : & str ) -> Cow < str > {
238+ fn normalize_challenge_id ( challenge_id : & str ) -> String {
243239 #[ cfg( target_os = "windows" ) ]
244240 {
245241 // Directories with '*' are not supported on windows
246- challenge_id. replace ( "*" , "" ) . into ( )
242+ challenge_id. replace ( "*" , "" )
247243 }
248244 #[ cfg( not( target_os = "windows" ) ) ]
249245 {
250- challenge_id. into ( )
246+ challenge_id. to_string ( )
251247 }
252248}
253249
@@ -258,7 +254,7 @@ impl<'a> DataDir<'a> {
258254 let challenge_id_normalized = normalize_challenge_id ( challenge_id) ;
259255
260256 let mut path = PathBuf :: from ( base_dir) ;
261- path. push ( challenge_id_normalized . as_ref ( ) ) ;
257+ path. push ( normalize_challenge_id ( challenge_id ) ) ;
262258 Ok ( path)
263259 }
264260
@@ -269,11 +265,11 @@ impl<'a> DataDir<'a> {
269265 DataDir :: Persistent ( mining_address) => {
270266 path. push ( "persistent" ) ;
271267 path. push ( mining_address) ;
272- } ,
268+ }
273269 DataDir :: Ephemeral ( mining_address) => {
274270 path. push ( "ephemeral" ) ;
275271 path. push ( mining_address) ;
276- } ,
272+ }
277273 DataDir :: Mnemonic ( wallet) => {
278274 path. push ( "mnemonic" ) ;
279275
@@ -300,8 +296,12 @@ impl<'a> DataDir<'a> {
300296 let mut path = self . challenge_dir ( base_dir, & challenge. challenge_id ) ?;
301297 path. push ( FILE_NAME_CHALLENGE ) ;
302298
303- let challenge_json = serde_json:: to_string ( challenge)
304- . map_err ( |e| format ! ( "Could not serialize challenge {}: {}" , & challenge. challenge_id, e) ) ?;
299+ let challenge_json = serde_json:: to_string ( challenge) . map_err ( |e| {
300+ format ! (
301+ "Could not serialize challenge {}: {}" ,
302+ & challenge. challenge_id, e
303+ )
304+ } ) ?;
305305
306306 std:: fs:: write ( & path, challenge_json)
307307 . map_err ( |e| format ! ( "Could not write {}: {}" , FILE_NAME_CHALLENGE , e) ) ?;
@@ -310,14 +310,23 @@ impl<'a> DataDir<'a> {
310310 }
311311
312312 // Saves a PendingSolution to the queue directory
313- pub fn save_pending_solution ( & self , base_dir : & str , solution : & PendingSolution ) -> Result < ( ) , String > {
313+ pub fn save_pending_solution (
314+ & self ,
315+ base_dir : & str ,
316+ solution : & PendingSolution ,
317+ ) -> Result < ( ) , String > {
314318 let mut path = PathBuf :: from ( base_dir) ;
315319 path. push ( "pending_submissions" ) ; // Dedicated directory for the queue
316320 std:: fs:: create_dir_all ( & path)
317321 . map_err ( |e| format ! ( "Could not create pending_submissions directory: {}" , e) ) ?;
318322
319323 // Use a unique file name based on challenge, address, and nonce
320- path. push ( format ! ( "{}_{}_{}.json" , solution. address, normalize_challenge_id( & solution. challenge_id) , solution. nonce) ) ;
324+ path. push ( format ! (
325+ "{}_{}_{}.json" ,
326+ solution. address,
327+ normalize_challenge_id( & solution. challenge_id) ,
328+ solution. nonce
329+ ) ) ;
321330
322331 let solution_json = serde_json:: to_string ( solution)
323332 . map_err ( |e| format ! ( "Could not serialize pending solution: {}" , e) ) ?;
@@ -329,7 +338,12 @@ impl<'a> DataDir<'a> {
329338 }
330339
331340 // Saves the temporary file indicating a solution was found but not queued/submitted
332- pub fn save_found_solution ( & self , base_dir : & str , challenge_id : & str , solution : & PendingSolution ) -> Result < ( ) , String > {
341+ pub fn save_found_solution (
342+ & self ,
343+ base_dir : & str ,
344+ challenge_id : & str ,
345+ solution : & PendingSolution ,
346+ ) -> Result < ( ) , String > {
333347 let mut path = self . receipt_dir ( base_dir, challenge_id) ?; // Use receipt dir for local persistence
334348 path. push ( FILE_NAME_FOUND_SOLUTION ) ;
335349
@@ -362,7 +376,11 @@ impl<'a> DataDir<'a> {
362376}
363377
364378// Checks if an address/challenge has a pending submission file in the queue dir
365- pub fn is_solution_pending_in_queue ( base_dir : & str , address : & str , challenge_id : & str ) -> Result < bool , String > {
379+ pub fn is_solution_pending_in_queue (
380+ base_dir : & str ,
381+ address : & str ,
382+ challenge_id : & str ,
383+ ) -> Result < bool , String > {
366384 use std:: path:: PathBuf ;
367385
368386 let mut path = PathBuf :: from ( base_dir) ;
@@ -374,7 +392,12 @@ pub fn is_solution_pending_in_queue(base_dir: &str, address: &str, challenge_id:
374392 if let Some ( filename) = entry. file_name ( ) . to_str ( ) {
375393 // Check if the filename starts with the required prefix and is a JSON file
376394 // The filename format is: address_challenge_id_nonce.json
377- if filename. starts_with ( & format ! ( "{}_{}_" , address, normalize_challenge_id( & challenge_id) ) ) && filename. ends_with ( ".json" ) {
395+ if filename. starts_with ( & format ! (
396+ "{}_{}_" ,
397+ address,
398+ normalize_challenge_id( & challenge_id)
399+ ) ) && filename. ends_with ( ".json" )
400+ {
378401 return Ok ( true ) ;
379402 }
380403 }
0 commit comments