diff --git a/src/subcommand/preview.rs b/src/subcommand/preview.rs index bd06d2c18b..f0dc76d69c 100644 --- a/src/subcommand/preview.rs +++ b/src/subcommand/preview.rs @@ -1,4 +1,4 @@ -use {super::*, fee_rate::FeeRate}; +use {super::*, fee_rate::FeeRate, std::sync::atomic}; #[derive(Debug, Parser)] pub(crate) struct Preview { @@ -10,6 +10,8 @@ pub(crate) struct Preview { help = "Inscribe inscriptions defined in ." )] batches: Option>, + #[arg(long, help = "Automatically mine a block every seconds.")] + blocktime: Option, #[arg(num_args = 0.., long, help = "Inscribe contents of .")] files: Option>, } @@ -160,12 +162,42 @@ impl Preview { } } - rpc_client.generate_to_address(1, &address)?; + if let Some(blocktime) = self.blocktime { + eprintln!( + "Mining blocks every {}...", + "second".tally(blocktime.try_into().unwrap()) + ); - Arguments { - options, - subcommand: Subcommand::Server(self.server), + let running = Arc::new(AtomicBool::new(true)); + + let handle = { + let running = running.clone(); + + std::thread::spawn(move || { + while running.load(atomic::Ordering::SeqCst) { + rpc_client.generate_to_address(1, &address).unwrap(); + thread::sleep(Duration::from_secs(blocktime)); + } + }) + }; + + Arguments { + options, + subcommand: Subcommand::Server(self.server), + } + .run()?; + + running.store(false, atomic::Ordering::SeqCst); + + handle.join().unwrap(); + } else { + Arguments { + options, + subcommand: Subcommand::Server(self.server), + } + .run()?; } - .run() + + Ok(Box::new(Empty {})) } } diff --git a/tests/core.rs b/tests/core.rs index 827da9f14e..80a60da8dd 100644 --- a/tests/core.rs +++ b/tests/core.rs @@ -22,7 +22,7 @@ fn preview() { .port(); let builder = CommandBuilder::new(format!( - "preview --http-port {port} --files alert.html inscription.txt --batches batch_1.yaml batch_2.yaml" + "preview --http-port {port} --files alert.html inscription.txt --batches batch_1.yaml batch_2.yaml --blocktime 1" )) .write("inscription.txt", "Hello World") .write("alert.html", "") @@ -62,4 +62,30 @@ fn preview() { .unwrap(), format!(".*(() + .unwrap(); + + for attempt in 0.. { + if attempt == 20 { + panic!("Bitcoin Core did not mine blocks",); + } + + if reqwest::blocking::get(format!("http://127.0.0.1:{port}/blockheight")) + .unwrap() + .text() + .unwrap() + .parse::() + .unwrap() + > blockheight + { + break; + } + + thread::sleep(Duration::from_millis(250)); + } }