Skip to main content

vanity_core/
estimate.rs

1use crate::pattern::Pattern;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4pub enum PatternRisk {
5    None,
6    Caution,
7    Long,
8    Impractical,
9}
10
11impl PatternRisk {
12    pub fn assess(attempts: f64, pattern_chars: usize, avg_secs: f64) -> Self {
13        if attempts >= 1e15 || pattern_chars >= 10 || avg_secs >= 86400.0 * 365.0 * 10.0 {
14            PatternRisk::Impractical
15        } else if attempts >= 1e12 || pattern_chars >= 8 || avg_secs >= 86400.0 * 30.0 {
16            PatternRisk::Long
17        } else if attempts >= 1e9 || pattern_chars >= 6 || avg_secs >= 86400.0 {
18            PatternRisk::Caution
19        } else {
20            PatternRisk::None
21        }
22    }
23
24    pub fn as_str(self) -> &'static str {
25        match self {
26            PatternRisk::None => "none",
27            PatternRisk::Caution => "caution",
28            PatternRisk::Long => "long",
29            PatternRisk::Impractical => "impractical",
30        }
31    }
32}
33
34#[derive(Debug, Clone)]
35pub struct GrindEstimate {
36    pub attempts: f64,
37    pub attempts_label: String,
38    pub avg_secs: f64,
39    pub time_label: String,
40    pub difficulty: &'static str,
41    pub difficulty_bars: String,
42    pub risk: PatternRisk,
43    pub pattern_chars: usize,
44}
45
46pub fn effective_pattern_chars(pattern: &Pattern) -> usize {
47    pattern.effective_literal_chars()
48}
49
50pub fn format_attempts(n: f64) -> String {
51    if n >= 1_000_000_000_000.0 {
52        format!("{:.1}T", n / 1_000_000_000_000.0)
53    } else if n >= 1_000_000_000.0 {
54        format!("{:.1}B", n / 1_000_000_000.0)
55    } else if n >= 1_000_000.0 {
56        format!("{:.1}M", n / 1_000_000.0)
57    } else if n >= 1_000.0 {
58        format!("{:.1}K", n / 1_000.0)
59    } else {
60        format!("{:.0}", n)
61    }
62}
63
64pub fn format_duration(avg_secs: f64) -> String {
65    if avg_secs < 5.0 {
66        "a few seconds".to_string()
67    } else if avg_secs < 60.0 {
68        format!("~{:.0} seconds", avg_secs)
69    } else if avg_secs < 3_600.0 {
70        format!("~{:.0} minutes", avg_secs / 60.0)
71    } else if avg_secs < 86_400.0 {
72        format!("~{:.1} hours", avg_secs / 3_600.0)
73    } else if avg_secs < 86_400.0 * 30.0 {
74        format!("~{:.0} days", avg_secs / 86_400.0)
75    } else if avg_secs < 86_400.0 * 365.0 {
76        format!("~{:.0} months", avg_secs / (86_400.0 * 30.0))
77    } else if avg_secs < 86_400.0 * 365.0 * 100.0 {
78        format!("~{:.0} years", avg_secs / (86_400.0 * 365.0))
79    } else {
80        "centuries+ — not practical on one machine".to_string()
81    }
82}
83
84/// Single-thread heuristic throughput when no live benchmark is available (wasm / SDK).
85pub fn default_keys_per_sec(chain_id: &str) -> f64 {
86    match chain_id {
87        "evm" | "eth" | "ethereum" | "aptos" | "apt" | "sui" | "near" => 35_000.0,
88        _ => 80_000.0,
89    }
90}
91
92pub fn grind_estimate(attempts: f64, keys_per_sec: f64, pattern: &Pattern) -> GrindEstimate {
93    let keys_per_sec = keys_per_sec.max(1.0);
94    let avg_secs = attempts / keys_per_sec;
95    let pattern_chars = effective_pattern_chars(pattern);
96    let risk = PatternRisk::assess(attempts, pattern_chars, avg_secs);
97
98    let (difficulty, filled) = match attempts {
99        a if a < 100_000.0 => ("Easy", 2),
100        a if a < 10_000_000.0 => ("Quick", 3),
101        a if a < 1_000_000_000.0 => ("Medium", 5),
102        a if a < 1_000_000_000_000.0 => ("Hard", 7),
103        _ => ("Extreme", 10),
104    };
105
106    GrindEstimate {
107        attempts,
108        attempts_label: format_attempts(attempts),
109        avg_secs,
110        time_label: format_duration(avg_secs),
111        difficulty,
112        difficulty_bars: format!("{}{}", "█".repeat(filled), "░".repeat(10 - filled)),
113        risk,
114        pattern_chars,
115    }
116}
117
118#[cfg(test)]
119mod tests {
120    use super::*;
121    use crate::pattern::Pattern;
122
123    fn suffix_pattern(suffix: &str) -> Pattern {
124        Pattern {
125            prefix: String::new(),
126            suffix: suffix.to_string(),
127            contains: String::new(),
128            prefix_match: String::new(),
129            suffix_match: suffix.to_ascii_lowercase(),
130            contains_match: String::new(),
131            ignore_case: true,
132        }
133    }
134
135    #[test]
136    fn long_suffix_is_impractical() {
137        let p = suffix_pattern("akshaysingh");
138        let est = grind_estimate(1e16, 720_000.0, &p);
139        assert_eq!(est.risk, PatternRisk::Impractical);
140    }
141
142    #[test]
143    fn short_suffix_is_easy() {
144        let p = suffix_pattern("ab");
145        let est = grind_estimate(3_364.0, 720_000.0, &p);
146        assert_eq!(est.risk, PatternRisk::None);
147    }
148}