• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tari-project / tari / 16123384529

07 Jul 2025 05:11PM UTC coverage: 64.327% (-7.6%) from 71.89%
16123384529

push

github

web-flow
chore: new release v4.9.0-pre.0 (#7289)

Description
---
new release esmeralda

77151 of 119935 relevant lines covered (64.33%)

227108.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.43
/base_layer/core/src/proof_of_work/proof_of_work_algorithm.rs
1
// Copyright 2020. The Tari Project
2
//
3
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
4
// following conditions are met:
5
//
6
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
7
// disclaimer.
8
//
9
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
10
// following disclaimer in the documentation and/or other materials provided with the distribution.
11
//
12
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
13
// products derived from this software without specific prior written permission.
14
//
15
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
16
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
20
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
21
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22
use std::{
23
    convert::TryFrom,
24
    fmt::{Display, Formatter},
25
    str::FromStr,
26
};
27

28
use borsh::{BorshDeserialize, BorshSerialize};
29
use serde::{Deserialize, Serialize};
30

31
/// Indicates the algorithm used to mine a block
32
#[repr(u8)]
33
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Hash, Eq, BorshSerialize, BorshDeserialize)]
4,416✔
34
#[borsh(use_discriminant = true)]
35
pub enum PowAlgorithm {
36
    RandomXM = 0,
37
    Sha3x = 1,
38
    RandomXT = 2,
39
}
40

41
impl PowAlgorithm {
42
    /// Returns true if the PoW algorithm is merged mined monero RandomX
43
    pub fn is_merged_mined_randomx(&self) -> bool {
×
44
        matches!(self, Self::RandomXM)
×
45
    }
×
46

47
    /// Returns true if the PoW algorithm is solo tari RandomX
48
    pub fn is_tari_randomx(&self) -> bool {
×
49
        matches!(self, Self::RandomXT)
×
50
    }
×
51

52
    /// Returns true if the PoW algorithm is Sha3
53
    pub fn is_sha3(&self) -> bool {
×
54
        matches!(self, Self::Sha3x)
×
55
    }
×
56

57
    /// A convenience functions that returns the PoW algorithm as a u64
58
    pub fn as_u64(&self) -> u64 {
×
59
        *self as u64
×
60
    }
×
61
}
62

63
impl TryFrom<u64> for PowAlgorithm {
64
    type Error = String;
65

66
    fn try_from(v: u64) -> Result<Self, Self::Error> {
×
67
        match v {
×
68
            0 => Ok(PowAlgorithm::RandomXM),
×
69
            1 => Ok(PowAlgorithm::Sha3x),
×
70
            2 => Ok(PowAlgorithm::RandomXT),
×
71
            _ => Err("Invalid PoWAlgorithm".into()),
×
72
        }
73
    }
×
74
}
75

76
impl FromStr for PowAlgorithm {
77
    type Err = anyhow::Error;
78

79
    fn from_str(s: &str) -> Result<Self, Self::Err> {
21✔
80
        let s_trimmed = s.replace("\"", "").replace("\'", "").replace(" ", "").to_uppercase();
21✔
81
        match s_trimmed.as_str() {
21✔
82
            "RANDOMXM" | "RANDOM_XM" | "MONERO_RANDOM_X" | "RANDOMX" | "RANDOM_X" | "RANDOMXMONERO" => {
21✔
83
                Ok(Self::RandomXM)
7✔
84
            },
85
            "SHA" | "SHA3" | "SHA3X" => Ok(Self::Sha3x),
14✔
86
            "RANDOMXT" | "RANDOM_XT" | "TARI_RANDOM_X" | "RANDOMXTARI" => Ok(Self::RandomXT),
4✔
87
            _ => Err(anyhow::Error::msg(format!("Unknown pow algorithm type: {}", s))),
×
88
        }
89
    }
21✔
90
}
91

92
impl Display for PowAlgorithm {
93
    fn fmt(&self, fmt: &mut Formatter<'_>) -> std::fmt::Result {
1✔
94
        let algo = match self {
1✔
95
            PowAlgorithm::RandomXM => "RandomXMonero",
×
96
            PowAlgorithm::Sha3x => "Sha3",
1✔
97
            PowAlgorithm::RandomXT => "RandomXTari",
×
98
        };
99
        fmt.write_str(algo)
1✔
100
    }
1✔
101
}
102

103
#[cfg(test)]
104
mod tests {
105
    use serde_json;
106

107
    use super::*;
108

109
    #[test]
110
    fn test_pow_algorithm_from_str_variants() {
1✔
111
        // Test valid variants for RandomXM
1✔
112
        let randomxm_variants = vec![
1✔
113
            "RandomXM",
1✔
114
            "RandomX",
1✔
115
            "randomx",
1✔
116
            "random_x",
1✔
117
            "randomxm",
1✔
118
            "RANDOM_XM",
1✔
119
            "monero_random_x",
1✔
120
        ];
1✔
121
        for variant in randomxm_variants {
8✔
122
            let algo = PowAlgorithm::from_str(variant).expect("Failed to parse RandomXM variant");
7✔
123
            assert_eq!(algo, PowAlgorithm::RandomXM);
7✔
124
        }
125

126
        // Test valid variants for Sha3x
127
        let sha3x_variants = vec![
1✔
128
            "Sha3x",
1✔
129
            "\"Sha3x\"",
1✔
130
            "\'Sha3x\'",
1✔
131
            "Sha 3 x",
1✔
132
            "sha",
1✔
133
            "sha3",
1✔
134
            "SHA3",
1✔
135
            "sha3X",
1✔
136
            "Sha3X",
1✔
137
            "SHA3X",
1✔
138
        ];
1✔
139
        for variant in sha3x_variants {
11✔
140
            let algo = PowAlgorithm::from_str(variant).expect("Failed to parse Sha3x variant");
10✔
141
            assert_eq!(algo, PowAlgorithm::Sha3x);
10✔
142
        }
143

144
        // Test valid variants for RandomXT
145
        let randomxt_variants = vec!["RandomXT", "randomxt", "tari_random_x", "RANDOM_XT"];
1✔
146
        for variant in randomxt_variants {
5✔
147
            let algo = PowAlgorithm::from_str(variant).expect("Failed to parse RandomXT variant");
4✔
148
            assert_eq!(algo, PowAlgorithm::RandomXT);
4✔
149
        }
150
    }
1✔
151

152
    #[test]
153
    fn test_pow_algorithm_serialization() {
1✔
154
        for algo in [PowAlgorithm::Sha3x, PowAlgorithm::RandomXM, PowAlgorithm::RandomXT] {
3✔
155
            let serialized = serde_json::to_string(&algo).expect("Failed to serialize PowAlgorithm");
3✔
156
            let deserialized: PowAlgorithm =
3✔
157
                serde_json::from_str(&serialized).expect("Failed to deserialize PowAlgorithm");
3✔
158
            assert_eq!(deserialized, algo);
3✔
159
        }
160
    }
1✔
161
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc