From 2672845a5cb4016fe4dac6e76e77bd219ea081dc Mon Sep 17 00:00:00 2001 From: raphjaph Date: Thu, 4 Jan 2024 00:16:06 +0100 Subject: [PATCH 1/2] Add JSON endpoint for status --- src/lib.rs | 4 ++-- src/subcommand/server.rs | 11 ++++++++--- src/templates.rs | 2 +- src/templates/status.rs | 32 ++++++++++++++++---------------- tests/json_api.rs | 33 +++++++++++++++++++++++++++++++++ tests/lib.rs | 3 ++- 6 files changed, 62 insertions(+), 23 deletions(-) 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..ea8f489920 100644 --- a/tests/json_api.rs +++ b/tests/json_api.rs @@ -365,3 +365,36 @@ 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 status_json: StatusHtml = serde_json::from_str(&response.text().unwrap()).unwrap(); + + pretty_assert_eq!(status_json.blessed_inscriptions, 1); + pretty_assert_eq!(status_json.cursed_inscriptions, 0); + pretty_assert_eq!(status_json.chain, Chain::Mainnet); + pretty_assert_eq!(status_json.height, Some(2)); + pretty_assert_eq!(status_json.inscriptions, 1); + pretty_assert_eq!(status_json.lost_sats, 0); + pretty_assert_eq!( + status_json.minimum_rune_for_next_block, + Rune(99246114928149462) + ); + pretty_assert_eq!(status_json.rune_index, false); + pretty_assert_eq!(status_json.runes, 0); + pretty_assert_eq!(status_json.sat_index, true); + pretty_assert_eq!(status_json.transaction_index, false); + pretty_assert_eq!(status_json.unrecoverably_reorged, false); +} diff --git a/tests/lib.rs b/tests/lib.rs index abc086ed83..cbe9f9a9a7 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -9,11 +9,12 @@ use { }, 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, }, From 8a5a357b832d51a74fde2f01da367921daac448e Mon Sep 17 00:00:00 2001 From: raphjaph Date: Thu, 4 Jan 2024 00:25:50 +0100 Subject: [PATCH 2/2] Use dummy values --- tests/json_api.rs | 43 ++++++++++++++++++++++++++++--------------- tests/lib.rs | 1 + 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/tests/json_api.rs b/tests/json_api.rs index ea8f489920..2be51014f4 100644 --- a/tests/json_api.rs +++ b/tests/json_api.rs @@ -380,21 +380,34 @@ fn get_status() { assert_eq!(response.status(), StatusCode::OK); - let status_json: StatusHtml = serde_json::from_str(&response.text().unwrap()).unwrap(); - - pretty_assert_eq!(status_json.blessed_inscriptions, 1); - pretty_assert_eq!(status_json.cursed_inscriptions, 0); - pretty_assert_eq!(status_json.chain, Chain::Mainnet); - pretty_assert_eq!(status_json.height, Some(2)); - pretty_assert_eq!(status_json.inscriptions, 1); - pretty_assert_eq!(status_json.lost_sats, 0); + 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.minimum_rune_for_next_block, - Rune(99246114928149462) + 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, + } ); - pretty_assert_eq!(status_json.rune_index, false); - pretty_assert_eq!(status_json.runes, 0); - pretty_assert_eq!(status_json.sat_index, true); - pretty_assert_eq!(status_json.transaction_index, false); - pretty_assert_eq!(status_json.unrecoverably_reorged, false); } diff --git a/tests/lib.rs b/tests/lib.rs index cbe9f9a9a7..0942ad1acf 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -7,6 +7,7 @@ use { blockdata::constants::COIN_VALUE, Network, OutPoint, Txid, }, + chrono::{DateTime, Utc}, executable_path::executable_path, ord::{ chain::Chain,