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

tari-project / tari / 16272458029

14 Jul 2025 04:32PM UTC coverage: 57.167% (-0.9%) from 58.047%
16272458029

push

github

web-flow
feat: modify soft disconnect criteria (#7307)

Description
---
We can be more efficient with soft disconnects when we compare against
expected RPC sessions and substream counts. This PR adds finer
discernment when doing soft peer disconnects.

Motivation and Context
---
The health check opens 2 substreams and 0 PRC sessions - that should
result in a disconnect if those are the only opened resources.

How Has This Been Tested?
---
System-level testing
```rust
2025-07-11 13:52:26.703289300 [comms::connection_manager::peer_connection] TRACE Hard disconnect - requester: 'Health check', peer: `d7c289e9e3c8377705ce599a96`, RPC clients: 0, substreams 2
2025-07-11 13:52:26.705658100 [comms::connection_manager::peer_connection] TRACE Soft disconnect - requester: 'Health check', peer: `0984896e74022c442c1034852c`, RPC clients: 1, substreams 3, NOT disconnecting
2025-07-11 13:52:26.705735900 [comms::connection_manager::peer_connection] TRACE Hard disconnect - requester: 'Health check', peer: `d025bc9e4bd423a9b304c491b8`, RPC clients: 0, substreams 2
2025-07-11 13:52:26.707647400 [comms::connection_manager::peer_connection] TRACE Hard disconnect - requester: 'Health check', peer: `51af08aff11f7129b4681d9950`, RPC clients: 0, substreams 2
```

What process can a PR reviewer use to test or verify this change?
---
Code review

<!-- Checklist -->
<!-- 1. Is the title of your PR in the form that would make nice release
notes? The title, excluding the conventional commit
tag, will be included exactly as is in the CHANGELOG, so please think
about it carefully. -->


Breaking Changes
---

- [x] None
- [ ] Requires data directory on base node to be deleted
- [ ] Requires hard fork
- [ ] Other - Please specify

<!-- Does this include a breaking change? If so, include this line as a
footer -->
<!-- BREAKING CHANGE: Description what the user should do, e.g. delete a
database, resync the chain -->


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
##... (continued)

31 of 46 new or added lines in 6 files covered. (67.39%)

1102 existing lines in 27 files now uncovered.

68701 of 120177 relevant lines covered (57.17%)

226749.69 hits per line

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

85.71
/base_layer/core/src/common/rolling_vec.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

23
use std::ops::Deref;
24

25
/// A vector that contains up to a number of elements. As new elements are added to the end, the first elements are
26
/// removed.
27
#[derive(Debug)]
28
pub struct RollingVec<T>(Vec<T>);
29

30
impl<T> RollingVec<T> {
31
    pub fn new(capacity: usize) -> Self {
54✔
32
        Self(Vec::with_capacity(capacity))
54✔
33
    }
54✔
34

35
    /// Adds a new element to the RollingVec.
36
    /// If adding an element will cause the length to exceed the capacity, the first element is removed and the removed
37
    /// value is returned.
38
    pub fn push(&mut self, item: T) -> Option<T> {
92✔
39
        if self.capacity() == 0 {
92✔
40
            return None;
3✔
41
        }
89✔
42

89✔
43
        let mut removed = None;
89✔
44
        if self.is_full() {
89✔
45
            removed = Some(self.inner_mut().remove(0));
2✔
46
        }
87✔
47

48
        self.inner_mut().push(item);
89✔
49
        removed
89✔
50
    }
92✔
51

52
    pub fn insert(&mut self, index: usize, item: T) {
×
53
        assert!(index < self.capacity());
×
54
        assert!(index < self.len());
×
55

56
        if self.is_full() {
×
57
            self.inner_mut().remove(0);
×
58
        }
×
59

60
        self.inner_mut().insert(index, item);
×
61
    }
×
62

63
    pub fn pop(&mut self) -> Option<T> {
×
64
        self.inner_mut().pop()
×
65
    }
×
66

67
    #[inline]
68
    pub fn is_full(&self) -> bool {
95✔
69
        // len never exceeds capacity
95✔
70
        debug_assert!(self.inner().len() <= self.inner().capacity());
95✔
71
        self.len() == self.capacity()
95✔
72
    }
95✔
73

74
    #[inline]
75
    pub fn capacity(&self) -> usize {
195✔
76
        self.inner().capacity()
195✔
77
    }
195✔
78

79
    /// Sorts the slice, but might not preserve the order of equal elements.
80
    /// This sort is unstable (i.e., may reorder equal elements), in-place (i.e., does not allocate), and O(n * log(n))
81
    pub fn sort_unstable(&mut self)
22✔
82
    where T: Ord {
22✔
83
        self.inner_mut().sort_unstable();
22✔
84
    }
22✔
85

86
    #[inline]
87
    fn inner(&self) -> &Vec<T> {
547✔
88
        &self.0
547✔
89
    }
547✔
90

91
    #[inline]
92
    fn inner_mut(&mut self) -> &mut Vec<T> {
113✔
93
        &mut self.0
113✔
94
    }
113✔
95
}
96

97
impl<T> Extend<T> for RollingVec<T> {
98
    fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
6✔
99
        let iter = iter.into_iter();
6✔
100
        let (lower, _) = iter.size_hint();
6✔
101

102
        let skip = if lower > self.capacity() {
6✔
103
            // If the iterator will emit more than the capacity, skip over the first elements that will be pushed out of
104
            // the rolling window
105
            lower - self.capacity()
2✔
106
        } else {
107
            0
4✔
108
        };
109

110
        for item in iter.skip(skip) {
26✔
111
            self.push(item);
26✔
112
        }
26✔
113
    }
6✔
114
}
115

