|
| 1 | +// src/data_types.rs |
| 2 | + |
| 3 | +use serde_json; |
| 4 | +use std::hash::{Hash, Hasher, DefaultHasher}; |
| 5 | +use std::path::PathBuf; |
| 6 | +use std::ffi::OsStr; |
| 7 | +use crate::api::ChallengeData; |
| 8 | +use std::io::Write; // Added for file flushing |
| 9 | + |
| 10 | +// NEW: Define a result type for the mining cycle |
| 11 | +#[derive(Debug, PartialEq)] |
| 12 | +pub enum MiningResult { |
| 13 | + FoundAndSubmitted((serde_json::Value, Option<String>)), |
| 14 | + AlreadySolved, // The solution was successfully submitted by someone else |
| 15 | + MiningFailed, // General mining or submission error (e.g., hash not found, transient API error) |
| 16 | +} |
| 17 | + |
| 18 | +// --- DataDir Structures and Constants --- |
| 19 | +pub const FILE_NAME_CHALLENGE: &str = "challenge.json"; |
| 20 | +pub const FILE_NAME_RECEIPT: &str = "receipt.json"; |
| 21 | +pub const FILE_NAME_DONATION: &str = "donation.txt"; |
| 22 | + |
| 23 | + |
| 24 | +#[derive(Debug, Clone, Copy)] |
| 25 | +pub enum DataDir<'a> { |
| 26 | + Persistent(&'a str), |
| 27 | + Ephemeral(&'a str), |
| 28 | + Mnemonic(DataDirMnemonic<'a>), |
| 29 | +} |
| 30 | + |
| 31 | +#[derive(Debug, Clone, Copy)] |
| 32 | +pub struct DataDirMnemonic<'a> { |
| 33 | + pub mnemonic: &'a str, |
| 34 | + pub account: u32, |
| 35 | + pub deriv_index: u32, |
| 36 | +} |
| 37 | + |
| 38 | +impl<'a> DataDir<'a> { |
| 39 | + pub fn challenge_dir(&'a self, base_dir: &str, challenge_id: &str) -> Result<PathBuf, String> { |
| 40 | + let mut path = PathBuf::from(base_dir); |
| 41 | + path.push(challenge_id); |
| 42 | + Ok(path) |
| 43 | + } |
| 44 | + |
| 45 | + pub fn receipt_dir(&'a self, base_dir: &str, challenge_id: &str) -> Result<PathBuf, String> { |
| 46 | + let mut path = self.challenge_dir(base_dir, challenge_id)?; |
| 47 | + |
| 48 | + match self { |
| 49 | + DataDir::Persistent(mining_address) => { |
| 50 | + path.push("persistent"); |
| 51 | + path.push(mining_address); |
| 52 | + }, |
| 53 | + DataDir::Ephemeral(mining_address) => { |
| 54 | + path.push("ephemeral"); |
| 55 | + path.push(mining_address); |
| 56 | + }, |
| 57 | + DataDir::Mnemonic(wallet) => { |
| 58 | + path.push("mnemonic"); |
| 59 | + |
| 60 | + let mnemonic_hash = { |
| 61 | + let mut hasher = DefaultHasher::new(); |
| 62 | + wallet.mnemonic.hash(&mut hasher); |
| 63 | + hasher.finish() |
| 64 | + }; |
| 65 | + path.push(mnemonic_hash.to_string()); |
| 66 | + |
| 67 | + path.push(&wallet.account.to_string()); |
| 68 | + |
| 69 | + path.push(&wallet.deriv_index.to_string()); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + std::fs::create_dir_all(&path) |
| 74 | + .map_err(|e| format!("Could not create challenge directory: {}", e))?; |
| 75 | + |
| 76 | + Ok(path) |
| 77 | + } |
| 78 | + |
| 79 | + pub fn save_challenge(&self, base_dir: &str, challenge: &ChallengeData) -> Result<(), String> { |
| 80 | + let mut path = self.challenge_dir(base_dir, &challenge.challenge_id)?; |
| 81 | + path.push(FILE_NAME_CHALLENGE); |
| 82 | + |
| 83 | + let challenge_json = serde_json::to_string(challenge) |
| 84 | + .map_err(|e| format!("Could not serialize challenge {}: {}", &challenge.challenge_id, e))?; |
| 85 | + |
| 86 | + std::fs::write(&path, challenge_json) |
| 87 | + .map_err(|e| format!("Could not write {}: {}", FILE_NAME_CHALLENGE, e))?; |
| 88 | + |
| 89 | + Ok(()) |
| 90 | + } |
| 91 | + |
| 92 | + pub fn save_receipt(&self, base_dir: &str, challenge_id: &str, receipt: &serde_json::Value, donation: &Option<String>) -> Result<(), String> { |
| 93 | + let mut path = self.receipt_dir(base_dir, challenge_id)?; |
| 94 | + path.push(FILE_NAME_RECEIPT); |
| 95 | + |
| 96 | + let receipt_json = receipt.to_string(); |
| 97 | + |
| 98 | + // FIX: Use explicit file handling and sync to guarantee persistence. |
| 99 | + let mut file = std::fs::File::create(&path) |
| 100 | + .map_err(|e| format!("Could not create {}: {}", FILE_NAME_RECEIPT, e))?; |
| 101 | + |
| 102 | + file.write_all(receipt_json.as_bytes()) |
| 103 | + .map_err(|e| format!("Could not write to {}: {}", FILE_NAME_RECEIPT, e))?; |
| 104 | + |
| 105 | + // CRITICAL: Force the OS to write the data to disk now. |
| 106 | + file.sync_all() |
| 107 | + .map_err(|e| format!("Could not sync {}: {}", FILE_NAME_RECEIPT, e))?; |
| 108 | + |
| 109 | + if let Some(donation_id) = donation { |
| 110 | + path.pop(); |
| 111 | + path.push(FILE_NAME_DONATION); |
| 112 | + |
| 113 | + std::fs::write(&path, &donation_id) |
| 114 | + .map_err(|e| format!("Could not write {}: {}", FILE_NAME_DONATION, e))?; |
| 115 | + } |
| 116 | + |
| 117 | + Ok(()) |
| 118 | + } |
| 119 | +} |
0 commit comments