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

ergoplatform / sigma-rust / 19909456309

03 Dec 2025 09:29PM UTC coverage: 86.947% (+8.5%) from 78.463%
19909456309

Pull #838

github

web-flow
Merge 717ebc4b7 into 2f840d387
Pull Request #838: Fix CI, bump dependencies and rust toolchain

20 of 24 new or added lines in 12 files covered. (83.33%)

1614 existing lines in 221 files now uncovered.

27478 of 31603 relevant lines covered (86.95%)

506307.07 hits per line

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

96.49
/ergo-p2p/src/peer_feature.rs
1
//! PeerFeature types
2
use std::convert::TryInto;
3

4
use derive_more::{From, Into};
5

6
use ergo_chain_types::PeerAddr;
7
use sigma_ser::vlq_encode::WriteSigmaVlqExt;
8
use sigma_ser::{ScorexParsingError, ScorexSerializable, ScorexSerializeResult};
9

10
/// Peer feature identifier
11
#[derive(PartialEq, Eq, Debug, Copy, Clone, From, Into)]
12
pub struct PeerFeatureId(u8);
13

14
impl ScorexSerializable for PeerFeatureId {
15
    fn scorex_serialize<W: sigma_ser::vlq_encode::WriteSigmaVlqExt>(
118✔
16
        &self,
118✔
17
        w: &mut W,
118✔
18
    ) -> ScorexSerializeResult {
118✔
19
        w.put_u8(self.0)?;
118✔
20

21
        Ok(())
118✔
22
    }
118✔
23

24
    fn scorex_parse<R: sigma_ser::vlq_encode::ReadSigmaVlqExt>(
118✔
25
        r: &mut R,
118✔
26
    ) -> Result<Self, sigma_ser::ScorexParsingError> {
118✔
27
        Ok(Self(r.get_u8()?))
118✔
28
    }
118✔
29
}
30

31
/// Peer features
32
#[derive(PartialEq, Eq, Debug, Hash, Clone)]
33
pub enum PeerFeature {
34
    /// Local address peer feature
35
    LocalAddress(LocalAddressPeerFeature),
36
}
37

38
impl PeerFeature {
39
    /// Id of the peer feature
40
    pub fn id(&self) -> PeerFeatureId {
118✔
41
        match self {
118✔
42
            PeerFeature::LocalAddress(_) => PeerFeatureId(2),
118✔
43
        }
44
    }
118✔
45

46
    /// Return the feature as a LocalAddressPeerFeature if its of that type
47
    /// otherwise returns None
48
    pub fn as_local_addr(&self) -> Option<&LocalAddressPeerFeature> {
6✔
49
        match self {
6✔
50
            PeerFeature::LocalAddress(pf) => Some(pf),
6✔
51
        }
52
    }
6✔
53
}
54

55
impl ScorexSerializable for PeerFeature {
56
    fn scorex_serialize<W: WriteSigmaVlqExt>(&self, w: &mut W) -> ScorexSerializeResult {
118✔
57
        self.id().scorex_serialize(w)?;
118✔
58

59
        let bytes = match self {
118✔
60
            PeerFeature::LocalAddress(pf) => pf.scorex_serialize_bytes(),
118✔
UNCOV
61
        }?;
×
62

63
        w.put_u16(bytes.len().try_into()?)?;
118✔
64
        w.write_all(&bytes)?;
118✔
65

66
        Ok(())
118✔
67
    }
118✔
68

69
    fn scorex_parse<R: sigma_ser::vlq_encode::ReadSigmaVlqExt>(
118✔
70
        r: &mut R,
118✔
71
    ) -> Result<Self, sigma_ser::ScorexParsingError> {
118✔
72
        let feature_id = PeerFeatureId::scorex_parse(r)?;
118✔
73
        let feature_size = r.get_u16()?;
118✔
74
        let mut feature_buf = vec![0u8; feature_size as usize];
118✔
75
        r.read_exact(&mut feature_buf)?;
118✔
76

77
        let feature = match feature_id {
118✔
78
            PeerFeatureId(2) => PeerFeature::LocalAddress(
79
                LocalAddressPeerFeature::scorex_parse_bytes(&feature_buf)?,
118✔
80
            ),
81
            _ => return Err(ScorexParsingError::Misc("unknown feature id".into())),
×
82
        };
83

84
        Ok(feature)
118✔
85
    }
118✔
86
}
87

88
/// Arbitrary
89
#[cfg(feature = "arbitrary")]
90
pub mod arbitrary {
91
    use super::*;
92
    use proptest::prelude::*;
93
    use proptest::prelude::{Arbitrary, BoxedStrategy};
94

95
    impl Arbitrary for PeerFeature {
96
        type Parameters = ();
97
        type Strategy = BoxedStrategy<Self>;
98

99
        fn arbitrary_with(_: Self::Parameters) -> Self::Strategy {
18✔
100
            prop_oneof![any::<LocalAddressPeerFeature>().prop_map(PeerFeature::LocalAddress)]
18✔
101
                .boxed()
18✔
102
        }
18✔
103
    }
104
}
105

106
/// LocalAddressPeerFeature
107
#[cfg_attr(feature = "arbitrary", derive(proptest_derive::Arbitrary))]
108
#[derive(PartialEq, Eq, Debug, Hash, Clone, From, Into)]
109
pub struct LocalAddressPeerFeature(PeerAddr);
110

111
impl ScorexSerializable for LocalAddressPeerFeature {
112
    fn scorex_serialize<W: sigma_ser::vlq_encode::WriteSigmaVlqExt>(
246✔
113
        &self,
246✔
114
        w: &mut W,
246✔
115
    ) -> ScorexSerializeResult {
246✔
116
        self.0.scorex_serialize(w)?;
246✔
117

118
        Ok(())
246✔
119
    }
246✔
120

121
    fn scorex_parse<R: sigma_ser::vlq_encode::ReadSigmaVlqExt>(
246✔
122
        r: &mut R,
246✔
123
    ) -> Result<Self, sigma_ser::ScorexParsingError> {
246✔
124
        Ok(PeerAddr::scorex_parse(r)?.into())
246✔
125
    }
246✔
126
}
127

128
#[allow(clippy::panic)]
129
#[allow(clippy::unwrap_used)]
130
#[cfg(test)]
131
#[cfg(feature = "arbitrary")]
132
mod tests {
133
    use super::*;
134
    use proptest::prelude::*;
135
    use sigma_ser::scorex_serialize_roundtrip;
136

137
    proptest! {
138
        #![proptest_config(ProptestConfig::with_cases(64))]
139

140
        #[test]
141
        fn local_address_feature_ser_roundtrip(v in any::<LocalAddressPeerFeature>()) {
142
            assert_eq![scorex_serialize_roundtrip(&v), v]
143
        }
144
    }
145
}
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