Skip to content

Commit 167f4c6

Browse files
committed
feat: only use snapshots if ENV present
1 parent 8ca8053 commit 167f4c6

File tree

2 files changed

+97
-27
lines changed

2 files changed

+97
-27
lines changed

testnet/stacks-node/src/tests/signer/mod.rs

Lines changed: 89 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use std::collections::HashSet;
1919
use std::path::PathBuf;
2020
use std::sync::atomic::{AtomicBool, Ordering};
2121
use std::sync::{Arc, Mutex};
22-
use std::thread;
2322
use std::time::{Duration, Instant};
23+
use std::{env, thread};
2424

2525
use clarity::boot_util::boot_code_id;
2626
use clarity::vm::types::PrincipalData;
@@ -110,6 +110,16 @@ pub struct SignerTest<S> {
110110
pub snapshot_path: Option<PathBuf>,
111111
}
112112

113+
struct SnapshotSetupInfo {
114+
snapshot_path: PathBuf,
115+
snapshot_exists: bool,
116+
}
117+
118+
enum SetupSnapshotResult {
119+
WithSnapshot(SnapshotSetupInfo),
120+
NoSnapshot,
121+
}
122+
113123
impl<S: Signer<T> + Send + 'static, T: SignerEventTrait + 'static> SignerTest<SpawnedSigner<S, T>> {
114124
pub fn new(num_signers: usize, initial_balances: Vec<(StacksAddress, u64)>) -> Self {
115125
Self::new_with_config_modifications(
@@ -232,35 +242,42 @@ impl<S: Signer<T> + Send + 'static, T: SignerEventTrait + 'static> SignerTest<Sp
232242
vec![pk]
233243
});
234244

235-
let mut snapshot_exists = false;
245+
let snapshot_setup_result = Self::setup_snapshot(snapshot_name, &naka_conf);
236246

237-
let snapshot_path = snapshot_name.map(|name| {
238-
let working_dir = naka_conf.get_working_dir();
247+
// let mut snapshot_exists = false;
239248

240-
let snapshot_path: PathBuf = format!("/tmp/stacks-node-tests/snapshots/{name}/")
241-
.try_into()
242-
.unwrap();
249+
// let snapshot_path = snapshot_name.map(|name| {
250+
// let working_dir = naka_conf.get_working_dir();
243251

244-
info!("Snapshot path: {}", snapshot_path.clone().display());
245-
246-
snapshot_exists = std::fs::metadata(snapshot_path.clone()).is_ok();
247-
248-
if snapshot_exists {
249-
info!(
250-
"Snapshot directory already exists, copying to working dir";
251-
"snapshot_path" => %snapshot_path.display(),
252-
"working_dir" => %working_dir.display()
253-
);
254-
let err_msg = format!(
255-
"Failed to copy snapshot dir to working dir: {} -> {}",
256-
snapshot_path.display(),
257-
working_dir.display()
258-
);
259-
copy_dir_all(snapshot_path.clone(), working_dir).expect(&err_msg);
260-
}
252+
// let snapshot_path: PathBuf = format!("/tmp/stacks-node-tests/snapshots/{name}/")
253+
// .try_into()
254+
// .unwrap();
261255

262-
snapshot_path
263-
});
256+
// info!("Snapshot path: {}", snapshot_path.clone().display());
257+
258+
// snapshot_exists = std::fs::metadata(snapshot_path.clone()).is_ok();
259+
260+
// if snapshot_exists {
261+
// info!(
262+
// "Snapshot directory already exists, copying to working dir";
263+
// "snapshot_path" => %snapshot_path.display(),
264+
// "working_dir" => %working_dir.display()
265+
// );
266+
// let err_msg = format!(
267+
// "Failed to copy snapshot dir to working dir: {} -> {}",
268+
// snapshot_path.display(),
269+
// working_dir.display()
270+
// );
271+
// copy_dir_all(snapshot_path.clone(), working_dir).expect(&err_msg);
272+
// }
273+
274+
// snapshot_path
275+
// });
276+
277+
let snapshot_exists = match &snapshot_setup_result {
278+
SetupSnapshotResult::WithSnapshot(info) => info.snapshot_exists,
279+
SetupSnapshotResult::NoSnapshot => false,
280+
};
264281

265282
let node = setup_stx_btc_node(
266283
naka_conf,
@@ -280,7 +297,10 @@ impl<S: Signer<T> + Send + 'static, T: SignerEventTrait + 'static> SignerTest<Sp
280297
stacks_client,
281298
num_stacking_cycles: 12_u64,
282299
signer_configs,
283-
snapshot_path,
300+
snapshot_path: match &snapshot_setup_result {
301+
SetupSnapshotResult::WithSnapshot(info) => Some(info.snapshot_path.clone()),
302+
SetupSnapshotResult::NoSnapshot => None,
303+
},
284304
}
285305
}
286306

