Skip to content

Commit

Permalink
Preview can mine blocks (ordinals#2809)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph committed Dec 4, 2023
1 parent f638793 commit 06ac7ba
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
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()

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:https://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:https://127.0.0.1:{port}/blockheight"))
.unwrap()
.text()
.unwrap()
.parse::<u64>()
.unwrap()
> blockheight
{
break;
}

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

0 comments on commit 06ac7ba

Please sign in to comment.