• 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

95.0
/ergo-p2p/src/peer_database/in_memory.rs
1
use ergo_chain_types::PeerAddr;
2

3
use crate::peer_info::PeerInfo;
4

5
use super::{PeerDatabase, PeerDatabaseError, PeerMap};
6

7
/// In-memory peer database implementation
8
#[derive(Default, Debug)]
9
pub struct InMemoryPeerDatabase {
10
    peers: PeerMap,
11
}
12

13
impl PeerDatabase for InMemoryPeerDatabase {
14
    fn get_peer_by_addr(&self, addr: PeerAddr) -> Option<PeerInfo> {
6✔
15
        Some(self.peers.get(&addr)?.clone())
6✔
16
    }
6✔
17

18
    fn add_or_update_peer(
28✔
19
        &mut self,
28✔
20
        peer_info: PeerInfo,
28✔
21
    ) -> Result<Option<PeerInfo>, PeerDatabaseError> {
28✔
22
        if let Some(addr) = peer_info.spec().addr() {
28✔
23
            Ok(self.peers.insert(addr, peer_info))
20✔
24
        } else {
25
            Err(PeerDatabaseError::NoPeerAddr)
8✔
26
        }
27
    }
28✔
28

29
    fn remove_peer(&mut self, addr: PeerAddr) -> Result<(), PeerDatabaseError> {
2✔
30
        self.peers
2✔
31
            .remove(&addr)
2✔
32
            .ok_or(PeerDatabaseError::NotFound)?;
2✔
33

34
        Ok(())
2✔
35
    }
2✔
36

37
    fn known_peers(&self) -> PeerMap {
×
38
        self.peers.clone()
×
UNCOV
39
    }
×
40
}
41

42
/// Arbitrary
43
#[allow(unused_must_use)]
44
#[cfg(feature = "arbitrary")]
45
pub mod arbitrary {
46
    use super::*;
47
    use proptest::collection::vec;
48
    use proptest::prelude::*;
49
    use proptest::prelude::{Arbitrary, BoxedStrategy};
50

51
    impl Arbitrary for InMemoryPeerDatabase {
52
        type Parameters = ();
53
        type Strategy = BoxedStrategy<Self>;
54

55
        fn arbitrary_with(_: Self::Parameters) -> Self::Strategy {
8✔
56
            (vec(any::<PeerInfo>(), 1..5))
8✔
57
                .prop_map(|infos| {
8✔
58
                    let mut db = Self::default();
8✔
59
                    for i in infos {
26✔
60
                        db.add_or_update_peer(i);
18✔
61
                    }
18✔
62
                    db
8✔
63
                })
8✔
64
                .boxed()
8✔
65
        }
8✔
66
    }
67

68
    impl InMemoryPeerDatabase {
69
        /// Seed database with a peer
70
        pub fn with_peer(mut self, info: PeerInfo) -> InMemoryPeerDatabase {
6✔
71
            self.add_or_update_peer(info);
6✔
72
            self
6✔
73
        }
6✔
74
    }
75
}
76

77
#[allow(clippy::unwrap_used)]
78
#[cfg(test)]
79
#[cfg(feature = "arbitrary")]
80
mod tests {
81
    use super::*;
82

83
    use sigma_test_util::force_any_val;
84

85
    #[test]
86
    fn get_peer_by_addr() {
2✔
87
        let info = force_any_val::<PeerInfo>().with_ensured_addr();
2✔
88
        let db = force_any_val::<InMemoryPeerDatabase>().with_peer(info.clone());
2✔
89
        let result = db.get_peer_by_addr(info.spec().addr().unwrap()).unwrap();
2✔
90

91
        assert_eq!(info, result);
2✔
92
    }
2✔
93

94
    // Test adding peer in the case its not already in the db
95
    #[test]
96
    fn add_or_update_peer_doesnt_exist() {
2✔
97
        let mut db = force_any_val::<InMemoryPeerDatabase>();
2✔
98
        let info = force_any_val::<PeerInfo>().with_ensured_addr();
2✔
99
        db.add_or_update_peer(info.clone()).unwrap();
2✔
100

101
        assert!(db.get_peer_by_addr(info.spec().addr().unwrap()).is_some())
2✔
102
    }
2✔
103

104
    // Test adding peer in the case it already exists
105
    #[test]
106
    fn add_or_update_peer_exists() {
2✔
107
        let info = force_any_val::<PeerInfo>().with_ensured_addr();
2✔
108
        let mut db = force_any_val::<InMemoryPeerDatabase>().with_peer(info.clone());
2✔
109
        let result = db.add_or_update_peer(info.clone()).unwrap().unwrap();
2✔
110

111
        // add or update returned the original peer_info
112
        assert_eq!(result, info)
2✔
113
    }
2✔
114

115
    #[test]
116
    fn remove_peer_db_contains_peer() {
2✔
117
        let info = force_any_val::<PeerInfo>().with_ensured_addr();
2✔
118
        let mut db = force_any_val::<InMemoryPeerDatabase>().with_peer(info.clone());
2✔
119
        let result = db.remove_peer(info.spec().addr().unwrap());
2✔
120

121
        assert!(result.is_ok());
2✔
122
        assert!(db.get_peer_by_addr(info.spec().addr().unwrap()).is_none())
2✔
123
    }
2✔
124
}
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