Skip to content

Commit

Permalink
Add no sync option to server command (#2966)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph authored Jan 6, 2024
1 parent b9b1ddd commit 809847a
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 10 deletions.
8 changes: 6 additions & 2 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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));
});
Expand Down
3 changes: 2 additions & 1 deletion src/subcommand/wallet/inscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@ pub(crate) struct Inscribe {
pub(crate) json_metadata: Option<PathBuf>,
#[clap(long, help = "Set inscription metaprotocol to <METAPROTOCOL>.")]
pub(crate) metaprotocol: Option<String>,
#[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,
Expand Down
13 changes: 7 additions & 6 deletions tests/command_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub(crate) struct CommandBuilder {
rpc_server_cookie_file: Option<PathBuf>,
rpc_server_url: Option<String>,
stdin: Vec<u8>,
tempdir: TempDir,
tempdir: Arc<TempDir>,
}

impl CommandBuilder {
Expand All @@ -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()),
}
}

Expand Down Expand Up @@ -98,7 +98,7 @@ impl CommandBuilder {
}
}

pub(crate) fn temp_dir(self, tempdir: TempDir) -> Self {
pub(crate) fn temp_dir(self, tempdir: Arc<TempDir>) -> Self {
Self { tempdir, ..self }
}

Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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<Path>) -> String {
Expand Down
2 changes: 1 addition & 1 deletion tests/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
1 change: 1 addition & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use {
regex::Regex,
reqwest::{StatusCode, Url},
serde::de::DeserializeOwned,
std::sync::Arc,
std::{
collections::BTreeMap,
fs,
Expand Down
69 changes: 69 additions & 0 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit 809847a

Please sign in to comment.