116
impl<T> Deref for RollingVec<T> {
117
    type Target = [T];
118

119
    fn deref(&self) -> &Self::Target {
162✔
120
        self.inner()
162✔
121
    }
162✔
122
}
123

124
impl<T: Clone> Clone for RollingVec<T> {
UNCOV
125
    fn clone(&self) -> Self {
×
UNCOV
126
        let mut v = Vec::with_capacity(self.capacity());
×
UNCOV
127
        v.extend(self.0.clone());
×
UNCOV
128
        Self(v)
×
UNCOV
129
    }
×
130
}
131

132
#[cfg(test)]
133
mod test {
134
    use super::*;
135

136
    #[test]
137
    fn it_is_always_empty_for_zero_capacity() {
1✔
138
        let mut subject = RollingVec::new(0);
1✔
139
        assert!(subject.is_empty());
1✔
140
        subject.push(123);
1✔
141
        assert!(subject.is_empty());
1✔
142
        assert_eq!(subject.len(), 0);
1✔
143
    }
1✔
144

145
    #[test]
146
    fn it_is_always_full_for_zero_capacity() {
1✔
147
        let mut subject = RollingVec::new(0);
1✔
148
        assert!(subject.is_full());
1✔
149
        subject.push(123);
1✔
150
        assert!(subject.is_full());
1✔
151
    }
1✔
152

153
    #[test]
154
    fn it_is_full_if_n_elements_are_added() {
1✔
155
        let mut subject = RollingVec::new(1);
1✔
156
        assert!(!subject.is_full());
1✔
157
        subject.push(1);
1✔
158
        assert!(subject.is_full());
1✔
159
    }
1✔
160

161
    #[test]
162
    fn it_rolls_over_as_elements_are_added() {
1✔
163
        let mut subject = RollingVec::new(1);
1✔
164
        subject.push(1);
1✔
165
        assert_eq!(subject.len(), 1);
1✔
166
        subject.push(2);
1✔
167
        assert_eq!(subject.len(), 1);
1✔
168
        assert_eq!(subject[0], 2);
1✔
169
    }
1✔
170

171
    #[test]
172
    fn it_extends_with_less_items_than_capacity() {
1✔
173
        let mut subject = RollingVec::new(5);
1✔
174
        let vec = (0..2).collect::<Vec<_>>();
1✔
175
        subject.extend(vec);
1✔
176

1✔
177
        assert_eq!(subject.len(), 2);
1✔
178
        assert!(!subject.is_full());
1✔
179

180
        assert_eq!(subject[0], 0);
1✔
181
        assert_eq!(subject[1], 1);
1✔
182
    }
1✔
183

184
    #[test]
185
    fn it_extends_without_exceeding_capacity() {
1✔
186
        let mut subject = RollingVec::new(5);
1✔
187
        let vec = (0..10).collect::<Vec<_>>();
1✔
188
        subject.extend(vec);
1✔
189

1✔
190
        assert_eq!(subject.len(), 5);
1✔
191
        assert!(subject.is_full());
1✔
192

193
        assert_eq!(subject[0], 5);
1✔
194
        assert_eq!(subject[1], 6);
1✔
195
        assert_eq!(subject[2], 7);
1✔
196
        assert_eq!(subject[3], 8);
1✔
197
        assert_eq!(subject[4], 9);
1✔
198
    }
1✔
199
}
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