@@ -302,6 +322,48 @@ impl<S: Signer<T> + Send + 'static, T: SignerEventTrait + 'static> SignerTest<Sp
302322
!std::fs::metadata(snapshot_path).is_ok()
303323
}
304324

325+
/// Setup a snapshot by copying the snapshot directory to the working directory.
326+
///
327+
/// If the env variable `STACKS_TEST_SNAPSHOT` is not set, this will return `NoSnapshot`.
328+
fn setup_snapshot(snapshot_name: Option<&str>, conf: &NeonConfig) -> SetupSnapshotResult {
329+
let Some(snapshot_name) = snapshot_name else {
330+
return SetupSnapshotResult::NoSnapshot;
331+
};
332+
333+
if env::var("STACKS_TEST_SNAPSHOT") != Ok("1".into()) {
334+
return SetupSnapshotResult::NoSnapshot;
335+
}
336+
337+
let working_dir = conf.get_working_dir();
338+
339+
let snapshot_path: PathBuf = format!("/tmp/stacks-node-tests/snapshots/{snapshot_name}/")
340+
.try_into()
341+
.unwrap();
342+
343+
info!("Snapshot path: {}", snapshot_path.clone().display());
344+
345+
let snapshot_exists = std::fs::metadata(snapshot_path.clone()).is_ok();
346+
347+
if snapshot_exists {
348+
info!(
349+
"Snapshot directory already exists, copying to working dir";
350+
"snapshot_path" => %snapshot_path.display(),
351+
"working_dir" => %working_dir.display()
352+
);
353+
let err_msg = format!(
354+
"Failed to copy snapshot dir to working dir: {} -> {}",
355+
snapshot_path.display(),
356+
working_dir.display()
357+
);
358+
copy_dir_all(snapshot_path.clone(), working_dir).expect(&err_msg);
359+
}
360+
361+
SetupSnapshotResult::WithSnapshot(SnapshotSetupInfo {
362+
snapshot_path,
363+
snapshot_exists,
364+
})
365+
}
366+
305367
/// Make a snapshot of the current working directory.
306368
///
307369
/// This will stop the bitcoind node and copy the working directory to the snapshot path.

testnet/stacks-node/src/tests/signer/v0.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,16 @@ impl SignerTest<SpawnedSigner> {
324324
/// This will also shutdown the test early, requiring a restart
325325
/// of the test.
326326
///
327+
/// If the test is not configured to use snapshots, it will boot to epoch 3.0
328+
/// and continue.
329+
///
327330
/// Returns `true` if the snapshot was created.
328331
pub fn bootstrap_snapshot(&self) -> bool {
332+
if self.snapshot_path.is_none() {
333+
self.boot_to_epoch_3();
334+
return false;
335+
}
336+
329337
if self.needs_snapshot() {
330338
self.boot_to_epoch_3();
331339
self.make_snapshot();

0 commit comments

Comments
 (0)