• 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

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> {
3✔
15
        Some(self.peers.get(&addr)?.clone())
3✔
16
    }
3✔
17

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

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

34
        Ok(())
1✔
35
    }
1✔
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 {
4✔
56
            (vec(any::<PeerInfo>(), 1..5))
4✔
57
                .prop_map(|infos| {
4✔
58
                    let mut db = Self::default();
4✔
59
                    for i in infos {
14✔
60
                        db.add_or_update_peer(i);
10✔
61
                    }
10✔
62
                    db
4✔
63
                })
4✔
64
                .boxed()
4✔
65
        }
4✔
66
    }
67

68
    impl InMemoryPeerDatabase {
69
        /// Seed database with a peer
70
        pub fn with_peer(mut self, info: PeerInfo) -> InMemoryPeerDatabase {
3✔
71
            self.add_or_update_peer(info);
3✔
72
            self
3✔
73
        }
3✔
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() {
1✔
87
        let info = force_any_val::<PeerInfo>().with_ensured_addr();
1✔
88
        let db = force_any_val::<InMemoryPeerDatabase>().with_peer(info.clone());
1✔
89
        let result = db.get_peer_by_addr(info.spec().addr().unwrap()).unwrap();
1✔
90

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

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

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

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

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

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

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