@@ -137,31 +137,34 @@ impl BlockWeightsCache {
137
137
138
138
let chain_height = self . tip_height + 1 ;
139
139
140
- let new_long_term_start_height = chain_height
141
- . saturating_sub ( self . config . long_term_window )
142
- . saturating_sub ( numb_blocks) ;
143
-
144
- let old_long_term_weights = get_long_term_weight_in_range (
145
- new_long_term_start_height
146
- // current_chain_height - self.long_term_weights.len() blocks are already in the cache.
147
- ..( chain_height - self . long_term_weights . window_len ( ) ) ,
148
- database. clone ( ) ,
149
- Chain :: Main ,
150
- )
151
- . await ?;
152
-
153
- let new_short_term_start_height = chain_height
154
- . saturating_sub ( self . config . short_term_window )
155
- . saturating_sub ( numb_blocks) ;
140
+ let old_long_term_weights = if let Some ( new_long_term_start_height) = chain_height
141
+ . checked_sub ( self . config . long_term_window + numb_blocks)
142
+ {
143
+ get_long_term_weight_in_range (
144
+ new_long_term_start_height
145
+ // current_chain_height - self.long_term_weights.len() blocks are already in the cache.
146
+ ..( new_long_term_start_height + numb_blocks) ,
147
+ database. clone ( ) ,
148
+ Chain :: Main ,
149
+ )
150
+ . await ?
151
+ } else {
152
+ vec ! [ ]
153
+ } ;
156
154
157
- let old_short_term_weights = get_blocks_weight_in_range (
158
- new_short_term_start_height
159
- // current_chain_height - self.long_term_weights.len() blocks are already in the cache.
160
- ..( chain_height - self . short_term_block_weights . window_len ( ) ) ,
161
- database,
162
- Chain :: Main ,
163
- )
164
- . await ?;
155
+ let old_short_term_weights =if let Some ( new_short_term_start_height) = chain_height
156
+ . checked_sub ( self . config . short_term_window + numb_blocks) {
157
+ get_blocks_weight_in_range (
158
+ new_short_term_start_height
159
+ // current_chain_height - self.long_term_weights.len() blocks are already in the cache.
160
+ ..( min ( numb_blocks, self . short_term_block_weights . window_len ( ) ) + new_short_term_start_height) ,
161
+ database,
162
+ Chain :: Main ,
163
+ )
164
+ . await ?
165
+ } else {
166
+ vec ! [ ]
167
+ } ;
165
168
166
169
for _ in 0 ..numb_blocks {
167
170
self . short_term_block_weights . pop_back ( ) ;
0 commit comments