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

Implement /block/:query JSON endpoint #2423

Merged
merged 11 commits into from
Oct 26, 2023
21 changes: 16 additions & 5 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use {
super::*,
crate::page_config::PageConfig,
crate::templates::{
BlockHtml, ClockSvg, HomeHtml, InputHtml, InscriptionHtml, InscriptionJson,
BlockHtml, BlockJson, ClockSvg, HomeHtml, InputHtml, InscriptionHtml, InscriptionJson,
InscriptionsBlockHtml, InscriptionsHtml, InscriptionsJson, OutputHtml, OutputJson, PageContent,
PageHtml, PreviewAudioHtml, PreviewCodeHtml, PreviewImageHtml, PreviewMarkdownHtml,
PreviewModelHtml, PreviewPdfHtml, PreviewTextHtml, PreviewUnknownHtml, PreviewVideoHtml,
Expand Down Expand Up @@ -577,7 +577,8 @@ impl Server {
Extension(page_config): Extension<Arc<PageConfig>>,
Extension(index): Extension<Arc<Index>>,
Path(DeserializeFromStr(query)): Path<DeserializeFromStr<BlockQuery>>,
) -> ServerResult<PageHtml<BlockHtml>> {
accept_json: AcceptJson,
) -> ServerResult<Response> {
let (block, height) = match query {
BlockQuery::Height(height) => {
let block = index
Expand All @@ -602,16 +603,26 @@ impl Server {
let (featured_inscriptions, total_num) =
index.get_highest_paying_inscriptions_in_block(height, 8)?;

Ok(
Ok(if accept_json.0 {
Json(BlockJson::new(
block,
Height(height),
Self::index_height(&index)?,
total_num,
featured_inscriptions,
))
.into_response()
} else {
BlockHtml::new(
block,
Height(height),
Self::index_height(&index)?,
total_num,
featured_inscriptions,
)
.page(page_config, index.has_sat_index()?),
)
.page(page_config, index.has_sat_index()?)
.into_response()
})
}

async fn transaction(
Expand Down
4 changes: 2 additions & 2 deletions src/templates.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use {super::*, boilerplate::Boilerplate};

pub(crate) use {
block::BlockHtml,
block::{BlockHtml, BlockJson},
clock::ClockSvg,
home::HomeHtml,
iframe::Iframe,
Expand All @@ -22,7 +22,7 @@ pub(crate) use {
transaction::TransactionHtml,
};

mod block;
pub mod block;
mod clock;
mod home;
mod iframe;
Expand Down
30 changes: 30 additions & 0 deletions src/templates/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,36 @@ impl BlockHtml {
}
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct BlockJson {
pub hash: String,
pub target: String,
pub best_height: u64,
pub height: u64,
pub total_num_inscriptions: usize,
pub featured_inscriptions: Vec<InscriptionId>,
}

impl BlockJson {
pub(crate) fn new(
block: Block,
height: Height,
best_height: Height,
total_num_inscriptions: usize,
featured_inscriptions: Vec<InscriptionId>,
) -> Self {
Self {
hash: block.header.block_hash().to_string(),
target: BlockHash::from_raw_hash(Hash::from_byte_array(block.header.target().to_be_bytes()))
.to_string(),
height: height.0,
best_height: best_height.0,
total_num_inscriptions,
featured_inscriptions,
}
}
}

impl PageContent for BlockHtml {
fn title(&self) -> String {
format!("Block {}", self.height)
Expand Down
26 changes: 26 additions & 0 deletions tests/json_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,29 @@ fn json_request_fails_when_not_enabled() {

assert_eq!(response.status(), StatusCode::NOT_ACCEPTABLE);
}

#[test]
fn get_block() {
let rpc_server = test_bitcoincore_rpc::spawn();

rpc_server.mine_blocks(1);

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

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

let block_json: BlockJson = serde_json::from_str(&response.text().unwrap()).unwrap();

assert_eq!(
block_json,
BlockJson {
hash: String::from("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"),
target: String::from("0000000000000000000000000000000000000000000000000000ffff00000000"),
best_height: 1,
height: 0,
total_num_inscriptions: 0,
featured_inscriptions: vec![],
}
);
}
4 changes: 2 additions & 2 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use {
inscription_id::InscriptionId,
rarity::Rarity,
templates::{
inscription::InscriptionJson, inscriptions::InscriptionsJson, output::OutputJson,
sat::SatJson,
block::BlockJson, inscription::InscriptionJson, inscriptions::InscriptionsJson,
output::OutputJson, sat::SatJson,
},
SatPoint,
},
Expand Down