@@ -36,10 +36,7 @@ use anyhow::{Context, Result};
3636use indexmap:: { IndexMap , IndexSet } ;
3737use rand:: SeedableRng ;
3838use rand_chacha:: ChaChaRng ;
39- use std:: {
40- cmp,
41- collections:: { BTreeMap , HashMap } ,
42- } ;
39+ use std:: collections:: { BTreeMap , HashMap } ;
4340use time:: OffsetDateTime ;
4441
4542pub type CurrentNetwork = MainnetV0 ;
@@ -222,7 +219,7 @@ impl<N: Network> TestChainBuilder<N> {
222219 /// This function panics if called from an async context.
223220 pub fn generate_blocks_with_opts (
224221 & mut self ,
225- mut num_blocks : usize ,
222+ num_blocks : usize ,
226223 mut options : GenerateBlocksOptions < N > ,
227224 rng : & mut TestRng ,
228225 ) -> Result < Vec < Block < N > > > {
@@ -232,34 +229,35 @@ impl<N: Network> TestChainBuilder<N> {
232229
233230 // If configured, skip enough blocks to reach the current consensus version.
234231 if options. skip_to_current_version {
235- let ( version, height ) = TEST_CONSENSUS_VERSION_HEIGHTS . last ( ) . unwrap ( ) ;
232+ let ( version, target_height ) = TEST_CONSENSUS_VERSION_HEIGHTS . last ( ) . unwrap ( ) ;
236233 let mut current_height = self . ledger . latest_height ( ) ;
237- println ! ( "Skipping {height} blocks to reach {version}" ) ;
238-
239- while current_height < * height && result. len ( ) < num_blocks {
240- let options = GenerateBlockOptions {
241- skip_votes : options. skip_votes ,
242- skip_nodes : options. skip_nodes . clone ( ) ,
243- ..Default :: default ( )
244- } ;
245-
246- let block = self . generate_block_with_opts ( options, rng) ?;
247- current_height = block. height ( ) ;
248- result. push ( block) ;
249- }
250234
251- // Subtract the number of placeholder blocks from the number of blocks to generate.
252- assert ! ( result. len( ) <= num_blocks) ;
253- num_blocks -= result. len ( ) ;
235+ let diff = target_height. saturating_sub ( current_height) ;
236+
237+ if diff > 0 {
238+ println ! ( "Skipping {diff} blocks to reach {version}" ) ;
239+
240+ while current_height < * target_height && result. len ( ) < num_blocks {
241+ let options = GenerateBlockOptions {
242+ skip_votes : options. skip_votes ,
243+ skip_nodes : options. skip_nodes . clone ( ) ,
244+ ..Default :: default ( )
245+ } ;
254246
255- println ! ( "Advanced to the current consensus version" ) ;
247+ let block = self . generate_block_with_opts ( options, rng) ?;
248+ current_height = block. height ( ) ;
249+ result. push ( block) ;
250+ }
251+
252+ println ! ( "Advanced to the current consensus version at height {target_height}" ) ;
253+ } else {
254+ debug ! ( "Already at the current consensus version. No blocks to skip." ) ;
255+ }
256256 }
257257
258- for _ in 0 ..num_blocks {
259- let num_txs = cmp:: min (
260- BatchHeader :: < N > :: MAX_TRANSMISSIONS_PER_BATCH * options. num_validators ,
261- options. transactions . len ( ) ,
262- ) ;
258+ while result. len ( ) < num_blocks {
259+ let num_txs = ( BatchHeader :: < N > :: MAX_TRANSMISSIONS_PER_BATCH * options. num_validators )
260+ . min ( options. transactions . len ( ) ) ;
263261
264262 let options = GenerateBlockOptions {
265263 skip_votes : options. skip_votes ,
0 commit comments