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

ergoplatform / sigma-rust / 19957094785

05 Dec 2025 08:23AM UTC coverage: 86.918% (+8.5%) from 78.463%
19957094785

Pull #837

github

web-flow
Merge dec08367a into 2f840d387
Pull Request #837: Split TransactionHintsBag hints properly

44 of 53 new or added lines in 13 files covered. (83.02%)

1621 existing lines in 221 files now uncovered.

27453 of 31585 relevant lines covered (86.92%)

253204.4 hits per line

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

91.67
/ergo-chain-types/src/json/autolykos_solution.rs
1
//! Code to implement `AutolykosSolution` JSON encoding
2

3
use alloc::string::String;
4
use alloc::string::ToString;
5
use alloc::vec::Vec;
6
use core::str::FromStr;
7
use num_bigint::BigUint;
8

9
use num_traits::FromPrimitive;
10
use serde::{Deserialize, Deserializer};
11

12
pub(crate) fn as_base16_string<S>(value: &[u8], serializer: S) -> Result<S::Ok, S::Error>
22✔
13
where
22✔
14
    S: serde::Serializer,
22✔
15
{
16
    serializer.serialize_str(&base16::encode_lower(value))
22✔
17
}
22✔
18

19
pub(crate) fn from_base16_string<'de, D, T: From<Vec<u8>>>(deserializer: D) -> Result<T, D::Error>
322✔
20
where
322✔
21
    D: Deserializer<'de>,
322✔
22
{
23
    use serde::de::Error;
24
    String::deserialize(deserializer)
322✔
25
        .and_then(|string| base16::decode(&string).map_err(|err| Error::custom(err.to_string())))
322✔
26
        .map(From::from)
322✔
27
}
322✔
28

29
/// Serialize `BigInt` as a string
30
pub(crate) fn bigint_as_str<S>(value: &Option<BigUint>, serializer: S) -> Result<S::Ok, S::Error>
11✔
31
where
11✔
32
    S: serde::Serializer,
11✔
33
{
34
    if let Some(value) = value {
11✔
35
        serializer.serialize_str(&value.to_string())
11✔
36
    } else {
37
        serializer.serialize_unit()
×
38
    }
39
}
11✔
40

41
/// Deserialize a `BigInt` instance from either a String or from a `serde_json::Number` value.  We
42
/// need to do this because the JSON specification allows for arbitrarily-large numbers, a feature
43
/// that Autolykos makes use of to encode the PoW-distance (d) parameter. Note that we also need to
44
/// use `serde_json` with the `arbitrary_precision` feature for this to work.
45
pub(crate) fn bigint_from_serde_json_number<'de, D>(
163✔
46
    deserializer: D,
163✔
47
) -> Result<Option<BigUint>, D::Error>
163✔
48
where
163✔
49
    D: Deserializer<'de>,
163✔
50
{
51
    use serde::de::Error;
52

53
    match DeserializeBigIntFrom::deserialize(deserializer) {
163✔
54
        Ok(s) => match s {
163✔
55
            DeserializeBigIntFrom::String(s) => BigUint::from_str(&s)
12✔
56
                .map(Some)
12✔
57
                .map_err(|e| Error::custom(e.to_string())),
12✔
58
            DeserializeBigIntFrom::SerdeJsonNumber(n) => {
151✔
59
                let bigint = if n.is_f64() {
151✔
60
                    let n_f64 = n
1✔
61
                        .as_f64()
1✔
62
                        .ok_or_else(|| Error::custom("failed to convert JSON number to f64"))?;
1✔
63

64
                    BigUint::from_f64(n_f64).ok_or_else(|| {
1✔
65
                        Error::custom("failed to create BigInt from f64".to_string())
×
UNCOV
66
                    })
×
67
                } else {
68
                    BigUint::from_str(&n.to_string()).map_err(|e| Error::custom(e.to_string()))
150✔
69
                };
70

71
                bigint.map(Some)
151✔
72
            }
73
        },
74
        Err(e) => Err(Error::custom(e.to_string())),
×
75
    }
76
}
163✔
77

78
#[derive(Deserialize)]
79
#[serde(untagged)]
80
enum DeserializeBigIntFrom {
81
    String(String),
82
    SerdeJsonNumber(serde_json::Number),
83
}
84

85
#[allow(clippy::unwrap_used)]
86
#[cfg(test)]
87
mod tests {
88
    use serde::de::IntoDeserializer;
89
    use serde_json::Value;
90

91
    use super::bigint_from_serde_json_number;
92

93
    #[test]
94
    fn test_scientific_notion_deser() {
1✔
95
        let pow_d_parameter = r#"4.69094608138843e64"#;
1✔
96
        let j: Value = serde_json::from_str(pow_d_parameter).unwrap();
1✔
97
        let result = bigint_from_serde_json_number(j.into_deserializer()).unwrap();
1✔
98

99
        assert_eq!(
1✔
100
            result.unwrap().to_string(),
1✔
101
            "46909460813884301641411510982628556119846083366464832536248844288"
102
        )
103
    }
1✔
104
}
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

© 2025 Coveralls, Inc