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

tari-project / tari / 17372468680

01 Sep 2025 08:43AM UTC coverage: 60.111% (-1.0%) from 61.105%
17372468680

push

github

SWvheerden
feat: improve header sync (#7421)

Description
---
When we have a valid PoW equal to or higher than what we have or what
has been advertised by the sync peer, we need to commit the headers, so
any downstream block sync error does not invalidate the headers that
have been downloaded.

_**Edit:** When block sync fails, we also make sure to swap to the best
PoW chain, also preserving banked headers if we do not need to reorg._

Closes #7342

Motivation and Context
---
See #7342

How Has This Been Tested?
---
System-level tests passed:
- Synching a previously synced node, a couple of days old.
- Syncing from scratch, encountering multiple block-sync failures, but
using the good PoW headers already banked.
```rust
   51952: 2025-08-18 16:07:02.097680100 [c::bn::block_sync] DEBUG Validating block body #22624 (PoW = RandomXTari, input(s): 1, output(s): 54, kernel(s): 2, latency: 108.00µs)
   51961: 2025-08-18 16:07:02.111648900 [c::bn::block_sync] DEBUG Validated in 14ms. Storing block body #22624 (PoW = RandomXTari, input(s): 1, output(s): 54, kernel(s): 2)
   51962: 2025-08-18 16:07:02.111668100 [c::bn::block_sync] TRACE Hash: <a class=hub.com/tari-project/tari/commit/91a1fbccc1f94e2aa93b6efdaf9c3fbf16d0588c">91a1fbccc<a href="https://github.com/tari-project/tari/commit/6fb8d66c20efe20d9ba71db70213e46332f9e8f5">3458b7ef444e605f62375077
   54024: 2025-08-18 16:11:06.389482300 [c::bn::block_sync] WARN  Peer did not supply all the blocks they claimed they had: Their claim - height: 74721, accumulated difficulty: <a class="double-link" href="https://github.com/tari-project/tari/commit/4089235547434023187024157117581153168163">408923554</a><a href="https://github.com/tari-project/tari/commit/6fb8d66c20efe20d9ba71db70213e46332f9e8f5">715651441378. Our status after block sync - height: 22624, accumulated difficulty: </a><a class="double-link" href="https://github.com/tari-project/tari/commit/1547640955349056263010512134240412613668">154764095</a><a href="https://github.com/tari-project/tari/commit/6fb8d66c20efe20d9ba71db70213e46332f9e8f5">889130919000
   54028: 2025-08-18 16:11:06.390047900 [c::bn::block_sync] WARN  Block sync failed - best header: 74723/1bcf293971df2d6c888a6589dca3a323b, best block: 22624/91a1fbccc1f94e2aa93b6efdaf9c3fbf16d0588c3458b7ef444e605f62375077. No more sync peers available: Block sync failed
   54109: 2025-08-18 16:11:11.596105400 [c::bn::header_sync] DEBUG Starting header sync.
   54110: 2025-08-18 16:11:11.59617... (continued)

33 of 132 new or added lines in 3 files covered. (25.0%)

1427 existing lines in 30 files now uncovered.

71522 of 118983 relevant lines covered (60.11%)

536188.7 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 {
56✔
32
        Self(Vec::with_capacity(capacity))
56✔
33
    }
56✔
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> {
97✔
39
        if self.capacity() == 0 {
97✔
40
            return None;
3✔
41
        }
94✔
42

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

48
        self.inner_mut().push(item);
94✔
49
        removed
94✔
50
    }
97✔
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 {
100✔
69
        // len never exceeds capacity
100✔
70
        debug_assert!(self.inner().len() <= self.inner().capacity());
100✔
71
        self.len() == self.capacity()
100✔
72
    }
100✔
73

74
    #[inline]
75
    pub fn capacity(&self) -> usize {
205✔
76
        self.inner().capacity()
205✔
77
    }
205✔
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)
24✔
82
    where T: Ord {
24✔
83
        self.inner_mut().sort_unstable();
24✔
84
    }
24✔
85

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

91
    #[inline]
92
    fn inner_mut(&mut self) -> &mut Vec<T> {
120✔
93
        &mut self.0
120✔
94
    }
120✔
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 {
169✔
120
        self.inner()
169✔
121
    }
169✔
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
    #![allow(clippy::indexing_slicing)]
135
    use super::*;
136

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

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

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

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

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

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

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

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

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

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