Skip to content

Commit

Permalink
Move --enable-json-api flag to server options (#2599)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph authored Oct 26, 2023
1 parent 5b347c3 commit c6edde8
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 33 deletions.
4 changes: 0 additions & 4 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,10 +550,6 @@ impl Index {
self.unrecoverably_reorged.load(atomic::Ordering::Relaxed)
}

pub(crate) fn is_json_api_enabled(&self) -> bool {
self.options.enable_json_api
}

fn begin_read(&self) -> Result<rtx::Rtx> {
Ok(rtx::Rtx(self.database.begin_read()?))
}
Expand Down
2 changes: 0 additions & 2 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ pub(crate) struct Options {
pub(crate) testnet: bool,
#[arg(long, default_value = "ord", help = "Use wallet named <WALLET>.")]
pub(crate) wallet: String,
#[arg(long, short = 'j', help = "Enable JSON API.")]
pub(crate) enable_json_api: bool,
}

impl Options {
Expand Down
11 changes: 6 additions & 5 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ pub(crate) struct Server {
https: bool,
#[arg(long, help = "Redirect HTTP traffic to HTTPS.")]
redirect_http_to_https: bool,
#[arg(long, short = 'j', help = "Enable JSON API.")]
pub(crate) enable_json_api: bool,
}

impl Server {
Expand All @@ -169,7 +171,7 @@ impl Server {
INDEXER.lock().unwrap().replace(index_thread);

let server_config = Arc::new(ServerConfig {
is_json_api_enabled: index.is_json_api_enabled(),
is_json_api_enabled: self.enable_json_api,
});

let config = options.load_config()?;
Expand Down Expand Up @@ -1305,8 +1307,8 @@ mod tests {
.network(bitcoin::network::constants::Network::Regtest)
.build(),
None,
&["--chain", "regtest", "--enable-json-api"],
&[],
&["--chain", "regtest"],
&["--enable-json-api"],
)
}

Expand All @@ -1331,9 +1333,8 @@ mod tests {
"--chain",
"regtest",
"--index-runes-pre-alpha-i-agree-to-get-rekt",
"--enable-json-api",
],
&[],
&["--enable-json-api"],
)
}

Expand Down
37 changes: 19 additions & 18 deletions tests/json_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use {super::*, bitcoin::BlockHash};
fn get_sat_without_sat_index() {
let rpc_server = test_bitcoincore_rpc::spawn();

let response = TestServer::spawn_with_args(&rpc_server, &["--enable-json-api"])
let response = TestServer::spawn_with_server_args(&rpc_server, &[], &["--enable-json-api"])
.json_request("/sat/2099999997689999");

assert_eq!(response.status(), StatusCode::OK);
Expand Down Expand Up @@ -43,8 +43,9 @@ fn get_sat_with_inscription_and_sat_index() {

let (inscription_id, reveal) = inscribe(&rpc_server);

let response = TestServer::spawn_with_args(&rpc_server, &["--index-sats", "--enable-json-api"])
.json_request(format!("/sat/{}", 50 * COIN_VALUE));
let response =
TestServer::spawn_with_server_args(&rpc_server, &["--index-sats"], &["--enable-json-api"])
.json_request(format!("/sat/{}", 50 * COIN_VALUE));

assert_eq!(response.status(), StatusCode::OK);

Expand Down Expand Up @@ -95,8 +96,9 @@ fn get_sat_with_inscription_on_common_sat_and_more_inscriptions() {
index: 0,
};

let response = TestServer::spawn_with_args(&rpc_server, &["--index-sats", "--enable-json-api"])
.json_request(format!("/sat/{}", 3 * 50 * COIN_VALUE + 1));
let response =
TestServer::spawn_with_server_args(&rpc_server, &["--index-sats"], &["--enable-json-api"])
.json_request(format!("/sat/{}", 3 * 50 * COIN_VALUE + 1));

assert_eq!(response.status(), StatusCode::OK);

Expand Down Expand Up @@ -131,8 +133,9 @@ fn get_inscription() {

let (inscription_id, reveal) = inscribe(&rpc_server);

let response = TestServer::spawn_with_args(&rpc_server, &["--index-sats", "--enable-json-api"])
.json_request(format!("/inscription/{}", inscription_id));
let response =
TestServer::spawn_with_server_args(&rpc_server, &["--index-sats"], &["--enable-json-api"])
.json_request(format!("/inscription/{}", inscription_id));

assert_eq!(response.status(), StatusCode::OK);

Expand Down Expand Up @@ -217,7 +220,8 @@ fn get_inscriptions() {
create_wallet(&rpc_server);
let inscriptions = create_210_inscriptions(&rpc_server);

let server = TestServer::spawn_with_args(&rpc_server, &["--index-sats", "--enable-json-api"]);
let server =
TestServer::spawn_with_server_args(&rpc_server, &["--index-sats"], &["--enable-json-api"]);

let response = server.json_request("/inscriptions");
assert_eq!(response.status(), StatusCode::OK);
Expand Down Expand Up @@ -308,14 +312,10 @@ fn get_inscriptions_in_block() {

rpc_server.mine_blocks(1);

let server = TestServer::spawn_with_args(
let server = TestServer::spawn_with_server_args(
&rpc_server,
&[
"--index-sats",
"--enable-json-api",
"--first-inscription-height",
"0",
],
&["--index-sats", "--first-inscription-height", "0"],
&["--enable-json-api"],
);

// get all inscriptions from block 11
Expand Down Expand Up @@ -354,7 +354,8 @@ fn get_output() {
});
rpc_server.mine_blocks(1);

let server = TestServer::spawn_with_args(&rpc_server, &["--index-sats", "--enable-json-api"]);
let server =
TestServer::spawn_with_server_args(&rpc_server, &["--index-sats"], &["--enable-json-api"]);

let response = server.json_request(format!("/output/{}:0", txid));
assert_eq!(response.status(), StatusCode::OK);
Expand Down Expand Up @@ -399,8 +400,8 @@ fn get_block() {

rpc_server.mine_blocks(1);

let response =
TestServer::spawn_with_args(&rpc_server, &["--enable-json-api"]).json_request("/block/0");
let response = TestServer::spawn_with_server_args(&rpc_server, &[], &["--enable-json-api"])
.json_request("/block/0");

assert_eq!(response.status(), StatusCode::OK);

Expand Down
18 changes: 15 additions & 3 deletions tests/test_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,18 @@ pub(crate) struct TestServer {
}

impl TestServer {
pub(crate) fn spawn_with_args(rpc_server: &test_bitcoincore_rpc::Handle, args: &[&str]) -> Self {
pub(crate) fn spawn_with_args(
rpc_server: &test_bitcoincore_rpc::Handle,
ord_args: &[&str],
) -> Self {
Self::spawn_with_server_args(rpc_server, ord_args, &[])
}

pub(crate) fn spawn_with_server_args(
rpc_server: &test_bitcoincore_rpc::Handle,
ord_args: &[&str],
server_args: &[&str],
) -> Self {
let tempdir = TempDir::new().unwrap();
fs::write(tempdir.path().join(".cookie"), "foo:bar").unwrap();
let port = TcpListener::bind("127.0.0.1:0")
Expand All @@ -24,11 +35,12 @@ impl TestServer {
.port();

let child = Command::new(executable_path("ord")).args(format!(
"--rpc-url {} --bitcoin-data-dir {} --data-dir {} {} server --http-port {port} --address 127.0.0.1",
"--rpc-url {} --bitcoin-data-dir {} --data-dir {} {} server {} --http-port {port} --address 127.0.0.1",
rpc_server.url(),
tempdir.path().display(),
tempdir.path().display(),
args.join(" "),
ord_args.join(" "),
server_args.join(" "),
).to_args())
.env("ORD_INTEGRATION_TEST", "1")
.current_dir(&tempdir)
Expand Down
3 changes: 2 additions & 1 deletion tests/wallet/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ fn splitting_merged_inscriptions_is_possible() {

rpc_server.mine_blocks(1);

let server = TestServer::spawn_with_args(&rpc_server, &["--index-sats", "--enable-json-api"]);
let server =
TestServer::spawn_with_server_args(&rpc_server, &["--index-sats"], &["--enable-json-api"]);

let response = server.json_request(format!("/output/{}:0", reveal_txid));
assert_eq!(response.status(), StatusCode::OK);
Expand Down

0 comments on commit c6edde8

Please sign in to comment.