Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preview automatically mine blocks #2809

Merged
merged 10 commits into from
Dec 4, 2023
44 changes: 38 additions & 6 deletions src/subcommand/preview.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use {super::*, fee_rate::FeeRate};
use {super::*, fee_rate::FeeRate, std::sync::atomic};

#[derive(Debug, Parser)]
pub(crate) struct Preview {
Expand All @@ -10,6 +10,8 @@ pub(crate) struct Preview {
help = "Inscribe inscriptions defined in <BATCHES>."
)]
batches: Option<Vec<PathBuf>>,
#[arg(long, help = "Automatically mine a block every <BLOCKTIME> seconds.")]
blocktime: Option<u64>,
#[arg(num_args = 0.., long, help = "Inscribe contents of <FILES>.")]
files: Option<Vec<PathBuf>>,
}
Expand Down Expand Up @@ -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()
raphjaph marked this conversation as resolved.
Show resolved Hide resolved

Ok(Box::new(Empty {}))
}
}
28 changes: 27 additions & 1 deletion tests/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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", "<script>alert('LFG!')</script>")
Expand Down Expand Up @@ -62,4 +62,30 @@ fn preview() {
.unwrap(),
format!(".*(<a href=/inscription/.*){{{}}}.*", 5)
);

let blockheight = reqwest::blocking::get(format!("http://127.0.0.1:{port}/blockheight"))
.unwrap()
.text()
.unwrap()
.parse::<u64>()
.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::<u64>()
.unwrap()
> blockheight
{
break;
}

thread::sleep(Duration::from_millis(250));
}
}
Loading