Skip to content

Commit c41a1be

Browse files
committed
misc(ledger/testchain-gen): minor cleanups
1 parent c9c4062 commit c41a1be

File tree

2 files changed

+42
-48
lines changed

2 files changed

+42
-48
lines changed

ledger/src/test_helpers/chain_builder.rs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@ use anyhow::{Context, Result};
3636
use indexmap::{IndexMap, IndexSet};
3737
use rand::SeedableRng;
3838
use rand_chacha::ChaChaRng;
39-
use std::{
40-
cmp,
41-
collections::{BTreeMap, HashMap},
42-
};
39+
use std::collections::{BTreeMap, HashMap};
4340
use time::OffsetDateTime;
4441

4542
pub 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,

ledger/testchain-generator/src/main.rs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,7 @@ use snarkvm_ledger::{
3232
use aleo_std::StorageMode;
3333
use anyhow::{Context, Result, bail};
3434
use clap::{Parser, builder::PossibleValuesParser};
35-
use std::{
36-
fs::{self, File},
37-
io::Read,
38-
path::Path,
39-
str::FromStr,
40-
};
35+
use std::{fs, path::Path, str::FromStr};
4136
use tracing::debug;
4237

4338
#[derive(Parser)]
@@ -120,24 +115,25 @@ fn generate_testchain<N: Network>(args: Args) -> Result<()> {
120115

121116
let mut txs = if let Some(path) = args.txs_path {
122117
let path = Path::new(&path);
123-
println!("Attempting to load txs from {}", path.display());
124118

125-
let mut txs = Vec::new();
126119
if path.is_dir() {
127-
let mut buffer = String::new();
128-
for entry in fs::read_dir(path)? {
129-
let entry = entry?;
130-
let path = entry.path();
131-
132-
let mut file = File::open(path)?;
133-
let _ = file.read_to_string(&mut buffer)?;
134-
let tx = Transaction::<N>::from_str(&buffer)?;
135-
txs.push(tx);
136-
buffer.clear();
137-
}
120+
println!("Attempting to load transactions from \"{}\"", path.display());
121+
} else {
122+
bail!("Cannot load transactions from \"{}\": not a valid directory", path.display());
123+
}
124+
125+
let mut txs = Vec::new();
126+
for entry in fs::read_dir(path)? {
127+
let entry = entry?;
128+
let path = entry.path();
129+
130+
let buffer = fs::read_to_string(path)?;
131+
let tx = Transaction::<N>::from_str(&buffer)?;
132+
133+
txs.push(tx);
138134
}
139135

140-
println!("Loaded {} txs from {}", txs.len(), path.display());
136+
println!("Loaded {} tranactionss from \"{}\"", txs.len(), path.display());
141137
txs
142138
} else {
143139
Default::default()

0 commit comments

Comments
 (0)