From 809847a0984ed2d7589e5955ae6aee4af20c46c1 Mon Sep 17 00:00:00 2001 From: raph Date: Sat, 6 Jan 2024 01:07:15 +0100 Subject: [PATCH] Add no sync option to server command (#2966) --- src/subcommand/server.rs | 8 +++- src/subcommand/wallet/inscribe.rs | 3 +- tests/command_builder.rs | 13 +++--- tests/index.rs | 2 +- tests/lib.rs | 1 + tests/server.rs | 69 +++++++++++++++++++++++++++++++ 6 files changed, 86 insertions(+), 10 deletions(-) diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index 925451d1c7..d1e60c57e5 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -170,6 +170,8 @@ pub(crate) struct Server { help = "Decompress encoded content. Currently only supports brotli. Be careful using this on production instances. A decompressed inscription may be arbitrarily large, making decompression a DoS vector." )] pub(crate) decompress: bool, + #[arg(long, alias = "nosync", help = "Do not update the index.")] + no_sync: bool, } impl Server { @@ -181,8 +183,10 @@ impl Server { if SHUTTING_DOWN.load(atomic::Ordering::Relaxed) { break; } - if let Err(error) = index_clone.update() { - log::warn!("Updating index: {error}"); + if !self.no_sync { + if let Err(error) = index_clone.update() { + log::warn!("Updating index: {error}"); + } } thread::sleep(Duration::from_millis(5000)); }); diff --git a/src/subcommand/wallet/inscribe.rs b/src/subcommand/wallet/inscribe.rs index 71dc9b039a..a27f88e438 100644 --- a/src/subcommand/wallet/inscribe.rs +++ b/src/subcommand/wallet/inscribe.rs @@ -85,10 +85,11 @@ pub(crate) struct Inscribe { pub(crate) json_metadata: Option, #[clap(long, help = "Set inscription metaprotocol to .")] pub(crate) metaprotocol: Option, - #[arg(long, help = "Do not back up recovery key.")] + #[arg(long, alias = "nobackup", help = "Do not back up recovery key.")] pub(crate) no_backup: bool, #[arg( long, + alias = "nolimit", help = "Do not check that transactions are equal to or below the MAX_STANDARD_TX_WEIGHT of 400,000 weight units. Transactions over this limit are currently nonstandard and will not be relayed by bitcoind in its default configuration. Do not use this flag unless you understand the implications." )] pub(crate) no_limit: bool, diff --git a/tests/command_builder.rs b/tests/command_builder.rs index 5cc9171c8e..01fc621fe2 100644 --- a/tests/command_builder.rs +++ b/tests/command_builder.rs @@ -36,7 +36,7 @@ pub(crate) struct CommandBuilder { rpc_server_cookie_file: Option, rpc_server_url: Option, stdin: Vec, - tempdir: TempDir, + tempdir: Arc, } impl CommandBuilder { @@ -49,7 +49,7 @@ impl CommandBuilder { rpc_server_cookie_file: None, rpc_server_url: None, stdin: Vec::new(), - tempdir: TempDir::new().unwrap(), + tempdir: Arc::new(TempDir::new().unwrap()), } } @@ -98,7 +98,7 @@ impl CommandBuilder { } } - pub(crate) fn temp_dir(self, tempdir: TempDir) -> Self { + pub(crate) fn temp_dir(self, tempdir: Arc) -> Self { Self { tempdir, ..self } } @@ -124,7 +124,7 @@ impl CommandBuilder { .stdin(Stdio::piped()) .stdout(Stdio::piped()) .stderr(Stdio::piped()) - .current_dir(&self.tempdir) + .current_dir(&*self.tempdir) .arg("--data-dir") .arg(self.tempdir.path()) .args(&self.args); @@ -134,7 +134,8 @@ impl CommandBuilder { #[track_caller] fn run(self) -> (TempDir, String) { - let child = self.command().spawn().unwrap(); + let mut command = self.command(); + let child = command.spawn().unwrap(); child .stdin @@ -157,7 +158,7 @@ impl CommandBuilder { self.expected_stderr.assert_match(stderr); self.expected_stdout.assert_match(stdout); - (self.tempdir, stdout.into()) + (Arc::try_unwrap(self.tempdir).unwrap(), stdout.into()) } pub(crate) fn run_and_extract_file(self, path: impl AsRef) -> String { diff --git a/tests/index.rs b/tests/index.rs index 143f260af4..72e3a20ecf 100644 --- a/tests/index.rs +++ b/tests/index.rs @@ -95,7 +95,7 @@ fn export_inscription_number_to_id_tsv() { let tsv = CommandBuilder::new("index export --tsv foo.tsv") .rpc_server(&rpc_server) - .temp_dir(temp_dir) + .temp_dir(Arc::new(temp_dir)) .stdout_regex(r"\{\}\n") .run_and_extract_file("foo.tsv"); diff --git a/tests/lib.rs b/tests/lib.rs index f1d3ad25a4..5037851cca 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -23,6 +23,7 @@ use { regex::Regex, reqwest::{StatusCode, Url}, serde::de::DeserializeOwned, + std::sync::Arc, std::{ collections::BTreeMap, fs, diff --git a/tests/server.rs b/tests/server.rs index 71edfe1103..c7792fb279 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -492,3 +492,72 @@ fn inscription_transactions_are_stored_with_transaction_index() { StatusCode::NOT_FOUND, ); } + +#[test] +fn run_no_sync() { + let rpc_server = test_bitcoincore_rpc::spawn(); + + let port = TcpListener::bind("127.0.0.1:0") + .unwrap() + .local_addr() + .unwrap() + .port(); + + let tempdir = Arc::new(TempDir::new().unwrap()); + + let builder = CommandBuilder::new(format!("server --address 127.0.0.1 --http-port {port}",)) + .rpc_server(&rpc_server) + .temp_dir(tempdir.clone()); + + let mut command = builder.command(); + + let mut child = command.spawn().unwrap(); + + rpc_server.mine_blocks(1); + + for attempt in 0.. { + if let Ok(response) = reqwest::blocking::get(format!("http://localhost:{port}/blockheight")) { + if response.status() == 200 { + assert_eq!(response.text().unwrap(), "1"); + break; + } + } + + if attempt == 100 { + panic!("Server did not respond to status check",); + } + + thread::sleep(Duration::from_millis(50)); + } + + child.kill().unwrap(); + + let builder = CommandBuilder::new(format!( + "server --no-sync --address 127.0.0.1 --http-port {port}", + )) + .rpc_server(&rpc_server) + .temp_dir(tempdir); + + let mut command = builder.command(); + + let mut child = command.spawn().unwrap(); + + rpc_server.mine_blocks(2); + + for attempt in 0.. { + if let Ok(response) = reqwest::blocking::get(format!("http://localhost:{port}/blockheight")) { + if response.status() == 200 { + assert_eq!(response.text().unwrap(), "1"); + break; + } + } + + if attempt == 100 { + panic!("Server did not respond to status check",); + } + + thread::sleep(Duration::from_millis(50)); + } + + child.kill().unwrap(); +}