@@ -19,8 +19,8 @@ use std::collections::HashSet;
19
19
use std:: path:: PathBuf ;
20
20
use std:: sync:: atomic:: { AtomicBool , Ordering } ;
21
21
use std:: sync:: { Arc , Mutex } ;
22
- use std:: thread;
23
22
use std:: time:: { Duration , Instant } ;
23
+ use std:: { env, thread} ;
24
24
25
25
use clarity:: boot_util:: boot_code_id;
26
26
use clarity:: vm:: types:: PrincipalData ;
@@ -110,6 +110,16 @@ pub struct SignerTest<S> {
110
110
pub snapshot_path : Option < PathBuf > ,
111
111
}
112
112
113
+ struct SnapshotSetupInfo {
114
+ snapshot_path : PathBuf ,
115
+ snapshot_exists : bool ,
116
+ }
117
+
118
+ enum SetupSnapshotResult {
119
+ WithSnapshot ( SnapshotSetupInfo ) ,
120
+ NoSnapshot ,
121
+ }
122
+
113
123
impl < S : Signer < T > + Send + ' static , T : SignerEventTrait + ' static > SignerTest < SpawnedSigner < S , T > > {
114
124
pub fn new ( num_signers : usize , initial_balances : Vec < ( StacksAddress , u64 ) > ) -> Self {
115
125
Self :: new_with_config_modifications (
@@ -232,35 +242,42 @@ impl<S: Signer<T> + Send + 'static, T: SignerEventTrait + 'static> SignerTest<Sp
232
242
vec ! [ pk]
233
243
} ) ;
234
244
235
- let mut snapshot_exists = false ;
245
+ let snapshot_setup_result = Self :: setup_snapshot ( snapshot_name , & naka_conf ) ;
236
246
237
- let snapshot_path = snapshot_name. map ( |name| {
238
- let working_dir = naka_conf. get_working_dir ( ) ;
247
+ // let mut snapshot_exists = false;
239
248
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();
243
251
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();
261
255
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
+ } ;
264
281
265
282
let node = setup_stx_btc_node (
266
283
naka_conf,
@@ -280,7 +297,10 @@ impl<S: Signer<T> + Send + 'static, T: SignerEventTrait + 'static> SignerTest<Sp
280
297
stacks_client,
281
298
num_stacking_cycles : 12_u64 ,
282
299
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
+ } ,
284
304
}
285
305
}
286
306
@@ -302,6 +322,48 @@ impl<S: Signer<T> + Send + 'static, T: SignerEventTrait + 'static> SignerTest<Sp
302
322
!std:: fs:: metadata ( snapshot_path) . is_ok ( )
303
323
}
304
324
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
+
305
367
/// Make a snapshot of the current working directory.
306
368
///
307
369
/// This will stop the bitcoind node and copy the working directory to the snapshot path.
0 commit comments