diff --git a/src/lib.rs b/src/lib.rs index 3805416a4e..55b2379d9d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -48,7 +48,6 @@ use { Witness, }, bitcoincore_rpc::{Client, RpcApi}, - chain::Chain, chrono::{DateTime, TimeZone, Utc}, ciborium::Value, clap::{ArgGroup, Parser}, @@ -84,6 +83,7 @@ use { }; pub use self::{ + chain::Chain, fee_rate::FeeRate, index::Index, inscriptions::{Envelope, Inscription, InscriptionId}, @@ -115,7 +115,7 @@ macro_rules! tprintln { mod arguments; mod blocktime; -mod chain; +pub mod chain; mod config; mod decimal; mod decimal_sat; diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index 3c3b1ec499..d767ba53d6 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -15,7 +15,7 @@ use { PreviewAudioHtml, PreviewCodeHtml, PreviewFontHtml, PreviewImageHtml, PreviewMarkdownHtml, PreviewModelHtml, PreviewPdfHtml, PreviewTextHtml, PreviewUnknownHtml, PreviewVideoHtml, RangeHtml, RareTxt, RuneHtml, RunesHtml, SatHtml, SatInscriptionJson, SatInscriptionsJson, - SatJson, StatusHtml, TransactionHtml, + SatJson, TransactionHtml, }, }, axum::{ @@ -769,8 +769,13 @@ impl Server { async fn status( Extension(server_config): Extension>, Extension(index): Extension>, - ) -> ServerResult> { - Ok(index.status()?.page(server_config)) + AcceptJson(accept_json): AcceptJson, + ) -> ServerResult { + Ok(if accept_json { + Json(index.status()?).into_response() + } else { + index.status()?.page(server_config).into_response() + }) } async fn search_by_query( diff --git a/src/templates.rs b/src/templates.rs index aae0b5f953..4a53f94836 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -47,7 +47,7 @@ mod rare; mod rune; mod runes; pub mod sat; -mod status; +pub mod status; mod transaction; #[derive(Boilerplate)] diff --git a/src/templates/status.rs b/src/templates/status.rs index 540e4a418f..2181cf2261 100644 --- a/src/templates/status.rs +++ b/src/templates/status.rs @@ -1,21 +1,21 @@ use super::*; -#[derive(Boilerplate)] -pub(crate) struct StatusHtml { - pub(crate) blessed_inscriptions: u64, - pub(crate) cursed_inscriptions: u64, - pub(crate) chain: Chain, - pub(crate) height: Option, - pub(crate) inscriptions: u64, - pub(crate) lost_sats: u64, - pub(crate) minimum_rune_for_next_block: Rune, - pub(crate) rune_index: bool, - pub(crate) runes: u64, - pub(crate) sat_index: bool, - pub(crate) started: DateTime, - pub(crate) transaction_index: bool, - pub(crate) unrecoverably_reorged: bool, - pub(crate) uptime: Duration, +#[derive(Boilerplate, Debug, PartialEq, Serialize, Deserialize)] +pub struct StatusHtml { + pub blessed_inscriptions: u64, + pub cursed_inscriptions: u64, + pub chain: Chain, + pub height: Option, + pub inscriptions: u64, + pub lost_sats: u64, + pub minimum_rune_for_next_block: Rune, + pub rune_index: bool, + pub runes: u64, + pub sat_index: bool, + pub started: DateTime, + pub transaction_index: bool, + pub unrecoverably_reorged: bool, + pub uptime: Duration, } impl PageContent for StatusHtml { diff --git a/tests/json_api.rs b/tests/json_api.rs index ab3dfb8e8e..2be51014f4 100644 --- a/tests/json_api.rs +++ b/tests/json_api.rs @@ -365,3 +365,49 @@ fn get_block() { } ); } + +#[test] +fn get_status() { + let rpc_server = test_bitcoincore_rpc::spawn(); + + create_wallet(&rpc_server); + + inscribe(&rpc_server); + + let response = + TestServer::spawn_with_server_args(&rpc_server, &["--index-sats"], &["--enable-json-api"]) + .json_request("/status"); + + assert_eq!(response.status(), StatusCode::OK); + + let mut status_json: StatusHtml = serde_json::from_str(&response.text().unwrap()).unwrap(); + + let dummy_started = "2012-12-12 12:12:12+00:00" + .parse::>() + .unwrap(); + + let dummy_uptime = Duration::from_secs(1); + + status_json.started = dummy_started; + status_json.uptime = dummy_uptime; + + pretty_assert_eq!( + status_json, + StatusHtml { + blessed_inscriptions: 1, + cursed_inscriptions: 0, + chain: Chain::Mainnet, + height: Some(2), + inscriptions: 1, + lost_sats: 0, + minimum_rune_for_next_block: Rune(99246114928149462), + rune_index: false, + runes: 0, + sat_index: true, + started: dummy_started, + transaction_index: false, + unrecoverably_reorged: false, + uptime: dummy_uptime, + } + ); +} diff --git a/tests/lib.rs b/tests/lib.rs index abc086ed83..0942ad1acf 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -7,13 +7,15 @@ use { blockdata::constants::COIN_VALUE, Network, OutPoint, Txid, }, + chrono::{DateTime, Utc}, executable_path::executable_path, ord::{ + chain::Chain, rarity::Rarity, subcommand::runes::RuneInfo, templates::{ block::BlockJson, inscription::InscriptionJson, inscriptions::InscriptionsJson, - output::OutputJson, sat::SatJson, + output::OutputJson, sat::SatJson, status::StatusHtml, }, Edict, InscriptionId, Rune, RuneId, Runestone, SatPoint, },