Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSON endpoint for status #2955

Merged
merged 3 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ use {
Witness,
},
bitcoincore_rpc::{Client, RpcApi},
chain::Chain,
chrono::{DateTime, TimeZone, Utc},
ciborium::Value,
clap::{ArgGroup, Parser},
Expand Down Expand Up @@ -84,6 +83,7 @@ use {
};

pub use self::{
chain::Chain,
fee_rate::FeeRate,
index::Index,
inscriptions::{Envelope, Inscription, InscriptionId},
Expand Down Expand Up @@ -115,7 +115,7 @@ macro_rules! tprintln {

mod arguments;
mod blocktime;
mod chain;
pub mod chain;
mod config;
mod decimal;
mod decimal_sat;
Expand Down
11 changes: 8 additions & 3 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -769,8 +769,13 @@ impl Server {
async fn status(
Extension(server_config): Extension<Arc<ServerConfig>>,
Extension(index): Extension<Arc<Index>>,
) -> ServerResult<PageHtml<StatusHtml>> {
Ok(index.status()?.page(server_config))
AcceptJson(accept_json): AcceptJson,
) -> ServerResult<Response> {
Ok(if accept_json {
Json(index.status()?).into_response()
} else {
index.status()?.page(server_config).into_response()
})
}

async fn search_by_query(
Expand Down
2 changes: 1 addition & 1 deletion src/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ mod rare;
mod rune;
mod runes;
pub mod sat;
mod status;
pub mod status;
mod transaction;

#[derive(Boilerplate)]
Expand Down
32 changes: 16 additions & 16 deletions src/templates/status.rs
Original file line number Diff line number Diff line change
@@ -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<u32>,
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<Utc>,
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<u32>,
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<Utc>,
pub transaction_index: bool,
pub unrecoverably_reorged: bool,
pub uptime: Duration,
}

impl PageContent for StatusHtml {
Expand Down
46 changes: 46 additions & 0 deletions tests/json_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<DateTime<Utc>>()
.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,
}
);
}
4 changes: 3 additions & 1 deletion tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
Loading